function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

var request = createRequestObject(); 

function getYears() {
	//do request
    resetSelect(document.getElementById("year"),"Year"); 
    var product = getQueryVariable("product");
   		
	request.open("get", "search_data.aspx?data=years&product=" + product, true);
	request.onreadystatechange = handleYears; 
	request.send(null);
	
    //populate division dropdown and clear model dropdown
   /*  */

}


function getMakes() {
	//do request
    resetSelect(document.getElementById("model"),"Model"); 
    var product = getQueryVariable("product");
    if(document.dkSearch.year.value=="")
	{
		alert("Please select the Year");
		document.getElementById('loader').innerHTML=""; 
		document.dkSearch.year.selectedIndex=0;
	}
	else
	{    		
		request.open("get", "search_data.aspx?data=make&product=" + product + "&modelYear=" + document.dkSearch.year.value, true);
		request.onreadystatechange = handleMake; 
		request.send(null);
	}

    //populate division dropdown and clear model dropdown
   /*  */

}

function getModels() {

	var product = getQueryVariable("product");
	if(document.dkSearch.year.value=="")
	{
		alert("Please select the Year");
		document.getElementById('loader').innerHTML=""; 
		document.dkSearch.make.selectedIndex=0;
	}
	else
	{
		//do request
		request.open("get", "search_data.aspx?data=models&product=" + product + "&modelYear=" + document.dkSearch.year.value + "&make=" + document.dkSearch.make.value, true);
	   	request.onreadystatechange = handleModel; 
		request.send(null);
	}	
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleYears(){
		if(request.readyState == 1){		
			document.getElementById('loader').innerHTML="<table width='100%' align='center'><tr><td width='50%' align='right'><img src='../images/ajax-loader.gif' /></td><td class='styleload1'><strong>Loading...</strong></td></tr></table>";
		}
		if(request.readyState == 4){				
			var response = request.responseText;
			fillSelect(document.getElementById("year"), response); 
			document.getElementById('loader').innerHTML=""; 
		}
	return request.readyState
}

function handleMake(){
		if(request.readyState == 1){		
			document.getElementById('loader').innerHTML="<table width='100%' align='center'><tr><td width='50%' align='right'><img src='../images/ajax-loader.gif' /></td><td class='styleload1'><strong>Loading...</strong></td></tr></table>";
		}
		if(request.readyState == 4){				
			var response = request.responseText;
			fillSelect(document.getElementById("make"), response); 
			document.getElementById('loader').innerHTML=""; 
		}
	return request.readyState
}

function handleModel(){
		if(request.readyState == 1){
			document.getElementById('loader').innerHTML="<table width='100%' align='center'><tr><td width='50%' align='right'><img src='../images/ajax-loader.gif' /></td><td class='styleload1'><strong>Loading...</strong></td></tr></table>";
		}
		if(request.readyState == 4){ 
			var response = request.responseText;
			fillSelect(document.getElementById("model"), request.responseText);
			document.getElementById('loader').innerHTML=""; 
	}
	return request.readyState
}


function getSearchDetails()
{
	var m;
	var n;
	var y;
	m=0;
	y=0;
	n=0;
	
	if(document.dkSearch.model.value=="")
		{
			n=1;
		}
	
	if(document.dkSearch.model.value!="")
	{
		if(document.dkSearch.make.value=="")
			{
				alert("Please select the make");
				m=1;

			}
		if(document.dkSearch.year.value=="")
			{
				alert("Please select the Year");
				y=1;
			}

		if(m==0 && y==0)
			{
				document.dkSearch.pts.value=0;
				getSearch();
			}
	}
		
	if(m=="1" || n=="1" || y=="1")
		{
			document.getElementById('searchdetails').innerHTML=""; 
		}
}

function getSearch()
{
	var modelYear = document.getElementById("year").value;
	var make = document.getElementById("make").value;
	var model = document.getElementById("model").value;
	var product = getQueryVariable("product");
	var client = getQueryVariable("clientID");

	document.getElementById("make").disabled=false;
	document.getElementById("model").disabled=false;
	
	request.open("get", "search_data.aspx?data=doSearch&modelYear=" + modelYear + "&make=" + make + "&model=" + model + "&product=" + product + "&client=" + client, true);
	request.onreadystatechange = handleSearch; 
	request.send(null);
}

function handleSearch(){
		var product = getQueryVariable("product");
		var qty = getQueryVariable("qty");

		if(request.readyState == 1){
			document.getElementById('loader').innerHTML="<table width='100%' align='center'><tr><td width='50%' align='right'><img src='../images/ajax-loader.gif' /></td><td class='styleload1'><strong>Loading...</strong></td></tr></table>";
		}
	
	if(request.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = request.responseText;

		var loadTable;
		
		if(response.length<=2){
					document.getElementById('loader').innerHTML="<table border='0' width='100%'><tr height='40'><td align='center'><font color='red'><b>NO RESULTS<b></font></td></tr></table>";
		}
		else{	
			buildTables(product,qty,response);		
			
		}

	}
	//return request.readyState
}


function getPartSearchDetails()
{
	if(document.dkSearch.partnumber.value=="")
	{
		alert("Please enter partnumber search value");
	}
	else
	{
		if(document.dkSearch.partnumber.value.length>=3)
		{
			document.dkSearch.pts.value=1;
			getPartSearch();
		}
		else
		{
			alert("Please enter at least 3 characters for part number search");
		}
	}

}


function getPartSearch()
{
	var client = getQueryVariable("clientID");
	var product = getQueryVariable("product");
	var part = document.getElementById("partnumber").value;

	request.open("get", "search_data.aspx?data=doPartSearch&part=" + part + "&product=" + product + "&clientID=" + client, true);
		
	request.onreadystatechange = handlePartSearch; 
	request.send(null);
}


function handlePartSearch(){

		var product = getQueryVariable("product");
		var qty = getQueryVariable("qty");
		
		if(request.readyState == 1){
			document.getElementById('loader').innerHTML="<table width='675' align='center'><tr><td width='50%' align='right'><img src='../images/ajax-loader.gif' /></td><td class='styleload1'><strong>Loading...</strong></td></tr></table>";
		}
	
	if(request.readyState == 4){ 
		var response = request.responseText;
	    var loadTable;
	
		if(response.length==2){
			document.getElementById('loader').innerHTML="<table border='0' width='675'><tr height='40'><td align='center'><font color='red'><b>NO RESULTS<b></font></td></tr></table>";
		}
		else{				
				buildTables(product,qty, response);	
		}
	}
	
	return request.readyState
}

function buildTables(product,qty,response)
{
//determine what product it is
		if(product=="DK"){
			if(qty==0){
					loadTable = "<table width='660' align='center' cellspacing='2' cellpadding='4' class='style1'>	<TR><TD width='60' align='center'><B>Part#</B></TD><TD width='65' align='left'><B>Product</B></TD><TD width='275' align='left' ><B>Description/PCS</B></TD><TD width='100' align='left'><B>View Details/<br>Select Finish</B></TD><TD width='70' align='left'><B>Price</B></TD><TD width='50' align='center'>&nbsp;</TD></TR></table><TABLE cellspacing='2' cellpadding='4' class='styleSearch' id='dashTable' style='WIDTH: 660px'  align='center'></TABLE>";
					document.getElementById('loader').innerHTML=loadTable;
					fillTable(document.getElementById("dashTable"), response, qty);
			}else{
					loadTable = "<table width='660' align='center' cellspacing='2' cellpadding='4' class='style1'>	<TR><TD width='60' align='center'><B>Part#</B></TD><TD width='65' align='left'><B>Product</B></TD><TD width='250' align='left' ><B>Description/PCS</B></TD><TD width='105' align='left'><B>View Details/<br>Select Finish</B></TD><TD width='50' align='left'><B>Price</B></TD><TD width='40' align='center'><B>QTY</B></TD><TD width='90' align='center'>&nbsp;</TD></TR></table><TABLE cellspacing='2' cellpadding='4' class='styleSearch'  id='dashTable' style='WIDTH: 660px' align='center'></TABLE>";
					document.getElementById('loader').innerHTML=loadTable;
					fillTable(document.getElementById("dashTable"), response, qty);			
			}
	
		}
		else{
			if(qty==0){
					loadTable = "<table width='660' align='center' cellspacing='2' cellpadding='4' class='style1'>	<TR><TD width='60' align='center'><B>Part#</B></TD><TD width='65' align='left'><B>Product</B></TD><TD width='275' align='left' ><B>Description</B></TD><TD width='100' align='left'><B>View Details</B></TD><TD width='50' align='left'><B>Price</B></TD><TD align='center'>&nbsp;</TD></TR></table><TABLE cellspacing='2' cellpadding='4' class='styleSearch' id='dashTable' style='WIDTH: 660px'  align='center'></TABLE>";
					document.getElementById('loader').innerHTML=loadTable;
					fillTable2(document.getElementById("dashTable"), response, qty);		
			}else{
					loadTable = "<table  width='660' align='center' cellspacing='2' cellpadding='4' class='style1'>	<TR><TD width='60' align='center'><B>Part#</B></TD><TD width='65' align='left'><B>Product</B></TD><TD width='285' align='left' ><B>Description</B></TD><TD width='105' align='left'><B>View Details</B></TD><TD width='50' align='left'><B>Price</B></TD><TD width='45' align='center'><B>QTY</B></TD><TD width='100' align='center'>&nbsp;</TD></TR></table><TABLE cellspacing='2' cellpadding='4' class='styleSearch'  id='dashTable' style='WIDTH: 660px' align='center'></TABLE>";
					document.getElementById('loader').innerHTML=loadTable;
					fillTable2(document.getElementById("dashTable"), response, qty);				
			}
	
		}

}

function getShopSarCode(v)
{
	request.open("get", "search_data.aspx?data=getPriceCode&price=" + v , false);
	request.onreadystatechange = handleShopSarCode; 
	request.send(null);
}

function handleShopSarCode(){
		if(request.readyState == 1){
			//document.getElementById('loader').innerHTML="<table width='100%' align='center'><tr><td width='50%' align='right'><img src='../images/ajax-loader.gif' /></td><td class='styleload1'><strong>Loading...</strong></td></tr></table>";
		}
	
	if(request.readyState == 4){ 
		var response = request.responseText;
		 document.getElementById('txtFWD').value = response;
	}
}

//----pass shipping cost...

function pausecomp(millis) 
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); } 
	while(curDate-date < millis);
} 


function getQueryVariable(variable) {
     
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      
      for (var i=0;i<vars.length;i++) {
       
        var pair = vars[i].split("=");
       
        if (pair[0] == variable) {
          return pair[1];
        }
      }
     
}


function closefn(tbrow)
{

	var oDDL = document.getElementById("ddMaterials");
	var curText = oDDL.options[oDDL.selectedIndex].text;
	var allParams = curText.split("-");
	var part = getQueryVariable("id");

	var matCode = allParams[0];
	var matDesc = allParams[1];
	var matCost = allParams[2];

	window.opener.document.getElementById("material"+tbrow).innerHTML = "<a href=\"javascript:openwindow('itemDetails.aspx?id=" + part + "&tbrow=" + tbrow + "')\">" + matCode + "-<BR>" + matDesc + "</a>";
	window.opener.document.getElementById("price"+tbrow).innerHTML = "<font color='red'><b>" + matCost + "</b></font>";
	window.opener.document.getElementById("part"+tbrow).innerHTML = part + "-" + matCode;
	window.opener.document.getElementById("cost"+tbrow).value = matCost.substring(2,matCost.length);
	window.opener.document.getElementById('price_1').value = matCost.substring(2,matCost.length);
	window.opener.document.getElementById('mat').value = "Finished in [" + matCode + "] " + matDesc;
	
	window.opener.document.getElementById("matRow"+tbrow).value = matCode;
	window.parent.close();


}


function delayer(){
	self.close();
}


function closewindow()
{
	window.close();
}


function clearTable(tableElm) {
	var rows = tableElm.rows;
	for( var i = rows.length - 1; i >= 0; --i )
  		tableElm.deleteRow( i );
}

function clearSelect(selectElm) {
    if (selectElm.options != null) {
        selectElm.options.length = 0;
    }
    selectElm.disabled = true;
}

function resetSelect(selectElm,type) {
    if (selectElm.options != null) {
        selectElm.options.length = 0;
    }
    selectElm.options[selectElm.options.length] = new Option(type, type, false, false);
    selectElm.disabled = true;
}

function fillSelect(selectElm, responseText) {
    selectElm.options.length = 0;
    var response = responseText.split(";;");
    for (var i = 0; i < response.length-1; i++) {
        var style = response[i].split("~~");
       	selectElm.options[selectElm.options.length] = new Option(style[1], style[0], false, false);
    }
    selectElm.disabled = false;
}


function fillModelsSelect(selectElm, modelsArray) {
    selectElm.options.length = 0;
    //selectElm.options[0] = new Option("", "0", true, true);
    for (var i = 0; i < modelsArray.length-1; i++) {
        var style = modelsArray[i].split("~~");
       	selectElm.options[selectElm.options.length] = new Option(style[0], style[1], false, false);
    }
    selectElm.disabled = false;
}

function resetfn()
{
	document.getElementById('loader').innerHTML=""; 
	
	document.getElementById("model").selectedIndex = 0
	document.getElementById("make").selectedIndex = 0
	
	resetSelect(document.getElementById("make"),"Make"); 
	resetSelect(document.getElementById("model"),"Model"); 
}


function fillTable(styleTable, responseText, qty) {
 	var table = document.getElementById( "dashTable" );
	var a,b,c,d,e,f;
	var client = getQueryVariable("clientID");
	
	if(qty==1){
		a = 60;
		b = 75;
		c = 275;
		d= 100;
		e= 50;
		f= 50;
	}else{
		a = 60;
		b = 75;
		c = 300;
		d= 100;
		e= 50;
		f= 50;
	}
 	//delete previous styles
 	clearTable( table );

 	//populate table with new styles
 	var alldashes = responseText.split(";;");
	for (var i = 0; i < alldashes.length-1; i++) {
		var eachDash = alldashes[i];
		var dashParams = eachDash.split("~~");
		var part = dashParams[0];
		var pcs = dashParams[1];
		var product = dashParams[2];
		var description = dashParams[3];
		var price = dashParams[4];

		var row = table.insertRow(-1);

		var td = row.insertCell(-1);
		td.setAttribute("width", a);
		td.setAttribute("align","left");
		td.innerHTML = "<div id='part" + i + "'>" + part + "</div>";

		var td2 = row.insertCell(-1);
		td2.setAttribute("width", b);
		td2.setAttribute("align","left");
		td2.innerHTML = "<div id='product" + i + "'><b>" + product + "</b></div>";

		var td3 = row.insertCell(-1);
		td3.setAttribute("width",c);
		td3.setAttribute("align","left");
		td3.innerHTML = "<div id='description" + i + "'>" + description + "</div>"; 
		// "  /  " + "<b>" + pcs + " PCS</b><
		var td4 = row.insertCell(-1);
		td4.setAttribute("width",d);
		td4.setAttribute("align","left");
		td4.innerHTML = "<INPUT type='hidden' value='0' id='matRow" + i + "' name='matRow" + i + "'><div id='material" + i + "'><a href=\"javascript:openwindow('itemDetails.aspx?id=" + part + "&tbrow=" + i + "&client=" + client + "')\">select finish</a></div>";	
		//td4.innerHTML = "<div id='material" + i + "'><a href=\"javascript:openwindow('itemDetails.aspx?id=" + part + "&tbrow=" + i + "')\">select finish</a></div>";	
	
		var td5 = row.insertCell(-1);
		td5.setAttribute("width",e);
		td5.setAttribute("align","center");
		td5.innerHTML = "<INPUT type='hidden' id='cost" + i + "' name='cost" + i + "' value='" + price + "' style='WIDTH: 24px; HEIGHT: 22px'><div id='price" + i + "'><font color='red'><b>select finish</b></font></div>";

		
		if(qty==1){
			var tdq = row.insertCell(-1);
			tdq.setAttribute("width",f);
			tdq.setAttribute("align","center");
			tdq.innerHTML = "<INPUT id='qty" + i + "' name='qty" + i + "' style='WIDTH: 24px; HEIGHT: 22px'>";
		}		
				
		var td6 = row.insertCell(-1);
		td6.setAttribute("align","center");
		td6.innerHTML = "<A href='#' onMouseUp='qvalidate(" + i + ")'><img name='jsbutton' width='74' height='20' src='../images/cart.jpg'  border='0' alt='add to cart'></A>";

	}
}


function fillTable2(styleTable, responseText, qty) {
 	var table = document.getElementById( "dashTable" );
	var client = getQueryVariable("clientID");
 	//delete previous styles
 	clearTable( table );
	
	if(qty==1){
		a = 60;
		b = 75;
		c = 300;
		d= 100;
		e= 50;
		f= 50;
	}else{
		a = 60;
		b = 70;
		c = 300;
		d= 100;
		e= 50;
		f= 50;
	}

 	//populate table with new styles
 	var allProducts= responseText.split(";;");
	for (var i = 0; i < allProducts.length-1; i++) {
		var eachProduct = allProducts[i];
		var productParams = eachProduct.split("~~");
		var part = productParams[0];
		var product = productParams[2];
		var description = productParams[1];
		var price = productParams[3];

		var row = table.insertRow(-1);

		var td = row.insertCell(-1);
		td.setAttribute("width",a);
		td.setAttribute("align","left");
		td.innerHTML = "<div id='part" + i + "'>" + part + "</div>";

		var td2 = row.insertCell(-1);
		td2.setAttribute("width",b);
		td2.setAttribute("align","left");
		td2.innerHTML = "<div id='product" + i + "'><b>" + product + "</b></div>";

		var td3 = row.insertCell(-1);
		td3.setAttribute("width", c);
		td3.setAttribute("align","left");
		td3.innerHTML = "<div id='description" + i + "'>" + description + "</div>"; 

		var td4 = row.insertCell(-1);
		td4.setAttribute("width",d);
		td4.setAttribute("align","left");
		td4.innerHTML = "<div id='material" + i + "'><a href=\"javascript:openwindow('productDetails.aspx?id=" + part + "&client=" + client + "')\">view details</a></div>";	
	
	
		var td5 = row.insertCell(-1);
		td5.setAttribute("width", e);
		td5.setAttribute("align","left");
		td5.innerHTML = "<INPUT type='hidden' id='cost" + i + "' name='cost" + i + "' value='" + price + "' style='WIDTH: 24px; HEIGHT: 22px'><div id='price" + i + "'><font color='red'><b>$" + price + "</b></font></div>";
		//td5.innerHTML = "<div id='price" + i + "'><font color='red'><b>" + price + "</b></font></div>";
		
		if(qty==1){
				var tdq = row.insertCell(-1);
				tdq.setAttribute("width",f);
				tdq.setAttribute("align","center");
				tdq.innerHTML = "<INPUT id='qty" + i + "' name='qty" + i + "' style='WIDTH: 24px; HEIGHT: 22px'>";
				
		}		

		var td6 = row.insertCell(-1);
		td6.setAttribute("align","center");
		td6.innerHTML = "<A href='#' onMouseUp='qvalidate(" + i + ")'><img name='jsbutton' width='74' height='20' src='../images/cart.jpg'  border='0' alt='add to cart'></A>";

	}
}

function openwindow(a) 
{ 
	window.open(a,'shadowsheet','width=850,height=650,resizable=no,scrollbars=yes'); 
} 


function setmm()
{
	document.make.vehMake.selectedIndex=0;
	document.make.vehModel.selectedIndex=0;
}

function validate()
{

	if(document.make.vehModel.value=="")
	{
		alert("Please select the  Model");
	
	return false;
	}

	if(document.make.vehModel.value=="0")
	{
		alert("PLEASE SELECT YOUR VEHICLE DETAILS:");
		return false;
	}
		return true;
}

function checknumber(x){
	var anum=/^-?\d+$/;
	if (anum.test(x))
			testresult=true;
	else{		
			testresult=false;
		}
	return (testresult);
}

function checkFinish(c){
	var qty = getQueryVariable("qty");
	var toReturn;
	
 if(qty==1){
	toReturn = true; 
 }else{
	if (document.getElementById("matRow"+c).value==0)
			toReturn=false;
	else	
			toReturn=true; 
 }

  return (toReturn);
}



function qvalidate(c){

	if(!checkFinish(c))
	{
		alert("Please Select A Finish First");
		return false;
	}
	else
	{

		var partParams = document.getElementById("part"+c).innerHTML.split("-");
		var product = partParams[0];
		var partNo = partParams[1];
		var material = partParams[2];
	
		
		document.getElementById('lloyd_code_1').value = document.getElementById("part"+c).innerHTML;

		document.getElementById('description_1').value= "Sherwood - " + document.getElementById("description"+c).innerHTML + "<br /> Part #:" + document.getElementById("part"+c).innerHTML + "(" + document.getElementById("year").value + " " + document.getElementById("make").value + " " + document.getElementById("model").value + " " + document.getElementById('mat').value + ".)";
		
		if(product=="2D" || product=="3D" || product=="CB"){		
			document.getElementById('lloyd_code_1').value = product+partNo+material;	
		}else{	
			document.getElementById('lloyd_code_1').value = document.getElementById("part"+c).innerHTML;
		}
		
		//document.getElementById('qty[0]').value = document.getElementById("qty"+c).value;		
		//document.getElementById('price[0]').value = document.getElementById("cost"+c).value;
		//var total = document.getElementById('price_n').value * document.getElementById('qty[0]').value;
		document.bitForm.submit();
		
	}
}

function qvalidate1()
	{
		if(document.make.txtfld.value=="1")
		{
		alert("Please click 'Go' button");
		return false;
	}

}
