/* Autor: http://www.niquelao.net */
var forms =  document.getElementsByTagName("form");
var inputs = document.getElementsByTagName("input");
var textareas =  document.getElementsByTagName("textarea");
var valorinicial = new Array();

function ttips(capa,id,valor){
	var ntop=0;
	var f1 = document.forms['fcontacto'];
	var t1 = f1.txt_campo;

	var targetElement = document.getElementById(id);
	var tipo = targetElement.type;
	switch ( tipo ) { 
		case "text":
			if (valorinicial[id] == valor)
				targetElement.value = "";
			break
		case "textarea":
			if (valorinicial[id] == valor) {
				targetElement.innerHTML = "";
				targetElement.value = "";
			}
			break
	}
	switch(id){
		case 'txt_nombre': text="Nombre"; ntop=0; break;
		case 'txt_fono': text="Teléfono"; ntop=23; break;
		case 'txt_email_1': text="E-mail"; ntop=46; break;
		case 'txt_email_2': text="Repetir e-mail"; ntop=68; break;
		case 'txt_mensaje': text="Mensaje"; ntop=93; break;
	}
	document.getElementById(capa).style.left=303;	
	document.getElementById(capa).style.top=ntop;	
	t1.value=text;
	document.getElementById(capa).style.visibility="visible";
}	
function oculta_tips(capa){
	document.getElementById(capa).style.visibility="hidden";
}
function formulario() {
	for (var i = 0; i < inputs.length; i++) {				
		if((inputs[i].type == "text") || (inputs[i].type == "password")) { 
			valorinicial[inputs[i].id]=inputs[i].value;
			inputs[i].onfocus = function() {ttips('campo', this.id, this.value)}
			inputs[i].onblur = function() {restaura(this.id,this.value)}
		}
	}
	for (var i = 0; i < textareas.length; i++) { 
		valorinicial[textareas[i].id]=textareas[i].value;
		textareas[i].onfocus = function() {ttips('campo', this.id, this.value)}
		textareas[i].onblur = function() {restaura(this.id,this.value)}
	}
}

function restaura(id){
  var targetElement = document.getElementById(id);	
	var tipo = targetElement.type;
	switch ( tipo ) { 
		case "text":
			if (targetElement.value.replace(/ /g, '') == '')
				targetElement.value = valorinicial[id];
				break
		case "password":
			if (targetElement.value.replace(/ /g, '') == '')
				targetElement.value = valorinicial[id];
				break
		case "textarea":
			revisa = targetElement.value.replace(/ /g, '');
			// Eliminamos saltos de línea y tabulaciones
			revisa = revisa.replace(/\n/g, '')
			revisa = revisa.replace(/\t/g, '')
			revisa = revisa.replace(/\r/g, '')
			if (revisa == '') {
				targetElement.innerHTML = valorinicial[id];
				targetElement.value = valorinicial[id];
			}
			break
	}
}
function validarForm(){
	var f1=document.forms[0];
	var wm = "Atención :\n\r\n";
	var noerror = 1;
	var vacios = false;
	
	var etiquetas = new Array("", "Nombre", "E-mail", "Repita su e-mail", "Teléfono/s", "Mensaje");
	var t1=f1.txt_nombre; var t2=f1.txt_email_1; var t3=f1.txt_email_2; var t4=f1.txt_fono; var t5=f1.txt_mensaje;
	for(i=1; i < etiquetas.length; i++){
		if(eval("t"+i).value==etiquetas[i]){
			vacios=true;
			break;
		}
	}
	if(vacios){
		wm+= "Debe completar todos los campos."; noerror=0;
	}else{
		//controlar fono
		if(!validar_fono(t4.value)){
				wm+= "El teléfono no parece ser válido."; noerror=0;
		}
		//controlar email
		if(validar_email(t2.value)){
			//comparar que ambos estén iguales
			if(t2.value != t3.value){
				wm+= "Debe escribir la misma dirección de e-mail."; noerror=0;
			}
		}else{
			wm+= "Dirección de correo inválida."; noerror=0;
		}
	}
	
	if (noerror == 0) {
	alert(wm);
	return false;
	}
	else return true;
}
window.onload = formulario;
