//Author				: Sonja Goosen
//Date Created			: 31-Mar-2005
//Last Changed by		: Sonja Goosen
//Date Changed			: 20-Apr-2005
//Version				: 2.0.20042005
//Comments:
//Date:		Desc:															
//--------------------------------------------------------------------------------------------
//7-Apr-2005 - add secondOption to clearListBox
//11-Apr-2005 Moved dofill.asp
//19-Apr-2005 New directory structure
//20-Apr-2005 Change UseSecConn parameter to dynamicFillPage
//
//ToDoList:
//Date:		Desc:															Status:
//--------------------------------------------------------------------------------------------


//These functions are used to fill listboxes

//Clear the listbox
//firstOption (optional) is added to the empty listbox
//secondOption (optional) is added to the empty listbox
function clearListBox(listObj, firstOption, secondOption){

	if (listObj.options)
	{
	    var len = listObj.options.length-1
	    for (var i = len; i >= 0; i--){
		    listObj.remove(i)
	    }
    		
	    if (firstOption != null) {
		    var ofirstOption = document.createElement("OPTION");
		    //ofirstOption.setAttribute('autocomplete','off');
		    
		    /*CHANGED BY PIETER LE ROUX 11-SEP-2007: WHEN FIRST OPTION THEN ALWAYS -1, WHEN SECOND OPTION AVAILABLE ALWAYS 0*/
		    /*if (secondOption != null) {*/
			    ofirstOption.value = "-1";
		    /*}
		    else {
			    ofirstOption.value = "0";
		    }*/
    		
		    ofirstOption.text = firstOption;
		    //listObj.add(ofirstOption,null);
		    listObj.options.add(ofirstOption);
	    }

	    if (secondOption != null) {
		    var osecondOption = document.createElement("OPTION");
		    osecondOption.value = "0";
		    osecondOption.text = secondOption;			
		    //listObj.add(osecondOption,null);
		    listObj.options.add(osecondOption);
	    }
    }	
}

//Fill the listbox from the string of value/text pairs
/*
	listObj = reference to the listbox to fill 
	strArr = string with format: value chr(31) text chr(30) value chr(31) text chr(30) value chr(31) text
	firstOption = (optional) this text will be added as the first option in the list with value 0 or -1 (if second option is secified)	
	secondOption = (optional) this text will be added as the second option in the list with value 0
*/
function fillListBox(listObj, strArr, firstOption, secondOption) {

	//listObj.setAttribute('autocomplete','off');

	clearListBox(listObj, firstOption, secondOption)

	var arr = strArr.split(String.fromCharCode(30))
	
	if (arr[0].length > 0) {
		for (var i=0; i<arr.length; i++){
			var optionPair = arr[i].split(String.fromCharCode(31))
			var oOption = document.createElement("OPTION");
			oOption.value = optionPair[0];
			oOption.text = optionPair[1];			
			//listObj.add(oOption, null);
			listObj.options.add(oOption);
		}	
	}
}

/*
Call the given stored procedures to populate the given listbox in the shuffler
	listboxName = referece of listbox to populate
	sql = sql query to execute to return the items to populate the listbox with
	DBColumnID = name of column returned by stored proc that contains the ID for an item (value of option)
	DBColumnName = name of column returned by stored proc that contains the text to display for an item (text of option)
	dynamicFillPage = defined the dofill_dynamic.asp page that contains the database connection, to use. Exapmle: "bin/commoncomponents/fill/Security/dofill_dynamic.asp"
	firstOption = (optional) this text will be added as the first option in the list with value 0 or -1 (if second option is secified)	
	secondOption = (optional) this text will be added as the second option in the list with value 0
*/
function fillListBoxSql(listbox, sql, DBColumnID, DBColumnName, dynamicFillPage, firstOption, secondOption) {	

	var completePop = function(oXML) 
	{		
		var result = oXML.responseText
		//alert(result);
		if (result.substr(0,5) == "ERROR") {
			alert("An error has occurred while populating the listbox: " + result)
		}					
		if (result.trim() != "") {
			//Populate listbox
			fillListBox(listbox, result, firstOption, secondOption);
		}
		else {
			//alert("No values found.");
		}
		regularCursor();
	}
	//var page = "bin/commoncomponents/fill/dofill_dynamic.asp"
	var page = dynamicFillPage
	var data = "action=getListData&proc=" + encodeURIComponent(sql) + "&DBColumnID=" + DBColumnID + "&DBColumnName=" + DBColumnName+ "&dynamicFillPage=" + dynamicFillPage + "&buildHtml=false"		

	hiddenSubmit(page,data, completePop, 0, null, null, false)
}




