// JavaScript Document

var xhr = false;

function update_email(subs_type){
	
	if(ValidateForm()){
		
		var subscribe_type = subs_type;		
		var email_id = 	document.getElementById("txtEmail");
		var security_code = "";
		
		if(subscribe_type=='subscribe'){
			document.getElementById("btnSubscribe").value="wait...";			
		}else if(subscribe_type=='unsubscribe'){
			security_code = document.getElementById("txtSecurity_code");					
			document.getElementById("imgUnsubscribe").src="loading.gif";	
		}
				
		var url = "update_email.php";
		var postdata = "email=" + email_id.value + "&subscribe_type=" + subscribe_type + "&security_code=" + security_code.value;
		
		xhr = setXHR();
		xhr.open("POST", url, true);
		xhr.setRequestHeader("content-Type", "application/x-www-form-urlencoded");
		xhr.onreadystatechange=update_email_callback;
		xhr.send(postdata);
				
	}				
}

function update_email_callback(){
	if (xhr.readyState == 4 &&  xhr.status==200) {			 
		var rs=xhr.responseText;		
		var email_response = rs.parseJSON();
										
		var email_id=document.getElementById("txtEmail");
		message = email_response.message;	
				
		if(email_response.subscribe_type=='unsubscribe'){
			var security_code = document.getElementById("txtSecurity_code");
			if(email_response.captcha_match_status==false){
				window.location = "newsletter_unsubscribe.php?email_id=" + email_id.value + "&message="+message;				
			} else{				
				
				var unsubscribe_msg = " <p id=hi>";
					unsubscribe_msg += "Hi, ";
					unsubscribe_msg += "<br />";
					unsubscribe_msg += "</p>";
				
					unsubscribe_msg += "<p id=content>";
					unsubscribe_msg += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; 
					unsubscribe_msg += "Your Email has been removed from our newletter list. Your e-mail address will be automatically obtained from the e-mail message and remove you from the NewsLetter list. <br /> <br />";			        
				unsubscribe_msg += "</p>";
				
				unsubscribe_msg += "<p id=signature>";
				unsubscribe_msg += "Thank You, <br />";
		
				unsubscribe_msg += "ex-sight.com <br /><br />";
				unsubscribe_msg += "</p>";
				
				unsubscribe_msg += "<br />";
				
				var divmessage = document.getElementById("divSubscribe");
				divmessage.innerHTML = unsubscribe_msg;	
		
			}
		} else {		
		
			alert(message);		
			document.getElementById("btnSubscribe").value="Subscribe";	
			
			emailID.value=""
			emailID.focus()
			return false		
		}
		
		email_id.focus();
		return false;		

	}	
}


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("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Invalid E-mail ID")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail ID")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail ID")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail ID")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail ID")
		return false
	 }

	 return true					
}

function ValidateForm(){
	var emailID=document.getElementById("txtEmail");
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
}
