
// This function tests the email text against a regexp
// alpha-characters with the posibility of a period then the @ symbol
// then alpha-characters then a period must be present and end with
// no less than 2 more than 3 alpha-characters.

 function IsEmail(tmpStr){
	var email = /^[\'\-\w._]+@[\'\-\w._]+.\w{2,3}$/;
	return email.test(tmpStr)
}

// Same concept as e-mail only I test for zip or postal before returning a value

function IsPostal(pstStr){

    var zipcode = /\d{5}(-\d{4})?/;
	var postcode = /\w\d\w\s?\d\w\d/;
	if(zipcode.test(pstStr))return true;
	  else
	if(postcode.test(pstStr))return true;
	 else
	return false;
	
}

//adding an error to the array

function _add_err(err_msg, err_field, boolFocus,FormName){
	var tmpLength = this.err_list.length;
	this.err_list[tmpLength] = new Array();
	this.err_list[tmpLength][0] = err_msg;
	this.err_list[tmpLength][1] = err_field;
	this.err_list[tmpLength][2] = boolFocus;
	this.err_list[tmpLength][3]	= FormName;	
}

// printing out the errors in an alert box
function _show_err(){
	// if empty exit function
	if(!this.err_list.length)
		return;
	
	var intro = "The form was not submitted because the following field(s) was/were not filled out:\n\n";
	var total_err = "";
	for(var i=0;i<this.err_list.length;i++){
		total_err += this.err_list[i][0] + "\n";
	}
	
	total_err = intro + total_err;
	alert(total_err);
	//if(this.err_list[0][2] != false){		
	//set the cursor to the first incomplete field
		//document.forms[this.err_list[0][3]].elements[this.err_list[0][1]].focus();
		//};
}

// clearing the array - good housekeepping
function _clear_errs(){
	this.err_list.length = 0;
}

//not being used here - however it can be used to state the number of errors created
function _num_errs(){
	return this.err_list.length;
}

//the constructor function for the error object
function Err(){
	this.err_list = new Array();
	this.add_err = _add_err;
	this.show_err = _show_err;
	this.num_errs = _num_errs;
	this.clear_errs = _clear_errs;
}

function radioval(x){
	for(i=0;i<x.length;i++)
		if(x[i].checked==true)
			return true;
	return false;
}
//-----------------------------------------------------------

//Create Err object
page_err = new Err();

//the validation of each element in the form is listed below
 function validate_form(FormName){
	var x=document.forms[FormName];
	
	page_err.clear_errs();
		
			
		if(FormName=="PassEnter"){
			if(x.UserName.value=="")page_err.add_err("User Name","UserName", true,"PassEnter");
			if(x.Password.value=="")page_err.add_err("Password","Password", true,"PassEnter");
		}
		
		if(FormName=="ChangePass"){
			if(x.UserName.value=="")page_err.add_err("User Name","UserName", true,"ChangePass");
			if(x.Password.value=="")page_err.add_err("Password","Password", true,"ChangePass");
			if(x.Password.value != x.ConPassword.value)page_err.add_err("Passwords Don\'t Match","ConPassword", true,"ChangePass");
			
		}
	
		if(FormName=="NewsletterNm"){
			if(x.FName.value=="")page_err.add_err("First Name","FName", true,"NewsletterNm");
			if(x.LName.value=="")page_err.add_err("Last Name","LName", true,"NewsletterNm");
			if(!IsEmail(x.email.value))page_err.add_err("E-mail", "email", true,"NewsletterNm"); 
		}
		
		if(FormName=="NewsletterDel"){
			if(!IsEmail(x.email.value))page_err.add_err("E-mail", "email", true,"NewsletterDel"); 
		}

		if(FormName=="RTSCalc"){
			if(x.mPMT.value=="")page_err.add_err("Monthly Payment","mPMT", true,"RTSCalc");
		}
		
		
//end of validations next we print them by calling the error objects show prop.

	  if(page_err.err_list.length!=0){
	  	page_err.show_err(FormName);
		return false;
	  }
	}

