window.onload = initPage;

function initPage() {
  externalLinks();
  if(document.getElementById('map')) {
    load();
  }
  if(document.getElementById('hire_section')) {
    document.getElementById('hire_section').onchange = getHireProducts;
  }
  if(document.getElementById('manufacturer') && document.getElementById('manufacturer').value != null) {
    document.getElementById('order_by').onchange = order_by_man;
  }
  if(document.getElementById('manufacturer') && document.getElementById('manufacturer').value == null) {
    document.getElementById('order_by').onchange = order_by_normal;
  }
  if(document.getElementById('manufacturer_select')) {
    document.getElementById('manufacturer_select').onchange = redirectMan;
  }
  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('vat[vat]')) {
    document.getElementById('vat[vat]').onclick = enableVATExmeptField;
    if(document.getElementById('vat[vat]').checked) {
      document.getElementById('country_abrv').className = 'form_country_abrv_cont_visible';
      document.getElementById('vat_number_cont').className = 'form_vatnumber_cont_visible';
    } else {
      document.getElementById('country_abrv').className = 'form_country_abrv_cont';
      document.getElementById('vat_number_cont').className = 'form_vatnumber_cont';
    }
  }
  if(document.getElementById('back')) {
    document.getElementById('back').onclick = GoBack;
  }
  if(document.getElementById('ctaflash')) {
    InsertFlashObject('ctaflash', 'flash/red/small_small_cta.swf', 186, 133);
  }
  if(document.getElementById('ctaflash2')) {
    InsertFlashObject('ctaflash2', 'flash/red/small_small_cta.swf', 186, 133);
  }
  if(document.getElementById('home_flash')) {
    InsertFlashObject('home_flash', 'flash/prokit-manufacturers.swf', 770, 149);
  }
  if(document.getElementById('quickcontact_name')) {
    document.getElementById('quickcontact_name').onfocus = clearfield;
    document.getElementById('quickcontact_name').onblur = reinstatefield;
    document.getElementById('quickcontact_email').onfocus = clearfield;
    document.getElementById('quickcontact_email').onblur = reinstatefield;
    document.getElementById('quickcontact_telephone').onfocus = clearfield;
    document.getElementById('quickcontact_telephone').onblur = reinstatefield;
    document.getElementById('quickcontact_enquiry').onfocus = clearfield;
    document.getElementById('quickcontact_enquiry').onblur = reinstatefield;
  }
}

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 clearfield() {
  switch(this.id) {
    case 'quickcontact_name':
      trimValue = trim(this.value);
      if(trimValue == 'Name') {
        this.value = '';
      }
    break;
    case 'quickcontact_email':
      trimValue = trim(this.value);
      if(trimValue == 'Email') {
        this.value = '';
      }
    break;
    case 'quickcontact_telephone':
      trimValue = trim(this.value);
      if(trimValue == 'Tel') {
        this.value = '';
      }
    break;
    case 'quickcontact_enquiry':
      trimValue = trim(this.value);
      if(trimValue == 'Enquiry') {
        this.value = '';
      }
    break;
  }
}

function reinstatefield() {
  switch(this.id) {
    case 'quickcontact_name':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Name';
      }
    break;
    case 'quickcontact_email':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Email';
      }
    break;
    case 'quickcontact_telephone':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Tel';
      }
    break;
    case 'quickcontact_enquiry':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Enquiry';
      }
    break;
  }
}

function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(51.4936753561844, -0.27995824813842773), 15);
var marker = new GMarker(new GLatLng(51.4936753561844, -0.27995824813842773));
GEvent.addListener(marker, "click", function() {
var html = '<div style="width: 210px;padding-right: 10px"><strong>PROKIT</strong><br \/>111 Power Road<br \/>London<br \/>W4 5PY<br \/><\/div>';
marker.openInfoWindowHtml(html);
});
map.addOverlay(marker);
GEvent.trigger(marker, "click");
}
}

function InsertFlashObject(objID, flashLocation, width, height, includeString) {
  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(includeString) == "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='" + includeString + "'";
    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="' + includeString + '"';
  }
  flashObject = flashObject + "></embed></object>";
  if(document.getElementById(objID)) {
    document.getElementById(objID).innerHTML = flashObject;
  }
}

function image_popup(image) {
  var image = image;
  var newLink = image.replace("main", "large");
  window.open(newLink,"","width=1024,height=768,location,menubar, resizable, scrollbars");
}

function test(image) {
  var image = image;
  document.getElementById('product_image').src= image;
  var width = document.images.product_image.width;
  var height = document.images.product_image.height;
  alert(document.images.product_image.width);
}

function externalLinks() {
  if (!document.getElementsByTagName) return;
  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 order_by_normal() {
  var section = document.getElementById('section').value;
  window.location = 'index.php?view=section&section=' + section + '&order=' + this.value;return true;
}

function order_by_man() {
  var section = document.getElementById('section').value;
  var manufacturer = document.getElementById('manufacturer').value;
  window.location = 'index.php?view=section&section=' + section + '&manufacturer=' + manufacturer +'&order=' + this.value;return true;
}

function getHireData(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) {
     
      breaker = xmlhttp.responseText.indexOf("DELIMITER",xmlhttp.responseText);
      responseLength = xmlhttp.responseText.length;
        
      valueS = xmlhttp.responseText.substring(0,breaker);
      value = valueS.split('|');
        
      titleS = xmlhttp.responseText.substring(breaker+9,responseLength);
      title = titleS.split('|');
      
      obj.options.length = 0;
      
      for(i=0;i<(title.length-1);i++) {
        obj.options[i] = new Option(title[i],value[i]);
      }
      //obj.innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}

function getHireProducts() {
  var section = this.value;
  getHireData("logicajax/hireform.php?section=" + section, "hire_product");
}

function redirectMan() {
  var man = document.getElementById('manufacturer_select');
  man = man.options[man.selectedIndex].value;
  if(man != 'red') {
    window.location = 'index.php?view=manufacturer&manufacturer=' + man;return true;
  } else {
    window.location = 'red/index.php';return true;
  }
}

function getShipping() {
  var country = document.getElementById('delivery_country').value;
  displayShipping("logicajax/shipping.php?country=" + country, "shipping_cont");
}

function getUKShipping() {
  var country = document.getElementById('delivery_country').value;
  var shippingID = document.getElementById('shipping').options[document.getElementById('shipping').selectedIndex].value;
  displayShipping("logicajax/shipping.php?country=" + country + '&shippingID=' + shippingID, "shipping_cont");
}

function displayShipping(phpFile, objID) {
  var obj = document.getElementById(objID);
  xmlhttp.open("GET", phpFile);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      obj.innerHTML =  xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}

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 enableVATExmeptField() {
  if(this.checked) {
   document.getElementById('country_abrv').className = 'form_country_abrv_cont_visible';
   document.getElementById('vat_number_cont').className = 'form_vatnumber_cont_visible';
  } else {
   document.getElementById('country_abrv').className = 'form_country_abrv_cont';
   document.getElementById('vat_number_cont').className = 'form_vatnumber_cont';
   document.getElementById('vat_number').value = '';
  }
}

function GoBack() {
  history.go(-1);
}

// make the specified div a windowed control in IE6; this masks an iframe (which is a windowed control) onto the div, turning the div into a windowed control itself
function makeWindowed(p_div) {
  var is_ie6 =
     document.all && 
     (navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1);
  if(is_ie6) {
     var html = "<iframe style=\"position: absolute;display: block; z-index: -1;width: 100%;height: 100%;top: 0;left: 0; filter: mask();background-color: #ffffff;\"></iframe>";
     if(p_div) p_div.innerHTML += html;
     // force refresh of div
     var olddisplay = p_div.style.display;
     p_div.style.display = 'none';
     p_div.style.display = olddisplay;
  };
}

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