var lform=document.getElementById("listings");
var shopping_cart=true;
var d_name='';
var d_spec='';
var d_asse='';

function add_to_cart(cat,qty,siz,sub_parts,title,did,name,spec,asse,psid)
{
  if(shopping_cart)
  {
    qty=new String(qty);
    shopping_cart=false;
    var updCart=false;
    var hasItems=false;
    var cartHTML='';
    var elem;
    elem=document.createElement('script');
    elem.src=rootpath+'/index.php?id=scengine&next=jsadd&item='+escape(cat)+'&qty='+escape(qty)+'&siz='+escape(siz)+'&sub_parts='+escape(sub_parts)+'&name='+escape(name)+'&spec='+escape(spec)+'&asse='+escape(asse)+'&psid='+escape(psid);
    var head = document.getElementsByTagName('head').item(0);
    var rbtn=document.getElementById('reqbutton');
    var rtxt=document.getElementById('reqtext');
    var cart=document.getElementById('shopping_cart');
    head.appendChild(elem);
    var upd=(new String(did)).split(',');
    var updt=(new String(title)).split('::');
    var updq=(new String(qty)).split(',');
    for(i=0;i<upd.length;i++)
    {
      try {
      upd[i]=upd[i].split('_');
      upd[i]=upd[i][0]+'_'+upd[i][1];
      if(x=document.getElementById(upd[i]))
      {
        if(updq[i]>0)
        {
          x.innerHTML='<table><tr valign=top><td class=sc_qty>'+updq[i]+'</td><td class=sc_item>'+updt[i]+'</td></tr></table>';
          hasItems=true;
        }
        else
        {
          x.innerHTML='';
        }
      }
      else
      {
        if(updq[i]>0)
        {
          updCart=true;
          hasItems=true;
          cartHTML+='<div id='+upd[i]+'><table><tr valign=top><td class=sc_qty>'+updq[i]+'</td><td class=sc_item>'+updt[i]+'</td></tr></table></div>';
        }
      }
      } catch(e) { }
    }
    disable_screen();
    if(hasItems)
    {
      rbtn.style.display='';
      rbtn.style.visibility='visible';
      rtxt.innerHTML='';
      if(updCart)
        cart.innerHTML+=cartHTML;
    }
    if(cart.innerHTML.indexOf('table')==-1&&cart.innerHTML.indexOf('TABLE')==-1)
    {
      alert(cart.innerHTML);
      rbtn.style.display='none';
      rbtn.style.visibility='hidden';
      rtxt.innerHTML='<!--No Requests Added-->';
    }
    setTimeout('enable_screen()', 1000);
  }
}
function hide_panel_info(catnum,cols,width)
{
  var x=document.getElementById('activation_num_'+catnum);
  for(i=0;i<x.value;i++)
  {
    z=document.getElementById('table_'+catnum+'_'+i);
    z.width=width;
    for(j=0;j<cols;j++)
    {
      y=document.getElementById('data_'+catnum+'_'+j+'_'+i);
      y.style.display='none';
      y.style.visibility='hidden';
    }
  }
}
function show_panel_info(catnum,cols,width)
{
  var x=document.getElementById('activation_num_'+catnum);
  for(i=0;i<x.value;i++)
  {
    z=document.getElementById('table_'+catnum+'_'+i);
    z.width=width;
    for(j=0;j<cols;j++)
    {
      y=document.getElementById('data_'+catnum+'_'+j+'_'+i);
      y.style.visibility='visible';
      y.style.display='block';
    }
  }
}

function show_prev_order_detail(oid)
{
  x=document.getElementById('details_'+oid);
  x.style.visibility='visible';
  x.style.display='block';
}

function hide_prev_order_detail(oid)
{
  x=document.getElementById('details_'+oid);
  x.style.visibility='hidden';
  x.style.display='none';
}

function disable_screen()
{
  var x,y;
  if (self.innerHeight) // all except Explorer
  {
        x = self.innerWidth;
        y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
        // Explorer 6 Strict Mode
  {
        x = document.documentElement.clientWidth;
        y = document.documentElement.clientHeight;
  }
  else if (document.body) // other Explorers
  {
        x = document.body.clientWidth;
        y = document.body.clientHeight;
  }

  var a=document.getElementById('disable_screen');
  a.style.visibility='visible';
  a.style.display='block';
  a.style.height=y+'px';
  a.style.width=x+'px';
  var b=document.getElementById('loading');
  b.style.visibility='visible';
  b.style.display='block';
  b.style.top=Math.round(y/2-100/2)+'px';
  b.style.left=Math.round(x/2-150/2)+'px';
}

function enable_screen()
{
  var x=document.getElementById('disable_screen');
  x.style.visbility='hidden';
  x.style.display='none';
  var y=document.getElementById('loading');
  y.style.visbility='hidden';
  y.style.display='none';
}

function remove_from_list(from_list,from_pool)
{
  var fl=document.getElementById(from_list);
  var fp=document.getElementById(from_pool);
  var fpl=new String(fp.value).split(",")
  var newfpl=new Array;
  if(fl.selectedIndex!=-1&&fl.value!='')
  {
    for(i=0,j=0;i<fpl.length;i++)
    {
      if(fl.value!=fpl[i])
        newfpl[j++]=fpl[i];
    }
    fl.remove(fl.selectedIndex);
    fp.value=newfpl.join(",");
  }
}

function add_to_list(to_list,from_list,to_pool)
{
  var tl=document.getElementById(to_list);
  var fl=document.getElementById(from_list);
  var tp=document.getElementById(to_pool);
  var tpl=new String(tp.value).split(",")
  if(fl.selectedIndex!=-1&&fl.value!=''&&get_index(tpl,fl.value)<0)
  {
    tpl[tp.value.length==0?0:tpl.length]=fl.value;
    tp.value=tpl.join(",");
    newopt=document.createElement('option');
    newopt.text=fl.options[fl.selectedIndex].text;
    newopt.value=fl.options[fl.selectedIndex].value;
    try{ tl.add(newopt,null); }
    catch (e) { tl.add(newopt,Number(-1)); }
    
  }
}

function get_index(arr,item)
{
  for(i=0;i<arr.length;i++)
    if(item==arr[i])
      return i;
  return -1;
}

function move_item_up(id,cpid)
{
  var sel=document.getElementById(id);
  var cpl=document.getElementById(cpid);
  var retval=true;

  //DETECT IF ITEM IS SELECTED
  if(sel.selectedIndex==-1)
  { retval=false; code=1; }

  //DETECT IF WE ARE AT THE TOP OF THE LIST
  if(sel.selectedIndex==0)
  { retval=false; code=2; }
 
  if(retval)
  {
    swap_options(cpl,sel,sel.selectedIndex,sel.selectedIndex-1); 
    sel.selectedIndex-=1; 
  }
  
  if(!retval)
  {
    if(code==1)
      alert("You must select an Item to move");
    if(code==2)
      alert("You cannot move this item up anymore");
  }
  return retval;
}

function move_item_down(id,cpid)
{
  var sel=document.getElementById(id);
  var cpl=document.getElementById(cpid);
  var retval=true;
 
  //DETECT IF ITEM IS SELECTED
  if(sel.selectedIndex==-1)
  { retval=false; code=1; }

  //DETECT IF WE ARE AT THE END
  if(sel.selectedIndex==sel.options.length-1)
  { retval=false; code=2; }

  if(retval)
  { 
    swap_options(cpl,sel,sel.selectedIndex,sel.selectedIndex+1);
    sel.selectedIndex+=1; 
  }

  if(!retval)
  {
    if(code==1)
      alert("You must select an Item to move");
    if(code==2)
      alert("You cannot move this item down anymore");
  }
  return retval;
}

function swap_options(olist,list,a,b)
{
  var tmp_text;
  var tmp_value;
  tmp_text=list.options[a].text;
  tmp_value=list.options[a].value;
  list.options[a].text=list.options[b].text;
  list.options[a].value=list.options[b].value;
  list.options[b].text=tmp_text;
  list.options[b].value=tmp_value;
  
  var s=new String(olist.value);
  var tmp;
  s=s.split(",");
  tmp=s[a];
  s[a]=s[b];
  s[b]=tmp;
  olist.value=s.join(",");
}

