<!-- Check form javascript validator                         -->
<!-- By Edward Francavilla                                   -->
// A utility function that returns true if password matches confirm password
function confirmp()
{
	var pass1 = document.change_pword.new_pass.value-0;
	var pass2 = document.change_pword.new_pass2.value-0;
	if (pass1 != pass2){ alert("password and confirm password are not the same"); 
		    return false;
	}
    return true;	
}

// A utility function that returns true if a string contains only 
// whitespace characters.
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;
}

// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function verify(f)
{
    var msg2;
    var empty_fields = "";
    var errors = "";
    var ex_year = "";
    var ex_date = "";
    var ex_month = ""; 

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        if (((e.type == "text") || (e.type == "textarea")|| (e.type == "password")) && !e.optional) {
            // first 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";
                }
            }
			*/?>
        }
	// now check that email is formatted correctly
	if ((e.name == "Email") && !e.optional) {
		var str=e.value
		var filter=/^.+@.+\..{2,4}$/;
		

		if (filter.test(str)){
		}
		else{
			errors += "-Invalid email\n" + e.value +  " is not a valid email address\n\n";
		}
		
	}


	    // now check that the Ccard is not expired
	if ((e.name == "expire_month") && !e.optional) {
			ex_month = e.value.toString()

	}

	if (e.name == "expire_year") {
			ex_year = e.value.toString()
			ex_date = ex_year + ex_month

		// get the present date
			var d = new Date()
			var year = d.getFullYear()
			var month = (d.getMonth() + 1)
		// if date less than 10 add 0 before month
			if(month<=9){
			   month = "0"+month
			}
			year = year.toString()
			var new_date = year + month
			if (ex_date < new_date){ 
				errors += "- Expired Credit Card\nThe date you selected is already expired\n\n";
			}

	}


    }

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) return true;

    msg2  = "______________________________________________________\n\n"
    msg2 += "The form was not submitted because of the following error(s).\n";
    msg2 += "Please correct these error(s) and re-submit.\n";
    msg2 += "______________________________________________________\n\n"

    if (empty_fields) {
        msg2 += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg2 += "\n";
    }
    msg2 += errors;
    msg2 += "\n______________________________________________________\n\n"
    alert(msg2);
    return false;
}

