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('product_column_right')){
    if(document.getElementById('quantity')){ 
      updatePriceRadioStart();
      //document.getElementById('quantity').onchange = updatePriceRadio
      //document.getElementById('quantity').onchange = updateTotalPriceQty;
      //document.getElementById('quantity').onMouseOut = updateTotalPriceQty;
      document.getElementById('quantity').onchange = updatePriceRadioStart;
      //document.getElementById('quantity').onMouseOut = updatePriceRadioStart;                
      //document.getElementById('quantity').onchange = function(){alert('change detected')};   
    }
  //}
  if(document.getElementById('map')) { mapload(); }
  if(document.getElementById('map2')) { mapload2(); document.getElementById('map_body2').style.display = "none"; }
  if(document.getElementById('map3')) { mapload3(); document.getElementById('map_body3').style.display = "none"; }
}

function showGMap(idN) {
  switch(idN) {
    case 1:
      document.getElementById('map_body').style.display = "block";
      document.getElementById('map_body').style.visibility = "visible";
      document.getElementById('map_body2').style.display = "none";
      document.getElementById('map_body2').style.visibility = "hidden";
      document.getElementById('map_body3').style.display = "none";
      document.getElementById('map_body3').style.visibility = "hidden";
    break; 
    
    case 2:
      document.getElementById('map_body').style.display = "none";
      document.getElementById('map_body').style.visibility = "hidden";
      document.getElementById('map_body2').style.display = "block";
      document.getElementById('map_body2').style.visibility = "visible";
      document.getElementById('map_body3').style.display = "none";
      document.getElementById('map_body3').style.visibility = "hidden";
    break;
    
    case 3:
      document.getElementById('map_body').style.display = "none";
      document.getElementById('map_body').style.visibility = "hidden";
      document.getElementById('map_body2').style.display = "none";
      document.getElementById('map_body2').style.visibility = "hidden";
      document.getElementById('map_body3').style.display = "block";
      document.getElementById('map_body3').style.visibility = "visible";
    break; 
  }
  return false;
}

// 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;
  
  imageTitle = document.getElementById('image_name_span');
  imageTitle.innerHTML = 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;
  
  imageTitle = document.getElementById('image_name_span');
  imageTitle.innerHTML = 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;
      }
      //if(document.getElementById('product_column_right') == true){   
        updateTotalPrice(xmlhttp.responseText);
      //} 
    }
  }  
  xmlhttp.send(null); 
}
function updateTotalPriceQty(){
  var prodID = document.getElementById('product_id').value;
  var priceIdentifier = 'product_price_' + prodID; 
  var priceResponse = document.getElementById(priceIdentifier).innerHTML;
  var quantity = document.getElementById('quantity').value;  
  var priceSplit = priceResponse.substring(1);
  var priceEach = priceSplit + 0;   
  var totalPrice = priceEach * quantity;
  var totalPrice = totalPrice.toFixed(2);
  var totalPriceIdentifier = 'total_price_' + prodID;
  document.getElementById(totalPriceIdentifier).innerHTML = totalPrice;
}
function updateTotalPriceQtyRadio(){
  var prodID = document.getElementById('product_id').value;  
  var priceIdentifier = 'product_price_' + prodID; 
  var priceResponse = document.getElementById(priceIdentifier).innerHTML;
  var quantity = document.getElementById('quantity').value;  
  var priceSplit = priceResponse.substring(1);    
  var priceEach = priceSplit + 0;               
  var totalPrice = priceEach * quantity;
  var totalPrice = totalPrice.toFixed(2);      
  var totalPriceIdentifier = 'total_price_' + prodID;
  document.getElementById(totalPriceIdentifier).innerHTML = totalPrice;
}
function updateTotalPrice(price){       
  var prodID = document.getElementById('product_id').value;
  var priceResponse = price;
  var quantity = document.getElementById('quantity').value;  
  var priceSplit = priceResponse.substring(7);
  var priceEach = priceSplit + 0;
  var totalPrice = priceEach * quantity;
  var totalPrice = totalPrice.toFixed(2);
  var totalPriceIdentifier = 'total_price_' + prodID;
  document.getElementById(totalPriceIdentifier).innerHTML = totalPrice;
}
function updatePrice(prodID, menuID, variantType) {
  var quantity = document.getElementById('quantity').value;
  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 + "'&qty=" + quantity, "product_price_" + prodID);
}
function updatePriceRadio(prodID, menuID, variantType) {
  var quantity = document.getElementById('quantity').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 = '';
        globalVariantBase = document.getElementById('option_' + globalVariantID[i] + '_' + prodID);
        globalVariantOptions = globalVariantBase.getElementsByTagName('input');
        for (j=0;j<globalVariantOptions.length;j++){
          if (globalVariantOptions[j].checked){
            globalVariantValue = globalVariantOptions[j].value;
          }
        }
        //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 + "'&qty=" + quantity, "product_price_" + prodID);
}

function updatePriceRadioStart() {
  var quantity = document.getElementById('quantity').value;
  var prodID = document.getElementById('product_id').value;
  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 = '';
        globalVariantBase = document.getElementById('option_' + globalVariantID[i] + '_' + prodID);
        globalVariantOptions = globalVariantBase.getElementsByTagName('input');
        for (j=0;j<globalVariantOptions.length;j++){
          if (globalVariantOptions[j].checked){
            globalVariantValue = globalVariantOptions[j].value;
          }
        }
        //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 + "'&qty=" + quantity, "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 display_flash(size) {
	if(size == 'shop-cta') {
		 document.write('<object type="application/x-shockwave-flash" data="flash/shop-cta.swf" width="362" height="215"><param name="movie" value="flash/shop-cta.swf" /><param value="transparent" name="wmode"/></object>');
	}
	
if(size == 'delivery') {
    if(window.location.pathname.indexOf('dubai') != -1) {
      document.write('<object type="application/x-shockwave-flash" data="../flash/delivery-banner.swf" width="418" height="61"><param name="movie" value="../flash/delivery-banner.swf" /><param value="transparent" name="wmode"/></object>');
    } else {
      document.write('<object type="application/x-shockwave-flash" data="flash/delivery-banner.swf" width="418" height="61"><param name="movie" value="flash/delivery-banner.swf" /><param value="transparent" name="wmode"/></object>');
    }
		
  }
  if(size == 'small') {
    if(window.location.pathname.indexOf('dubai') != -1) {
      document.write('<object type="application/x-shockwave-flash" data="../flash/header_banner.swf" width="990" height="193"><param name="movie" value="../flash/header_banner.swf" /><param value="transparent" name="wmode"/></object>');
    } else {
      document.write('<object type="application/x-shockwave-flash" data="flash/header_banner.swf" width="990" height="193"><param name="movie" value="flash/header_banner.swf" /><param value="transparent" name="wmode"/></object>');
    }
		
  } else if(size == 'large') {
    if(window.location.pathname.indexOf('dubai') != -1) {
      document.write('<object type="application/x-shockwave-flash" data="../flash/home_banner.swf" width="990" height="306"><param name="movie" value="../flash/home_banner.swf" /><param value="transparent" name="wmode"/></object>');
    } else {
      document.write('<object type="application/x-shockwave-flash" data="flash/home_banner.swf" width="990" height="306"><param name="movie" value="flash/home_banner.swf" /><param value="transparent" name="wmode"/></object>');
    }
  } 
}

function toggle(id) {
  
  var testimonial;

  testimonial = document.getElementById('testimonial_'+id+'_short').style.display;

  if(testimonial == 'none') {
    document.getElementById('testimonial_'+id+'_short').style.display = 'block';
    document.getElementById('testimonial_'+id+'_full').style.display = 'none';
  } else {
    document.getElementById('testimonial_'+id+'_short').style.display = 'none';
    document.getElementById('testimonial_'+id+'_full').style.display = 'block';
  }
}

function openfloatmenu(tmenu){
  var omenu = document.getElementById(tmenu);
  if(omenu != null){
    omenu.className = 'floatmenu_open';
  } 
}

function closefloatmenu(tmenu){
  var cmenu = document.getElementById(tmenu);
  if(cmenu != null){
    cmenu.className = 'floatmenu'; 
  }
}

function openedmenu(tmenu){
  var omenu = document.getElementById(tmenu);
  if(omenu != null){
    omenu.className = 'top_menu_cont_a_selected';
  } 
}

function closedmenu(tmenu){
  var omenu = document.getElementById(tmenu);
  if(omenu != null){
    omenu.className = 'top_menu_cont_a';
  } 
}

function InsertFlashObject(objID, flashLocation, width, height, flashString) {
  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' />";
  if(typeof(flashString) == "undefined") {
    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'";
  } else {
    flashObject = flashObject + "<param name='FlashVars' value='" + flashString + "'";
    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 + "FlashVars='" + flashString + "'";
  }
  flashObject = flashObject + "></embed></object>";
  if(document.getElementById(objID)) {
    document.getElementById(objID).innerHTML = flashObject;
  }
}