<!-- //
/// proper form validation class in JS

var validate = function() {

    // html form name
    var htmlForm = document.getElementById("aspnetForm"); // will _always_ be this;

    // form array object to add fields to validate to
    this.vform = new Array(); 
    
    // field object to add to form obj; 
    var vfield = function(refName, name, validRule) {
        this.refName = refName;
        this.name = name;
        try {
            this.formObject = fieldFinder(name);
            this.value = getVal(this.formObject);
        } catch (err) {
            this.formObject = null;
            this.value = "";
        }
        this.validRule = validRule;
    } 
    
    // add field to form
    this.addFormField = function(field) {
        this.vform[this.vform.length] = field;
    }
    
    // generic add validation rule method
    this.addVField = function(refName, name, validRule) {
        var field = new vfield(refName, name, validRule);
        this.addFormField(field);
    }
    
    // specific add methods
    // not null
    this.addVField_notnull = function(refName, name) {
        var field = new vfield(refName, name, "notnull");
        this.addFormField(field);
    }
    
    // alpha num
    this.addVField_alphanum = function(refName, name) {
        var field = new vfield(refName, name, "alphanum");
        this.addFormField(field);
    }
    
    // num only
    this.addVField_num = function(refName, name) {
        var field = new vfield(refName, name, "num");
        this.addFormField(field);
    }
    
    // email
    this.addVField_email = function(refName, name) {
        var field = new vfield(refName, name, "email");
        this.addFormField(field);
    }
    
    // email
    this.addVField_date = function(refName, name) {
        var field = new vfield(refName, name, "date");
        this.addFormField(field);
    }
    
    // regex patterns;
    var alpha = /[^A-Za-z]/ig;
    var alphanum = /[^\w \-]/ig;
    var dirname = /[^A-Za-z0-9]/ig;
    var num = /[^\d]/ig;
    var phone =/[^\d \-()\.]/ig
    var notnull = /[^\s]/ig;
    var email = /(\w{1,})@.*\.(\w{2,})/ig;
    var URL = /(http:\/\/)?[\w\.-]+\.[A-Za-z\.]{2,}/ig;
    var dte = /\d{1,2}\/\d{1,2}\/[20]?[\d{2}][ ]?[\d:ampm ]?/ig;
    var date = /\d{1,2}\/\d{1,2}\/[20]?[\d{2}]/ig;
    var img = /\.(gif|jp[e]?g)$/ig;
    
    
    this.validateForm = function() {
        var valid = true;
        for (x = 0; x < this.vform.length; x++) {
            if (valid) {
                var field = this.vform[x];                
                if (field.formObject != null && field.validRule != "") {               
                    if (field.validRule == "notnull") {
                        valid = (field.value.length > 0) ? true : false;
                    } else if (field.validRule == "alphanum") {
                        valid = (field.value.search(alphanum) == -1) ? true : false;
                    } else if (field.validRule == "num") {
                        valid = (field.value.replace(/\s/ig,"").search(num) == -1) ? true : false;
                    } else if (field.validRule == "email") {
                        valid = (field.value.match(email) == null) ? false : true;
                    } else if (field.validRule = "date") {
                        valid = (field.value.match(date) == null) ? false : true;
                    }
                    if (!valid) {
                        if (field.validRule == "notnull") 
			{
                            
				if (field.refName == "Ethnicity")
                                	alert("Please select an option in the student's ethnicity field.\r\nThis information is used to report to our donors.\r\nIf you are uncomfortable disclosing, you may select other.");
                            	else
                                	alert("The " + field.refName + " field is required.");
                        } 
			else if (field.validRule == "date") {
                            alert("Please format your " + field.refName + " as: MM/DD/YYYY");
                        } else {
                            alert("The value you have entered for " + field.refName + " is invalid.");
                        }
                        field.formObject.focus();
                        break;
                    }                  
                }
            }
        }
        return valid;
    }
    
    var fieldFinder = function(namePart) {
        var field = null;
        var htmlField = null;
        for (x = 0; x < htmlForm.elements.length; x++) {
            if (htmlForm.elements[x].name.toLowerCase().indexOf(namePart.toLowerCase()) > -1) {
                field = htmlForm.elements[x];
                break;
            }
        }
        return field;
    }
	
// get form field value 
    function getVal(f) {
		var val = "";
		//alert("testing: " + f.name);
		var fType = new String(f.type);
		if (fType == "select-one" || fType == "select-multiple") {
			for (xxx = 0 ; xxx < f.options.length; xxx++) {
				if (f.options[xxx].selected == true) {
					val = (f.type == "select-one") ? f.options[xxx].value : val + "|" + f.options[xxx].value;
				}
			}
		} else if (fType == "checkbox") {
			if (f.length) {
				if (f.length > 1) { 
					for (zzz = 0; zzz < f.length; zzz++) {
						if (f[zzz].checked) {
							if (val == "") {
								val += f[zzz].value;
							} else {
								val += "," + f[zzz].value;
							}
						}
					}
				} else {
					if (f.checked) {
						val = f.value;
					}
				}
			} else {
				if (f.checked) {
					val = f.value;
				}
			}
		} else if (fType == "undefined" || fType == "radio") {
			if (f.length > 1) { 
				for (yyy = 0; yyy < f.length; yyy++) {
					if (f[yyy].checked) {
						val += (val != "") ? "," + f[yyy].value : f[yyy].value;
					}
				}
			} else {
				f.checked = true;
				val = f.value;
			}
		} else {
			val = f.value;
		}
		return val;
	}


/// form specific validation routines:
    this.validateContrib = function() {
		htmlForm = document.getElementById("contrib");
        this.vform = new Array();
        var valid = true;
        this.addVField_notnull("Donation Amount","don_amt"); 
		this.addVField_num("Donation Amount", "don_amt");
		var donAmt = getVal(htmlForm.don_amt);
		var donAnon = getVal(htmlForm.don_anon);
		var donName = getVal(htmlForm.don_name);
		donName = (donName.toLowerCase() == "enter name here") ? "" : donName;
		htmlForm.don_name.value = donName;
		if (donAnon == "N") {
			if (donAmt != "") {
				if (Number(donAmt) >= 100) {
					this.addVField_notnull("Recognition Name", "don_name");					
				}
			}
		} else {
			htmlForm.don_name.value = "";
		}
		var donFor = getVal(htmlForm.don_for);
		var donForName = getVal(htmlForm.don_for_name);
		donForName = (donForName.toLowerCase() == "enter name here") ? "" : donForName;
		htmlForm.don_for_name.value = donForName;
		if (donFor == "H" || donFor == "M") {
			this.addVField_notnull("Donation Made on Behalf Of Name", "don_for_name");
		}
        return this.validateForm();
    }
	
	this.validateContribShort = function() 
	{
		htmlForm = document.getElementById("contrib");
        this.vform = new Array();
        var valid = false;
        
		var contribution_amt = getVal(htmlForm.checkoutcontrib);
		var contribution_name = getVal(htmlForm.donname);
		
		if (contribution_amt > 0)
		{
		    valid = true;
		    if ( contribution_amt >= 100 && contribution_name == "" )
		    {
		        valid = false;
		        alert("Your donation of $" + getVal(htmlForm.checkoutcontrib) + " requires that you indicate how you wish to be acknowledged in our playbills.");
		    }   
		}
		else
		    alert("Please enter a valid amount.");

		return valid;
	}
	
	this.validateRegister = function() {
		htmlForm = document.reg;
		this.vform = new Array();
		if (getVal(htmlForm.child_no) == "0") {
			this.addVField_notnull("Child's First Name","fname"); 
			this.addVField_notnull("Child's Last Name","lname");
			this.addVField_notnull("Child's Gender","gender");
			this.addVField_notnull("Date of Birth: Month","bday_month");
			this.addVField_notnull("Date of Birth: Day","bday_day");
			this.addVField_notnull("Date of Birth: Year","bday_year");
			this.addVField_notnull("School Type","school_type");
			this.addVField_notnull("Current Grade","school_year");
			this.addVField_notnull("Current School","school_name");
			this.addVField_notnull("Emergency Contact First Name","emerFname");
			this.addVField_notnull("Emergency Contact Last Name","emerLname");
			this.addVField_notnull("Emergency Contact Relation to Child","emerRel");
			this.addVField_notnull("Emergency Phone - Area Code","emerPhone1a");
			this.addVField_notnull("Emergency Phone - Prefix","emerPhone1b");
			this.addVField_notnull("Emergency Phone - Suffix","emerPhone1c");
			this.addVField_notnull("Emergency Phone Type","emerPhone1Type");
		}
		this.addVField_notnull("Ethnicity","ethnicity");
		this.addVField_notnull("Student's First Class", "firstClass");
		var nsoList = "5,6,7,8,9,10,11,12,";
//		if (getVal(htmlForm.firstClass) == "Y" && (htmlForm.orientation) && (htmlForm.hdn_pre_requisite_returning_students_only != ""))
//		    this.addVField_notnull("Orientation Date","orientation");
//		}
		this.addVField_notnull("Terms and Conditions", "terms_and_conditions");
		var valid = this.validateForm();
		/*if (valid) {
			getVal("emerPhone1").value = getVal("emerPhone1").value.replace(/[^\d]/ig,"");
			getVal("emerPhone2").value = getVal("emerPhone2").value.replace(/[^\d]/ig,"");
		}*/
		//alert(valid);
		return valid;
	}
	
	//		if (getVal(htmlForm.firstClass) == "Y" && (htmlForm.orientation)) {
//			this.addVField_notnull("Orientation Date","orientation");

	
	this.validateChildRecord = function() {
		htmlForm = document.child;
		this.vform = new Array();
		if (htmlForm.fname && htmlForm.lname) {
			this.addVField_notnull("Child's First Name","fname"); 
			this.addVField_notnull("Child's Last Name","lname");
		}
		this.addVField_notnull("Ethnicity","ethnicity");
		this.addVField_notnull("Child's Gender","gender");
		this.addVField_notnull("Date of Birth: Month","bday_month");
		this.addVField_notnull("Date of Birth: Day","bday_day");
		this.addVField_notnull("Date of Birth: Year","bday_year");
		this.addVField_notnull("School Type","school_type");
		this.addVField_notnull("Current Grade","school_year");
		this.addVField_notnull("Current School","school_name");
		this.addVField_notnull("Emergency Contact First Name","emerFname");
		this.addVField_notnull("Emergency Contact Last Name","emerLname");
		this.addVField_notnull("Emergency Contact Relation to Child","emerRel");
		this.addVField_notnull("Emergency Phone - Area Code","emerPhone1a");
		this.addVField_notnull("Emergency Phone - Prefix","emerPhone1b");
		this.addVField_notnull("Emergency Phone - Suffix","emerPhone1c");
		this.addVField_notnull("Emergency Phone Type","emerPhone1Type");
		this.addVField_notnull("Terms and Conditions","medicalWaive");
		var valid = this.validateForm();
		/*if (valid) {
			getVal("emerPhone1").value = getVal("emerPhone1").value.replace(/[^\d]/ig,"");
			getVal("emerPhone2").value = getVal("emerPhone2").value.replace(/[^\d]/ig,"");
		}*/
		//alert(valid);
		return valid;
	}
	
}

// -->