/*Favor comentar cada procion de codigo que se agrege, cada metodo o funcion*/



/*----------------------------------------------------------*/

function validador(fo){
	this.errors=new Array();
	this.controles=new Array();
	this.nombresParaControlesDelCarro=new Array();
	this.carritoDeCompras=false;
	this.formulario=fo;
	
	
}

/****************************				PARA EL CARRITO					**************************************
	LA SIGUIENTE FUNCION AGREGA LOS CAMPOS OCULTOS NECESARIOS PARA HACER LA VALIDACION DESDE PHP
	EJEMPLO
		cart[0]: control hidden	value:'ENT+' (entero positivo) -------------------->apunta al PRIMER control requerido cuya validacion debe ser ENTERO POSITIVO
		cart[1]: control hidden	value:'FECHA' (entero positivo) -------------------->apunta al SEGUNDO  control requerido cuya validacion debe ser FECHA
		
									nombres deseados para los campos cuando
									se muestre el mensaje de error.
		nombresCart[0]: control hidden	value:'nombre de cliente' (entero positivo) -------------------->apunta al PRIMER control requerido cuya validacion debe ser ENTERO POSITIVO
		nombresCart[1]: control hidden	value:'tarjeta de credito' (entero positivo) -------------------->apunta al SEGUNDO  control requerido cuya validacion debe ser FECHA
	agregado por ismael	Para carrito de Compra
*/
	
validador.prototype.ponerCamposOcultosParaCarro=function(){
	if (this.errores()){return false;}
	for(var i=0; i<this.controles.length; i++){
		var ctrl=document.createElement('input');
		ctrl.type="hidden";
		ctrl.name='cart[]';
		ctrl.value=this.controles[i].getAttribute('validacion');
		this.formulario.appendChild(ctrl);
		/* se agreagan los nombres desados para los campos */
		var ctrl2=document.createElement('input');
		ctrl2.type="hidden";
		ctrl2.name='nombresCart[]';
		ctrl2.value=this.nombresParaControlesDelCarro[i];
		this.formulario.appendChild(ctrl2);
		
		var ctrl3=document.createElement('input');
		ctrl3.type="hidden";
		ctrl3.name='nombresInputsCart[]';
		ctrl3.value=this.controles[i].name;
		this.formulario.appendChild(ctrl3);
		
	}
}

validador.prototype.setearNombresDeseadosParaCarro=function(nombres){
	if (this.errores()){return false;}
	this.nombresParaControlesDelCarro=nombres;
}
validador.prototype.esParaCarrito=function(boleano){
	this.carritoDeCompras=boleano;
}

validador.prototype.errores=function(){
	if (this.errores.length>0){
		return true;
	}else{
		return false;
	}
}

validador.prototype.validarINPUT=function(control){
	if (this.errores()){return false;}
	if(control.type.toUpperCase()=='TEXT' || control.type.toUpperCase()=='PASSWORD'){
		switch(control.getAttribute('validacion').toUpperCase()){
		case 'FECHA':
			retorno = false;
			var itm = control.value;
			if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) return true;
			if (itm.match(/^\d\d\d\d[\/-]\d\d[\/-]\d\d$/)) return true;
			if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) return true;
			return retorno;
			break;
		case 'ENT+':
			if (!isNaN(parseInt(control.value))){
				control.value=parseInt(control.value);
				if (parseInt(control.value)>0){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
			break;
		case 'ENT-':
			if (!isNaN(parseInt(control.value))){
				control.value=parseInt(control.value);
				if (parseInt(control.value)<0){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
			break;
		case 'DECIMAL':
			if (!isNaN(parseFloat(control.value))){
				control.value=parseFloat(control.value).toFixed(2);
				return true;
			}else{
				return false;
			}
			break;
		case 'TEXTO':
			if (control.value.length>0){
				return true;
			}else{
				var msj = document.getElementById('msj').innerHTML='Por favor complete los campos marcados en rojo.';
				return false;
			}
			break;
			//Funcion para cotizador residencial en Scotia seguros 
		case 'CONTENIDO':
		
			if (control.value.length>0){
				var contenido=control.value.replace(",", "")	;
				var construcciones = document.getElementById('Construcciones').value;
				
				var total;
				total = parseFloat(construcciones.replace(",", "")) * 0.3;
				
				if(contenido <= total){					
					return true;
				}else{
					alert('La cantidad debe ser menor');
					return false;
				}
			}else{
				return true;
			}			
			break;
		case 'EMAIL':
			
			var validateEmail = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/.
			test(control.value);
			if(validateEmail) {
				return true;
			}	else		{
				return false;
			}
			break;
		case 'ANHO':
			fecha = new Date();
			anho = fecha.getFullYear();
			if(!isNaN(control.value)){
				if ((control.value<(anho-10))||(control.value>(anho+1))){
					alert('El vehículo no puede tener una antigüedad mayor a 10 años al año actual');
					return false;
					
				}else{
					return true;
				}
			}
			else{
				return false;
			}
			break;
		}
	}else{
		
		return false;
	}
}

validador.prototype.ponerEstilosNormales=function(){
		if (this.errores()){return false;}
		for (var i=0; i<this.controles.length; i++){
			if (this.controles[i].className){
				if(this.controles[i].className.substring(this.controles[i].className.length-3, this.controles[i].className.length).toUpperCase()=="BAD"){
					Style=this.controles[i].className.substring(0, this.controles[i].className.length-3);
				}else{
					Style=this.controles[i].className;
				}
				this.controles[i].className=Style;
				
			}
		}
	
}

validador.prototype.validarSELECT=function(control){
	if (this.errores()){return false;}
	if (control.value==0){
		return false;
	}else{
		return true;
	}
}
validador.prototype.validarTEXTAREA=function(control){
	if (this.errores()){return false;}
	switch(control.getAttribute('validacion').toUpperCase()){
		case 'MENSAJE':
				if(control.value.length>5){
					return true;
				}else{
					return false;
				}
			break;//por gusto
		case 'TEXTO':
			if(control.value.length>0){
				return true;
			}else{
				return false;
			}
			break; //por gusto
		default: 
			return false;
			break; // por gusto
		
	}
}
validador.prototype.validarControl=function(control){
	if (this.errores()){return false;}
	switch(control.nodeName.toUpperCase()){
		case 'INPUT':
			return this.validarINPUT(control);
			break;
		case 'SELECT':
			return this.validarSELECT(control);
			break;
		case 'TEXTAREA':
			return this.validarTEXTAREA(control);
			break;
		default:
			return false;
			break;
	}
}
validador.prototype.validar=function(){
	try{
		if(this.formulario){ 
			for(var i=0; i<this.formulario.elements.length; i++){
				if (this.formulario.elements[i].getAttribute('req')){
					if (this.formulario.elements[i].getAttribute('req')=='1'){
						if(this.formulario.elements[i].getAttribute('validacion')){
							this.controles.push(this.formulario.elements[i]);
						}
					}
				}
			}
		}else{
			this.errors.push('sin formulario');
		}
	}catch(e){
		this.errors.push(e);
	}
	if (this.errores()){return false;}
	var retorno=true;
	var control=null;
	this.ponerEstilosNormales();
	for (var i=0; i<this.controles.length; i++){
		if (this.controles[i].className){
			if(this.controles[i].className.substring(this.controles[i].className.length-3, this.controles[i].className.length).toUpperCase()=="BAD"){
				Style=this.controles[i].className.substring(0, this.controles[i].className.length-3);
			}else{
				Style=this.controles[i].className;
			}
			BADStyle=Style+"BAD";
		}
		
		if (!this.validarControl(this.controles[i])){
			//this.controles[i].style.border="solid 1px #ff0000";
			this.controles[i].className=BADStyle;
			retorno=false;
			if (control==null){
				control=this.controles[i];
			}
		}
		
		
	}
	if (control!=null){
		control.focus();
	}
	if (retorno){
		if (this.carritoDeCompras)
			{	this.ponerCamposOcultosParaCarro()		};
	}
	return retorno;
}


