// JavaScript Document



function showMailList() {
	document.getElementById("oriMailList").reset();
	document.getElementById("oriMailList").style.display="block";
	document.getElementById("oriMailListThanks").style.display="none";		
}

function isValidEmailMailingList(email) {
	var emailReg = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
    var regex = new RegExp(emailReg);
	if(!regex.test(email)) {
	//	if (email!="")
		return 0;			
	} return 1
}

function inserirMailList() {
	var thanks ="";
	var serverAddress = "/form/parse_mail_list_ajax_1.php";
	var params="inserir=mailinglist";	
	
	values_obri = new Array("emailMailList","emailDestinoMailList","formNameMailList","autoRespostaMailList");

	if (!isValidEmailMailingList(document.getElementById("emailMailList").value)) {
		alert('Inseriu um E-mail invalido!')
		document.getElementById("emailMailList").focus();
		return;
	}
	
	for (var i=0; i<values_obri.length; i++) {
		var val = document.getElementById(values_obri[i]).value;
		params += "&" + values_obri[i] + "=" + encodeURIComponent(val) ;
	}
	
	var nparams=5;
	var xmlHttp = createXmlHttpRequestObject();
	// only continue if xmlHttp isn't void
	if (xmlHttp) {
	    // try to connect to the server
	    try {
    	  	// continue only if the XMLHttpRequest object isn't busy
        	// and the cache is not empty
		    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {				
		        // make a server request to validate the extracted data
        		xmlHttp.open("POST", serverAddress, true);
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
//			    xmlHttp.setRequestHeader("Connection", "close");
				xmlHttp.onreadystatechange = function () {
					if (xmlHttp.readyState == 4) {
					  if (xmlHttp.status == 200) {
					    try {
							// retrieve the server's response 
							var response = xmlHttp.responseText;
							// server error?
							if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
								throw(response.length == 0 ? "Server error." : response);
							// get response in XML format (assume the response is valid XML)
							responseXml = xmlHttp.responseXML;
							// get the document element
							//xmlDoc = responseXml.documentElement;
							//var result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
							//var error = xmlDoc.getElementsByTagName("error")[0].firstChild.data;
							//var thanks = xmlDoc.getElementsByTagName("thanks")[0].firstChild.data;
						
							document.getElementById("oriMailList").style.display="none";
							document.getElementById("oriMailListThanks").innerHTML = response;
							document.getElementById("oriMailListThanks").style.display="block";	
	
					    } catch(e) {
						    displayError(e.toString());
					    }
					  } else {
					  	displayError(xmlHttp.statusText);
					  }
					}												
				    }
		        xmlHttp.send(params);
	  		  }
    		} catch (e) {
	  			// display an error when failing to connect to the server
				displayError(e.toString());
    		}
		}

//	obj = document.getElementById("oriform");
//    document.body.removeChild(obj);
//	document.getElementById("oriformContainer").innerHTML = 'Olá';
}


// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() {
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try   {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }  catch(e)  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
      try { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } catch (e) {} // ignore potential error
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    displayError("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}


function displayError($message)
{
	// when set to true, display detailed AJAX error messages
var showErrors = true;

  // ignore errors if showErrors is false
  if (showErrors)
  {
    // turn error displaying Off
    showErrors = false;
    // display error message
 
    alert("Error encountered: \n" + $message);
    // retry validation after 10 seconds
//    setTimeout("validate();", 10000);
  }
}