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;
	if ( isEmptyOK == true && isEmpty(checkStr ) )
		return true;
    if ( isWhitespace(checkStr) ) 
	{
		WarnInvalid(theField, fieldName);
		return false;
	}

	if(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 CheckNumeric(theField, isEmptyOK, fieldName)
{
	var checkStr;
	
	checkStr=theField.value;
	if ( isEmptyOK == true && isEmpty(checkStr) ) 
		return true;
    
    if ( isWhitespace(checkStr) || isNaN(checkStr) ) 
	{
		WarnInvalid(theField, fieldName);
		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("@");
	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)
	{
		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 radio_button_checker() {
    // set var radio_choice to false
    var radio_choice = false;

    // Loop from zero to the one minus the number of radio button selections
    for (counter = 0; counter < document.forms[0].HomeOwner.length; counter++) {
        // If a radio button has been selected it will return true
        // (If not it will return false)
        if (document.forms[0].HomeOwner[counter].checked)
            radio_choice = true;
    }

    if (!radio_choice) {
        // If there were no selections made display an alert box 
        alert("Please select whether you own a home or rent.")
        return (false);
    }
    return (true);
}

