/*
 * Mail Ajax Utility
 */
  var MailUtil = {

    version: '1.0.1',
    request : null,
   
    //Uses AJAX to send the request to the MailHandler.ashx page
    sendEmail : function() {
       var valid = this.validation();
       $("lblMailAFriend").innerHTML = "";
       if(valid == "true")
       {
            this.doAjaxRequest("/AJAX/MailHandler.ashx",
                "uni=" + $("uniId").value +  "&to=" + $("txtEmailTo").value + "&from=" + $("txtEmailFrom").value,
                mailAjaxHandler
                );
       }
       else {
            $("lblMailAFriend").innerHTML = valid;
       }
       return false;
    },
    //Sends a comment via email to a recipient at UnionView
    sendComment : function() {
        var msg = "loading ...";
        try {
            if($("txtComments").value.length>0) {
                $("imgLoading").style.display = "";
                
                var comments = escape($("txtComments").value);
                this.doAjaxRequest("/AJAX/MailHandler.ashx",
                    "comments=" + comments,
                    mailAjaxHandler
                    );        
            }
            else {
                 msg = "No comments have been made";
            }
        }
        catch(e) {
            msg = e;
        }
        
        $("lblErrorMessage").innerHTML = msg;
    },
    
    //Validate that a from and to address have been entered
    validation : function() {
    
        this.setColor($("txtEmailTo")    ,"#707070");
        this.setColor($("txtEmailFrom")  , "#707070");
        
        if(!this.isEmail($("txtEmailTo").value))
        {
            this.setColor($("txtEmailTo")    ,"crimson");
            return "To address is invalid";
        }
        if(!this.isEmail($("txtEmailFrom").value))
        {
            this.setColor($("txtEmailFrom")    ,"crimson");
            return "From address is invalid";
        }
        return "true";
    },
    setColor: function(obj, color) {
        obj.style.color = color;
    },
    
    //Checks to see if the value is a valid email
    isEmail : function (sEmail) {
        var ePattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
        return ePattern.test(sEmail);
    },
    
    //The onblur event of the input box
    onBlur : function(obj, text) {
        if(obj.value.length == 0)
            obj.value = text;    
    },
    
    //The onfocus event of the input box
    onFocus : function(obj, text) {
        if(obj.value == text)
            obj.value = "";
    },
    
     //When an email has successfully sent
     renderSuccessMessage : function(msg) {
        $("InviteFriend").style.display = "none";
        $("SuccessfullySent").style.display = "";
     },
     
     //When a comment has been successfully sent.
     renderCommentSuccess : function(msg) {
        $("imgLoading").style.display = "none";
        $("lblErrorMessage").innerHTML = msg;
     },
     
     //This performs the AJAX request
     doAjaxRequest : function(url, params, completeHandler) {
        try {
            var req = this.request = getXmlHttpRequest(); 
            req.onreadystatechange = completeHandler;
            req.open("POST", url, true);
            req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            req.send(params);
        }
        catch(e)
        {
            alert(e);
        }
     }
     
     
}
//This is called when the AJAX Handler page sends the results back to the page
function mailAjaxHandler() {
    try {
        var xhr = MailUtil.request
        if(xhr.readyState == 4) {

                eval(xhr.responseText);
                MailUtil.request = null;
            }
    }catch(ex)
    {
        alert(ex);
    }
        
         
}
