

function round(number,X) {
// rounds number to X decimal places, defaults to 2
X = (!X ? 2 : X);
return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

function checkEmail(s) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(s.value)){
		return (true)
	}
	return (false)
}

function checkStrings(pw1, pw2) {

if (pw1 != pw2) {

return false;
}
else return true;
}

function isAlphaNumeric (s)
{   var i;
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c)) )
		{
			return false;
		}
    }
    return true;
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


function verify(f) {
    var msg;
    var empty_fields = "";
    var errors = "";
	
	
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
		
		if (e.minLength != null){
				if (e.value.length < e.minLength){
					
					errors += "- The field " + e.name + " must have at least " + e.minLength + " characters\n";	
				
				}
		} 
		if (e.maxLength != null){
				if (e.value.length > e.maxLength){
					
					errors += "- The field " + e.name + " must have less than " + e.maxLength + " characters\n";	
				
				}
		} 
		if (e.isSelected){
			if (e.selectedIndex == 0){
				errors += "- No " + e.name + " was selected\n";
			}
		}
		
		if (e.onlyLettersAndNumbers){
			if (!isAlphaNumeric(e.value)){
				errors += "- " + e.name + " field can only contain letters and numbers and no spaces are allowed.\n";
			}	
		}
		
		
		if (e.verifyEmail){
			if (!checkEmail(e)){
				errors += "- Email address provided is not valid\n";
			}
		}
		if (e.verifyStrings){
			var comparisonString;
			comparisonString = e.name + "_verify";
			
			if(!checkStrings(e.value, f.elements[comparisonString].value)){
				errors += "- " + e.name + " and " + f.elements[comparisonString].name + " do not match\n";
			}
			
		}
		if (e.isChecked){
			if(e.checked != true){
				errors += "- " + e.name + " needs to be checked\n";
			}
		}
		
		if (((e.type == "text") || (e.type == "textarea") || (e.type == "password")) && !e.optional) {
            // first_name check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name;
                continue;
            }
			

			
            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) { 
			    var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field " + e.name + " must be a number";
                    if (e.min != null) 
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }
            }
        }
    }

    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "This REALMTB form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}
