// Error indicator
var has_error;
var error_count;
var errormsg;

// whitespace characters
var whitespace = " \t\n\r";


// charInString (CHARACTER c, STRING s)
//
// Returns true if single character c (actually a string)
// is contained within string s.

function charInString (c, s) {   
	for (i = 0; i < s.length; i++) {
	   if (s.charAt(i) == c) return true;
    	}
    return false
	}

function reset_error(HeaderMsg) {
	has_error=false;
	error_count = 0;
 	errormsg=HeaderMsg + '\n_____________________________\n\n';
}


// 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.

function isWhitespace (s) {
   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search 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.

    for (i = 0; i < s.length; i++) {   
    	// Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    	}

    // All characters are whitespace.
    return true;
}


// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripInitialWhitespace (s) {   
	var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    return s.substring (i, s.length);
}

function Trim(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );
  return sInString.replace( /\s+$/g, "" );
}


<!-----------------------------------DATE VALIDATION------------------------------------------->


// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=2000;
var maxYear=2100;
var tmCh = ":"

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function isNumber(s, minval, maxval){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    if (s < minval | s > maxval)
		return false;
    
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

//
// only takes dd/mm/yyyy or yyyy/mm/dd
//
function isDate(dtStr, format, seperator, s){
	var daysInMonth = DaysArray(12)
		
	if (format=="dd-mm-yyyy") {
		var pos1=dtStr.indexOf(seperator)
		var pos2=dtStr.indexOf(seperator,pos1+1)
		
		var strDay=dtStr.substring(0, pos1)
		var strMonth=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1)			
		}
	else {
		var pos1=dtStr.indexOf(seperator)
		var pos2=dtStr.indexOf(seperator,pos1+1)
				
		var strYear=dtStr.substring(0,pos1)
		var strMonth=dtStr.substring(pos1+1,pos2)
		var strDay=dtStr.substring(pos2+1)
		}
	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) 
		strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) 
		strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	year=parseInt(strYr)
	month=parseInt(strMonth)
	day=parseInt(strDay)
		
	if (pos1==-1 || pos2==-1){
		if (error_count <= 10)
			errormsg+= s + '- The date format should be : '+format+'\n';
   		has_error=true;
		error_count++;	
		return false
		}
	if (month<1 || month>12){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid month\n';
   		has_error=true;	
		error_count++;
		return false
		}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid day\n';
   		has_error=true;
   		return false
		}
		
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid 4 digit year between ' + minYear + ' and ' + maxYear +'\n';
   		has_error=true;
		error_count++;
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, "-"))==false){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid date\n';
   		has_error=true;		
		error_count++;
		return false
	}
return true
}

function isDateIgnoreLimit(dtStr, format, seperator, s){
	var daysInMonth = DaysArray(12)
		
	if (format=="dd-mm-yyyy") {
		var pos1=dtStr.indexOf(seperator)
		var pos2=dtStr.indexOf(seperator,pos1+1)
		
		var strDay=dtStr.substring(0, pos1)
		var strMonth=dtStr.substring(pos1+1,pos2)
		var strYear=dtStr.substring(pos2+1)			
		}
	else {
		var pos1=dtStr.indexOf(seperator)
		var pos2=dtStr.indexOf(seperator,pos1+1)
				
		var strYear=dtStr.substring(0,pos1)
		var strMonth=dtStr.substring(pos1+1,pos2)
		var strDay=dtStr.substring(pos2+1)
		}
	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) 
		strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) 
		strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	year=parseInt(strYr)
	month=parseInt(strMonth)
	day=parseInt(strDay)
		
	if (pos1==-1 || pos2==-1){
		if (error_count <= 10)
			errormsg+= s + '- The date format should be : '+format+'\n';
   		has_error=true;
		error_count++;	
		return false
		}
	if (month<1 || month>12){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid month\n';
   		has_error=true;	
		error_count++;
		return false
		}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid day\n';
   		has_error=true;
   		return false
		}
		
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, "-"))==false){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid date\n';
   		has_error=true;		
		error_count++;
		return false
	}
return true
}

function isTime(dtStr, s){
	var strHour=dtStr.substring(0,2)
	var strMinute=dtStr.substring(3,5)

	hour=parseInt(strHour)
	minute=parseInt(strMinute)
	
	if (dtStr.charAt(2)!=":") {
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a time (hh:mm) \n';
   		has_error=true;
		error_count++;
   		return false
		}	
	
	if (hour<0 || hour>23){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid hour\n';
   		has_error=true;
		error_count++;
   		return false
		}
		
	if (minute<0 || minute>59){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid minute\n';
   		has_error=true;
		error_count++;
   		return false
		}
	
	return true
}

function isDateTime(dtStr, s){
	var daysInMonth = DaysArray(12)
	
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var pos3=dtStr.indexOf(" ",pos2+1)
	var pos4=dtStr.indexOf(tmCh,pos3+1)

	var strYear=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(pos2+1, pos3)

	var strHour=dtStr.substring(pos3+1,pos4)
	var strMinute=dtStr.substring(pos4+1)
	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) 
		strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	year=parseInt(strYr)
	month=parseInt(strMonth)
	day=parseInt(strDay)
	hour=parseInt(strHour)
	minute=parseInt(strMinute)
	
	if (pos1==-1 || pos2==-1){
		if (error_count <= 10)
			errormsg+= s + '- The date format should be : yyyy-mm-dd hh:mm\n';
   		has_error=true;	
		error_count++;
		return false
		}
	if (month<1 || month>12){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid month\n';
   		has_error=true;	
		error_count++;
		return false
		}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid day\n';
   		has_error=true;
		error_count++;
   		return false
		}
	if (hour<0 || hour>23){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid hour\n';
   		has_error=true;
		error_count++;
   		return false
		}
	if (minute<0 || minute>59){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid minute\n';
   		has_error=true;
		error_count++;
   		return false
		}
	
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid 4 digit year between ' + minYear + ' and ' + maxYear +'\n';
   		has_error=true;		
		error_count++;
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, "-: "))==false){
		if (error_count <= 10)
			errormsg+= s + ' - Please enter a valid date\n';
   		has_error=true;		
		error_count++;
		return false
	}
return true
}


function emailCheck (emailStr, s) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
    if (error_count <= 10) 
    	errormsg+= s + " - Email address seems incorrect (check @ and .'s)\n";
   	has_error=true;	
	error_count++;
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    if (error_count <= 10)
    	errormsg+= s + " - The username doesn't seem to be valid.\n";
   	has_error=true;	
	error_count++;
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	    	if (error_count <= 10)
				errormsg+= s + " - Destination IP address is invalid!\n";
   			has_error=true;	
			error_count++;
			return false
			}
		}
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	if (error_count <= 10)
		errormsg+= s + " - The domain name doesn't seem to be valid.\n";
   	has_error=true;	
	error_count++;
	return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   if (error_count <= 10)
   		errormsg+= s + ' - The address must end in a three-letter domain, or two letter country.\n';
   has_error=true;	
   error_count++;
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
	if (error_count <= 10)
   		errormsg+= s + ' - This address is missing a hostname!\n';
   has_error=true;	
   error_count++;
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

function PasswordCheck (szPassword, s) {
	if (szPassword.length < 8) {
		if (error_count <= 10)
			errormsg+= s + '\n';
   		has_error=true;	
		error_count++;	
		return false
		}
	else
		return true
	}



<!---------------------------------------------VALIDATE FIELD---------------------------------------->

function validate_field(theField, Type, sErrorMessage) {
  var old_error_count = error_count;
  
  if (Type != "DROPDOWN") {
	if(isWhitespace(theField.value))
  		{
  		if (error_count <= 10) 
   			errormsg+= sErrorMessage + '\n';
   		has_error=true;
		error_count++;
   		//theField.focus();
  		}
  	}  
  if (Type == "PASSWORD") {
	theField.value = Trim(theField.value);
    PasswordCheck (theField.value, sErrorMessage )
	}
  if (Type == "DATETIME") {
	theField.value = Trim(theField.value);
    isDateTime(theField.value, sErrorMessage)
	}
  else if (Type == "DATE") {
	theField.value = Trim(theField.value);
	var oo = theField.getAttribute("IgnoreLimit");
	if (oo && oo == "true") {
	    isDateIgnoreLimit(theField.value, "dd-mm-yyyy", "-", sErrorMessage)
		}
	else {
	    isDate(theField.value, "dd-mm-yyyy", "-", sErrorMessage)
		}
	}	
  else if (Type == "EMAIL") {
	theField.value = Trim(theField.value);
    emailCheck (theField.value, sErrorMessage )
	}
  else if (Type == "DROPDOWN") {
	if(theField.selectedIndex == 0){
        if (error_count <= 10) 
        	errormsg+= sErrorMessage + '\n';
   		has_error=true;	
		error_count++;	
		} 	
	}   	
  else if (Type == "TIME") {
	theField.value = Trim(theField.value);
	isTime(theField.value, sErrorMessage)
	}		
  else if (Type == "INTEGER") {
    if(isInteger(theField.value)==false){
		if (error_count <= 10) 
			errormsg+= sErrorMessage + '\n';
   		has_error=true;	
		error_count++;
		}	
	}
  else if(Type == "MINIMUMSTAY") {
  	if(theField.value != "" || theField.value != null){
    if(theField.value==0){
    	if (error_count <= 10) 
        	errormsg+= 'Minumum requirement for ' + sErrorMessage + ' is 7 days. \n';
   		has_error=true;	
		error_count++;
        }
    	}
    }
  else 
  	{
  	theField.value = Trim(theField.value);
  	if(theField.length <= 0)
    {
      if (error_count <= 10) 
        errormsg+= sErrorMessage + '\n';
      has_error=true;	
      error_count++;
		}	
  	}
  	if (error_count > old_error_count)
  		return(false);
  	else
  		return(true);
  	}
 
 	function textCounter(field, length) {
 		if (length==0)
 			return;
 		
 		if (field.value.length > length)
			{
			field.value = field.value.substring(0, length);
			}
		field.document.getElementsByName(field.name + '_Len')[0].value = length - field.value.length;
		}
	
	function setCounter(FieldName, length) {
		textCounter(document.getElementsByName(FieldName)[0], length);
		}	
		
		
		
function ValidateAllFields(HeaderMgs) 
	{
	reset_error(HeaderMgs);
	for(i=0;i<document.forms.length;++i) 
	{
	  	for(j=0;j<document.forms[i].elements.length;++j) 
	  	{
        var MandatoryValue;
        if(document.forms[i].elements[j].getAttribute("Mandatory") != null)
          MandatoryValue = document.forms[i].elements[j].getAttribute("Mandatory");
        else
          MandatoryValue = document.forms[i].elements[j].getAttribute("mandatory");
          
	  		if (MandatoryValue != null)
	  		{
	  			if(MandatoryValue.length > 0)
	  			{
	  				document.forms[i].elements[j].style.backgroundColor = "#FFFFFF";
	  					//document.forms[i].elements[j].style = document.forms[i].elements[j].style.replace("validateFail", "");
              var ErrorNameValue;
              if(document.forms[i].elements[j].getAttribute("ErrorName") != null)
                ErrorNameValue = document.forms[i].elements[j].getAttribute("ErrorName");
              else
                ErrorNameValue = document.forms[i].elements[j].getAttribute("errorname");                
                
	  				if(validate_field(document.forms[i].elements[j], MandatoryValue, ErrorNameValue) == false)
	  				{
	  					// Set the background style
	  					// ------------------------
	  					document.forms[i].elements[j].style.backgroundColor = "#E5ABAB";
	  				}
	  			}
				}
	  	}
	  }
 	

 	if(!has_error) {
 		return (true);
 		}
 	else {
 		alert(errormsg);
 		return(false);
 		} 	
	}
		
		
	
		
		
		