/*
EGB Support
Copyright 2006-10, Don Kent, eShopHost Ltd
History
	1.0 19/09/06
	1.1 23/09/06 Live
	1.2 25/09/06 3. Added RideCancelled Yes/No
	1.3 26/09/06 7. added
	1.4 11/10/06 10 to xx added
	1.5 08/10/09 3. Apparent logic error for Past & Future rides. Also needs extra where clause in query
	2.0 06/07/10 New feature: Trophy Table navigation support for linkage to ASP.NET Trophy League Results, etc.
	2.1 13/07/10 CN92 16. Revised page name schema for Trophy Table for past results (dropping the p suffix for previous year's), etc.
	2.2 20/07/10 WBR121 3. Filter conditions for future rides was wrong - was selecting closing dates in the future
	2.3 02/12/10
		4. Dev. Next Year Schedule from dB - need to handle both scheduleX.asp & nextyearscheduleX.asp where X is d or a
		1. Dev. Add RideType: PQ (Perfeq)

Contents:
-	For the Schedule Index page - handling the preferences
	0. doSubmit () - handle changes to RideType, Location, Open Order
	1. RideTypeChange () - Filter by ...
	2. LocationChange () - Filter by ...
	3. OpenChange () - Filter by ...
	4. OrderChange () - Sort by ...
	5. SetPreferences () - onLoad of page to match the URL parameters
	6. ClkWhere ()

-	For the Results Index Page - handling the preferences
	10. resultssubmit (form)
	11. SortOrder (form)
	12. SortBy (form)
	13. FilterBy (form)
	14. SetResultsPrefs ()

-	For the Trophies page
	15. TrophyTableInit
	16. goTrophyResultPage

	A. getRadioValue (objR)
	B. setRadio (Name, objR)
	C. setCombo (Name, objC)
	D. getParameter (Name) - from URL (Get method)

Instructions:
	1. Requires eshophj1.js
*/

// Values for feeding into the SQL via hiddens:
	// Boolean (Yes/No) - match:
		var Any = ""; // SQL is then LIKE '%%'
		var Yes = "-1";
		var No = "0";

	// DateString values
		var Small = "10001231";
		var Large = "99991231";

function doSubmit () { // 0. onsubmit="doSubmit ()"
	// no script function required for filter by 'month' of the ride
	// SQL: Month >= From AND Month <= To
	// No need to set default values for the hiddens on the form
	// = handle changes to RideType, Location, Open Order
	RideTypeChange (); // 1.
	LocationChange (); // 2.
	OpenChange (); // 3.
	OrderChange (); // 4.
}


function RideTypeChange () { // 1. Filter by ...
	var F = getObj("RidesList"); // form
	var RT = F.RideType; // radio
	RT = getRadioValue (RT);
	// SQL: MajorRide matches MR, AND FEI matches FEI, AND Perfeq matches PQ
	// Re-set default hidden values (true if RT == "All")
	F.MR.value = Any;
	F.FEI.value = Any;
	F.PQ.value = Any;
	// Set for chosen RideType
	switch (RT) {
		case "FEI":
			F.FEI.value = Yes;
			break;
		case "Major":
			F.MR.value = Yes;
			break;
		case "Perfeq":
			F.PQ.value = Yes;
			break;
	}
}

function LocationChange () { // 2. Filter by ...
	var F = getObj("RidesList"); // form
	var RT = F.Where; // radio
	RT = getRadioValue (RT);
	// SQL Branch matches B
	if (RT == "All") { // default
		F.B.value =Any; // hidden
	}
	if (RT == "Group") {
		var G = F.Branch.value; // combo
		F.B.value = G;
	}
}

function OpenChange () { // 3. Filter by ...
	var F = getObj("RidesList"); // form
	var RT = F.Open; // radio
	RT = getRadioValue (RT);

	var ToDay = new Date();
	var M = ToDay.getMonth() + 1;
	M = M < 10 ? "0" + M : M.toString();
	var D = ToDay.getDate();
	D = D < 10 ? "0" + D : D.toString();
	ToDay = ToDay.getFullYear() + "" + M + "" + D;

	/* SQL - common to all schedule requests - sometimes clause 2 is inactivated (made false)
	(	(OpenDateString <= OD				Ride Opens for entries
			AND ClosingDateString >= CD		Ride Closes for entries
			AND RideFull matches RF
			AND RideCancelled matches RC)
		OR (DateString >= RD				Ride Date
			AND RideFull matches RF
			AND LateEntries matches LE)	) - assume LE is false if RC is false so no need to test
	*/

	// Not all radio buttons need to be present on the form
	// Hidden input fields on the form: OD, CD, RD, RF, RC, LE
	if (RT == "All") { // default
		// SQL (
		// (OpenDateString <= large AND <-- always true
		// ClosingDateString >= small AND <-- always true
		// RideFull matches * AND <-- always true
		// RideCancelled matches *) <-- always true ==> always true
		// OR (
		// ClosingDateString <= small AND
		// DateString >= small AND
		// RideFull matches * AND
		// LateEntries matches *) ==> N/A
		// )
		F.OD.value = Large;
		F.CD.value = Small;
		F.RD.value = Small;
		F.RF.value = Any;
		F.RC.value = Any;
		F.LE.value = Any;
	}
	if (RT == "Open") {
		// SQL (
		// (OpenDateString <= today AND
		// ClosingDateString >= today AND
		// RideFull matches false AND
		// RideCancelled matches false) [in the normal entry window]
		// OR (
		// ClosingDateString <= today AND
		// DateString >= today AND
		// RideFull matches false AND
		// LateEntries matches true) [in the Late Entry Window]
		// )
		F.OD.value = ToDay;
		F.CD.value = ToDay;
		F.RD.value = ToDay;
		F.RF.value = No;
		F.RC.value = No;
		F.LE.value = Yes;
	}
	if (RT == "Past") {
		// SQL (
		// (OpenDateString <= small AND <-- always false 
		// ClosingDateString >= today AND
		// RideFull matches * AND
		// RideCancelled matches *) <== clause false i.e. n/a
		// OR (
		// ClosingDateString <= today AND
		// DateString >= small AND	<-- always true
		// RideFull matches * AND	<-- always true
		// LateEntries matches *) <== clause active
		// )
		F.OD.value = Small;
		F.CD.value = ToDay;
		F.RD.value = Small;
		F.RF.value = Any;
		F.RC.value = Any;
		F.LE.value = Any;
	}
	if (RT == "Future") {
		// SQL (
		// (OpenDateString <= small AND <-- always false 
		// ClosingDateString >= large AND <-- always false
		// RideFull matches * AND <-- always true 
		// RideCancelled matches *) <== clause false i.e. n/a
		// OR (
		// ClosingDateString <= large AND <-- always true
		// DateString >= today AND <-- ride is today or in the future
		// RideFull matches * AND <-- always true
		// LateEntries matches *) <== clause active
		// )
		F.OD.value = Small;
		F.CD.value = Large;
		F.RD.value = ToDay;
		F.RF.value = Any;
		F.RC.value = Any;
		F.LE.value = Any;
	}
}

function OrderChange () { // 4. Sort by ...
	var F = getObj("RidesList"); // form
	var RT = F.Order; // radio
	RT = getRadioValue (RT);
	var action = location.pathname;
	action = action.match(/(\w+)\w\.asp/i); // e.g. ([nextyear]schedule)d.asp
	action = action[1];
	if (RT == "Date") { // default
		F.action = action + "d.asp";
	}
	if (RT == "Name") {
		F.action = action + "a.asp";
	}
}

function SetPreferences () { // 5.
	var F = getObj("RidesList"); // form
	// set radios & combos from the URL parameter values
	setRadio ("RideType", F.RideType);
	setRadio ("Where", F.Where);
	setRadio ("Open", F.Open);
	setRadio ("Order", F.Order);
	setCombo ("From", F.From);
	setCombo ("To", F.To);
	setCombo ("Branch", F.Branch);
	ClkWhere ();
}

function ClkWhere () { // 6.
	var F = getObj("RidesList"); // form
	var CG = getObj("ChooseGroup");
	if (getRadioValue (F.Where) == "Group") {
		CG.style.display = "inline";
	} else {
		CG.style.display = "none";
	}
}


function resultssubmit(F) { // 10. onsubmit="resultssubmit (this)"
	F.Year.value = FilterBy(F);
	F.Sort.value = SortBy(F) + SortOrder(F);
	return true;
}


function SortOrder(F) { // 11. 
	var SO = F.SO; // radio
	return getRadioValue (SO);
}


function SortBy(F) { // 12. 
	var BY = F.BY; // radio
	return getRadioValue (BY);
}


function FilterBy(F) { // 13. 
	var YR = F.YR; // radio
	YR = getRadioValue (YR);
	if (YR == "single") {
		YR = F.SINGLE.value;
	}
	return YR;
}


function SetResultsPrefs () { // 14. <body onload="SetResultsPrefs()">
	var F = getObj("Results"); // form
	// set radios & combo from the URL parameter values
	var Y = getParameter("Year");
	var single;
	if (isNaN(parseInt(Y))) { // e.g. 'all'
		var TD = new Date();
		single = TD.getFullYear();
		single = (Y == "old") ? single - 1 : single;
	} else { // e.g. 2006
		single = Y;
		Y = "single";
	}
	putCombo (single, F.SINGLE);
	putRadio (Y, F.YR);

	var S = getParameter("Sort");
	putRadio (S.substr(0, 4), F.BY);
	putRadio (S.substr(4), F.SO);
}


function TrophyTableInit () { // 15. <body onload="TrophyTableInit ()">
	// Initialise Trophy Table
	// 1. Check Navigation table (form) is present
	if (!getObj("TrophyTable")) {
		return;
	}

	// *** CONFIGURE ***
	var TrophyListQuantity = 8;

	// 2. Attach handlers to View buttons
	var ViewButton;
	for (var i = 1; i <= TrophyListQuantity ; i++) {
		ViewButton = getObj("TrophyView" + i);
		if (ViewButton ) {
			ViewButton.onclick = goTrophyResultPage;
		}
	}
}


function goTrophyResultPage (evt) { //	16.
	// Handle navigation to Trophy Result Page after click of TrophyView button
	// 1. Capture Event object
	evt = evt ? evt : (event ? event : null);
	if (!evt) {
		return;
	}

	// 2. Determine the orginating element
	var elem = evt.target ? evt.target : (evt.srcElement ? evt.srcElement : null);
	if (!elem) {
		return;
	}

	// 3. Handle button click event
	// 3.0 Get Trophy Number
	var num = elem.id;
	num = num.substr (num.length - 1);

	// 3.1 Get Trophy Year
	var TrophyYear = getObj ("TrophyYear" + num);
	if (!TrophyYear) {
		return;
	}
	var TrophyYearIndex = TrophyYear.selectedIndex;
	TrophyYear = TrophyYear.options [TrophyYearIndex ].value;

	// 3.1 Navigate to Trophy Results Page
	// *** CONFIGURE *** to match actual page names, etc.
	if (num < 5) {
		// Trophy Leagues go dynamic or fixed pages
		var Leagues = ["trophysenior", "trophyyoung", "trophyjunior", "trophynovice"];
		if ((num == 2 || num == 3) && TrophyYearIndex > 0) {
			// Young (2) & Junior (3)
			// Temporary diversion for previous years (as SQL results not currently accurate)
			// old schema
//			location.href = ((TrophyYearIndex == 1) ? "" : "p") + Leagues [num -2];
			// new schema
			location.href = Leagues[num - 1] + TrophyYear + ".html";
		} else {
			// SQL results OK for all 4 leagues for current year (1 .. 4)
			// & for previous years for Senior & Novice (1 & 4) ...
			// But published results < 2008 deviate for all 4 leagues (1 .. 4)
			if (TrophyYear < 2008) {
				// Go to fixed pages for deviated results - all 4 leagues
				// new schema
				location.href = Leagues[num - 1] + TrophyYear + ".html";
			} else {
				// Go to dynamic pages for current year (all 4 leagues)
				// & 2008 on for Senior (1) & Novice (4)
				location.href = "TrophyLeagues.aspx?LeagueYear=" + TrophyYear + "&LeagueTableId=" + num;
			}
		}
	} else {
		// Sponsored Trophies (5-7) & Others (8) go to fixed pages
		//					5				6				7			8
		var Sponsored = ["trophyror", "trophyperfeq", "trophykbis", "trophyresults"];
		// old schema
//		location.href = ((TrophyYearIndex == 0) ? "" : "p") + Sponsored[num - 5];
		// new schema
		location.href = Sponsored[num - 5] + TrophyYear + ".html";
	}
}


function getRadioValue (objR) { // A.
	for (var i = 0; i < objR.length; i++) {
		if (objR[i].checked) {
			return objR[i].value;
		}
	}
}


function setRadio (Name, objR) { // Bi.
	var P = getParameter(Name);
	return putRadio (P, objR);
}


function putRadio (Val, objR) { // Bii.
	var found = false;
	for (var i = 0; i < objR.length; i++) {
		if (objR[i]) {
			if (objR[i].value == Val) {
				objR[i].checked = true;
				found = true;
			} else {
				objR[i].checked = false;
			}
		}
	}
	return found;
}


function setCombo (Name, objC) { // Ci.
	var P = getParameter(Name);
	return putCombo (P, objC);
}


function putCombo (Val, objC) { // Cii.
	var found = false;
	var Opt = objC.options;
	var i;
	for (i = 0; i < Opt.length; i++) {
		if (Opt[i].value == Val) {
			found = true;
			break;
		}
	}
	objC.selectedIndex = i;
	return found;
}


function getParameter (Name) { // D.
	var S = decodeURIComponent(location.search); // e.g. ?RD=&CD=23&OD=B=South+West
	S = S.replace(/\+/g, " ");
	var Pe, P = 0; // start at start
	var Timeout = 0;
	do {
		P = S.indexOf(Name + "=", P); // match parametername=
		Timeout++;
	} while (P > -1 && Timeout < 100
		&& !((S.charAt(P-1) == "&") || (S.charAt(P-1) == "?"))); // continue if part word
	Pe = S.indexOf ("&", P); // end point
	return S.substring(P + Name.length + 1, (Pe > -1)? Pe : S.length); // value extracted
}

