/* Form VALIDATION ***********************************************************************
All <input>s to be validated must have an id, and the <form> that contains them 
must have an id.  Invalid <input>s turn hot pink and disable all submit buttons in the same 
<form>. */

// define the backgroundColor for invalid input
var invalidClr='#ffcccc';	//light pink.  Edit?
var validClr='#eeeeee';	//light grey.  Edit?

/* define sets of allowed char */
var Upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var Lwr = 'abcdefghijklmnopqrstuvwxyz';
var Digit = '0123456789';
var Spc = ' ';

//define supersets of allowed char
var Alpha=Upr+Lwr;
var LwrSpc=Lwr+Spc;								//multiple lowercase words
var AlphaSpc=Upr+Lwr+Spc;						//mult words
var AlphaDigit=Upr+Lwr+Digit;
var UprDigit=Upr+Digit;

var AlphaDigitSpc=Upr+Lwr+Digit+Spc;
var Price=Digit+'.';
var Username=Upr+Lwr+Digit+'.~-_';				//username
var Pass=Lwr+Digit+'.~-_';						//password
var Local=Upr+Lwr+Digit+'.!#$%&*+-/=?^_`{|}~';	//e-mail local-part

var Dname=Upr+Lwr+Digit+'.-_';					//domain name
var MoDyYr=Digit+'/';								//date as mo/dy/year
var Address=Upr+Lwr+Digit+Spc+';#';				//addresses

/*	this function does the actual validation and returns a boolean
on the entire <input> as to whether all char are members of the designated set.
	inputstr: <input> string being validated
	set: the char set being validated against
	req: min number of char in inputstr */
function isValid(inputstr,set,req) {
	if (inputstr.length<req) return false;
	for (i=0; i<inputstr.length; i++) {
		if (set.indexOf(inputstr.charAt(i),0) == -1) return false;
		}
	return true;
	}


/* Call this validation function with an onkeyup event in the <input>.
Call with 3 args:
	inputID: ID of the text or password <input> being validated
	req: min number of char required in the <input>
	set: variable-name of the set of allowed char */
function valInput(inputID,req,set) {
	the_input = document.getElementById(inputID);	//an object, the input tag being validated
	var inputstr = the_input.value;	//<input> string being validated
	var inputValid=false;	//initialize, whether <input> is valid
	
	var formValid=true;	//initialize, whether entire <form> has any invalid <input>s
	
	//array of <input>s in the <form> containing the_input
	var all_inputs=document.getElementById(the_input.form.id).getElementsByTagName("input");
	
	//convert the set string into a set variable and call isValid()
	switch (set) {
		case 'Upr':
			inputValid=isValid(inputstr,Upr,req); break;
		case 'Lwr':
			inputValid=isValid(inputstr,Lwr,req); break;
		case 'Digit':
			inputValid=isValid(inputstr,Digit,req); break;
		case 'Alpha':
			inputValid=isValid(inputstr,Alpha,req); break;
		case 'LwrSpc':
			inputValid=isValid(inputstr,LwrSpc,req); break;
		case 'AlphaSpc':
			inputValid=isValid(inputstr,AlphaSpc,req); break;
		case 'AlphaDigit':
			inputValid=isValid(inputstr,AlphaDigit,req); break;
		case 'UprDigit':
			inputValid=isValid(inputstr,UprDigit,req); break;
		case 'AlphaDigitSpc':
			inputValid=isValid(inputstr,AlphaDigitSpc,req); break;
		case 'Price':
			inputValid=isValid(inputstr,Price,req); break;
		case 'Username':
			inputValid=isValid(inputstr,Username,req); break;
		case 'Pass':
			inputValid=isValid(inputstr,Pass,req); break;
		case 'Local':
			inputValid=isValid(inputstr,Local,req); break;
		case 'Dname':
			inputValid=isValid(inputstr,Dname,req); break;
		case 'MoDyYr':
			inputValid=isValid(inputstr,MoDyYr,req); break;
		case 'Address':
			inputValid=isValid(inputstr,Address,req); break;
		}
	
	//if invalid, turn invalidClr
	if(inputValid) the_input.style.backgroundColor=validClr;
	else the_input.style.backgroundColor=invalidClr;

	/* step through all <input>s in the form, 
	pick out all text and password types, 
	and test for invalidClr */
	for(var n=0; n<all_inputs.length; n++){
		if(all_inputs[n].type=="text" || all_inputs[n].type=="password")
			if(all_inputs[n].style.backgroundColor==invalidClr)
			formValid=false;
		}
	
	/* step through <input>s in the form again, pick out all submit types 
	and enable OR disable them */
	for(var n=0; n<all_inputs.length; n++){
		if(all_inputs[n].type=="submit")
			all_inputs[n].disabled=!formValid;
		}
	}
