$("document").ready( function (){


//AJAX SETUP
    $.ajaxSetup({ cache: false }); //initiate ajax
    $('#loading').ajaxStart(function (){ $(this).show();}).ajaxStop(function (){ $(this).hide();}); //loading graphic





//CALENDER
	$(function() {
		$("input[class='pick_date']").datepicker( { dateFormat : 'yy-mm-dd' } );
	});

//SUPERFISH MENU
	$("ul.sf-menu").supersubs({
		minWidth:    12,   // minimum width of sub-menus in em units
		maxWidth:    27,   // maximum width of sub-menus in em units
		extraWidth:  1     // extra width can ensure lines don't sometimes turn over
						   // due to slight rounding differences and font-family
	}).superfish();  // call supersubs first, then superfish, so that subs are
					 // not display:none when measuring. Call before initialising
					 // containing tabs for same reason.



//SELECT SUBMIT
	if ($("form#select").length > 0) {
		var selval = $("form#select select").val();
		if (selval == '') {
			$("#add").hide();
		} else {
			$("#add").show();
		}

		$("form#select select").change(function () {
			$("form#select").submit();
		});
	}


// MOSTLY ADMIN IU
	//$("#submit").hide();
	$("#dtable .input").hide();

	$("#dtable .display .button_update").click(
			function() {

				//var rowid = $(this).parent().parent().parent("tr").attr("id");
				//var rowid = $(this).parents("row").attr("id");
				var rowid = $(this).closest(".row").attr("id");
				$(".display .button").hide()
				$("#"+rowid+" .input").show();
				$("#"+rowid+" .display").hide();

				if($("#"+rowid+" input[name='action']").val('')) {
					$("#"+rowid+" input[name='action']").val("update");

				}

				$("#"+rowid).addClass("editing");


			});

	$("#dtable .display .button_add").click(
			function() {
				var rowid = $(this).closest(".row").attr("id");
				$(".display .button").hide()
				$("#"+rowid+" .display").hide();
				$("#"+rowid+" .input").show();



				if($("#"+rowid+" input[name='action']").val('')) {
					$("#"+rowid+" input[name='action']").val("add");

				}
				$("#"+rowid).addClass("editing");


			});

	$("#dtable .cancel .button_cancel").click(
			function() {
				var rowid = $(this).closest(".row").attr("id");
				$(".display .button").show()
				$("#"+rowid+" .input").hide();
				$("#"+rowid+" .display").show();
				if(!$("#"+rowid+" input[name='action']").val('')) {
					$("#"+rowid+" input[name='action']").val("");

				}
				$("#"+rowid).removeClass("editing");


			});


	$("#dtable .display .button_delete").click(
			function () {
				var rowid = $(this).closest(".row").attr("id");
			var formid = "form_"+rowid;
			if($("#"+rowid+" input[name='action']").val('')) {
				$("#"+rowid+" input[name='action']").val("delete");

			}



			var groupstype = "schools";
			var sellerstype = "students";

			if ($("#"+rowid+" input[name='page']").val() == 'salesreps.php') {
				var fname = $("#"+rowid+" input[name='fname']").val()
				var lname = $("#"+rowid+" input[name='lname']").val()
				var msg = "You are about to delete "+fname+" "+lname+".  They will also be removed from any currently assigned "+groupstype+"!  \n\rClick OK to delete. \n\rClick Cancel to stop delete process. ";
			} else
			if ($("#"+rowid+" input[name='page']").val() == 'groups.php') {
				var name = $("#"+rowid+" input[name='name']").val();
				var msg = "You are about to delete "+name+".  Click OK to delete.  Click Cancel to stop delete process. ";
			} else
			if ($("#"+rowid+" input[name='page']").val() == 'teams.php') {
				var name = $("#"+rowid+" input[name='name']").val();
				var msg = "You are about to delete "+name+".  They will also be removed from any currently assigned "+sellerstype+"! \n\rClick OK to delete. \n\rClick Cancel to stop delete process. ";
			} else
			if ($("#"+rowid+" input[name='page']").val() == 'sellers.php') {
				var fname = $("#"+rowid+" input[name='fname']").val()
				var lname = $("#"+rowid+" input[name='lname']").val()
				var msg = "You are about to delete "+fname+" "+lname+".  They will also be removed from any currently assigned teams or "+groupstype+"! \n\rClick OK to delete. \n\rClick Cancel to stop delete process. ";
			} else {
				var msg = "Error: Check message confirmation. ";
			}

			var agree = confirm(msg);
			if(agree) {
				$("#"+formid).submit();
			} else {
				return false;
				$("#"+rowid+" input[name='action']").val("");
			}
	});



//MOSTLY STUDENT UI

//SHOW TEAMS ? (SELLER SIGNUP)
/*
	$(".team").hide();

	var page = $('body').attr('id');

	switch (page) {
		case 'confirm':
			teams_exist('membership','suid');
			break;
		case 'signup':
			teams_exist('list','guid');
			break;
		default:
			break;
	}
*/




//INSTRUCTIONS on CONFIRM page.
	$(".instructions").hide();
	$("#instructions").click( function () {
		$(".instructions").toggle();
	});





//SHARE
$(function () {
	var url = $('#share_link').html();
	$('#share_block').bookmark(
		//{sites: ['facebook','twitter','myspace']}, //add or remove site links here
		{
		sites: ['facebook','twitter','myspace'], //add or remove site links here
		icons: 'template/images/bookmarks.png', //path from displayed html to image
		url  : url, //'www.google.com'
		title: "I'm having a fundraiser!"
		}
	);

	});




//SALES REPORTS


	$("#gen_report").click( function() {
		//var call 	= $("input[name='page']").val(); //reports.php
		//var action 	= $("input[name='action']").val(); //display
		var sdate 	= $("input[name='sdate']").val();
		var edate 	= $("input[name='edate']").val();
		var date_pattern = /^\d{4}-\d{2}-\d{2}$/;
		error = false;
		message = '';

		if(sdate == '')
		{
			error = true;
			message = "Please enter start date.";
		}
		else
		if(edate == '')
		{
			error = true;
			message = "Please enter end date.";
		}
		else
		if( !date_pattern.test(sdate) || !date_pattern.test(edate) )
		{
			error = true;
			message = "Please enter dates in YYYY-MM-DD format.  (eg: May 9th, 2010 would be 2010-05-09)";
		}
		else
		{
			error = false;
			$("#rinfo").empty(); //so errors don't stack up
			//$("#rinfo").append("<p class='failure'>"+message+"</p>");
			get_report(sdate, edate);
		}


		if (error)
		{
			//alert(message);
			$("#rinfo").empty(); //so errors don't stack up
			$("#rinfo").append("<p class='failure'>"+message+"</p>");
		}

	});






//FUNCTIONS



function teams_exist(call,id){ //send data to script through ajax call, return data to this page
//call: list || membership
		var file = "includes/functions.php";
		var id = $("input[name='"+id+"']").val()
		var target = 'ajax';

		$.post(
			file,
			{ //post_label: value
				action: 'check',
				page: target,
				id: id,
				call: call
			},
			function(data) {
				if (data.error) {
					$("#error_block").empty(); //so errors don't stack up
					$("#error_block").append("<p class='error'>"+data.error+"</p>");
				} else if (data.exist){
					if (data.exist == '1') {
						$(".team").show();
					} else {
					}
				} else {

				}
			},
			"json"
		);
}


function get_report(sdate,edate){ //send data to script through ajax call, return data to this page
//call: list || membership
		var file = "includes/functions.php";

		$.post(
			file,
			{ //post_label: value
				action: 'display',
				page: 'ajax',
				call: 'reports',
				sdate: sdate,
				edate: edate
			},
			function(data) {
				if (data.error) {
					$("#rinfo").empty(); //so errors don't stack up
					$("#rinfo").append("<p class='failure'>"+data.error+"</p>");
				} else if (data.exist){
					$("#rinfo").empty(); //so errors don't stack up
					$("#rinfo").append("<p class='success'>"+data.exist+"</p>");

					$("#return_holder").empty();
					$("#return_holder").append(data.content);


				} else {
					$("#return_holder").empty();

				}
			},
			"json"
		);
}




});









