﻿// Copyright (c) 2010 PageBean, Inc.  (http://www.pagebean.com)
// All rights reserved.

$(document).ready
(
	function() {
	    onCallCenterHoursResize(true);

	    $(".simplePopup a").click
		(
			function() {
			    $.ajax
				(
					{
					    url: $(this).attr("content"),
					    data: {},
					    cache: true,
					    success: function(data) {
					    $(".simplePopupWrapper").html("");
					    $(".simplePopupWrapper").html(data);
					        onCallCenterHoursResize(false);
					    }
					}
				);

					$("#simplePopupOverlay").fadeTo(500, 0.45);
					$("#simplePopupDialog").delay(200).fadeIn(300);
			}
		);		

			$("#simplePopupClose").live
		(
			"click",
			function() {
	    $("#simplePopupOverlay").fadeOut(500);
	    $("#simplePopupDialog").fadeOut(200);
			}
		)

	    $(window).resize
		(
			function() {
	    onCallCenterHoursResize(true);
			}
		);

	    $(window).scroll
		(
			function() {
	    onCallCenterHoursResize(true);
			}
		);

	    $("#lnkSendEmail").live
		(
			"click", function() {
			    if ($(this).hasClass("button9")) {
			        var errorMessage = "ERROR(S):<br>";
			        if ($("#txt_emailTo").val() == "") {
			            errorMessage += "You must enter at least one e-mail address for the recipient(s).<br>"
			        }
			        if ($("#txt_emailFrom").val() == "") {
			            errorMessage += "You must enter an e-mail address for the sender."
			        }
			        if (errorMessage != "ERROR(s):<br>") {
			            $("#lbl_errorMessage2").html(errorMessage);
			            $("#panel_errorMessage2").css('visibility', 'visible');
			        }
			        else {
			            $.ajax
					(
						{
						    url: "/ajax/sendEmail.aspx",
						    data:
							{
							    edpNo: $("#h_edpno").val(),
							    mailTo: $("#txt_emailTo").val(),
							    mailFrom: $("#txt_emailFrom").val(),
							    note: $("#txt_note").val(),
							    sendCopy: $("#chk_sendCopy")[0].checked
							},
						    success: function() {
						    $("#simplePopupOverlay").fadeOut(500);
						    $("#simplePopupDialog").fadeOut(200);
						        $("html, body").animate({ scrollTop: 0 }, 1000);
						    },
						    failure: function() {
						        $("#txt_emailTo").val() = "FAIL";
						    }
						}
					);
			        }
			    }
			}
		);
			$("#simplePopupOverlay").click(
			    function ()
			    {
			        $("#simplePopupOverlay").fadeOut(500);
			        $("#simplePopupDialog").fadeOut(200);
			    }
			)
	}
);

	function onCallCenterHoursResize(stop) {
    if ($(window).height() > $("body").height()) {
        $("#simplePopupOverlay").css("height", $(window).height() + "px");
    }
    else {
        $("#simplePopupOverlay").css("height", $("body").height() + "px");
    }

    $("#simplePopupOverlay").css("left", $(window).scrollLeft() + "px");

    if (stop) {
        $("#simplePopupDialog").stop(true, true);
    }

    if ($("#simplePopupDialog").height() < $(window).height()) {
        $("#simplePopupDialog").animate({ top: ($(window).height() / 2) - ($("#simplePopupDialog").height() / 2) + $(window).scrollTop() + "px" }, 250);
    }
    else {
        $("#simplePopupDialog").css("top", "0px");
    }

    if ($("#simplePopupDialog").width() < $(window).width()) {
        $("#simplePopupDialog").animate({ left: ($(window).width() / 2) - ($("#simplePopupDialog").width() / 2) + $(window).scrollLeft() + "px" }, 250);
    }
    else {
        $("#simplePopupDialog").css("left", "0px");
    }
}
