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('area_offices')) {
    document.getElementById('area_offices').onchange = showOffice;
  }
  if(document.getElementById('mode_1')) {
    document.getElementById('order_by').onchange = order_by_normal;
  }
  if(document.getElementById('more_qualifications')) {
    document.getElementById('more_qualifications').onchange = showMore;
  }
  if(document.getElementById('basket_tab')) {
    document.getElementById('basket_tab').onmouseover = mousePointer;
    document.getElementById('basket_tab').onmouseout = mouseReset;
    document.getElementById('basket_tab').onclick = viewBasket;
  }
  if(document.getElementById('main_flash')) {
    InsertFlashObject('main_flash', 'flash/top-banner.swf', 783, 124);
  }
  if(document.getElementById('qualification')) {
    document.getElementById('qualification').onfocus = clearfield;
    document.getElementById('qualification').onblur = reinstatefield;
  }
  if(document.getElementById('qualification_year')) {
    document.getElementById('qualification_year').onfocus = clearfield;
    document.getElementById('qualification_year').onblur = reinstatefield;
  }
  if(document.getElementById('qualification_reg_number')) {
    document.getElementById('qualification_reg_number').onfocus = clearfield;
    document.getElementById('qualification_reg_number').onblur = reinstatefield;
  }
  if(document.getElementById('qualification2')) {
    document.getElementById('qualification2').onfocus = clearfield;
    document.getElementById('qualification2').onblur = reinstatefield;
  }
  if(document.getElementById('qualification_year2')) {
    document.getElementById('qualification_year2').onfocus = clearfield;
    document.getElementById('qualification_year2').onblur = reinstatefield;
  }
  if(document.getElementById('qualification_reg_number2')) {
    document.getElementById('qualification_reg_number2').onfocus = clearfield;
    document.getElementById('qualification_reg_number2').onblur = reinstatefield;
  }
  if(document.getElementById('business_postcode')) {
    document.getElementById('business_postcode').onfocus = clearfield;
    document.getElementById('business_postcode').onblur = reinstatefield;
  }
  if(document.getElementById('more_info')) {
    document.getElementById('more_info').onfocus = clearfield;
    document.getElementById('more_info').onblur = reinstatefield;
  }
  if(document.getElementById('area_postcode')) {
    document.getElementById('area_postcode').onfocus = clearfield;
    document.getElementById('area_postcode').onblur = reinstatefield;
  }
  if(document.getElementById('safety_inspection_enquiry')) {
    document.getElementById('safety_inspection_enquiry').onfocus = clearfield;
    document.getElementById('safety_inspection_enquiry').onblur = reinstatefield;
  }
  if(document.getElementById('reg_management_enquiry')) {
    document.getElementById('reg_management_enquiry').onfocus = clearfield;
    document.getElementById('reg_management_enquiry').onblur = reinstatefield;
  }
  if(document.getElementById('electrical_enquiry')) {
    document.getElementById('electrical_enquiry').onfocus = clearfield;
    document.getElementById('electrical_enquiry').onblur = reinstatefield;
  }
  if(document.getElementById('fireofficers_enquiry')) {
    document.getElementById('fireofficers_enquiry').onfocus = clearfield;
    document.getElementById('fireofficers_enquiry').onblur = reinstatefield;
  }
  if(document.getElementById('gsave_calculator')) {
    document.getElementById('gsave_calculator').onsubmit = initCalculator;
  }
}

function viewBasket() {
  window.location = "basket.html";
}

function mousePointer() {
  this.style.cursor = 'pointer';
}

function mouseReset() {
  this.style.cursor = 'default';
}

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";
      }
    }
  }
}

// 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;
}

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;
      }
    }
  }
  xmlhttp.send(null);
}

function updatePrice(prodID, menuID, variantType) {
  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 + "'", "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;
  // Commented out as delivery and VAT included in price
  //getPrice("logicajax/shippingCharge.php?country=" + country + '&shippingID=' + shippingID + '&subTotal=' + subTotal, "delivery_menu");
}

function showMore() {
  displayMore = this.value;
  if(displayMore == 2) {
    displayMoreQualificationFields();
  }
}

function showMoreMore(id) {
  displayMore =  document.getElementById(id).value;
  if(displayMore == 2) {
    displayMoreQualificationFields();
  }
}

function order_by_normal() {
  var section = document.getElementById('section').value;
  window.location = 'shop.html?order=' + this.value;
  return true;
}

function image_popup(image) {
  var image = image;
  var newLink = image.replace("main", "large");
  window.open(newLink, "", "width=800, height=600, location, menubar");
}

function toggleSelects() {
  var rv = -1;// Return value assumes failure.
  if(navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if(re.exec(ua) != null) {
      rv = parseFloat( RegExp.$1 );
    }
  }

  if(rv == 6) {
    for(var i = 0;i < document.forms.length;i++) {
      for(var e = 0;e < document.forms[i].length;e++) {
        if(document.forms[i].elements[e].tagName == 'SELECT') {
          if(document.forms[i].elements[e].style.visibility == 'hidden') {
            document.forms[i].elements[e].style.visibility = 'visible';
          } else {
            document.forms[i].elements[e].style.visibility = 'hidden';
          }
        }
      }
    }
  }
}

function clearfield() {
  switch(this.id) {
    case 'qualification':
      trimValue = trim(this.value);
      if(trimValue == '[Qualification]') {
        this.value = '';
      }
    break;
    case 'qualification_year':
      trimValue = trim(this.value);
      if(trimValue == '[Year obtained]') {
        this.value = '';
      }
    break;
    case 'qualification_reg_number':
      trimValue = trim(this.value);
      if(trimValue == '[Registration Number]') {
        this.value = '';
      }
    break;
    case 'business_postcode':
      trimValue = trim(this.value);
      if(trimValue == 'State postcodes') {
        this.value = '';
      }
    break;
    case 'more_info':
      trimValue = trim(this.value);
      if(trimValue == 'Add additional qualifications or more information') {
        this.value = '';
      }
    break;
    case 'area_postcode':
      trimValue = trim(this.value);
      if(trimValue == 'Postcode') {
        this.value = '';
      }
    break;
    case 'safety_inspection_enquiry':
      trimValue = trim(this.value);
      if(trimValue == 'Franchise Enquiry') {
        this.value = '';
        this.style.color = '#000';
      }else{
        this.style.color = '#000';
      }
    break;
    case 'reg_management_enquiry':
      trimValue = trim(this.value);
      if(trimValue == 'Franchise Enquiry') {
        this.value = '';
        this.style.color = '#000';
      }else{
        this.style.color = '#000';
      }
    break;
    case 'electrical_enquiry':
      trimValue = trim(this.value);
      if(trimValue == 'Franchise Enquiry') {
        this.value = '';
        this.style.color = '#000';
      }else{
        this.style.color = '#000';
      }
    break;
    case 'fireofficers_enquiry':
      trimValue = trim(this.value);
      if(trimValue == 'Experienced Fire Officers') {
        this.value = '';
        this.style.color = '#000';
      }else{
        this.style.color = '#000';
      }
    break;
  }
}

function reinstatefield() {
  switch(this.id) {
    case 'qualification':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = '[Qualification]';
      }
    break;
    case 'qualification_year':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = '[Year obtained]';
      }
    break;
    case 'qualification_reg_number':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = '[Registration Number]';
      }
    break;
    case 'business_postcode':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'State postcodes';
      }
    break;
    case 'more_info':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Add additional qualifications or more information';
      }
    break;
    case 'area_postcode':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Postcode';
      }
    break;
    case 'safety_inspection_enquiry':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Franchise Enquiry';
      }else if(this.value != 'Franchise Enquiry'){
        this.style.color = '#000';
      }
    break;
    case 'reg_management_enquiry':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Franchise Enquiry';
      }else if(this.value != 'Franchise Enquiry'){
        this.style.color = '#000';
      }
    break;
    case 'electrical_enquiry':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Franchise Enquiry';
      }else if(this.value != 'Franchise Enquiry'){
        this.style.color = '#000';
      }
    break;
    case 'fireofficers_enquiry':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Experienced Fire Officers';
      }else if(this.value != 'Experienced Fire Officers'){
        this.style.color = '#000';
      }
    break;
  }
}

function clearfield2(id) {
  elem = document.getElementById(id);
  switch(id) {
    case 'qualification2':
      trimValue = trim(elem.value);
      if(trimValue == '[Qualification]') {
        elem.value = '';
      }
    break;
    case 'qualification_year2':
      trimValue = trim(elem.value);
      if(trimValue == '[Year obtained]') {
        elem.value = '';
      }
    break;
    case 'qualification_reg_number2':
      trimValue = trim(elem.value);
      if(trimValue == '[Registration Number]') {
        elem.value = '';
      }
    break;
  }
}

function reinstatefield2(id) {
  elem = document.getElementById(id);
  switch(id) {
     case 'qualification2':
      trimValue = trim(elem.value);
      if(trimValue == '') {
        elem.value = '[Qualification]';
      }
    break;
    case 'qualification_year2':
      trimValue = trim(elem.value);
      if(trimValue == '') {
        elem.value = '[Year obtained]';
      }
    break;
    case 'qualification_reg_number2':
      trimValue = trim(elem.value);
      if(trimValue == '') {
        elem.value = '[Registration Number]';
      }
    break;
  }
}

function trim(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function exp_col(section){
  var content = 'qa_answer_'+section;
  var link = 'qa_button_'+section;
  /*if(document.getElementById(content).style.display == 'none'){
    document.getElementById(content).style.display = 'block';
    document.getElementById(link).innerHTML = '<a href=\"javascript: void(0); exp_col_left_cat('+section+', \''+name+'\')\" title=\"Collapse '+ name +' section\">\r\nCollapse\r\n<br/>\r\n<img src=\"images/template/collapse_arrow.gif\" width=\"9\" height=\"10\" alt=\"Collapse section\" title=\"Collapse '+ name +' section\" />\r\n</a>\r\n';
  }else{
    document.getElementById(content).style.display = 'none';
    document.getElementById(link).innerHTML = '<a href=\"javascript: void(0); exp_col_left_cat('+section+', \''+name+'\')\" title=\"Expand '+ name +' section\">\r\nExpand\r\n<br/>\r\n<img src=\"images/template/expand_arrow.gif\" width=\"9\" height=\"10\" alt=\"Expand section\" title=\"Expand '+ name +' section\" />\r\n</a>\r\n';
  }*/
  if(document.getElementById(content).className == 'qa_answer'){
    document.getElementById(content).className = 'qa_answer_expanded';
    document.getElementById(link).innerHTML = '<a href=\"javascript: void(0); exp_col('+section+')\" title=\"Hide answer\"><img src=\"images/buttons/form-answer.gif\" width=\"93\" height=\"21\" alt=\"Hide Answer\" title=\"Hide Answer\" />\r\n</a>\r\n';
  }else{
    document.getElementById(content).className = 'qa_answer';
    document.getElementById(link).innerHTML = '<a href=\"javascript: void(0); exp_col('+section+')\" title=\"Show answer\"><img src=\"images/buttons/form-answer.gif\" width=\"93\" height=\"21\" alt=\"Show Answer\" title=\"Show Answer\" />\r\n</a>\r\n';
  }
}
