/*
 * JavaScript code Copyright (c) 2009 Mike Landis, Purple Ivy.Net
 *
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 *
 * Date: 2009-09-28 
 * Revision: 1
 */
 

function valToInt(id) {
	var v;
	v = parseInt($(id).attr("value"));
	if (isNaN(v)) v = "0";
	return v;
}

function getSeatSelectionString(currentSelections, category, htmlColor, customerColor) {
    var returnString, numSeats;
    returnString = currentSelections;
    numSeats = $("#" + category + "_" + htmlColor).attr("value");
    if (numSeats > 0) {
        if (currentSelections != "") returnString += ", ";
        // force string concat cause Javascript stupidly uses + for that instead of . or & !!
        returnString += "" + numSeats + " " + customerColor + " seat";
        // it might be plural
        if (numSeats > 1) returnString += "s";
    }
    return returnString;
}
	
function recalculateTotalSeats() {
	// fix the numbers	
	var t;
	$("#seats_yellow" ).attr({value: valToInt("#seats_yellow")});
	$("#seats_silver" ).attr({value: valToInt("#seats_silver")});
	$("#seats_green"  ).attr({value: valToInt("#seats_green")});
	$("#seats_blue"   ).attr({value: valToInt("#seats_blue")});
	$("#seats_magenta").attr({value: valToInt("#seats_magenta")});		
	t = $("#seats_yellow" ).attr("value") * $("#seats_yellow" ).attr("price")   
	  + $("#seats_silver" ).attr("value") * $("#seats_silver" ).attr("price")    
	  + $("#seats_green" ).attr("value")  * $("#seats_green"  ).attr("price")  
	  + $("#seats_blue" ).attr("value")   * $("#seats_blue"   ).attr("price")  
	  + $("#seats_magenta").attr("value") * $("#seats_magenta").attr("price");
	$("#seats_total").attr({value : " " + t});

	recalculateTotals();
}

function recalculateTotalSeats3() {
	// fix the numbers	
	var t;
	$("#seats3_yellow" ).attr({value: valToInt("#seats3_yellow")});
	$("#seats3_silver" ).attr({value: valToInt("#seats3_silver")});
	$("#seats3_green"  ).attr({value: valToInt("#seats3_green")});
	$("#seats3_blue"   ).attr({value: valToInt("#seats3_blue")});
	$("#seats3_magenta").attr({value: valToInt("#seats3_magenta")});		
	t = $("#seats3_yellow" ).attr("value") * $("#seats3_yellow" ).attr("price") 
	  + $("#seats3_silver" ).attr("value") * $("#seats3_silver" ).attr("price") 
	  + $("#seats3_green" ).attr("value")  * $("#seats3_green"  ).attr("price") 
	  + $("#seats3_blue" ).attr("value")   * $("#seats3_blue"   ).attr("price") 
	  + $("#seats3_magenta").attr("value") * $("#seats3_magenta").attr("price");
	$("#seats3_total").attr({value : " " + t});

    if (t > 0) {
        $("#ShowSelectionNote").show(); 
        $("#FinalSelectedShows").html("Please Select Three Shows");
    }
    else {
        $("#ShowSelectionNote").hide();
        $("#FinalSelectedShows").html("");
    }
    
	recalculateTotals();
}

function fixGiftAmount() {
	var giftAmount;
	// fix gift amount to be an integer
	giftAmount = parseInt($("#gift_amount").attr('value'));
	if (isNaN(giftAmount)) giftAmount = 0;
	$("#gift_amount").attr({value: giftAmount });
	recalculateTotals();
}

function setGiftAmount(g) {
	$("#gift_amount").attr({value: g });
	recalculateTotals();	
}

function recalculateTotals() {
	var t, g, gt, gtNoShipping, s1, s2, seatSelections, seatSelectionsPlusShows;
	s1 = parseInt($("#seats_total").attr("value"));
	if (isNaN(s1)) s1 = 0;
	s2 =  parseInt($("#seats3_total").attr("value")); 
	if (isNaN(s2)) s2 = 0;
	t = s1 + s2 ;
	
	$("#season_total").attr({value: " " + t});
	g = parseInt( $("#gift_amount").attr("value") );
    gtNoShipping = g + t;
	gt = 5 + gtNoShipping;
	if (isNaN(gt) || t=="") gt="";
	$("#grand_total").attr({value: " " + gt});

    seatSelections = "";
    
    // parameters here are category, HTML id,  color name for customer
    seatSelections = getSeatSelectionString(seatSelections, "seats", "yellow",  "Gold");
    seatSelections = getSeatSelectionString(seatSelections, "seats", "silver",  "Silver");
    seatSelections = getSeatSelectionString(seatSelections, "seats", "green",   "Green");
    seatSelections = getSeatSelectionString(seatSelections, "seats", "blue",    "Blue");
    seatSelections = getSeatSelectionString(seatSelections, "seats", "magenta", "Purple");
    
    seatSelections = getSeatSelectionString(seatSelections, "seats3", "yellow",  "Gold Pick-3");
    seatSelections = getSeatSelectionString(seatSelections, "seats3", "silver",  "Silver Pick-3");
    seatSelections = getSeatSelectionString(seatSelections, "seats3", "green",   "Green Pick-3");
    seatSelections = getSeatSelectionString(seatSelections, "seats3", "blue",    "Blue Pick-3");
    seatSelections = getSeatSelectionString(seatSelections, "seats3", "magenta", "Purple Pick-3");
    $("#FinalSeatSelection").html(seatSelections);

    seatSelectionsPlusShows = $("#FinalSeatSelection").html();
    if ($("#numShowsSelected").attr("value") > 0) {
        seatSelectionsPlusShows += " - Shows: " + $("#FinalSelectedShows").html();
    }

    // Store the info in Paypal and Email Forms
    if ($("#gift_amount").attr("value") > 0) {
        seatSelectionsPlusShows = seatSelectionsPlusShows + ", $" + $("#gift_amount").attr('value') +  " donation";
    }
    $("#PaypalOrderDetails").attr({value: seatSelectionsPlusShows});
    $("#PaypalAmount").attr({value: " " + gtNoShipping}); // paypal adds the shipping

    $("#EmailOrderDetails").attr({value: seatSelectionsPlusShows});
    $("#EmailAmount").attr({value: " " + gt});

    
}


function recalculatePick3Selections() {
    var shows = "", numshows = 0, doalert = false;
    
    recalculateTotalSeats3();
    
    // count up the number of seats, and build the string of selections.
    for (var i=1; i<=5; i++) {
        if ($("#pick3" + i).attr("checked")) {
            if (numshows < 3) {
                if (shows != "") shows += ", ";
                    shows += $("#pick3" + i).attr("value");
                numshows++;
            }
            else {  // oops the customer picked too many! clear out the extras, set alert flag
                doalert = true;
                $("#pick3" + i).attr({"checked" : false});
            }
        }
    }
    
    if (doalert) alert("Please note that you can only select three shows for Pick-3.  Deselect the show you don't want to see in order to choose a different one.");
    
    switch (numshows) {
        case 0: $("#FinalSelectedShows").html("Please Select Three Shows"); break;
        case 1: $("#FinalSelectedShows").html(shows + " plus two more"); break;
        case 2: $("#FinalSelectedShows").html(shows + " plus one more"); break;
        case 3: $("#FinalSelectedShows").html(shows); break;
    }
    $("#numShowsSelected").attr({"value": numshows});
    
    recalculateTotals();
    
}

function fixFormField(t) {
    if ($('#' + t).attr('value') == "") {
        $('#' + t).attr({value: t.title});
        $('#' + t).addClass('notset');
    }
    else {
        $('#' + t).removeClass('notset');
    }  
}

function validateEmailForm() {
    var allSet = true;

  
    fixFormField('formName');
    fixFormField('formEmail');
    fixFormField('formConf');
    fixFormField('formPhone');
      
    // Only require name and email to actually be set before turning on submit button
    if ($('#formName').hasClass('notset')) allSet = false;
    if ($('#formEmail').hasClass('notset')) allSet = false;
    if ($('#formConf').hasClass('notset')) allSet = false;
    //if ($('#formPhone').hasClass('notset')) allSet = false;
    
    if (allSet) {
        if ($('#formEmail').attr('value') != $('#formConf').attr('value')) {
            $('#EmailIncompleteInfo').html('Please check your information above and confirm your email address');
            $('#formConf').addClass('notset').attr({value: " Confirm Email"});
            $('#EmailIncompleteInfo').show();
            $('#EmailCompleteInfo').hide();
        }
        else {
            $('#EmailIncompleteInfo').hide();
            $('#EmailCompleteInfo').show();
        }
    }
}

function clearEmailFormField(t) {

    if ($('#' + t.id).hasClass('notset')) {
        $('#' + t.id).removeClass('notset');
        $('#' + t.id).attr({value: ''});
    }
}

function setSubscribeOption(t) {
    $('#PaypalSubscribeOption').attr({value: t.value});
    $('#EmailSubscribeOption').attr({value: t.value});
}

    
$(document).ready(function() {

// turn off both payment options so the user can pick one.
$("#payManual").hide();  $("#payMethodManual").addClass("payMethodButtonOff");
$("#payPaypal").hide();  $("#payMethodPaypal").addClass("payMethodButtonOff");

// change initial value of "0" to blank on entry to a field everywhere for seats.
$('.inputText_seat').click( function(event) {
		if( event.target.value == "0" ) {
			 event.target.value = "";
		};
		}
	);
$('#payMethodManual').click( function() { 
	$("#payPaypal").hide(); $("#payManual").show(); 
	$("#payMethodPaypal").addClass("payMethodButtonOff");
	$("#payMethodManual").removeClass("payMethodButtonOff");
} );
$('#payMethodPaypal').click( function() { 
	$("#payPaypal").show(); $("#payManual").hide(); 
	$("#payMethodPaypal").removeClass("payMethodButtonOff");
	$("#payMethodManual").addClass("payMethodButtonOff");	
} );

$('.pick3').change(          function(event) { recalculatePick3Selections(); });
$('.seats').change(          function(event) { recalculateTotalSeats(); });
$('.seats3').change(         function(event) { recalculateTotalSeats3(); });
$('#gift_amount').change(    function(event) { fixGiftAmount(); });
$('#GiftsOfSupport').change( function(event) { setGiftAmount(event.target.value); } );
$('.formValidate').change(   function(event) { validateEmailForm(); } );
$('.formValidate').click(    function(event) { clearEmailFormField(event.target); } );
$('.subscribeOption').click( function(event) { setSubscribeOption(event.target); } );
fixGiftAmount();
recalculatePick3Selections();
validateEmailForm();
recalculateTotals();

});