window.onload = initForm;

function initForm() { 
  if(document.getElementById('newsletter-subscriber')){
       InsertFlashObject("newsletter-subscriber", "flash/newsletter-subscriber.swf",213,168);
  }
   if(document.getElementById('top-flash')){
  	   InsertFlashObject("top-flash", "flash/banner.swf",768,144);
  }
  external_links();
  if(document.getElementById('delivery_country')){
     document.getElementById('delivery_country').onchange = getShipping;       
  } 
  if(document.getElementById('delivery_country') && document.getElementById('delivery_country').value != ''){  
     getShipping();       
  } 
}

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


/* Driver Downloads */

var http_request = false;

function makePOSTRequest(url, parameters, type, vartype) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
         // set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  
  if (type=='models') {
    http_request.onreadystatechange = updateModels;
  } else {
    http_request.onreadystatechange = updateDownloads;
  }

  http_request.open(vartype, url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function updateModels() {
  if (http_request.readyState == 4) {
     if (http_request.status == 200) {

        targetObj = document.getElementById('product_selector');
        targetObj.options.length=0;
        targetObj[0] = new Option('', '');
        
        models = http_request.responseText;
        models = models.split('|');

        for (i = 0; i<models.length-1; i++) {  
            model = models[i].split(':');
            value = model[0];
            text = model[1];                
            targetObj[i] = new Option(text, value);
        }
        document.getElementById('product_selector').disabled=false;
        document.getElementById('downloads').innerHTML = "";
     } else {
        alert('There was a problem with the request.');
     }
  }
}

function updateDownloads() {
  if (http_request.readyState == 4) {
     if (http_request.status == 200) {
        faults = http_request.responseText;
        document.getElementById('downloads').innerHTML = faults;
     } else {
        alert('There was a problem with the request.');
     }
  }
}

function get_products() {
  selectedIndex = document.getElementById('category_selector').selectedIndex;
  categoryId = document.getElementById('category_selector').options[selectedIndex].value;
  postStr = "category_id=" + categoryId;
  makePOSTRequest('logicajax/products.php', postStr, 'models', 'POST');
}

function get_text() {
  selectedIndex = document.getElementById('category_selector').selectedIndex;
  makeId = document.getElementById('category_selector').options[selectedIndex].value ;
  selectedIndex = document.getElementById('product_selector').selectedIndex;
  modelId = document.getElementById('product_selector').options[selectedIndex].value ;
  postStr = "category_id=" + makeId + "&product_id=" + modelId;
  makePOSTRequest('logicajax/downloads.php', postStr, 'faults', 'POST');
}

function get_text_get(makeId, modelId, pageNumber) {
  postStr = "make_id=" + makeId + "&model_id=" + modelId + "&page=" + pageNumber;
  makePOSTRequest('faults.php', postStr, 'faults', 'GET');
}

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