window.onload = initForm;
function initForm() {
  if(document.getElementById('index')) {
   document.getElementById('index').onmouseover = onMouseHoverHomeMenuBG;
    document.getElementById('index').onmouseout = onMouseOutHomeMenuBG;
  }
  if(document.getElementById('contact-us')) {
   document.getElementById('contact-us').onmouseover = onMouseHoverContactMenuBG;
    document.getElementById('contact-us').onmouseout = onMouseOutContactMenuBG;
  }
  if(document.getElementById('productsAnchor')){
    document.getElementById('productsAnchor').onmouseover = styleCursor;
  }
  external_links();
  if(document.getElementById('delivery_country')) {
    document.getElementById('delivery_country').onchange = getShipping;
  }
  if(document.getElementById('flash_no_shop')) {
    insertFlashObject('flash_no_shop', 'flash/banner.swf', 980, 128);
  }
  if(document.getElementById('map')) {
    mapload();
  }
  if(document.getElementById('delivery_country') && document.getElementById('delivery_country').value != '') {
    getShipping();
  }
}
function trackPDFClicks() {
  // NOTE: This function is called after the analytics - you'll find the function call in the database, directly after the analytics code
  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") != 'undefined' && anchor.getAttribute("href") != null) {
        if(anchor.getAttribute("href").substring(anchor.getAttribute("href").length - 4) == '.pdf') {
          if(anchor.getAttribute("href").substring(0, 5) == 'pdfs/') {
            trackLabel = anchor.getAttribute("href").substring(5);
          } else {
            trackLabel = anchor.getAttribute("href");
          }

          if(anchor.getAttribute("title") != 'undefined') {
            trackLabel = anchor.getAttribute("title");
          }

          var category = 'General';
          if(document.getElementById('taxonomy')) {
            if(document.getElementById('taxonomy').getAttribute("name") != 'undefined') {
              category = document.getElementById('taxonomy').getAttribute("name");
            }
          }

          anchor.attachEvent(
            'onclick', function() {
              pageTracker._trackEvent(category, 'PDF Download', trackLabel);
              void(0);
            }
          )
        }
      }
    }
  }
}
function insertFlashObject(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;
}
// 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";
      }
    }
  }
}
function onMouseHoverHomeMenuBG(){
  if(document.getElementById('menu_left')){
   document.getElementById('menu_left').style.backgroundImage = 'url(images/template/menu-left-corner-active.jpg)';
  }
}
function onMouseOutHomeMenuBG(){
  if(document.getElementById('menu_left')){
   document.getElementById('menu_left').style.backgroundImage = 'url(images/template/menu-left-corner.jpg)';
  }
}
function onMouseHoverContactMenuBG(){
  if(document.getElementById('menu_right')){
   document.getElementById('menu_right').style.backgroundImage = 'url(images/template/menu-right-corner-active.jpg)';
  }
}
function onMouseOutContactMenuBG(){
  if(document.getElementById('menu_right')){
   document.getElementById('menu_right').style.backgroundImage = 'url(images/template/menu-right-corner.jpg)';
  }
}
function styleCursor(){
  document.getElementById('productsAnchor').style.cursor = 'default';
}
function searchByMan() {
  var man = document.getElementById('product_search');
  man = man.options[man.selectedIndex].value;
  window.location = 'index.php?view=manufacturer&manufacturer=' + man;return true;
}
// 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) {
  var obj = document.getElementById(objID);
  var id = objID.substr(14);
  var title = new Array();
  var value = new Array();
  xmlhttp.open("GET", phpFile);
  xmlhttp.onreadystatechange = function() {
   if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {

      var splitResult = xmlhttp.responseText.split("|");
      if (splitResult[0] == 'below min') {
          document.getElementById('error_msg_'+id).innerHTML = 'The minimum required order length for this item is '+splitResult[1]+' metres';
          obj.innerHTML = 'N/A';
          document.getElementById('add_to_basket_btn_'+id).style.visibility = 'hidden';
          return;
      }
      if (splitResult[0] == 'upper max') {
          document.getElementById('error_msg_'+id).innerHTML = 'The maximum order length for this item is '+splitResult[1]+' metres';
          obj.innerHTML = 'N/A';
          document.getElementById('add_to_basket_btn_'+id).style.visibility = 'hidden';
          return;
      }

      if(document.getElementById('error_msg_'+id)) {
        document.getElementById('error_msg_'+id).innerHTML = '&nbsp;';
      }
      if(document.getElementById('add_to_basket_btn_'+id)) {
        document.getElementById('add_to_basket_btn_'+id).style.visibility = 'visible';
      }

      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);
}
// Update price
function updatePrice(prodID, menuID, variantType) {
  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 + "|";
      }
    }
  }
  getPrice("logicajax/updateProductPrice.php?p=" + prodID + "&variantsIDs='" + variantsIDs + "'&globVariantsIDs='" + globalVariantsIDs
           + "'&variantsValues='" + variantsValues + "'&globVariantsValues='" + globalVariantsValues + "'", "product_price_" + prodID);
}
// Calculate volume price
function updateVolumePrice(prodID) {
  if(document.getElementById('quantity_' + prodID)) {
   var volume = document.getElementById('quantity_' + prodID).value;
    getPrice("logicajax/updateVolumePrice.php?p=" + prodID + "&volume=" + volume, "product_price_" + prodID);
  }
}
// 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 {
    /*if(document.getElementById('delivery_country').value == 1) {
      var shippingID = 31;
    } else {*/
      var shippingID = null;
    //}
  }
  var subTotal = document.getElementById('sub_total').innerHTML;
  getPrice("logicajax/shippingCharge.php?country=" + country + '&shippingID=' + shippingID + '&subTotal=' + subTotal, "delivery_menu");
}
// 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 toggleTrade() {
  var displayProperty = document.getElementById('button3').style.display;
  if (displayProperty == 'none') {
      document.getElementById('button3').style.display = 'inline';
  } else {
      document.getElementById('button3').style.display = 'none';
  }
}

