/**
 * Clase de validacion de formularios
 *
 * @author shoto
 * @version 5.1.9
 */

function Constraint(nom,tipo,fargs,msg){this.field=nom;this.tipo=tipo;this.args=fargs;this.msg=msg;}
function Validation(){}
Validation.prototype.obligatorio=function(control,args){var dato='';if(typeof(control)!=='string'){if(control.type){if((control.type==='text')||(control.type==='textarea')||(control.type==='file')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else if(control.type==='select-multiple'){var i;for(i=0;i<control.length;i++){if(control.options[i].selected){dato=dato+control.options[i].value;}}}else if(control.type==='checkbox'){if(control.checked){dato=1;}}else if(control.type==='radio'){if(control.checked){dato=1;}}}else{if(control[0].type==='radio'){var j=0;for(j=0;j<control.length;j++){if(control[j].checked){dato=1;}}}else{alert('Obligatorio: validation dont work with type of field form.');return;}}}else{dato=control;}
if(dato===''){return false;}else{return true;}};Validation.prototype.email=function(control,args){var dato;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('Email: validation dont work with type of field form.');return;}}else{dato=control;}
if(dato!==''){var expr=/^[_\.0-9a-zA-Z\-]+@([0-9a-zA-Z][0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}$/;var found=expr.exec(dato);if(!found){return false;}else{return true;}}else{return true;}};Validation.prototype.ip=function(control,args){var dato;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('IP: la validación no funciona con el tipo de campo de formulario.');return false;}}else{dato=control;}
if(dato!==''){var partes=dato.split('.');if(partes.length!==4){return false;}else{for(i=0;i<4;i++){var num=partes[i];if(num>255||num<0||num.length===0||isNaN(num)){return false;}}}}else{return false;}
return true;};Validation.prototype.checkTipoArchivo=function(control,args){var dato='';var extPer;if(typeof(control)!=='string'){if(control.type){if(control.type==='file'){dato=control.value;}else{alert('checkTipoArchivo: validation dont work with type of field form.');return;}}}else{dato=control;}
if(dato!==''){var ext=this._getExtension(dato);for(extPer in args){if(ext===args[extPer]){return true;}}
return false;}else{return true;}};Validation.prototype.rango=function(control,param){var args=[];args=param.split(",");var number;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='password')||(control.type==='hidden')){number=control.value;}else if(control.type==='select-one'){number=control.options[control.selectedIndex].value;}else{alert('Rango: validation dont work with type of field form.');return;}}else{number=control;}
if(number!==''){var num=parseFloat(number);if(isNaN(num)){return false;}
if((args[0]!=='')&&(args[1]!=='')){if((num>=parseFloat(args[0]))&&(num<=parseFloat(args[1]))){return true;}else{return false;}}else if((args[0]==='')&&(args[1]!=='')){if(num<=parseFloat(args[1])){return true;}else{return false;}}else if((args[0]!=='')&&(args[1]==='')){if(num>=parseFloat(args[0])){return true;}else{return false;}}}else{return true;}};Validation.prototype.alpha=function(control,args){var dato;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('alpha: validation dont work with type of field form.');return;}}else{dato=control;}
if(dato!==''){var expr=/[^a-zA-ZñÑáéíóúäëïöüÁÉÍÓÚÄËÏÖÜ]/;if(dato.match(expr)){return false;}else{return true;}}else{return true;}};Validation.prototype.alphanum=function(control,args){var dato;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('alphanum: validation dont work with this type of field form.');return;}}else{dato=control;}
if(dato!==''){var expr=/[^0-9a-zA-ZñÑáéíóúäëïöüÁÉÍÓÚÄËÏÖÜ]/;if(dato.match(expr)){return false;}else{return true;}}else{return true;}};Validation.prototype.texto=function(control,args){var dato;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='textarea')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('texto: validation dont work with this type of field form.');return;}}else{dato=control;}
if(dato!==''){var expr;if(control.type==='textarea'){expr=/[^0-9a-zA-ZñÑáéíóúäëïöüÁÉÍÓÚÄËÏÖÜ\¿\?\!\¡$%#@\-\'\.\ \n,]/;}else{expr=/[^0-9a-zA-ZñÑáéíóúäëïöüÁÉÍÓÚÄËÏÖÜ\¿\?\!\¡$%#@\-\'\.\ ,]/;}
if(dato.match(expr)){return false;}else{return true;}}else{return true;}};Validation.prototype.numero=function(control,args){var dato;var expr=/^[0-9]+$/;var found=false;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('numero: validation dont work with type of field form.');return;}}else{dato=control;}
if(dato!==''){if(args===''){dato=dato.replace(/\./g,'');found=expr.exec(dato);if(!found){return false;}else{return true;}}else{var params=args.split(',');if(params[2]===''){params[2]=',';}
var numero=dato.split(params[2]);numero[0]=numero[0].replace(/\./g,'');if(params.length!==3){alert('numero: number of parameters not valid.');return false;}
if((isNaN(parseInt(params[0],10)))||(isNaN(parseInt(params[1],10)))){alert('numero: not valid parameters.');return false;}
if(numero.length===1){found=expr.exec(numero[0]);if(!found){return false;}
if(numero[0].length<=parseInt(params[0],10)){return true;}else{return false;}}else{var found0=expr.exec(numero[0]);var found1=expr.exec(numero[1]);if((!found0)||(!found1)){return false;}
if((numero[0].length<=parseInt(params[0],10))&&(numero[1].length<=parseInt(params[1],10))){return true;}else{return false;}}}}else{return true;}};Validation.prototype.cmaximo=function(control,args){var dato;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='password')||(control.type==='hidden')||(control.type==='textarea')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('cmaximo: validation dont work with type of field form.');return;}}else{dato=control;}
if(dato!==''){var cant=parseInt(args,10);if(isNaN(cant)){alert('cmaximo: parameter is not valid.');return false;}
if(dato.length>cant){return false;}else{return true;}}else{return true;}};Validation.prototype.cminimo=function(control,args){var dato;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='password')||(control.type==='hidden')||(control.type==='textarea')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('cminimo: validation dont work with type of field form.');return;}}else{dato=control;}
if(dato!==''){var cant=parseInt(args,10);if(isNaN(cant)){alert('cminimo: parameter is not valid.');return false;}
if(dato.length<cant){return false;}else{return true;}}else{return true;}};Validation.prototype.regex=function(control,args){var dato;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='textarea')||(control.type==='file')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('regex: validation dont work with this type of field form.');return;}}else{dato=control;}
if(dato!==''){try{var expr=args;if(dato.match(expr)){return true;}}catch(e){alert('[lib_validator]Error: '+e);}
return false;}else{return true;}};Validation.prototype.fecha=function(control,args){var dato;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='textarea')||(control.type==='file')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('fecha: validation dont work with this type of field form.');return;}}else{dato=control;}
if(dato!==''){var reg='';var mifecha2='';if(args===''){reg=/(\d\d)\/(\d\d)\/(\d\d\d\d)$/i;mifecha2=dato.replace(reg,"$3/$2/$1");}else{try{reg=eval(args);}catch(e){alert('[lib_validator]Error: '+e);}
mifecha2=dato.replace(reg,"$3/$2/$1");}
var mifecha=new Date(mifecha2+' 12:00:00');var dia=mifecha.getDate();if(dia<10){dia='0'+dia;}
var mes=mifecha.getMonth()+1;if(mes<10){mes='0'+mes;}
var anio=mifecha.getFullYear();var newfecha=dia+'/'+mes+'/'+anio;if(dato!==newfecha){return false;}else{return true;}}else{return true;}};Validation.prototype.dontselect=function(control,args){var dato;if(typeof(control)!=='string'){if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('dontselect: validation dont work with type of field form.');return;}}else{dato=control;}
if(dato!==''){if(dato===args){return false;}else{return true;}}else{return true;}};Validation.prototype.rut=function(control,args){var dato;if(typeof(control)!=='string'){if((control.type==='text')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('rut: validation dont work with type of field form.');return;}}else{dato=control;}
if(dato!==''){if(!this._validate_rut(dato,args)){return false;}else{return true;}}else{return true;}};Validation.prototype.is=function(control,args){var dato='';if(typeof(control)!=='string'){if(control.type){if((control.type==='text')||(control.type==='textarea')||(control.type==='file')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else if(control.type==='checkbox'){if(control.checked){dato=1;}}}else{if(control[0].type==='radio'){var i=0;for(i=0;i<control.length;i++){if(control[i].checked){dato=1;}}}else{alert('is: validation dont work with type of field form.');return;}}}else{dato=control;}
if(dato!==args){return false;}else{return true;}};Validation.prototype.notIs=function(control,args){var dato='';if(typeof(control)!=='string'){if(control.type){if((control.type==='text')||(control.type==='textarea')||(control.type==='file')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else if(control.type==='checkbox'){if(control.checked){dato=1;}}}else{if(control[0].type==='radio'){var i=0;for(i=0;i<control.length;i++){if(control[i].checked){dato=1;}}}else{alert('notIs: validation dont work with type of field form.');return;}}}else{dato=control;}
if(dato===args){return false;}else{return true;}};Validation.prototype.url=function(control,args){var dato;if((control.type==='text')||(control.type==='password')||(control.type==='hidden')){dato=control.value;}else if(control.type==='select-one'){dato=control.options[control.selectedIndex].value;}else{alert('URL: validation dont work with type of field form.');return;}
if(dato!==''){var expr=/^https?:\/\/[a-zA-Z\-\_\.0-9\/]+$/;var found=expr.exec(dato);if(!found){return false;}else{return true;}}else{return true;}};Validation.prototype._validate_rut=function(crut,args){var tmpstr="";var i,largo,dv,rut,suma,mul,res,dvi;if(args!==''){var elements_ar=document.getElementsByName(args);var obj_dv=elements_ar[0].value;crut=crut+String+obj_dv;}
for(i=0;i<crut.length;i++){if(crut.charAt(i)!==' '&&crut.charAt(i)!=='.'&&crut.charAt(i)!==','&&crut.charAt(i)!=='-'){tmpstr=tmpstr+crut.charAt(i);}}
crut=tmpstr;largo=crut.length;if(largo<2){return false;}
if(largo>2){rut=crut.substring(0,largo-1);}else{rut=crut.charAt(0);}
dv=crut.charAt(largo-1);this._checkCDV(dv);if(rut===null||dv===null){return false;}
var x=2;var s=0;for(i=rut.length-1;i>=0;i--){if(x>7){x=2;}
s+=rut[i]*x;x++;}
var dvr=11-(s%11);if(dvr==10){dvr='k';}
if(dvr==11){dvr='0';}
if(dv.toLowerCase()==dvr){return true;}else{return false;}};Validation.prototype._checkCDV=function(dvr){var dv=dvr+String;if(dv!=='0'&&dv!=='1'&&dv!=='2'&&dv!=='3'&&dv!=='4'&&dv!=='5'&&dv!=='6'&&dv!=='7'&&dv!=='8'&&dv!=='9'&&dv!=='k'&&dv!=='K'){return false;}
return true;};Validation.prototype._getExtension=function(filePath){var ext='';try{var patron=new RegExp(/\.([^.]+)$/);var res=patron.exec(filePath);ext=res[1];}catch(e){}
return ext;};function Validador(formname,mode,color){this.fname=formname;if((mode!=='one')&&(mode!=='acum')){alert('mode not valid in Validator constructor');return null;}
this.mode=mode;this.debug=false;this.debugElemID='debugConsole';if(color===''){color='ffb900';}
this.bgcolor=color;this.validations=[];}
Validador.prototype.setDebug=function(status){if(!this.fname){alert('Validator object not found');return;}
this.debug=status;return true;};Validador.prototype.addDebug=function(msg){if(!this.debug){return false;}
if(!this.fname){alert('Validator object not found');return;}
var objDebug=document.getElementById(this.debugElemID);if(objDebug){objDebug.innerHTML+=msg+'<br/>';}
return true;};Validador.prototype.addconstraint=function(control,fvalidation,fargs,errmsg){if(!this.fname){alert('Validator object not found');return;}
this.validations.push(new Constraint(control,fvalidation,fargs,errmsg));return true;};Validador.prototype.delconstraint=function(control,fvalidation){if(!this.fname){alert('Validator object not found');return;}
var erase=0;var validacion;for(validacion=0;validacion<this.validations.length;validacion++){if((this.validations[validacion].field===control)&&(this.validations[validacion].tipo===fvalidation)){this.validations[validacion].field='';this.validations[validacion].tipo='';this.validations[validacion].args='';this.validations[validacion].msg='';erase++;}}
if(erase!==0){return false;}
return true;};Validador.prototype.validar=function(){var validacion=0;var strerr='';var obj_elem='';for(validacion=0;validacion<this.validations.length;validacion++){if(this.validations[validacion].field===''){this.addDebug('validation['+validacion+'] ignored , attribute field empty.');continue;}
var objForm=this._getFormObj(this.fname);if(!objForm){this.addDebug('Form ['+this.fname+'] not found');alert('Form ['+this.fname+'] not found');return false;}
var elem=this.validations[validacion].field;if(!objForm.elements[elem]){this.addDebug('Field Form ['+this.validations[validacion].field+'] not found, assumes string ');obj_elem=elem;}else{obj_elem=objForm.elements[elem];}
this.addDebug(this.validations[validacion].tipo+'('+obj_elem.name+','+this.validations[validacion].args+')');var instanceValidation=new Validation();var result;try{result=eval('instanceValidation.'+this.validations[validacion].tipo+'(obj_elem,this.validations[validacion].args)');}catch(e){this.addDebug('[lib_validator]Error: '+e);alert('[lib_validator]Error: '+e);}
if(!result){if(this.mode==='acum'){strerr=strerr+' '+this.validations[validacion].msg+"\n";this.addDebug(this.validations[validacion].msg.constructor.toString());this._change_color(obj_elem,this.bgcolor);}else{this._executeAction(this.validations[validacion].msg);this._change_color(obj_elem,this.bgcolor);if(!this._isString(obj_elem)&&obj_elem.type!=='hidden'){if(obj_elem.type){obj_elem.focus();}else{obj_elem[0].focus();}}
return false;}}else{this._executeRedoAction(this.validations[validacion].msg);this._change_color(obj_elem,'');}}
if((this.mode==='acum')&&(strerr!=='')){alert(strerr);return false;}
return true;};Validador.prototype.send=function(){if(!document.forms[this.fname]){alert('Form ['+this.fname+'] not found');return false;}
document.forms[this.fname].submit();};Validador.prototype._getFormObj=function(formId){var objForm=null;try{objForm=document.getElementById(formId);if(!objForm){objForm=document.forms[formId];}}catch(e){}
return objForm;};Validador.prototype._change_color=function(obj,color){if(obj.style){obj.style.backgroundColor=color;}};Validador.prototype._isArray=function(obj){if(obj.constructor.toString().indexOf('Array')===-1){return false;}else{return true;}};Validador.prototype._isString=function(obj){if(obj.constructor===null||obj.constructor.toString().indexOf("String")===-1){return false;}else{return true;}};Validador.prototype._executeAction=function(actionObj){var mObj;this.addDebug(actionObj.constructor.toString());try{if(this._isArray(actionObj)){for(mObj in actionObj){actionObj[mObj].performAction();}}else{actionObj.performAction();}}catch(e){this.addDebug('_executeAction: performAction error'+e);alert(actionObj);}};Validador.prototype._executeRedoAction=function(actionObj){var mObj;this.addDebug(actionObj.constructor.toString());try{if(this._isArray(actionObj)){for(mObj in actionObj){actionObj[mObj].performRedoAction();}}else{actionObj.performRedoAction();}}catch(e){this.addDebug('_executeRedoAction: performRedoAction error'+e);}};String.prototype.performAction=function(){alert(this);};
