function createRequestObject() {
    
   var req;

   if(window.XMLHttpRequest){
      // Firefox, Safari, Opera...
      req = new XMLHttpRequest();
   } else if(window.ActiveXObject) {
      // Internet Explorer 5+
      req = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
      // There is an error creating the object,
      // just as an old browser is being used.
      alert('Su navegador no soporta AJAX.');
   }

   return req;

}

var http = createRequestObject();
var box;

function is_empty(value) {
	if(value==null) {
		return true;
	}
	return (value=='');
}


function echeck(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("Email Inválido")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Email Inválido")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Email Inválido")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Email Inválido")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Email Inválido")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Email Inválido")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Email Inválido")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.getElementById("email_mailing")
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.focus()
		return false
	}
	return true
 }

function enviaremail(email) {

	if (ValidateForm() != true ){
		return;
	}

	var email1 = encodeURIComponent(document.getElementById("email_mailing").value);
	http.open('POST', 'ajax_enviar_email.php', true);
	http.onreadystatechange = handleajax;
	http.setRequestHeader('Encoding','iso-8859-1');
	http.setRequestHeader('Accept-Charset','iso-8859-1');
	http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	http.send('email='+ email1 );
}

function handleajax() {
	if(http.readyState == 4){
		if (http.status == 200){
			var response = http.responseText;
			if(response) {
				alert(response);
				document.getElementById("email_mailing").value = "";
			}else{
				alert(response);
			}
		}else{
			var response = http.responseText;
			if(response) {
				alert("Falla cargando la página\n" + response);
			}else{
				alert("Sin respuesta");
			}
		}
	} else {
		//document.getElementById(box).innerHTML = "Enviando datos ...";
	}
}

