/*********  Function Prototype *************************
function isEmpty(s);
function isWhitespace (s);
function isEmail (s);
function ForceEntry(objField, FieldName);
function ForceNumber(objField, FieldName);
function ForceAlpha(objField, FieldName);
function ForceMoney(objField, FieldName);
function RTrim(strTrim);
function isDateNumber(strNum,method);
function PromptErrorMsg(Field,strError);
function ForceDate(datefield,strField, datetime);
function IsPostCode(strZip);
function ForceLength(objField, nLength, strWarning);
function IsTime(strTime);
function IsWebAddress(objfield, fieldname);
function Forcevalidtypeentry(Field,FieldName, entrytype);
function Forcevalidentry(Field,FieldName, entrytype);
function isSelected(Fieldobj,FieldName);
function isChecked(Fieldobj,FieldName);
function isTelephone(objField, FieldName)

****************************************************/

var whitespace = " \t\n\r";
var formok = false;
var noofvalidation = 0;

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

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;
}

function isEmail (s)
{  
    var i = 1;
    var sLength = s.length;
	var numeric = new Array("1","2","3","4","5","6","7","8","9","0","~","`","!","@","#","$","%","^","&","*","(",")","_","=","+","[","{","}","]",";",":","<",">",".","/","?",",") //,"[","{","]","}","\","|",";",":","'","<",".",">","/","?")
	var k,kk;
    var value = s.charAt(sLength-1)
	for (var k = 0; k < 40; k++) {
		    if ((value.indexOf(numeric[k]) != "-1")){ 
			return false;                   
	        }	
		}
	if( s.indexOf(" ") > 0){
		for (var kk=s.indexOf(" ");kk<sLength ;kk++ )
		{
			if (s.charAt(kk)!=" ")
			{
				return false;
			}
		}
	}
		
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }
	
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function ForceEntry(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField)) {
		alert("You need to enter information for \"" + FieldName + "\"");
		objField.focus();
		objField.select();
		return false;
	}
	return true;
}
		
function ForceNumber(objField, FieldName)
{
	var strField = new String(objField.value);
	var i = 0;
	var subparts = strField.split(".");
	 
	if(subparts.length > 2){
		alert("\"" +FieldName +"\"" + " must be a valid \"NUMERIC\" entry.  Please do not use commas or dollar signs or any non-numeric symbols.");
		objField.select();
		objField.focus();
		return false;
	}

	for (i = 0; i < strField.length; i++)
		if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && strField.charAt(i) != '.') {
			alert("\"" +FieldName +"\"" + " must be a valid \"NUMERIC\" entry.  Please do not use commas or dollar signs or any non-numeric symbols.");
			objField.select();
			objField.focus();
			return false;
		}
	return true;
}

function ForceAlpha(objField, FieldName)
{
        var i
		var value = objField.value;
	var numeric = new Array("1","2","3","4","5","6","7","8","9","0","~","`","!","@","#","$","%","^","&","*","(",")","_","=","+","[","{","}","]",";",":","<",">",".","/","?",",") //,"[","{","]","}","\","|",";",":","'","<",".",">","/","?")
	for (var i = 0; i < 40; i++) {
		    if ((value.indexOf(numeric[i]) != "-1")){ 
			alert("Please don't write numeric values in "+FieldName+" field");
			return false;                   
	        }	
		}
		return true;
	}
 
function ForceMoney(objField, FieldName)
{
	var strField = new String(objField.value);
	
	if (isWhitespace(strField)) return true;

	var i = 0;

	for (i = 0; i < strField.length; i++)
		if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && (strField.charAt(i) != '.')) {
			alert("\"" + FieldName+"\"" + " must be a valid \"NUMERIC\" entry.  Please do not use commas or dollar signs or any non-numeric symbols.");
			objField.focus();
			return false;
		}

	return true;
}



function RTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var endpos = 0

	for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
		c = str.charAt(i);
		if (whitespace.indexOf(c) == -1)
			endpos = i;
	}

	return str.substring(0,endpos+1);
}

function isDateNumber(strNum,method)
{
	var str = new String(strNum);
	var i = 0;

	if (isNaN(parseInt(str)) || parseInt(str) < 0) return false;

	if (method == 2)
		if (parseInt(str) > 31)
			return false;
	if (method == 1)
		if (parseInt(str) > 12)
			return false;

	for (i = 0; i < str.length; i++)
		if (str.charAt(i) < '0' || str.charAt(i) > '9')
			return false;
	return true;
}

function PromptErrorMsg(Field,strError)
{
	alert("You have entered an invalid \"DATE\".  Please make sure your date format is in \"DD/MM/YYYY\" format.");
	Field.select();
	Field.focus();
}

function ForceDate(datefield,strField, datetime)
{
	var str = new String(datefield.value);
	var datestr, datetimearray;
	if(datetime = "Date_Time"){
		datetimearray = str.split(" ");
		str = datetimearray[0];
	}
		
	var i = 0, count = str.length, j = 0;
	while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
		i++;

	if (i == count || i > 2) {
		PromptErrorMsg(datefield,strField);
		return false;
	}

	var addOne = false;
	if (i == 2) addOne = true;

	if (!isDateNumber(str.substring(0,i),2)) {
		PromptErrorMsg(datefield,strField);
		return false;
	}

	j = i+1;
	i = 0;

	while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)
		i++;

	if (i+j == count || i > 2) {
		PromptErrorMsg(datefield,strField);
		return false;
	}

	if (!isDateNumber(str.substring(j,i+j),1)) {
		PromptErrorMsg(datefield,strField);
		return false;
	}

	j = i+3;
	i = 0;

	if (addOne) j++;

	while (i+j < count)
		i++;


	if (i != 2 && i != 4) {
		PromptErrorMsg(datefield,strField);
		return false;
	}

	if (!isDateNumber(str.substring(j,i+j),3)) {
		PromptErrorMsg(datefield,strField);
		return false;
	}

	return true;
}


function IsPostCode(strZip){
	var s = new String(strZip);

	if (s.length > 4)
		return false;

	//if(s.charAt(0) < '2' || s.charAt(0) > '8')
	//	return false;
		
	for (var i=1; i < s.length; i++)
		if (s.charAt(i) < '0' || s.charAt(s) > '9')
			return false;
				
	return true;
}


function ForceLength(objField, nLength, strWarning){
	var strField = new String(objField.value);

	if (strField.length > nLength) {
		alert(strWarning);
		return false;
	} else
		return true;
}

/****************************************************************/

// PURPOSE:  Check to see if the string passed in is a valid time.
//	A valid time is defined as a string which is postfixed with either
//  "PM" or "AM".  Next it checks to see if there is a colon in the
//  string.  If there is, it makes sure that at least one digit preceeds
//  it and two proceed it.

function IsTime(strTime){
	var strTestTime1 = new String(strTime);
	strTestTime = strTestTime1.toUpperCase();

	var bolTime = false;

	if (strTestTime.indexOf("PM",1) != -1 || strTestTime.indexOf("AM",1))
		bolTime = true;
					
	if (bolTime && strTestTime.indexOf(":",0) == 0)
		bolTime = false;
			
	var nColonPlace = strTestTime.indexOf(":",1);
	if (bolTime && ((parseInt(nColonPlace) + 5) < (strTestTime.length - 1) || (parseInt(nColonPlace) + 4) > (strTestTime.length - 1)))
		bolTime = false;
		
	return bolTime;
}
	
function IsWebAddress(objfield, fieldname){
	var webadd = new String(objfield.value);
	var substrs = webadd.split(".");
	
	if((webadd.search("www.") != -1 || webadd.search("WWW.") != -1) && substrs.length >= 3){
		return true;
	}else{
		return false;
	}
}
		
function Forcevalidtypeentry(Field,FieldName, entrytype)
{
	alert("\"" + FieldName +"\"" +" is a REQUIRED field and must be a valid \"" + entrytype + "\" entry. ");
	Field.select();
	Field.focus();
	return true;
}

function Forcevalidentry(Field,FieldName, entrytype)
{
	alert("\"" + FieldName +"\"" +"  must have a valid \"" + entrytype + "\" entry. ");
	Field.select();
	Field.focus();
	return true;
}

function isSelected(Fieldobj,FieldName){
	var selopt =Fieldobj.options[Fieldobj.selectedIndex].value ;
      if(selopt == "0" || selopt == "")
	  {
			alert("Please select "+FieldName);
			Fieldobj.focus();
			return false;
			}
		else{
		return true;
		}
}

function isChecked(Fieldobj,FieldName,fieldnum){
	var i,flag;
	flag=0;
	for (i=0;i<fieldnum ; i++ )
	{
			if(Fieldobj[i].checked){
				flag=1;
			}
	}
	if (flag==0){
			//alert("Please check "+FieldName+"!");
			//Fieldobj.focus();
			return false;
	}
	else{
	return true;
	}
}

function isTelephone(objField, FieldName)
{
	var strField = new String(objField.value);
	var i = 0;
	for (i = 0; i < strField.length; i++)
		if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && strField.charAt(i) != '_'  && strField.charAt(i) != '(' && strField.charAt(i) != ')'  && strField.charAt(i) != ' '  && strField.charAt(i) != ';') {
			return false;
		}
	return true;
}

function isDate(dateStr) 
{
	
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
		
	if (matchArray == null) {
	return false;
	}

	month = matchArray[3]; // p@rse date into variables
	day = matchArray[1];
	year = matchArray[5];

	if (month < 1 || month > 12) { // check month range
	alert("Month must be between 1 and 12.");
	return false;
	}

	if (day < 1 || day > 31) {
	alert("Day must be between 1 and 31.");
	return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	alert("Month "+ month +" doesn`t have 31 days!")
	return false;
	}

	if (month == 2) { // check for february 29th
	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	if (day > 29 || (day==29 && !isleap)) {
	alert("February " + year + " doesn`t have " + day + " days!");
	return false;
	}
	}
	return true; // date is valid
}