//Ajax validation
preLoadImagen1 = new Image();
preLoadImagen1.src = "img/proceso_compra/exclamacion_red.gif";

preLoadImagen2 = new Image();
preLoadImagen2.src = "img/proceso_compra/tick.gif";
var miError = false;

/**
 * Valida un campo del formulario
 * @param element Nombre campo
 * @param form    Formulario
 * @return
 */
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";
	var idDiv = "" + elementId + "Div";
	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";
		}
		if (document.getElementById(idDiv) != null) {
			hacerVisible(idDiv);
		}
	} else {
		if (document.getElementById(idImg) != null) {
			document.getElementById(idImg).src = "img/proceso_compra/tick.gif";
		}
		if (document.getElementById(idDiv) != null) {
			hacerInvisible(idDiv)
		}
	}
}

function hacerVisible(idDiv) {
	var obj = document.getElementById(idDiv);
	if (obj) {
		obj.style.display = "";
	}
}

function hacerInvisible(idDiv) {
	var obj = document.getElementById(idDiv);
	if (obj) {
		obj.style.display = "none";
	}
}
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 onlyNumbers(e) {
	var patron;
	var tecla = (document.all) ? e.keyCode : e.which; // IE o Netscape/Firefox/Opera
	if (tecla == 0 || tecla == 8)
		return true;
	else {
		patron = /\d/
		te = String.fromCharCode(tecla);
		return patron.test(te);
	}
}

function onlyCharacters(e) {
	var patron;
	var tecla = (document.all) ? e.keyCode : e.which; // IE o Netscape/Firefox/Opera
	if (tecla == 0 || tecla == 8 || tecla == 32
			|| (tecla >= 225 && tecla <= 250))
		return true;
	else {
		patron = /[A-Za-z]/
		te = String.fromCharCode(tecla);
		return patron.test(te);
	}
}

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";
						}
					}

				}
			}
		}
	}
}

