window.onload = initForm;
function initForm() {
  if(document.getElementById('flash')) {
		insert_flash_object('flash', 'flash/banner.swf', 793, 223);
  }
  if(document.getElementById('product_search')) {
    if(document.getElementById('product_search').value != 'Keyword, product code') {
      document.getElementById('product_search').style.color = "#000000";
    }
  }
  if(document.getElementById('invoice_ship[invoice_ship]')) {
    document.getElementById('invoice_ship[invoice_ship]').onclick = toggleDelivery;
  }
  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('product_search')) {
    document.getElementById('product_search').onfocus = clearfield;
    document.getElementById('product_search').onblur = reinstatefield;
  }
  if(document.getElementById('prod_id')) {
    updatePrice();
  }
  if(document.getElementById('customQuantityPrice')) {
    updatePriceCustomQuantity();
  }
}
// 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;
}
// Get price
function getPrice(phpFile, objID) {
  if(document.getElementById(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);
  }
}
// Get custom quantity price
function getCustomQuantityPrice(phpFile, objID) {
  if(document.getElementById(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;
      }
    }
    xmlhttp.send(null);
  }
}
// Update price
function updatePrice() {
  prodID = document.getElementById('prod_id').value;
  if(document.getElementById('variantsIDs_' + prodID)) {
    var variantsIDs = document.getElementById('variantsIDs_' + prodID).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 + "|";
      }
    }           
  }
  if(document.getElementById('page_quantity_' + prodID)) {
    pageQty = document.getElementById('page_quantity_' + prodID).value;
    pageQtyFull = '&pages=' + pageQty;
  } else {
    pageQtyFull = '';
  }
  if(document.getElementById('quantity_' + prodID)) {
    qty = document.getElementById('quantity_' + prodID).value;
    getPrice("logicajax/updateProductPrice.php?p=" + prodID + "&q=" + qty + pageQtyFull + "&variantsIDs='" + variantsIDs + "'&globVariantsIDs='" + globalVariantsIDs
             + "'&variantsValues='" + variantsValues + "'&globVariantsValues='" + globalVariantsValues + "'", "product_price_" + prodID);
  }
}
// Update price, custom quantity
function updatePriceCustomQuantity() {
  prodID = document.getElementById('prod_id').value;
  if(document.getElementById('quantity_' + prodID)) {
    qty = document.getElementById('quantity_' + prodID).value;
    if(document.getElementById('selectedMatrix')) {
      getCustomQuantityPrice("logicajax/updateCustomQuantityPrice.php?p=" + prodID + "&q=" + qty + "&m=" + document.getElementById('selectedMatrix').value, 'customQuantityPrice');
    } else {
      getCustomQuantityPrice("logicajax/updateCustomQuantityPrice.php?p=" + prodID + "&q=" + qty, 'customQuantityPrice');
    }
  }
}
// Get shipping
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");
}
// Get glossary
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);
}
// Regenerate captcha
function regenerateCaptcha(imageID, viewName) {
  xmlhttp.open("GET", "logicajax/generateCaptcha.php?imageid=" + imageID + "&viewname=" + viewName);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      document.getElementById(imageID).src = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}
// Trim
function trim(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}
// Left trim
function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
// Right trim
function rtrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
// Clear input field
function clearfield() {
  switch(this.id) {
    case 'product_search':
      trimValue = trim(this.value).toLowerCase();
      if(trimValue == 'keyword, product code') {
        this.value = '';
        this.style.color = "#000000";
      }
    break;
  }
}
// Repopulate input field
function reinstatefield() {
  switch(this.id) {
    case 'product_search':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Keyword, product code';
        this.style.color = "#AAAAAA";
      }
    break;
  }
}
function insert_flash_object(objID, flashLocation, width, height) {
  var flashObject;
  flashObject = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='" + width + "' height='" + height + "'>";
  flashObject = flashObject + "<param name='wmode' value='transparent'>";
  flashObject = flashObject + "<param name='movie' value='" + flashLocation + "' />";
  flashObject = flashObject + "<param name='quality' value='high' />";
  flashObject = flashObject + "<embed src='" + flashLocation + "' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='" + width + "' height='" + height + "' wmode='transparent'";
  flashObject = flashObject + "></embed></object>";
  document.getElementById(objID).innerHTML = flashObject;
}
// Toggle delivery
function toggleDelivery() {
  if(this.checked) {
    if(trim(document.getElementById('delivery_name').value) == '') {
      document.getElementById('delivery_name').value = document.getElementById('invoice_name').value;
    }
    if(trim(document.getElementById('delivery_company').value) == '') {
      document.getElementById('delivery_company').value = document.getElementById('invoice_company').value;
    }
    if(trim(document.getElementById('delivery_address1').value) == '') {
      document.getElementById('delivery_address1').value = document.getElementById('invoice_address1').value;
    }
    if(trim(document.getElementById('delivery_address2').value) == '') {
      document.getElementById('delivery_address2').value = document.getElementById('invoice_address2').value;
    }
    if(trim(document.getElementById('delivery_postaltown').value) == '') {
      document.getElementById('delivery_postaltown').value = document.getElementById('invoice_postaltown').value;
    }
    if(trim(document.getElementById('delivery_county').value) == '') {
      document.getElementById('delivery_county').value = document.getElementById('invoice_county').value;
    }
    if(trim(document.getElementById('delivery_postcode').value) == '') {
      document.getElementById('delivery_postcode').value = document.getElementById('invoice_postcode').value;
    }
    document.getElementById('delivery_country').value = document.getElementById('invoice_country').value;
    if(trim(document.getElementById('delivery_contact_number').value) == '') {
      document.getElementById('delivery_contact_number').value = document.getElementById('invoice_contact_number').value;
    }
    if(trim(document.getElementById('delivery_fax').value) == '') {
      document.getElementById('delivery_fax').value = document.getElementById('invoice_fax').value;
    }
    if(trim(document.getElementById('delivery_mobile').value) == '') {
      document.getElementById('delivery_mobile').value = document.getElementById('invoice_mobile').value;
    }
  } else {
    if(trim(document.getElementById('delivery_name').value) == document.getElementById('invoice_name').value) {
      document.getElementById('delivery_name').value = '';
    }
    if(trim(document.getElementById('delivery_company').value) == document.getElementById('invoice_company').value) {
      document.getElementById('delivery_company').value = '';
    }
    if(trim(document.getElementById('delivery_address1').value) == document.getElementById('invoice_address1').value) {
      document.getElementById('delivery_address1').value = '';
    }
    if(trim(document.getElementById('delivery_address2').value) == document.getElementById('invoice_address2').value) {
      document.getElementById('delivery_address2').value = '';
    }
    if(trim(document.getElementById('delivery_postaltown').value) == document.getElementById('invoice_postaltown').value) {
      document.getElementById('delivery_postaltown').value = '';
    }
    if(trim(document.getElementById('delivery_county').value) == document.getElementById('invoice_county').value) {
      document.getElementById('delivery_county').value = '';
    }
    if(trim(document.getElementById('delivery_postcode').value) == document.getElementById('invoice_postcode').value) {
      document.getElementById('delivery_postcode').value = '';
    }
    document.getElementById('delivery_country').value = 1;
    if(trim(document.getElementById('delivery_contact_number').value) == document.getElementById('invoice_contact_number').value) {
      document.getElementById('delivery_contact_number').value = '';
    }
    if(trim(document.getElementById('delivery_fax').value) == document.getElementById('invoice_fax').value) {
      document.getElementById('delivery_fax').value = '';
    }
    if(trim(document.getElementById('delivery_mobile').value) == document.getElementById('invoice_mobile').value) {
      document.getElementById('delivery_mobile').value = '';
    } 
  }
}
