/* Validation routines for email this graph */

function EG_Validate_Emails() {
  var femail = document.getElementById("f_from_email");
  var temail = document.getElementById("f_to_email");
  var msg    = '';
  
  if (!isValidEmail(femail.value)) {
    msg += "Invalid From email address\n";
    femail.focus();
  }
  if (!isValidEmail(temail.value)) {
    msg += "Invalid To email address\n";
    temail.focus();
  }
  if (msg.length > 0) {
    alert(msg);
    return(false);
  }
  else {
    return (true);
  }
}

function EG_submit() {
  var isvalid = EG_Validate_Emails();
  if (isvalid) {
    
    var form   = document.getElementById("SH_emailgraph");
    var params = 'q=' + encodeURI(form.q.value) + '&l=' + encodeURI(form.l.value) + '&from_email=' + encodeURI(form.from_email.value) + '&to_email=' + encodeURI(form.to_email.value);
    var uri    = form.action + '?' + params;

    //callback to handle request
    var callback = {
      success: function(o) { //success handler
        var rtntxt = o.responseText;
        if( rtntxt.indexOf('sent successfully') != -1 ) {
          alert('Email was successfully sent');
        }
        else {
          alert('Error occurred sending the email - please check your email addresses');
        }
      },
      failure: function(o) { //failure handler
        alert("Unable to send the email");
      }
    };

    //perform asynchronous request
    YAHOO.util.Connect.asyncRequest('GET',uri,callback);

  }
  // Always return false so we don't submit the request twice
  return false;
}

