// All Ajax Requests are performed by a single php file and uses two parameters to control what it does.
var ajaxfunction = "zc_generic_ajax.php";
// When using GET you may get a cached result which is to be avoided if processing data which changes quickly, e.g. items in shopping cart.
var xmlDocPB; // Product Breakdown XML
var xmlDocSC; // Shopping Cart XML
// Variable for the quick product Finder
var optgender;
var optbrand;
var opttype;
var optsize;
var optprice;

function BuildMiniShoppingCart(mch,mci,mct,mcc)
{
  document.getElementById('minicartheader').innerHTML  = mch;
  document.getElementById('minicartitems').innerHTML  = mci;
  document.getElementById('minicarttotal').innerHTML  = mct;
  document.getElementById('minicartcheckout').innerHTML  = mcc;
}


function getHTTPObject()
{
    var xmlhttp;
    if(window.XMLHttpRequest) {xmlhttp = new XMLHttpRequest();}
    else if (window.ActiveXObject) {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); if (!xmlhttp) {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}}
    return xmlhttp;
}
// In order to avoid this, add a unique parameter/value to the url
function UID()
{var uniqueid = "&ui=" + Math.random(); return  uniqueid;}

function genderchange()
{
  if (document.getElementById('quick_gender').value  == '')
  {
    // Default selected so disable all other drop down
    document.getElementById('quick_manufacturers_id').innerHTML = '';
    document.getElementById('quick_categories_id').innerHTML = '';
    document.getElementById('quick_size').innerHTML = '';
    document.getElementById('quick_price').innerHTML = '';
    
    document.getElementById('quick_manufacturers_id').disabled = true;
    document.getElementById('quick_categories_id').disabled = true;
    document.getElementById('quick_size').disabled = true;
    document.getElementById('quick_price').disabled = true;
  }
  else
  {
    // Gender is equal to M or W so populate the the dropdown lists
    // Disable brands and categories temporarily
    document.getElementById('quick_manufacturers_id').disabled = true;
    document.getElementById('quick_categories_id').disabled = true;
    genderchange_send()
  }
}
function genderchange_send()
{
    // we need to find out what parameters we have got.
    qpf_get_selections();
    var para = '?type=quickfinderchange&gender='+optgender+'&brand='+optbrand+'&cat='+opttype+'&size='+optsize+'&price='+optprice;
    ajax_genderchange = getHTTPObject(); // Build Ajax Object
    ajax_genderchange.open("GET", ajaxfunction+para+UID(), true); // Build Request
    ajax_genderchange.onreadystatechange = genderchange_process; // Define call-back routine
    ajax_genderchange.send(); // Send the request
}
function genderchange_process()
{
    if (ajax_genderchange.readyState == 4 )
    {
      // IE has an issue populating <select> elements with options so we have to recreate the whole <select> element
      // We are now going to learn how to get an XML response and then traverse through the XML returned.
      xmlDocPB=ajax_genderchange.responseXML;
      Build_Brand_Options();      
      Build_Type_Options();
      Build_Size_Options();
      Build_Price_Options();      
    }
}
function brandchange()
{
    brandchange_send();
}
function brandchange_send()
{
    qpf_get_selections();
    var para = '?type=quickfinderchange&gender='+optgender+'&brand='+optbrand+'&cat='+opttype+'&size='+optsize+'&price='+optprice;
    ajax_brandchange = getHTTPObject(); // Build Ajax Object
    ajax_brandchange.open("GET", ajaxfunction+para+UID(), true); // Build Request
    ajax_brandchange.onreadystatechange = brandchange_process; // Define call-back routine
    ajax_brandchange.send(); // Send the request
}
function brandchange_process()
{
    if (ajax_brandchange.readyState == 4)
    {
      // IE has an issue populating <select> elements with options so we have to recreate the whole <select> element
      // We are now going to learn how to get an XML response and then traverse through the XML returned.
      xmlDocPB=ajax_brandchange.responseXML;
      Build_Brand_Options();      
      Build_Type_Options();
      Build_Size_Options();
      Build_Price_Options();
    }
}

function typechange()
{
    typechange_send();
}
function typechange_send()
{
    qpf_get_selections();
    var para = '?type=quickfinderchange&gender='+optgender+'&brand='+optbrand+'&cat='+opttype+'&size='+optsize+'&price='+optprice;
    ajax_typechange = getHTTPObject(); // Build Ajax Object
    ajax_typechange.open("GET", ajaxfunction+para+UID(), true); // Build Request
    ajax_typechange.onreadystatechange = typechange_process; // Define call-back routine
    ajax_typechange.send(); // Send the request
}
function typechange_process()
{
//    if (ajax_typechange.readyState == 4 && ajax_typechange.status==200)
    if (ajax_typechange.readyState == 4 )
    {
      // IE has an issue populating <select> elements with options so we have to recreate the whole <select> element
      // We are now going to learn how to get an XML response and then traverse through the XML returned.
      xmlDocPB=ajax_typechange.responseXML;
      Build_Brand_Options();      
      Build_Type_Options();
      Build_Size_Options();
      Build_Price_Options();
    }
}
function sizechange()
{
    typechange_send();
}
function sizechange_send()
{
    qpf_get_selections();
    var para = '?type=quickfinderchange&gender='+optgender+'&brand='+optbrand+'&cat='+opttype+'&size='+optsize+'&price='+optprice;
    ajax_sizechange = getHTTPObject(); // Build Ajax Object
    ajax_sizechange.open("GET", ajaxfunction+para+UID(), true); // Build Request
    ajax_sizechange.onreadystatechange = sizechange_process; // Define call-back routine
    ajax_sizechange.send(); // Send the request
}
function sizechange_process()
{
    if (ajax_sizechange.readyState == 4)
    {
      // IE has an issue populating <select> elements with options so we have to recreate the whole <select> element
      // We are now going to learn how to get an XML response and then traverse through the XML returned.
      xmlDocPB=ajax_sizechange.responseXML;
      Build_Brand_Options();      
      Build_Type_Options();
      Build_Size_Options();
      Build_Price_Options();
    }
}

function pricechange()
{
    typechange_send();
}
function pricechange_send()
{
    qpf_get_selections();
    var para = '?type=quickfinderchange&gender='+optgender+'&brand='+optbrand+'&cat='+opttype+'&size='+optsize+'&price='+optprice;
    ajax_pricechange = getHTTPObject(); // Build Ajax Object
    ajax_pricechange.open("GET", ajaxfunction+para+UID(), true); // Build Request
    ajax_pricechange.onreadystatechange = pricechange_process; // Define call-back routine
    ajax_pricechange.send(); // Send the request
}
function pricechange_process()
{
    if (ajax_pricechange.readyState == 4)
    {
      // IE has an issue populating <select> elements with options so we have to recreate the whole <select> element
      // We are now going to learn how to get an XML response and then traverse through the XML returned.
      xmlDocPB=ajax_pricechange.responseXML;
      Build_Brand_Options();      
      Build_Type_Options();
      Build_Size_Options();
      Build_Price_Options();
    }
}

function qpf_get_selections()
{
  optgender = document.getElementById('quick_gender').value;
  optbrand  = document.getElementById('quick_manufacturers_id').value;
  opttype   = document.getElementById('quick_categories_id').value;
  optsize   = document.getElementById('quick_size').value;
  optprice  = document.getElementById('quick_price').value; 
}

function Build_Brand_Options()
{
  // Get the relevant select element
  var brandid=document.getElementById("quick_manufacturers_id");
  // as we are using the Dom functions to add options we need to clear the options before we start to add new ones.
  document.getElementById('quick_manufacturers_id').innerHTML = '';
  // Add the default options
  var brandoption = document.createElement('option');brandoption.text='Any';brandoption.value='';try {brandid.add(brandoption,null);} catch(ex) {brandid.add(brandoption);}
  // Now extract all of the brand option <brandopt> elements.
  var brandopt=xmlDocPB.getElementsByTagName("brandopt");
  for (i=0;i<brandopt.length;i++)
  {
    var brandoption = document.createElement('option');
    // The counts reflect number of products times sizes available which is wrong. So do not show the counts yet.
    //var titlecount = brandopt[i].getElementsByTagName("title")[0].childNodes[0].nodeValue+' ('+ brandopt[i].getElementsByTagName("count")[0].childNodes[0].nodeValue+')';
    var titlecount = brandopt[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
    brandoption.text=titlecount;
    brandoption.value=brandopt[i].getElementsByTagName("value")[0].childNodes[0].nodeValue;
    if (brandopt[i].getElementsByTagName("selected")[0].childNodes[0].nodeValue =='Y') {brandoption.selected=true;} else {brandoption.selected=false;}
    try {brandid.add(brandoption,null);} catch(ex) {brandid.add(brandoption);}
  }
  document.getElementById('quick_manufacturers_id').disabled = false;
}
function Build_Type_Options()
{
  // Get the relevant select element
  var typeid=document.getElementById("quick_categories_id");
  // as we are using the Dom functions to add options we need to clear the options before we start to add new ones.
  document.getElementById('quick_categories_id').innerHTML = '';
  // Add the default options
  var typeoption = document.createElement('option');typeoption.text='Any';typeoption.value='';try {typeid.add(typeoption,null);} catch(ex) {typeid.add(typeoption);}
  // Now extract all of the Type option <brandopt> elements.
  var typeopt=xmlDocPB.getElementsByTagName("typeopt");
  for (i=0;i<typeopt.length;i++)
  {
    var typeoption = document.createElement('option');
    // The counts reflect number of products times sizes available which is wrong. So do not show the counts yet.
    //var titlecount = typeopt[i].getElementsByTagName("title")[0].childNodes[0].nodeValue+' ('+ typeopt[i].getElementsByTagName("count")[0].childNodes[0].nodeValue+')';
    var titlecount = typeopt[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
    typeoption.text=titlecount;
    typeoption.value=typeopt[i].getElementsByTagName("value")[0].childNodes[0].nodeValue;
    if (typeopt[i].getElementsByTagName("selected")[0].childNodes[0].nodeValue =='Y') {typeoption.selected=true;} else {typeoption.selected=false;}
    try {typeid.add(typeoption,null);} catch(ex) {typeid.add(typeoption);}
  }
  document.getElementById('quick_categories_id').disabled = false;
}
function Build_Size_Options()
{
  // Get the relevant select element
  var sizeid=document.getElementById("quick_size");
  // as we are using the Dom functions to add options we need to clear the options before we start to add new ones.
  document.getElementById('quick_size').innerHTML = '';
  // Add the default options
  var sizeoption = document.createElement('option');sizeoption.text='Any';sizeoption.value='';try {sizeid.add(sizeoption,null);} catch(ex) {sizeid.add(sizeoption);}
  // Now extract all of the Type option <brandopt> elements.
  var sizeopt=xmlDocPB.getElementsByTagName("sizeopt");
  for (i=0;i<sizeopt.length;i++)
  {
    var sizeoption = document.createElement('option');
    // The counts reflect number of products times sizes available which is wrong. So do not show the counts yet.
    //var titlecount = sizeopt[i].getElementsByTagName("title")[0].childNodes[0].nodeValue+' ('+ sizeopt[i].getElementsByTagName("count")[0].childNodes[0].nodeValue+')';
    var titlecount = sizeopt[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
    sizeoption.text=titlecount;
    sizeoption.value=sizeopt[i].getElementsByTagName("value")[0].childNodes[0].nodeValue;
    if (sizeopt[i].getElementsByTagName("selected")[0].childNodes[0].nodeValue =='Y') {sizeoption.selected=true;} else {sizeoption.selected=false;}
    try {sizeid.add(sizeoption,null);} catch(ex) {sizeid.add(sizeoption);}
  }
  document.getElementById('quick_size').disabled = false;
}
function Build_Price_Options()
{
  // Get the relevant select element
  var priceid=document.getElementById("quick_price");
  // as we are using the Dom functions to add options we need to clear the options before we start to add new ones.
  document.getElementById('quick_price').innerHTML = '';  
  // Add the default options
  var priceoption = document.createElement('option');priceoption.text='Any';priceoption.value='';try {priceid.add(priceoption,null);} catch(ex) {priceid.add(priceoption);}
  // Now extract all of the Type option <brandopt> elements.
  var priceopt=xmlDocPB.getElementsByTagName("priceopt");
  for (i=0;i<priceopt.length;i++)
  {
    var priceoption = document.createElement('option');
    // The counts reflect number of products times sizes available which is wrong. So do not show the counts yet.
    //var titlecount = priceopt[i].getElementsByTagName("title")[0].childNodes[0].nodeValue+' ('+ priceopt[i].getElementsByTagName("count")[0].childNodes[0].nodeValue+')';
    var titlecount = priceopt[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
    priceoption.text=titlecount;
    priceoption.value=priceopt[i].getElementsByTagName("value")[0].childNodes[0].nodeValue;
    if (priceopt[i].getElementsByTagName("selected")[0].childNodes[0].nodeValue =='Y') {priceoption.selected=true;} else {priceoption.selected=false;}
    try {priceid.add(priceoption,null);} catch(ex) {priceid.add(priceoption);}
  }
  document.getElementById('quick_price').disabled = false;
}

function ajb_add_cart_send(PID,CID)
{
  // validate the quantity
  var qty=document.getElementById('cart_quantity').value;
  if (isNaN(qty)){alert('Quantity Must Be a Whole Number greater than zero');document.getElementById('cart_quantity').value='1';return;}
  if (parseInt(qty) < 0 ){alert('Quantity Must Be greater than zero');document.getElementById('cart_quantity').value='1';return;}
  qty = parseInt(qty);
   
  // we need to identify what attributes have been selected. These can be radio buttons or select options.
  var frm_element = document.getElementById('product_details');
  var attribs='';
  var attid='';
  var attname 

  // Now get the attributes.
  for(i=0; i<frm_element.elements.length; i++)
  {
    if(frm_element.elements[i].type=="select-one" || frm_element.elements[i].type=="radio")
    {
      // at the moment all names are formated as id[x] and for backward compatibility i need to keep then like thst but for Ajax we need to oik out the number.
      attid = frm_element.elements[i].name.replace('id[','');
      attid = attid.replace(']','');
      ii = i+1;
      attribs = attribs+'&a'+ii+'k='+attid+'&a'+ii+'v='+frm_element.elements[i].value;
    }
  }
  var para = '?type=adjustcart&PID='+PID+'&CID='+CID+'&adjqty='+qty+attribs;
  ajax_add_cart = getHTTPObject(); // Build Ajax Object
  ajax_add_cart.open("GET", ajaxfunction+para+UID(), true); // Build Request
  ajax_add_cart.onreadystatechange = add_cart_process; // Define call-back routine
  ajax_add_cart.send(); // Send the request
  
}
function add_cart_process()
{
    if (ajax_add_cart.readyState == 4)
    {
      xmlDocSC=ajax_add_cart.responseXML;
      build_mini_shopping_carts();
      update_product_info();      
    }
}


function ajb_adj_cart_send(PID,CID,adjval,PName,attribs)
{
  if (adjval==0 || ((parseInt(document.getElementById('cart_quantity_'+PID).innerHTML) + parseInt(adjval) == 0)))
  {
    var answer = confirm("Are you sure that you want to remove "+PName+" from your basket");
    if (!answer) {return;} else {adjval=0;}
  }
  var para = '?type=adjustcart&PID='+PID+'&CID='+CID+'&adjqty='+adjval+'&attributes='+attribs;
  ajax_adj_cart = getHTTPObject(); // Build Ajax Object
  ajax_adj_cart.open("GET", ajaxfunction+para+UID(), true); // Build Request
  ajax_adj_cart.onreadystatechange = adj_cart_process; // Define call-back routine
  ajax_adj_cart.send(); // Send the request
  //document.getElementById('cart_quantity['+PID+']').value = newqty;
}
function adj_cart_process()
{
    if (ajax_adj_cart.readyState == 4)
    {
      xmlDocSC=ajax_adj_cart.responseXML;
      build_mini_shopping_carts();
      update_shopping_cart();      
    }
}

function get_cart_details()
{
  var para = '?type=get_cart_details';
  ajax_get_cart_details = getHTTPObject(); // Build Ajax Object
  ajax_get_cart_details.open("GET", ajaxfunction+para+UID(), true); // Build Request
  ajax_get_cart_details.onreadystatechange = get_cart_details_process; // Define call-back routine
  ajax_get_cart_details.send(); // Send the request
}
function get_cart_details_process()
{
    if (ajax_get_cart_details.readyState == 4)
    {
      xmlDocMSC=ajax_get_cart_details.responseXML;
      build_mini_shopping_carts();
      update_shopping_cart();
      
    }
}

function build_mini_shopping_carts()
{
    document.getElementById('minicartitems').innerHTML = xmlDocSC.getElementsByTagName("items")[0].childNodes[0].nodeValue;
    document.getElementById('minicarttotal').innerHTML = xmlDocSC.getElementsByTagName("value")[0].childNodes[0].nodeValue;
    // Now that the mini cart has been updated we need to let the user know that something has happenned.
    // Hide or show checkout link
    if (xmlDocSC.getElementsByTagName("cartitems")[0].childNodes[0].nodeValue !=0)
    {
      document.getElementById('minicartcheckoutrow').className = 'visible';
    }
}

function update_shopping_cart()
{
  // Lets check to see if an item was removed
  var usc=xmlDocSC.getElementsByTagName("productremoved");
  var asc=xmlDocSC.getElementsByTagName("productupdated");
  // Lets see if we can clear the stocklevel warning.
  var slw = xmlDocSC.getElementsByTagName("showstockwarning")[0].childNodes[0].nodeValue;
  if (slw=='N' && document.getElementById("markProductOutOfStock"))
  {document.getElementById('markProductOutOfStock').innerHTML='';}
  else
  {document.getElementById('markProductOutOfStock').innerHTML='Products marked with Insufficient Stock are out of stock or there are not enough in stock to fill your order.<br />Please change the quantity of the products marked with (Insufficient Stock). Thank you';}
  if (usc.length > 0) 
  {
    // Maximun of one id returned, so we can access it directly
    var scmaintable = document.getElementById('scmaintable'); // Get the main shopping cart table object.
    var pdeleted = 'scprodrow_'+usc[0].getElementsByTagName("productid")[0].childNodes[0].nodeValue;
    // Now get the rows index in the table and remove the entire row.
    var prow = document.getElementById(pdeleted).rowIndex;
    scmaintable.deleteRow(prow);
    var answer = alert("Your basket has been updated.");
  }
  else if  (asc.length > 0) // We will only update the shopping cart a products is deleted (reduced to zero) or increased 
  {
    var pqty = 'cart_quantity_'+asc[0].getElementsByTagName("productid")[0].childNodes[0].nodeValue;
    var pcost = 'cart_cost_'+asc[0].getElementsByTagName("productid")[0].childNodes[0].nodeValue;
    var pswid = 'stock_warning_'+asc[0].getElementsByTagName("productid")[0].childNodes[0].nodeValue;
    var psw=asc[0].getElementsByTagName("productstockwarning")[0].childNodes[0].nodeValue;
    // Check to see if we need a product stock warning
    if (psw != 'N')
    {document.getElementById(pswid).innerHTML = asc[0].getElementsByTagName("productstockwarning")[0].childNodes[0].nodeValue;}
    else
    {document.getElementById(pswid).innerHTML = '';}
    
    
    document.getElementById(pqty).innerHTML = asc[0].getElementsByTagName("productqty")[0].childNodes[0].nodeValue;
    document.getElementById(pcost).innerHTML = asc[0].getElementsByTagName("productcost")[0].childNodes[0].nodeValue;

    var answer = alert("Your basket has been updated.");
  }
  // Now update the shopping Cart Totals
  document.getElementById('sctotalitems').innerHTML = xmlDocSC.getElementsByTagName("items")[0].childNodes[0].nodeValue;
  document.getElementById('cartSubTotal').innerHTML = xmlDocSC.getElementsByTagName("value")[0].childNodes[0].nodeValue;
  //document.getElementById('sctotalweight').innerHTML = xmlDocSC.getElementsByTagName("weight")[0].childNodes[0].nodeValue;
  // Now alert the visitor
  
}
function update_product_info()
{
  var asc=xmlDocSC.getElementsByTagName("productupdated");
  if (asc.length > 0)
  {
    var pname=asc[0].getElementsByTagName("prodname")[0].childNodes[0].nodeValue;
    var pqty=asc[0].getElementsByTagName("adjquantity")[0].childNodes[0].nodeValue;
    if (parseInt(pqty) < 2){h= ' Has '} else {h= ' Have '} 
  } 
  var answer = alert(pqty+' '+pname+h+"been added to your basket.");
}
function annimate_element(elementid)
{
  
  
}

