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_page_buy_bar')){ 
    updatePriceStart();
    document.getElementById('product_page_buy_bar').onchange = updatePriceStart;      
  }*/
  if(document.getElementById('product_listing_order')){
    document.getElementById('product_listing_order').onchange = orderProducts;
  }
  if(document.getElementById('product_listing_brand')){
    document.getElementById('product_listing_brand').onchange = orderProducts;
  }
  
  if(document.getElementById('sizeguide_height')){
    document.getElementById('sizeguide_height').onchange = get_size;
  }
  if(document.getElementById('sizeguide_weight')){
    document.getElementById('sizeguide_weight').onchange = get_size;
  }
  if(document.getElementById('manufacture_search_dropdown')){
    document.getElementById('manufacture_search_dropdown').onchange = go_to_brand;
  }
  if(document.getElementById('contact_nature')){
    document.getElementById('contact_nature').onchange = contact_nature;
    contact_nature();
  }
  if(document.getElementById("contact_right_map_body")){
    contact_map();
  }
  if(document.getElementById("dordogne_map")){
    dordogne_map();
  }
  if(document.getElementById("swim_centre_map")){
    swimcentre_map_f();
  }                                                     
}

// 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, sizeguidePhpFile) {
  //alert(phpFile);
  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;
      }  
        price = xmlhttp.responseText;
        updateTotalPrice(price);
        if(objID != 'delivery_menu'){
			getSizeguideValues(sizeguidePhpFile);
		} 
    }
  }  
  xmlhttp.send(null); 
}

function getSizeguideValues(phpFile) {
  var valueA = document.getElementById('sizeguide_result');
  //var valueB = document.getElementById('sizeguide_result_right');
  //alert(phpFile);
  xmlhttp.open("GET", phpFile);
  xmlhttp.onreadystatechange = function() {
    //alert('readystate: '+xmlhttp.readyState+" status: "+xmlhttp.status);
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      response = xmlhttp.responseText;
      //responseValues = response.split("|");
      valueA.innerHTML = response;
      //valueB.innerHTML = responseValues[1]; 
    }
  }  
  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 updateTotalPrice(price) {
  if(document.getElementById('product_id')) {
    var prodID = document.getElementById('product_id').value;    
  }
  var priceResponse = price;
  //alert(priceResponse);
  //var quantity = document.getElementById('quantity').value;      
  //var priceSplit = priceResponse.substring(7);
  //var priceEach = priceSplit + 0;
  //var totalPrice = priceEach + 0 /** quantity*/;
  //var totalPrice = priceEach.toFixed(2);
  //var totalPriceIdentifier = 'total_price_' + prodID;   
  var totalPriceIdentifier = 'product_price_' + prodID;
  if(document.getElementById(totalPriceIdentifier)) {
    document.getElementById(totalPriceIdentifier).innerHTML = priceResponse;
  }
}

function updatePrice(prodID, menuID, variantType) {
  var menuArray = menuID.split("_");
  var varID = menuArray[1];
  if(varID == 61){
    if(document.getElementById('sizeguide_table_id')){
      tableID = document.getElementById('sizeguide_table_id').innerHTML;
      sizeValue = document.getElementById(menuID).value;
      var sizeguidePhpFile = "logicajax/sizeguide.php?table=" + tableID + "&size=" + sizeValue;
      //getSizeguideValues(phpFile);
    }
  }
  //var quantity = document.getElementById('quantity').value;
  var quantity = 1;
  if(document.getElementById('variantsIDs_'+prodID)){
    var variantsIDs = document.getElementById('variantsIDs').value;  
    variantID = variantsIDs.split('|');
    var variantsValues = '';                                                    
    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;  
    globalVariantID = globalVariantsIDs.split('|');
    var globalVariantsValues = '';                                                      
    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, sizeguidePhpFile);
}

function updatePriceStart() {
  var prodID = document.getElementById('product_id').value;
  var quantity = document.getElementById('quantity').value;
  if(document.getElementById('variantsIDs_'+prodID)){
    var variantsIDs = document.getElementById('variantsIDs').value;  
    variantID = variantsIDs.split('|');
    var variantsValues = '';                                                    
    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;  
    globalVariantID = globalVariantsIDs.split('|');
    var globalVariantsValues = '';                                                      
    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 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 == '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>');
    }
  } else if(size == 'shop-cta') {
    document.write('<object type="application/x-shockwave-flash" data="flash/shop-cta.swf" width="358" height="211"><param name="movie" value="flash/shop-cta.swf" /><param value="transparent" name="wmode"/></object>');
  } else {
    if(window.location.pathname.indexOf('dubai') != -1) {
      document.write('<object type="application/x-shockwave-flash" data="../flash/right-cta.swf" width="184" height="125"><param name="movie" value="../flash/right-cta.swf" /><param value="transparent" name="wmode"/></object>');
    } else {
      document.write('<object type="application/x-shockwave-flash" data="flash/right-cta.swf" width="184" height="125"><param name="movie" value="flash/right-cta.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 tri2o_over(){
  var menu_item = document.getElementById('top_menu_3');
  menu_item.innerHTML = '<img src="images/template/mst_menu_tri20_shop_active.gif" alt="Tri20 Shop" title="Tri20 Shop" height="42" width="150" onmouseout="tri2o_out()"/>';
}

function tri2o_out(){
  var menu_item_out = document.getElementById('top_menu_3');
  if (menu_item_out.className != 'active'){
    menu_item_out.innerHTML = '<img src="images/template/mst_menu_tri20_shop.gif" alt="Tri20 Shop" title="Tri20 Shop" height="42" width="150" onmouseover="tri2o_over()"/>';
  }
}

function tri2o_sc_over(){
  var menu_item = document.getElementById('top_menu_5');
  menu_item.innerHTML = '<img src="images/template/mst_menu_tri20_sc_active.gif" alt="Tri20 Swim Centre" title="Tri20 Swim Centre" height="42" width="151" onmouseout="tri2o_sc_out()"/>';
}

function tri2o_sc_out(){
  var menu_item_out = document.getElementById('top_menu_5');
  if (menu_item_out.className != 'active'){
    menu_item_out.innerHTML = '<img src="images/template/mst_menu_tri20_sc.gif" alt="Tri20 Swim Centre" title="Tri20 Swim Centre" height="42" width="151" onmouseover="tri2o_sc_over()"/>';
  }
}

function orderProducts(page){
  var section = document.getElementById('product_listing_section').value;
  var new_url = 'section.htm?section='+section;
  if(document.getElementById('product_listing_brand')){
    var brand = document.getElementById('product_listing_brand').value;
    if(brand.length > 0){
      new_url = new_url + "&brand="+brand;
    }
  }
  if(document.getElementById('product_listing_order')){  
    var order = document.getElementById('product_listing_order').value;
    if(order.length > 0){
      var new_url = new_url + '&order='+order;
    }
  }
  if(document.getElementById('page_no')){
    var page_no = document.getElementById('page_no').value;
    if(page_no.length > 0){
      new_url = new_url + "&page="+page_no;
    }
  }
  window.location = new_url;
}

function get_size(){
  var table = document.getElementById('sizeguide_table').value;
  var weight = document.getElementById('sizeguide_weight').value;
  var height = document.getElementById('sizeguide_height').value;
  
  if((table.length > 0) && (weight.length > 0) && (height.length > 0)){
    //alert('table: ' + table + ' - weight: ' + weight + ' - height: ' + height);
    var resultSpan = document.getElementById('sizeguide_result');
    var phpFile = "logicajax/sizeguide.php?table=" + table + "&height=" + height + "&weight=" + weight;
    //alert(phpFile);
    xmlhttp.open("GET", phpFile);
    xmlhttp.onreadystatechange = function() {
      if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
        resultSpan.innerHTML = xmlhttp.responseText;
      }
    }  
    xmlhttp.send(null);
  }
}

function go_to_brand(){
  var brand = document.getElementById('manufacture_search_dropdown').value;
  window.location = 'brand.htm?brand='+brand;
}

function contact_nature(){
  var nature = document.getElementById('contact_nature').value;
  if(nature == 22){
    var divList = document.getElementsByTagName('div');
    for (var i = 0; i < divList.length; i++){
      if(divList[i].className == 'f_contact_cont_dates'){
        divList[i].style.display = "block"; 
      }
    }  
  }else{
    var divList = document.getElementsByTagName('div');
    for (var i = 0; i < divList.length; i++){
      if(divList[i].className == 'f_contact_cont_dates'){
        divList[i].style.display = "none"; 
      }
    }
  }  
}

var map;
var point;
var marker;
var html;

function contact_map(){
  if (GBrowserIsCompatible()) {
    // get the HTML which we need to use to create the new info box
    var html = document.getElementById('googlemap').innerHTML;
    // replace the linkname tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('linkname','linkname2');
    // replace the getDirToggle tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('getDirToggle','getDirToggle2');  
    
    map = new GMap2(document.getElementById("contact_right_map_body"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(51.358986,-1.042886), 12);
    point = new GLatLng(51.358986,-1.042886);
    marker = new GMarker(point);
    map.addOverlay(marker)
    // open the info window with the modified HTML. no duplicate ID's for us!
    map.openInfoWindowHtml(map.getCenter(), html);
    
    // add an onclict event, too!
    GEvent.addListener(marker, "click", function() {  
      // open the info window with the modified HTML. no duplicate ID's for us!    
      map.openInfoWindowHtml(point, html);
    });
  } 
}

function prepareForm(form, cid){
  if (GBrowserIsCompatible()) {
    gApplication.prepareForm(form, cid);
  }
}

function displayDir(){    
  if (GBrowserIsCompatible()) {  
    var toggle = document.getElementById("getDirToggle").style.display;
    if(toggle == 'none'){      
      document.getElementById("getDirToggle").style.display='block';
      document.getElementById("linkname").innerHTML = "Don't get directions";      
    }else{
      document.getElementById("getDirToggle").style.display='none';
      document.getElementById("linkname").innerHTML = "Get directions";
    }
    
    // get the HTML which we need to use to create the new info box
    var html = document.getElementById('googlemap').innerHTML;
    // replace the linkname tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('linkname','linkname2');
    // replace the getDirToggle tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('getDirToggle','getDirToggle2');      
    // open the info window with the modified HTML. no duplicate ID's for us!
    map.openInfoWindowHtml(point, html);
  }
}

var dordogne_map;
var dordogne_point;
var dordogne_marker;
var dordogne_html;

function dordogne_map(){
  if (GBrowserIsCompatible()) {
    // get the HTML which we need to use to create the new info box
    var dordogne_html = document.getElementById('dordogne_googlemap').innerHTML;
    // replace the linkname tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('linkname','linkname2');
    // replace the getDirToggle tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('getDirToggle','getDirToggle2');  
    
    dordogne_map = new GMap2(document.getElementById("dordogne_map"));
    dordogne_map.addControl(new GSmallMapControl());
    dordogne_map.addControl(new GMapTypeControl());
    dordogne_map.setCenter(new GLatLng(45.289094,0.273907), 12);
    dordogne_point = new GLatLng(45.289094,0.273907);
    dordogne_marker = new GMarker(dordogne_point);
    dordogne_map.addOverlay(dordogne_marker)
    // open the info window with the modified HTML. no duplicate ID's for us!
    dordogne_map.openInfoWindowHtml(dordogne_map.getCenter(), dordogne_html);
    
    // add an onclict event, too!
    GEvent.addListener(dordogne_marker, "click", function() {  
      // open the info window with the modified HTML. no duplicate ID's for us!    
      dordogne_map.openInfoWindowHtml(dordogne_point, dordogne_html);
    });
  } 
}

function dordogne_prepareForm(form, cid){
  if (GBrowserIsCompatible()) {
    gApplication.prepareForm(form, cid);
  }
}

function dordogne_displayDir(){    
  if (GBrowserIsCompatible()) {  
    var toggle = document.getElementById("dordogne_getDirToggle").style.display;
    if(toggle == 'none'){      
      document.getElementById("dordogne_getDirToggle").style.display='block';
      document.getElementById("dordogne_linkname").innerHTML = "Don't get directions";      
    }else{
      document.getElementById("dordogne_getDirToggle").style.display='none';
      document.getElementById("dordogne_linkname").innerHTML = "Get directions";
    }
    
    // get the HTML which we need to use to create the new info box
    //var dordogne_html = document.getElementById('dordogne_googlemap').innerHTML;
    // replace the linkname tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('linkname','linkname2');
    // replace the getDirToggle tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('getDirToggle','getDirToggle2');      
    // open the info window with the modified HTML. no duplicate ID's for us!
    //map.openInfoWindowHtml(dordogne_point, dordogne_html);
  }
}

var swimcentre_map;
var swimcentre_point;
var swimcentre_marker;
var swimcentre_html;

function swimcentre_map_f(){
  if (GBrowserIsCompatible()) {
    // get the HTML which we need to use to create the new info box
    var swimcentre_html = document.getElementById('swim_centre_googlemap').innerHTML;
    // replace the linkname tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('linkname','linkname2');
    // replace the getDirToggle tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('getDirToggle','getDirToggle2');  
    
    swimcentre_map = new GMap2(document.getElementById("swim_centre_map"));
    swimcentre_map.addControl(new GSmallMapControl());
    swimcentre_map.addControl(new GMapTypeControl());
    swimcentre_map.setCenter(new GLatLng(51.423303,-1.004914), 12);
    swimcentre_point = new GLatLng(51.423303,-1.004914);
    swimcentre_marker = new GMarker(swimcentre_point);
    swimcentre_map.addOverlay(swimcentre_marker)
    // open the info window with the modified HTML. no duplicate ID's for us!
    swimcentre_map.openInfoWindowHtml(swimcentre_map.getCenter(), swimcentre_html);
    
    // add an onclict event, too!
    GEvent.addListener(swimcentre_marker, "click", function() {  
      // open the info window with the modified HTML. no duplicate ID's for us!    
      swimcentre_map.openInfoWindowHtml(swimcentre_point, swimcentre_html);
    });
  } 
}

function swimcentre_prepareForm(form, cid){
  if (GBrowserIsCompatible()) {
    gApplication.prepareForm(form, cid);
  }
}

function swimcentre_displayDir(){    
  if (GBrowserIsCompatible()) {  
    var toggle = document.getElementById("swim_centre_getDirToggle").style.display;
    if(toggle == 'none'){      
      document.getElementById("swim_centre_getDirToggle").style.display='block';
      document.getElementById("swim_centre_linkname").innerHTML = "Don't get directions";      
    }else{
      document.getElementById("swim_centre_getDirToggle").style.display='none';
      document.getElementById("swim_centre_linkname").innerHTML = "Get directions";
    }
    
    // get the HTML which we need to use to create the new info box
    //var dordogne_html = document.getElementById('dordogne_googlemap').innerHTML;
    // replace the linkname tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('linkname','linkname2');
    // replace the getDirToggle tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('getDirToggle','getDirToggle2');      
    // open the info window with the modified HTML. no duplicate ID's for us!
    //map.openInfoWindowHtml(dordogne_point, dordogne_html);
  }
}
