//--------- On Page to validate

/*
function checkWholeForm(theForm) {
    var why = "";
    why += checkEmail(theForm.email);
    why += checkPhone(theForm.phone);
    why += checkPassword(theForm.password);
    why += checkUsername(theForm.username);
    why += isEmpty(theForm.notempty);
    why += isDifferent(theForm.different);
    why += checkDigits(obj, strError);
***************************
!!! Need to work on the Radio validation
    for (i=0, n=theForm.radios.length; i<n; i++) {
        if (theForm.radios[i].checked) {
            var checkvalue = theForm.radios[i].value;
            break;
        } 
    }
    why += checkRadio(checkvalue);
    why += checkDropdown(theForm.choose.selectedIndex);
***************************
    if (why != "") {
       alert(why);
       return false;
    }
    
    return true;
}
*/
//-----------------------------------------------------------------

//-----------------------------------------------------------------
// non-empty textbox - pass in object and error message

function isEmpty(obj, strError) {
	var error = "";
	  if (obj.value.length == 0) {
		 	error = strError + "\n";
			obj.style.backgroundColor="#FF0000";
			obj.style.color="#FFFFFF";
	  }
	  else {
		obj.style.backgroundColor="#FFFFFF";
    		obj.style.color="#000000";
	  }
	return error;	  
}

//-----------------------------------------------------------------
// email address - test for valid email address
function checkEmail (obj) {
	var error="";
	if ( obj.value != "") {
		obj.style.backgroundColor="#FFFFFF";
    		obj.style.color="#000000";
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(obj.value))) { 
			error = "- Must Be a valid email address.\n";
			obj.style.backgroundColor="#FF0000";
       			obj.style.color="#FFFFFF";
		}
		else {
			//test email for illegal characters
			var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
				if (obj.value.match(illegalChars)) {
					error = "- Email address contains illegal characters.\n";
					obj.style.backgroundColor="#FF0000";
	        			obj.style.color="#FFFFFF";
				}
		}
	}
	else {
		error = "- Email Address\n";
		obj.style.backgroundColor="#FF0000";
		obj.style.color="#FFFFFF";
	}
		
	return error;    
}

//-----------------------------------------------------------------
// phone number - strip out delimiters and check for 10 digits

function checkPhone (obj,len) {
	var error = "";
	if (obj.value != "") {
		obj.style.backgroundColor="#FFFFFF";
    		obj.style.color="#000000";
		var stripped = obj.value.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) {
			error = "- The phone number contains illegal characters.";
			obj.style.backgroundColor="#FF0000";
       			obj.style.color="#FFFFFF";
   		}
		if (!(stripped.length == len)) {
			error = "- The phone number is the wrong length. Make sure you have entered it properly.\n";
			obj.style.backgroundColor="#FF0000";
       			obj.style.color="#FFFFFF";
		} 
	}
	else {	
      		error = "- Phone Number\n";
		obj.style.backgroundColor="#FF0000";
       		obj.style.color="#FFFFFF";
	}
	return error;
}

//-----------------------------------------------------------------
// zip code - test for 5 digits
function checkZip(obj) {
	var error = "";
	var digits=/^\d{5}$/  //regular expression defining a 5 digit number
	if (obj.value != "") {
		obj.style.backgroundColor="#FFFFFF";
    		obj.style.color="#000000";
		if (obj.value.search(digits)) { 
			error = "- Zip code must be 5 digits.\n";
			obj.style.backgroundColor="#FF0000";
			obj.style.color="#FFFFFF";
		}	
	}
	else {	
		error = "- Zip Code\n";
		obj.style.backgroundColor="#FF0000";
		obj.style.color="#FFFFFF";
	}
	return error;
}

//-----------------------------------------------------------------
// digits test for only digits
function checkDigits(obj, strError) {
	var error = "";
	var digits=/^\d/  // regular expression only numbers
	if (obj.value != "") {
		obj.style.backgroundColor="#FFFFFF";
    		obj.style.color="#000000";
		if (obj.value.search(digits)) { 
			error = strError + " Only Numbers \n";
			obj.style.backgroundColor="#FF0000";
			obj.style.color="#FFFFFF";
		}	
	}
	else {	
		error = strError + "\n";
		obj.style.backgroundColor="#FF0000";
		obj.style.color="#FFFFFF";
	}
	return error;
}

//-----------------------------------------------------------------
// valid selector from dropdown list

function checkDropdown(obj) {
	var error = "";
    if (obj.value == 0) {
		error = "- Please Select choice from drop down.\n";
		obj.style.backgroundColor="#FF0000";
       		obj.style.color="#FFFFFF";
    }    
    else {
	obj.style.backgroundColor="#FFFFFF";
	obj.style.color="#000000";
    }
	return error;
}  

//-----------------------------------------------------------------
// exactly one radio button is chosen

function checkRadio(obj) {
var error = "";

    for (i=0, n=obj.length; i<n; i++) {
        if (obj[i].checked) {
            var checkvalue = obj[i].value;
            break;
        } 
    }
   if (!(checkvalue)) {
       error = "- Please select a radio button.\n";
	
    }
return error;
}

//-----------------------------------------------------------------
// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (obj) {
	var error = "";
	if (obj.value != "") {
		var illegalChars = /[\W_]/; // allow only letters and numbers
		if ((obj.value.length < 6) || (obj.value.length > 8)) {
		   error = "The password is the wrong length.\n";
		}
		else if (illegalChars.test(obj.value)) {
		  error = "The password contains illegal characters.\n";
		} 
		else if (!((obj.value.search(/(a-z)+/)) && (obj.value.search(/(A-Z)+/)) && (obj.value.search(/(0-9)+/)))) {
		   error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
		}
	}
	else {     
	   error = "You didn't enter a password.\n";
	}
	return error;    
}    

//-----------------------------------------------------------------
// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (obj) {
	var error = "";
	if (obj.value != "") {
		var illegalChars = /\W/; // allow letters, numbers, and underscores
		if ((obj.value.length < 4) || (obj.value.length > 10)) {
		   error = "The username is the wrong length.\n";
		}
		else if (illegalChars.test(obj.value)) {
		error = "The username contains illegal characters.\n";
		} 
	}
	else {
		error = "You didn't enter a username.\n";
	}    
	return error;
}       

//-----------------------------------------------------------------
// was textbox altered

function isDifferent(obj) {
var error = ""; 
  if (obj.value != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}







	



