window.onload=function() {
	setListeners();
	loadAjax("html/home.html?lang=" + lang);
}

var buttons = [{button: 'logo', link: 'html/home.html?lang=' + lang}, {button: 'b1', button_footer: 'a1', link: 'html/group.html?lang=' + lang}, {button: 'b2', button_footer: 'a2', link: 'html/business.html?lang=' + lang}, {button: 'b3', button_footer: 'a3', link: 'html/customers.html?lang=' + lang}, {button: 'b4', button_footer: 'a4', link: 'html/presence.html?lang=' + lang}, {button: 'b5', button_footer: 'a5', link: 'html/contact.html?lang=' + lang}, {button_footer: 'a6', link: 'html/join_us.html?lang=' + lang}];

function setListeners() {
	for (var i = 0; i < buttons.length; i++) {
		if (buttons[i].button) {
			var b = document.getElementById(buttons[i].button);
			b.id = i;
			b.onclick = function() {
				loadAjax(buttons[this.id].link);
			}
			b.onmouseover = function() {
				this.getElementsByTagName('span')[0].style.color = "#e2e2e2";
			}
			b.onmouseout = function() {
				this.getElementsByTagName('span')[0].style.color = "#FFF";
			}
		}
		if (buttons[i].button_footer) {
			var b = document.getElementById(buttons[i].button_footer);
			b.id = i;
			b.onclick = function() {
				loadAjax(buttons[this.id].link);
			}
		}
	}
}

function newAjax(){
    var xmlhttp = false;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function loadAjax(url){
	var content = document.getElementById('content');
    var ajax = newAjax(); 
    ajax.open("GET", url, true);
    ajax.onreadystatechange = function() {
		if (ajax.readyState == 4) {
            if (ajax.status == 200) {
                content.innerHTML = ajax.responseText;
            } else if (ajax.status == 404) {
                content.innerHTML = "La pagina no existe";
            } else {
                content.innerHTML = "Error: " + ajax.status; 
            }
        }
    }
    ajax.send(null);
}

function sendForm() {
	var content = document.getElementById('content');
	var ajax = newAjax();
	var strURL = 'html/send.html';
	ajax.open('POST', strURL, true);
    ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            if (ajax.status == 200) {
                content.innerHTML = ajax.responseText;
            } else if (ajax.status == 404) {
                content.innerHTML = "La pagina no existe";
            } else {
                content.innerHTML = "Error: " + ajax.status; 
            }
        }
	}
    ajax.send(getquerystring());
}

function getquerystring() {
    var form = document.forms['f1'];
    var nombre = form.nombre.value;
    qstr = 'nombre=' + escape(nombre);
	var email = form.email.value;
	qstr += '&email=' + escape(email);
	var apellido = form.apellido.value;
	qstr += '&apellido=' + escape(apellido);
	var telefono = form.telefono.value;
	qstr += '&telefono=' + escape(telefono);
	if (form.empresa) { 
		var empresa = form.empresa.value;
		qstr += '&empresa=' + escape(empresa);
	}
	if (form.profesion) { 
		var profesion = form.profesion.value;
		qstr += '&profesion=' + escape(profesion);
	}
	if (form.sector) { 
		var sector = form.sector.value;
		qstr += '&sector=' + escape(sector);
	}
	var asunto = form.asunto.value;
	qstr += '&asunto=' + escape(asunto);
	var mensaje = form.mensaje.value;
	qstr += '&asunto=' + escape(mensaje);
	var lang = form.lang.value;
	qstr += '&lang=' + escape(lang);
	var contact = form.contact.value;
	qstr += '&contact=' + escape(contact);
    return qstr;
}

function validateForm() {
	var form = document.forms['f1'];
	var errors = [];
	if (lang = "en") {
		if (form.nombre.value == "") { errors.push("You must enter your Name"); }
		if (form.apellido.value == "") { errors.push("You must enter your Last Name"); }
		if (form.email.value == "") { errors.push("You must enter your Emai"); }
		if (form.mensaje.value == "") { errors.push("You must enter your Message"); }
	} else {
		if (form.nombre.value == "") { errors.push("Debe completar el campo Nombre"); }
		if (form.apellido.value == "") { errors.push("Debe completar el campo Apellido"); }
		if (form.email.value == "") { errors.push("Debe completar el campo Emai"); }
		if (form.mensaje.value == "") { errors.push("Debe completar el campo Mensaje"); }
	}
	
	if (errors.length == 0) {
		sendForm();
	} else {
		var strErrors = "";
		for (var i = 0; i < errors.length; i++) {
			strErrors += "<li>";
			strErrors += errors[i];
			strErrors += "</li>";
		}
		document.getElementById('uerrors').innerHTML = strErrors;
		document.getElementById('errors').style.display = "inline";
		document.getElementById('errors').style.visibility = "visible";
	}
}

function closeErrors() {
	document.getElementById('errors').style.display = "none";
	document.getElementById('errors').style.visibility = "hidden";
}




