﻿// Function to center an object 
jQuery.fn.center = function () 
{
    this.css("position","absolute");
    //this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( 1000 - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


$(document).ready
(
    function() 
    {
     
        // Show the Dialog if the hdnShowDialog (hidden field) is not empty
        if ( $("input[name$=hdnShowDialog]").val() != "" )
        {
            // Clear the form
            document.forms[0].reset();
        
            // Show the thank you dialog
            $(".dialog").center().show();
        }
        
        //-- Event handler for CLOSE BUTTON in the Dialog box
        $(".dialog .closeButton, .dialog #dialogClose").click
        (
            function()
            {
                $(".dialog").fadeOut(200);
                return false;
            }
        );
        $(".cvvDialog .closeButton, .cvvDialog #dialogClose").click
        (
            function()
            {
                $(".cvvDialog").fadeOut(200);
                return false;
            }
        ); 
        
        //-- Hover event for the CLOSE BUTTON in the Dialog box
        $(".dialog .closeButton, .cvvDialog .closeButton").hover
        (
            function()
            {
                $(".dialog .closeButton").css("background-position", "0 -20px");
                $(".cvvDialog .closeButton").css("background-position", "0 -20px");
            },
            function()
            {
                $(".dialog .closeButton").css("background-position", "0 0");
                $(".cvvDialog .closeButton").css("background-position", "0 0");
            }
        ); 
        
        //-- Event handler for RETURN HOME button in the Dialog box
        $(".dialog #returnHome").click
        (
            function()
            {
                window.location.href = "defaultAthlete.aspx";
            }
        ); 
        
        $(".whatIsThis").click(
            function()
            {
                $(".cvvDialog").show();
            }
        );
        
        //-- Click event handler for ADD ATHLETE
        $(".addAthlete").click
        (
            function()
            {
                //-- First see if there is another athlete box to display
                if($(this).parent().next().attr("class") == "athleteInfo")
                {
                    $(this).parent().next().fadeIn();
                }
                return false;
            }
        );
        //-- Click event handler for CLOSE ATHLETE
        $(".closeAthlete").click
        (
            function()
            {
                //-- Close the parent box
                $(this).parent().fadeOut();
                return false;
            }
        );
        
        
        //***************** FORM SCHOOL INFO CHANGE EVENT *****************************************************
        // Monitor school name
        $("#CoachInfo input[id$='SchoolName']").keyup
        (
            function() 
            { 
                $(".athleteInfo input[id$='School']").val($(this).val());
            }
        );
        // Monitor state
        $("#CoachInfo select[id$='State']").change
        (
            function() 
            { 
                $(".athleteInfo select[id$='State']").attr("selectedIndex", $(this).attr("selectedIndex"));
                //alert($(this).attr("selectedIndex"));
            }
        ); 
        // Monitor sport
        $("#CoachInfo select[id$='Sport']").change
        (
            function() 
            { 
                $(".athleteInfo select[id$='Sport']").attr("selectedIndex", $(this).attr("selectedIndex"));
                //alert($(this).attr("selectedIndex"));
            }
        ); 
        //*****************************************************************************************************
        
        
        
        //***************** PAYMENT PAGE(s) EVENTS ************************************************************
        //-- Add Passenger click event
        $(".addPassenger").click(
            function()
            {
                //-- Get the smallest (number) passenger id that isn't visible
                //$(this).parent().children(".addPax").eq(0).show();
                
                for (var i = 0; i <= 8; i++)
                {
                    if($(this).parent().children(".addPax").eq(i).is(":visible") == false)
                    {
                        $(this).parent().children(".addPax").eq(i).show();
                        break;
                    }
                    
                    if (i == 7)
                    {
                        $(this).hide();
                    }
                }
                
                return false;
            }
        );
        
        //-- Shows the PASSENGER row of detail if the user has entered some (usually encountered
        //-- when there is an error on the submit).
        for (var i = 0; i <= 8; i++)
        {
            if($(".addPax input[id*='passenger']").eq(i).val() != "")
            {
                $(".addPax").eq(i).show();
            }
        }
        
        
        $("#VisaImage").click(
            function()
            {
                $(this).siblings("select[id$='creditCard']").attr("selectedIndex", 0);
                return false;
            }
        );
        $("#McImage").click(
            function()
            {
                $(this).siblings("select[id$='creditCard']").attr("selectedIndex", 1);
                return false;
            }
        );
        $("#DiscoverImage").click(
            function()
            {
                $(this).siblings("select[id$='creditCard']").attr("selectedIndex", 2);
                return false;
            }
        );
        //-- Updates the Passenger1 name with the same name as the Billing info name
        $(".pageCols input[id$='firstName']").keyup(
            function()
            {
                if($(".pageCols input[id$='sameAsBilling']").attr("checked"))
                {
                    $(".pageCols input[id$='passenger1']").val($(this).val() + ' ' + $(".pageCols input[id$='lastName']").val());
                }
            }
        );
        //-- Updates the Passenger1 name with the same name as the Billing info name
        $(".pageCols input[id$='lastName']").keyup(
            function()
            {
                if($(".pageCols input[id$='sameAsBilling']").attr("checked"))
                {
                    $(".pageCols input[id$='passenger1']").val($(".pageCols input[id$='firstName']").val() + ' ' + $(this).val());
                }
            }
        );
        //-- Updates the passenger/participant state info with the same as the billing state
        $(".pageCols select[id$='state']").change
        (
            function() 
            { 
                $(".pageCols select[id*='statePax']").attr("selectedIndex", $(this).attr("selectedIndex"));
            }
        ); 
        //-- Keeps the TOTAL amount current as the users enter values for the passenger amount
        $(".pageCols input[id*='paxAmount']").change(
            function()
            {
                //-- First strip out any non numeric values
                $(this).val($(this).val().replace(/,/g,"").replace(/$/g,""));
                if(!(isNaN(parseFloat( $(this).val()))))
                {
                    var total = 0.0
                    for (var i = 1; i <= 10; i++)
                    {
                        total += parseFloat($(".pageCols input[id$='paxAmount" + i + "']").val().replace(",",""));
                    }
                    $(this).val(CurrencyFormatted(parseFloat($(this).val()).toFixed(2)));
                    $(".pageCols input[id$='paymentAmount']").val(total.toFixed(2));
                    $(".pageCols .paymentAmtDisplay").text("$" + CurrencyFormatted(total.toFixed(2)));
                    //$(this).val(Math.round(parseFloat($(this).val()*100)/100));
                    
                    //alert($(".pageCols input[id$='paymentAmount']").val());
                }
                else
                {
                    alert('You did not enter a valid value in the AMOUNT field.');
                    $(this).val('0.00');
                }
            }
        );
        //-- Changes the value of PASSENGER1 when the user clicks the checkbox
        $(".pageCols input[id$='sameAsBilling']").click(
            function()
            {
                if( $(this).attr("checked") )
                {
                    $(".pageCols input[id$='passenger1']").val($(".pageCols input[id$='firstName']").val() + ' ' + $(".pageCols input[id$='lastName']").val());
                }
                else
                {
                    $(".pageCols input[id$='passenger1']").val('');
                }
            }
        );
        //-- Click event for close passenger button
        $(".closePax").click(
            function()
            {
                // Zero out the Amount field
                $(this).siblings("input[id*='paxAmount']").val("0.00");
                $(this).siblings("input[id*='passenger']").val("");
                //$(this).siblings("select[id*='statePax']").attr("selectedIndex", 0);
                $(this).siblings("select[id*='teamPax']").attr("selectedIndex", 0);
                
                // Update the total payment amount
                var total = 0.0
                for (var i = 1; i <= 10; i++)
                {
                    total += parseFloat($(".pageCols input[id$='paxAmount" + i + "']").val().replace(",",""));
                }
                $(".pageCols input[id$='paymentAmount']").val(total.toFixed(2));
                $(".pageCols .paymentAmtDisplay").text("$" + CurrencyFormatted(total.toFixed(2)));
            
                $(this).parent().hide();
                $(".addPassenger").show();
                return false;
            }
        );
        //-- Click handler for printer friendly version
        $(".printerFriendly").click(
            function()
            {
                window.open("default_printfriendly.aspx");
                return false;
            }
        );
        
        if ($("#printerContent").length > 0)
        {
            //alert('hi');
            $("#printerContent").html(window.opener.$("#PaymentReceipt").html()); 
            window.print();
        }
        
        
        
        $(".fillTestData").click(
            function()
            {
                $(".pageCols input[id$='firstName']").val("Ryan");
                $(".pageCols input[id$='lastName']").val("Lifferth");
                $(".pageCols input[id$='address1']").val("1234 Somewhere Ave");
                $(".pageCols input[id$='city']").val("Layton");
                $(".pageCols select[id$='state']").attr("selectedIndex", 45);
                $(".pageCols input[id$='postalCode']").val("84041");
                $(".pageCols input[id$='phone']").val("801-555-1234");
                $(".pageCols input[id$='email']").val("ryanlifferth@gmail.com");
                
                $(".pageCols input[id$='sameAsBilling']").attr("checked", "true");
                $(".pageCols input[id$='passenger1']").val("Ryan Lifferth");
                $(".pageCols select[id$='statePax1']").attr("selectedIndex", 45);
                $(".pageCols select[id$='teamPax1']").attr("selectedIndex", 1);
                $(".addPax").eq(0).show();
                $(".pageCols input[id$='passenger2']").val("Aimee Fetui");
                $(".pageCols select[id$='statePax2']").attr("selectedIndex", 45);
                $(".pageCols select[id$='teamPax2']").attr("selectedIndex", 7);
                
                $(".pageCols input[id$='ccNumber']").val("4005550000000019");
                $(".pageCols select[id$='ccExpYear']").attr("selectedIndex", 1);
                                
                return false;
            }
        );
        //*****************************************************************************************************





        //---- REQUEST INFO NEXT YEAR formatting ---------------------------------------------------------------
        $("select[id$='sport']").change(
            function()
            {
                //alert( $(this).val() );
                switch ($(this).val())
                {
                    case "Football":
                    case "Girls Basketball":
                    case "Volleyball":
                        $(".individualSportsFields").hide();
                        $(".teamSportsFields").fadeIn();
                        break;
                    case "Track & Field":
                    case "Cross Country":
                        $(".teamSportsFields").hide();
                        $(".individualSportsFields").fadeIn();
                        break;
                    default:
                        $(".individualSportsFields").hide();
                        $(".teamSportsFields").hide();
                        break;
                }

            }
        );

        $(".addEvent").click(
            function()
            {
                for (var i = 0; i <= 8; i++)
                {
                    if($(this).parent().children(".newEvent").eq(i).is(":visible") == false)
                    {
                        $(this).parent().children(".newEvent").eq(i).show();
                        break;
                    }
                    
                    if (i == 7)
                    {
                        $(this).hide();
                    }
                }

                return false;
            }
        );


        //---- End REQUEST INFO NEXT YEAR formatting ----



       

    }
);


function CurrencyFormatted(amount)
{
    var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}