var Profanity = new Array("shit","fuck","ass","damn","bitch");
var INVALID_CREDITORS_LIKE = new Array("none","n/a","don't","dont","do not");
function IsInArray(strValue, theArray)
{
	var i;
	var result = false;
	for (i = 0; i < theArray.length; i++)
	{
		if ( strValue == theArray[i] )
			return true;
	}
}

function IsAllVowels(strValue)
{
	if (strValue == null || strValue.length == 0)
		return false;
	
	var VOWELS = "aeiouy";	
	for (var i = 0; i < strValue.length; i++)
	{
		if (VOWELS.indexOf( strValue.charAt(i) ) == -1)
			return false;
	}	
	
	return true;
}

function HasNoVowels(strValue)
{
	if (strValue == null || strValue.length == 0)
		return false;	
	
	var VOWELS = "aeiouy";	
	for (var i = 0; i < strValue.length; i++)
	{
		if (VOWELS.indexOf( strValue.charAt(i) ) >= 0)
			return false;
	}	
	
	return true;
}

function Warn(theField, s)
{
	alert(s);
	theField.focus();	
}

function WarnInvalid(theField, fieldName)
{
	Warn(theField, "Please enter a valid " + fieldName);	
}

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

function isDigit (c)
{   
	return ( (c >= "0") && (c <= "9") )
}

function isUnsignedInteger (s)
{   var i;

	for (i = 0; i < s.length; i++)
   {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


function isWhitespace (s)
{   
	var whitespace = " \t\n\r";
	var i;

    if (isEmpty(s)) 
		return true;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) 
			return false;
    }

    return true;
}

function CheckText(theField, isEmptyOK, fieldName)
{
	var checkStr;
	
	checkStr = theField.value;
	if ( isEmptyOK && isEmpty(checkStr) ) 
		return true;
    
    if ( isWhitespace(checkStr) ) 
	{
		WarnInvalid(theField, fieldName);
		return false;
	}
		
	return true;
}

function CheckSelect(theField, isEmptyOK, fieldName)
{
	var checkStr;
	
	//checkStr = theField.options[theField.selectedIndex].text;
	checkStr = theField.value; //change by clyde pham
	if ( isEmptyOK == true && isEmpty(checkStr ) )
		return true;
    if ( isWhitespace(checkStr) ) 
	{
		WarnInvalid(theField, fieldName);
		return false;
	}

	if(checkStr=="Select:" || checkStr=="Choose")
	{
		WarnInvalid(theField, fieldName);
		return false;
	}
		
	return true;
}



function CheckUnsignedInteger(theField, isEmptyOK, fieldName, length)
{
	var checkStr, doLengthCheck;
	doLengthCheck = CheckUnsignedInteger.arguments.length == 4
	checkStr = theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
    
	if( isWhitespace(checkStr) || !isUnsignedInteger(checkStr) )
	{
		WarnInvalid(theField, fieldName);
		return(false);
	}
	
	if (doLengthCheck && checkStr.length != length)
	{
		WarnInvalid(theField, fieldName);
		return(false);
	}
	
	return true;
}

function CheckUnsignedInteger2(theField, isEmptyOK, fieldName)
{
	var checkStr, doLengthCheck;
	doLengthCheck = CheckUnsignedInteger2.arguments.length == 3
	checkStr = theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
    
	if( isWhitespace(checkStr) || !isUnsignedInteger(checkStr) )
	{
		WarnInvalid(theField, fieldName);
		return(false);
	}
	
	if (doLengthCheck && (checkStr.length < 4 || checkStr.length > 20))
	{
		WarnInvalid(theField, fieldName);
		return(false);
	}
	
	return true;
}

function CheckUnsignedInteger3(theField, isEmptyOK, fieldName, Min, Max)
{
	var checkStr, doLengthCheck;
	doLengthCheck = CheckUnsignedInteger3.arguments.length == 5
	checkStr = theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
    
	if( isWhitespace(checkStr) || !isUnsignedInteger(checkStr) )
	{
		WarnInvalid(theField, fieldName);
		return(false);
	}
	
	if (doLengthCheck && (checkStr.length < Min || checkStr.length > Max))
	{
		WarnInvalid(theField, fieldName);
		return(false);
	}
	
	return true;
}


function CheckNumeric(theField, isEmptyOK, fieldName)
{
	var checkStr;

	checkStr=theField.value.replace(",", "");
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;

    	
    if ( isWhitespace(checkStr) || isNaN(checkStr) ) 
	{
			WarnInvalid(theField, fieldName);
			return false;
	}
	    	
	return true;
}

function CheckMonthlyIncome(theField, isEmptyOK, fieldName, minAmount)
{
	var checkStr;
	
	checkStr=theField.value.replace(",", "");
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
  
    if ( isWhitespace(checkStr) || isNaN(checkStr) )  
	{
			WarnInvalid(theField, fieldName);
			return false;
	}
	
	if(checkStr<minAmount){
		Warn(theField, "Your monthly income before taxes must be at least $" + minAmount + " to qualify.")
		return false;
	}
		
	return true;
}

function CheckEmail(theField, isEmptyOK, fieldName)
{
	var checkStr;
	
	checkStr=theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
    
    if(checkStr=="" || checkStr.lastIndexOf("@")==-1 || checkStr.lastIndexOf(".")==-1)
	{
		WarnInvalid(theField, fieldName);
		return false;
	}
	
	var emailArr=checkStr.split("@");
	var user=emailArr[0];
	var domainAddress=emailArr[1];
	var domainAddressArr=domainAddress.split(".");
	var domain=domainAddressArr[0];
	var net=domainAddressArr[1];

	if(user.length==0 || domain.length==0 || net.length==0)
	{
		WarnInvalid(theField, fieldName);
		return false;	
	}

   	if (theField.value.replace("www","") != theField.value)
	{
		WarnInvalid(theField, fieldName);
		return false;

	}

    	if (theField.value.replace(/^[0123456789]+@/,"") != theField.value)
	{
		WarnInvalid(theField, fieldName);
		return false;

	}

	return true;
}

function CheckZip(theField, isEmptyOK, fieldName)
{
	var checkStr;
	checkStr=theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
	
	if ( isWhitespace(checkStr) || !isUnsignedInteger(checkStr) || (checkStr.length != 5 && checkStr.length != 9) )
    {
			WarnInvalid(theField,fieldName);
			return false;
		}	
	return true;
}

function CheckAmountOwed(theField, isEmptyOK, fieldName)
{
	var isUnsignedInt;
	isUnsignedInt = CheckUnsignedInteger(theField[ theField.selectedIndex ], isEmptyOK, fieldName)
	if (!isUnsignedInt) 
		return false;

	var checkStr = theField[ theField.selectedIndex ].value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
    if (parseInt(checkStr) < 5000 )
	{
		Warn(theField, "Please select a value greater than or equal to \"5000\" in the \"Amount Owed\" field.")
		return false;	
	}
	
	return true;
}

function CheckCreditor(theField, isEmptyOK, fieldName)
{
	var checkCred, isOk;
	isOk = CheckText(theField, isEmptyOK, fieldName)
	if (!isOk)		
		return false;
	
	checkCred = theField.value.toLowerCase()
	if (checkCred==null || checkCred=="" || checkCred.length < 4 || IsInArray(checkCred,Profanity) || IsInArray(checkCred,INVALID_CREDITORS_LIKE)||IsAllVowels(checkCred) == true||HasNoVowels(checkCred) == true)
	{
		WarnInvalid(theField,fieldName);
		return false;
	}
		
	return true;
}

function CheckRadiobutton(theField, isEmptyOK, fieldName) {

myOption = -1;

for (var j=0; j<theField.length; j++) 
{
	if (theField[j].checked) 
	{
		myOption = j;
	}
}

if (myOption == -1) 
{
	if ( isEmptyOK ) 
		return true;

	alert("Please enter a valid " + fieldName);
	return false;
}

return true;
}

function CheckRadiobuttonExt(theField, isEmptyOK, Message) {

myOption = -1;

for (var j=0; j<theField.length; j++) 
{
	if (theField[j].checked) 
	{
		myOption = j;
	}
}

if (myOption == -1) 
{
	if ( isEmptyOK ) 
		return true;

	alert(Message);
	return false;
}

return true;
}


function CheckCheckBox(theField, isEmptyOK, fieldName) {

myOption = -1;

	if (theField.checked) 
	{
		myOption = 1;
		return true;
	}


if (myOption == -1) 
{
	if ( isEmptyOK ) 
		return true;

	alert(fieldName);
	return false;
}

return true;
}




function CheckCheckBox2(theField, isEmptyOK, fieldName) {

myOption = -1;

	if (theField.checked) 
	{
		myOption = 1;
		return true;
	}


if (myOption == -1) 
{
	if ( isEmptyOK ) 
		return true;

	//alert(fieldName);
	//return false;
}

return false;
}

function CheckAddress(theField, isEmptyOK, fieldName)
{
	var checkStr;
	
	checkStr = theField.value;
	if ( isEmptyOK && isEmpty(checkStr) ) 
		return true;
    
    if ( isWhitespace(checkStr) ) 
	{
		WarnInvalid(theField, fieldName);
		return false;
	}

    if (theField.value.replace(/\d/g,"") == theField.value)
	{
		WarnInvalid(theField, fieldName);
		return false;
	}

    if (theField.value.replace(/[BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz][BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz][BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz][BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz][BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz]/,"") != theField.value)
	{
		WarnInvalid(theField, fieldName);
		return false;

	}

    if (theField.value.replace(/[AEIOUYaeiou][AEIOUYaeiou][AEIOUYaeiou][AEIOUYaeiou]/,"") != theField.value)
	{
		WarnInvalid(theField, fieldName);
		return false;

	}


		
	return true;
}




function CheckName(theField, isEmptyOK, fieldName)
{
	var checkStr;
	
	checkStr = theField.value;
	if ( isEmptyOK && isEmpty(checkStr) ) 
		return true;
    
    if ( isWhitespace(checkStr) ) 
	{
		WarnInvalid(theField, fieldName);
		return false;
	}

    if (theField.value.replace(/\d/g,"") != theField.value)
	{
		WarnInvalid(theField, fieldName);
		return false;
	}

    if (theField.value.replace(/[BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz][BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz][BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz][BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz][BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz]/,"") != theField.value)
	{
		WarnInvalid(theField, fieldName);
		return false;

	}

    if (theField.value.replace(/[AEIOUYaeiou][AEIOUYaeiou][AEIOUYaeiou][AEIOUYaeiou]/,"") != theField.value)
	{
		WarnInvalid(theField, fieldName);
		return false;

	}



		
	return true;
}


function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}


function CheckIncome(theField, emptyOK, s)
{

	if ((emptyOK == true) && (isEmpty(theField.value))) 
		return true;

	if (isEmpty(theField.value))
	{
		alert('Please enter a valid currency value for your income')
		return false;
	}

	var stripped;
	stripped = stripCharsInBag(theField.value, "$,")

	if (isNaN(stripped)) 
	{
		//alert("Please enter a valid currency value for the " + s + " field.");
		alert('Please enter a valid currency value for your income')
		return false;
	}
	else
	{
		theField.value = stripped; 
		return true;
	}

}

//******************************************************************************
// Spanish Validation
//******************************************************************************

function WarnInvalidSpanish(theField, fieldName)
{
	Warn(theField, fieldName);	
}

function CheckTextSpanish(theField, isEmptyOK, fieldName)
{
	var checkStr;
	
	checkStr = theField.value;
	if ( isEmptyOK && isEmpty(checkStr) ) 
		return true;
    
    if ( isWhitespace(checkStr) ) 
	{
		WarnInvalidSpanish(theField, fieldName);
		return false;
	}
		
	return true;
}

function CheckSelectSpanish(theField, isEmptyOK, fieldName)
{
	var checkStr;
	
	//checkStr = theField.options[theField.selectedIndex].text;
	checkStr = theField.value; //change by clyde pham
	if ( isEmptyOK == true && isEmpty(checkStr ) )
		return true;
    if ( isWhitespace(checkStr) ) 
	{
		WarnInvalidSpanish(theField, fieldName);
		return false;
	}

	if(checkStr=="Select:" || checkStr=="Choose")
	{
		WarnInvalidSpanish(theField, fieldName);
		return false;
	}
		
	return true;
}

function CheckEmailSpanish(theField, isEmptyOK, fieldName)
{
	var checkStr;
	
	checkStr=theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
    
    if(checkStr=="" || checkStr.lastIndexOf("@")==-1 || checkStr.lastIndexOf(".")==-1)
	{
		WarnInvalidSpanish(theField, fieldName);
		return false;
	}
	
	var emailArr=checkStr.split("@");
	user=emailArr[0];
	domainAddress=emailArr[1];
	var domainAddressArr=domainAddress.split(".");
	domain=domainAddressArr[0];
	net=domainAddressArr[1];

	if(user.length==0 || domain.length==0 || net.length==0)
	{
		WarnInvalidSpanish(theField, fieldName);
		return false;	
	}

   	if (theField.value.replace("www","") != theField.value)
	{
		WarnInvalidSpanish(theField, fieldName);
		return false;

	}

    	if (theField.value.replace(/^[0123456789]+@/,"") != theField.value)
	{
		WarnInvalidSpanish(theField, fieldName);
		return false;

	}

	return true;
}

function CheckZipSpanish(theField, isEmptyOK, fieldName)
{
	var checkStr;
	checkStr=theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
	
	if ( isWhitespace(checkStr) || !isUnsignedInteger(checkStr) || (checkStr.length != 5 && checkStr.length != 9) )
    {
			WarnInvalidSpanish(theField,fieldName);
			return false;
		}	
	return true;
}

function CheckUnsignedIntegerSpanish(theField, isEmptyOK, fieldName, length)
{
	var checkStr, doLengthCheck;
	doLengthCheck = CheckUnsignedIntegerSpanish.arguments.length == 4
	checkStr = theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
    
	if( isWhitespace(checkStr) || !isUnsignedInteger(checkStr) )
	{
		WarnInvalidSpanish(theField, fieldName);
		return(false);
	}
	
	if (doLengthCheck && checkStr.length != length)
	{
		WarnInvalidSpanish(theField, fieldName);
		return(false);
	}
	
	return true;
}

function CheckNumericSpanish(theField, isEmptyOK, fieldName)
{
	var checkStr;
	
	checkStr=theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
    
    if ( isWhitespace(checkStr) || isNaN(checkStr) ) 
	{
		WarnInvalidSpanish(theField, fieldName);
		return false;
	}
		
	return true;
}

function CheckMonthlyIncomeSpanish(theField, isEmptyOK, fieldName, minAmount)
{
	var checkStr;
	
	checkStr=theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
    
    if ( isWhitespace(checkStr) || isNaN(checkStr) ) 
	{
		WarnInvalidSpanish(theField, fieldName);
		return false;
	}
	
	if(checkStr<minAmount){
		Warn(theField, "Su ingreso mensual antes de que los impuestos deban ser por lo menos $" + minAmount + " para calificar.")
		return false;
	}
		
	return true;
}





function CheckMultipleCheckbox(theField, sAlert)
{

	var checkbox_choices = 0;
	var counter;
	var checked;
	checked = false;
	
	// Loop from zero to the one minus the number of checkbox button selections
	for (counter = 0; counter < theField.length; counter++)
	{
	
		// If a checkbox has been selected it will return true
		// (If not it will return false)
		if (theField[counter].checked)
		{ 
			checked = true;
		}
	}
	
	if (checked == false)
	{
		alert(sAlert);
	}
	
	return checked;
}

