
var whitespace = " \t\n\r";

function showHomePage() {
	document.location.href = "index.php";
}

function getDocument(doc) {
	document.location.href = doc;
}

function SetFocus(TargetFormName) {
 	var target = 0;
  	if (TargetFormName != "") {
    	for (i=0; i<document.forms.length; i++) {
      		if (document.forms[i].name == TargetFormName) {
        		target = i;
        		break;
      		}
    	}
  	}
	
	if (document.forms.length > 0) {
  		var TargetForm = document.forms[target];
	}
	else {
		// There are no forms displayed on the page - trying to set focus would result in error
		return;
	}
    
  	for (i=0; i<TargetForm.length; i++) {
    	if ( (TargetForm.elements[i].type != "image") && (TargetForm.elements[i].type != "hidden") && (TargetForm.elements[i].type != "reset") && (TargetForm.elements[i].type != "submit") ) {
      		TargetForm.elements[i].focus();

      		if ( (TargetForm.elements[i].type == "text") || (TargetForm.elements[i].type == "password") ) {
        		TargetForm.elements[i].select();
      		}
      		break;
    	}
	}
}

//
// General function for validating form data before submision
//
function validateMandatory(theForm) {
 	for (i=0; i<theForm.elements.length; i++) {
    	el=theForm.elements[i];
    	if (el.className.match(/Integer/) && el.type=="text") {
      		if (!checkInteger(el,el.title)) {
         		return false; 
	  		} 
		}
    	if (el.className.match(/Number/) && el.type=="text") {
      		if (!checkNumber(el)) {
         		return false;
	  		} 
		}
    	//if (el.title.match(/Date/) && el.type=="text") {
      	//	if (!checkDate(el,el.title)) {
        // 		return false; 
	  	//	} 
		//}
    	//if (el.title.match(/Time/) && el.type=="text") {
      	//	if (!checkTime(el,el.title)) {
        //		return false; 
	  	//	} 
		//}
    	if (el.className.match(/Mandatory/)) {
      		if (!checkString(el,el.title)) {
         		return false;
	  		} 
		}
  	}
  	return true;
}

function checkInteger (theField, s) {
 	if ((theField.value.match(/\./g)) || (theField.value.match(/\+/g)) ) {
     	alert( s + " Must be a Integer");
     	theField.focus();
     	return false; 
 	}
 	if (isFinite(theField.value)) {
   		if(theField.value == "") {
     		theField.value="0";
   		}
   		if(isFinite(theField.max)) {
      		if (parseInt(theField.value)>parseInt(theField.max)) {
         		alert( theField.title + " Must be Less Than or = " + theField.max);
         		theField.focus();
         		return false; 
	  		} 
   		}
   		if(isFinite(theField.min)) {
      		if (parseInt(theField.value)<parseInt(theField.min)) {
         		alert( theField.title + " Must be Greater Than or = " + theField.min);
         		theField.focus();
         		return false; 
	  		} 
   		}
   		return true;
 	}
 	else {
   		alert( s + " Must be Numeric");
   		theField.focus();
   		return false; 
 	}
}

function checkNumber(theField) {
	if (isWhitespace(theField.value)) { 
 		return true;
	}

 	if (theField.value.match(/\+/g)) {
     	alert( theField.title + " Must be a Numeric");
     	theField.focus();
     	return false; 
	}

 	if (isFinite(theField.value)) {
   		if(isFinite(theField.max)) {
      		if (parseFloat(theField.value)>parseFloat(theField.max)) {
         		alert( theField.title + " Must be Less Than or = " + theField.max);
         		theField.focus();
         		return false; 
			} 
		}
   		if(isFinite(theField.min)) {
      		if (parseFloat(theField.value)<parseFloat(theField.min)) {
         		alert( theField.title + " Must be Greater Than or = " + theField.min);
         		theField.focus();
         		return false; 
			} 
		}
   		return true;
	}
 	else {
   		alert( theField.title + " Must be Numeric");
   		theField.focus();
   		return false;
	}
}

//======================================================================
// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
//======================================================================
function checkString (theField, s, emptyOK) {
	if (checkString.arguments.length == 2) 
		emptyOK = false;
 	if ((emptyOK == true) && (isEmpty(theField.value))) 
		return true;
 	if (isWhitespace(theField.value))
   		return warnEmpty (theField, s);
 	else 
		return true;
}

//------------------------------------------------------------
// Check whether string s is empty.
//------------------------------------------------------------
function isEmpty(s) {
 	return ((s == null) || (s.length == 0))
}

//------------------------------------------------------------
// Returns true if string s is empty or
// whitespace characters only.
// Searches through string's characters one by one
// until we find a non-whitespace character.
// When we do, return false; if we don't, return true.
//------------------------------------------------------------
function isWhitespace (s) {
 	var i;
 	if (isEmpty(s)) 
		return true;
 	for (i = 0; i < s.length; i++) {
   		var c = s.charAt(i);
   		if (whitespace.indexOf(c) == -1) 
			return false;
 	}
	return true; // All characters are whitespace.
}
//------------------------------------------------------------
// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.
//------------------------------------------------------------
function warnEmpty (theField, s) {
 	alert( s + " is a required field.\nPlease enter it now.");
 	theField.focus();
 	return false;
}




