window.onload = initForm;
function initForm() {
  //addFieldVal('qcname', 'Name*');
  //addFieldVal('qctelephone', 'Telephone*');
  //addFieldVal('qcemail', 'Email*');
  //addFieldVal('qclocation', 'Postcode*');
  //addFieldVal('qcmodel', 'Model Type');

  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('delivery_country') && document.getElementById('delivery_country').value != '') {
    getShipping();
  }
  if(document.getElementById('basket_country')) {
    document.getElementById('delivery_country_menu').style.display = 'block';
    document.getElementById('basket_country').onchange = getBasketShipping;
  }
  if(document.getElementById('basket_country') && document.getElementById('basket_country').value != '') {
    getBasketShipping();
  }

  if(document.getElementById('invoice_country')) {
    document.getElementById('invoice_country').onchange = getCheckoutShipping;
  }
  if(document.getElementById('invoice_country') && document.getElementById('invoice_country').value != '') {
    getCheckoutShipping();
  }

  if(document.getElementById('ebike_selector_form')) {
     document.getElementById('ebike_selector_form').onsubmit = function () {return checkMinHeight();};
  }
  if(document.getElementById("rotator_cont")){
    setInterval("home_image_rotator()", 4000);
  }
  hideAllAnswers();
}
function addFieldVal(element, val) {
  if(document.getElementById(element)) {
    document.getElementById(element).onfocus = function () {clearfield(this, val);};
    document.getElementById(element).onblur = function () {reinstatefield(this, val);};
  }
}
function clearfield(element, val) {
  trimValue = trim(element.value);
  if(trimValue == val) {
    element.value = '';
  }
}
function reinstatefield(element, val) {
  trimValue = trim(element.value);
  if(trimValue == '') {
    element.value = val;
  }
}
function trim(str, chars) {
  var replaceLeft, replaceRight;
  chars = chars || "\\s";
  replaceLeft = str.replace(new RegExp("^[" + chars + "]+", "g"), "");
  replaceRight = replaceLeft.replace(new RegExp("[" + chars + "]+$", "g"), "");
  return replaceRight;
}
function checkMinHeight() {
  if(document.getElementById('fx1651').value == '8041' && document.getElementById('fx1631').value == '7951') {
    alert("Please select a height of more than 45cm, or choose a bike without Crossbar...");
    return false;
  }
  return true;
}
function cleanFindStorePostcode(id, submit) {
  var field = document.getElementById(id);
  if(submit == false) {
    if(document.getElementById('find_store_postcode').value == '') {
      document.getElementById('find_store_postcode').value = 'POSTCODE:';
    }
  }
  if(id == 'find_store_postcode' && field.value == 'POSTCODE:') {
    field.value = '';
  }
}
function findStorePostcodeSubmit() {
  cleanEnquireNow('find_store_postcode', true);
}
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;
}
var map;
var point;
var marker;
var html;
function mapload() {
  if(GBrowserIsCompatible()) {
    var html = document.getElementById('googlemap').innerHTML;
    html = html.replace('linkname','linkname2');
    html = html.replace('getDirToggle','getDirToggle2');
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(51.409486, -1.507874), 15);
    point = new GLatLng(51.409486, -1.507874);
    marker = new GMarker(point);
    map.addOverlay(marker)
    map.openInfoWindowHtml(map.getCenter(), html);
    GEvent.addListener(marker, "click", function() {
      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";
    }
    var html = document.getElementById('googlemap').innerHTML;
    html = html.replace('linkname','linkname2');
    html = html.replace('getDirToggle','getDirToggle2');
    map.openInfoWindowHtml(point, html);
  }
}
function switch_product_image(image, title) {
  var pImage;
  pImage = document.getElementById('product_image_src');
  pImage.src = 'images/products/main/' + image;
  pImage.title = title;
}
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;
    }
  }
  xmlhttp.send(null);
}

function getBasketPrice(phpFile, objID, objID2) {
  var obj = document.getElementById(objID);

  if(typeof(objID2) != 'undefined') {
    var obj2 = document.getElementById(objID2);
  }

  var title = new Array();
  var value = new Array();
  xmlhttp.open("GET", phpFile);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      //alert(xmlhttp.responseText);
      obj.innerHTML = xmlhttp.responseText;
      if(typeof(objID2) != 'undefined') {
        obj2.innerHTML = xmlhttp.responseText;
      }
    }
  }
  xmlhttp.send(null);
}

function updatePrice(prodID, menuID, variantType) {
  if(document.getElementById('variantsIDs_' + prodID)) {
    var variantsIDs = document.getElementById('variantsIDs_' + prodID).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 + "'", "product_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);
  }
}
function getShipping() {
  var country = document.getElementById('delivery_country').value;
  var subTotal = document.getElementById('sub_total').innerHTML;
  getPrice("logicajax/shippingCharge.php?country=" + country + '&subTotal=' + subTotal, "delivery_menu");
}
function getBasketShipping() {
  var country = document.getElementById('basket_country').value;
  var subTotal = document.getElementById('sub_total').innerHTML;
  //alert("logicajax/basketShippingCharge.php?country=" + country + '&subTotal=' + subTotal);
  getBasketPrice("logicajax/basketShippingCharge.php?country=" + country + '&subTotal=' + subTotal, "basket_delivery_menu");
}
function getCheckoutShipping() {
  var country = document.getElementById('invoice_country').value;
  var subTotal = document.getElementById('sub_total').innerHTML;
  //alert("logicajax/basketShippingCharge.php?country=" + country + '&subTotal=' + subTotal);
  getBasketPrice("logicajax/basketShippingCharge.php?country=" + country + '&subTotal=' + subTotal, "basket_delivery_menu");
}
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);
}
function toggle(id) {
  var testimonial;
  testimonial = document.getElementById('review_'+id+'_short').style.display;
  if(testimonial == 'none') {
    document.getElementById('review_'+id+'_short').style.display = 'block';
    document.getElementById('review_'+id+'_full').style.display = 'none';
  } else {
    document.getElementById('review_'+id+'_short').style.display = 'none';
    document.getElementById('review_'+id+'_full').style.display = 'block';
  }
}
function hideAllAnswers() {
  for(i = 1;i <= 6;i++) {
    if(document.getElementById('q' + i)) {
      hideAnswer(document.getElementById('q' + i));
    }
  }
}
function showAnswer(q) {
  q.onclick = function () {hideAnswer(this);};
  var id = q.id.substr(1);
  if(document.getElementById('50_a' + id)) {
    document.getElementById('50_a' + id).style.display = "block";
  }
}
function hideAnswer(q) {
  q.onclick = function () {showAnswer(this);};
  var id = q.id.substr(1);
  if(document.getElementById('50_a' + id)) {
    document.getElementById('50_a' + id).style.display = "none";
  }
}


var bannerImgs = [];
var bannerNames = [];
var bannerSideImgs = [];
var bannerSideNames = [];
var bannerLink = [];

bannerImgs[0] = 'home-winter5-center.jpg';
bannerNames[0] = 'Commuting bike';
bannerSideImgs[0] = 'home-commuting-left.jpg';
bannerSideNames[0] = 'Commuting bike';
bannerLink[0] = 'section.htm?section=commuting-electric-bikes';

bannerImgs[1] = 'home-commuting-centerORIG.jpg';
bannerNames[1] = 'Commuting bike';
bannerSideImgs[1] = 'home-commuting-left.jpg';
bannerSideNames[1] = 'Commuting bike';
bannerLink[1] = 'section.htm?section=commuting-electric-bikes';

bannerImgs[2] = 'home-recreational-center.jpg';
bannerNames[2] = 'Recreational bike';
bannerSideImgs[2] = 'home-recreational-left.jpg';
bannerSideNames[2] = 'Recreational bike';
bannerLink[2] = 'section.htm?section=comfort-electric-bikes';

bannerImgs[3] = 'home-sport-center.jpg';
bannerNames[3] = 'Sports bike';
bannerSideImgs[3] = 'home-sport-left.jpg';
bannerSideNames[3] = 'Sports bike';
bannerLink[3] = 'section.htm?section=sport-electric-bikes';

var c = 0;

function home_image_rotator(){
  c = (c + 1) % bannerImgs.length;
  document.getElementById("rotator_cont").innerHTML = "<a href=\""+bannerLink[c]+"\"><img src=\"images/template/"+bannerImgs[c]+"\" alt=\""+bannerNames[c]+"\" width=410 height=390 border=0></a>";
  document.getElementById("rotator_side_cont").innerHTML = "<a href=\""+bannerLink[c]+"\"><img src=\"images/template/"+bannerSideImgs[c]+"\" alt=\""+bannerSideNames[c]+"\" width=95 height=186 border=0></a>";
}

// dynamically scroll to anchors (for all links with a class of 'auto-scroll')
function setup_autoscroll() {
  $(".auto-scroll").click(function(event){
    // prevent the default action for the click event
    event.preventDefault();
    // get the full anchor link - e.g. mysite.com/index.htm#home
    var fullLinkUrl = this.href;
    // split the url by # and get the anchor target name - i.e. 'home' in mysite.com/index.htm#home
    var parts = fullLinkUrl.split("#");
    var target = parts[1];
    // get the top offset of the target anchor
    var targetOffset = $("#"+target).offset();
    var targetTop = targetOffset.top;
    // go to that anchor by setting the body scroll top to anchor top
    $('html, body').animate({scrollTop:targetTop}, 500);
  });
}

