function test() {
	alert("This was included");
}

var validations = new Array();

function registerValidation(f) {
  validations.push(f);
}

function doValidation() {
 
    validateMessage = "";
    validateErrors = 0;
    checkMsg = "";
    
    for (i in validations) {
  	  checkMsg += validations[i]();
    }

    // scan for hidden fields named "valid:something:type:flag"
    // Format of the field is "valid:fieldname:type:flag"
    // The value of the field is a colon seperated list of functions that this
    // value must pass to be declared valid
    // type is the type of value to be validated
    // flag should be 1 or 0 to indicate whether the field is mandatory, ie whether it can be blank
    
    //  <INPUT TYPE='hidden' NAME='valid:CardName:text' value='Please enter your name as it is printed on your card.'>
    
    numFields = document.forms[0].elements.length;
    
    for (i=0; i < numFields; i++) {
        if(document.forms[0].elements[i].name.indexOf("valid") != -1) {
         
            fieldName = document.forms[0].elements[i].name.split(":")[1];
            validFunc = document.forms[0].elements[i].name.split(":")[2];
            mandatory = document.forms[0].elements[i].name.split(":")[3];
            errMsg = document.forms[0].elements[i].value;
            //alert(fieldName + " : " + validFunc + " : " + errMsg);
            
            evalStr = "checkMsg += valid" + validFunc + "('forms[0]','" + fieldName + "','" + errMsg + "'," + mandatory + ");";
            eval(evalStr);
       
        } 
        
    }
 
    // Now that we've looped through our validation, we need to test for the presence of
    // a value in "checkMsg" - if it isn't null, validation failed somewhere...
    
    if(checkMsg != "") {
        validationAlert = "The following errors were found:\n\n" + checkMsg + "\n\nPlease correct these before submitting the form.";
        alert(validationAlert);
        return false;
    } else {
    	//alert("returning true");
    	//return false;
        return true;
    }

	return false
}

function validtext(formName, fieldName, errMsg, mandatory) {

    retMsg = "";
    
    // get the value of the field being checked into "checkText" and then test
    var checkText = "checkText = document." + formName + "." + fieldName + ".value";
    eval(checkText);

    // If the field value is null, we have an error - therefore, populate retMsg with the errMsg    
    if (mandatory > 0 && checkText == "") {
        retMsg = " - " + errMsg + "\n";
    }

    return retMsg;
}    

function validnumber(formName, fieldName, errMsg, mandatory) {

    retMsg = "";
    
    // get the value of the field being checked into "checkText" and then test
    var checkText = "checkText = document." + formName + "." + fieldName + ".value";
    eval(checkText);

    // If the field value is null, we have an error - therefore, populate retMsg with the errMsg    
    if (mandatory > 0 && (checkText == "" || !isInteger(checkText))) {
        retMsg = " - " + errMsg + "\n";
    }

    return retMsg;
}   

function isInteger(s) {

    var i;

    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    
    return true;
}



function stripCharsInBag(s, bag) {

    var i;
    var returnString = "";

    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function validphonenumber(formName, fieldName, errMsg, mandatory) {

    // These chars + digits are allowed
    var validPhoneChars = "()+ ";

    // Set up retMsg as null
    retMsg = "";

    // get the value of the field being checked into "checkText" and then test
    var checkText = "checkText = document." + formName + "." + fieldName + ".value";
    eval(checkText);

	if(mandatory == 0 && checkText == "") {
		return retMsg;
	}

    // Strip out valid characters from string
    checkText = stripCharsInBag(checkText, validPhoneChars);

    // If the field value is null or is not a valid phone number, we have an error - therefore, populate retMsg with the errMsg
    if (checkText == "" || !isInteger(checkText)) {
        retMsg = " - " + errMsg + "\n";
    }

    return retMsg;
}       

function validemail(formName, fieldName, errMsg, mandatory) {

    retMsg = "";
    
    var checkEmail = "checkEmail = document." + formName + "." + fieldName + ".value";
    eval(checkEmail);
    
    if (checkEmail == "") {
    	if(mandatory > 0) {
        	retMsg = " - " + errMsg + "\n";
        }
        return retMsg;
    }
    
    // test for the @ sign and die if not found.
    atPos = checkEmail.indexOf("@");
    
    if(atPos == -1) {
        retMsg = " - " + errMsg + "\n";
        return retMsg;
    }

    // Check for presence of dot adjacent to @ sign = not legal!
    if((checkEmail.charAt((atPos * 1)+1) == ".") || (checkEmail.charAt((atPos * 1)-1) == ".")) {
        retMsg = " - " + errMsg + "\n";
        return retMsg;
    }       
    
    // And check for at least one dot after the @ sign
    if(checkEmail.lastIndexOf(".") < atPos) {
        retMsg = " - " + errMsg + "\n";
        return retMsg;
    }       
    
    // All is well and good if we get to here   
    return retMsg;            
}



function validdate(formName, fieldName, errMsg, mandatory) {
    var regexp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
    return validregexp(formName, fieldName, errMsg, mandatory, regexp);
}


function validalphanumeric(formName, fieldName, errMsg, mandatory) {
    var regexp = /^[A-Za-z0-9_]*$/;
    return validregexp(formName, fieldName, errMsg, mandatory, regexp);
}


function validpassword(formName, fieldName, errMsg, mandatory) {
    var regexp = /^\S{6,}/;
    return validregexp(formName, fieldName, errMsg, mandatory, regexp);
  return "";
}



function validregexp(formName, fieldName, errMsg, mandatory, regexp) {  
    var retMsg = "";
    var check = "check = document." + formName + "." + fieldName + ".value";
    eval(check);
    var retMsg = "";    
    if(!check.match(regexp)) {
        retMsg = " - " + errMsg + "\n";
        return retMsg;
    }
    if(mandatory == 1 && check.length==0) {
   	retMsg = " - " + errMsg + "\n";
        return retMsg;
    }
    return retMsg;
}

