//** functions.js
//**
//** blah...
//**
//** Author:  Greg Merriman
//** Created: Wed 25 Feb 2009



function addLoadEvent( func )
{
  var oldonload = window.onload;
  
  if (typeof window.onload != 'function')
  {
    window.onload = func;
  }
  else
  {
    window.onload = function()
    {
      oldonload();
      func();
    }
  }
}


  
function prepareAjax()
{
  //create a boolean variable to check for valid IE instance
  var xmlhttp = false;
  
  //check if we are using IE
  try
  {
    //if the JavaScript version is greater than 5
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
    //if not, then use the older active x object
    try
    {
      //if we are using IE
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (E)
    {
      //else we must be using a non-IE browser
      xmlhttp = false;
    }
  }
  
  //if we are using a non-IE browser
  //create a JavaScript instance of the object
  if(!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
    xmlhttp = new XMLHttpRequest();
  }
  
  return xmlhttp;
}



function processAjax( requestedPage, requestMethod, requestString, htmlElement )
{ 
  //get an XMLHttpRequest object for use
  xmlhttp = prepareAjax();
  
  if(requestMethod == "get")
  {
    xmlhttp.open("GET", requestedPage);
    xmlhttp.onreadystatechange = function()
    {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
      {
        htmlElement.innerHTML = xmlhttp.responseText;
        //var newdiv = document.createElement("div");
        //newdiv.innerHTML = xmlhttp.responseText;
        //htmlElement.appendChild(newdiv);
      }
    }
    xmlhttp.send(null);
  }
  else
  {
    xmlhttp.open("POST", requestedPage, true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlhttp.onreadystatechange = function()
    {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
      {
        htmlElement.innerHTML = xmlhttp.responseText;
        //var newdiv = document.createElement("div");
        //newdiv.innerHTML = xmlhttp.responseText;
        //htmlElement.appendChild(newdiv);
      }
    }
    xmlhttp.send(requestString);
  }
}



function preparePackageSelector()
{
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("product_selector")) return false;
  
  var selector = document.getElementById("product_selector");
  
  var inputs = selector.getElementsByTagName("input");
  
  for (var i=0; i<inputs.length; i++ )
  {
    inputs[i].onclick = function()
    {
      return toggleProduct(this);
    }
  }
}



function toggleProduct( whichProduct )
{
  //the selected product
  var product = whichProduct.id;
  var selected = whichProduct.checked;
  
  //the location we are loading the product data into...
  var elementID = "basket_contents";
  
  var requestPage = "buildpackage.php?product=" + product + "&selected=" + selected;
  
  var htmlElement = document.getElementById(elementID);
  
  //process the request
  //processAjax( requestedPage, requestMethod, requestString, htmlElement )
  processAjax(requestPage, "get", "", htmlElement);
}

addLoadEvent(preparePackageSelector);
