/// <reference path="jQuery Source/jquery-1.3.2-vsdoc.js" />
/// <reference path="W:\db.js" />

var AJAXDiagnostic = false;
var AJAXCounter = 0;
var cDataScript = "isql";
var strRewardUnit = "";

var bTopPicksLeft = true;
var cLastImg = "";
$(document).ready(function () {
	$("#input_code").keyup(function (event) {
		if (event.keyCode == 13) { $("#btnLogin").click(); }
	});

	$('#content').position(
			{ at: "center center",
				of: "body",
				my: "center center",
				offset: "0 -30"
			});
			
	$("#btnLogin").button().click(function () { CheckLoginCode(); });
	$("#btnBrowse").button().click(function () { $("#frmBrowse").submit(); });
	$("#btnHelp").button().click(function () { window.location = "help.html"; });
});

// href="help.html"


function NewAccountRadio(nOpt) {
	if (nOpt == 1) {
		$(".RewardCode_ExistingAccount").hide();
		$("#divNameEmail").slideDown(500, function() { $("#txtAccountName").focus(); });
		
	} else {
		$("#divNameEmail").slideUp(500);
		$(".RewardCode_ExistingAccount").show();
		 $("#txtAccountCode").focus(); 
	}	
	
}

function CheckLoginCode() {
	if ($("#diagGoodRewardCode").is(":visible")) return;
	var cCode = $("#input_code").val().toLowerCase();
	$(".InputtedCode").text($("#input_code").val());
	$("#diagBadCode p").hide();

	var AccountFetched = function(aRows) {
		$("#diagWait").dialog("destroy");
		if (!aRows || aRows.length == 0) {
			$("#diagBadCode p").hide();
			$("#diagBadCode .AccountCode").show();
			$("#diagBadCode").dialog({
				autoOpen: true,
				modal: true,
				buttons: { Continue: function() { $(this).dialog('close'); } },
				close: function(event, ui) { $(this).dialog('destroy'); $("#input_code").focus(); }
			});
			return;
		}
		if (aRows[0].active == "f") {
			$("#diagBadCode p").hide();
			$("#diagBadCode .InactiveAccount").show();
			$("#diagBadCode").dialog({
				autoOpen: true,
				modal: true,
				buttons: { Continue: function() { $(this).dialog('close'); } },
				close: function(event, ui) { $(this).dialog('destroy'); $("#input_code").focus(); }
			});
			return;
		}
		$("#diagWait span").hide();
		$("#diagWait .OpenShop").show();
		$("#diagWait").dialog({ autoOpen: true, modal: true });

		$("#login_code").val(cCode);
		$("#frmLogin").submit();
	}

	var RewardCodeFetched = function(aRows) {

		var GoodRewardCodeEntered = function(aRows) {

			var CustomerFetched = function(aResults) {

				var ExisitingAccountFromReward = function() {
					var cAccountCode = $("#txtAccountCode").val().toLowerCase();
					if (cAccountCode.length != 7) {
						$("#diagBadCode p").hide();
						$("#diagBadCode .ShortAccountCode").show();
						$("#diagBadCode").dialog({
							autoOpen: true,
							modal: true,
							buttons: { Continue: function() { $(this).dialog('close'); } },
							close: function(event, ui) { $(this).dialog('destroy'); $("#txtAccountCode").focus(); }
						});
						return;
					}

					$("#diagWait span").hide();
					$("#diagWait .AccountCodeCheck").show();
					$("#diagWait").dialog({ autoOpen: true, modal: true });
					var oSQL3 = new Run_CGI("sql", "SELECT * FROM shopaccounts WHERE code = " + quoteSQL(cAccountCode, "PG") + " and customer_id=" + quoteSQL(aRows[0].customer_id, "PG"),
						function(aRows2) {
							if (!aRows2 || aRows2.length == 0) {
								$("#diagWait").dialog("destroy");
								$("#diagBadCode p").hide();
								$("#diagBadCode .AccountCode .InputtedCode").text(cAccountCode);
								$("#diagBadCode .AccountCode").show();
								$("#diagBadCode").dialog({
									autoOpen: true,
									modal: true,
									buttons: { Continue: function() { $(this).dialog('close'); } },
									close: function(event, ui) { $(this).dialog('destroy'); $("#txtAccountCode").focus(); }
								});
								return;
							}
							var cNewBal = String(Number(aRows2[0].balance) + Number(aRows[0].value));
							// Mark card as used and up balance on shopaccount
							var cSQL =
								"UPDATE shopcards set used = " + quoteSQL(aRows[0].value) +
									" where code = " + quoteSQL(cCode, "PG") + " and customer_id = " + quoteSQL(aRows[0].customer_id, "PG") + ";" +
								"INSERT INTO shopaccountitems (code, item, amount) " +
									"VALUES (" + quoteSQL(cAccountCode, "PG") + ", " + quoteSQL("Card Deposit: " + cCode, "PG") + "," + quoteSQL(aRows[0].value) + ");" +
								"UPDATE shopaccounts set balance = " + quoteSQL(cNewBal, "PG") +
									" where code = " + quoteSQL(cAccountCode, "PG") + " and customer_id = " + quoteSQL(aRows[0].customer_id, "PG") + ";";

							var oSQL4 = new Run_CGI("sql", cSQL,
								function() {
									$(".spanAccountName").text(aRows2[0].student);
									$(".AccountBalance").text(cNewBal);
									$("#diagWait").dialog("destroy");
									$("#diagDepositMade").dialog({
										autoOpen: true, modal: true,
										close: function(event, ui) { $("#login_code").val(cAccountCode); $("#frmLogin").submit(); },
										buttons: { Close: function() {
											$("#diagWait span").hide();
											$("#diagWait .OpenShop").show();
											$("#diagWait").dialog({ autoOpen: true, modal: true });
											$(this).dialog('close');
										}
										}
									});
								});

						});
				};

				var NewAccountFromReward = function() {
					$("#diagWait span").hide();
					$("#diagWait .CreateAccount").show();
					$("#diagWait").dialog({ autoOpen: true, modal: true });
					var cName = $("#txtAccountName").val();
					var cSQL =
						"SELECT add_shopaccount3(" + quoteSQL(cName, "PG") + "," + quoteSQL(aRows[0].customer_id, "PG") +
							", null," + quoteSQL(aRows[0].value) + ",null) as account_code;" +
						"UPDATE shopcards set used = " + quoteSQL(aRows[0].value) +
							" where code = " + quoteSQL(cCode, "PG") + " and customer_id = " + quoteSQL(aRows[0].customer_id, "PG") + ";";
					var oSQL5 = new Run_CGI("sql", cSQL,
						function(aNewAccount) {
							var cSQL = "UPDATE shopaccounts set email = " + quoteSQL($("#txtAccountEmail").val(), "PG") +
									" where code = " + quoteSQL(aNewAccount[0].account_code, "PG") + " and customer_id = " + quoteSQL(aRows[0].customer_id, "PG") + ";";
							var oSQL6 = new Run_CGI("sql", cSQL, false);
							$(".spanAccountName").text($("#txtAccountName").val());
							$(".AccountBalance").text(aRows[0].value);
							$("#diagWait").dialog("destroy");
							$("#diagNewAccount").dialog({
								autoOpen: true, modal: true,
								close: function(event, ui) { $("#login_code").val(aNewAccount[0].account_code); $("#frmLogin").submit(); },
								buttons: { Close: function() {
									$("#diagWait span").hide();
									$("#diagWait .OpenShop").show();
									$("#diagWait").dialog({ autoOpen: true, modal: true });
									$(this).dialog('close');
								}
								}
							});
						});
				};


				strRewardUnit = aResults[0].REWARDUNIT;
				tokenrate = aResults[0].TOKENRATE;
				SetRewardUnitElements();

				$("#diagWait").dialog("destroy");
				$(".RewardCodeValue").text(aRows[0].value);
				$("#input_code").unbind("keyup");
				$("#diagGoodRewardCode .CustomerName").text(aResults[0].NAME);
				$("#diagGoodRewardCode input[type='text']").keyup(function(event) {
					event.stopPropagation();
					if (event.keyCode == 13) { $('.ui-dialog-buttonpane button:contains("Okay")').click(); }
				});
				$("#diagGoodRewardCode").dialog({
					width: 500,
					autoOpen: true,
					modal: true,
					close: function(event, ui) { $(this).dialog('destroy'); $("#input_code").focus(); },
					buttons: {
						"Cancel": function() { $(this).dialog('close'); },
						"Okay": function() {
							if ($("#radioAccount")[0].checked) ExisitingAccountFromReward();
							else NewAccountFromReward();
						}
					}

				});
				$("#txtAccountCode").focus();
			}

			var oSQL2 = new Run_CGI(cDataScript,
				"SELECT a.NAME, b.ENUMTEXT as RewardUnit, a.TOKENRATE " +
				"FROM CUSTOMERS a, ENUMERATIONS b " +
				"where a.id=" + quoteSQL(aRows[0].customer_id, "IB") + "and b.ENUMTYPE=70 and (a.TOKENUNIT=b.ENUMCODE)",
				function(aResults) { CustomerFetched(aResults); });
		}

		if (!aRows || aRows.length == 0) {
			$("#diagWait").dialog("destroy");
			$("#diagBadCode .RewardCode").show();
			$("#diagBadCode").dialog({
				autoOpen: true,
				modal: true,
				buttons: { Continue: function() { $(this).dialog('close'); } },
				close: function(event, ui) { $(this).dialog('destroy'); $("#input_code").focus(); }
			});
			return;
		} else {
			if (aRows[0].value == aRows[0].used) {
				$("#diagWait").dialog("destroy");
				$("#diagBadCode .RewardCodeUsed").show();
				$("#diagBadCode").dialog({
					autoOpen: true,
					modal: true,
					buttons: { Continue: function() { $(this).dialog('close'); } },
					close: function(event, ui) { $(this).dialog('destroy'); $("#input_code").focus(); }
				});
				return;
			} else {
				GoodRewardCodeEntered(aRows);
			}
		}
	}


	// Base Code
	var oSQL = false;
	$("#diagWait span").hide();
	switch (cCode.length) {
	case 6:
		$("#diagWait .RewardCodeCheck").show();
		$("#diagWait").dialog({ autoOpen: true, modal: true });
		oSQL = new Run_CGI("sql", "SELECT * FROM shopcards WHERE code = " + quoteSQL(cCode, "PG"), function(data) { RewardCodeFetched(data) });
		break;
	case 7:
		$("#diagWait .AccountCodeCheck").show();
		$("#diagWait").dialog({ autoOpen: true, modal: true });
		oSQL = new Run_CGI("sql", "SELECT * FROM shopaccounts WHERE code = " + quoteSQL(cCode, "PG"), function(data) { AccountFetched(data) });
		break;
	default:
		$("#diagBadCode p").hide();
		if (cCode.slice(0, 15) == "enter your code" || cCode.length == 0) $("#diagBadCode .NoCode").show();
		else if (cCode.length < 6) $("#diagBadCode .ShortCode").show();
		else if (cCode.length > 7) $("#diagBadCode .LongCode").show();

		$("#diagBadCode").dialog({
			autoOpen: true,
			modal: true,
			buttons: { Continue: function() { $(this).dialog('close'); } },
			close: function(event, ui) { $(this).dialog('destroy'); $("#input_code").focus(); }
		});
		return;
		
	}
}

