﻿/*
	RAPLab.Web.Opportunity.Controls.OppSearchCtl
	Dependencies:
*/
Util.ensureNamespacePath("RAPLab.Web.Opportunity.Controls.OppSearchCtl");
/*=======================================================================*/

	RAPLab.Web.Opportunity.Controls.OppSearchCtl.init = function(aUniqueIDPrefix,
				aTestMode, aProgramData, aLocationData) {
		//alert($("#" + aUniqueIDPrefix+"lstPrograms_mListCtl").length);

		RAPLab.Web.Opportunity.Controls.OppSearchCtl.mTestMode = aTestMode;
		
		$("#" + aUniqueIDPrefix+"lstPrograms").dropdownchecklist({
					width: 125, maxDropHeight: 300, firstItemChecksAll: true,
						mUncheckAllWhenAllIsChecked: true,
					mDisplayOverride: 'Choose Programs:',
					mOnPostInit: RAPLab.Web.Opportunity.Controls.OppSearchCtl.lst_OnPostInit,
					mOnPostSyncSel: RAPLab.Web.Opportunity.Controls.OppSearchCtl.lst_OnPostSyncSel,

					mUniqueIDPrefix: aUniqueIDPrefix,
					mCtlID: "lstPrograms",
					mDisplayList: aProgramData.mDisplayList,
					mDisabled: aProgramData.mDisabled
				});
		$("#" + aUniqueIDPrefix+"lstLocations").dropdownchecklist({
					width: 125, maxDropHeight: 300, firstItemChecksAll: true,
						mUncheckAllWhenAllIsChecked: true,
					mDisplayOverride: 'Choose Locations:',
					mOnPostInit: RAPLab.Web.Opportunity.Controls.OppSearchCtl.lst_OnPostInit,
					mOnPostSyncSel: RAPLab.Web.Opportunity.Controls.OppSearchCtl.lst_OnPostSyncSel,

					mUniqueIDPrefix: aUniqueIDPrefix,
					mCtlID: "lstLocations",
					mDisplayList: aLocationData
				});
		if (RAPLab.Web.Opportunity.Controls.OppSearchCtl.mTestMode != 1)
			$("#" + aUniqueIDPrefix+"lstCtznLevel").dropdownchecklist({
						width: 125, firstItemChecksAll: false,
						mDisplayOverride: 'Citizenship:',
						mOnPostInit: RAPLab.Web.Opportunity.Controls.OppSearchCtl.lstlstCtznLevel_OnPostInit,
						mOnPostSyncSel: RAPLab.Web.Opportunity.Controls.OppSearchCtl.lstCtznLevel_OnPostSyncSel,

						mUniqueIDPrefix: aUniqueIDPrefix,
						mCtlID: "lstCtznLevel",
						mDisplayList: null,
						mDebug: true
					});
		$("#" + aUniqueIDPrefix+"lstLevel").dropdownchecklist({
					width: 125, firstItemChecksAll: true,
					mDisplayOverride: 'Level:',
					mOnPostInit: RAPLab.Web.Opportunity.Controls.OppSearchCtl.lst_OnPostInit,
					mOnPostSyncSel: RAPLab.Web.Opportunity.Controls.OppSearchCtl.lst_OnPostSyncSel,

					mUniqueIDPrefix: aUniqueIDPrefix,
					mCtlID: "lstLevel",
					mDisplayList: null
				});
	}
	
/*-------------------------------------------------------------*/

	//context is dropdownchecklist
	RAPLab.Web.Opportunity.Controls.OppSearchCtl.lst_OnPostInit = function() {
		//on init, if first item is checked, use _syncSelected to check everything
		if (this.getAllSelected()) {
			var firstCheckbox = this.dropWrapper.find("input").filter(":first");
			this._syncSelected(firstCheckbox);
		}
		else {
			if (this.options.mDisabled)
				this.disable();
			RAPLab.Web.Opportunity.Controls.OppSearchCtl.lst_OnPostSyncSel.call(this, null);
		}
	}
	
/*-------------------------------------------------------------*/

	//context is dropdownchecklist
	RAPLab.Web.Opportunity.Controls.OppSearchCtl.lst_OnPostSyncSel = function(aSndrCheckbox) {
		var aText = "";
		
		if (RAPLab.Web.Opportunity.Controls.OppSearchCtl.mTestMode == 1) {
			aText = this._formatText("&#8226; ", "<br/>", true);
		}
		else {
			var aOptionEles = this.sourceSelect.find("option");
			if (this.getAllSelected()) {
				aText = aOptionEles.filter(":first").text();
			}
			else {
				var aThis = this;
				aOptionEles.each(function() {
					if ($(this).attr("selected")) {
						if (aText.length > 0)
							aText += "; ";
						if (aThis.options.mDisplayList)
							aText += aThis.options.mDisplayList[$(this).attr("value")];
						else
							aText += $(this).text();
					}
				});
			}
		}
		
		var aOutEle = $("#" + this.options.mUniqueIDPrefix+"out"+this.options.mCtlID)[0];
		aOutEle.innerHTML = aText;
	}
	
/*-------------------------------------------------------------*/

	//context is dropdownchecklist
	RAPLab.Web.Opportunity.Controls.OppSearchCtl.lstlstCtznLevel_OnPostInit = function() {
		RAPLab.Web.Opportunity.Controls.OppSearchCtl.lstCtznLevel_OnPostSyncSel.call(this, null);
	}

/*-------------------------------------------------------------*/

	//context is dropdownchecklist
	RAPLab.Web.Opportunity.Controls.OppSearchCtl.lstCtznLevel_OnPostSyncSel = function(aSndrCheckbox) {
		if (aSndrCheckbox) {
			var aValue = $(aSndrCheckbox).attr("value").substr(0, 2);

			var aDropWrapper = this.dropWrapper;
			var aOptionEles = this.sourceSelect.find("option[value^='"+aValue+"']");
			//alert(aValue + ", " + aOptionEles.length);
			
			//unselect all whose value starts w/ aSndrCheckbox value (e.g., C_, L_)
			for (var i = 0; i < aOptionEles.length; i++) {
				this.checkItem(aOptionEles[i], false, aSndrCheckbox[0]);
			}
			
			//select the first one if nothing is selected
			var aSelOptionEles = this.sourceSelect.find("option[value^='"+aValue+"'][selected]");
			if (aSelOptionEles.length == 0) {
				this.checkItem(aOptionEles[0], true, null);
			}
		} //if (aSndrCheckbox)
		
		var aText = "";
		
		aText = this._formatText("", ";", true);
		
		/*
		handled multiple label/sections in list
		aText = "<span style='font-weight: bold;'>Citizenship: </span>" +
				aText.split(";")[0] +
				"; " +
				"<span style='font-weight: bold;'>Level: </span>" +
				aText.split(";")[1];
		*/
		
		var aOutEle = $("#" + this.options.mUniqueIDPrefix+"out"+this.options.mCtlID)[0];
		aOutEle.innerHTML = aText;		
	}
	
/*-------------------------------------------------------------*/
/*
	If there were mulitple SearchCtl's on a page, this is a littel tricky:
	* it will validate all of them.
*/
	RAPLab.Web.Opportunity.Controls.OppSearchCtl.vdateLocations = function(source, arguments) {
		var aResult = true;
		var aLists = $("select[id$='lstLocations']").each(function() {
			var aSelCount = $(this).find("option[selected]").length;
			var aFirstItemSel = $(this).find("option:first").attr("selected");
			//alert(aFirstItemSel + "-" + $(this).find("option:first").find("[selected]").length);
			if (aSelCount > 10 && (!aFirstItemSel || aFirstItemSel != true)) {
				//alert(aSelCount);
				aResult = false;
			}
		});
		
		arguments.IsValid = aResult;
	}

/*=======================================================================*/
//RAPLab.Web.Opportunity.Controls.OppSearchCtl
/*=======================================================================*/

