
// the validate function is for the part request page. It has to be placed here as the 
// script is ignored when the page is included in an ajax call if it is not. --Drew
function validate()
{
    var display = "You requested to be contacted by " + document.Request.ContactMethod.value + ".\n";
    if(document.Request.ContactMethod.value=="Phone" &&
        (document.Request.name.value=="" || document.Request.phone.value==""))
        alert(display + "Please enter your name and phone number.");
    else if(document.Request.ContactMethod.value=="E-Mail" &&
        (document.Request.name.value=="" || document.Request.email.value==""))
        alert(display + "Please enter your name and email address.");
    else if(document.Request.ContactMethod.value=="Fax" &&
        (document.Request.name.value=="" || document.Request.fax.value==""))
        alert(display + "Please enter your name and fax number.");
    else if(document.Request.ContactMethod.value=="Mail" &&
        (document.Request.name.value=="" ||
            document.Request.mailingaddress.value==""
            || document.Request.city.value==""
            || document.Request.state.value==""
            || document.Request.postalcode.value==""))
        alert(display + "Please enter your name, address, city, state, and zipcode.");
    else
        document.Request.submit();
}

function partChange()
{
    var ajaxRequest;  // The variable that makes Ajax possible!
    var manufMake = document.getElementById('ManufMake');
    var model     = document.getElementById('Model');
    var part      = document.getElementById('PartID');

// this section tests whether the dropdowns are hidden or not
if ( dmanuf == "no")
{
var elem, vis;
        elem = document.getElementById("manuflabel");
        vis = elem.style;
        vis.display="none";

        elem = document.getElementById("manufDiv");
        vis = elem.style;
        vis.display="none";

		// if the manufacturer is hidden also hide the model
		dmodel = "no";
}
if ( dmodel == "no")
{
var elem, vis;
        elem = document.getElementById("modellabel");
        vis = elem.style;
        vis.display="none";

        elem = document.getElementById("modelDiv");
        vis = elem.style;
        vis.display="none";
}
if ( dkeyword == "no")
{
var elem, vis;
        elem = document.getElementById("keywordlabel");
        vis = elem.style;
        vis.display="none";

        elem = document.getElementById("keywordDiv");
        vis = elem.style;
        vis.display="none";
}




    if(defPart != "" && defPart.toLowerCase() != "any")
    {
        for(i=0; i<part.length; ++i)
	{
	    if(part.options[i].value == defPart)
	    {
	        part.options[i].selected = "selected";
		defPart = "";
	    }
	}
    }

    if(part.SelectedIndex != 0)
    {
        manufMake.options[0].text = "Loading...";
        manufMake.disabled = "disabled";
        model.options[0].text = "Loading...";
        model.disabled = "disabled";
    }
    
    try
    {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
    	// Internet Explorer Browsers
    	try{
    		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
    		try{
    			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    		} catch (e){
    			// Something went wrong
    			alert("Your browser isn't supported by HeavyTruckParts.Net or javascript has been disabled");
    			return false;
    		}
    	}
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            var ajaxDisplay = document.getElementById('manufDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
            var manufMake = document.getElementById('ManufMake');
            var model     = document.getElementById('Model');
	    manufMake.disabled = "";
	    model.disabled = "";
	    model.length = 1;
	    model.options[0].text  = "Any";
	    model.options[0].value = "any";

	    if(defMake != "" && defMake.toLowerCase() != "any")
	    {
	        for(i=0; i < manufMake.length; ++i)
		{
		    if(manufMake.options[i].value == defMake)
		    {
		        manufMake.options[i].selected = "selected";
		    }
		}
		defMake = "";
		manufChange();
	    }
        }
    }
    var part = document.getElementById('PartID').value;
    var queryString = "?part=" + part;	
    ajaxRequest.open("GET", "search_menu.php" + queryString, true);
    ajaxRequest.send(null); 
}

function manufChange()
{
    var ajaxRequest;  // The variable that makes Ajax possible!
    var model = document.getElementById('Model');

    model.disabled = "disabled";
    model.options[0].text = "Loading...";
    
    try
    {
    	// Opera 8.0+, Firefox, Safari
    	ajaxRequest = new XMLHttpRequest();
    }
    catch (e)
    {
    	// Internet Explorer Browsers
    	try
	{
    	    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    	}
	catch (e)
	{
    	    try
	    {
    	        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    	    }
	    catch (e)
	    {
    	        // Something went wrong
    	        alert("Your browser isn't supported by HeavyTruckParts.Net or javascript has been disabled ");
    		return false;
    	    }
    	}
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function()
    {
    	if(ajaxRequest.readyState == 4)
	{
    		var ajaxDisplay = document.getElementById('modelDiv');
    		ajaxDisplay.innerHTML = ajaxRequest.responseText;
		
		if(defModel != "" && defModel.toLowerCase() != "any")
		{
		    var modelSelect = document.getElementById('Model');
		    for(i = 0; i < modelSelect.length; ++i)
		    {
		        if(modelSelect.options[i].value == defModel)
			{
			    modelSelect.options[i].selected = "selected";
			}
		    }
		    defModel = "";
		}
    	}
    }
    var part = document.getElementById('PartID').value;
    var ManufMake = document.getElementById('ManufMake').value;
    var queryString = "?part=" + part + "&ManufMake=" + ManufMake;	
    ajaxRequest.open("GET", "search_menu.php" + queryString, true);
    ajaxRequest.send(null); 
}

// Make sure a part type is selected
function validate_form(sortBy)
{
    var partID = document.getElementById('PartID');
    var sessionInput  = document.getElementById('sessionHidden');
    var sessionStr = sessionInput.value;
    var nagInput  = document.getElementById('nagHidden');
    var nagCnt = nagInput.value;
	// The nag value is negative if the search is from ITrack.
	//	Obviously, we don't need to nag ITrack users. -- Brian
    if(nagCnt >= 0) ++nagCnt;
    nagInput.value = nagCnt;
    var submitInput = document.getElementById('Search');

    if (partID.selectedIndex > 0)
    {
		var ajaxRequest;  // The variable that makes Ajax possible!
		
		try
		{
		    // Opera 8.0+, Firefox, Safari
		    ajaxRequest = new XMLHttpRequest();
		}
		catch (e)
		{
		    // Internet Explorer Browsers
		    try
		    {
		        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		    }
		    catch (e)
		    {
			try
			{
			   ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
			    // Something went wrong
			    alert("Your browser isn't supported by HeavyTruckParts.Net or javascript has been disabled ");
		  	    return false;
			}
		    }
		}
		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function stateChange()
		{
		    if(ajaxRequest.readyState == 4)
		    {
		        var ajaxDisplay = document.getElementById('searchResult');
		        ajaxDisplay.innerHTML = ajaxRequest.responseText;
				// Call Google Analytics
				if (JS_productcode == "0")
				{	
					urchinTracker("/sql.php");
				}
				var ary_linkPartInfo = document.getElementsByName('linkPartInfo');
// I think that this sends the search number to the detailview pages and the 
// popup pages -- Drew
// That looks to be the case.  I went ahead and had sql.php do this instead,
// 	so I'm going to comment this out and see if there are problems.
/*				if (ary_linkPartInfo)
				{
					var hiddenSearchNum = document.getElementById('hiddenSearchNum');
					nSearchNum = hiddenSearchNum.value;
					for(var i = 0; i < ary_linkPartInfo.length; ++i)
					{
						var strLink = ary_linkPartInfo[i].href;
						if(strLink.indexOf(")") != -1)
						{
							strLink = strLink.substring(0, strLink.length - 2 );
							strLink += "&searchnum=" + nSearchNum;
							strLink += "')";
						}
						else
						{
							strLink += "&searchnum=" + nSearchNum;
						}
						ary_linkPartInfo[i].href = strLink;
					}
				}*/
		    }
		}

    	var partSelect    = document.getElementById('PartID');
    	var manuSelect    = document.getElementById('ManufMake');
    	var modelSelect   = document.getElementById('Model');
    	var keywordText   = document.getElementById('keywords');
		var partText   	  = document.getElementById('part');
        var Invoice 	  = document.getElementById('Invoice');

		if(typeof keywordText.value == 'undefined')
			{
		    	for(var i = 0; i < Invoice.length; ++i)
		        {
					if(Invoice.elements[i].id == "keywords")
					{
					keywordText = Invoice.elements[i];
					}
				}
			}


    	formData  = "PartID="
		formData += encodeURIComponent(partSelect.options[partSelect.selectedIndex].value);
		formData += "&ManufMake=";
		formData += encodeURIComponent(manuSelect.options[manuSelect.selectedIndex].value);
		formData += "&Model=";
		formData += encodeURIComponent(modelSelect.options[modelSelect.selectedIndex].value);
		formData += "&keywords=";
		formData += encodeURIComponent(keywordText.value);

		formData += "&session=" + sessionStr; 
		formData += "&nag=" + nagCnt;

		formData += "&part=";
        formData += encodeURIComponent(partText.value);

    	if(sortBy != null)
    	    {
		    formData += "&SortBy=" + sortBy;
    	        // If we sorted by the same thing on the last time then toggle
		    // ascending descending this time
		    if(sortBy == LastSort) 
		    {
		        // Toggle ascending descending
		        if(Order == "ASC")
		        {
		            Order = "DESC";
		        }
		        else
		        {
		            Order = "ASC";
		        }
		    }
		    else // Otherwise just sort ascending
		    {
		        Order = "ASC";
		    }
		    LastSort = sortBy;
		    formData += "&Order=" + Order;
    	    }

		// send them to the nag page on the fifth search, any searches
		//	more than 100 searches in a session, and every fifth search
		//	thereafter, occasionally more often.
		// The code at the end means that the system will nag every time
		// 	a random number between 1 and 5 - the nagcount modulus 5 
		//	comes out to be 1.  That means it WILL pop up a nag at 9, 14,
		//	19, 24, etc... and MIGHT pop up nags at any time prior to that, 
		//	after 5.
    	if (JS_productcode == "0" && 
			((nagCnt == 5) || (nagCnt > 100) || 
			 (nagCnt > 5 
			  	&& Math.floor(Math.random() * (5 - (nagCnt % 5)) + 1) == 1)))
		{
			submitInput.value = "Search Without Signing Up";
			ajaxRequest.open("POST","nag.php");
			
			ajaxRequest.send(formData);

    	}
		else
	 	{
			// sets the title to use the information searched
			var title_part = partSelect.options[partSelect.selectedIndex].text;
			var title_manuf = manuSelect.options[manuSelect.selectedIndex].value;
			var title_model = modelSelect.options[modelSelect.selectedIndex].value;

			if (title_manuf.toLowerCase() == "any")
			{
				var title_manuf = "";	
			}
			else 
			{
				var title_manuf = manuSelect.options[manuSelect.selectedIndex].value;
			}
			if (title_model.toLowerCase() == "any")
            {
                var title_model = "";
            }
            else
            {
                var title_model = modelSelect.options[modelSelect.selectedIndex].value;
            }
			document.title = JS_refer_name + " - Search Results for " 
				+ title_manuf + " " + title_model + " " + title_part;


			submitInput.value = "Search";
			ajaxRequest.open("POST","sql.php");
			ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajaxRequest.setRequestHeader("Content-length", formData.length);
			ajaxRequest.setRequestHeader("Connection", "close");

			ajaxRequest.send(formData);
	 	}
    }
    else
    {
        alert("Please select a part type.");
        Invoice.PartID.focus();
    }
}

