// JavaScript Document
function mostrar(que, como){
	if(document.getElementById(que).style.display!=como){
		document.getElementById(que).style.display=como;
	}else{
		document.getElementById(que).style.display='none';
	}
}
function agregarTexto(msjid,pa,texto){
	var msj = document.createElement("span");
	var txt = document.createTextNode(texto);
	var br = document.createElement("br");
	msj.setAttribute('id',msjid);
	br.setAttribute('id','br'+msjid);
	msj.className='error';
	pa.appendChild(br);
	pa.appendChild(msj);
	pa.setAttribute('class','td_error');
	document.getElementById(msjid).appendChild(txt);
}
function validar(){
	var errors=Array(),args=validar.arguments;
	for (i=0;i<args.length;i+=2){
		var pa = document.getElementById(args[i]).parentNode;
		var msjid = 'msj'+i;
		if(document.getElementById(msjid) != null ){
			pa.removeChild(document.getElementById('br'+msjid));
			pa.removeChild(document.getElementById(msjid));
			pa.setAttribute('class','td_normal');
		}
		// CAMPO REQUERIDO
		if(args[i+1].indexOf('R')>=0){
			if(document.getElementById(args[i]).value==""
			|| document.getElementById(args[i]).value=="Nombre:"
			|| document.getElementById(args[i]).value=="E-mail:"){
				agregarTexto(msjid,pa,"Requerido");
				errors[i] = "error";
			}
		}
		// VALIDACIÓN DE EMAIL
		if(args[i+1].indexOf('isEmail')>=0 && !errors[i]){
			emailval = document.getElementById(args[i]).value;
			arroba = emailval.lastIndexOf('@');
			arrobas = emailval.match(/\@/g);
			punto = emailval.lastIndexOf('.');
			if(arroba<1 || punto<arroba || punto==arroba+1 || punto==emailval.length-1 || arrobas.length > 1 || emailval.indexOf("@.")>=0){
				agregarTexto(msjid,pa,"Por favor, ingrese un email válido");
				errors[i] = "error";
			}
		}
		// VALIDACIÓN DE CAMPO NUMÉRICO
		if(args[i+1].indexOf('isNum')>=0 && !errors[i]){
			val = document.getElementById(args[i]).value;
			if (isNaN(val)) {
				agregarTexto(msjid,pa,"Ingrese sólo números");
				errors[i] = "error";
			}
		}
		// VALIDACIÓN DE CAMPO NUMÉRICO ESTRICTO
		if(args[i+1].indexOf('isSnum')>=0 && !errors[i]){
			val = document.getElementById(args[i]).value;
			if (val.toString().search(/^[0-9]*$/) != 0) {
				agregarTexto(msjid,pa,"Ingrese sólo números");
				errors[i] = "error";
			}
		}
	}
	document.errores = (errors.length==0);
}
function enviar(formu){
	if(document.errores==true){
		document.getElementById(formu).style.display = 'none';
		document.getElementById('enviando').style.display = 'block';
	}
}
function bfCampo(idCampo, texto, foco){
	var campo = document.getElementById(idCampo);
	if(foco){
		if(campo.value == texto){
			campo.value = '';
		}
	}else{
		if(campo.value == ''){
			campo.value = texto;
		}
	}
}
function igualarEmail(){
	document.getElementById('emailconfirm').value = document.getElementById('email').value;
}
function apaisar(formu){
	if(document.getElementById(formu).provincia.selectedIndex!=24){
		document.getElementById(formu).pais.selectedIndex=3;
	}else{
		document.getElementById(formu).pais.selectedIndex=0;
	}
}
function aprovinciar(formu){
	if(document.getElementById(formu).pais.selectedIndex!=3){
		document.getElementById(formu).provincia.selectedIndex=24;
	}else{
		document.getElementById(formu).provincia.selectedIndex=0;
	}
}
function agregarImg(id_tabla){
	actual = typeof actual == "undefined" ? 2 : actual+1;
	var tabla = document.getElementById(id_tabla);
	var ultimaFila = tabla.rows.length-1;
	var row = tabla.insertRow(ultimaFila);
	var cellLeft = row.insertCell(0);
	var cellRight = row.insertCell(1);
	var numero = document.createTextNode(actual);
	var fileField = document.createElement("input");
	fileField.setAttribute("type","file");
	fileField.setAttribute("name","imagen"+actual);
	cellLeft.appendChild(numero);
	cellRight.appendChild(fileField);
}
