// VALIDATION.JS



var CHECK_FOR_LENGTH	= 0x01 ;
var CHECK_FOR_NUMERIC	= 0x02 ;
var CHECK_FOR_EMAIL		= 0x04 ;
var CHECK_FOR_VALIDNAME	= 0x08 ;
var CHECK_FOR_DATE		= 0x10 ;
var CHECK_FOR_FILENAME	= 0x20 ;
var ALLOW_BLANK			= 0x40 ;
var CHECK_FOR_BOOLEAN	= 0x80 ;
var CHECK_FOR_TIME		= 0x100 ;


var Controlar = new Array() ;
var Fechas_Falsas = new Array(5) ;
Fechas_Falsas[0] = "3002"
Fechas_Falsas[1] = "3102"  
Fechas_Falsas[2] = "3104"
Fechas_Falsas[3] = "3106"
Fechas_Falsas[4] = "3109"
Fechas_Falsas[5] = "3111"

// Constructor

function ElementoDeControl( field, options, min, max, msg )

{

	this.field	= field ;

	this.options= options ;

	this.min	= min ;

	this.max	= max ;



	if ( ! msg || msg.length == 0 )

		this.msg = new String ( "El valor de '" + field.name + "'\n no es válido")

	else

		this.msg	= msg ;

}



function ValidateField( obj )

{

with ( obj )

{

	if ( options & ALLOW_BLANK )

	{

		if ( obj.field.value.length == 0 )

			return false ;

	}

	

	if ( options & CHECK_FOR_LENGTH )

	{

		if ( CheckForLength (obj) )

			return 1 ;

	}



	if ( options & CHECK_FOR_EMAIL )

	{

		if (CheckForEMail (obj)) 

			return 1;

	}



	if ( options & CHECK_FOR_NUMERIC )

	{

		if (CheckForNumeric (obj))

			return 1;

	}



	if ( options & CHECK_FOR_DATE )

	{

		if (CheckForDate (obj))

			return 1;

	}

	

	if ( options & CHECK_FOR_VALIDNAME )

	{

		if (CheckForValidName (obj))

			return 1;

	}		



	if ( options & CHECK_FOR_FILENAME )

	{

		if (CheckForFileName (obj))

			return 1;

	}		



	if ( options & CHECK_FOR_BOOLEAN )

	{

		if (CheckForBoolean (obj))

			return 1;

	}	

	

	if ( options & CHECK_FOR_TIME )

	{

		if (CheckForTime (obj))

			return 1;

	}	



}

return false ;

}



function ValidateAllFields ()

{

	var xxx ;

	for ( xxx = 0 ; xxx < Controlar.length ; xxx++ )

	{

		if ( ValidateField ( Controlar[xxx] ) ) 

		{

			MensajeError ( Controlar[xxx], xxx ) ;

			return true ;

		}

	}

	return false ;

}



// Mensaje de error

function MensajeError ( obj, index )

{

	var prevObj ;



	alert ( obj.msg ) ;



	if ( obj.field.type == "hidden" )

	{

		for ( obj = Controlar[index-1] ; index && obj.field.type == "hidden" ; index-- ) ;

		if ( obj.field.type == "hidden" )

			obj= null ;

	}



	if ( obj )

		obj.field.focus() ;

}



function AddValidationItem (field, CHECK, min, max, msg)

{

	var xxx = Controlar.length ;

	Controlar[xxx] = new ElementoDeControl(field, CHECK, min, max, msg) ;



}



function ResetValidationItems ()

{

	var xxx ;

	for ( xxx = 0 ; xxx < Controlar.length ; xxx++ )

		delete Controlar[xxx];

	Controlar.length = 0;

}



function CheckForLength ( obj )

{

	var text ;



	if ( obj & CHECK_FOR_NUMERIC )

		return false ; // incompatible options



	text = doTrim ( obj.field.value ) ;



	with ( obj )

	{

		if ( min != -1 )

		{

			if ( text.length < min )

				return true ;

		}

		if ( max != -1 )

		{

			if ( text.length > max)

				return true ;

		}

	}

	return false ;

}



function CheckForEMail (obj)

{

	var str			= obj.field.value ;

	var narrobas	= 0

	var npuntos		= 0 

	

	str = str.toLowerCase()

	index = str.indexOf ('@', 0)



	if ( index < 1)

	{

		return 1;

	}

	

	else if (str.indexOf("..",0) != -1 )

	{

		return 1 ;

	}

	else

	{

		nombre = str.substring(0,index)

		host = str.substring(index+1,str.length)

		if ( host.indexOf ('.', 0) <= 0 || nombre.indexOf('.', 0) == 0 )

		{

			return 1;

		}

		

		if ( nombre.charAt(nombre.length-1) == '.' || host.charAt(host.length-1) == '.' )

		{

			return 1;

		}

		

		// Busca caracteres invalidos en el nombre del email

		for( xxx = 0 ; xxx < nombre.length ; xxx++ )

		{

			var chrCode = nombre.charCodeAt(xxx) ;

			test =	checkRange(chrCode,45,46) ||

					checkRange(chrCode,48,57) || 

					checkRange(chrCode,95,95) || 

					checkRange(chrCode,97,122) || 

					checkRange(chrCode,126,126);

			if (!test)

				return 1;

		}



		// Busca caracteres invalidos en el nombre del host

		for( xxx = 0 ; xxx < host.length ; xxx++ )

		{

			var chrCode = host.charCodeAt(xxx) ;

			test =	checkRange(chrCode,45,46) ||

					checkRange(chrCode,48,57) || 

					checkRange(chrCode,95,95) || 

					checkRange(chrCode,97,122) || 

					checkRange(chrCode,126,126);

			if (!test)

				return 1;

		}

	}

	

	return false ;

}





function checkRange (value,min,max)

{

	return (value >= min && value <= max);



}



function CheckForNumeric (obj)

{

	with(obj){



		if ( field.value.length == 0 || isNaN(field.value) )

		{

		return 1 ;

		}

		if ( min != -1 )

		{

			if ( parseFloat(field.value) < min )

				return true ;

		}

		if ( max != -1 )

		{

			if ( parseFloat(field.value) > max )

				return true ;

		}

		

		//if( parseFloat(field.value) < min || parseFloat(field.value) > max )

		//return 1 ;

	}

	return false ;

}



function CheckForBoolean (obj)

{

	with(obj){



		if( parseFloat(field.value) != min )

		return 1 ;

	}

	return false ;

}



function CheckForDate (obj)

{

	with(obj){

		var day = parseInt(obj.field.value.substring(0,2),10) ;

		var month = parseInt(obj.field.value.substring(2,4),10) ;

		var year = parseInt(obj.field.value.substring(4),10) ;

		 

		if ( obj.field.value.length != 8 || isNaN(field.value) )

			return 1 ;



		if ( day > 31 || day <= 0 )

			return 1 ;

							

		if ( month > 12 || month <= 0 )

			return 1 ;

			

		if ( year <= 0 )

			return 1 ;

			

		for( xxx = 0 ; xxx < Fechas_Falsas.length ; xxx++ )

		{	

			if ( Fechas_Falsas[xxx].substring(0,2) == day && 

				Fechas_Falsas[xxx].substring(2,4) == month )

				return 1 ;

		}

	

		if( day == 29 && month == 2 && !checkBisiesto(year) )

			return 1 ;

	}

	return false ;

}	



function checkBisiesto(year)

{

	return ( year%4 == 0 ) ;

}



function CheckForValidName (obj)

{



	var str = obj.field.value ;



	if ( obj.field.value.length == 0 )

	return 1 ;



	for( xxx = 0 ; xxx < str.length ; xxx++ )

	{

		var chrCode = str.charAt(xxx).charCodeAt() ;

		if ( (chrCode < 48 && str.charAt(xxx) != '-' && str.charAt(xxx) != '.') || 

			 (chrCode > 57 && chrCode < 65) || 

			 (chrCode > 90 && chrCode < 97 && str.charAt(xxx) != '_') ||

			  chrCode > 122 )

			return 1 ;

			

	}

	return false ;

}



function CheckForFileName (obj)

{



	if ( obj.field.value.length == 0 )

	return 1 ;



	var str = obj.field.value ;

	

	if ( str.indexOf('"',0) != -1 || str.indexOf('*',0) != -1 || 

		 str.indexOf(':',0) != -1 || str.indexOf('/',0) != -1 || 

		 str.indexOf('<',0) != -1 || str.indexOf('>',0) != -1 ||

		 str.indexOf('?',0) != -1 || str.indexOf('|',0) != -1 ||

		 str.indexOf('\\',0) != -1 )

		 return 1;

	

	for( xxx = 0 ; xxx < str.length ; xxx++ )

	{

		var chrCode = str.charAt(xxx).charCodeAt() ;

	

		if (chrCode < 32 && chrCode > 126 )

			return 1 ;

	}

	return false ;

}



function CheckForTime (obj)

{

	with(obj){



		var horas, minutos, segundos

		var str = obj.field.value ;

		

		index = str.indexOf (':', 0 )

		if ( index < 1) return 1;

		

		horas = str.substring(0,index)

		index2 = str.indexOf (':', index+1 )

		

		if ( index2 == -1 ) {

			minutos = str.substring(index+1,str.length)

		}

		else

		{	

			minutos = str.substring(index+1,index2)

			segundos = str.substring(index2+1,str.length)

		}	

		

		if ( horas.length == 0 || parseInt(horas) > 12 || parseInt(horas) < 1)

			return true ;



		if ( minutos.length != 2 || parseInt(minutos) > 59 || parseInt(minutos) < 0)

			return true ;

		

		if ( parseInt(segundos) > 59 || parseInt(segundos) < 0)

			return true ;		

		

		if ( index2 > 0 && segundos.length != 2 )

			return true ;		

			

	

	}

	return false ;

}



function doTrim( str )

{

	var begin, end, retStr, length, index ;

	

	length = str.length ;



	for( index = 0 ; index < length && str.charAt(index)==" "; index++ ) ;

	

	if ( index == str.length )

		length = 0 ;

	else

		begin = index ;



	for( index = length-1 ; index >= 0 && str.charAt(index)==" "; index-- ) ;

	

	if ( index == -1 )

		retStr = "" ;

	else

		retStr = str.substring( begin, index + 1)



	return retStr ;

}