/*
This javascript is an object oriented version of MI's Story Tools.  Much of the code here was developed by Lucas Myers.
It has since been slightly modified to fit into the Reference Site, and some additions have been made.
Gabe Doliner made it object oriented.

This script is called from and used by the story detail template only
*/

/* object constructor */
function miStoryTool() {
    /* story tool functions */
    this.toolstate = "off";
    this.toolnames = new Array();
    this.toolnames['email'] = "email this story";

    //initialize the story tool
    this.storyTool = function( tool, url ) {
        // set title of tool
        $("#toolname").empty();
        $("#toolname").append(this.toolnames[tool]);
        // clean up tool area
        $("#tool").empty();
        $("#tool").append("loading...");

        // send request for tool, display if loads ok otherwise display error
        $.ajax({
            type: "GET",
            url: url,
            success: function(msg) {
                $("#tool").empty();
                $("#tool").append(msg);
            },
            error: function() {
                $("#tool").empty();
                $("#tool").append("There was a problem loading this tool.");
            }
        });

        // display the toolbox
        if ( this.toolstate == "off" ) {
            $("#toolbox").fadeIn("fast");
            this.toolstate = "on";
        }
    }

    //close the tool
    this.closeTool = function() {
        // hide toolbox
        $("#toolbox").fadeOut("fast");
        this.toolstate = "off";
        //$("#toolbox").css("top","0px");
    }

    
    this.sendStory = function(theForm) {
        // validate form
        if ( this.validate(theForm) === true )
        {
            // clear tool and display message
            $("#tool").empty();
            $("#tool").append("sending...");

            // send form for processing
            $.ajax({
                type: "POST",
                url: "/cgi-bin/mi/mailastory.cgi",
                data: { domain: theForm.domain.value, url_form: theForm.url_form.value, email_type: theForm.email_type.value, url_text: theForm.url_text.value, url_html: theForm.url_html.value, url_story: theForm.url_story.value, to_email: theForm.to_email.value, from_email: theForm.from_email.value, from_name: theForm.from_name.value, comments: theForm.comments.value, headline: theForm.headline.value },
                success: function(msg){
                    // clear tool message, display message from server
                    $("#tool").empty();
                    $("#tool").append(msg);
                    $("#emailForm").empty();
                    
                    // close tool after delay
                    $("#toolbox").fadeOut(3000);
                    this.toolstate = "off";
                },
                error: function(){
                    $("#tool").empty();
                    $("#tool").append("There was a problem sending this story, please try again.");
                }
            });
        }

        return false;
    }

    this.mvForm = function() {
        this.adj = $("#story_body").height() - 150;
        $("#toolbox").css( "top", this.adj );
    }

    this.validate = function (theForm) {
        this.theForm = theForm;
        with(this.theForm)
        {
            // CHECK NAME
            if (from_name.value == "") {
                alert("Please enter your name.");
                from_name.focus();
                return false;
            }

            // Check "To" email address(es)
            if ( to_email.value == "" ) {
                alert( "Please enter a 'To' email address!" );
                to_email.focus();
                return false;
            }
            this.emailArr = to_email.value.split(',');
            if ( this.emailArr.length > 5 ) {
                alert( "Only 5 'To' email addresses are allowed!" );
                to_email.focus();
                return false;
            }
            for (var i = 0; i < this.emailArr.length; i++) {
                if ( !this.validateEmail( this.emailArr[i] ) ) {
                    alert( "'To' email address [" + this.emailArr[i] + "] is invalid" );
                    to_email.focus();
                    return false;
                }
            }

            // Check "From" email address
            if ( from_email.value == "" ) {
                alert("Please enter a 'From' email address!");
                from_email.focus();
                return false;
            }
            if ( !this.validateEmail ( from_email.value ) ) {
                alert("Please enter a valid 'From' email address!");
                from_email.focus();
                return false;
            }

            return(true);
        }  //  with(theForm)
    }  //  END  validate()

    this.trim = function(str) {
        this.str = str;
        return this.str.replace(/^\s+|\s+$/g, '');
    }

    this.validateEmail = function(valfield) {
        this.tfld = this.trim( valfield );  // value of field with whitespace trimmed off
        this.email = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
        return ( !this.email.test( this.tfld ) ) ? false : true;
    }
}
/* end object constructor */

/* instantiate the object */
mi_story_tool = new miStoryTool();
