var ajax_relay_url = 'http://www.nrsweb.org/includes/archive/relay_request.php';
var send_processor_url = 'http://events.nrsweb.org/mailer/send.php';
var send_email_subject = 'Document From NRS Archive: [fname]';
var send_email_text    = 'Please find attached the requested document from the NRS Archive.\r\nThanks for your interest';
var send_instructions = 'Enter your email address then click <i>send</i> to receive your copy of [fname]';
var send_title_info='Use this form to retrieve and archived document and have it sent to your email inbox';
var send_address_info='Enter your email address here';
var send_but_go_info='Click here to send document';
var send_but_can_info='Click here to back out';
var send_address_prompt='To:';
var send_isRelay = true;



/* *******************************************************************************************
 *
 *  send_create_form
 * 
 *  This function creates the form users will fill-out to send a specified document to
 *  their email inbox.
 *
 **********************************************************************************************/

function send_create_form()
{
   document.write('<div id="send_form_background_div">');
   document.write('<div id="send_form_floater_div">&nbsp;</div>');
   document.write('<div id="send_form_div">');
   document.write('<img id="send_form_title_img" src="../includes/archive/assets/title.gif" alt="archived document" title="'+send_title_info+'">');
   document.write('<form id="send_form_delivery" name="getit" method="post" action="'+send_processor_url+'">');
   document.write('<div id="send_instructions"></div>');
   document.write('<div class="send_prompt_container">');
   document.write('<div class="send_prompt_label"><label for="send_address">'+send_address_prompt+'</label></div>');
   document.write('<input class="send_prompt_value" id="send_address" type="text" title="'+send_address_info+'" name="email" value="" />');
   document.write('<input id="send_but_go" type="button" title="'+send_but_go_info+'" value="Send" />');
   document.write('<input id="send_but_can" type="button" title="'+send_but_can_info+'" value="Cancel" />');
   document.write('</div>');
   document.write('<input id="send_doc" type="hidden" name="attachment" value="http://www.nrsweb.org/docs/ImplementationGuidelines.pdf" />');
   document.write('<input id="send_subject" type="hidden" name="subject" value="'+send_email_subject+'" />');
   document.write('<input id="send_text" type="hidden" name="text" value="'+send_email_text+'" />');
   document.write('<div id="send_result"></div>');
   document.write('</form>');
   document.write('</div>');
   document.write('</div>');
}

/* *******************************************************************************************
 *
 *  send_promt4mail
 * 
 *  This function displays and populates entry form .
 *
 **********************************************************************************************/
function send_prompt4mail(obj)
{

	$("#send_form_background_div").css("height", $(document).height());
	$("#send_form_background_div").css("display", "block");
	$("#send_form_div").css("position", "absolute");
    $("#send_form_div").css("top", (($(window).height() - $("#send_form_div").outerHeight()) / 2) + $(window).scrollTop() + "px");
    $("#send_form_div").css("left", (($(window).width() - $("#send_form_div").outerWidth()) / 2) + $(window).scrollLeft() + "px");

	$("#send_doc").val(obj.href);
    fname = $("#send_doc").val().replace(/^.*(\\|\/|\:)/, '');
	$("#send_address").val("enter email address here");
	$("#send_subject").val(send_email_subject);
	$("#send_text").val(send_email_text);
	$('#send_result').html("&nbsp;");
    $("#send_instructions").html(send_instructions.replace('[fname]', fname));
}



/* *******************************************************************************************
 *
 *  JQuery Initializer
 * 
 *  This function initializes and activates the send functionality.  Any link having a
 *  css class="archive"  will be setup as an archive link.  When users click on the link
 *  a they are presented with a form into which they enter their email address. When they click
 *  their form's "send" button, the document is sent to them via email.
 *
 **********************************************************************************************/

//$(document).ready(function(){

function archiver_ready()
{
 // Hide overlay, if necessary
    $("#send_form_background_div").css("display", "none");



 // Setup click handler for any links tagged with the "archive" css class
    $(".archive").click(function(event) { send_prompt4mail(this); return false;});

 // Setup click handler to initiate retrieval of archived document
    $("#send_but_go").click(function(event){
      fname = $("#send_doc").val().replace(/^.*(\\|\/|\:)/, '');
	  subject = $("#send_subject").val().replace('[fname]', fname);
	  args = "attachment="+$("#send_doc").val()+"&subject="+subject+
	         "&text="+$("#send_text").val()+"&email="+$("#send_address").val();
  	  
	  if (send_isRelay == true) 
	      args = args + "&url="+send_processor_url;

 // Setup Callback to handle response from request processor
    cb = function(data) { 
      aResponse = data.split(';');
  	  if  (aResponse[0] == 0)
 	       $('#send_result').html(aResponse[1]); 
	  else $("#send_form_background_div").css("display", "none");
	  };

    // If using AJAX relay functionality, then post to relay script, else send directly
	// directly to request processor.
	   if (send_isRelay == true)
            jQuery.post(ajax_relay_url, args, cb);
	   else jQuery.post(send_processor_url, args, cb);
	   return false;

   });

 // Setup click handler for cancel button
    $("#send_but_can").click(function(event){
    $("#send_form_background_div").css("display", "none");
	});

}
//); Use only when implementing jQuery Ready Function Here



// Load css file (Hardcoded for now -- need better solution)
    document.write('<link href="../../includes/archive/send.css" rel="stylesheet" type="text/css">');
   
 // Generate code for the form
	send_create_form();

