var FLAG='true';

function ajax_call()
{
  if(ge("rcpt").value !='' &&  ge("from").value !=''){
   check_email();
   ge('remail').focus();
  }
}

function check_email()
{ 
  createXMLRequest();
    var frm_email_id =ge("from").value;
      var url = "ajaxgateway.php?action=CheckUserEmail&email_id="+frm_email_id;
      var parames = "email_id=" + escape(frm_email_id);
      xmlHttp.open("POST", url, true);
      xmlHttp.onreadystatechange = handleState;
      xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
      xmlHttp.send(parames);
      // disable the submit button after ajax call
      document.getElementById('send-button-div').innerHTML=' <a href="#" onMouseOver="imgOn(\'sendit\');" onMouseOut="imgOver(\'sendit\');"> <img src="/en/images/btns/sendit_off.gif" alt="Send It" name="sendit" /></a>';
      showpopup();
}

// This function will wait for 20 sec for Ajax response, failing which it will show a popup message
function showpopup()
{
  setTimeout('', 20000);
  if(FLAG=='true'){
    alert ("Please log in to your YouSendIt account, or if you do not have an account, please re-enter your email address in the 'From' field.");
   }
}

function createXMLRequest()
{
   if (window.ActiveXObject){
     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   else if (window.XMLHttpRequest){
     xmlHttp = new XMLHttpRequest();
   }
}

function handleState()
{
   FLAG='false';
   serverNotAvailable = new Error("Please log in to your YouSendIt account, or if you do not have an account, please re-enter your email address in the 'From' field."); 
   try{
      if(xmlHttp.readyState == 4){ 
        if(xmlHttp.status == 200){
          var response = xmlHttp.responseText;
          var result = eval('(' + response + ')');
          var email= ge("from").value;
          if ('EMAIL_EXISTS' == result.message_code){ //if email_id in 'From' field exists redirect user to login page
           window.location = "/login.php?ss=407&email="+email;
          }
           else if ('EMAIL_DOESNOT_EXISTS' == result.message_code){ // enable again the 'Submit' button
               document.getElementById('send-button-div').innerHTML='<a href="#" onclick="omniture_track_upload_start();start_uploadmgr(\'home\');" onMouseOver="imgOn(\'sendit\');" onMouseOut="imgOver(\'sendit\');"> <img src="/en/images/btns/sendit_off.gif" alt="Send It" name="sendit" /></a>';     
           }
       }
      else if (xmlHttp.status == 404){
           alert('Service not available');  
       }
      else
          throw serverNotAvailable;
    }
   
  }catch(e){
         alert ("Please log in to your YouSendIt account, or if you do not have an account, please re-enter your email address in the 'From' field.");
         return false;
         }  
}


