/*email Story function */;
function emailClip(title, emailTemplate, uri, parent, site, cssClass){
    /* Main Variables */
    var self = this;
    this.emailTemplate = (emailTemplate) ? emailTemplate : "bhgShareThis"; 
    this.uri = uri;
    this.title = title;
    this.site = site;
    this.parentId = parent;
    this.recipient = "";

    /* Creates and sends email messages */
    this.send = function() {
        if (!this.verify()) {
            self.ed = new Object();
            self.ed.fromName = $("fname").value;
            self.ed.fromEmail = $("femail").value;
            self.ed.toName = $("uname").value;
            self.recipient = $("uname").value;
            self.ed.toEmail = $("uemail").value;
            self.ed.url = uri;
            self.ed.message = $("mess").value;
            self.ed.template = self.emailTemplate;
            self.ed.title = this.title;
            self.ed.sendCopyToSender = $("sendcopy").checked;
            ShareContentService.shareContent(ed, self.sent);
        }
    };

    /* Displays response message */
    this.sent = function() {
        $("alert").innerHTML = self.title + " has been sent to " + self.recipient + ". Your friend should receive it momentarily!";
        $("alert").style.display = "block";
        $("form").style.display = "none";
    };

    
    /* Verifies the form */
    this.verify = function() {
    	var hasError = false;
        if (!$('fname').value.length){
            $('fname').style.backgroundColor = "#FF6347";
            hasError = true;
        }
        else{
            $('fname').style.backgroundColor = "#FFFFFF";
        }

        if (!$('femail').value.length || !isValidEmail($('femail').value)){
            $('femail').style.backgroundColor = "#FF6347";
            hasError = true;
        }
        else{
            $('femail').style.backgroundColor = "#FFFFFF";
        }

        if (!$('uname').value.length){
            $('uname').style.backgroundColor = "#FF6347";
            hasError = true;
        }
        else{$('uname').style.backgroundColor = "#FFFFFF";
        }

        if (!$('uemail').value.length || !isValidEmail($('uemail').value)){
            $('uemail').style.backgroundColor = "#FF6347"; hasError = true;
        }
        else{
            $('uemail').style.backgroundColor = "#FFFFFF";
        }
        return hasError;
    };

    /* Removes the form from the page */
    this.removeEm = function() {
        $(self.parentId).removeChild($("emclp"));
    };

    /* Obtains the form via Ajax request */
    this.getForm = function(){
		new Ajax("/" + (this.site ? this.site : 'bhg') + "/templates/emailFriend/index.jsp", {method:'get', onComplete:self.showForm});
    };

    /* Appends the obtained form to the specified parent element */
    this.showForm = function(request){  	
        /* Add new div to body */
        var element = document.createElement("div");
        element.id = "emclp";
        if(cssClass) element.className = cssClass;
        element.innerHTML = request.responseText;
        $(self.parentId).appendChild(element);
    };

    /* Self execute */
    this.getForm();
}
