/****************************************************************/

// File Name: FormValidations.js
// Description: This is a common vlidations script file

/****************************************************************/


// Right trims the string...  Useful for SQL datatypes of CHAR

function RTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var endpos = 0

	for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
		c = str.charAt(i);
		if (whitespace.indexOf(c) == -1)
			endpos = i;
	}

	return str.substring(0,endpos+1);
}

function LTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var len = str.length;
	var endpos = len;
	var begpos = 0;

	while (begpos == 0)
	{
		len = str.length;
		if(len>0)
		{
			c = str.charAt(0);
			if (whitespace.indexOf(c) == 0)
			{
				str = str.substring(1);
			}			
			else
			{
				begpos = -1;	
			}
		}
		else
		{
			begpos = -1
			str = "";	
		}		
	}	

	return str;		
		
}


function CheckLength(p_Field, p_len)
{
	var strField = LTrim(p_Field.value + "");
	if (strField.length < p_len)
	{
		return false;
	}
	return true;

}



function ValidateField(p_Field)
{
	var FieldValue = LTrim(p_Field.value + "");
	p_Field.value = FieldValue;
	if (FieldValue.length == 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}


function ValidateDate(p_Date)
{
	var p_DateArr;
	var p_DateVal;
	p_DateVal = p_Date.value + "";
	
	var Month, Day, Year
	var strMonth, strDay, strYear

	var re;
	re = /-/gi;
	
	
	if (p_DateVal.length > 0)
	{
		p_DateVal = p_DateVal.replace(re,"/")
	
		p_DateArr = p_DateVal.split("/");

		if (p_DateArr.length != 3)
		{
			alert("Date must be in format MM/DD/YYYY.")
			p_Date.focus();
			return false;
		}
		else
		{

			strMonth	= p_DateArr[0];
			strDay		= p_DateArr[1];
			strYear		= p_DateArr[2]; 

			Month	= parseInt(strMonth, 10);
			Day		= parseInt(strDay, 10);
			Year	= parseInt(strYear, 10); 


			if ((Month<1 || Month>12 || isNaN(strMonth) || strMonth.indexOf('.') !=-1) )
			{
				alert("Month is incorrect.\nDate must be in format MM/DD/YYYY")
				p_Date.focus();
				return false;
			}
	
			if ((Day<1 || Day>31|| isNaN(strDay) || strDay.indexOf('.') !=-1) )
			{
				alert("Date is incorrect.\nDate must be in format MM/DD/YYYY")
				p_Date.focus();
				return false;
			}

			if (strYear.length != 4 || isNaN(strYear))
			{
				alert("Year is incorrect.\nDate must be in format MM/DD/YYYY")
				p_Date.focus();
				return false;
			}
			else
			{
				if (Year < 1770)
				{
					alert("Year is incorrect. Year cannot be less than 1770");
					p_Date.focus();
					return false;
				}
			}
	
			
			if (Month==4 || Month==6 || Month==9 || Month==11)
			{
				if (Day==31) 
				{
					alert("Date in the month is incorrect")
					p_Date.focus();
					return false;
				}
			}
			
			if (Month==2)
			{
				var g=parseInt(Year/4,10)
				if (isNaN(g)) 
				{
					alert("Year is incorrect")
					p_Date.focus();
					return false;
				}
				
				if (Day>29)
				{
					alert("Date in Februaury is incorrect")
					p_Date.focus();
					return false;
				}
				
				if (Day==29 && ((Year/4)!=parseInt(Year/4,10)))
				{
					alert("Date in Februaury is incorrect")
					p_Date.focus();
					return false;
				}
			}
	
		}

	}
	else
	{
		alert("Date must be in format MM/DD/YYYY.")
		p_Date.focus();
		return false;
	}

	return true;


}


// DEFINE VARIABLES

// whitespace characters
var whitespace = " \t\n\r";



// Check whether string s is empty.

function isEmpty(s)
{  
 return ((s == null) || (s.length == 0))
}



// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

/****************************************************************/

// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function ValidateCurrency(p_theField, p_FieldName)
{
	var strCurrency;
	var re;
	
	re = /[\,\$]/gi;
	
	strCurrency = p_theField.value;
	strCurrency = strCurrency.replace(re,"");
	if (isNaN(strCurrency))
	{
		alert(p_FieldName + " is not valid");
		p_theField.focus();
		return false;
	}
	p_theField.value = strCurrency;
	return true;
}


function ValidateNumber(p_theField, p_FieldName, p_intLength)
{
	var fieldValue;
	fieldValue = p_theField.value;
	if (fieldValue.length > 0)
	{
		if (isNaN(fieldValue))
		{
			alert(p_FieldName + " is not valid");
			p_theField.focus();
			return false;
		}
		else
		{
			if (fieldValue.length < p_intLength)
			{
				alert(p_FieldName + " cannot be less than " + p_intLength + ' digits');
				p_theField.focus();
				return false;
			}
		}
		
		if (fieldValue.indexOf(".") != -1)
		{
			alert(p_FieldName + " cannot contain decimals");
			p_theField.focus();
			return false;
		}

		if (fieldValue.indexOf("-") != -1)
		{
			alert(p_FieldName + " cannot be negative");
			p_theField.focus();
			return false;
		}

	}
	return true;
}


function ValidateDateGreater(testDate, compareDate)
{

	if ((Date.parse(testDate)-Date.parse(compareDate)) > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}


function isAlphaNumeric(theField, nameField)
{
	var theFieldVal;
	var theFieldLen;
	var theChar;

	var regExp1 = /[a-z]|[0-9]/i;

	theFieldVal = LTrim(theField.value + "");
	theFieldLen = theFieldVal.length
	
	for(i=0; i< theFieldLen; i++)
	{
		theChar = theFieldVal.substr(i,1);

		if (theChar.search(regExp1) == -1)
		{
			alert(nameField + " must be alphanumeric")
			theField.focus()
			return false;
		}

	}
	
	return true;
}

function ValidateDateLess(testDate, compareDate)
{
	if ((Date.parse(testDate)-Date.parse(compareDate)) < 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}


