// JavaScript Document
var subscribe_in_progress = 0;

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("Oops! Bad email address.\r\\nTry again!")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert("Oops! Bad email address.\r\\nTry again!")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   alert("Oops! Bad email address.\r\\nTry again!")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   alert("Oops! Bad email address.\r\\nTry again!")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   alert("Oops! Bad email address.\r\\nTry again!")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   alert("Oops! Bad email address.\r\\nTry again!")
		    return false
		 }

 		 return true					
}

var subscribeSuccess = function(o) {
	if(o.responseText !== undefined){
	  subscribe_in_progress = 0;
	  if (o.responseText == "OK")
	    alert("Subscription accepted.");
	}
}

var handleFailure = function(o){
	if(o.responseText !== undefined){
		str = "Server error\r\n";
		str += "HTTP status: " + o.status + "\r\n";
		str += "Status code message: " + o.statusText;
		alert(str);
	}
}

var callback =
{ 
  success: subscribeSuccess,
  failure: handleFailure
};

function subscribe()
{
  if (subscribe_in_progress) 
  {
	  alert("Subscribe request already in progress.");
	  return;
  }
  else
    subscribe_in_progress = 1;
  
  var email = document.subscribe.email.value;
  // validate email
  if (email == "")
  {
    alert("You must enter an email address. Try again!");
	document.subscribe.email.focus();
	return;
  }
  
  if (echeck(email))
  {
    url = "svc/subscribe.php?email=" + email;  
    // send request
    var request = YAHOO.util.Connect.asyncRequest('GET', url, callback);
	document.subscribe.email.value = "";
  }
  else
  {
	subscribe_in_progress = 0;
	document.subscribe.email.focus();
  }
  
  return;
}

