/*
Class: IndexControllerClass
Description: Clase que controla la interaccion de la pagina web: Index.htm
Author: MDPCS
Info: info@mantenimientodepcs.es
*/

//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;
}

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.validateForm = function(){
	//Obtencion de datos del formulario
	var formObj = document.FRM_CONTACT;

	var categoryObj = formObj.category;
	var fullNameObj = formObj.fullName;
	var phoneObj = formObj.phone;
	var emailObj = formObj.email;
	var commentsObj = formObj.comments;

	var category = this.trimString(categoryObj.value);
	var fullName = this.trimString(fullNameObj.value);
	var phone = this.trimString(phoneObj.value);
	var email = this.trimString(emailObj.value);
	var comments = this.trimString(commentsObj.value);

	//Flag de validacion
	var flag = false;

	if(category != ""){
		if(fullName != ""){
			if(phone != ""){
				if(email != ""){
					if(this.checkEmail(email)){
						if(comments != ""){
							flag = true;
						}else{
							alert("Por favor, indiquenos el motivo de su consulta");
							commentsObj.focus();
						}
					}else{
						alert("Por favor, introduzca su email");
						emailObj.focus();
					}
				}else{
					alert("Por favor, introduzca su email");
					emailObj.focus();
				}
			}else{
				alert("Por favor, introduzca la empresa en la que trabaja");
				phoneObj.focus();
			}
		}else{
			alert("Por favor, introduzca su Nombre Completo");
			fullNameObj.focus();
		}
	}else{
		alert("Por favor, introduzca la empresa en la que trabaja");
		categoryObj.focus();
	}

	if(flag){
		this.disableForm();
		this.AJAX_sendEmail(category,fullName,phone,email,comments);
	}
}

/*
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.");
};

//Make a request to know if the email exist in the system
Contact.prototype.AJAX_sendEmail = function(category,fullName,phone,email,comments){
	var seed = Math.ceil(1000000000*Math.random());
	//var url = "http://www.esmeta.es/extranet/urgente24/r_php/esmeta/sendEmail.php";
	var url = "./r_php/esmeta/sendEmail.php";
	
	var str = "";
	str += "&category=" + category;
	str += "&fullName=" + fullName;
	str += "&phone=" + phone;
	str += "&email=" + email;
	str += "&comments=" + comments;
	
	var formParameters = 'seed='+ seed + str;
	
	var XMLResults = "";
	
	$.ajax({
        type: "POST",
        url: "" + url,
		async:false,
        data: "" + formParameters,
        success: function(data){
			XMLResults = data;
		}
	});
	
	/*
	//Ajax Prototype Framework
	var myAjax = new Ajax.Request(url,
	{
	method: 'get',
	parameters: pars,
	onComplete: this.showResponseSendEmail,
	onFailure: this.reportError
	});
	*/
	
	//alert(XMLResults)
	
	this.showResponseSendEmail(XMLResults);
}

Contact.prototype.showResponseSendEmail = function (originalRequest){
	//alert(originalRequest.responseText);
	
	//var data = originalRequest.responseText;	
	var data = originalRequest;
	var message = readEsmetaXML("esmeta",data);
	//alert(message)
	if(message != -1){
		message = readEsmetaXML("transaction",data);
		if(message != -1){
			if(message == 1){
				alert("Su consulta ha sido enviada correctamente");

				document.location.href ="index.htm";
			}else{
				alert("Hemos tenido incidencia con el servicio.\nIntente ponerse en contacto con nosotros\na través de otros medios.");
			}
		}else{
			alert("Hemos tenido incidencia con el servicio.\nIntente ponerse en contacto con nosotros\na través de otros medios.");
			//alert("Impossible to read Esmeta XML Protocol");
		}
	}else{
		alert("Hemos tenido incidencia con el servicio.\nIntente ponerse en contacto con nosotros\na través de otros medios.");
		//alert("Impossible to read Esmeta XML Protocol");
	}

};

Contact.prototype.disableForm = function(){
	var formObj = document.FRM_CONTACT;
	formObj.reset();
	formObj.btSubmit.disabled=true;
}

