/************************************************ 
Veritask Software 

Copyright 2004-2005 Veritask, LLC. All rights reserved.

************************************************/


// variables globales
var TheExplorer = navigator.appName;





function ValidateInt(TheEvent) {
	var TheKey;
	var IsValid=false;

	if (TheExplorer.indexOf("Explorer",0) != -1) {
		TheKey = TheEvent.keyCode;
	} else {
		TheKey=TheEvent.which;
	}
	if (((TheKey > 47) && (TheKey < 58)) || (TheKey==8) || (TheKey==13) ||
	    (TheKey==45) ) {
		IsValid=true;
	}
	if (TheExplorer.indexOf("Explorer",0) != -1) {
		if (!IsValid) {
			TheEvent.keyCode = "";
		}
	}
	return IsValid;
}




function ValidateNatural(TheEvent) {
	var TheKey;
	var IsValid=false;

	if (TheExplorer.indexOf("Explorer",0) != -1) {
		TheKey = TheEvent.keyCode;
	} else {
		TheKey=TheEvent.which;
	}
	if (((TheKey > 47) && (TheKey < 58)) || (TheKey==8) || (TheKey==13)) {
		IsValid=true;
	}
	if (TheExplorer.indexOf("Explorer",0) != -1) {
		if (!IsValid) {
			TheEvent.keyCode = "";
		}
	}
	return IsValid;
}





function ValidateFloat(TheEvent, control) {
	var TheKey;
	var IsValid=false;

	if (TheExplorer.indexOf("Explorer",0) != -1) {
		TheKey = TheEvent.keyCode;
	} else {
		TheKey=TheEvent.which;
	}
	if (((TheKey > 47) && (TheKey < 58)) || (TheKey==8) || (TheKey==13) || 
	    (TheKey==45) || (TheKey==46)) {
    	if ( (TheKey==46) && (control.value.indexOf(".")!= -1 ) ) {
    	  IsValid = false 
    	 }
    	 else {
    	   IsValid = true;
    	 }		 
	}
	if (TheExplorer.indexOf("Explorer",0) != -1) {
		if (!IsValid) {
			TheEvent.keyCode = "";
		}
	}
	return IsValid;
}


function ValidateDate (TheEvent,control) {
	var TheKey;
	var IsValid=false;

	if (TheExplorer.indexOf("Explorer",0) != -1) {
		TheKey = TheEvent.keyCode;
	} else {
		TheKey=TheEvent.which;
	}

	if (((TheKey > 47) && (TheKey < 58)) || (TheKey==8) || (TheKey==13)) {
		IsValid=true;
	} else if (TheKey==47) {
		//Valida que no haya dos TheCharacteres "/"
		var i=0;
		var position=0;
		var thevalue=control.value;
		var theday=0;
		var strtheday="";
		while (thevalue.indexOf("/") != -1) {
			i++;
			position=thevalue.indexOf("/");
			thevalue=thevalue.substring(position+1, thevalue.length);
		}
		if (i==2) {
			alert("The date cannot have 3 times the character /");
			IsValid=false;
		} else if (i==1) {
			if (thevalue=="") {
				alert("Invalid Month");
				strtheday=control.value;
				strtheday=strtheday.substring(0, position);
				control.value="";
				IsValid=false;
			} else {
				TheMonth = parseFloat(thevalue);
				strtheday=control.value;
				strtheday=strtheday.substring(0, position);
				theday = parseFloat(strtheday);
				if (TheMonth > 0 && TheMonth < 13) {
					//Ahora valida el TheMonth contra el theday
					if ( TheMonth==4 || TheMonth==6 || TheMonth==9 || TheMonth==11) {
						if ( theday > 30 ) {						
							alert("This month haven't "+ theday + " days");
							//control.value="";
							IsValid=false;
						} else {
							IsValid=true;
						}
					} else if (TheMonth==2) {
						if (theday > 29) {
								alert("This month haven't "+ theday + " days");
								//control.value="";
								IsValid=false;
							} else {
								IsValid=true;
							}
					} else {
						IsValid=true;
					}
				} else {
					alert("Invalid Month");
					//control.value="";
					IsValid=false;
				}
			}
		} else if (i==0) {
			//Este es el primer TheCharacter "/"
			if (thevalue=="") {
				alert("Invalid Day");
				//control.value="";
				IsValid=false;
			} else {
				//valida el theday
				theday=parseFloat(thevalue);
				if ((theday==0) || (theday > 31)) {
					alert("Invalid Day");
					//control.value="";
					IsValid=false;
				} else {
					IsValid=true;
				}
			}
		}
	}
	if (TheExplorer.indexOf("Explorer",0) != -1) {
		if (!IsValid) {
			TheEvent.keyCode = "";
		}
	}
	return IsValid;
}



 
 function SelectControl (Control) {
   Control.focus();
   Control.select(); 
 }

 
 
 
 
  
  
  function ValidateControlsNotNull(TheDBSPForm,TheArray) {
     for (var i = 0; i < TheDBSPForm.elements.length; i++) {  	
       if (IsInTheArray(TheDBSPForm.elements[i].name,TheArray)) {
            	if (IsEmpty(TheDBSPForm.elements[i].value)) {
                    alert ("This field is required!");
                    TheDBSPForm.elements[i].focus()
                    TheDBSPForm.elements[i].select()	
                    return false           
                }         
       }         
     }
    return true;
  }

  



function UpperCase (Control) {
  Control.value=Control.value.toUpperCase();

}


function LowerCase (Control) {
  Control.value=Control.value.toLowerCase();

}




/******************************************************************************
  Funciones de soporte para las funciones principales.
******************************************************************************/



  function IsInTheArray(Elemento, TheArray) {
    for (var i = 0; i < TheArray.length; i++) {  	
      if (TheArray[i]==Elemento) {        
          return true
      }         
    }
    return false  
  }



  
  function IsEmpty(TheString) {
    if (TheString == "" || TheString == null) {
      return true
    }
  
   for (var i = 0; i < TheString.length; i++) {
      var TheCharacter = TheString.substring(i, i + 1)
      if (TheCharacter!=" " && TheCharacter!="\t" && TheCharacter!="\n") {
        return false
      }
   }
  
    return true
  }


  
  
   function IsDateValid(TheDate) {
   
     var theday= SubTheStringDelim(TheDate,"/",0,1)
     var TheMonth= SubTheStringDelim(TheDate,"/",1,2)
     var TheYear= SubTheStringDelim(TheDate,"/",2,3)
     
     if (IsEmpty(TheDate)) {
          return true		
     }
   
     if ( (!Validatetheday(theday)) || (!ValidateTheMonth(TheMonth)) || (!ValidateTheYear(TheYear)) ) { 	
          return false;
     }     
   
     return(IsDateValidCalendarioGregoriano(theday,TheMonth,TheYear,TheDate) ) 
   }
   
   
   
   
   
   function IsNumber(TheString) {
   
     if (TheString == "" || TheString == null) {
       return true
     }
   
     for (var i = 0; i < TheString.length; i++) {
       var TheCharacter = TheString.substring(i, i + 1)
       if (TheCharacter < "0" || TheCharacter > "9") {
         return false
       }
     }
     return (!(TheString.length==0))
   }


   
   function InRange(TheString, Min, Max) {
     var num = parseInt(TheString)
     if (num < Min || num > Max) {
      return false
     }
     return true
   }
   
   
   function DeleteZeros(TheString) {
     var TheResult = TheString
     while (TheResult.substring(0,1) == "0") {
      TheResult = TheResult.substring(1,TheResult.length)
     }
     return TheResult
   }
   
   
   function ValidateNumericField(TheField,Min,Max) {
     var TheEntrance = DeleteZeros(TheField)
   
     if ((!IsNumber(TheEntrance)) || (!InRange(TheEntrance,Min,Max)) || IsEmpty(TheEntrance) ) {
         return false
     }
   
     return true
   }
     
     
	 
   function Validatetheday(TheField) {
     return (ValidateNumericField(TheField,1,31))
   }
   
   function ValidateTheMonth(TheField) {
     return (ValidateNumericField(TheField,1,12))
   }
   
   function ValidateTheYear(TheField) {
     return (ValidateNumericField(TheField,1900,9999))
   }
   
   
   
   
    function TheDateCalendarioGregoriano(theday,TheMonth,TheYear) {
        var thedayMaximo=0;
        var TheMonthAux=parseInt(TheMonth)
    
        if (TheMonth==1 || TheMonth==3 || TheMonth==5 || TheMonth==7 || TheMonth==8 || TheMonth==10 || TheMonth==12) {
           thedayMaximo=31
        }
        else {
           if (TheMonth==2) {
               if ((TheYear % 4)==0 && TheYear!=1900) {
                    thedayMaximo=29
               }
               else {
                    thedayMaximo=28
               }
           }
           else {
               thedayMaximo=30
           }
        }
    
        return(parseInt(theday)<=thedayMaximo)
    }
   
   
    
    
    function IsDateValidCalendarioGregoriano(theday,TheMonth,TheYear,Control) {
        var theTheMonthsage="This date " +theday+"/"+TheMonth+"/"+TheYear+ " exist not."
    
        if(TheDateCalendarioGregoriano(theday,TheMonth,TheYear)) {
            return(true)    
        }
        else {
             alert(theTheMonthsage)
             return false
       }
    
    }   
	
	
	
    function SubTheStringDelim(TheString,Delimitador,Inicio,Fin) {
      var AparDelim=0
      var SubTheString=""
      for (var i = 0; i < TheString.length; i++) {
        var TheCharacter = TheString.substring(i, i + 1)
    
        if (TheCharacter==Delimitador) {
            AparDelim++
        }
     
        if ((AparDelim>=Inicio) && (AparDelim<Fin) ) {
            if (!(TheCharacter==Delimitador)) {
               SubTheString= SubTheString+TheCharacter
            }
        } 
        
        if (AparDelim==Fin) {
            return(SubTheString)
        }          
      }	
      return (SubTheString)  	
    }



/******************************************************************************
  funcion ValidaCorreo
  Esta función verifica que el correo introducido sea correcto.
******************************************************************************/

function ValidateEmail(correo){
  var i=1
  var Posfinal = correo.length
  var IsValid = false
  if(Posfinal>0){
    if((IsLetter(correo.charAt(0)))){ 
       var token = correo.charAt(i)
       while((i<Posfinal)&&(token!='@')&&((IsLetter(token))||(IsNumber(token))||(token=='.')||(token=='_'))){
          i++;   
          token = correo.charAt(i);
       }
       if ((token == '@')&&(correo.charAt(i+1)!= '.')&&((i+1)<Posfinal)){
          i++;   
          token = correo.charAt(i)
          while((i<Posfinal)&&(token!='.')&&((IsLetter(token))||(IsNumber(token))||(token=='_'))){
              i++;   
              token = correo.charAt(i)
          }
          if((token == '.')&&(correo.charAt(i+1)!='.')&&((i+1)<Posfinal)){
            i++;   
            token = correo.charAt(i)
            while((i<Posfinal)&&(token!='.')&&((IsLetter(token))||(IsNumber(token)||(token=='_')))){
               i++;   
               token = correo.charAt(i)
            }
            if (i>=Posfinal){
               return true
            }else{
               if ((token == '.')&&((i+1)<Posfinal)){
                 i++;   
                 token = correo.charAt(i)
                 while((i<Posfinal)&&((IsLetter(token))||(IsNumber(token))||(token=='_'))){
                   i++;   
                   token = correo.charAt(i)
                 }
                 if(i>=Posfinal){
                    return true
                 }else{
                    alert('Invalid e-mail, please verify.')
                    return false
                 }
               }else{
                  alert('Invalid e-mail, please verify.')
                  return false
               }
            }
          }else{
             alert('Invalid e-mail, please verify.')
             return false
          }
       }else{
          alert('Invalid e-mail, please verify.')
          return false
       }
    }else{
       alert('Invalid e-mail, please verify.')
       return false;
    }
  }else{
     return true;
  }
}

/******************************************************************************
  funcion IsLetter
  Devuelve true si el TheCharacter recibido es una TheLetter.
******************************************************************************/

function IsLetter(TheLetter){
   if(((TheLetter>='A') && (TheLetter<='Z')) ||((TheLetter>='a')&&(TheLetter<='z'))){
      return true;
   }else{
      return false;
   }
}
/******************************************************************************
  funcion IsNumber
  Devuelve true si el TheCharacter recibido es un número.
******************************************************************************/

function IsNumber(TheNumber){
   if((TheNumber>='0') && (TheNumber<='9')){
      return true;
   }else{
      return false;
   }
}
function IsValidEmail(email){
  checkEmail = email.value
  if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))) 
  {
    alert("You have entered an invalid email address. Please try again.");
    email.select();
    return false;
  } 
  else {
   return true;
  }
}


