//Ajax validation
preLoadImagen1 = new Image();
preLoadImagen1.src = "img/proceso_compra/exclamacion_red.gif";

preLoadImagen2 = new Image();
preLoadImagen2.src = "img/proceso_compra/tick.gif";

	function validateFormInputField(element,form)
	 {

		var id = "";

		if (element.type == "radio"){
			id = (element.id).substr(0,(element.id).length-1)
		}else{
	   		id = element.id;

	   }
	   var command = form.id;
	   var value = element.value;
	   ValidationHelperJS.getInputFieldValidationMessage(id, value, command, {
	           callback:function(dataFromServer) {
	                   setInputFieldStatus(id, dataFromServer);
	           }
	   });

	 }

	function validateFormInputFieldFake(element,form) {

		var elementId = "";

		if (element.type == "radio"){
			elementId = (element.id).substr(0,(element.id).length-1);
		}else{
	   		elementId = element.id;
	   	}

		var idImg = "" + elementId + "Img";

		if (document.getElementById(idImg)!=null){
			document.getElementById(idImg).src = "img/proceso_compra/tick.gif";
	    }
	 }

	function setInputFieldStatus(elementId, message)
	 {
	    var id = "" + elementId + "Error";
	    var idImg = "" + elementId + "Img";
		if (document.getElementsByName(elementId)[0].type=="radio"){
			idImg = "" + elementId.substr(0,elementId.length) + "1Img";
		}
	    //Limpiamos posible error Spring
	    var idSpringError = document.getElementById(elementId+".errors");

	    if(idSpringError!= null){
	    	idSpringError.innerHTML="";
	    }
	    if (document.getElementById(id)!=null){
			if (message!=""){
				if(id == 'cobCaptchaTextError' && document.getElementById('divErrorCaptcha')!=null){
		    		document.getElementById('divErrorCaptcha').style.visibility="visible";
		    	}
	    		document.getElementById(id).innerHTML = message;
		    	cambiarCaptcha();
		    }else{
		    	if(id == 'cobCaptchaTextError' && document.getElementById('divErrorCaptcha')!=null){
		    		document.getElementById('divErrorCaptcha').style.visibility="hidden";
		    	}else{
		    		document.getElementById(id).innerHTML = message;
		    	}
		    }

		}

	    	if (message!==""){
	    		if (document.getElementById(idImg)!=null){
		    		document.getElementById(idImg).src = "img/proceso_compra/exclamacion_red.gif";
	    		}

	    	}else{
	    		if (document.getElementById(idImg)!=null){
		    		document.getElementById(idImg).src = "img/proceso_compra/tick.gif";
		    	}
	    	}

	 }

	function cambiarCaptcha(){
		img1 = new Image();
		img1.src="viewCaptcha.do?" + Math.floor(Math.random()*10000000);
		if (document.getElementById('captchaImage')!=null){
			document.getElementById('captchaImage').src = img1.src;
			if (document.getElementById('cobCaptchaText')!=null){
				document.getElementById('cobCaptchaText').value='';
			}

		}
	}

	 function actualizarImgValidation(){

		if (document.getElementById("erroresValidacion.errors")!=null){

			if (document.getElementById("erroresValidacion.errors").innerHTML!=''){

					for (i=0; i< document.images.length; i++){
						var idImg = document.images[i].id;

						if (idImg.substr(idImg.length-3,idImg.length)=='Img'){
							var path = idImg.substr(0,idImg.length-3);
							if (path != ""){

								var elemento = document.getElementById(path);

								if (elemento.type=="radio"){
									path = path.substr(0,path.length-1);
								}

								if (document.getElementById(path + ".errors")!=null){

									document.getElementById(idImg).src = "img/proceso_compra/exclamacion_red.gif";
								}else{
									document.getElementById(idImg).src = "img/proceso_compra/tick.gif";
								}
							}

						}
					}

			}
		}
	}
