// JavaScript Document

$(document).ready(function(){ showTotalPromise(); hideForm(); zebraUL(); showHideInformation(); loadTotals(); checkName(); checkEmail(); checkPhone();});

$(document).ready(function(){
	$('.warning').remove();
	$.get("../assets/php/token.php",function(txt){
  		$(".secure").append('<input type="hidden" name="ts" value="'+txt+'" />');
	});
});

$(document).ready(function() {
    $("a[@rel='external']").addClass("external").click(function() { window.open($(this).attr('href')); return false; });
});

function zebraUL(){
    $(document).ready(function(){
        $(".zebra li:even").addClass("alt");
        //$(".zebra li:odd").addClass("reg");
    });
}

function hideForm(){
    $(document).ready(function(){
    	$(document).ready(function() {
		  $('#accordian> div').hide(); 
		  $('#accordian> h2').click(function() {
			var $nextDiv = $(this).next();
			var $visibleSiblings = $nextDiv.siblings('div:visible');
		 
			if ($visibleSiblings.length ) {
                            $visibleSiblings.slideUp('slow', function() {
                            $nextDiv.slideToggle('slow');
                            $("#bottom_section").css("height","1200px");
			  });
			} else {
			   $nextDiv.slideToggle('slow');
                           $("#bottom_section").css("height","1200px");
			}
		  });
	});
        $("#eat_js").click(function(){
            $("#eat_form_js").slideToggle("slow");
            $("#drink_form_js").slideUp('slow');
            $("#go_form_js").slideUp('slow');
            $("#live_form_js").slideUp('slow');
            $("#mail_form_js").slideUp('slow');
            $("#trash_form_js").slideUp('slow');
            $("#work_form_js").slideUp('slow');
            $("#bottom_section").css("height","1200px");
            return false;
        });
        $("#drink_js").click(function(){
            $("#drink_form_js").slideToggle("slow");
            $("#eat_form_js").slideUp('slow');
            $("#go_form_js").slideUp('slow');
            $("#live_form_js").slideUp('slow');
            $("#mail_form_js").slideUp('slow');
            $("#trash_form_js").slideUp('slow');
            $("#work_form_js").slideUp('slow');
            $("#bottom_section").css("height","1200px");
            return false;
        });
        $("#go_js a").click(function(){
            $("#go_form_js").slideToggle("slow");
            $("#eat_form_js").slideUp('slow');
            $("#drink_form_js").slideUp('slow');
            $("#live_form_js").slideUp('slow');
            $("#mail_form_js").slideUp('slow');
            $("#trash_form_js").slideUp('slow');
            $("#work_form_js").slideUp('slow');
            $("#bottom_section").css("height","1200px");
            return false;
        });
        $("#live_js a").click(function(){
            $("#live_form_js").slideToggle("slow");
            $("#eat_form_js").slideUp('slow');
            $("#drink_form_js").slideUp('slow');
            $("#go_form_js").slideUp('slow');
            $("#mail_form_js").slideUp('slow');
            $("#trash_form_js").slideUp('slow');
            $("#work_form_js").slideUp('slow');
            $("#bottom_section").css("height","1200px");
            return false;
        });
        $("#mail_js a").click(function(){
            $("#mail_form_js").slideToggle("slow");
            $("#eat_form_js").slideUp('slow');
            $("#drink_form_js").slideUp('slow');
            $("#go_form_js").slideUp('slow');
            $("#live_form_js").slideUp('slow');
            $("#trash_form_js").slideUp('slow');
            $("#work_form_js").slideUp('slow');
            $("#bottom_section").css("height","1200px");
            return false;
        });
        $("#trash_js a").click(function(){
            $("#trash_form_js").slideToggle("slow");
            $("#eat_form_js").slideUp('slow');
            $("#drink_form_js").slideUp('slow');
            $("#go_form_js").slideUp('slow');
            $("#live_form_js").slideUp('slow');
            $("#mail_form_js").slideUp('slow');
            $("#work_form_js").slideUp('slow');
            $("#bottom_section").css("height","1200px");
            return false;
        });
        $("#work_js a").click(function(){
            $("#work_form_js").slideToggle("slow");
            $("#eat_form_js").slideUp('slow');
            $("#drink_form_js").slideUp('slow');
            $("#go_form_js").slideUp('slow');
            $("#live_form_js").slideUp('slow');
            $("#mail_form_js").slideUp('slow');
            $("#trash_form_js").slideUp('slow');
            $("#bottom_section").css("height","1200px");
            return false;
        });
        $("#goodperson h2").click(function(){
            $("#accordian div:visible").slideUp("slow");
	    $("#bottom_section").css("height","575px");
            return false;
        });
    });
}

function showHideInformation() {
    $(document).ready(function() {
        $(".extra_info p").hide();
        //$("#extra_info a").click(function(){$("#extra_info.p").toggle(); return false;} );
        $(".extra_info a").click(function(){$(this).next().toggle(); return false;} );
        //$(this).parent().next().slideDown("slow");
        
    });
}

function showTotalPromise() {
    $(document).ready(function() {
        //Make the call to the XML web service
        $.get("../assets/php/currentTotalPromise.php", function(currentTotalPromise) {

            //Format the time returned by the server
			var temp = $("total", currentTotalPromise).text();
            var total = [ $("total", currentTotalPromise).text()];
			var people = [ $("people", currentTotalPromise).text()];
			var americans = [(Math.floor(temp/1500))];

            //Determine how many milliseconds to will wait until 
            //the time needs to be refreshed again
            var refresh = [(30 - $("sec", currentTotalPromise).text()) * 1000 ];

            //Display the time on the end-user client
            $("#promise_js").html(total.join(''));
			$("#people_js").html(people.join(''));
			$("#american_js").html(americans.join(''));

            //Set a timer so that the time on the end-user client updates 
            // in sync with the server time to display the true current time
            setTimeout('showTotalPromise()', refresh);
        });
    });
}

function showUserPromise() {
    $(document).ready(function() {
        $.get("../assets/php/currentUserPromise.php", function(currentUserPromise) {
            var total = [ $("total", currentUserPromise).text()];
            var refresh = [(22 - $("sec", currentUserPromise).text()) * 1000 ];
            $("#user_js").html(total.join(''));
            setTimeout('showUserPromise()', refresh);
        });
    });
}

function loadTotals() {
    $(document).ready(function() { 
		var total = 0;
		
		var eat1 = $("#eat1").is(':checked');
		var eat2 = $("#eat2").is(':checked');
		var eat3 = $("#eat3").is(':checked');
		var drink1 = $("#drink1").is(':checked');
		var drink2 = $("#drink2").is(':checked');
		var go1 = $("#go1").is(':checked');
		var go2 = $("#go2").is(':checked');
		var go3 = $("#go3").is(':checked');
		var go4 = $("#go4").is(':checked');
		var live1 = $("#live1").is(':checked');
		var live2 = $("#live2").is(':checked');
		var live3 = $("#live3").is(':checked');
		var live4 = $("#live4").is(':checked');
		var live5 = $("#live5").is(':checked');
		var live6 = $("#live6").is(':checked');
		var live7 = $("#live7").is(':checked');
		var live8 = $("#live8").is(':checked');
		var live9 = $("#live9").is(':checked');
		var live10 = $("#live10").is(':checked');
		var mail1 = $("#mail1").is(':checked');
		var mail2 = $("#mail2").is(':checked');
		var trash1 = $("#trash1").is(':checked');
		var trash2 = $("#trash2").is(':checked');
		var trash3 = $("#trash3").is(':checked');
		var trash4 = $("#trash4").is(':checked');
		var work1 = $("#work1").is(':checked');
		var work2 = $("#work2").is(':checked');
		var work3 = $("#work3").is(':checked');
		
		// EAT
		if(eat1){
			// Savings: 2 pounds per day
			total += $("#eat1_2").val() * 2;
			var eat1_total = [$("#eat1_2").val() * 2];
			$("#eat1_hidden").attr('value', eat1_total.join(''));
		}
		if(eat2){
			// Savings: 666 pounds per day
			total += $("#eat2_2").val() * 1;
			var eat2_total = [$("#eat2_2").val() * 1];
			$("#eat2_hidden").attr('value', eat2_total.join(''));
		}
		if(eat3){
			// SAVINGS: 1.5 pounds per day
			total += $("#eat3_2").val() * 1.5;
			var eat3_total = [$("#eat3_2").val() * 1.5];
			$("#eat3_hidden").attr('value', eat3_total.join(''));
		}
		
		// DRINK
		if(drink1){
			// Savings: 166 lbs per year, 14 lbs a month, 3.4 lbs a week
			if($("#drink1_2").val() == "1"){
				total += 166;
				var drink1_total = [166];
			}else if($("#drink1_2").val() == "2"){
				total += 14;
				var drink1_total = [14];
			}else if($("#drink1_2").val() == "3"){
				total += 3.4;
				var drink1_total = [3.4];
			}
			$("#drink1_hidden").attr('value', drink1_total.join(''));
		}
		if(drink2){
			// Savings: 1.25 lbs for five days; .25 lbs per day
			total += $("#drink2_2").val() * .25;
			var drink2_total = [$("#drink2_2").val() * .25];
			$("#drink2_hidden").attr('value', drink2_total.join(''));
		}
		
		// GO
		if(go1){
			// Savings: 2.2 lbs per day
			total += $("#go1_2").val() * 2.2;
			var go1_total = [$("#go1_2").val() * 2.2];
			$("#go1_hidden").attr('value', go1_total.join(''));
		}
		if(go2){
			// Savings: 327 lbs per year, 27 lbs per month
			if($("#go2_2").val() == "1"){
				total += 327;
				var go2_total = [327];
			}else if($("#go2_2").val() == "2"){
				total += 27;
				var go2_total = [27];
			}
			$("#go2_hidden").attr('value', go2_total.join(''));
		}
		if(go3){
			// Savings: 67 pounds a month
			total += 67;
			var go3_total = [67];
			$("#go3_hidden").attr('value', go3_total.join(''));
		}
		if(go4){
			// Savings: 205 lbs per trip
			total += $("#go4_2").val() * 205;
			var go4_total = [$("#go4_2").val() * 205];
			$("#go4_hidden").attr('value', go4_total.join(''));
		}
		
		// LIVE
		if(live1){
			// Savings: 100 lbs annually per bulb
			total += $("#live1_2").val() * 100;
			var live1_total = [$("#live1_2").val() * 100];
			$("#live1_hidden").attr('value', live1_total.join(''));
		}
		if(live2){
			// Savings: 2 lbs per week
			total += 2;
			var live2_total = [2];
			$("#live2_hidden").attr('value', live2_total.join(''));
		}
		if(live3){
			// Savings: 250 lbs per year, 0.7 lbs per day
			total += 250;
			var live3_total = [250];
			$("#live3_hidden").attr('value', live3_total.join(''));
		}
		if(live4){
			// Savings: 342 lbs per year, 0.94 lbs per day
			total += $("#live4_2").val() * 0.94;
			var live4_total = [$("#live4_2").val() * 0.94];
			$("#live4_hidden").attr('value', live4_total.join(''));
		}
		if(live5){
			// Savings: 500 lbs per year per degree, 42 pounds per month
			total += 42;
			var live5_total = [42];
			$("#live5_hidden").attr('value', live5_total.join(''));
		}
		if(live6){
			// Savings: 10 pounds per week
			total += 10;
			var live6_total = [10];
			$("#live6_hidden").attr('value', live6_total.join(''));
		}
		if(live7){
			// Savings: Save 28 lbs weekly for total elimination of dryer; 14 lbs weekly for partial elimination
			if($("#live7_2").val() == "1"){
				total += 28;
				var live7_total = [28];
			}else if($("#live7_2").val() == "2"){
				total += 14;
				var live7_total = [14];
			}
			$("#live7_hidden").attr('value', live7_total.join(''));
		}
		if(live8){
			// Savings: 300 lbs per year
			total += 300;
			var live8_total = [300];
			$("#live8_hidden").attr('value', live8_total.join(''));
		}
		if(live9){
			// Savings: 550 lbs per year
			total += 550;
			var live9_total = [550];
			$("#live9_hidden").attr('value', live9_total.join(''));
		}
		if(live10){
			// Savings: 550 lbs per year
			total += 1000;
			var live10_total = [1000];
			$("#live10_hidden").attr('value', live10_total.join(''));
		}
		
		// MAIL
		if(mail1){
			 // Savings: 37 lbs per year
			total += 37;
			var mail1_total = [37];
			$("#mail1_hidden").attr('value', mail1_total.join(''));
		}
		if(mail2){
			// Savings: 15.4 pounds per year for 1 catalogue
			total += $("#mail2_2").val() * 15.2;
			var mail2_total = [$("#mail2_2").val() * 15.2];
			$("#mail2_hidden").attr('value', mail2_total.join(''));
		}
			

		// TRASH
		if(trash1){
			 // Savings: 288 lbs per year
			total += 288;
			var trash1_total = [288];
			$("#trash1_hidden").attr('value', trash1_total.join(''));
		}
		if(trash2){
			// Savings: 273 lbs per year, 23 lbs a month, 5.6 lbs weekly
			if($("#trash2_2").val() == "1"){
				total += 273;
				var trash2_total = [273];
			}else if($("#trash2_2").val() == "2"){
				total += 23;
				var trash2_total = [23];
			}else if($("#trash2_2").val() == "3"){
				total += 5.6;
				var trash2_total = [5.6];
			}
			$("#trash2_hidden").attr('value', trash2_total.join(''));
		} 
		if(trash3){
			// Savings: 184 lbs per year, 15 lbs monthly, 3.8 lbs weekly
			if($("#trash3_2").val() == "1"){
				total += 184;
				var trash3_total = [184];
			}else if($("#trash3_2").val() == "2"){
				total += 15;
				var trash3_total = [15];
			}else if($("#trash3_2").val() == "3"){
				total += 3.8;
				var trash3_total = [3.8];
			}
			$("#trash3_hidden").attr('value', trash3_total.join(''));
		}
		if(trash4){
			// Savings: 1.4 lbs per month; 17 lbs per year
			if($("#trash4_2").val() == "1"){
				total += 17;
				var trash4_total = [17];
			}else if($("#trash4_2").val() == "2"){
				total += 1.4;
				var trash4_total = [1.4];
			}
			$("#trash4_hidden").attr('value', trash4_total.join(''));
		}

		// WORK
		if(work1){
			// Savings: 184 lbs per year, 15 lbs monthly, 3.8 lbs weekly
			total += $("#work1_2").val() * 10;
			var work1_total = [$("#work1_2").val() * 10];
			$("#work1_hidden").attr('value', work1_total.join(''));
		}
		if(work2){
			// Savings: .55 lbs daily; 3.8 lbs weekly, 15 lbs monthly
			if($("#work2_2").val() == "1"){
				total += 15;
				var work2_total = [15];
			}else if($("#work2_2").val() == "2"){
				total += 3.8;
				var work2_total = [3.8];
			}
			$("#work2_hidden").attr('value', work2_total.join(''));
		}
		if(work3){
			// Savings: 2.7 lbs per day, 13.7 lbs for a 5-day workweek,  55 lbs per month
			if($("#work3_2").val() == "1"){
				total += 55;
				var work3_total = [55];
			}else if($("#work3_2").val() == "2"){
				total += 13.7;
				var work3_total = [13.7];
			}
			$("#work3_hidden").attr('value', work3_total.join(''));
		}
		
		// TOTALS
		var user_total = [total];
		$("#user_js").html(user_total.join(''));
        $("#total_hidden").attr('value', user_total.join(''));

	});
}

function checkName(){
	var anon  = $("#identity_anon").is(':checked');
	var blank = [""];
	var message = ["&nbsp;&nbsp;&nbsp;" + "Your promise has not been saved."];
	
	if(anon){
		var name = ["Anonymous"];
		$("#identity_name").attr('value', name.join(''));
		$("#identity_name").attr("disabled","disabled");
		$("#identity_name").removeClass("error");
		$("#error_message").html(message.join(''));
	}else{
		$("#identity_name").attr('value', blank.join(''));
		$("#identity_name").removeAttr("disabled");
		$("#error_message").html(message.join(''));
	}
}

function checkEmail(){
	var sendmail = $("#identity_sendmail").is(':checked');
	var blank = [""];
	var message = ["&nbsp;&nbsp;&nbsp;" + "Your promise has not been saved."];
	
	if(sendmail){
		$("#identity_email").attr('value', blank.join(''));
		$("#identity_email").removeAttr("disabled");
	}else{
		$("#identity_email").attr("disabled","disabled");
		$("#identity_email").removeClass("error");
		$("#error_message").html(message.join(''));
	}
}

function checkPhone(){
	var sendtxt = $("#identity_sendtxt").is(':checked');
	var blank = [""];
	var message = ["&nbsp;&nbsp;&nbsp;" + "Your promise has not been saved."];
	
	if(sendtxt){
		$("#identity_phone").attr('value', blank.join(''));
		$("#identity_phone").removeAttr("disabled");
	}else{
		$("#identity_phone").attr("disabled","disabled");
		$("#identity_phone").removeClass("error");
		$("#error_message").html(message.join(''));
	}
}

function lookup(inputString){
	//http://nodstrum.com/2007/09/19/autocompleter/
	if(inputString.length == 0) {
		// Hide the suggestion box.
		$('#suggestions').hide();
	} else {
		$.post("../assets/php/rpc.php", {queryString: ""+inputString+""}, function(data){
			if(data.length >0) {
				$('#suggestions').show();
				$('#autoSuggestionsList').html(data);
			}
		});
	}
} // lookup

function fill(thisValue) {
	$('#identity_country').val(thisValue);
	setTimeout("$('#suggestions').hide();", 200);
}