// Other bits of Code for Date, Integer and Number Validation
//	if (checkDates(document.frmMain.txtdteTest,'Test Date is not a valid Date - Please enter'))
//	     {   return;
//		 }
//	if (checknumber(document.frmMain.txtdblBalancingRate.value) == false)
//		 {  alert('Balancing Rate is not a valid Number - Please enter');
//			return;
//		 }

function emailCheck (emailStr) {
/* 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. */
	alert("La dirección de Email es incorrecta (comprueba @ y .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("El nombre de usuario es incorrecto")
    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) {
	        alert("La dirección de IP del destino es incorrecta.")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("El dominio no es válido.")
    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.
   alert("La dirección debe terminar en las tres letras del dominio, o dos letras del país correspondiente.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="La dirección no posee valida extensión de dominio ej. 'dominio.com'!"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

function checknumber(object_value)
    {
    //Returns true if value is a number
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
    }

function checkdate(object_value)
    {
    //Returns true if value is a date format or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a date in the mm/dd/yyyy format
	isplit = object_value.indexOf('/');

	if (isplit == -1 || isplit == object_value.length)
		return false;

    sDay = object_value.substring(0, isplit);

	if (sDay.length == 0)
        return false;

	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length)
		return false;

	sMonth = object_value.substring((sDay.length + 1), isplit);

	if (sMonth.length == 0)
        return false;

	sYear = object_value.substring(isplit + 1);

	if (!checkinteger(sMonth)) //check month
		return false;
	else
	if (!checkrange(sMonth, 1, 12)) //check month
		return false;
	else
	if (!checkinteger(sYear)) //check year
		return false;
	else
	if (!checkrange(sYear, 0, 9999)) //check year
		return false;
	else
	if (!checkinteger(sDay)) //check day
		return false;
	else
	if (!checkday(sYear, sMonth, sDay)) // check day
		return false;
	else
		return true;
    }

function checkday(checkYear, checkMonth, checkDay)
    {

	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
			checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else
	if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	return checkrange(checkDay, 1, maxDay); //check day
    }

function checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;
 
    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".gif";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)

    //Was it a decimal?
    if (check_char < 1)
	return checknumber(object_value);
    else
	return false;
    }

function numberrange(object_value, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
	{
        if (object_value < min_value)
		return false;
	}

    // check maximum
    if (max_value != null)
	{
	if (object_value > max_value)
		return false;
	}
	
    //All tests passed, so...
    return true;
    }

function checkrange(object_value, min_value, max_value)
    {
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;


    if (!checknumber(object_value))
	{
	return false;
	}
    else
	{
	return (numberrange((eval(object_value)), min_value, max_value));
	}
	
    //All tests passed, so...
    return true;
    }

function checkDates(element,msg){
	if (element.value != ''){
		var tmpVal = validateDate(element.value,'-');
		if (tmpVal == false){
			//failed the first test, so start again
			tmpVal = validateDate(element.value,'/');
			if (tmpVal == false)  {
				//failed the second test, one more case....
					tmpVal = validateDate(element.value,'');
					if (tmpVal == false)  {
						// not a date in any of our formats so display error msg	
						alert(msg);
						return true;
					}
					else{
					//final case is true
					element.value = tmpVal;
					return false;
					}
			}	
			else{
			element.value = tmpVal;
			return false;
			}
		}
		else {
		element.value = tmpVal;
		return false;
		}
	}
}

function validateDate(object_value,seperator){
//Returns true if value is a date format 
    //otherwise returns false	
 if (seperator != "" ){
    if (object_value.length == 0)
        return false;
    //Returns true if value is a date in the mm/dd/yyyy format
	isplit = object_value.indexOf(seperator);

	if (isplit == -1 || isplit == object_value.length)
		return false;
	
    //sMonth = object_value.substring(0, isplit);
    sDay = object_value.substring(0, isplit);

	if (sDay.length == 0)
        return false;

	isplit = object_value.indexOf(seperator, isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length)
		return false;

    //sDay = object_value.substring((sMonth.length + 1), isplit);
	sMonth = object_value.substring((sDay.length + 1), isplit);

	if (sMonth.length == 0)
        return false;

	sYear = object_value.substring(isplit + 1);
	
	//convert the mmm into a numeric so that it can be read
	sMonth = convertMonth(sMonth);

}
else{ 
//case of dmmyy or ddmmyy
	
	switch (object_value.length){
	case 5:
		sDay = object_value.substring(0,1);
		sMonth = object_value.substring(1,3);
		sYear = object_value.substring(3,5);
		break;	
	case 6:
		sDay = object_value.substring(0,2);
		sMonth = object_value.substring(2,4);
		sYear = object_value.substring(4,6);
		break;
	default:
		return false;
	}
}	
	//convert the year into four digit year if it isn't
	switch (sYear.length){
	case 1:
		if (sYear > 29){
			sYear = 190 + sYear
		}
		else{
			sYear = 200 + sYear
		}
		break;
	case 2:
		if (sYear > 29){
			sYear = 19 + sYear
		}
		else{
			sYear = 20 + sYear
		}
		break;
	case 3:
		sYear = 2 + sYear;
		break;
	}
	
	if (!checkinteger(sMonth)) //check month
		return false;
	else
	if (!checkrange(sMonth, 1, 12)) //check month
		return false;
	else
	if (!checkinteger(sYear)) //check year
		return false;
	else
	if (!checkrange(sYear, 0, 9999)) //check year
		return false;
	else
	if (!checkinteger(sDay)) //check day
		return false;
	else
	if (!checkday(sYear, sMonth, sDay)) // check day
		return false;
	else
		return sDay + '/' + returnTextMonth(sMonth) + '/' + sYear;
		//return true;    
}

function convertMonth(sMonth){
	switch ((sMonth).toUpperCase()){
		case "ENE":
			sMonth = "1";
			break;
		case "FEB":
			sMonth = "2";
			break;
		case "MAR":
			sMonth = "3";
			break;
		case "ABR":
			sMonth = "4";
			break;
		case "MAY":
			sMonth = "5";
			break;
		case "JUN":
			sMonth = "6";
			break;
		case "JUL":
			sMonth = "7";
			break;
		case "AGO":
			sMonth = "8";
			break;
		case "SEP":
			sMonth = "9";
			break;
		case "OCT":
			sMonth = "10";
			break;
		case "NOV":
			sMonth = "11";
			break;
		case "DIC":
			sMonth = "12";
			break;
	}
	return sMonth;
}

function returnTextMonth(sMonth){
	switch (sMonth){
		case "1":
			sMonth = "Ene";
			break;
		case "01":
			sMonth = "Ene";
			break;
		case "2":
			sMonth = "Feb";
			break;
		case "02":
			sMonth = "Feb";
			break;
		case "3":
			sMonth = "Mar";
			break;
		case "03":
			sMonth = "Mar";
			break;
		case "4":
			sMonth = "Abr";
			break;
		case "04":
			sMonth = "Abr";
			break;
		case "5":
			sMonth = "May";
			break;
		case "05":
			sMonth = "May";
			break;
		case "6":
			sMonth = "Jun";
			break;
		case "06":
			sMonth = "Jun";
			break;
		case "7":
			sMonth = "Jul";
			break;
		case "07":
			sMonth = "Jul";
			break;
		case "8":
			sMonth = "Ago";
			break;
		case "08":
			sMonth = "Ago";
			break;
		case "9":
			sMonth = "Sep";
			break;
		case "09":
			sMonth = "Sep";
			break;
		case "10":
			sMonth = "Oct";
			break;
		case "11":
			sMonth = "Nov";
			break;
		case "12":
			sMonth = "Dic";
			break;
	}
	return sMonth;
}

function ResetFields(frmName){
//used to reset all the fields if no key is selected.

var num = document.forms(frmName).elements.length;
 for (i=1;i<num;i++){
	var ename = document.forms(frmName).elements[i].name;
	//validate depending on what data type it is.
		switch(ename.substring(0,3)){
			case "cbo":	
			//go to the first selected item
				document.forms(frmName).elements(ename).selectedIndex = 0;
				break;
			case "txt":	
				document.forms(frmName).elements(ename).value = "";
				break;
			case "chk":	
				document.forms(frmName).elements(ename).checked = false;
				break;
			default:
				break;
			}
	}
}

function checkTime(element,msg){
	if (element.value != ''){
		sHour = element.value.substring(0,element.value.indexOf(':'));
		sMin = element.value.substring(element.value.indexOf(':')+1,element.value.length);
		if (sMin == '')
		{
			sMin = '0';
		}
		if (sHour == '')
		{
			sHour = '0';
		}
		if (element.value.indexOf(':') == -1)
		{
			alert(msg);
			return true;
		}	
		if (!checkinteger(sHour)) // Check Hours
		{
			alert(msg);
			return true;
		}
		else
		{
			if (!checkrange(sHour, 0, 23)) // Check Hours
			{
				alert(msg);
				return true;
			}
		}
		if (!checkinteger(sMin)) // Check Minutes
		{
			alert(msg);
			return true;
		}
		else
		{
			if (!checkrange(sMin, 0, 59)) // Check Minutes
			{
				alert(msg);
				return true;
			}
		}
		// Format the Time, just to be nice...
		if (sMin < 10)
		{
			sMin = '0' + sMin;
		}
		if (sHour < 10)
		{
			sHour = '0' + sHour;
		}
		element.value = sHour + ":" + sMin;
		return false;
	}
}
