// JavaScript Document



var imageSrc = 'images/showmenu.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 == "")?"":strMsg;

	if(add){		
		document.getElementById(id).focus();
		alert(strMsg);
		//document.getElementById('mensajeAlert').innerHTML = text;
		//anotherwindow.openAlert();
		return false;
	}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, formName){
	var bValid = true;

	var objX;

	putMessage('');	

	for(var i=0; i < RESTRICTIONS.length; i++){

		objX = document.getElementById(RESTRICTIONS[i].field);		

		//alert(objX.type);

		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"){

			// We must get the text of the selected option

			radioButtons = eval("document." + formName + "." + RESTRICTIONS[i].field);

			bValid &= RESTRICTIONS[i].validate(radioButtons);

		}

		else if(objX.type == "checkbox"){

			// We must get the text of the selected option

			checkButtons = eval("document." + formName + "." + RESTRICTIONS[i].field);

			bValid &= RESTRICTIONS[i].validate(checkButtons);

		}else{

			bValid &= RESTRICTIONS[i].validate(objX.value);	

		}				
		if(!bValid){
			i =RESTRICTIONS.length;
		}
	}	

	// Show the error message of do submit.
	if(!bValid){
		return false;
	}else{
		var formularioEnvio = eval("document."+formName);
		formularioEnvio.action = url;
		formularioEnvio.submit();
	}

	return bValid;

}

// =================================================================

// 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;

}

function compareTest(value){	

	var bReturn = true;

	if(!isNull(value)){
		bReturn = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value);
		if(!bReturn){
			putMessage(this.message,true,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 notNullDepend(pField,field2,arrayOptions,pMessage){
	this.field = pField;
	this.field2 = field2;
	this.arrayOptions=arrayOptions;
	this.pattern = "[^|^ |^-]";
	this.message = pMessage;
	this.validate = notNullDependTest;
}

function notNullDependTest(value){
	isSelectField=false;
	valueSelcted=document.getElementById(this.field2).value;
	var tam= this.arrayOptions.length;
	for(i=0;i<tam;i++){
		if(valueSelcted==this.arrayOptions[i]){
			isSelectField=true;
		}
	}
	if(isSelectField){
		return validate(value,this.pattern,this.message, this.field);
	}else{
		return true;
	}	
}

function NotNullTest(value){	

	return validate(value,this.pattern,this.message, this.field);

}

function ifSelectField(checkfiel,pField,pMessage){
	this.field = pField;
	this.checkfiel = checkfiel;
	this.message = pMessage;
	this.pattern = "[^|^ |^-]";
	this.validate = ifSelectFieldTest;
}

function ifSelectFieldTest(value){	
	if(document.getElementById(this.checkfiel).checked){
		return validate(value,this.pattern,this.message, this.field);
	}else{
		return true;
	}
}

function equalFields(pField,field2,pMessage){
	this.field = pField;
	this.field2 = field2;
	this.message = pMessage;
	this.validate = isEqual;
}


function isEqual(val){	
	if(document.getElementById(this.field2).value==val){
		return true
	}else{
		putMessage(this.message,true, this.field);
		return false;
	}
}
/*

 * 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;

} 

/*

 * 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 = '/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/';

	this.message = pMessage;

	this.validate = compareTest;

} 



/*

 * 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;

	for( ; i < value.length; i++ )

	{

		if(value[i].checked)

		{

			bReturn = true;

		}

	}



	if(!bReturn)

	{

		putMessage(this.message,true, this.field);

	}



	return bReturn;

}



function isDate(pField,pMessage){
	this.field = pField;
	this.message = pMessage;
	this.validate = CheckDateTest;
} 

function CheckDateTest(value){
	var bReturn = true;
	borrar = value;
      if ((value.substr(2,1) == "/") && (value.substr(5,1) == "/"))
      {      
         for (i=0; i<10; i++)
	     {	
            if (((value.substr(i,1)<"0") || (value.substr(i,1)>"9")) && (i != 2) && (i != 5))
			{
               bReturn = false;
               break;  
			}  
         }
	     if (borrar)
	     { 
	        a = value.substr(6,4);
		    m = value.substr(3,2);
		    d = value.substr(0,2);
		    if((a < 1900) || (a > 2006) || (m < 1) || (m > 12) || (d < 1) || (d > 31)){
		        bReturn = false;
			}
		    else
		    {
		       if((a%4 != 0) && (m == 2) && (d > 28)){   
		           bReturn = false;// 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)))
			          bReturn = false;;	      				  	 
			   } 
		    } 
         } 
      }    			
	  else{
	      bReturn = false;
	  } 
	 if(!bReturn)

	{

		putMessage(this.message,true, this.field);

	}
   return bReturn ;  
} 


function CheckField(pField,pMessage){

	this.field = pField;

	this.pattern = '^[a-zA-Zá-úÁ-Ú\-\+0-9., ]*$';

	this.message = pMessage;

	this.validate = CheckFieldTest;

} 



function CheckFieldTest(value){	

	var bReturn = false;

	var i = 0;

	

	if(value.checked)

	{

		bReturn = true;

	}



	if(!bReturn)

	{

		putMessage(this.message,true, this.field);

	}



	return bReturn;

}

