// DEFINICION GLOBAL DEL FORMULARIO
//**************************************************************
var configFields = {fields:[
		 {field:'tunombre', validation:['required']},
		 {field:'tuemail', validation:['required', 'email']},
		 {field:'sunombre', validation:['required']},
		 {field:'suemail', validation:['required', 'email']},
		 {field:'comentario', validation:['none']}
],
submitButton: 'botEnviar',
langDefinition: 'ES'
};

var configFieldsManifiesto = {fields:[
		 {field:'mani_nombres', validation:['required']},
		 {field:'mani_apellidos', validation:['required']},
		 {field:'mani_email', validation:['required', 'email']},
		 {field:'mani_telefono', validation:['none']},
		 {field:'mani_institucion', validation:['required']},
		 {field:'mani_website', validation:['required']},
		 {field:'mani_cargo', validation:['none']},
		 {field:'mani_comentario', validation:['none']}
],
submitButton: 'mani_enviar',
langDefinition: 'ES'
};
//**************************************************************
var phpAjaxCallerPath = "clases/ajax_functions.php";
//**************************************************************

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function trim(str) {
    return str.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
}

function iniciar_ajax(){
var xmlHttp=null;
   var ua = navigator.userAgent.toLowerCase();
   if (!window.ActiveXObject){
     xmlHttp = new XMLHttpRequest();
   }
   else if (ua.indexOf('msie 5') == -1){
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
   }
   else{
     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   }

   return xmlHttp;
}

function mostrar_mensaje(cadena, campo, error, invertirComportamiento){
	if(!invertirComportamiento){
		invertirComportamiento = false;
	}
	limpiar_contendor(campo);
	var elemento = document.getElementById(campo);
	var contenedor = elemento.parentNode;
	var new_div = document.createElement("div");
	var i;
	var totalW = 0;
	for(i = 0; i < contenedor.children.length; i++){
		totalW += contenedor.children.item(i).clientWidth;
	}
	new_div.id = "msg_"+campo;
	if(error){
		var imagen = document.createElement("img");
		new_div.style.marginTop = "3px";
		new_div.style.marginBottom = "5px";
		new_div.style.clear = "both";
		new_div.style.textAlign = "right";
		contenedor.appendChild(new_div);
		var imagen = document.createElement("img");
		imagen.src = "images/dialog_close.png";
		imagen.border = "0";
		imagen.align = "right";
		var texto = document.createElement("span");
		texto.innerHTML = cadena;
		texto.style.padding = "2px";
		texto.style.color = "#ffffff";
		texto.style.height = "16px";
		texto.style.fontFamily = "Verdana, Geneva, sans-serif";
		texto.style.fontSize = "10px";
		texto.style.lineHeight = "16px";
		texto.style.fontWeight = "bold";
		texto.style.fontStyle = "italic";
		new_div.appendChild(imagen);
		new_div.appendChild(texto);
	}else{
		var imagen = document.createElement("img");
		new_div.style.marginTop = "3px";
		new_div.style.marginBottom = "5px";
		new_div.style.textAlign = "right";
		new_div.style.clear = "both";
		contenedor.appendChild(new_div);
		var imagen = document.createElement("img");
		if(invertirComportamiento){
			imagen.src = "images/dialog_close.png";
		}else{
			imagen.src = "images/dialog_ok_apply.png";
		}
		imagen.border = "0";
		imagen.align = "right";
		var texto = document.createElement("span");
		texto.innerHTML = cadena;
		texto.style.padding = "2px";
		if(invertirComportamiento){
			texto.style.color = "#ffffff";
		}else{
			texto.style.color = "#ffffff";
		}
		texto.style.height = "16px";
		texto.style.fontFamily = "Verdana, Geneva, sans-serif";
		texto.style.fontSize = "10px";
		texto.style.lineHeight = "16px";
		texto.style.fontWeight = "bold";
		texto.style.fontStyle = "italic";
		new_div.appendChild(imagen);
		new_div.appendChild(texto);
	}
}

function mostrar_mensaje2(cadena, campo, error, invertirComportamiento){
	if(!invertirComportamiento){
		invertirComportamiento = false;
	}
	limpiar_contendor(campo);
	var elemento = document.getElementById(campo);
	var contenedor = elemento.parentNode;
	var new_div = document.createElement("div");
	var i;
	var totalW = 0;
	for(i = 0; i < contenedor.children.length; i++){
		totalW += contenedor.children.item(i).clientWidth;
	}
	new_div.id = "msg_"+campo;
	if(error){
		var imagen = document.createElement("img");
		new_div.style.marginTop = "3px";
		new_div.style.marginBottom = "5px";
		new_div.style.clear = "both";
		new_div.style.textAlign = "left";
		contenedor.appendChild(new_div);
		var imagen = document.createElement("img");
		imagen.src = "images/dialog_close2.png";
		imagen.border = "0";
		imagen.align = "left";
		var texto = document.createElement("span");
		texto.innerHTML = cadena;
		texto.style.padding = "2px";
		texto.style.color = "#000000";
		texto.style.height = "16px";
		texto.style.fontFamily = "Verdana, Geneva, sans-serif";
		texto.style.fontSize = "10px";
		texto.style.lineHeight = "16px";
		texto.style.fontWeight = "bold";
		texto.style.fontStyle = "italic";
		new_div.appendChild(imagen);
		new_div.appendChild(texto);
	}else{
		var imagen = document.createElement("img");
		new_div.style.marginTop = "3px";
		new_div.style.marginBottom = "5px";
		new_div.style.textAlign = "left";
		new_div.style.clear = "both";
		contenedor.appendChild(new_div);
		var imagen = document.createElement("img");
		if(invertirComportamiento){
			imagen.src = "images/dialog_close2.png";
		}else{
			imagen.src = "images/dialog_ok_apply2.png";
		}
		imagen.border = "0";
		imagen.align = "left";
		var texto = document.createElement("span");
		texto.innerHTML = cadena;
		texto.style.padding = "2px";
		if(invertirComportamiento){
			texto.style.color = "#000000";
		}else{
			texto.style.color = "#000000";
		}
		texto.style.height = "16px";
		texto.style.fontFamily = "Verdana, Geneva, sans-serif";
		texto.style.fontSize = "10px";
		texto.style.lineHeight = "16px";
		texto.style.fontWeight = "bold";
		texto.style.fontStyle = "italic";
		new_div.appendChild(imagen);
		new_div.appendChild(texto);
	}
}

function limpiar_contendor(campo){
	try{
		var elemento = document.getElementById(campo);
		var contenedor = elemento.parentNode;
		var msg = document.getElementById("msg_"+campo);
		if(msg){
			contenedor.removeChild(msg);
		}
	}catch(error){
	}
}

function validarCampo(defCampo){
	var campo = document.getElementById(defCampo.field);
	var retValue = true;
	var i;
	for(i=0;i<defCampo.validation.length;i++){
		if(defCampo.errors <= 0){
			switch(defCampo.validation[i]){
				case 'required':
				   if(trim(campo.value) == ""){
					   defCampo.errors ++;
					   mostrar_mensaje(langDefintion.errorRequired, defCampo.field, true);
					   retValue = false;
				   }else{
					   limpiar_contendor(defCampo.field);
					   retValue = true;
				   }
				break;
				
				case 'email':
				   if(isEmailAddr(campo.value)){
					   limpiar_contendor(defCampo.field);
					   retValue = true;
				   }else{
					   defCampo.errors ++;
					   mostrar_mensaje(langDefintion.errorEmail, defCampo.field, true);
					   retValue = false;
				   }
				break;
				
				default:
				   retValue = true;
			}
		}
	}
	return retValue;
}

function validarCampoManifiesto(defCampo){
	var campo = document.getElementById(defCampo.field);
	var retValue = true;
	var i;
	for(i=0;i<defCampo.validation.length;i++){
		if(defCampo.errors <= 0){
			switch(defCampo.validation[i]){
				case 'required':
				   if(trim(campo.value) == ""){
					   defCampo.errors ++;
					   mostrar_mensaje2(langDefintion.errorRequired, defCampo.field, true);
					   retValue = false;
				   }else{
					   limpiar_contendor(defCampo.field);
					   retValue = true;
				   }
				break;
				
				case 'email':
				   if(isEmailAddr(campo.value)){
					   limpiar_contendor(defCampo.field);
					   retValue = true;
				   }else{
					   defCampo.errors ++;
					   mostrar_mensaje2(langDefintion.errorEmail, defCampo.field, true);
					   retValue = false;
				   }
				break;
				
				default:
				   retValue = true;
			}
		}
	}
	return retValue;
}

function validarFormulario(configArray){
	var error_count = 0;
	
	for(i=0;i<configArray.fields.length;i++){
		configArray.fields[i].errors = 0;
		if(!validarCampo(configArray.fields[i])){
			error_count++;
		}
	}
	if(error_count == 0){
		return true;
	}else{
		return false;
	}
}

function validarFormularioManifiesto(configArray){
	var error_count = 0;
	
	for(i=0;i<configArray.fields.length;i++){
		configArray.fields[i].errors = 0;
		if(!validarCampoManifiesto(configArray.fields[i])){
			error_count++;
		}
	}
	if(error_count == 0){
		return true;
	}else{
		return false;
	}
}


function insertarCargador(objeto, reemplazar){
	var elemento = document.getElementById(objeto);
	var padre = elemento.parentNode;
	if(reemplazar){
		padre.removeChild(elemento);
	}else{
		elemento.disabled = true;
	}
	var imagen = document.createElement("img");
	imagen.src = "images/ajaxloader.gif";
	imagen.id = "cargadorAjax";
	imagen.border = "0";
	imagen.align = "right";
	padre.appendChild(imagen);
}

function insertarCargador2(objeto, reemplazar){
	var elemento = document.getElementById(objeto);
	var padre = elemento.parentNode;
	if(reemplazar){
		padre.removeChild(elemento);
	}else{
		elemento.disabled = true;
	}
	var imagen = document.createElement("img");
	imagen.src = "images/ajaxloader2.gif";
	imagen.id = "cargadorAjax";
	imagen.border = "0";
	imagen.align = "right";
	padre.appendChild(imagen);
}

function removerCargador(){
	var elemento = document.getElementById("cargadorAjax");
	var padre = elemento.parentNode;
	padre.removeChild(elemento);
}

function simpleAjaxCall(accionPHP, campos, okFunction, errorFunction){
	var xmlHttp = iniciar_ajax();
	var i;
	if (xmlHttp == null){
	  return false;
	}
	
	var post_data = "";
	post_data += "accion="+accionPHP;
	for(i=0; i<campos.length; i++){
		post_data += "&"+campos[i].field+"="+trim(document.getElementById(campos[i].field).value);
	}
	post_data += "&langDef="+configFields.langDefinition;
	
	var url = phpAjaxCallerPath;
	url = url+"?unique="+Math.random();
	xmlHttp.onreadystatechange = function(){stateChangedSimpleAjaxCall(xmlHttp, okFunction, errorFunction)};
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", post_data.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(post_data);
}

function stateChangedSimpleAjaxCall(xmlHttp, okFunction, errorFunction){ 
   if(xmlHttp.readyState == 4){ 
      if(xmlHttp.responseText.match("OK")){
		  eval(okFunction+"()");
	  }else{
		  eval(errorFunction+"('"+xmlHttp.responseText+"')");
	  }
   }
}

function eviarAmigoOK(){
	var i;
	for(i=0;i<configFields.fields.length;i++){
		document.getElementById(configFields.fields[i].field).value = "";
	}
	document.getElementById("comentario").value = "";
	removerCargador();
	document.getElementById(configFields.submitButton).disabled = false;
	mostrar_mensaje(langDefintion.SendOK, configFields.submitButton, false);
	var okmsg = setTimeout("limpiarMsgOK()",5000);
}

function eviarAmigoError(errorCode){
	removerCargador();
	document.getElementById(configFields.submitButton).disabled = false;

	switch(trim(errorCode)){
		case 'errorConexion':
			mostrar_mensaje(langDefintion.SendError, configFields.submitButton, false, true);
		break;
		
		case 'errorSpam':
			mostrar_mensaje(langDefintion.errorSpam, configFields.submitButton, false, true);
		break;
		
		default:
		   mostrar_mensaje(langDefintion.generalError, configFields.submitButton, false, true);
	}
	var okmsg = setTimeout("limpiarMsgOK()",5000);
}

function limpiarMsgOK(){
	try{
		var msg = document.getElementById("msg_"+configFields.submitButton);
		var padre = msg.parentNode;
		padre.removeChild(msg);
	}catch(error){}
}

function sendToFriend(lang){
	configFields.langDefinition = lang;
	if(validarFormulario(configFields)){
		insertarCargador(configFields.submitButton, false);
		simpleAjaxCall('enviarAmigo', configFields.fields, 'eviarAmigoOK', 'eviarAmigoError');
	}
	
}

function enviar_manifiesto(lang){
	if(validarFormularioManifiesto(configFieldsManifiesto)){
		insertarCargador2(configFieldsManifiesto.submitButton, false);
		simpleAjaxCall('enviarManifiesto', configFieldsManifiesto.fields, 'eviarManifiestoOK', 'eviarManifiestoError');
	}
	
}

function eviarManifiestoOK(){
	var i;
	for(i=0;i<configFieldsManifiesto.fields.length;i++){
		document.getElementById(configFieldsManifiesto.fields[i].field).value = "";
	}
	document.getElementById("mani_comentario").value = "";
	removerCargador();
	document.getElementById(configFieldsManifiesto.submitButton).disabled = false;
	mostrar_mensaje2(langDefintion.SendOK, configFieldsManifiesto.submitButton, false);
	var okmsg = setTimeout("limpiarMsgOK2()",5000);
}

function eviarManifiestoError(errorCode){
	removerCargador();
	document.getElementById(configFieldsManifiesto.submitButton).disabled = false;

	switch(trim(errorCode)){
		case 'errorConexion':
			mostrar_mensaje2(langDefintion.SendError, configFieldsManifiesto.submitButton, false, true);
		break;
		
		case 'errorSpam':
			mostrar_mensaje2(langDefintion.errorSpam, configFieldsManifiesto.submitButton, false, true);
		break;
		
		default:
		   mostrar_mensaje2(langDefintion.generalError, configFieldsManifiesto.submitButton, false, true);
	}
	var okmsg = setTimeout("limpiarMsgOK2()",5000);
}

function limpiarMsgOK2(){
	try{
		var msg = document.getElementById("msg_"+configFieldsManifiesto.submitButton);
		var padre = msg.parentNode;
		padre.removeChild(msg);
	}catch(error){}
}