//Copyright(c) 1999-2000 Lending Universe Inc.		                 
//PURPOSE: client-side form field validation code
//	This set of function are general includes for validation
//	They are designed in pairs the validation and the event function
//	the event function will call the validation with the event src
//PARAMETERS:    
//CALLED FROM:
//CREATED:                   
//MODIFIED: 12/21/99    new email check and added validCurrency()
//MODIFIED: 01/27/99    modified all
//			04/20/2000	- min pass length is now 6chars
//			10/04/2000 - ValidPhone()
//			11/09/2000 - ValidPhone() modified to allow "+" characters	
//			11/30/2000 - new function ValidState_Country()
//			01/31/2001 - bug fix in ValidCurrency() and ValidNumeric() functions
//			03/07/2001 - changes to ensure compatibility with WebTV browsers

function DefaultValue(item) {
	var strDefault = item.defaultValue;
	if (strDefault==null || strDefault=="")
		strDefault="";
	return strDefault;
}

function TrimString(strValue) {
	var ichar, icount;
	ichar = strValue.length - 1;
	icount = -1;
	while (strValue.charAt(ichar)==' ' && ichar > icount)
		--ichar;
	if (ichar!=(strValue.length-1))
		strValue = strValue.slice(0,ichar+1);
	ichar = 0;
	icount = strValue.length - 1;
	while (strValue.charAt(ichar)==' ' && ichar < icount)
		++ichar;
	if (ichar!=0)
		strValue = strValue.slice(ichar,strValue.length);
	return strValue;
}

function DateToSimpleForm(dtItem) {
	var toSimpleForm = new String;
	toSimpleForm = dtItem.toLocaleString();
	toSimpleForm = toSimpleForm.substring(0,toSimpleForm.indexOf(' '));
	return toSimpleForm;
}


function NonBlank(item, item_name) {
	var strErrorMsg = "Please enter the " + item_name + ".";
	item.value = TrimString(item.value);
	if (item.value.length==0) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function NoSpaces(item, item_name) {
	var strErrorMsg = item_name + " must not have spaces.";
	item.value=TrimString(item.value);
	
	for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
		if (item.value.charAt(intLoop) == ' ') {
			item.focus();
			alert(strErrorMsg);
			return false;
		}}
  return true;
}

function PasswordLenghtMin(item, item_name) {
	var strErrorMsg = item_name + " must be at least 4 characters long.";
	item.value=TrimString(item.value);
	if (item.value.length<4) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function PasswordLenghtMax(item, item_name) {
	var strErrorMsg = item_name + " must be at most 50 characters long.";
	item.value=TrimString(item.value);
	if (item.value.length>50) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function StringLenghtMax(item, item_name, len) {
	var strErrorMsg = item_name + " must be at most " + len + " characters long.";
	item.value=TrimString(item.value);
	if (item.value.length>len) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function ValidNumber(item, item_name) {
	var strErrorMsg = item_name + " must be a valid numeric.";
	var strDefault = DefaultValue(item);
	if (strDefault.length==0) {
		strDefault="0";
	}
	item.value=TrimString(item.value);
	if (item.value.length==0)
		item.value=strDefault;
		
	var num = ".0123456789";
	for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
		if (num.indexOf(item.value.charAt(intLoop)) == -1) {
			item.focus();
			alert(strErrorMsg);
			return false;
		}
	}
	if (item.value.indexOf(".")!=item.value.lastIndexOf(".")) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}


function ValidHours(item, item_name) {
	var strErrorMsg = item_name;
	if (!ValidNumber(item, item_name))
		return false;
	var itemValue = new Number(item.value);
	if ((itemValue < 0 || itemValue > 80)) {
		item.focus();
		alert(strErrorMsg + " must have a value from 0 to 80 hours.");
		return false;
	}
	itemValue *= 4;
	if ((itemValue)!=Math.ceil(itemValue)) {
		item.focus();
		alert(strErrorMsg + " must be a valid quartely increment.");
		return false;
	}
	return true;
}

function ValidDate(item, item_name) {
	var strErrorMsg = item_name;
	if (isNaN(Date.parse(item.value))) {
		item.focus();
		alert(strErrorMsg + " must be a valid Date.");
		return false;
	}
	var dtItem = new Date(Date.parse(item.value));
	item.value = DateToSimpleForm(dtItem);
	return true;
}

function ValidDate2(item, item_name) {
	var strErrorMsg = item_name;
	if (isNaN(Date.parse(item.value))) {
		item.focus();
		alert(strErrorMsg + " must be a valid Date.");
		return false;
	}
	return true;
}

function DateDifference(item1, item2) {
 if((Date.parse(item2.value) - Date.parse(item1.value)) < 0) {
 	item1.focus();
 	alert("Begining date must be before ending date!")
 	return false;
 }
 	
	return true;

 }
 
 
 
function ItemSelected(item, item_name) {
	var strErrorMsg = item_name + " must be a valid selection.";
	if (item.selectedIndex==0) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function ValidState_Country(itemState, itemCountry) {
	var strErrorMsg = 'Please correct State/Province or Country.';
	var iState = itemState.options[itemState.selectedIndex].value;
	var iCountry = itemCountry.options[itemCountry.selectedIndex].value;

	
	if (iState == 1 && (iCountry == 840 || iCountry == 124)) {
		itemState.focus();
		alert('State/Province is required for USA and Canada. ' + strErrorMsg);
		return false;
	}
	if ((iState > 1 && iState < 10000) && iCountry != 840) {
		itemState.focus();
		if (iCountry != 124)
			alert("Please select '--Other/Not in USA--' if your State/Province is outside of USA or Canada and enter it in the box below.");
		if (iCountry == 124)
			alert('State/Province is required for USA and Canada. ' + strErrorMsg);
		
		return false;
	}
	if ((iState > 10000 && iState < 20000) && iCountry != 124) {
		itemState.focus();
		if (iCountry != 840)
			alert("Please select '--Other/Not in USA--' if your State/Province is outside of USA or Canada and enter it in the box below.");
		if (iCountry == 840)
			alert('State/Province is required for USA and Canada. ' + strErrorMsg);		
		
		return false;
	}

	return true;
}


function ValidZip(item, item_name) {
	var strErrorMsg = item_name + " must be of the form 99999-9999.";
	item.value = TrimString(item.value);
	if (item.value.length==0) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}

	return true;
}

function ValidSSN(item, item_name) {
	var strErrorMsg = item_name + " must be a valid SSN.";
	item.value = TrimString(item.value);
	if (item.value.length==0) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}

	return true;
}


function ValidEmail(item, item_name) {
	var strErrorMsg = item_name + " is not a valid Email.";
	var i = 0;
	var iLen;
	var vTmp;

	item.value = TrimString(item.value);
	iLen = item.value.length;

	// it must be at least 5 characters long (x@x.x)
	if (iLen < 5)
	{
		item.focus();
		alert(strErrorMsg);
		return false;
	}

	// check if there are any invalid characters
	for (i = 0; i < iLen; i++)
	{
		vTmp = item.value.charAt(i);
		if (vTmp == ' ' || vTmp == ',' || vTmp == ';' || vTmp == '<' || vTmp ==
'>')
		{
			item.focus();
			alert(strErrorMsg);
			return false;
		}
	}

	// "@" must exist and cannot be the first or the last character
	i = item.value.indexOf("@");
	if (i < 1 || i == (iLen - 1))
	{
		item.focus();
		alert(strErrorMsg);
		return false;
	}

	if (item.value.substr(0,1)=="." || item.value.substr(i-1,1) == "." ||
item.value.substr(i+1,1) == "." || item.value.substr(iLen-1,1) == ".")
	{
		item.focus();
		alert(strErrorMsg);
		return false;
	}

	if (item.value.indexOf("..")>-1 || item.value.indexOf("@@") > -1)
	{
		item.focus();
		alert(strErrorMsg);
		return false;
	}

	return true;
}



function ValidEmail_old(item, item_name) {
	var strErrorMsg = item_name + " is not a valid Email.";
	var i = 0;
	var iLen;
	
	item.value = TrimString(item.value);
	iLen = item.value.length;

	// it must be at least 5 characters long (x@x.x)
	if (iLen < 5)
	{
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	
	// check if there are any spaces
	for (i = 0; i < iLen; i++)
	{
		if (item.value.charAt(i) == ' ')
		{
			item.focus();
				strErrorMsg="Ovde sam";
			alert(strErrorMsg);
			return false;
		}
	}
	
	// "@" must exist and cannot be the first or the last character
	i = item.value.indexOf("@");
	if (i < 1 || i == (iLen - 1))
	{
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	
	if (item.value.substr(0,1)=="." || item.value.substr(i-1,1) == "." || item.value.substr(i+1,1) == "." || item.value.substr(iLen-1,1) == ".")
	{
		item.focus();
		alert(strErrorMsg);
		return false;
	}

	if (item.value.indexOf("..")>-1 || item.value.indexOf("@@") > -1)
	{
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	
	return true;
}

function ValidCurrency(item, item_name, minValue) {
	var minV = minValue;
	var strErrorMsg = item_name + " must be a valid currency.";
	
	if (minV != null) {
	   strErrorMsg = item_name + " must be a valid currency greater than " + minValue + ".";
	}
	   
	var strDefault = DefaultValue(item);
	if (strDefault.length==0) {
		strDefault="0";
	}
	item.value=TrimString(item.value);
	if (item.value.length==0)
		item.value=0;
	// remove leading $ sign if exists
	if (item.value.charAt(0) == "$")  item.value = item.value.substr(1);	
	
	//remove separating commas
	var sTemp="";
	for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
	    sTemp += ((item.value.charAt(intLoop)!=',')?item.value.charAt(intLoop):"");
	}
	item.value=sTemp;
	
	var num = ".0123456789";
	for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
		if (num.indexOf(item.value.charAt(intLoop)) == -1) {
			item.focus();
			alert(strErrorMsg);
			return false;
		}
	}
	if (item.value.indexOf(".")!=item.value.lastIndexOf(".")) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}

	if ((minV != null) && (item.value < minV)) {
		item.focus();
		alert(item_name + " must be a valid currency greater than " + minValue + ".")
		//alert(strErrorMsg);
		return false;
	}
	return true;
}

function ValidPhone(item, item_name) {

	var sTmp;
	var strErrorMsg = item_name + " must be a valid phone number.";
	
	var strDefault = DefaultValue(item);
	if (strDefault.length==0) {
		strDefault="";
	}

	item.value=TrimString(item.value);

	if (item.value.length==0)
		item.value=strDefault;
	
	var num = "xX0123456789()/-+.";
	for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
		sTmp = item.value.charAt(intLoop)
		if (num.indexOf(item.value.charAt(intLoop)) == -1 && sTmp != ' ')
		{
			item.focus();
			alert(strErrorMsg);
			return false;
		}
	}
	
	return true;
}

function G2Z(item, item_name) {
	var strErrorMsg = item_name + " must be non zero value.";
	var strDefault = item.value;
	var fDefault = parseFloat(strDefault)
	
	if (!(fDefault>0.0)) {
		item.focus();
		alert(strErrorMsg);
		return false;	
	}
	return true;
}

