function validate_required(field,alerttxt)
{
	with (field)
	{
		if (value==null||value=="")
		  {alert(alerttxt);return false}
		else {return true}
	}
}

function validate_email(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) 
		  {alert(alerttxt);return false}
		else {return true}
	}
}

function subscribe() {
	$("#nl_body").slideUp();
	$("#nl_h1").html= "Subscrição efectuada com sucesso";
	$.post("actions/ajax_nlsubscribe.php",
			{name: $("#nl_name").attr("value"), email: $("#nl_email").attr("value"), enterprise: $("#nl_enterprise").attr("value")}
	);
}

function XMLHTTPObject() {
    var xmlhttp=false;
    //If XMLHTTPReques is available
    if (XMLHttpRequest) {
        try {xmlhttp = new XMLHttpRequest();}
        catch(e) {xmlhttp = false;}
    } else if(typeof ActiveXObject != 'undefined') {
	//Use IE's ActiveX items to load the file.
        try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} 
        catch(e) {
            try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
            catch(E) {xmlhttp = false;}
        }
    } else  {
        xmlhttp = false; //Browser don't support Ajax
    }
    return xmlhttp;
}

var http = new XMLHTTPObject();


//This function will be called when the form is submited
function saveData() {
	
	
	if (validate_form(document.getElementById("subscribenl")) == true) {
	
	    var name = document.getElementById("nl_name").value;
	    var enterprise = document.getElementById("nl_enterprise").value;
	    var email = document.getElementById("nl_email").value;
	    
	    var url = "../actions/ajax_nlsubscribe.php";
		var params = "name="+name+"&enterprise="+enterprise+"&email="+email+"&return=confirm";
	
	    //By calling this file, we have saved the data.
	    http.open("POST",url,true);
	    
	    //Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
	    
	    http.onreadystatechange = function() {
	        if(http.readyState == 4) {
	            if(http.status == 200) {
	                var result = http.responseText;
	                var msg = "";
	                if(result == '1') {
	                    //Display the thank you message.
	                    msg = "Obrigado pela sua subscrição.";
	                } else {
	                    msg = "Falhou: já subscrito.";
	                }
	                document.getElementById("nl_body").innerHTML = msg;
	            }
	        }
	    }
	    http.send(params);
	}
    
    return false;//Prevent the form from being submited
}


function init() {
    if(http) {//If the browser supports Ajax functions
    	//document.getElementById("subscribenl").onsubmit = validate_form(this);
        document.getElementById("subscribenl").onsubmit = saveData; //The saveData function is attached to the submit action of the form.
    }
}


function validate_form(thisform)
{
	with (thisform)
	{
		if ( (validate_required(name,"O campo \"Nome\" é de preenchimento obrigatório.")==false) )
				{name.focus();return false}
		else if (
					(validate_required(email,"O campo \"Email\" é de preenchimento obrigatório.")==false) ||
					(validate_email(email,"O endereço de email introduzido não é válido.")==false) 
				)
		  		{email.focus();return false}
	}
	return true;
}

window.onload = init; //The 'init' function will be called when the page is loaded.