// JavaScript Document

// =================================================================
// VALIDATION FUNCTIONS
// =================================================================
/*
 * Put a message in the div with id 'mensajes'.
 */
 
var imageSrc = 'images/showmenu.gif';
var RESTRICTIONS = [];
 
function putMessage(strMsg, add, id){
	var field = "";
	if(id)
	{
		//field = "<a href=\"javascript:document.getElementById('" + id + "').focus();\"><img src=\"" + imageSrc + "\" border=\"0\" /></a>";
		alert(strMsg);
		$(id).focus();
		return(false);
	}
	var text = (strMsg == "")?"":"<br/> " + field + " " + strMsg;
	if(add){
		//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(idForm, RESTRICTIONS){
	var bValid = true;
	var objX;
	putMessage('');	
	for(var i=0; i < RESTRICTIONS.length; i++){
		objX = $(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"){
			// We must get the text of the selected option
			radioButtons = eval("document." + idForm + "." + RESTRICTIONS[i].field);
			bValid &= RESTRICTIONS[i].validate(radioButtons);
		}else{
			bValid &= RESTRICTIONS[i].validate(objX.value);	
		}				
	}	
	// Show the error message of do submit.
	if(!bValid){
		//show('error');
		//hidden('noerror');
	}else{
		//document.search.action = url;
		//document.search.submit();
	}
	return bValid;
}

 function doValidationsMessages(url, fieldRestrictions, formName){
	var bValid = true;
	var objX;
	putMessage('');	
	for(var i=0; i < fieldRestrictions.length; i++){
		objX = eval("document." + formName + "." + fieldRestrictions[i].field);		
		// When the object is select
		if(objX.type == "select-one"){
			// We must get the text of the selected option
			bValid &= fieldRestrictions[i].validate(objX.options[objX.selectedIndex].text);
		}else{
			bValid &= fieldRestrictions[i].validate(objX.value);	
		}				
	}	
	// Show the error message of do submit.
	if(!bValid){
		show('error');
		hidden('noerror');
	}else{
		eval( 'document.' + formName).action = url;
		eval( 'document.' + formName + '.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,pPattern,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,pPattern,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,pPattern,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,pPattern,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,pPattern,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;
}
/*
 * 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 DateField(pField, pValue,pMessage){
	this.field = pField;
	this.pattern = '^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$';
	this.message = pMessage;
	this.validate = ValidacionTest;
} 
