window.onload = initPage;

function initPage() {
  
  if(document.getElementById('flash_object')) {
    InsertFlashObject("flash_object", "flash/home.swf", 491, 491); 
  }
  
  if(document.getElementById('home_flash_2')) {
    InsertFlashObject('home_flash_2', 'flash/home_news_491x190.swf', 491, 190);
  }
  
  externalLinks();
    
  if(document.getElementById('manufacturer') && document.getElementById('type').value == 1) {                              
    document.getElementById('order_by').onchange = order_by_normal;    
    document.getElementById('manufacturer').onchange = get_by_man;    
  }
  if(document.getElementById('manufacturer') && document.getElementById('type').value == 2) {
    document.getElementById('order_by').onchange = order_by_man;    
  }
  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').className = 'form_vatnumber_cont_visible'; 
    } else {
      document.getElementById('country_abrv').className = 'form_country_abrv_cont';
      document.getElementById('vat_number').className = 'form_vatnumber_cont';
    }
  }  
}

function popup_video(ID) {
  var videoID = ID;
  window.open("index.php?view=video&video=" + videoID, 'CapitalCapture', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,width=640,height=480');
  return false;
}

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

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;
  var manufacturer = document.getElementById('manufacturer').value;
  var type = document.getElementById('type').value;
  window.location = 'index.php?view=section&section=' + section + '&manufacturer=' + manufacturer + '&order=' + this.value + '&type=' + type; return true;
}

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

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

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

/*function setShippingOption() {
  var country = document.getElementById('delivery_country');
  if(country.value == 200){
    alert(document.getElementById('shipping').selectedIndex);
  document.getElementById('shipping_cost').value = document.getElementById('shipping').selectedIndex;                  
  }
}*/

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;              
     // setShippingOption();  
    }
  }  
  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').className = 'form_vatnumber_cont_visible';
  } else {
   document.getElementById('country_abrv').className = 'form_country_abrv_cont';
   document.getElementById('vat_number').className = 'form_vatnumber_cont';
  }
}

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

function getSupportPlans(productID) {
  xmlhttp.open("GET", "logicajax/supportproducts.php?product=" + productID);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {    
      document.getElementById('support_selection').innerHTML =  xmlhttp.responseText;
    }
  }  
  xmlhttp.send(null);   
}

function toggleDiv(id) {
  divID = document.getElementById('toggle_' + id);
  if(divID.style.height == '42px') {
    divID.style.height = 'auto';
    divID.style.overflow = 'visible';
  } else {
    divID.style.height = '42px';
    divID.style.overflow = 'hidden';
  }
}