function mostrar_error(div, div2) {
	div.style.background='#ffeaea';
	div2.innerHTML='<img src="'+rutaweb+'/img/dialog-cancel.gif">';
}

function CheckSubmit(e) {
	e=e?e:window.event;
	if(e.keyCode==13) {
		var formulario = document.getElementById("formulario");
		enviar_formulario(formulario);
	}
}

function no_mostrar_error(div, div2) {
	div.style.background='#FFFFFF';	
	div2.innerHTML='<img src="'+rutaweb+'/img/dialog-ok.png">';
}

function comprobar_fc(campo,error,longitud,email) {
	if(email) {
		if(campo.value.indexOf ('@', 0) == -1) {
			mostrar_error(campo,error);
			return false;
		} else {
			no_mostrar_error(campo,error);
		}		
	} else {
		if(campo.value.length < longitud) {
			mostrar_error(campo,error);
			return false;
		} else {
			no_mostrar_error(campo,error);
		}	
	}
}

function mostrar_telefono(campo) {
	if(campo.value==2) {
		mostrar_div(formulario_contactar_telefono1);
		mostrar_div(formulario_contactar_telefono2);
	} else {
		ocultar_div(formulario_contactar_telefono1);
		ocultar_div(formulario_contactar_telefono2);
	}
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function enviar_formulario(f) {

	var ajax = xmlhttp();
	var formulario_contactar = document.getElementById("formulario_contactar");
	var fc_cargando = document.getElementById("fc_cargando");
	var fc_enviado = document.getElementById("fc_enviado");
	var name_error = document.getElementById("name_error");
	var email_error = document.getElementById("email_error");
	var telefono_error = document.getElementById("telefono_error");
	var subject_error = document.getElementById("subject_error");
	var message_error = document.getElementById("message_error");
	var code_error = document.getElementById("code_error");

	if(comprobar_fc(f.name,name_error,3)==false) { return false; }
	if(comprobar_fc(f.email,email_error,3,true)==false) { return false; }
	if(f.deptid.value == 002) { if(comprobar_fc(f.telefono,telefono_error,5)==false) { return false; } }
	if(comprobar_fc(f.subject,subject_error,3)==false) { return false; }
	if(comprobar_fc(f.message,message_error,3)==false) { return false; }
	if(comprobar_fc(f.code,code_error,5)==false) { return false; }

	ajax.onreadystatechange=function(){
		if(ajax.readyState==1) {
			mostrar_div(fc_cargando);
		}
		if(ajax.readyState==4) {
			ocultar_div(fc_cargando);
			mostrar_div(fc_enviado);
			formulario_contactar.style.height="80px";
			formulario_contactar.innerHTML="";
		}
	}

	ajax.open("POST",rutaweb+"/s_contactar_ok.php",true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("deptid="+f.deptid.value+"&name="+f.name.value+"&email="+f.email.value+"&subject="+f.subject.value+"&urgency="+f.urgency.value+"&telefono="+f.telefono.value+"&message="+f.message.value);

	return false;

}

function enviar_devolucion(f) {

	if(f.name.value=="")			{ alert("Debe rellenar el campo de nombre"); return false; }
	if(f.telefono.value.length < 3)	{ alert("Debe rellenar el campo de teléfono"); return false; }

	var ajax = xmlhttp();

	ajax.onreadystatechange=function(){
		if(ajax.readyState==1) {
			mostrar_div(div_dev_cargando);
		}
		if(ajax.readyState==4) {
			ocultar_div(div_dev_cargando);
			mostrar_div(div_dev_enviado);
		}
	}

	ajax.open("POST",rutaweb+"/s_contactar_ok.php",true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("deptid="+f.deptid.value+"&name="+f.name.value+"&email="+f.email.value+"&subject="+f.subject.value+"&urgency="+f.urgency.value+"&telefono="+f.telefono.value+"&message="+f.message.value);

	return false;

}

function cambiar_contenido_div() {
	
}

function aw_enviar_devolucion(f) {

	if(f.name.value=="")			{ alert("Debe rellenar el campo de nombre"); return false; }
	if(f.telefono.value.length < 3)	{ alert("Debe rellenar el campo de teléfono"); return false; }

	var aw_comp_simple_devolucion_conchat = document.getElementById("aw_comp_simple_devolucion_conchat");
	var aw_dev_cargando = document.getElementById("aw_dev_cargando");
	var aw_dev_enviado = document.getElementById("aw_dev_enviado");

	var ajax = xmlhttp();

	ajax.onreadystatechange=function(){
		if(ajax.readyState==1) {
			ocultar_div(aw_comp_simple_devolucion_conchat);
			mostrar_div(aw_dev_cargando);
		}
		if(ajax.readyState==4) {
			ocultar_div(aw_dev_cargando);
			mostrar_div(aw_dev_enviado);
		}
	}

	ajax.open("POST",rutaweb+"/s_contactar_ok.php",true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("deptid="+f.deptid.value+"&name="+f.name.value+"&email="+f.email.value+"&subject="+f.subject.value+"&urgency="+f.urgency.value+"&telefono="+f.telefono.value+"&message="+f.message.value);

	return false;

}