window.onload = initForm;

function initForm() { 
  external_links();
  if(document.getElementById('delivery_country')){
     document.getElementById('delivery_country').onchange = getShipping;       
  } 
  if(document.getElementById('delivery_country') && document.getElementById('delivery_country').value != ''){  
     getShipping();       
  } 
  if(document.getElementById('payment_option1')){
    document.getElementById('payment_option1').onclick = gatewayHandler;
    document.getElementById('payment_option2').onclick = gatewayHandler;
  }
}

// Open links in new window
function external_links() { 
  if(!document.getElementsByTagName) {
    return;
  } else { 
    var anchors = document.getElementsByTagName("a"); 
    for (var i=0; i<anchors.length; i++) { 
      var anchor = anchors[i]; 
      if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "external nofollow") { 
        anchor.target = "_blank";
      }
    }
  } 
}


// Switch product image
function switch_product_image(image, title) {  
  var pImage;
  
  pImage = document.getElementById('product_image_src');
  
  pImage.src = 'images/products/main/' + image;
  pImage.title = title;
}

// Reset product image
function reset_product_image(image, title) {
  var pImage;
  
  pImage = document.getElementById('product_image_src');
  pImage.src = 'images/products/main/' + image;
  pImage.title = title;
}

function getPrice(phpFile, objID) {
  var obj = document.getElementById(objID);
  var title = new Array();
  var value = new Array();
  xmlhttp.open("GET", phpFile);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      obj.innerHTML = xmlhttp.responseText;
      if(obj == 'delivery_menu'){
       document.getElementById('delivery_method').value = document.getElementById('shipping_menu').options[document.getElementById('shipping_menu').selectedIndex].value;
      }
    }
  }  
  xmlhttp.send(null);  
}

function updatePrice(prodID, menuID, variantType) {
  if(document.getElementById('variantsIDs_'+prodID)){
    var variantsIDs = document.getElementById('variantsIDs').value;
    //Get the IDs of the variants
    variantID = variantsIDs.split('|');
    var variantsValues = '';
    //Loop through the variants based on their IDs and get the selected values
    for(i=0; i<variantID.length; i++){
      if(document.getElementById('option_' + variantID[i] + '_' + prodID)){
        variantValue = document.getElementById('option_' + variantID[i] + '_' + prodID).value;   
        variantsValues = variantsValues + variantValue + "|";   
      }
    }   
  } 
  if(document.getElementById('globalVariantsIDs_'+prodID)){       
    var globalVariantsIDs = document.getElementById('globalVariantsIDs_'+prodID).value;
    //Get the IDs of the global variants     
    globalVariantID = globalVariantsIDs.split('|');
    var globalVariantsValues = '';
     //Loop through the global variants based on their IDs and get the selected values
    for(i=0; i<globalVariantID.length; i++){
      if(document.getElementById('option_' + globalVariantID[i] + '_' + prodID)){
        globalVariantValue = document.getElementById('option_' + globalVariantID[i] + '_' + prodID).value;
        globalVariantsValues = globalVariantsValues + globalVariantValue + "|";   
      }
    }           
  }       
 
  getPrice("logicajax/updateProductPrice.php?p=" + prodID + "&variantsIDs='" + variantsIDs + "'&globVariantsIDs='" + globalVariantsIDs
                       + "'&variantsValues='" + variantsValues + "'&globVariantsValues='" + globalVariantsValues + "'", "product_price_" + prodID);
}

function getShipping() {
  var country = document.getElementById('delivery_country').value;
  if(document.getElementById('shipping_menu')) {
    var shippingID = document.getElementById('shipping_menu').options[document.getElementById('shipping_menu').selectedIndex].value;
  } else {
    var shippingID = null;
  }
  var subTotal = document.getElementById('sub_total').innerHTML;
  getPrice("logicajax/shippingCharge.php?country=" + country + '&shippingID=' + shippingID + '&subTotal=' + subTotal, "delivery_menu");
}

function getGlossary(letter) {
  xmlhttp.open("GET", "logicajax/glossary.php?letter=" + letter);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {    
      document.getElementById('glossary').innerHTML =  xmlhttp.responseText;              
    }
  }  
  xmlhttp.send(null);   
}

function gatewayHandler(){
 getPGForm("logicajax/paymentGatewayForm.php?pg=" + this.value);
}
