/***************************************************************************************************************
 * input validity script 
 * Alan Rollins - August 15, 2002
 *
 * to implement this script as a validity checker ...
 *
 * of course - include the script in the <head> section
 *
 * in the body tag you might want to set focus to the top field 'onLoad=document.form.fieldname.focus()'
 *
 * in the <form> tag use onSubmit="return checkInput();"
 *
 * in the <input> tags use onChange="isCHECK(document.test.fieldname, CHECK1, 'fieldname', CHECK2);"
 *
 * where CHECK = Text, Email, Time, Date, URL, Blank
 * where CHECK1 = true or false = perform check while inputting? (true will warn you instantly of bad input)
 * where CHECK2 = true or false = can the field be blank? (true means the field may NOT be left blank)
 *
 * also in <input> tags use onfocus="moveAllowed();"
 *
 * in the <submit> tag use onClick="DisableEdits(false);" and onfocus="moveAllowed();"
 *
 * after the form add the following <script> section:
 *
 *     function checkInput()
 *         {
 *          BigJsString="";
 *          ObjToSetFocus = null; 
 *          if (ExecuteEdits) {
 *            isText(document.test.username, false, "username", true);
 *            isInteger(document.test.integer, false, "integer", true);
 *            isURL(document.test.number, false, "number", true);
 *            isEmail(document.test.email, false, "email", true);
 *            isBlank(document.test.notblank, false, "notblank");
 *
 *            if (BigJsString != "") {
 *	       alert(BigJsString);
 *	       ObjToSetFocus.focus();
 *	       return false;
 *            } else {
 *	       return true;
 *            }
 *          } // end if(executeEdits)
 *            return true; 
 *         }
 *
 **************************************************************************************************************/

var BigJsString = "";
var ObjToSetFocus;
var ExecuteEdits = true;
var newWin = null;
var dontmove = false;
var blankstring = " may not be left blank.\n";

function moveAllowed() {
        if (dontmove) {
	  oldobj.select();
          oldobj.focus();
        }
}

function DisableEdits(value)
{
	if (value == true)
		ExecuteEdits = false;
	else
		ExecuteEdits = true;
}

function windowOpen(win)
{
	windowClose();
	newWin = window.open(win, "myWin","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=550,height=180,left=15,top=15");
}

function windowClose()
{
	if (!(newWin == null))
	{
		if (typeof newWin == "object") 
		{
			newWin.close();
			if (typeof newWin == "object") 
			{
				newWin.close();
			}
		}
	}
	return true;
}

function isBlank(obj, Instant, FieldName)
{
	var i;
	var answer = true;
        var blank = true;
	var s = obj.value;
	dontmove = false;
	errorString = "";
	sendalert = false;

        if (Instant == true) {
           if (blank && isEmpty(s)) {
             errorString = errorString + FieldName + blankstring;
	     sendalert = true;
	   }
           if (sendalert) {
		alert (errorString);
		// obj.focus();
		dontmove = true;
		oldobj = obj;
	   }
	   } else {
           if (blank && isEmpty(s)) {
                errorString = errorString + FieldName + blankstring;
		sendalert = true;
	   } 
           if (sendalert) {
		BigJsString = BigJsString + errorString;
		if (ObjToSetFocus == null)
		   ObjToSetFocus = obj;
	   }
	}
	return answer;
}

function isText(obj, Instant, FieldName, blank)
{
	var i;
	var answer = false;
	var re = /\W/;
	var s = obj.value;
	dontmove = false;
	errorString = "";
	sendalert = false;

             if (isEmpty(s)) {
               if (blank) {
                 errorString = errorString + FieldName + blankstring;
                 sendalert = true;
               }
             } else {
               if (re.test(s)) {
                 errorString = errorString + FieldName + " may only contain letters and/or numbers.\n";
                 sendalert = true;
               }
             }
             if (sendalert) {
	       answer = true;
               if (Instant == true) {
		 alert (errorString);
		 // obj.focus();
		 dontmove = true;
		 oldobj = obj;
	       } else {
		 BigJsString = BigJsString + errorString;
		 if (ObjToSetFocus == null) {
		   ObjToSetFocus = obj;
                 }
	       }
 	     }
	return answer;
}

function isNumber(obj, Instant, FieldName, blank)
{
	var i;
	var answer = false;
	var re = /[^0-9\.]/;
	var s = obj.value;
	dontmove = false;
	errorString = "";
	sendalert = false;

             if (isEmpty(s)) {
               if (blank) {
                 errorString = errorString + FieldName + blankstring;
                 sendalert = true;
               }
             } else {
               if (re.test(s)) {
                 errorString = errorString + FieldName + " must be a number.\n";
                 sendalert = true;
               }
             }
             if (sendalert) {
	       answer = true;
               if (Instant == true) {
		 alert (errorString);
		 // obj.focus();
		 dontmove = true;
		 oldobj = obj;
	       } else {
		 BigJsString = BigJsString + errorString;
		 if (ObjToSetFocus == null) {
		   ObjToSetFocus = obj;
                 }
	       }
 	     }
	return answer;
}

function isInteger(obj, Instant, FieldName, blank)
{
	var i;
	var answer = false;
	var re = /[^0-9]/;
	var s = obj.value;
	dontmove = false;
	errorString = "";
	sendalert = false;

             if (isEmpty(s)) {
               if (blank) {
                 errorString = errorString + FieldName + blankstring;
                 sendalert = true;
               }
             } else {
               if (re.test(s)) {
                 errorString = errorString + FieldName + " must be an integer.\n";
                 sendalert = true;
               }
             }
             if (sendalert) {
	       answer = true;
               if (Instant == true) {
		 alert (errorString);
		 // obj.focus();
		 dontmove = true;
		 oldobj = obj;
	       } else {
		 BigJsString = BigJsString + errorString;
		 if (ObjToSetFocus == null) {
		   ObjToSetFocus = obj;
                 }
	       }
 	     }
	return answer;
}

function isURL(obj, Instant, FieldName, blank)
{
	var i;
	var answer = false;
	var re = /[^\w\@\~\-\+\=\&\#\!\%\.\:\/\?]/;
	var s = obj.value;
	// if s has something in it look for http and put it in if it isn't there already
           if (!(isEmpty(s))) {
		var re2 = /^http:\/\//;
		if (!(re2.test(s))) {
		   s = "http://" + s;
		   obj.value = s;
		}
	   }
	dontmove = false;
	errorString = "";
	sendalert = false;

             if (isEmpty(s)) {
               if (blank) {
                 errorString = errorString + FieldName + blankstring;
                 sendalert = true;
               }
             } else {
               if (re.test(s)) {
                 errorString = errorString + FieldName + " must be in the form http://website.\n";
                 sendalert = true;
               }
             }
             if (sendalert) {
	       answer = true;
               if (Instant == true) {
		 alert (errorString);
		 // obj.focus();
		 dontmove = true;
		 oldobj = obj;
	       } else {
		 BigJsString = BigJsString + errorString;
		 if (ObjToSetFocus == null) {
		   ObjToSetFocus = obj;
                 }
	       }
 	     }
	return answer;
}

function isEmail(obj, Instant, FieldName, blank)
{
	var i;
	var answer = false;
        var re = /^[a-zA-Z0-9\-\.\_]+\@{1}[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,3}$)|(\.\d{1,3}$)/;
	var s = obj.value;
	dontmove = false;
	errorString = "";
	sendalert = false;

             if (isEmpty(s)) {
               if (blank) {
                 errorString = errorString + FieldName + blankstring;
                 sendalert = true;
               }
             } else {
               if (!(re.test(s))) {
                 errorString = errorString + FieldName + " must be in the form <user>@<domain>.<zone>.\n";
                 sendalert = true;
               }
             }
             if (sendalert) {
	       answer = true;
               if (Instant == true) {
		 alert (errorString);
		 // obj.focus();
		 dontmove = true;
		 oldobj = obj;
	       } else {
		 BigJsString = BigJsString + errorString;
		 if (ObjToSetFocus == null) {
		   ObjToSetFocus = obj;
                 }
	       }
 	     }
	return answer;
}

function isDate(obj, Instant, FieldName, blank)
{
	var i;
	var answer = false;
        var re = /^\d{2}\/\d{2}\/\d{4}$/;
	var s = obj.value;
	dontmove = false;
	errorString = "";
	sendalert = false;

             if (isEmpty(s)) {
               if (blank) {
                 errorString = errorString + FieldName + blankstring;
                 sendalert = true;
               }
             } else {
               if (!(re.test(s))) {
                 errorString = errorString + FieldName + " must be in the form mm/dd/yyyy.\n";
                 sendalert = true;
               }
             }
             if (sendalert) {
	       answer = true;
               if (Instant == true) {
		 alert (errorString);
		 // obj.focus();
		 dontmove = true;
		 oldobj = obj;
	       } else {
		 BigJsString = BigJsString + errorString;
		 if (ObjToSetFocus == null) {
		   ObjToSetFocus = obj;
                 }
	       }
 	     }
	return answer;
}

function isPhone(obj, Instant, FieldName, blank)
{
	var i;
	var answer = false;
        var re = /^\d{3}-\d{3}-\d{4}/;
	var s = obj.value;
	dontmove = false;
	errorString = "";
	sendalert = false;

             if (isEmpty(s)) {
               if (blank) {
                 errorString = errorString + FieldName + blankstring;
                 sendalert = true;
               }
             } else {
               if (!(re.test(s))) {
                 errorString = errorString + FieldName + " must be in the form ###-###-####.\n";
                 sendalert = true;
               }
             }
             if (sendalert) {
	       answer = true;
               if (Instant == true) {
		 alert (errorString);
		 // obj.focus();
		 dontmove = true;
		 oldobj = obj;
	       } else {
		 BigJsString = BigJsString + errorString;
		 if (ObjToSetFocus == null) {
		   ObjToSetFocus = obj;
                 }
	       }
 	     }
	return answer;
}

function isTime(obj, Instant, FieldName, blank)
{
	var i;
	var answer = false;
        var re = /^\d{2}\:\d{2}\:(AM)|(PM)$/i;
	var s = obj.value;
	dontmove = false;
	errorString = "";
	sendalert = false;

             if (isEmpty(s)) {
               if (blank) {
                 errorString = errorString + FieldName + blankstring;
                 sendalert = true;
               }
             } else {
               if (!(re.test(s))) {
                 errorString = errorString + FieldName + " must be in the form hh:mm:AM(PM).\n";
                 sendalert = true;
               }
             }
             if (sendalert) {
	       answer = true;
               if (Instant == true) {
		 alert (errorString);
		 // obj.focus();
		 dontmove = true;
		 oldobj = obj;
	       } else {
		 BigJsString = BigJsString + errorString;
		 if (ObjToSetFocus == null) {
		   ObjToSetFocus = obj;
                 }
	       }
 	     }
	return answer;
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isEmpty(s)
{	return ((s == null) || (s.length == 0))
}

function isLengthOk(obj, len, Instant, FieldName)
{
	var answer = true;
	dontmove = false;


	if (obj.value.length != len)
	{
		answer = false;
	}
	if (!answer)
	{
		if (Instant == true) 
		{
			alert (FieldName + " must be "+len+" digits.\n");
			// obj.focus();
			dontmove = true;
			oldobj = obj;
		}
		else
		{
			BigJsString = BigJsString + FieldName + " must be "+len+" digits.\n";
			if (ObjToSetFocus == null)
				ObjToSetFocus = obj;
			//alert ("debug = " + BigJsString);
		}
	}
	return answer;				
}


function isLenText(obj, len, Instant, FieldName)
{
	var i;
	var answer = true;
	var s = obj.value;
	dontmove = false;


	answer = !(isEmpty(s));
	if (answer)
	{
		if (len != 0)
		{
			answer = isLengthOk(obj, len, Instant, FieldName);
		}
	}

	return answer;
}

function isSSN(ssn1, ssn2, ssn3)
{
	if (!isText(ssn1, 3) || !isText(ssn2, 2) || !isText(ssn3,4))
		return false;

	return true;
}


function isChecked(obj, Instant, FieldName)
{
	var answer = false;
	dontmove = false;

	
	//obj.value == null is false if the radio button has only one selection
	//					it is true otherwise. 
	//This would be equilivent to the check (obj.length = 0) but obj.length is 
	//		undefined for radio buttons with only 1 element
	if (obj.value != null)
	{
		//There is only one radio button option.
		if (obj.checked == true)
			answer = true;
	}
	else
	{
		if (obj.length > 0)
		{
			for (j=0;j < obj.length;j++)  
			{
				if (obj[j].checked == true)
				{
					answer = true;
				}

			}
		}
	}

	if (!answer)
	{
		if (Instant == true) 
		{
			alert (FieldName + " must be selected.\n");
				if (obj.value != null) {
					// obj.focus();
					dontmove = true;
					oldobj = obj;
                                }
				else {
					obj[0].focus();
					dontmove = true;
					oldobj = obj[0];
                                }
		}
		else
		{
			BigJsString = BigJsString + FieldName + " must be selected.\n";
			//must set specific radio button to set focus - default is the 1st element
			if (ObjToSetFocus == null)
			{
				if (obj.value != null)
					ObjToSetFocus = obj;
				else
					ObjToSetFocus = obj[0];
			}
		}
	}			
	
	return answer;
}

