/*
Class: Contact
Description: Clase que controla la interaccion de la pagina web: Index.htm
Author: JAB
Info: bren@juanantonio.info
*/

//Clase que controla el fichero Home.htm
function Contact(){
	this.error = false;
};

/*********************************
***** FUNCTIONS & UTILITIES *****
***********************************/

function readEsmetaXML(tag,data){
	var openTag = "<" + tag + ">";
	var closedTag = "</" + tag + ">";
	var index1 = data.indexOf(openTag);
	if(index1 == -1){
		return index1;
	}
	var index2 = data.indexOf(closedTag);
	if(index2 == -1){
		return index2;
	}
	var from = index1 + openTag.length;
	var until = index2
	var content = data.substring(from,until);
	return content;
};

Contact.prototype.trimString = function(str){
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
};

/*************************
***** CONTACT FORM *****
**************************/

/**
General procedure to block submit event into a web form
*/
Contact.prototype.onSubmitWebForm = function(){
	return false;
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
Contact.prototype.checkEmail = function(str){
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//alert("Invalid E-mail ID")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		//alert("Invalid E-mail ID")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//alert("Invalid E-mail ID")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		//alert("Invalid E-mail ID")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		//alert("Invalid E-mail ID")
		return false
	 }

	 return true					
}



Contact.prototype.validateFormContact = function(){//contacto.php
	//Obtencion de datos del formulario
	
	var nameObj = document.getElementById('txt_YourName');
	var emailObj = document.getElementById('txt_YourEmail');
	var selectObj = document.getElementById('sel_selectcontact');
	var messageObj = document.getElementById('txtarea_Message');
	
	var name = this.trimString(nameObj.value);
	var email = this.trimString(emailObj.value);
	var select = this.trimString(selectObj.value);
	var message = this.trimString(messageObj.value);
	
	//Flag de validacion
	var flag = false;

	if((name != "") && (name != "Your Name")){
		if(email != ""){
			if(this.checkEmail(email)){
				if(select != ""){
					if(message != ""){
						flag = true;
					}else{
						alert("Please type your message");
						messageObj.focus();
					}
				}else{
					alert("Please choose email destination");
					selectObj.focus();
				}
			}else{
				alert("Please type your email");
				emailObj.focus();
			}
		}else{
			alert("Please type your email");
			emailObj.focus();
		}
	}else{
		alert("Please type your name");
		nameObj.focus();
	}

	if(flag){
		//this.disableForm(); ACTIVAR ESTO !!!!!!!
		this.AJAX_sendEmailContact(name,email,select,message);
	}
}

/*
AJAX METHODS
*/

//Generic method to return an Ajax Error with Javascript Prototype Framework
Contact.prototype.reportError = function(){
	alert("Sorry, There was a error in the process.");
};

Contact.prototype.AJAX_sendEmailContact = function(name,email,select,message){//Contactar.php

	var seed = Math.ceil(1000000000*Math.random());
	//var url = "http://www.esmeta.es/extranet/urgente24/r_php/esmeta/sendEmail.php";
	var url = "r_php/mail.php";

	var str = "";
	str += "&name=" + name;
	str += "&email=" + email;
	str += "&select=" + select;
	str += "&message=" + message;
	
	var formParameters = 'seed='+ seed + str;
	//alert(formParameters);
	
	var XMLResults = "";
	
	$.ajax({
        type: "POST",
        url: "" + url,
		async:false,
        data: "" + formParameters,
        success: function(data){
			XMLResults = data;
		}
	});
		
	//alert(XMLResults)
	
	this.showResponseSendEmail(XMLResults);
}

Contact.prototype.showResponseSendEmail = function (originalRequest){
	//alert(originalRequest.responseText);
	
	//var data = originalRequest.responseText;	
	var data = originalRequest;
	//alert(data)

	var message = readEsmetaXML("esmeta",data);
	//alert(message)
	if(message != -1){
		message = readEsmetaXML("transaction",data);
		if(message != -1){
			if(message == 1){
//				alert("Thank you for submitting your inquiry, someone will respond to you shortly.");
				$("#btn").html("<p>Thank you for submitting your inquiry,<br /> someone will respond to you shortly.</p>");

				//document.location.href ="/consulta-enviada.php";
			}else{
				alert("We have had impact with the service.\n Try to contact us through other means.");
			}
		}else{
			alert("We have had impact with the service.\n Try to contact us through other means.");
			//alert("Impossible to read Esmeta XML Protocol");
		}
	}else{
		alert("We have had impact with the service.\n Try to contact us through other means.");
		//alert("Impossible to read Esmeta XML Protocol");
	}

};

Contact.prototype.disableForm = function(){
	var formObj = document.FRM_CONTACT;
	//formObj.reset();
	formObj.submitButton.disabled=true;
	formObj.submitButton.value="Sending...";
}


