// Function for clearing input box default values
function clearDefaultandCSS(el) {
	if (el.defaultValue==el.value) el.value = ""
	// If Dynamic Style is supported, clear the style
	if (el.style) el.style.cssText = "width: 171px;"
}


// Create an XMLHttpRequest object...
function getXMLHTTP()
{
  try
  {
    request_o = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch(ex)
  {
    //either this is not IE, or it is a version of IE which does not support XMLHTTP
    var notIECompatibleXMLHTTP=true;
  }
  if(notIECompatibleXMLHTTP==true)
  {
    try
    {
      request_o = new XMLHttpRequest();
    }
    catch(ex)
    {
      //we can't use AJAX because this browser is not compatible.
      request_o = false;
    }
  }
  return request_o;
}

// Functions for the various AJAX-using pages
function init_select_product()
{
  // Check if AJAX is supported
  xmlHttp=getXMLHTTP();
  if (xmlHttp==null)
  {
    alert ("Unfortunately your browser is too old to support our quote system. "+
           "Please upgrade your browser or telephone us for a quote instead.");
    return;
  }
  var url="/ajax.php";
  url=url+"?type=product";
  url=url+"&section="+document.getElementById("section").value;
  url=url+"&sid="+Math.random();
  xmlHttp.onreadystatechange=do_select_product;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}
function do_select_product()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
    document.getElementById("selectproduct").innerHTML=xmlHttp.responseText;
  }
}

function product_selected()
{
  xmlHttp=getXMLHTTP();
  // Need to clear and disable all the other controls
  var crlf=String.fromCharCode(13)+String.fromCharCode(10);
  document.getElementById("selectdate").innerHTML="            <select name='date' id='date' style='width: 175px;' disabled>"+crlf+
"              <option value='false'>select price above...</option>"+crlf+
"            </select>";
  document.getElementById("selectlength").innerHTML="            <select name='length' id='length' style='width: 175px;' disabled>"+crlf+
"              <option value='false'>select date above...</option>"+crlf+
"            </select>";
  document.getElementById("selectbrand").innerHTML="            <input type='text' name='brand' id='brand' style='width: 171px;'"+crlf+
"                   value='select warranty length above...' disabled />";
  document.getElementById("selectsubmit").innerHTML="          <input name='submit' type='submit' value='submit' disabled />";

  if (document.getElementById("product").value == 'false')
  {
    // Disable price too because no product is selected
    document.getElementById("selectprice").innerHTML="            <select name='price' id='price' style='width: 175px;' disabled>"+crlf+
"              <option value='false'>select product above...</option>"+crlf+
"            </select>";
  } else {
    document.getElementById("selectprice").innerHTML="            <select name='price' id='price' style='width: 175px;'>"+crlf+
"              <option value='false'>loading prices...</option>"+crlf+
"            </select>";
    var url="/ajax.php";
    url=url+"?type=prodsel";
    url=url+"&section="+document.getElementById("section").value;
    url=url+"&item="+document.getElementById("product").value;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=do_product_selected;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
  }
}
function do_product_selected()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
    document.getElementById("selectprice").innerHTML=xmlHttp.responseText;
  }
}


function price_selected()
{
  xmlHttp=getXMLHTTP();
  var crlf=String.fromCharCode(13)+String.fromCharCode(10);
  if (document.getElementById("price").value == 'false')
  {
    // Need to clear and disable subsequent controls
    document.getElementById("selectdate").innerHTML="            <select name='date' id='date' style='width: 175px;' disabled>"+crlf+
"              <option value='false'>select price above...</option>"+crlf+
"            </select>";
    document.getElementById("selectlength").innerHTML="            <select name='length' id='length' style='width: 175px;' disabled>"+crlf+
"              <option value='false'>select date above...</option>"+crlf+
"            </select>";
    document.getElementById("selectbrand").innerHTML="            <input type='text' name='brand' id='brand' style='width: 171px;'"+crlf+
"                   value='select warranty length above...' disabled />";
    document.getElementById("selectsubmit").innerHTML="          <input name='submit' type='submit' value='submit' disabled />";
  } else {
    // Need to clear and disable last three controls
    document.getElementById("selectlength").innerHTML="            <select name='length' id='length' style='width: 175px;' disabled>"+crlf+
"              <option value='false'>select date above...</option>"+crlf+
"            </select>";
    document.getElementById("selectbrand").innerHTML="            <input type='text' name='brand' id='brand' style='width: 171px;'"+crlf+
"                   value='select warranty length above...' disabled />";
    document.getElementById("selectsubmit").innerHTML="          <input name='submit' type='submit' value='submit' disabled />";
    var url="/ajax.php";
    url=url+"?type=pricesel";
    url=url+"&section="+document.getElementById("section").value;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=do_price_selected;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
  }
}
function do_price_selected()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
    document.getElementById("selectdate").innerHTML=xmlHttp.responseText;
  }
}


function date_selected()
{
  document.getElementById("selectbrand").innerHTML="            <input type='text' name='brand' id='brand' style='width: 171px;'"+crlf+
"                   value='select warranty length above...' disabled />";
  document.getElementById("selectsubmit").innerHTML="          <input name='submit' type='submit' value='submit' disabled />";
  xmlHttp=getXMLHTTP();
  if ((document.getElementById("month").value == 'false') || (document.getElementById("year").value == 'false'))
  {
    var crlf=String.fromCharCode(13)+String.fromCharCode(10);
    // Need to clear and disable subsequent controls
    document.getElementById("selectlength").innerHTML="            <select name='length' id='length' style='width: 175px;' disabled>"+crlf+
"              <option value='false'>select date above...</option>"+crlf+
"            </select>";
  } else {
    var url="/ajax.php";
    url=url+"?type=datesel";
    url=url+"&section="+document.getElementById("section").value;
    url=url+"&month="+document.getElementById("month").value;
    url=url+"&year="+document.getElementById("year").value;
    url=url+"&sid="+Math.random();
    // ...?
    xmlHttp.onreadystatechange=do_date_selected;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
  }
}
function do_date_selected()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
    document.getElementById("selectlength").innerHTML=xmlHttp.responseText;
  }
}

function length_selected()
{
  if (document.getElementById("length").value == 'false')
  {
    var crlf=String.fromCharCode(13)+String.fromCharCode(10);
    // Need to disable subsequent controls
    document.getElementById("selectbrand").innerHTML="            <input type='text' name='brand' id='brand' style='width: 171px;'"+crlf+
"                   value='select warranty length above...' disabled />";
    document.getElementById("selectsubmit").innerHTML="          <input name='submit' type='submit' value='submit' disabled />";
  } else {
    var crlf=String.fromCharCode(13)+String.fromCharCode(10);
    document.getElementById("selectbrand").innerHTML="            <input type='text' name='brand' id='brand' style='width: 171px; font-style: italic'"+crlf+
"                   value='Enter brand name...' onFocus='clearDefaultandCSS(this)' onKeyUp='brand_selected()' />";
  }
}

function brand_entered()
{
//  if (document.getElementById("brand").value == 'Enter brand name...')
  if (document.getElementById("brand").defaultValue==document.getElementById("brand").value)
  {
    // Clear the brand box when we first enter it.
    document.getElementById("brand").value == '';
  }
}

function brand_selected()
{
  if (document.getElementById("brand").value.length == 0)
  {
    // Need to disable subsequent control
    document.getElementById("selectsubmit").innerHTML="          <input name='submit' type='submit' value='submit' disabled />";
  } else {
    document.getElementById("selectsubmit").innerHTML="          <input name='submit' type='submit' value='submit' />";
  }
}

