// checks to see if it is an integer
// if not returns false
function isInteger(strInt)
{
	var i;
	var intSigleDigit;
	if (strInt.length == 1)
	{
		if ( parseInt( strInt ) + '' != strInt ) return false		
	}
	for (i=0; i<strInt.length-1; i++)
	{
		intSigleDigit = strInt.slice(i, i+1);
		if ( parseInt( intSigleDigit ) + '' != intSigleDigit ) return false
	}
	return true;
}
function checkPosInteger(inpStr) {
	var allValid = true;
	var checkOK = "0123456789";
	for (i = 0;  i < inpStr.length;  i++)
	{
		ch = inpStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))	
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}	
	if (inpStr < "0")
		allValid = false ;
  return allValid ;
}

// check to see if the date is in valid MM/DD/YY(YY) format
// between year: 1900 and year: 2100
// return true - if format is MM/DD/YY(YY)
// return false - if format is not MM/DD/YY(YY)
function checkDateFormat(strDate){
//alert("Here");
	var firstSlash;
	var secondSlash;
	var intMonth;
	var intDate;
	var intYear;
	var isItDate;
	isItDate = true;
	// get the location of month and date divider
	firstSlash = strDate.indexOf("/")
	if (firstSlash == -1) isItDate = false ; // if there are no divider
//alert("Here1")
	if (isItDate == true)	
		{
//alert("Here2")		
		// get the location of the date and year divider
		secondSlash = strDate.indexOf("/", firstSlash + 1) //if there are no divider
//alert(secondSlash);		
		if (secondSlash == -1) isItDate = false ;
//alert(isItDate);		
		// get the month value as integer
		if ( isInteger( strDate.substr(0, firstSlash) ) == false ) isItDate = false ;
//alert("Here3")		
		intMonth = parseInt(strDate.substr(0, firstSlash), 10);
//alert("Here4")		
		if ((intMonth > 12) || (intMonth < 1)) isItDate = false;
//alert("Here5")
		var intBegOfDate;
		intBegOfDate = firstSlash + 1;
		if ( isInteger( strDate.substr(intBegOfDate, secondSlash - intBegOfDate) ) == false ) isItDate = false
		intDate = parseInt(strDate.substr(intBegOfDate, secondSlash - intBegOfDate), 10);
		if ((intDate > 31) || (intDate < 1)) isItDate = false;

		var intBegOfYear;
		intBegOfYear = secondSlash + 1;
		if ( isInteger( strDate.substr(intBegOfYear, strDate.length - intBegOfYear) ) == false ) isItDate = false
		intYear = parseInt(strDate.substr(intBegOfYear, strDate.length - intBegOfYear), 10);
		if ((intYear < 0) || (intYear > 2100)) isItDate = false;
	
		switch (intMonth){
			case 2:
				if (intDate > 29) isItDate = false;
				break;
			case 4:
				if (intDate > 30) isItDate = false;
				break;
			case 6:
				if (intDate > 30) isItDate = false;
				break;
			case 9:
				if (intDate > 30) isItDate = false;
				break;
			case 11:
				if (intDate > 30) isItDate = false;
				break;
			}
		// convert the non-within range dates
		// for example Feb 29 becomes March 1 on non-leap years since
		// there is not Feb 29 on non-leap years
		if (isItDate == true) 
			{
			var newTestDate;
			newTestDate = new Date(intYear, intMonth-1, intDate);
			if (intMonth != (newTestDate.getMonth()+1)) isItDate = false;
			if (intDate != newTestDate.getDate()) isItDate = false;
			if ( (intYear != newTestDate.getFullYear()) 
					&& (intYear != newTestDate.getYear()) ) 
				{
				isItDate = false;
				}
			}
	}
	return(isItDate);
}

// check to see if the date is in valid MM/YY format
// between year: 1900 and year: 2100
// return true - if format is MM/YY
// return false - if format is not MM/YY
function checkMMYYDateFormat(inpStr) {
  var allValid = true;
	var checkOK = "0123456789/";
	var intCountSlash = 0 ;
	for (i = 0;  i < inpStr.length;  i++)
	{
		ch = inpStr.charAt(i);		
		if (ch == "/" )
			intCountSlash = intCountSlash + 1 ;					
		for (j = 0;  j < checkOK.length;  j++)
		{
			if (ch == checkOK.charAt(j))
			{
				if ((ch == "/" ) && !(i == "2"))
					allValid = false;
				break;
			}
		}
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (intCountSlash!=1)
		allValid = false ;
	if (inpStr.length != 5)
		allValid = false ;
  return allValid;
}

// check to see if the the value is valid zip code
function checkZipCode(strZip)
{
	var ZipOriginal;
	var ZipExtension;
	var strHypen;
	if ( (strZip.length != 10) && (strZip.length != 5) ) return false;

	if (strZip.length == 5)
	{
		if ( isInteger(strZip) == false ) return false;
	}

	if (strZip.length == 10)
	{
		ZipOriginal = strZip.slice(0, 4);
		strHypen = strZip.charAt(5);
		ZipExtension = strZip.slice(6, 9);
		if ( isInteger(ZipOriginal) == false ) return false;
		if ( isInteger(ZipExtension) == false ) return false;
		if ( strHypen != '-' ) return false;
	} 
	// at this point, the zip code is valid format
	return true;
}

function checkSSN(inpField) {
	var allValid = true;
	//check the length of the input string
	var inpStr = inpField.value ;

//alert(inpStr);

	if (inpStr.length == 11)
	{
		var numbOfHyphen = 0;
		var posHyphen1 = 0;
		var posHyphen2 = 0;
		var checkOK = "0123456789-";	
		for (i = 0;  i < inpStr.length;  i++)
		{
			ch = inpStr.charAt(i);
			if (ch == "-")
			{
				numbOfHyphen = numbOfHyphen + 1 ;
				if (posHyphen1 == 0)
				{
					posHyphen1 = i ;
				}
				else
				{
					posHyphen2 = i;
				}
			}		
			for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))	
				break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
		}		
		if ((inpStr < "0") || (numbOfHyphen != 2) || (posHyphen1 != 3) || (posHyphen2 != 6))
			allValid = false ;
	}
	else
	{
		if (inpStr.length == 9)
		{
			if(!checkPosInteger(inpStr))
				allValid = false;
			else
				inpField.value = inpStr.substr(0,3) + "-" + inpStr.substr(3,2) + "-" + inpStr.substr(5,4);	
		}
		else
			allValid = false ;	
	}

  return allValid ;
}