//####################### CARGO LOS MENSAJES Y LOS PRELOAD DE LAS IMAGENES #########
onload=function(){
	
	cAyuda=document.getElementById("mensajesAyuda");
	cNombre=document.getElementById("ayudaTitulo");
	cTex=document.getElementById("ayudaTexto");
	
	divTransparente=document.getElementById("transparencia");
	divMensaje=document.getElementById("transparenciaMensaje");
	
	urlDestino="modules/mail.php";
	form=document.getElementById("formulario");
		
	claseNormal="input";
	claseError="inputError";
	
	preCarga("ok.gif", "loading.gif", "error.gif");
	
}

function ocultaMensaje(reload)
{
	divTransparente.style.display="none";
	
	if(reload == 1){
		document.location.reload();
	}
}

function muestraMensaje(mensaje)
{
	divMensaje.innerHTML=mensaje;
	divTransparente.style.display="block";
}
function preCarga()
{
	imagenes=new Array();
	for(i=0; i<arguments.length; i++)
	{
		imagenes[i]=document.createElement("img");
		imagenes[i].src=arguments[i];
	}
}

//######################### CREO EL OBJETO AJAX ###########################
function nuevoAjax()
{ 
	var xmlhttp=false; 
	try 
	{ 
		// No IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } 
	return xmlhttp; 
}
//#########################################################################
function campoError(campo)
{
	campo.className=claseError;
	error=1;
}

function limpiaForm()
{
	for(i=0; i<=4; i++)
	{
		form.elements[i].className=claseNormal;
	}
	document.getElementById("textarea").className=claseNormal;
}
//##################### VALIDACION EMAIL ##################################
function validaCorreo(mail)
{
	var reg=/(^[a-zA-Z0-9._-]{1,30})@([a-zA-Z0-9.-]{1,30}$)/;
	if(reg.test(mail)) return true;
	else return false;
}
//#########################################################################
function vacioForm(){

	form.textfield1.value = "";
	form.textfield2.value = "";
	form.textfield3.value = "";
	form.textfield4.value = "";
	form.textarea.value = "";
	
}

//##################### VALIDACION DE CAMPOS OBLIGATORIOS #################
function validacion(){
	
	limpiaForm();
	error=0;
	
	var name = form.textfield1.value;
	var email = form.textfield2.value;
	var phone = form.textfield3.value;
	var captcha = form.textfield4.value;
	var message = form.textarea.value;
	var contactenos = form.contactenos.value;

	
	if(!validaCorreo(document.all('textfield2').value)) campoError(document.all('textfield2'));
	if(document.all('textfield1').value==""){campoError(document.all('textfield1'));}
	if(document.all('textfield4').value=="") campoError(document.all('textfield4'));
	if(document.all('textarea').value=="") campoError(document.all('textarea'));
	
	
	if(error==1){
		
		if(document.all('textfield1').value==""){
			var texto= "<table align='center'><tr align='center'><td align='center'>";
			texto += "<img src='./images/error.gif' alt='Error'>";
			texto += "<br><br><font color='#990000'>ERROR: Field Company is mandatory.<br></font><font color='#990000'>Remember that all fields marked with * are mandatory.<br></font>";
			texto += "<font color='#990000'>Please complete an resend<br></font></tr></td>";
			texto += "<tr align='center'><td align='center'><button style='width:48px; height:18px; font-size:10px;' onClick='ocultaMensaje(0)' type='button'>Ok</button>";
			texto +="</td></tr></table>";
			muestraMensaje(texto, "false");
		}
		else if(document.all('textfield2').value==""){
			
			var texto= "<table align='center'><tr align='center'><td align='center'>";
			texto += "<img src='./images/error.gif' alt='Error'>";
			texto += "<br><br><font color='#990000'>ERROR: Field City is mandatory.<br></font><font color='#990000'>Remember that all fields marked with * are mandatory.<br></font>";
			texto += "<font color='#990000'>Please complete an resend<br></font></tr></td>";			
			texto += "<tr align='center'><td align='center'><button style='width:48px; height:18px; font-size:10px;' onClick='ocultaMensaje(0)' type='button'>Ok</button>";
			texto +="</td></tr></table>";
			muestraMensaje(texto);
		}
		else if(document.all('textfield4').value==""){
			
			var texto= "<table align='center'><tr align='center'><td align='center'>";
			texto += "<img src='./images/error.gif' alt='Error'>";
			texto += "<br><br><font color='#990000'>ERROR: Field Export Manager Name is mandatory.<br></font><font color='#990000'>Remember that all fields marked with * are mandatory.<br></font>";
			texto += "<font color='#990000'>Please complete an resend<br></font></tr></td>";			
			texto += "<tr align='center'><td align='center'><button style='width:48px; height:18px; font-size:10px;' onClick='ocultaMensaje(0)' type='button'>Ok</button>";
			texto +="</td></tr></table>";
			muestraMensaje(texto);
		}		
		else if(document.all('textarea').value==""){

			var texto= "<table align='center'><tr align='center'><td align='center'>";
			texto += "<img src='./images/error.gif' alt='Error'>";
			texto += "<br><br><font color='#990000'>ERROR: Field Comments is mandatory.<br></font><font color='#990000'>Remember that all fields marked with * are mandatory.<br></font>";
			texto += "<font color='#990000'>Please complete an resend<br></font></tr></td>";			
			texto += "<tr align='center'><td align='center'><button style='width:48px; height:18px; font-size:10px;' onClick='ocultaMensaje(0)' type='button'>Ok</button>";
			texto +="</td></tr></table>";
			muestraMensaje(texto, "false");		
		}
		
	}
	else{
		
		var texto="<table align='center'><tr align='center'><td align='center'>";
		texto += "<img src='./images/loading.gif' alt='Enviando'>"
		texto += "<br><br>Sending mail. Please wait.<br><br></tr></td>"
		texto += "<tr align='center'><td align='center'><button style='width:60px; height:18px; font-size:10px;' onClick='ocultaMensaje(0)' type='button'>Ocultar</button>";
		texto += "</td></tr></table>";
		muestraMensaje(texto);
		
		var ajax=nuevoAjax();
		ajax.open("POST", urlDestino, true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("name="+name+"&email="+email+"&phone="+phone+"&message="+message+"&capcha="+captcha+"&contactenos="+contactenos);
		
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==4){
				
				var respuesta=ajax.responseText;

				if(respuesta=="OK"){
						
						var texto= "<table align='center'><tr align='center'><td align='center'>";
						texto += "<img src='./images/ok.gif' alt='Ok'>";
						texto += "<br><br><p align='center' class='text_blue'><strong>Your Message was successfully sent<br></strong>";
						texto += "<strong>to our office.<br>We will contact you as soon as possible.</strong></p></tr></td>";
						texto += "<p align='center' class='text_blue'><strong>Thank You</strong>.</p>";
						texto += "<tr align='center'><td align='center'><button style='width:48px; height:18px; font-size:10px;' onClick='ocultaMensaje(1)' type='button'>Ok</button>";
						texto +="</td></tr></table>";
                       	muestraMensaje(texto);
                       	vacioForm();
                       	
				}
				else{
					
					var texto= "<table align='center'><tr align='center'><td align='center'>";
					texto += "<img src='./images/error.gif' alt='Error'>";
					texto += "<br><br><font color='#990000'>ERROR: Please try again later.<br></font></tr></td>";
					texto += "<tr align='center'><td align='center'><button style='width:48px; height:18px; font-size:10px;' onClick='ocultaMensaje(0)' type='button'>Ok</button>";
					texto +="</td></tr></table>";
						
					muestraMensaje(texto);
					vacioForm();
				}
			}
		}
	}
}