//Validaciones varias de javascript...
	
//Manejador de eventos del teclado
//inhibe el apostrofe y  procesa el enter como submit
function handleKeys(funtocall,isNumeric,isPhone){
	var keycode;
    var isNS = (navigator.appName=="Netscape")?true:false;
    //alert("isNS = " + isNS);
    
    if (!isNS) {
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        else return true;
    } else {
        if (window.event) keycode = window.event.keyCode;
        else return true;
    }

	if (keycode == 39) {		
		return false
	}
	if (isNumeric==true){
		if (   (keycode<58 && keycode >47 )|| (keycode ==13)){
			 
		}
		else {
			return false;
		}
	}
	if (isPhone==true){
		if (   (keycode<58 && keycode >47 ) || (keycode ==32) || (keycode ==45) || (keycode ==41) || (keycode ==40) || (keycode ==13)){		
			 
		}else {	 
			return false;
		}   
	}
	if (keycode == 13) {
		//alert("Please Click on the Submit button to send this");
		//document.forms[0].submit();
		//enterWork();
		if (funtocall!=""){
			eval(funtocall+"()");
		}
		//return false		
	}
	return true;
}
//document.onkeypress = handleKeys;

 

//indica si campo está vacio 
function isEmpty(item){

	var inputStr=item.value;
	inputStr = Trim(inputStr);
	if (inputStr == null || inputStr == "") {
		return true;
	}
	if (Trim(inputStr) == ""){
		return true;
	}
	return false;
}	

function Trim(cadena) {
 
//Elimina los espacios en blanco al principio y final de la cadena
	inputStr = cadena.toString();
	cadena = "";
	for (var i=0;i < inputStr.length; i++){
		var oneChar = inputStr.charAt(i);
		if (oneChar != " " && oneChar != "'" && oneChar != '"')	{
			cadena = cadena + oneChar;
		}
	}
	return cadena;
}	
	
function validDropDown(fieldName,message) {
	if (fieldName[fieldName.selectedIndex].value == "" ) {
		//alert(message);
		fieldName.focus();
		return false;
	}
	return true;
}
      

function validPhone(fieldName,message,canBeNull) {
 
	   if ( ((canBeNull == 'True') || (canBeNull == true) )
		&& (trim(fieldName.value) == '')){
  
		return true;
	}     
 	iLen= fieldName.value.length; 
	s=fieldName.value;
  	for (i = 0; i < iLen; i++)	{   
			// Check that current character is number.
			var c = s.charAt(i);
 
			if (!(isDigit(c) || c == " " || c== "-" || c== "(" || c== ")" )) { 
				alert(message);
 
				return false;
				}
			else{
 
				return true;
				}
				
		}
 	
	//car = new RegExp("/\d\s[-]/gi");
	//return (!car.test(fieldName.value))
	
}        

	
function validNumber(fieldName,message,canBeNull) {	
	fieldName.value=trim(fieldName.value);
	   if ( ((canBeNull == 'True') || (canBeNull == true) )
			&& (fieldName.value == '')){
		return true;
		}        	
	
	s=fieldName.value;
	if (!isInteger(s)) {
		//alert( message);
		fieldName.focus();
			return false;
		}  
	
	return true;	
}        


function isInteger (s)
{   var i;

		// Search through string's characters one by one
		// until we find a non-numeric character.
		// When we do, return false; if we don't, return true.

		for (i = 0; i < s.length; i++)	{   
			// Check that current character is number.
			var c = s.charAt(i);
			if (!isDigit(c)) return false;
		}

		// All characters are numbers.
		return true;
}

function isDigit (c){ 
	return ((c >= "0") && (c <= "9"))
}


function validMinLength(fieldName,message,minLength,count,canBeNull) {
   if ( ((canBeNull == 'True') || (canBeNull == true) )
	&& (fieldName.value == '')){
	   return true;
	}	
	if (fieldName.value.length < minLength) {

		alert(message);

		fieldName.focus();

		return false;

	}

	else

		return true;
}

function validMaxLength(fieldName,message,minLength,count) {
	if (fieldName.value.length > minLength) {
		alert(message);
		fieldName.focus();
		return false;
	}
	else
		return true;
}

//quitar espacios al principio y al final no enmedio!
function trim(inputStr){

	cadena = "";
	bEspacio=false;
	bLetra=false;
	iLen = inputStr.length;
	var i=0;
	var oneChar = inputStr.charAt(i);
	while (oneChar == " ") {
		i++;
		oneChar = inputStr.charAt(i);
	}
	
	var j = iLen-1;
 
	oneChar = inputStr.charAt(j);
	
	while (oneChar == " " ) {
		j--;
		oneChar = inputStr.charAt(j);
		
	}
	if (oneChar==" ") {
		inputStr = 	inputStr.slice(i,j)
	}
	else {
		inputStr = 	inputStr.slice(i,j+1)	
	}
	

	iLen = inputStr.length;		
	for (var i=0;i < iLen; i++)
	{
		oneChar = inputStr.charAt(i);		
		if (oneChar != "'"  && oneChar != '"')		{
			cadena = cadena + oneChar;
		}
	}

	return cadena;
}	
	

//NO olvdiar usar ="JavaScript1.2"
function (fieldName, message,bFocus) {
	fieldName.value = trim(fieldName.value);
	if (fieldName.value.length == 0) {
		alert(message);
		if (bFocus) {
			fieldName.focus();
		}
		return false;
	}
	else
		return true;
}


// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) and numbers only.

function isAlphanumeric (fieldName)
{   var i;
    var s=fieldName.value;
    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);
        
        if (! (( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) ) || ((c >= "0") && (c <= "9")) || (c == "_") ) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}

// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.

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 makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}



// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day 
// form a valid date.
// 

function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.

    // Explicitly change type to integer to make code work in both

    // JavaScript 1.1 and JavaScript 1.2.

	var daysInMonth = makeArray(12);
		daysInMonth[1] = 31;
		daysInMonth[2] = 29;   // must programmatically check this
		daysInMonth[3] = 31;
		daysInMonth[4] = 30;
		daysInMonth[5] = 31;
		daysInMonth[6] = 30;
		daysInMonth[7] = 31;
		daysInMonth[8] = 31;
		daysInMonth[9] = 30;
		daysInMonth[10] = 31;
		daysInMonth[11] = 30;
		daysInMonth[12] = 31;

    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February

    if (intDay > daysInMonth[intMonth]) return false; 
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
    return true;
}


/*
-- Original:  Mike Welagen (welagenm@hotmail.com) --
*/
<!-- Begin
//var msgInvalidDate  = "That date is invalid.  Please try again.";
var msgInvalidDate  = "Fecha inválida. Intente denuevo.";

function setMsgInvalidDate(msg){
	msgInvalidDate = msg;
}

function checkdate(objName) {
var dato = objName.value;
var datefield = objName;


//quitar los espacios extras..
car = /\s/gi;
dato = dato.replace(car, "");	
objName.value = dato;	


if (dato.length < 6 && dato.length > 0) {
	alert(msgInvalidDate);
	datefield.select();
	datefield.focus();
	return false;
}

if (chkdate(objName) == false) {
datefield.select();
alert(msgInvalidDate);
datefield.focus();
return false;
}
else {
return true;
   }
}
function chkdate(objName) {

//var strDatestyle = "US"; //United States date style
var strDatestyle = "EU";  //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
 
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
   }
}
if (strYear.length == 2) {
strYear = '20' + strYear;
}
// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}
if (strDatestyle == "US") {
datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
}
else {
//datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}

function doDateCheck(from, to) {
if (Date.parse(from.value) <= Date.parse(to.value)) {
alert("The dates are valid.");
}
else {
if (from.value == "" || to.value == "") 
alert("Both dates must be entered.");
else 
alert("To date must occur after the from date.");
   }
}

//Validate email entries
function validEmail(fieldName,message,canBeNull) {
 
	   if ( ((canBeNull == 'True') || (canBeNull == true) )
		&& (trim(fieldName.value) == '')){
  
		return true;
	}     
	if (validateEmail(fieldName.value)){
		return true;
	}
	else{
		alert(message);
		return false;
	}

}        

function validateEmail(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("Email address seems incorrect (check @ and .'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("The username doesn't seem to be valid.")
	    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("Destination IP address is invalid!")
			return false
		    }
	    }
	    return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
	    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("The address must end in a three-letter domain, or two letter country.")
	   return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}

//  End -->

	

	

	

	


