// JavaScript Document

var imageSrc = '../img/buscar.gif';

// =================================================================
// VALIDATION FUNCTIONS
// =================================================================
/*
 * Put a message in the div with id 'mensajes'.
 */
function putMessage(strMsg, add, id){
	var field = "";
	if(id)
	{
		field = "<a href=\"javascript:document.getElementById('" + id + "').focus();\"><img src=\"" + imageSrc + "\" border=\"0\" /></a>";
	}
	var text = (strMsg == "")?"":"<br/> " + field + " " + strMsg;
	if(add){
		alert(strMsg);
		document.getElementById(id).focus();
		//document.getElementById('message').innerHTML += text;
	}else{
		//document.getElementById('message').innerHTML = text;
	}
}

/*
 * Validation usign Regular Expressions.
 */
function validate(str,pattern,error, id){
	var bReturn;
	var reg_exp = new RegExp(pattern);
	if(reg_exp.test(str)){
		bReturn = true;
	}else{
		bReturn = false;
		putMessage(error,true, id);
	}
	return bReturn;
}

/*
 * Funcion that indicate if a variable is Null (empty).
 */
var NOT_EMPTY = /[^|^ |^-]/;
function isNull(value){
	var bReturn = false;
	if(!NOT_EMPTY.test(value) || value == "-1"){
		bReturn = true;
	}
	return bReturn;
}

/*
 * A set of validations for send the form 'search',
 * using the Polymorphic Array RESTRICTIONS.
 */
 function doValidations(url){
	var bValid = true;
	var objX;
	putMessage('');	
	for(var i=0; i < RESTRICTIONS.length; i++){
		objX = document.getElementById(RESTRICTIONS[i].field);		
		if( objX == null )
		{
			//alert( 'No se encontró el campo [' + RESTRICTIONS[i].field + ']' );
			return null;
		}
		// When the object is select
		if(objX.type == "select-one"){
			// We must get the text of the selected option
			bValid &= RESTRICTIONS[i].validate(objX.options[objX.selectedIndex].text);
		}
		else if(objX.type == "radio" || objX.type == "checkbox"){
			// We must get the text of the selected option
			radioButtons = eval("document.search." + RESTRICTIONS[i].field);
			bValid &= RESTRICTIONS[i].validate(radioButtons);
		}else{
			objX.value=objX.value.replace("'",'"');
			bValid &= RESTRICTIONS[i].validate(objX.value);	
		}		
		if(!bValid){
			i =RESTRICTIONS.length;
		}
	}	
	// Show the error message of do submit.
	if(!bValid){
		//alert("Falso")
		//show('error');
		//hidden('noerror');
		return(false);
	}else{
		//alert(oFCKeditor);	
		document.search.submit();
	}
}

// =================================================================
// VALIDATION CLASSES
// =================================================================
/*
 * pField is the name of the field.
 * pPattern is the complete pattern for RegExp
 * pMessage is the message that appear if is not valid
 */
function Validacion(pField,pPattern,pMessage){
	this.field = pField;
	this.pattern = pPattern;
	this.message = pMessage;
	this.validate = ValidacionTest;
} 
function ValidacionTest(value){	
	var bReturn = true;
	if(!isNull(value)){
		bReturn = validate(value,this.pattern,this.message, this.field);
	}
	return bReturn;
}
/*
 * pField is the name of the field.
 * pLength is the length. F.e.: 
 *		3 if must have length 3
 * 		3, if must have length 3 or higher 
 * pMessage is the message that appear if is not valid
 */
function Longitud(pField,pLength,pMessage){
	this.field = pField;
	this.pattern = pLength;
	this.message = pMessage;
	this.validate = LongitudTest;
} 
function LongitudTest(value){	
	var bReturn = true;
	if(!isNull(value)){
		bReturn = validate(value,"^[^?]{" + this.pattern + "}$",this.message, this.field);
	}
	return bReturn;
}
/*
 * pField is the name of the field.
 * pMessage is the message that appear if is null
 */
function NotNull(pField,pMessage){
	this.field = pField;
	this.pattern = "[^|^ |^-]";
	this.message = pMessage;
	this.validate = NotNullTest;
} 
function NotNullTest(value){	
	return validate(value,this.pattern,this.message, this.field);
}
/*
 * pField is the name of the field.
 * pMessage is the message that appear if is null
 */
function NotIs(pField,pValue,pMessage){
	this.field = pField;
	this.pattern = pValue;
	this.message = pMessage;
	this.validate = NotIsTest;
} 
function NotIsTest(value){	
	var bReturn = (value != this.pattern);
	if(bReturn == false){ putMessage(this.message,true, this.field); }
	return bReturn;
}
/*
 * pField is the name of the field.
 * pPattern is the complete pattern for RegExp
 * pMessage is the message that appear if is not valid
 */
function NumericField(pField,pMessage){
	this.field = pField;
	this.pattern = '^[\-\+0-9]*$';
	this.message = pMessage;
	this.validate = ValidacionTest;
} 
function RealField(pField,pMessage){
	this.field = pField;
	this.pattern = '^[\-\+0-9.]*$';
	this.message = pMessage;
	this.validate = ValidacionTest;
} 
/*
 * pField is the name of the field.
 * pPattern is the complete pattern for RegExp
 * pMessage is the message that appear if is not valid
 */
function TextField(pField,pMessage){
	this.field = pField;
	this.pattern = '^[a-zA-Zá-úÁ-Ú\-\+0-9., ]*$';
	this.message = pMessage;
	this.validate = ValidacionTest;
} 
/*
 * pField is the name of the field.
 * pPattern is the complete pattern for RegExp
 * pMessage is the message that appear if is not valid
 */
function EmailField(pField,pMessage){
	this.field = pField;
	this.pattern = '^[a-zA-Z0-9.\_\-]+@[a-zA-Z0-9.\_\-]+.[a-zA-Z0-9.\_\-]+$';
	this.message = pMessage;
	this.validate = ValidacionTest;
} 

/*
 * pField is the name of the field.
 * pPattern is the complete pattern for RegExp
 * pMessage is the message that appear if is not valid
 */
function RadioField(pField,pMessage){
	this.field = pField;
	this.pattern = '^[a-zA-Zá-úÁ-Ú\-\+0-9., ]*$';
	this.message = pMessage;
	this.validate = RadioFieldTest;
} 

function RadioFieldTest(value){	
	var bReturn = false;
	var i = 0;
	if(value.length>0){
		for(i=0 ; i < value.length; i++ )
		{
			
			if(value[i].checked)
			{
				bReturn = true;
			}
		}
	
		if(!bReturn)
		{
			putMessage(this.message,true, this.field);
		}
	}else{
		if(!value.checked){
			putMessage(this.message,true, this.field);
			bReturn = false;
		}else{
			bReturn = true;
		}
	}
	return bReturn;
}
function fechaField(pField,pMessage){
	this.field = pField;
	this.pattern = '';
	this.message = pMessage;
	this.validate = fechascompleta;
} 

function fechas(dia,mes,anyo,campo)
{ 
	borrar="verdadero";
	var bReturn = true;
	a = anyo;
	m = mes;
	d = dia;
	if((a < 1900) || (a > 2050) || (m < 1) || (m > 12) || (d < 1) || (d > 31))
		borrar = 'falso';
	else
	{
		if((a%4 != 0) && (m == 2) && (d > 28))	   
			borrar = 'falso'; // Año no viciesto y es febrero y el dia es mayor a 28
		else	
		{
			if ((((m == 4) || (m == 6) || (m == 9) || (m==11)) && (d>30)) || ((m==2) && (d>29)))
			borrar = 'falso';	      				  	 
		}  // else
	} // fin else
	if (borrar == 'falso'){
	    putMessage("Formato de fecha invalido",true,campo);
		bReturn=false;
	}
	return bReturn;
} // FUNCION

function fechascompleta(value)
{ 
		bReturn=true;
caja=value;
   if (caja)
   {  
      borrar = caja;
      if ((caja.substr(2,1) == "/") && (caja.substr(5,1) == "/"))
      {      
         for (i=0; i<10; i++)
	     {	
            if (((caja.substr(i,1)<"0") || (caja.substr(i,1)>"9")) && (i != 2) && (i != 5))
			{
               borrar = '';
               break;  
			}  
         }
	     if (borrar)
	     { 
	        a = caja.substr(6,4);
		    m = caja.substr(3,2);
		    d = caja.substr(0,2);
		    if((a < 1900) || (a > 2050) || (m < 1) || (m > 12) || (d < 1) || (d > 31))
		       borrar = '';
		    else
		    {
		       if((a%4 != 0) && (m == 2) && (d > 28))	   
		          borrar = ''; // Año no viciesto y es febrero y el dia es mayor a 28
			   else	
			   {
		          if ((((m == 4) || (m == 6) || (m == 9) || (m==11)) && (d>30)) || ((m==2) && (d>29)))
			         borrar = '';	      				  	 
			   }  // else
		    } // fin else
         } // if (error)
      } // if ((caja.substr(2,1) == "/") && (caja.substr(5,1) == "/"))			    			
	  else
	     borrar = '';
   } // if (caja)   
	if (borrar == ''){
	    putMessage("Formato de fecha invalido",true, this.field);
		//alert(value);
		bReturn=false;
	}
	return bReturn;  
}