///////////////////////////////////////////////////////////////////////
//     Routines to validate input fields on header                    //
///////////////////////////////////////////////////////////////////////

function getHeadDetails(Form, prmButton) {
	
	if (prmButton=="byItem")  
		{ validateVapPart(Form);
		return
		}
	
	if (prmButton=="byDescription")  
		{validateHeadDescription(Form); 
		return
		}
	
	if (prmButton=="byModelSearch")  
	{
		//alert('byModelSearch');
		validateModelSearch(Form); 
	return
	}
	
	alert(prmButton + " not working");		
	return false;	
	}

///////////////////////////////////////////////////////////////////////
//     Routines to validate input fields advanced search                   //
///////////////////////////////////////////////////////////////////////

function getDetails(prmForm, prmButton) {
    
	if (prmButton=="byItem")  
		{ validateVapPart(prmForm);
		return
		}
	
	if (prmButton=="byDescription")  
		{validateDescription(prmForm); 
		return
		}
		
	if (prmButton=="byOemPart")  
		{validateOemPart(prmForm); 
		return
		}
	
	if (prmButton=="byModelSearch")  
	{validateModelSearch(prmForm); 
	return
	}
	
	alert(prmButton + " not working");		
	return false;	
	}

///////////////////////////////////////////////////////////////////////
//     Validate VapPart                                              //
///////////////////////////////////////////////////////////////////////
function validateVapPart(prmForm) {
	
	tmp=prmForm.prmItem.value;
	
	if (tmp.length != 7) 
	
	{
	    alert("All Vap part numbers are 7 characters long. Please rekey.");
	    prmForm.prmItem.focus();
	    return false}
	else
	{	
		tmp=tmp.toUpperCase();
		prmForm.prmTitle.value = "Part details for " + tmp + "";
		prmForm.prmMachineId.value = 0;
		prmForm.prmPageDetail.value = "05";
		if (1==1) { //prmForm.name=='aspnetForm'
		    objfrm =prmForm;
		    //change the title querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmTitle','Part details for ' + tmp);
            //change the machineID querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmMachineId','0');
            //change the page details querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmPageDetail','05');
            //add the prm item
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmItem',tmp);
		    prmForm.submit();
		}
		else {
		    prmForm.submit();
		    return true    
		}
	}
	}
	
///////////////////////////////////////////////////////////////////////
//     Validate Descriptive search                                   //
///////////////////////////////////////////////////////////////////////
function validateDescription(prmForm) {

	
	tmp=prmForm.prmDescription.value;
	if (tmp.length < 3 ) 
	
	{
	    alert("Please use at lease 3 characters for the search.");
	    prmForm.prmDescription.focus();
	    return false
	}
	else
	{	
	    prmForm.prmTitle.value = "Part details for <b>" + prmForm.prmDescription.value + "</b>";
	    prmForm.prmPageDetail.value = "20"
		
	    oemIndex = prmForm.cboOemA.selectedIndex;
        prmForm.prmOem.value = prmForm.cboOemA[oemIndex].value;
	    
	    machineIdIndex = prmForm.cboMachineId.selectedIndex;
	    prmForm.prmMachineId.value = prmForm.cboMachineId[machineIdIndex].value;
		
		if (1 == 1) { //prmForm.name == 'aspnetForm'
		    objfrm =prmForm;
		    //change the title querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmTitle','Part details for ' + prmForm.prmDescription.value);
            //change the page details querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmPageDetail','20');
            //change the oem querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmOem',prmForm.cboOemA[oemIndex].value);
		    //change the machineID querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmMachineId',prmForm.cboMachineId[machineIdIndex].value);
            //change the prmDescription querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmDescription',prmForm.prmDescription.value);            
		}
		// frmItemEnq.action="default.asp";
		prmForm.submit();
		return true
	}
	}

///////////////////////////////////////////////////////////////////////
//     Validate Descriptive search                                   //
///////////////////////////////////////////////////////////////////////
function validateHeadDescription(prmForm) {

	
	tmp=prmForm.prmDescription.value;
	if (tmp.length < 3 ) 
	
	{
	    alert("Please use at lease 3 characters for the search.");
	    tmp.focus();
	    return false}
	else
	{	
		
		window.location.href = "default.asp?prmSource=15&prmPageDetail=20&prmOem=&prmMachineId=0&prmTitle=Part+details+for+" + tmp + "&prmDescription=" + tmp + "";

		return true
	}
	}


///////////////////////////////////////////////////////////////////////
//     Validate Oem Part                                             //
///////////////////////////////////////////////////////////////////////
function validateOemPart(prmForm) {

	
	tmp=prmForm.prmOemPart.value;
	if (tmp.length < 3 ) 
	
	{
	    alert("Please use at lease 3 characters for the oemPart. ");
	    prmForm.prmOemPart.focus();
	    return false}
	else
	{	
		prmForm.prmTitle.value = "OEM details for <b>" + prmForm.prmOemPart.value + "</b>";
		prmForm.prmPageDetail.value = "25"
		
		oemIndex = prmForm.cboOemB.selectedIndex;
	    prmForm.prmOem.value = prmForm.cboOemB[oemIndex].value;
	    
	    if (1 == 1) { //prmForm.name == 'aspnetForm'
	        objfrm =prmForm;
		    //change the title querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmTitle','OEM details for ' + prmForm.prmOemPart.value);
            //change the detail querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmPageDetail','25');
            //change the oem reference querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmOem',prmForm.cboOemB[oemIndex].value);
	        //change the oem part number querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmOemPart',prmForm.prmOemPart.value);
	    }
		// frmItemEnq.action="default.asp";
		prmForm.submit();
		return true
	}
	}
	
      function popup(url, w, h, prmData) {
        var options = "width=" + w + ",height=" + h + ",";
        options += "resizable=yes,scrollbars=yes,status=no,";
        options += "menubar=no,toolbar=no,location=no,directories=no";
        var newWin = window.open(url, 'webcopyright', options);
        newWin.focus();
      }
      
///////////////////////////////////////////////////////////////////////
//     Validate Tractor model search                                 //
///////////////////////////////////////////////////////////////////////
function validateModelSearch(prmForm) {

//prmTitle=Models+that+match+135
//&prmPageDetail=60
//&prmSource=10
//&prmOem=
//&prmMachineId=0
//&prmModelSearch=135
	
	var tmp=prmForm.prmModelSearch.value;
	//alert('len: ' + tmp.length);
	if (tmp.length < 2 ) 
	{
	    alert("Please use at lease 2 characters for the search.");
	    prmForm.prmModelSearch.focus();
	    return false}
	else
	{	
		prmForm.prmTitle.value = "Models that match <b>" + prmForm.prmModelSearch.value + "</b>";
		prmForm.prmPageDetail.value = "60";
		prmForm.prmSource.value = "10";
		prmForm.prmModelSearch.Value = tmp;
		if (prmForm.name !== 'frmHeadSearch') { //prmForm.name == 'aspnetForm'
		    objfrm =prmForm;
		    //change the title querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmTitle','Models that match ' + tmp);
            //change the details querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmPageDetail','60');
            //change the source querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmSource','10');
            //change the prmOem querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmOem','');
            //change the machineId querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmMachineId','0');
            //change the model search querystring
            objfrm.action = ChangeQueryStringValue(objfrm.action,'prmModelSearch',tmp);
            prmForm.submit();
		}
		else {
		    window.location.href = "default.asp?prmSource=10&prmPageDetail=60&prmOem=&prmMachineId=0&prmTitle=Models+found+using+" + prmForm.prmModelSearch.value + "&prmModelSearch=" + prmForm.prmModelSearch.value + "";
		}

		return true
	}
	}	  
	
	
	/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
                
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			
	if (field.id=="prmItem")  
		{ validateVapPart(frmHeadSearch);
		return
		}
	
	if (field.id=="prmDescription")  
		{validateHeadDescription(frmHeadSearch); 
		return
		}
	
	if (field.id=="prmModelSearch")  
	{validateModelSearch(frmHeadSearch); 
	return
	}
	
		} 
		else
		return true;
	} 
	
	
	
/*MiWeb streaming object replace querystring values - http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3614&lngWId=2 */
function ChangeQueryStringValue(action,qsname,qsvalue) {
    	if (action.indexOf('?') == -1 )	 //If no querystring present
    		return (action+"?"+qsname+"="+UrlEncode(qsvalue)); //x.asp?Name=Peter+Paul
    	else
    	{		
    		if ( (action.indexOf("?"+qsname+"=") == -1 ) && (action.indexOf("&"+qsname+"=") == -1 ) ) //Passed querystring not already present		


        		{
        			return (action+"&"+qsname+"="+UrlEncode(qsvalue)); //x.asp?Age=25&Name=Peter+Paul;
        		}
        		else //Passed Querystring already present, replace it no matter if it is ?Name= or &Name=


            		{
            			var replaceQSPrefix=(action.indexOf("?"+qsname+"=")==-1)?"&"+qsname+"=":"?"+qsname+"=";
            			var replaceQSSuffix=""; //This will be John in case of ?Name=John or &Name=John 
            			startpos=action.indexOf(replaceQSPrefix);
            			
            			for (a=(startpos+replaceQSPrefix.length);a<action.length;a++)


                			{
                				if ( action.charAt(a)=='&' ) //Next querystring beginning
                					break;
                				else
                					replaceQSSuffix += action.charAt(a);
                			}
                			newaction=action.replace(replaceQSPrefix+replaceQSSuffix,replaceQSPrefix+UrlEncode(qsvalue));
                			return newaction;
                		}	
                	}		
            }

            function UrlEncode(text) 


                {		
                	//text="A.asp?name=Amit Chauhan&Age=25"; //Example
                	
                	/*
                	text=text.replace("/\//g","%2F");
                	text=text.replace("/?/g","%3F");
                	text=text.replace("/=/g","%3D");
                	text=text.replace("/&/g","%26");			
                	*/	
                	
                	//text=ReplaceAll(text, "/\", "%2F"); 
                	text=ReplaceAll(text, "?", "%3F");
                	text=ReplaceAll(text, "=", "%3D");
                	text=ReplaceAll(text, "&", "%26");
                	text=ReplaceAll(text, " ", "+");
                	text=ReplaceAll(text, ",", "%2c");	
                	
                	return text;
            }

            //Replace all given string from a string
            //     
            function ReplaceAll(varb, replaceThis, replaceBy)


                {	
                	newvarbarray=varb.split(replaceThis);
                	newvarb=newvarbarray.join(replaceBy);	
                	return newvarb;
            }
//sr - revision - 25/03/2009 - quick order entry function to add the quickorder items to the URL 1VPB10001VPB4000
function quickorderentry(maxitems, page) {
	var url='';
	for (i=((parseFloat(page)-1)*10) ;i<=((parseFloat(page)-1)*10)+parseFloat(maxitems);i++) {
		if ((document.getElementById('qty_' + i).value.match(/^\d+$/ )) && (document.getElementById('part_' + i).value.length == 7)) {
			url += document.getElementById('qty_' + i).value + document.getElementById('part_' + i).value + document.getElementById('ware_' + i).value;
		}
	}
	return url;	
}
//sr - end revision - 25/03/2009
//sr - revision - 25/06/2009 - get the trade account profile WAREHOUSE|PRICEBOOK|ORDER|CANBUYIN|DEFAULT
function tradeaccountprofile(warehouses) {
	var url='';
	for (i=0;i<=warehouses;i++) {
		if (document.getElementById('hid_profile_' + i).value==1) {
			url+=document.getElementById('hid_ware_' + i).value + '|' + document.getElementById('cmb_price_' + i).value + '|' + document.getElementById('txt_order_' + i).value + '|'
			if (document.getElementById('chk_buy_' + i).checked) {
				url+='1|';
			}
			else {
			url+='0|';
			}
			if (document.getElementById('chk_default_' + i).checked) {
				url+='1|';
			}
			else {
				url+='0|';
			}
		}
	}
	return url;
}
function postcheck() {
	var r=Math.floor(Math.random()*100000001)
	return 'ck' + r;
}
//sr - END revision - 25/06/2009 