var compareItems= new Array();

function toggleCompareItem(sku){
  var foundIndex=false;
  for(i=0; i < compareItems.length ;i++){
    if(compareItems[i]==sku){
      foundIndex=i;
    }
  }
  if(foundIndex!==false){
    compareItems.splice(foundIndex,1);
    document.getElementById('cmp'+sku).checked=false;
  }
  else{
    if(compareItems.length < 3){
      compareItems.push(sku);
      document.getElementById('cmp'+sku).checked=true;
    }
    else{
      alert('Sorry, but you can only compare up to 3 items at a time.');
      document.getElementById('cmp'+sku).checked=false;
    }
  }
}

function submitCompare(){
  if(compareItems.length >1){
    url=base+'compare.php?compareSkus='+compareItems.join('|');
    document.location.href=url;
  }else{alert('Please select at least two products to compare.');return false;}
}

function initCompareItems(){
  itemArray=document.getElementsByName("cmpItem");
  for(i=0; i < itemArray.length ;i++){
    if(itemArray[i].checked==true){
      //toggleCompareItem(itemArray[i].value);
      //alert('pushItem='+itemArray[i].value);
      compareItems.push(itemArray[i].value);
    }
  }
  //alert(compareItems.length);
}

function checkCompareItems(){
  //alert('6compareItems length='+compareItems.length);
  if(compareItems.length > 0){
    for(i=0; i < compareItems.length ;i++){
      //alert(i+'='+compareItems[i]);
      el=document.getElementById('cmp'+compareItems[i]);
      if(el){el.checked=true;}
    }
  }
}

YAHOO.util.Event.onDOMReady(initCompareItems);