//Functions inside
//
// checkText(name,string,maxlength,minlength,output)
// checkEmail(string,output)
// checkPassword(string,output)
// checkPhone(string,output)
// checkDropdown(name,choice,output)
// checkCheckbox(name,checkbox,output)
// checkAll(theform)


//Checks a text field for errors
//name = Field name
//string = Field value
//maxlength = maximum length allowed
//minlength = minimum length allowed
//output: 1 = alert 2 = masterform
function checkText(name,string,maxlength,minlength,alpha,output){
	var error = "";
	var illegalChars = /[\WA-Z]/;
		//If to short
	if(string.length < minlength){
		error = "The " + name + " is to short.\n";
		}
		//If to long
	if(string.length > maxlength){
		error = "The " + name + " is to long.\n";
		}
		//If bad chars
	if(alpha==1)
		{
		if(illegalChars.test(string)){
			error = "The " + name + " must be all lowercase and only alpha numeric.\n";
			}
		}
		//If empty
	if(string == ""){
		error = "You didn't enter a " + name + ".\n";
		}
		// 1 = alert 2 = masterform
	if(output == 1){
		if(error != ""){
			return(alert(error));
		}
//		return true;
	}
	if(output == 2){
			return error;
	}	
}

function checkEmail(string,output){
	var error = "";
	var illegalChars = /[\(\)\<\>\,\;\:\\\/\[\]]/;
	var emailFilter = /^.+@.+\..{2,3}$/;
	if(illegalChars.test(string)){
		error = "The email address contains illegal characters.\n";
		}
	if(string == ""){
		error = "You didn't enter a password.\n";
		}
	if(!(emailFilter.test(string)))
		{
		error = "Please enter a valid Email address.\n";
		}
	if(output == 1){
		if(error != ""){
			return(alert(error));
		}
//		return true;
	}
	if(output == 2){
			return error;
	}	
}

function checkPassword(string,output){
	var error = "";
	var illegalChars = /[\W_A-Z]/;
	
		//If to short
	if(string.length < 4){
		error = "The password is to short.\n";
		}
		//If to long
	if(string.length > 20){
		error = "The password is to long.\n";
		}
		//If bad chars
	if(illegalChars.test(string)){
		error = "The password must be all lowercase and only alpha numeric.\n";
		}
		//If empty
	if(string == ""){
		error = "You didn't enter a password.\n";
		}
		// 1 = alert 2 = masterform
	if(output == 1){
		if(error != ""){
			return(alert(error));
		}
//		return true;
	}
	if(output == 2){
			return error;
	}	
}

function checkPhone(string,output){
	var stripped = string.replace(/[\(\)\,\-\ ]/g,'');
	var error = "";

	if(stripped.length != 10 && stripped.length != 7 && stripped.length != 11){
		error = "The phone number is the wrong length.\n";
	}	
	if(isNaN(parseInt(stripped))){
		error = "The phone number contains illegal characters.\n";
	}
	if(string == ""){
		error = "You didn't enter a Phone number.\n";
	}
	if(output == 1){
		if(error != ""){
			return(alert(error));
		}
//		return true;
	}
	if(output == 2){
		return error;
	}		
}

function checkDropdown(name,choice,output){
	var error = "";
	
	if(choice == 0){
		error = "You need to select a " + name + ".\n";
	}
	if(output == 1){
		if(error != ""){
			return(alert(error));
		}
//		return true;
	}
	if(output == 2){
			return error;
	}	
}

function checkCheckbox(name,checkbox,output){
	var error ="";

	if(checkbox.checked != true){
		if(name =="Accept"){
			error = "You are required to accept to the associated Policies of this form before submitting it.\n"
		}else{
			error = "You need to " + name + ".\n";
		}
	}
	if(output == 1){
		if(error != ""){
			return(alert(error));
		}
//		return true;
	}
	if(output == 2){
			return error;
	}	
}