/**
 * Constructor for AutoCategorySearch
 * 
 * @param HTMLSelectElement el - the select box which started this
 * @return
 */
function AutoCategorySearch(){
}

/**
 * The following static variable is used to hold the to element for the next search item1.
 */
AutoCategorySearch.toEl = null;
AutoCategorySearch.url = window.location.protocol + "//" + window.location.host
							+ window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')+1)
							+ "../ajax/auto-populate.php";

AutoCategorySearch.to = null;

//Used for auto-populating chaining events
AutoCategorySearch.nextMode = null;
AutoCategorySearch.nextTarget = null;
AutoCategorySearch.nextDefault = null;

//used to determine the current mode
AutoCategorySearch.currentMode = null;

/**
 * Loads the select with values returned from synchronous load call
 * 
 * @param String data
 * @return void
 */
AutoCategorySearch.loadSelect = function(data){
	//alert(data);
	var selectList = eval('(' + data + ')');
	
	var curIndex = AutoCategorySearch.toEl.value; //selectedIndex;
	
	//Do we have a default override?
	if (!AutoCategorySearch.nextMode && AutoCategorySearch.nextDefault)
		curIndex = AutoCategorySearch.nextDefault;
	
	//remove the previous options
	AutoCategorySearch.clearSelect(AutoCategorySearch.toEl);
	
	//if we don't already have selected value, then we need to make sure not to make none the selected value
	/*if((curIndex <= 0 && AutoCategorySearch.searchArray(AutoCategorySearch.nextDefault, selectList, true) == false)
			|| AutoCategorySearch.searchArray(curIndex, selectList, true) == false)
	{
		//add the default option
//		AutoCategorySearch.addToSelect(AutoCategorySearch.toEl, 0, "None", true);
	}
	else
	{
		//add the default option
//		AutoCategorySearch.addToSelect(AutoCategorySearch.toEl, 0, "None", false);
	}*/
	
	if (!AutoCategorySearch.nextMode)
		AutoCategorySearch.nextDefault = null;
	
	//add the new options to the select element
	for (item1 in selectList)
	{
		//if we have a selected index, and its found
		if(AutoCategorySearch.searchArray(curIndex, selectList, true) != false && curIndex == item1)
		{
			AutoCategorySearch.addToSelect(AutoCategorySearch.toEl, item1, selectList[item1], true);
		}else{
			AutoCategorySearch.addToSelect(AutoCategorySearch.toEl, item1, selectList[item1], false);
		}
	}
	
	curIndex = AutoCategorySearch.toEl.value; //selectedIndex;

	//Should we auto-populate another field?
	if (AutoCategorySearch.nextMode && AutoCategorySearch.nextTarget) {
		var nextMode = AutoCategorySearch.nextMode;
		var nextTarget = AutoCategorySearch.nextTarget;
		//var nextDefault = AutoCategorySearch.nextDefault;
		//Reset to defaults
		AutoCategorySearch.nextMode = null;
		AutoCategorySearch.nextTarget = null;
		//AutoCategorySearch.nextDefault = null;
		//alert("Value: "+ AutoCategorySearch.toEl.value);
		//alert("Index: "+ AutoCategorySearch.toEl.selectedIndex);
		//alert("Mode: "+ nextMode);
		//alert("Target: "+ nextTarget);
		AutoCategorySearch.prototype.search(curIndex, nextMode, nextTarget);
	}
	resetCursor();
};

/**
 * Array search function for finding items in an array
 * 
 * @param mixed needle - the item to search for
 * @param array haystack - array to search in
 * @param boolean searchByKey - whether or not a key is the needle
 * @return
 */
AutoCategorySearch.searchArray=function(needle, haystack, searchByKey){
	for(i in haystack)
	{
		if(searchByKey && i==needle)
		{
			return i;
		}
		else if(needle == haystack[i])
		{
			return i;
		}
	}
	return false;
};

/**
 * Clear the select of all elements
 * @param clear the select of all elements selectToClear
 * @return
 */
AutoCategorySearch.clearSelect=function(selectToClear){
	// Always clear an option list from the last entry to the first
    for (x = selectToClear.length; x >= 0; x = x - 1) {
    	selectToClear[x] = null;
    }
	/*for (item1 in selectToClear.options){
		selectToClear.remove(item1);
	}*/
};

/**
 * Add to select box
 * 
 * @param HTMLSelectElement selectToAddTo
 * @param String optionValue - the value needed by the application
 * @param String optionText - the Text the user will see
 * @param bool isSelected - whether or not the item1 is selected
 * @return void
 */
AutoCategorySearch.addToSelect=function(selectToAddTo, optionValue, optionText, isSelected){
	var optionToAdd = document.createElement('option');
	optionToAdd.text = optionText;
	optionToAdd.value = optionValue;
	if(isSelected)
	{
		optionToAdd.selected = true;
	}
	try{
		selectToAddTo.add(optionToAdd, null);
	} catch (ex) {
		selectToAddTo.add(optionToAdd);
	}
};

/**
 * Used to search for categories and sub-categories in chain (one after the other)
 * @param item1
 * @return boolean indicating search started or not
 */
AutoCategorySearch.prototype.searchInChain=function(item1, mode, to, modeNext, toNext, defaultNext) {
	AutoCategorySearch.nextMode = modeNext;
	AutoCategorySearch.nextTarget = toNext;
	AutoCategorySearch.nextDefault = defaultNext;
	AutoCategorySearch.prototype.search(item1, mode, to);
}

/**
 * Used to search for sub categories
 * @param item1
 * @return boolean indicating search started or not
 */
AutoCategorySearch.prototype.search=function(item1, mode, to) {
	//set the to
	AutoCategorySearch.toEl = document.getElementById(to);
	AutoCategorySearch.currentMode = mode;
	
	//No values here
	if (item1 <= 0) {
		//remove the previous options
		AutoCategorySearch.clearSelect(AutoCategorySearch.toEl);
		//Make none the selected value
		AutoCategorySearch.addToSelect(AutoCategorySearch.toEl, 0, "Please Select Region To Populate", true);
		return;
	}
	
	//intialize communication
	var url = encodeURI(AutoCategorySearch.url + "?v=" + mode + "&id=" + item1);
	
	//initialize communications
	var comm = new Communication(url);
	
	waitCursor();
	//send request
	comm.send('', AutoCategorySearch.loadSelect);
	
};

autoCatSearch = new AutoCategorySearch();
autoSubCatSearch = new AutoCategorySearch();

function waitCursor(){
	//switch cursor to wait mode
	$('body').css('cursor', 'wait'); 
	$('input').css('cursor', 'wait');
	$('select').css('cursor', 'wait');
}

function resetCursor(){
	//switch cursor to wait mode
	$('body').css('cursor', 'default'); 
	$('input').css('cursor', 'pointer');
	$('select').css('cursor', 'pointer');
}


