if (document.images)
{
	var engn = new Image;
	engn.src = "images/Home-Page/eng_Up.gif";
	var enga = new Image;
	enga.src = "images/Home-Page/eng_over.gif";

	var thain = new Image;  
	thain.src = "images/Home-Page/thai_up.gif";  
	var thaia = new Image;  
	thaia.src = "images/Home-Page/thai_over.gif";  
}

//Function to automatically fill in some fields
function fillText(option)
{

	if (option == 'a' )
	{
		customerString = window.document.wtyRegister.lstCustomers.value;
		customerNo = customerString.substr(0,customerString.indexOf(",",1));
		customer = customerString.substr(customerString.indexOf(",",1)+1);
		window.document.wtyRegister.txtcustno.value =  customerNo;
		window.document.wtyRegister.hiddenCustNo.value =  customerNo;
		window.document.wtyRegister.hiddenCustomer.value =  customer;
	}
	if (option == 'pn' )
	{
		partNoString = window.document.wtyRegister.lstPartNo.value;
		theClass = partNoString.substr(0,partNoString.indexOf(",",1));
//		partNo = partNoString.substr(partNoString.indexOf(",",1)+1);
		partNo = partNoString.substr(partNoString.indexOf(",",1)+1,partNoString.indexOf(",,",1)-3);
		theDesc = partNoString.substr(partNoString.indexOf(",,",1)+2,partNoString.indexOf(",,,")-partNoString.indexOf(",,")-2);
		theID = partNoString.substr(partNoString.indexOf(",,,",1)+3,partNoString.indexOf(",,,,")-partNoString.indexOf(",,,")-3);
		theType = partNoString.substr(partNoString.indexOf(",,,,",1)+4);
		window.document.wtyRegister.txtclass.value = theClass;
		window.document.wtyRegister.txtDescription.value = theDesc;
		window.document.wtyRegister.txtID.value = theID;
		window.document.wtyRegister.txtType.value = theType;				
		window.document.wtyRegister.hiddenClass.value = theClass;
		window.document.wtyRegister.txtPartNo.value = partNo;
		window.document.wtyRegister.hiddenPartNo.value = partNo;		
	}
	if (option == 'b' )
	{
		partNoString = window.document.wtyRegister.lstPartNo.value;
		theClass = partNoString.substr(0,partNoString.indexOf(",",1));
		partNo = partNoString.substr(partNoString.indexOf(",",1)+1);
		window.document.wtyRegister.txtclass.value = theClass;
		window.document.wtyRegister.hiddenClass.value = theClass;
		window.document.wtyRegister.hiddenPartNo.value = partNo;		
	}
	if (option == 'c' )
	{
		faultString = window.document.wtyRegister.lstFaultCode.value;
		faultCode = faultString.substr(0,faultString.indexOf(",",1));
		faultDesc = faultString.substr(faultString.indexOf(",",1)+1);
		window.document.wtyRegister.hiddenFaultCode.value =  faultCode;
		window.document.wtyRegister.hiddenFaultDesc.value =  faultDesc;		
	}
	if (option == 'd' )
	{
		FascoOEMString = window.document.wtyRegister.lstOEMCust.value;
		FascoOEMNo = FascoOEMString.substr(0,FascoOEMString.indexOf(",",1));
		FascoOEM = FascoOEMString.substr(FascoOEMString.indexOf(",",1)+1);
		window.document.wtyRegister.txtOEMCustNo.value = FascoOEMNo;
		window.document.wtyRegister.hiddenOEMCustNo.value = FascoOEMNo;
		window.document.wtyRegister.hiddenOEMCust.value = FascoOEM;		
	}		
	if (option == 'e' )
	{
		FaultString = window.document.wtyRegister.lstFaultCode.value;
		FaultCode = FaultString.substr(0,FaultString.indexOf(",",1));
		Fault = FaultString.substr(FaultString.indexOf(",",1)+1);
		window.document.wtyRegister.txtFaultDesc.value =  Fault;
		window.document.wtyRegister.hiddenFaultCode.value =  FaultCode;
		window.document.wtyRegister.hiddenFaultDesc.value =  Fault;	
	}
}

//Function to automatically clear out other list boxes
function unselectOthers(option)
{
	if (option == 'agent' )
	{
		window.document.viewAgents.lstState.value = "";
		window.document.viewAgents.lstSuburb.value = "";
		window.document.viewAgents.lstZIP.value = "";
		window.document.viewAgents.lstCountry.value = "";
	}
	if (option == 'suburb' )
	{
		window.document.viewAgents.lstAgent.value = "";
		window.document.viewAgents.lstState.value = "";
		window.document.viewAgents.lstZIP.value = "";
		window.document.viewAgents.lstCountry.value = "";
	}
	if (option == 'state' )
	{
		window.document.viewAgents.lstAgent.value = "";
		window.document.viewAgents.lstSuburb.value = "";
		window.document.viewAgents.lstZIP.value = "";
		window.document.viewAgents.lstCountry.value = "";
	}
	if (option == 'zip' )
	{
		window.document.viewAgents.lstAgent.value = "";
		window.document.viewAgents.lstState.value = "";
		window.document.viewAgents.lstSuburb.value = "";
		window.document.viewAgents.lstCountry.value = "";
	}
	
	if (option == 'country' )
	{
		window.document.viewAgents.lstAgent.value = "";
		window.document.viewAgents.lstState.value = "";
		window.document.viewAgents.lstSuburb.value = "";
		window.document.viewAgents.lstZIP.value = "";
	}
}

function act(imgName) 
{
  if (document.images)
     document[imgName].src = eval(imgName + 'a.src')
}

function inact(imgName) 
{
  if (document.images)
    document[imgName].src = eval(imgName + 'n.src')
}

//Preloading images
function MM_preloadImages()
{ //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


//Check that the form is not submitted with blank fields
function checkForm () 
{

	var errors = '';
	var numErrors = 0;

		if (document.wtyRegister.lstCreditTo.value=="Fasco OEM Customer")
		{
			if ( (document.wtyRegister.lstOEMCust.value==",") || (document.wtyRegister.lstOEMCust.value==""))
			{
				alert("A Fasco OEM Customer must be selected");
				document.wtyRegister.lstOEMCust.focus();
				return false;
			}
		}

		if ( (document.wtyRegister.lstPartNo.value==",") || (document.wtyRegister.lstPartNo.value==""))
		{
			alert("A part number must be selected.");
			document.wtyRegister.lstPartNo.focus();
			return false;
		}
		
		if (document.wtyRegister.txtDOMftg.value=="")
		{
			alert("A date of manufacture must be entered. Type in 01/01/1900 if date not known.");
			document.wtyRegister.txtDOMftg.focus();
			return false;
		}

		if (document.wtyRegister.txtSerialNo.value=="")
		{
			var PNSuffix = document.wtyRegister.lstPartNo.value.substring(3,5);
			if (PNSuffix == "80" )
			{
				alert("Serial number is compulsary for this part number, please enter one. If you cannot see the serial number please contact FASCO and we will provide you with one.");
				document.wtyRegister.txtSerialNo.focus();
				return false;
			}
		}
		
		if (  (document.wtyRegister.lstCustomers.value=="0,0") || (document.wtyRegister.lstCustomers.value=="") )
		{
			alert("A Sub Agent must be selected.");
			document.wtyRegister.lstCustomers.focus();
			return false;
		}
	
		if (document.wtyRegister.txtFascoClaim.value=="")
		{
			alert("Fasco MRT No. field must be entered.");
			document.wtyRegister.txtFascoClaim.focus();
			return false;
		}

		if (document.wtyRegister.txtCustClaim.value=="")
		{
			alert("Fasco Customer Claim field must be entered. If you do not have one type: Agent number/mmyy (i.e. 302007/1203)");
			document.wtyRegister.txtCustClaim.focus();
			return false;
		}

		if ( (!isNumeric(document.wtyRegister.txtQtyReturned.value)) || (document.wtyRegister.txtQtyReturned.value == "") )
		{
			alert("Qty. Returned must be entered and must be a numeric value.");
			document.wtyRegister.txtQtyReturned.focus();
			return false;
		}
	
		if ( (!isNumeric(document.wtyRegister.txtQty.value)) || (document.wtyRegister.txtQty.value == "") )
		{
			alert("Qty. Faulty must be entered and must be a numeric value.");
			document.wtyRegister.txtQty.focus();
			return false;
		}
		
		if ( (document.wtyRegister.lstFaultCode.value=="0,0") || (document.wtyRegister.lstFaultCode.value=="") )
		{
			alert("A fault code/description must be selected");
			document.wtyRegister.lstFaultCode.focus();
			return false;
		}

		//Warranty Approved selection checking
		if ( (document.wtyRegister.RadioGroupWtyApp[0].checked==false) && (document.wtyRegister.RadioGroupWtyApp[1].checked==false) )
		{
			alert("The Warranty has to be either approved or not approved, please tick the correct option.");
			document.wtyRegister.RadioGroupWtyApp[0].focus();
			return false;
		}
		
		//Warranty Approved when a customer liability fault code has been entered.
		if (document.wtyRegister.RadioGroupWtyApp[0].checked==true)
		{
		
			var PNSuffix = document.wtyRegister.lstFaultCode.value.substring(0,1);
			//alert(PNSuffix)
			if (PNSuffix == "C" )
			{
				alert("Warranty cannot be approved since it is a customer liability fault code.");
				document.wtyRegister.lstFaultCode.focus();
				return false;
			}		
		}
		
		if ( (document.wtyRegister.lstField.value=="0") || (document.wtyRegister.lstField.value=="") )
		{
			alert("A Field Line option must be selected");
			document.wtyRegister.lstField.focus();
			return false;
		}	
		 
		// Checking numeric values for Labor
		if (!isNumeric(document.wtyRegister.txtLabor.value))
		{
				if (document.wtyRegister.txtLabor.value == "")
				{
					document.wtyRegister.txtLabor.value = "0"
				}
				else
				{
					alert("Labor must be a numeric value.")
					document.wtyRegister.txtLabor.focus()
					return false
				}
		}
		
		if ( (document.wtyRegister.lstField.value=="LR") && (document.wtyRegister.chkScrap.checked==true) )
		{
			alert("Part cannot be scrapped, it requires authentication from FASCO QA. Please select any of the returned fields.");
			document.wtyRegister.lstField.focus();
			return false;
		}		
		
		// Checking numeric values for Parts
		if ( !isNumeric(document.wtyRegister.txtParts.value))
		{
				if (document.wtyRegister.txtParts.value == "")
				{
					document.wtyRegister.txtParts.value = "0"
				}
				else
				{
					alert("Parts must be a numeric value.")
					document.wtyRegister.txtParts.focus()
					return false
				}
		}
		// Checking numeric values for Delivery
		if ( !isNumeric(document.wtyRegister.txtDelivery.value))
		{
				if (document.wtyRegister.txtDelivery.value == "")
				{
					document.wtyRegister.txtDelivery.value = "0"
				}
				else
				{
					alert("Delivery must be a numeric value.");
					document.wtyRegister.txtDelivery.focus();
					return false;
				}
		}		


		// Date of Manufacture
		if ( document.wtyRegister.txtDOMftg.value != "" )
		{
			if (checkDate(document.wtyRegister.txtDOMftg.value) == false)
			{
				document.wtyRegister.txtDOMftg.focus();
				return false;
			}
		}
		
	
		if ( document.wtyRegister.txtDateReceived.value != "" )
		{
			if (checkDate(document.wtyRegister.txtDateReceived.value) == false)
			{
				document.wtyRegister.txtDateReceived.focus();
				return false;
			}
		}
		
		if ( document.wtyRegister.txtDateEntered.value != "" )
		{
			if (checkDate(document.wtyRegister.txtDateEntered.value) == false)
			{
				document.wtyRegister.txtDateEntered.focus();
				return false;
			}
		}		
		//Checking Fault return fields
		theScrap = document.wtyRegister.chkScrap.checked
		theRetNotRepair = document.wtyRegister.chkRetNotRepair.checked
		theRetRepair = document.wtyRegister.chkRetRepair.checked
		theRetReplaced = document.wtyRegister.chkRetReplaced.checked
		if ((theScrap==false) && (theRetNotRepair==false) && (theRetRepair==false) && (theRetReplaced==false))
		{
			alert("You must tick one of the options (Scrap, Returned not repaired, ...).");
			return false;
		}
		if ((theRetRepair==true) && (theRetNotRepair==true))
		{
			alert("You cannot tick Returned not repaired and Retuned repaired at the same time.");		
			return false;
		}
		if ( ((theRetRepair==true) || (theRetNotRepair==true)) && ((theScrap==true) || (theRetReplaced==true)) )
		{
			alert("You cannot tick these check boxes at the same time (Scrap, Returned not repaired, ...).");
			return false;
		}
		
		return true;
}



// Check that a string contains only letters and numbers
function isAlphanumeric(string, ignoreWhiteSpace) 
{
	if (string.search) 
	{
		if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) 
		return false;
	}
	if(string == "")
        {
          return false;
        }
	return true;
}

function checkFormPN () 
{
		//Checking pnum.asp
		if ( (document.wtyRegister.txtPartNo.value==""))
		{
			alert("A part number must be entered.");
			document.wtyRegister.txtPartNo.focus();
			return false;
		}
		if ( (document.wtyRegister.txtclass.value==""))
		{
			alert("A class must be entered.");
			document.wtyRegister.txtclass.focus();
			return false;
		}
						
		return true;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) 
{
	if (string.search) 
	{
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1))
		return false;
	}
        if(string == "")
        {
          return false;
        }
	return true;
}


// Check that an email address has the form something@something.something
// This is a stricter standard than RFC 821 (?) which allows addresses like postmaster@localhost


function isEmail(email) {
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";

    // Check for null
    if (email == "") {
        return true;
    }

    // Check for invalid characters as defined above
    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }
    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }
    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    // There must be at least one @ symbol
    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    // But only ONE @ symbol
    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    // Also check for at least one period after the @ symbol
    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}

//************************************ Opening a small new window
function popupWin()
{
	newWindow = window.open('enter_items_conf.asp','340x400','toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,width=340,height=370')
	//setTimeout('closeWin(newWindow)', 5000);	// delay 5 seconds before closing
}

function closeWin(newWindow) 
{
	newWindow.close();				// close small window and depart
}

function closeWindow() 
{
	window.close();				// close small window and depart
}

function checkDate(dateVal)
{
			theDate = dateVal //window.document.wtyRegister.txtDateReceived.value;
			theDay = theDate.substr(0,theDate.indexOf("/",1));
			theString = theDate.substr(theDate.indexOf("/",1)+1);
			theMonth = theString.substr(0,theString.indexOf("/",1));
			theYear = theString.substr(theString.indexOf("/",1)+1);
			
			if ((parseInt(theDay) <= 0) || (parseInt(theDay) > 31) || (isNumeric(theDay)==false) )
			{
				alert ( "Wrong date, the day value, " + theDay + ", could be wrong. Format must be dd/mm/yyyy (i.e. 24/12/2003)" );
				return false;
			}
			if ((parseInt(theMonth) <= 0) || (parseInt(theMonth) > 12) || (isNumeric(theMonth)==false) )
			{
				alert ( "Wrong date, the month value, " + theMonth + ", could be wrong. Format must be dd/mm/yyyy (i.e. 24/12/2003)" );
				return false;
			}
			if ((parseInt(theYear) < 2000 && (parseInt(theYear) != 1900)) || (isNumeric(theYear)==false))
			{
				alert ( "Wrong date, the year value, " + theYear + ", could be wrong. Format must be dd/mm/yyyy (i.e. 24/12/2003)" );
				return false;
			}
			
			return true;

}
