//-------------------------------------------------
// ValidateFunctions.js
// Libreria de funciones para validación de datos
//
// CTsoluciones copyright 2001
//-------------------------------------------------

String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

var validateForm=new validateFunctions();
function validateFunctions() {
	this.sMsg = "";
	
	this.sAlpha			= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	this.sAlphaExt		= this.sAlpha + "ÁÉÍÓÚÑÄËÏÖÜÂÊÎÔÛ áéíóúñäëïöüâêîôû .";
	this.sAlphaTE		= "()[] +-";
	this.sAlphaSymbol	= "() +-/\\*# ?¿!·$%&/()={}[]_.:;,";
	this.sCRLF			= "\r\n";
	this.sSpace			= " ";
	this.sAt				= "@";
	this.sDot				= ".";
	this.sDash			= "/";
	this.sNumeral		= "#";
	this.sUnderscore	= "_";
	this.sNumeric		= "1234567890";
	this.sNumericExt	= this.sNumeric + ".,+-/*()";
	this.sNumericTE		= this.sNumeric + "()[] +-";
	this.sURL				= this.sAlpha + "ÁÉÍÓÚÑÄËÏÖÜÂÊÎÔÛáéíóúñäëïöüâêîôû" + this.sNumeric + "/:._-\?&%$=~ ";
	this.sAlphaEMail	= this.sAlpha + this.sNumeric + "@.-_";
	this.sEsimo			= "º";    
	this.sHyphen			= "-";
	this.sComa			= ",";
	this.sTwoPoints		= ":";
	this.sDate			= this.sNumeric + this.sDash;
	this.sDesc			= this.sAlphaExt + this.sNumericExt + this.sAlphaSymbol;
	this.sFileName		= this.sAlpha + this.sNumeric + ":\ /._-()[]";
	
	this.ValidateURL = function (str) {
		var v = new RegExp(); 
		v.compile("^([A-Za-z]+://+)[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
		if (!v.test(str)) {
			return false;
		}
		return true;
	}

	this.ValidateString = function (theField, checkOK) {
	  var checkStr = theField.value;
	  var allValid = true;
	  for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		  if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length) {
		  allValid = false;
		  break;
		}
	  }
	  if (!allValid) {
		return (false);
	  } else {
		return (true);
	  }
	}
	
	this.ValidateEmail = function (theField) {
	  var checkOK = this.sAlphaEMail;
	  var checkStr = theField.value;
	  var allValid = true;
	  var ExisteAt = false;
	
	  for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		if (ch == "@") ExisteAt = true;
		for (j = 0;  j < checkOK.length;  j++) {
		  if (ch == checkOK.charAt(j))
			break;
		}
		if (j == checkOK.length) {
		  allValid = false;
		  break;
		}
	  }
	  
	  if ((!allValid) || (!ExisteAt)) {
		return (false);
	  } else {
		return (true);
	  }
	}
	
	this.ValidateRequired = function (theField) {
	  var checkStr = theField.value;
	  checkStr=checkStr.trim();
	  if (checkStr.length == 0) {
		return (false);
	  } else {
		return (true);
	  }
	}
	
	this.ValidateCmbRequired = function (theCmbField) {
	  var checkFlag = true;
	  
	  if (theCmbField.selectedIndex >= 0) {
		 var checkStr = theCmbField.options[theCmbField.selectedIndex].value;
		 if (checkStr.length == 0)  checkFlag = false;
	  } else {
		checkFlag = false;
	  }
	  
	  if (!checkFlag) {
		return (false);
	  } else {
		return (true);
	  }
	}
	
	this.GetSelectedCmb = function ( theCmb ) {
		if (theCmb.selectedIndex >= 0) {
			return(theCmb.options[theCmb.selectedIndex].value);
		} else {
			return(false);
		}
	}
	
	this.ValidateRadioRequired = function ( theRadioField ) {
		var checkFlag = false;
	
		for (var i=0; i<theRadioField.length;  i++) {
			if (theRadioField[i].checked) {
				checkFlag = true;
				break;			
			}
		}
	  
		if (!checkFlag) {
			return (false);
		} else {
			return (true);
		}
	}
	
	this.GetSelectedRadio = function ( theRadioField ) {
		var value = '';
		
		for (var i=0; i<theRadioField.length;  i++) {
			if (theRadioField[i].checked) {
				value = theRadioField[i].value;
				break;			
			}
		}
		
		return(value);
	}
	
	this.ValidateCheckboxRequired = function ( theCheckboxField ) {
		if (!theCheckboxField.checked) {
			return (false);
		} else {
			return (true);
		}
	}
	
	this.GetSelectedCheckbox = function ( theCheckboxField ) {
		return(theCheckboxField.value);
	}
	
	this.ValidateLen = function (theField, nMinLen, nMaxLen) {
	  var checkStr = theField.value.trim();
	
	  if (checkStr.length < nMinLen) {
		return (false);
	  } else {
		  if (checkStr.length > nMaxLen) {
			return (false);
		  } else {
			return (true);
		  }
	  }
	}
	
	this.ValidateRange = function (theField, nMinValue, nMaxValue) {
	  if (theField.value < nMinValue) {
		return (false);
	  } else {
		  if (theField.value > nMaxValue) {
			return (false);
		  } else {
			return (true);
		  }
	  }
	}
	
	this.DateCompare = function (strDate1, strDate2) {
		intDia1 = parseInt(strDate1.substring(0,2)) ;
		intDia2 = parseInt(strDate2.substring(0,2)) ;
		intMes1 = parseInt(strDate1.substring(3,5)) ;
		intMes2 = parseInt(strDate2.substring(3,5)) ;
		intAnio1 = parseInt(strDate1.substring(6,10)) ;
		intAnio2 = parseInt(strDate2.substring(6,10)) ;
	
		if (intAnio1>intAnio2)
			return(false);
	
		if (intAnio1==intAnio2) {
			if (intMes1>intMes2)
				return (false);
			if (intMes1==intMes2) {
				if (intDia1>intDia2)
					return (false);
			}
		}
	
		return (true);
	}

	this.ValidateDateRange = function (theField1, theField2) {
		if (! this.StrValidateDate( theField1.value )) {
			this.sMsg = "La fecha 1 a comparar no es válida, " + sMsg;
			return (false);
		}
		if (! this.StrValidateDate( theField2.value )) {
			this.sMsg = "La fecha 2 a comparar no es válida, " + sMsg;
			return (false);
		}
		
		// Las fecha poseen formato correcto
		// Analizo su contenido
		
		// Separo los componentes de dd/mm/aaaa
		// Parseo valores de dia, mes, año
		var sDate1 = new String( theField1.value );
		var iPos1  = sDate1.indexOf('/');
		var iPos2  = sDate1.indexOf('/',iPos1+1);
		
		var iDay1   = parseFloat( sDate1.substring(0,iPos1) ) ;
		var iMonth1 = parseFloat( sDate1.substring(iPos1+1,iPos2) ) ;
		var iYear1  = parseInt( sDate1.substring(iPos2+1,sDate1.length) );
		
		var sDate2 = new String( theField2.value );
		var iPos1  = sDate2.indexOf('/');
		var iPos2  = sDate2.indexOf('/',iPos1+1);
		
		var iDay2   = parseFloat( sDate2.substring(0,iPos1) ) ;
		var iMonth2 = parseFloat( sDate2.substring(iPos1+1,iPos2) ) ;
		var iYear2  = parseInt( sDate2.substring(iPos2+1,sDate2.length) );
		
		
		// Comparo elementos de fecha
		if (iYear1>iYear2) return(false);
	
		if (iYear1==iYear2) {
			if (iMonth1>iMonth2) return (false);
			if (iMonth1==iMonth2) {
				if (iDay1>iDay2) return (false);
			}
		}
	
		return (true);
	}
	
	this.ValidateDate = function (theField) {
		var bReturn = this.StrValidateDate( theField.value );
		return (bReturn);
	}
	
	this.ValidateFormatDate = function (theField) {
		var bReturn = this.StrValidateDate( theField.value );
		if (bReturn) {
			// Separo los componentes de dd/mm/aaaa
			// Parseo valores de dia, mes, año
			var sDate = new String( theField.value );
			var iPos1 = sDate.indexOf('/');
			var iPos2 = sDate.indexOf('/',iPos1+1);
		
			var sDay   = '0' + sDate.substring(0,iPos1);
			var sMonth = '0' + sDate.substring(iPos1+1,iPos2);
			var sYear  = sDate.substring(iPos2+1,sDate.length);
			
			theField.value = sDay.substring(sDay.length-2,sDay.length) + "/" +
							 sMonth.substring(sMonth.length-2,sMonth.length) + "/" +
							 sYear;
		}
		return (bReturn);
	}
	
	this.ValidateDate2 = function(strDate) {
		// Parsea global de la fecha: formato (dd/mm/aaaa)
		this.sMsg = " debe poseer un formato (dd/mm/aaaa)";
	
		// Valida tamaño de la fecha.
		intFecLen = strDate.length;
		if(intFecLen < 10) return false;
		
		// Valida que el tercer caracter sea una /.
		strBarra_dia = strDate.substring(3,2);
		if(strBarra_dia != '/') return false;
	
		// Valida que el sexto caracter sea una /.
		strBarra_mes = strDate.substring(6,5);
		if(strBarra_mes != '/')  return false;
		
		// Valida que no halla / en el dia.
		strDia = strDate.substring(0,2);
		if(! IsNumber(strDia)) return false;
		
		// Valida que no halla / en el mes.
		strMes = strDate.substring(3,5);
		if(! IsNumber(strMes)) return false;
		
		// Valida que no halla / en el año.
		strAnio = strDate.substring(6,10);
		if(! IsNumber(strAnio)) return false;
		
		// Parsea el dia.
		intDia = parseFloat(strDate.substring(0,2)) ;
		
		// Parsea el mes.
		intMes = parseFloat(strDate.substring(3,5)) ;
	
		// Parsea el año.
		intAnio = parseInt(strDate.substring(6,10));
		
		// Se fija si es una fecha válida
		// Anio es valido siempre que sea positivo
		sMsg = "debe poseer un año válido.";
		if (intAnio < 1) return false; 
		
		sMsg = "debe poseer un mes válido.";
		if (intMes < 1) return false;
		
		sMsg = "debe poseer un día válido.";
		if (intDia < 1) return false;
		
		   
		sMsg = "debe poseer un mes válido.";
		if (intMes > 12) return false;
		
		sMsg = "debe poseer un día válido.";
		
		if (intDia > 31) return false;
		
		if (intMes==2) {
		   //Se fija si el anio es biciesto
			  if ( !(intAnio % 4) && ( intAnio % 100 || ! (intAnio % 400)))
			  { // Bisiesto
				if (intDia > 29) return false;
			  } else {
				if (intDia > 28) return false;
			  }           
		}
		
		if (intMes==4) {
		   if (intDia > 30) return false;
		}           
		
		if (intMes==6) {
		   if (intDia > 30) return false;
		}           
	
		if (intMes==9) {
		   if (intDia > 30) return false;
		}           
			
		if (intMes==11) {
		   if (intDia > 30) return false;
		}           
		
		return true;
	}
	
	this.StrValidateDate = function (strDate) {
		// Parseo global de la fecha: formato (dd/mm/aaaa)
		var sFormatMsg = "debe poseer un formato (dd/mm/aaaa)";
	
		// Separo los componentes de dd/mm/aaaa
		var sDate = new String( strDate );
		var iPos1 = sDate.indexOf('/');
		var iPos2 = sDate.indexOf('/',iPos1+1);
		
		var sDay   = sDate.substring(0,iPos1);
		var sMonth = sDate.substring(iPos1+1,iPos2);
		var sYear  = sDate.substring(iPos2+1,sDate.length);
		
		
		// Valido contenidos numéricos en dia, mes, año
		if(! this.IsNumber(sDay)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un día con contenido numérico.";
			return(false);
		}
		if(! this.IsNumber(sMonth)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un mes con contenido numérico.";
			return(false);
		}
		if(! this.IsNumber(sYear)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un año con contenido numérico.";
			return(false);
		}
		
		// Parseo valores de dia, mes, año
		var iDay   = parseFloat(sDay) ;
		var iMonth = parseFloat(sMonth) ;
		var iYear  = parseInt(sYear);
		
		// Controlo contenidos de dia, mes, año
		if((iDay<1) || (iDay>31)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un día válido (1..31)";
			return(false);
		}
		if((iMonth<1) || (iMonth>12)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un mes válido (1..12)";
			return(false);
		}
		if((iYear<1900) || (iYear>3000)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un año válido (1900..)";
			return(false);
		}
		
		// Controlo dia segun el mes
		if (iMonth==2) {
		   //Se fija si el anio es biciesto
		   if ( !(iYear % 4) && ( iYear % 100 || ! (iYear % 400))) { // Bisiesto
				if (iDay > 29) {
					this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un día válido.";
					return(false);
				}
		   } else {
				if (iDay > 28) {
					this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un día válido.";
					return(false);
				}
		   }
		}
		
		if ((iMonth==4) && (iDay > 30)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un día válido.";
			return(false);
		}
		if ((iMonth==6) && (iDay > 30)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un día válido.";
			return(false);
		}
		if ((iMonth==9) && (iDay > 30)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un día válido.";
			return(false);
		}
		if ((iMonth==11) && (iDay > 30)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un día válido.";
			return(false);
		}
		
		return(true);
	}
	
	this.IsNumber = function (theField) {
	  var checkOK = "0123456789";
	  var checkStr = theField;
	  var allValid = true;
	  for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		  if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length) {
		  allValid = false;
		  break;
		}
	  }
	  if (!allValid) {
		return (false);
	  } else {
		return (true);
	  }
	}
	
	this.ValidateFechaNac = function(strDate) {
		var FechaActual = new Date();
		FechaActual.getDate();
		intAnio = FechaActual.getYear();
	
		// Valida que no halla / en el año.
		strAnio = strDate.substring(6,10);
	
		if ((intAnio - (parseInt(strAnio) - 1900)) < 5) {
		  sMsg = "debe poseer más de 5 años.";
		  return false;
		}
	
		return true;
	}
	
	this.ValidateHHMM = function (theField) {
		// Parseo global de la hora: formato (hh:mm)
		var sFormatMsg = "debe poseer un formato (hh:mm)";
	
		// Separo los componentes de hh:mm
		// Parseo valores de hh, mm
		var sHHMM = new String( theField.value );
		var iPos1 = parseFloat(sHHMM.indexOf(':'));
		
		if (iPos1==-1) {
			var sHora  = '0' + sHHMM;
			var sMin   = '00';
		} else {
			var sHora  = '0' + sHHMM.substring(0,iPos1);
			var sMin   = '0' + sHHMM.substring(iPos1+1,sHHMM.length);
		}
		
		// Valido contenidos numéricos en hora, minuto
		if(! this.IsNumber(sHora)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer una hora con contenido numérico.";
			return(false);
		}
		if(! this.IsNumber(sMin)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un minuto con contenido numérico.";
			return(false);
		}
			
		// Parseo valores de hora, minuto
		var iHora   = parseFloat(sHora) ;
		var iMin = parseFloat(sMin) ;
		
		// Controlo contenidos de hora, minuto
		if((iHora<0) || (iHora>23)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer una hora válida (0..23)";
			return(false);
		}
		if((iMin<0) || (iMin>59)) {
			this.sMsg = this.sMsg + sFormatMsg + ", debe poseer un minuto válida (0..59)";
			return(false);
		}
		
		// Reajusto el valo anteponiendole ceros	
		sHora  = '0' + sHora;
		sMin   = '0' + sMin;
		
		theField.value = sHora.substring(sHora.length-2,sHora.length) + ":" +
						 sMin.substring(sMin.length-2,sMin.length);
							 
		return (true);
	}
	
	this.recorta = function( pCadena, nLargo, cFinal ){
		var retorna;
		retorna = "";
		
		nLargo--;
		
		if ( pCadena.length > nLargo )
		{
			retorna = pCadena.substring(0,nLargo) + cFinal;
		}else{
			retorna = pCadena;
		}
		
		return(retorna);
	}

}