 // this is javascript
var lb_search_parameters = false; 
var gv_oldcity = "";


function f_popup(Class)
{
if (! window.focus)return true;  
		var lv_url = "licence_sub_cat_type_popup.jsp" + Class; 
 
			popupwindow = window.open(lv_url,"Lookup","width=400,height=680,directories=no,toolbar=no, resizable=yes,menubar=no,scrollbars=yes,dependent, titlebar='Legend',alwaysRaised=yes,left=245,top=15");

return false;
}

function f_SetOutput(a_selectedIndex, a_formName){
	// alert("The selected index is: " + a_selectedIndex);
	// alert ("The affected form is: " + a_formName );
		if (a_formName == 'mOutput') {
			document.mOutputbottom.output.options[a_selectedIndex].selected = true; 
		}else{
			document.mOutput.output.options[a_selectedIndex].selected = true; 
		}
	}
	

 

function f_toggleSubClass(lv_subclass){
   // alert(lv_subclass.charAt(5) );
	var lv_rows = dw_licence_sub_cat_type.RowCount();
	var lv_count = 0;
	var lv_eval = "";
	var lv_category_type = "";
    var lv_docstring
	lv_docstring = "document.mLicenceClass." + lv_subclass + ".checked"

	if (eval(lv_docstring) ){
	  // alert(lv_subclass + " Checked" );
		 for (var i = 1; i <= lv_rows; i++) {
			lv_count = i-1;
			lv_category_type = dw_licence_sub_cat_type.GetItem(i, 'category_type' );
		// alert(lv_category_type.charAt(6)); 
			if ( lv_category_type.charAt(6) == lv_subclass.charAt(5)  ){
				dw_licence_sub_cat_type.SetItem(i, 'selection', 'yes');
				lv_eval = "document.dw_licence_sub_cat_type_dataForm.selection_" + lv_count + ".checked=true";
			}
			eval(lv_eval);
		}
	 
	}else{
		              //  alert(lv_subclass + " UnChecked" );
		for (var i = 1; i <= lv_rows; i++) {
			lv_count = i-1;
			lv_category_type = dw_licence_sub_cat_type.GetItem(i, 'category_type' );
			if ( lv_category_type.charAt(6) == lv_subclass.charAt(5)  ){
				dw_licence_sub_cat_type.SetItem(i, 'selection', 'no');
				lv_eval = "document.dw_licence_sub_cat_type_dataForm.selection_" + lv_count + ".checked=false";
			}
			eval(lv_eval);
		}
	}
	
	f_deselectBYOB_VLT();
	dw_licence_sub_cat_type.AcceptText();
}

 
function f_deselectAllSubClasses() {
	
	var lv_rows = dw_licence_sub_cat_type.RowCount();
	var lv_count = 0;
	var lv_eval = "";
	var lv_docstring = "";
	
	for (var i = 1; i <= lv_rows; i++) {
			lv_count = i-1;
			dw_licence_sub_cat_type.SetItem(i, 'selection', 'no');
			lv_eval = "document.dw_licence_sub_cat_type_dataForm.selection_" + lv_count + ".checked=false";
			eval(lv_eval);
		}
		document.mSearch.sdall.value="none";
		document.mLicenceClass.classA.checked=false;
		document.mLicenceClass.classB.checked=false;
		document.mLicenceClass.classC.checked=false;
		document.mLicenceClass.classD.checked=false;
		document.mLicenceClass.classE.checked=false;

		dw_licence_sub_cat_type.AcceptText();
}

function f_SelectDeselectAllLicenceTypes() {
	
	var lv_rows = dw_licence_sub_cat_type.RowCount();
	var lv_count = 0;
	var lv_eval = "";
	
	if (document.mSearch.sdall.value=="all"){
	//alert("document.mSearch.sdall.value=" + document.mSearch.sdall.value )
		document.mSearch.sdall.value="none";
		f_ClearLicenceClassForm();
 
	}else{
	//alert("document.mSearch.sdall.value=" + document.mSearch.sdall.value )
		document.mSearch.sdall.value="all";
		document.mLicenceClass.classA.checked=true;;
		document.mLicenceClass.classB.checked=true;
		document.mLicenceClass.classC.checked=true;
		document.mLicenceClass.classD.checked=true;
		document.mLicenceClass.classE.checked=true;

	}
		
	for (var i = 1; i <= lv_rows; i++) {
		lv_count = i-1;
		if (document.mSearch.sdall.value=="all") {
			dw_licence_sub_cat_type.SetItem(i, 'selection', 'yes');
			lv_eval = "document.dw_licence_sub_cat_type_dataForm.selection_" + lv_count + ".checked=true";
		}else{
			dw_licence_sub_cat_type.SetItem(i, 'selection', 'no');
			lv_eval = "document.dw_licence_sub_cat_type_dataForm.selection_" + lv_count + ".checked=false";
		}
		eval(lv_eval);
	}
	
	f_deselectBYOB_VLT();
	dw_licence_sub_cat_type.AcceptText();
}

// NOTE:  dw_licensee_location_ItemChanged compares the name in the dropdown (i.e. 'Edmonton') to the 
// DIV tag ID (i.e. ID="Edmonton").  Be sure the spellings are identical. 
// Javascript is CaSe SensitivE! ( but only in Firefox...)	   // 
function dw_licensee_location_ItemChanged(row, columnName, newValue, oldValue, kFlag) {
//alert("Item Changed" + row + " " + columnName+ " " + newValue+ " " + oldValue+ " " + kFlag )
	var lv_map_form = document.getElementById('MAP_FORM');
	var lv_tab_map_filler = document.getElementById('tab_map_filler'); 
    var lv_oldCity = document.getElementById(dw_licensee_location.GetItem(row, columnName));
	var lv_newCity = document.getElementById(newValue);
	
	gv_oldcity = newValue;
	// Hide the old DIV tag. 
    if (undefined != lv_oldCity ) {
    	//lv_oldCity.style.visibility="hidden";
		lv_oldCity.style.display = "none";
		lv_tab_map_filler.style.display = "block";
					//lv_oldCity.style.top="500 px"; 
		  			//lv_oldCity.style.left="0px"; 
		  			//lv_oldCity.style.position="absolute"; 
		 // lv_map_form.style.visibility="hidden";
		 lv_map_form.style.display = "none";
		 
    }else{
		if (oldValue != null && oldValue != "") {
		    lv_oldCity = document.getElementById(oldValue);
		    if (lv_oldCity != null) {
        	   // lv_oldCity.style.visibility="hidden";
			   lv_oldCity.style.display = "none";
			   lv_tab_map_filler.style.display = "block";
				 		//lv_oldCity.style.top="500 px"; 
		  			//lv_oldCity.style.left="0px"; 
		  			//lv_oldCity.style.position="absolute"; 
				//lv_map_form.style.visibility="hidden";
				lv_map_form.style.display = "none";
			}
		}
	}
	// If the DIV tag exists, make it visible. 
    if (lv_newCity != null) {
         lv_map_form.style.display = "block";
		 lv_map_form.style.position="relative"; 
		 lv_tab_map_filler.style.display = "none";
		 //lv_newCity.style.top="20 px"; 
		 // lv_newCity.style.left="30 px"; 
		  //lv_newCity.style.position="relative"; 
		  lv_newCity.style.display = "block";
		 // lv_newCity.style.visibility="visible";
		 		  //lv_map_form.style.top="20 px"; 
		 //lv_map_form.style.left="330 px"; 
		 // lv_map_form.style.position="relative"; 
		

    }
	
	document.mSearch.location.value = newValue;
	// When changing location, we must blank out the PostalCode values...
	document.mSearch.postal_code.value = "";
	document.mPostCode.postal_code.value = "";
	document.mPostCode.postal_code_disp.rows = 1;
	document.mPostCode.postal_code_disp.value = "";	
			
}



function Assemble_PostCode(PCode) {

	var lv_postal_code = "";
	var lv_ind = 0;
	var lv_ps_length = 3;	//length of Postal Code: 3 char
	var lv_ps_spr = 3; //length of Postal Code Separator: 1 char
	var lv_subs = /000/g;
	//var lv_maxrows = 15; // maximum number of Postal Code FSA rows to display
	//var lv_rowstocreate = 1; // Number of rows to create
	//var lv_coderows = 1; // hmm
	
	lv_postal_code = document.mPostCode.postal_code.value;
	
	lv_ind = lv_postal_code.indexOf(PCode);
	
	if (lv_ind >= 0) {
		if (lv_ind == 0) {
			lv_postal_code = lv_postal_code.substring(lv_ps_length + lv_ps_spr);
		}else{
			lv_postal_code = lv_postal_code.substring(0, lv_ind - lv_ps_spr) + lv_postal_code.substring(lv_ind + lv_ps_length);
		}
	}else{
		if ( lv_postal_code.length == null || lv_postal_code.length == 0) {
			lv_postal_code = PCode;
		}else{
			lv_postal_code = lv_postal_code + "000" + PCode;
		}
	}

	document.mSearch.postal_code.value = lv_postal_code;
	document.mPostCode.postal_code.value = lv_postal_code;

	document.mPostCode.postal_code_disp.value = lv_postal_code.replace(lv_subs, " ");
	f_SetPostalCodeDisplayRows(lv_postal_code);
	return false;
}

function f_SetPostalCodeDisplayRows(a_postal_code){
	var lv_maxrows = 15; // maximum number of Postal Code FSA rows to display
	var lv_rowstocreate = 1; // Number of rows to create
	var lv_coderows = 1; // hmm
	var lv_postal_code = "";
	
	if (a_postal_code == null || a_postal_code == "") {
		a_postal_code = 0;
	}
	
	lv_postal_code = a_postal_code;
	
	if (lv_postal_code == 0){
		lv_rowstocreate = 1;
	}else{
		lv_coderows =  (lv_postal_code.length/6) + 1;
		if (lv_coderows < lv_maxrows ){
			lv_rowstocreate = lv_coderows;
		}else{
			lv_rowstocreate = lv_maxrows;
		}
	}

	document.mPostCode.postal_code_disp.rows = lv_rowstocreate;
	f_scrollToBottom (document.mPostCode.postal_code_disp);
}

function f_scrollToBottom (element) {
	// force the last added postal code FSA to appear 
	// at the bottom of the textarea by scrolling down
	// ...this only works for IE4+ 
  if (document.all){
		element.scrollTop = element.scrollHeight;
	  }
}


function mClear() {
// This function is called by the Clear button. 
	// alert("mClear has been clicked - gv_oldcity is: " + gv_oldcity);
	 f_ClearSearchForm(); // clears mSearch
	 f_ClearPostalCodeForm();	// clears mPostCode 
	 f_deselectAllSubClasses(); // Clears mLicenceClass
	 f_ClearLicenseeName();
}

function f_ClearLicenceClassForm(){
    // form mLicenceClass is on licence_class.jsp
	document.mLicenceClass.classA.checked=false;
	document.mLicenceClass.classB.checked=false;
	document.mLicenceClass.classC.checked=false;
	document.mLicenceClass.classD.checked=false;
	document.mLicenceClass.classE.checked=false;
}

function f_ClearLicenseeName(){
	// form mLicenseeName is on licensee_name.jsp
	document.mLicenseeName.customer_name.value = "";
}

function f_ClearPostalCodeForm(){
    // form mPostCode is on map_form.jsp
	document.mPostCode.postal_code_disp.rows = "1";
	document.mPostCode.postal_code_disp.value = "";
	document.mPostCode.postal_code.value = "";
	// form mSearch is on licence_class.jsp
	document.mSearch.postal_code.value = "";
}

function f_deselectBYOB_VLT(){
	document.mSearch.byob.checked=false;
	document.mSearch.byob.value = "";
	document.mSearch.vlt.checked=false;
	document.mSearch.vlt.value = "";
}

function f_ClearSearchForm(){
	// form mSearch is on licence_class.jsp
	var lv_map_form = document.getElementById('MAP_FORM'); 
	f_deselectBYOB_VLT();	// clears the BYOB and VLT checkboxes on form mSearch
 	dw_licensee_location.SetItem(1, 1, "ALL OF ALBERTA");
	//dw_licensee_location.AcceptText();
	
	dw_licensee_location_ItemChanged(1, "location", "ALL OF ALBERTA", gv_oldcity);
	document.mSearch.location.value = "";
	document.mSearch.postal_code.value = "";
	document.mSearch.sub_class.value = "";
	document.mSearch.customer_name.value = "";
	document.mSearch.sdall.value="none";
}

function mSubmit() {
// This function is called by the SEARCH button. 
	mPopulate();
}

//*****************************************************************************
//  function mPopulate()
// 	THIS function gathers all the arguments needed for the stored procedure
// 	and puts them into form mSearch, which is located on licence_class.jsp	
// 	If all is well, it also does the submit.
//
//		Modifications. 
//      For Liquor 
//*****************************************************************************
function mPopulate() {

	var lv_customer_name = ""; 
	var lv_location = "";
	var lv_sub_class = "";
	var lv_description_code = "";
	var lv_authorization_subclass_id = "";
	var lv_selection = "";
	var lv_sub_class_description_code = "";
		 
// Get Location  (TAB 1)
		lv_location = dw_licensee_location.GetItem(dw_licensee_location.GetRow(), "location");

	// If the dw row is empty (i.e. after user has pressed the "BACK" button), 
    // we populate the lv_location variable with the value currently cached in the search form  
		if ( lv_location == -1 || lv_location == null || lv_location == "") {
			lv_location = document.mSearch.location.value;
		}
		
	// The stored procedure searches for ALL OF ALBERTA with an
	// empty string as the location argument
		if( lv_location == "ALL OF ALBERTA"){
			lv_location = ""; // 
		}		
		document.mSearch.location.value = lv_location;
		
// Get Customer Name (TAB 2)
	lv_customer_name = document.mLicenseeName.customer_name.value;
	document.mSearch.customer_name.value = lv_customer_name;

//Get BYOB (TAB 3)
	if (document.mSearch.byob.checked) {
		document.mSearch.byob.value = "yes";
	}else{
		document.mSearch.byob.value = "no";
	}

//Get VLT (TAB 3 - currently disabled)
	if (document.mSearch.vlt.checked) {
		document.mSearch.vlt.value = "yes";
	}else{
		document.mSearch.vlt.value = "no";  
	}
	
//Get Licence Sub-Classes (TAB 4))
	lv_rows = dw_licence_sub_cat_type.RowCount();
	for (var i = 1; i <= lv_rows; i++) {
		lv_selection = dw_licence_sub_cat_type.GetItem(i, 'selection');
		var lv_bool;
		var lv_exec = "document.dw_licence_sub_cat_type_dataForm.selection_" + (i-1) + ".checked";
//		alert("Row: " + i + "; mString: " + lv_exec + "; Check Box: " + eval(lv_exec));

		lv_bool = eval(lv_exec);
		if (lv_bool) {
			// The Codes are now used for searching, not the IDs. 
			lv_description_code = dw_licence_sub_cat_type.GetItem(i, 'description_code');
			lv_authorization_subclass_id = dw_licence_sub_cat_type.GetItem(i, 'authorization_subclass_id');
			if (lv_sub_class.length == 0) {
				// The variable was empty: this is the first item in the list. 
				lv_sub_class_description_code = lv_description_code;
				//lv_sub_class = lv_authorization_subclass_id;
				lv_sub_class = lv_sub_class_description_code;
			}else{
				// Altered divider from 'ZZZ' to '0'
				lv_sub_class_description_code = lv_sub_class_description_code + "0" + lv_description_code;
				//lv_sub_class = lv_sub_class + "Z" + lv_authorization_subclass_id;
				lv_sub_class  = lv_sub_class_description_code;
			}
		}
		
	}
	
	// If, after all this, the lv_sub_class variable is still EMPTY, it could be that the user 
	// pressed the back button.  In that case, grab whatever value was in the form previously (if any). 
//	if (lv_sub_class == ""){
//		lv_sub_class = document.mSearch.sub_class.value;
//	}
	 //alert(lv_sub_class_description_code);
	document.mSearch.sub_class.value = lv_sub_class;
	document.mSearch.sub_class_description_code.value = lv_sub_class_description_code;

	//alert("Form Location: " + document.mSearch.location.value);
	//alert("Var Location: " + lv_location);
	//alert("Sub-Class: " + lv_sub_class);
	// alert("Postal Code: " + document.mSearch.postal_code.value);
	// alert("BYOB: " + document.mSearch.byob.value);
	// alert("VLT: " + document.mSearch.vlt.value);
	// alert("Format: " + document.mSearch.output.value);
	 // alert("Sub_class_description: " + document.mSearch.lv_sub_class_description_code.value);
	
// Set the action page according to the type of output selected	
	switch (document.mOutput.output.value){
		
		case "html":
			document.mSearch.action = "liquorlicenseelist_html.jsp";
			break;
		
		case "pdf":
			document.mSearch.action = "liquorlicenseelist_pdf.jsp";
			break;
		
		case "excel":
			document.mSearch.action = "liquorlicenseelist_excel.jsp";
			break;
		
		default:
			document.mSearch.action = "liquorlicenseelist_html.jsp";
	}
	
	//Save search param in the Cookies
	if (document.mSearch.location.value == null || document.mSearch.location.value == "" || document.mSearch.location.value == "null"){
		SetCookie("location", "");
	}else{
		SetCookie("location", document.mSearch.location.value);
	}

	if (document.mSearch.customer_name.value == null || document.mSearch.customer_name.value == "" || document.mSearch.customer_name.value == "null"){
		SetCookie("customer_name", "");
	}else{
		SetCookie("customer_name", document.mSearch.customer_name.value);
	}

	if (document.mSearch.sub_class.value == null || document.mSearch.sub_class.value == "" || document.mSearch.sub_class.value == "null"){
		SetCookie("sub_class", "");
	}else{
		SetCookie("sub_class", document.mSearch.sub_class.value);
	}

	if (document.mSearch.postal_code.value == null || document.mSearch.postal_code.value == "" || document.mSearch.postal_code.value == "null"){
		SetCookie("postal_code", "");
	}else{
		SetCookie("postal_code", document.mSearch.postal_code.value);
	}

	if (document.mSearch.byob.value == null || document.mSearch.byob.value == "" || document.mSearch.byob.value == "null"){
		SetCookie("byob", "");
	}else{
		SetCookie("byob", document.mSearch.byob.value);
	}

	if (document.mSearch.vlt.value == null || document.mSearch.vlt.value == "" || document.mSearch.vlt.value == "null"){
		SetCookie("vlt", "");
	}else{
		SetCookie("vlt", document.mSearch.vlt.value);
	}
	//End
		
	document.mSearch.submit();
	return true;
}

function mLoad() {

	var lv_ie = "msie";
	var lv_ns = "netscape";
	var lv_new_search = "";
	var lv_postal_code = "";
	var lv_subs = /000/g;

	var lv_client_browser = navigator.userAgent.toLowerCase();
	var lv_ind = lv_client_browser.indexOf(lv_ie);
	
	if (GetCookie("location") == "xxx") {
		lv_new_search = "yes";
	}else{
		lv_new_search = "no"
	}

	if (lv_new_search == "yes") {
		document.mPostCode.postal_code.value = "";
		document.mPostCode.postal_code_disp.value = "";
		document.mSearch.location.value = "";
		document.mSearch.postal_code.value = "";
		document.mSearch.byob.value = "";
		document.mSearch.vlt.value = "";
		document.mSearch.sub_class.value = "";

		dw_licensee_location.SetItem(1, 1, "ALL OF ALBERTA");

		document.mSearch.sdall.value="none";
		f_ClearLicenceClassForm();

		var lv_rows = dw_licence_sub_cat_type.RowCount();
		var lv_count = 0;
		for (var i = 1; i <= lv_rows; i++) {
			lv_count = i-1;
			dw_licence_sub_cat_type.SetItem(i, 'selection', 'no');
			lv_eval = "document.dw_licence_sub_cat_type_dataForm.selection_" + lv_count + ".checked=false";
			eval(lv_eval);
		}
	}else{
		f_SetPostalCodeDisplayRows(GetCookie("postal_code"));
		var lv_location = GetCookie("location");
		gv_oldcity = GetCookie("location");
		
		if (lv_location == "" || lv_location == null || lv_location == "null") {
			dw_licensee_location.SetItem(1, 1, "ALL OF ALBERTA");
			document.mSearch.location.value = "";
		}else{
			dw_licensee_location.SetItem(1, "location", lv_location);
			var lv_newCity = document.getElementById(lv_location);
			var lv_map_form = document.getElementById('MAP_FORM');
			if (lv_newCity != null) {
				lv_newCity.style.visibility="visible";
				lv_map_form.style.visibility="visible";
			}
			document.mSearch.location.value = GetCookie("location");
		}
		
		document.mSearch.postal_code.value = GetCookie("postal_code");
		document.mSearch.customer_name.value = GetCookie("customer_name");
		document.mSearch.sub_class.value = GetCookie("sub_class");
		document.mSearch.byob.value = GetCookie("byob");
		document.mSearch.vlt.value = GetCookie("vlt");
		
		document.mPostCode.postal_code.value = GetCookie("postal_code");

		lv_postal_code = GetCookie("postal_code");
		if (lv_postal_code == null || lv_postal_code == "") {
			document.mSearch.postal_code.value = "";
			document.mPostCode.postal_code.value = "";
			document.mPostCode.postal_code_disp.value = "";
		}else{
			document.mPostCode.postal_code_disp.value = lv_postal_code.replace(lv_subs, " ");
		}

	}


/*
	if (lv_ind > 0 ) {
		//This is MS IE
		//alert("mLoad is firing now");
		f_SetPostalCodeDisplayRows(document.mSearch.postal_code.value);
		document.mPostCode.postal_code_disp.cols = "5"
		var lv_location = document.mSearch.location.value;
		gv_oldcity = document.mSearch.location.value;
		
		if (lv_location == "") {
			//alert("Location = ''");
		}else{
			dw_licensee_location.SetItem(1, "location", lv_location);
			var lv_newCity = document.getElementById(lv_location);
			var lv_map_form = document.getElementById('MAP_FORM');
			if (lv_newCity != null) {
				lv_newCity.style.visibility="visible";
				lv_map_form.style.visibility="visible";
			}
		}
	}else{
		//This is Netscape -- it doesn't remember what was on the search form.  
		document.mPostCode.postal_code_disp.cols = "4"
		document.mSearch.location.value = "";
		dw_licensee_location.SetItem(1, 1, "ALL OF ALBERTA");
		document.mSearch.postal_code.value = "";
		document.mSearch.byob.value = "";
		document.mSearch.sub_class.value = "";
		
		document.mSearch.sdall.value="none";
		f_ClearLicenceClassForm();

		var lv_rows = dw_licence_sub_cat_type.RowCount();
		var lv_count = 0;
		for (var i = 1; i <= lv_rows; i++) {
			lv_count = i-1;
			dw_licence_sub_cat_type.SetItem(i, 'selection', 'no');
			lv_eval = "document.dw_licence_sub_cat_type_dataForm.selection_" + lv_count + ".checked=false";
			eval(lv_eval);
		}
		
		dw_licensee_location.SetItem(1, "location", ""); 
	}
	// document.sub_location.enter_location.focus; 
	// document.sub_location.enter_location.select(); 

*/	

	if (lv_ind > 0 ) {
		//This is MS IE
		document.mPostCode.postal_code_disp.cols = "5"
	}else{
		//This is Netscape
		document.mPostCode.postal_code_disp.cols = "4"
	}

	
}

 
function whichButton() {
	if (event.keyCode == 13) {
		mPopulate();
	}
}


function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
	endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
		return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
		((path == null) ? "" : ("; path=" + path)) +  
		((domain == null) ? "" : ("; domain=" + domain)) +    
		((secure == true) ? "; secure" : "");
}

