/* story tool functions */
var toolstate = "off";
var toolnames = new Array ();
toolnames['email'] = "email this story";

function storyTool(tool,url) {
	// set title of tool
	$("#toolname").empty();
	$("#toolname").append(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(toolstate == "off") {
                $("#toolbox").fadeIn("fast");
                toolstate = "on";
        }
}

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

function sendStory(theForm) {
	// validate form
	if(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  },
			success: function(msg){
				// clear tool message, display message from server
				$("#tool").empty();
				$("#tool").append(msg);
				$("#emailForm").empty();
				
				// close tool after delay
				$("#toolbox").fadeOut(3000);
				toolstate = "off";
			},
			error: function(){
				$("#tool").empty();
				$("#tool").append("There was a problem sending this story, please try again.");
			}
		});
	}

	return false;
}

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

function validate(theForm) {
  with(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;
    }
    var emailArr = to_email.value.split(',');
    if (emailArr.length > 5) {
      alert("Only 5 'To' email addresses are allowed!");
      to_email.focus();
      return false;
    }
    for (var i = 0; i < emailArr.length; i++) {
      if (!validateEmail(emailArr[i])) {
        alert("'To' email address [" + 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 (!validateEmail(from_email.value)) {
      alert("Please enter a valid 'From' email address!");
      from_email.focus();
      return false;
    }

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

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

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

/* story tool functions END */

