function getXmlHttpObj()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  return xmlHttp;
}


function stateChanged(xmlHttp, productSelectId, imageId, promoId)
{  
    xmlHttp.onreadystatechange = function(){
     if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     {
        var products = new Array(); 
        products = eval(xmlHttp.responseText);
        var element = document.getElementById(productSelectId);
        element.options.length = 0;
        var alertstr = "";
        for(var i=0; i<products.length; i++){
          // create a new option with two values in one: the id and the image filename.
          // the two values will be sperated by a ',': "[id],[imageFilename]"
          // before the submission of the form a script will keep only the first value
          var elementOption = new Option(products[i][0], products[i][1]+","+products[i][2]);
          alertstr += "---("+promoId+"/"+products[i][1]+")---" ;
          if(promoId!=null && products[i][1]==promoId){
                elementOption.selected = true;
          } 
          element.options[element.length] = elementOption;
          
        }
        //alert(alertstr);
        // if there aren't any products give a message (with no value)
        if(products.length==0){
          var elementOption = new Option("Category has no products", " , ");
          element.options[element.length] = elementOption;
        }
     }
     else {
             //alert(xmlHttp.status);
     }
     getProductImage(productSelectId, imageId);
   }
}

/* Populates the product select menu according to the selected value of the category list */
function getProductList(categorySelectId, productSelectId, imageId, promoId)
{     
  var cSelect = document.getElementById(categorySelectId);     
  var cid =  cSelect.options[cSelect.selectedIndex].value;  // selected value (category id)        
  xmlHttp=getXmlHttpObj();    
  stateChanged(xmlHttp, productSelectId, imageId, promoId);
  url="getproductsforpromos.php";
  url=url+"?cid="+cid;
  xmlHttp.open("GET",url,true) ;
  xmlHttp.send(null);
}

function getProductImage(productSelectId, imageId){
  var pSelect = document.getElementById(productSelectId);
  if(pSelect.selectedIndex>-1){
    var pValues =  pSelect.options[pSelect.selectedIndex].value.split(",");  // selected value(s) (array) 
    var imageFilename = pValues[1];
    var imageUrl = "";
    if(imageFilename!=" "){
        imageUrl =  "img/products/"+imageFilename;  
        document.getElementById(imageId).innerHTML = "<img src=\""+imageUrl+"\" alt=\"\" >";
    } else{
         document.getElementById(imageId).innerHTML = "";
    }
  }else{
     document.getElementById(imageId).innerHTML = "";
  }
}

function checkPromoForm(){
  if(document.promosForm.pSelect0.value==" , " || document.promosForm.pSelect1.value==" , "){
    alert('One or both of your selections are not valid. Please try again.');
    return false;
  }
  var details0 = document.promosForm.pSelect0.value.split(",");
  document.promosForm.id0.value = details0[0]; 
  var details1 = document.promosForm.pSelect1.value.split(",");
  document.promosForm.id1.value = details1[0];
  return true;
}