/*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 = {};
            self.ed.fromName = $("#fname").val();
            self.ed.fromEmail = $("#femail").val();
            self.ed.toName = $("#uname").val();
            self.recipient = $("#uname").val();
            self.ed.toEmail = $("#uemail").val();
            self.ed.url = uri;
            self.ed.message = $("#mess").val();
            self.ed.template = self.emailTemplate;
            self.ed.title = this.title;
            self.ed.sendCopyToSender = $("#sendcopy").attr('checked');
            ShareContentService.shareContent(ed, self.sent);
        }
    };

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

    
    /* Verifies the form */
    this.verify = function() {
    	var hasError = false;
        if (!$('#fname').val().length){
            $('#fname').css('background-color',"#FF6347");
            hasError = true;
        }
        else{
            $('#fname').css('background-color',"#FFFFFF");
        }

        if (!$('#femail').val().length || !isValidEmail($('#femail').val())){
            $('#femail').css('background-color', "#FF6347");
            hasError = true;
        }
        else{
            $('#femail').css('background-color',"#FFFFFF");
        }

        if (!$('#uname').val().length){
            $('#uname').css('background-color',"#FF6347");
            hasError = true;
        }
        else{$('#uname').css('background-color',"#FFFFFF");
        }

        if (!$('#uemail').val().length || !isValidEmail($('#uemail').val())){
            $('#uemail').css('background-color',"#FF6347"); hasError = true;
        }
        else{
            $('#uemail').css('background-color',"#FFFFFF");
        }
        return hasError;
    };

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

    /* Obtains the form via Ajax request */
    this.getForm = function(){
		$.ajax({
            url:"/" + (this.site ? this.site : 'bhg') + "/templates/emailFriend/index.jsp",
            type:'get',
            success:function(html){
                $(self.showForm).html(html);
            }
        });
    };

    /* 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();
}

