// JavaScript Document ///
// Custom toggle fade function
jQuery.fn.fadeToggle = function(speed, easing, callback){
    return this.animate({
        opacity: 'toggle'
    }, speed, easing, callback);
};

/* custom.js 
 to handle js common to wrapper template and to some page components
 part of re-design March 2010
 */
function sendZip(){
    var zipcode = $("#zipInput").val();
    if (zipcode.length < 5) {
        alert("Error in your zipcode");
    }
    else {
        var url = "/plan-b-locations.aspx";
        url += "?zipcode=" + zipcode;
        window.location = url;
    }
};

//function timeToolDisplay(){
//
/// add logic to hide any conflicting modal window ////

/*$.get("_modContent/timeTool.html", function(data) { 
 if($("#didKnow").css("display")!="none") {
 $("#didKnow").toggleClass("openedKnow",500);
 }
 
 $("#timeTool").toggleClass("openedTool",1000).html(data);
 $("#timeToolInputs").show();
 $("a.closer").click(function() {
 $("#timeTool").toggleClass("openedTool",500); }
 
 );
 
 $("#submitBtn").click(function(){
 
 timeToolResults();
 });
 
 });*/
//$.get("_modContent/timeTool.html", function(data) { 
/*if($("#didKnow").css("display")!="none") {
 $("#didKnow").toggleClass("openedKnow",500);
 }*/
//$("#timeTool").toggleClass("openedTool",1000).html(data);
//$("#timeToolInputs").show();
//$("a.closer").click(function() {
// $("#timeTool").toggleClass("openedTool",500); }

// );

//$("#submitBtn").click(function(){

//timeToolResults();
// });

// });


//}


/*function didKnowDisplay(){
 /// add logic to hide any conflicting modal window ////
 $.get("_modContent/didYouKnow.html", function(data){
 if ($("#timeTool").css("display") != "none") {
 $("#timeTool").toggleClass("openedTool", 500);
 }
 $("#didKnow").html(data).toggleClass("openedKnow", 1000);
 $("a.closerKnow").click(function(){
 $("#didKnow").toggleClass("openedKnow", 500);
 });
 });
 
 }*/
function timeToolResults(){
    //alert("yep hit");
    
	<!-- Activity Name for this tag is:What is Plan B_Time_Tool: start -->
	var rand = Math.floor(Math.random() * 10000000);
	spotlight_ping = new Image();	
	spotlight_ping.src = "http://ad.doubleclick.net/activity;src=1853101;type=planb307;cat=whati225;ord=1;num="+ rand;
	<!-- Activity Name for this tag is:What is Plan B_Time_Tool: end -->
	<!-- Activity GOOGLETRACK for this tag is:What is Plan B_Time_Tool: start -->
	pageTracker._trackEvent('TimeTool', 'Submit', 'Planbonestep');
	<!-- Activity GOOGLETRACK for this tag is:What is Plan B_Time_Tool: end -->	
    var $this = $(this);
    $this.find(".error").css("display", "none");
    var $result = $("#result");
    var $message = $(".msg");
    $this.find(".error").css("display", "none");
    
    var $dayValue = $("select[name=day] option:selected").val();
    var $hourValue = $("input#time").val();
    var $ampmValue = $("input:radio[name=ampm]:checked").val();
    $this.find(".error").css("display", "none");
    
    var $dayValue = $("select[name=day] option:selected").val();
    var $hourValue = $("input#time").val();
    var $ampmValue = $("input:radio[name=ampm]:checked").val();
    
    if ($dayValue == "" || ($hourValue == "" || !Number($hourValue) || Number($hourValue) > 12 || Number($hourValue) <= 0)) {
        if ($dayValue == "") {
            //alert("no day!");
            $("select[name=day]").parent().find(".error").css("display", "inline");
        }
        if ($hourValue == "" || !Number($hourValue) || Number($hourValue) > 12 || Number($hourValue) <= 0) {
            //alert("no hour!");
            $("input#time").parent().parent().find(".error").css("display", "inline");
        }
    }
    else {
        var $msg = "";
        $("#timeToolInputs").hide();
        var $totalDayHours = Number($dayValue) * 24;
        
        var $totalHours = Number($hourValue);
        if ($ampmValue != "AM") {
            if ($totalHours != 12) {
                $totalHours += 12;
            }
        }
        //
        var $currentDate = new Date();
        var $currentHour = $currentDate.getHours();
        /*$.log("Current hour is: " + $currentHour);
         $.log("Entered hour is: " + $hourValue);
         $.log("Considering ampm, the hour is: " + $totalHours);*/
        var $total = $totalDayHours + ($currentHour - $totalHours);
        var $remainingHours = 72 - $total;
        
        if ($remainingHours > 0 && $remainingHours <= 72) {
            $msg = "You have " + $remainingHours.toString() + " hours remaining to take Plan B One-Step. Please take it as soon as possible because the sooner you take it, the better it works.";
        }
        else 
            if ($remainingHours > 72) {
                $msg = "Please select a time earlier than the present.";
            }
            else 
                if ($remainingHours <= 0) {
                    $msg = "Plan B One-Step should be used within 72 hours after unprotected sex. Please contact a healthcare professional for advice.";
                }
        //9alert($msg);
        $message.text($msg);
        $result.fadeIn("slow");
    }
    
    
}
