/*  
	Title : Auto Suggest Box
	Author : Tom Coote
	Wedsite : http://www.tomcoote.co.uk
*/

 /**
	* @argument SearchInputID - The id of the text input for the loop up.
	* @argument MultiSelectID - The id of the select tag for the results to be placed.
	* @argument fnSearch - The JavaScript function to call to get results, 
	*						this function will get passed a string value of the requested search text and
	*						must return an array of objects with the following members; id, text
	* @argument fnFound - The JavaScript function to call when a result is selected,
	*						this function will get passed the id and text of the selected object plus any extra arguments passed in
	*						when the results were created.
	*/
function AutoSuggestBox2(SearchInputID, MultiSelectID, fnSearch, fnFound) {
    
	var that = {}; // Must use 'that' so not to confuse with 'this' which is the global object.

	/* Private Variables 
	*
	* These variables can only be accessed by functions inside the scope of this object.
	*/
	var eSearchInput, eMultiSelect;
	var iMaxListHeight = 280;
	var arrArgs; 
	var iSelectTextHeight = 22; // You may need to change this depending on your CSS.

    /* Private Functions 
	*
	* These functions can only be accessed by functions inside the scope of this object.
	*/

	function Initiate() {
		eSearchInput = document.getElementById(SearchInputID);
		eMultiSelect = document.getElementById(MultiSelectID);

		// Position the results area directly underneath the input box ready for deployment
		PositionElement();
		eMultiSelect.style.visibility = 'hidden';

		// Add events
		eMultiSelect.onclick = GetResultClickHandler;
		eMultiSelect.onkeypress = GetResultKeyPressHandler;
	}
	var openclose = false;

	function GetResultClickHandler() {
		var id = eMultiSelect.options[eMultiSelect.selectedIndex].id;
		if(id != 'orgform')
		{
			if(openclose)
			{
				animatedcollapse.toggle('orgform');
				openclose = false;
			}
			text = eMultiSelect.options[eMultiSelect.selectedIndex].innerHTML;

			eSearchInput.value = text;
			eMultiSelect.style.visibility = 'hidden';
	
			fnFound(id,text,arrArgs);
		}
		else
		{
			if(!openclose)
 			{
				animatedcollapse.toggle('orgform');
				openclose = true;
 			}
			
			text = eMultiSelect.options[eMultiSelect.selectedIndex].innerHTML;
			eSearchInput.value = text;
			eMultiSelect.style.visibility = 'hidden';
			document.dataform.T_S_person.value = '';
			document.dataform.T_S_email1.value = '';

			document.dataform.T_S_ccode.value = '';
			document.dataform.T_S_acode.value = '';
			document.dataform.T_S_phone.value = '';
	
			document.dataform.T_S_cfax.value = '';
			document.dataform.T_S_afax.value = '';
			document.dataform.T_S_fax.value = '';
		}
	}
	function GetResultKeyPressHandler(e) {
		if (GetKeyCode(e) == 13) {
			GetResultClickHandler();
		}
	}

	function GetKeyCode(e)
	{
		if (e) {
			return e.charCode ? e.charCode : e.keyCode;
		}
		else {
			return window.event.charCode ? window.event.charCode : window.event.keyCode;
		}
	}

	function PositionElement() {
		eMultiSelect.style.position = 'absolute';
		eMultiSelect.style.width = eSearchInput.offsetWidth + 'px';
	}

	/* Public Variables 
	*
	* These variables are available from the returning object that this constructor creates,
	* new public variables can be added to the returning object at any time.
	*/
	var undefined;

	/* Public Functions 
	*
	* These functions are available from the returning object that this constructor creates,
	* new public functions can be added to the returning object at any time.
	*/

	that.CreateResults = function(e) {

		// Check for additional arguments
		if (arguments != undefined) {
			arrArgs = arguments;
		}

		// Check for up/down key press
		var unicode = GetKeyCode(e);

		if (unicode == 40) {
			if (eMultiSelect.style.visibility == 'visible') {
				eMultiSelect.options.selectedIndex = 0;
				eMultiSelect.focus();
				return;
			}
		}
		if (unicode == 38) {
			if (eMultiSelect.style.visibility == 'visible') {
				eMultiSelect.options.selectedIndex = eMultiSelect.options.length-1;
				eMultiSelect.focus();
				return;
			}
		}

		// Check for valid search criteria
		if (eSearchInput.value.length < 1) {
			eMultiSelect.style.visibility = 'hidden';
			return;
		}

		// Get results
		var arrResults = fnSearch(eSearchInput.value), i, eOption, iCount = 0;
		
		if (arrResults == undefined) {
			eMultiSelect.style.visibility = 'hidden';
			return;
		}

		eMultiSelect.innerHTML = ''; 

		for (i=0; i < arrResults.length; i++) {
			if (arrResults[i] != undefined) {

				eOption = document.createElement('option');
				eOption.setAttribute('id',arrResults[i].id);
				//<--added by nitin on 17 Apr 2009
				eOption.setAttribute('onmouseover','style.background=\'#fdffda\';');
				eOption.setAttribute('onmouseout','style.background=\'#FFFFFF\';');
				eOption.setAttribute('style','padding-top:5px;padding-bottom:5px;border-top:1px #dddddd solid;');

				//eOption.attributes.item("style").nodeValue = "padding-top:5px;padding-bottom:5px;border-top:1px #dddddd solid;";

				//eOption.style.backgroundColor = '#fff000';

				//eOption.style.borderTop = '1px solid #dddddd';

				//document.getElementById(arrResults[i].id).style.borderTop='1px solid #dddddd';

				//eOption.style.borderColor = '#dddddd';

				//document.all.eOption.style.borderTop = "1px #dddddd solid";


				
				//_setStyle(eOption, 'padding-top:5px;padding-bottom:5px;border-top:1px #dddddd solid;');


				//-->
				eOption.innerHTML = arrResults[i].text;

				//eOption.innerHTML = "<div style=\"padding-top:5px;padding-bottom:5px;border-top:1px #dddddd solid;\">"+arrResults[i].text+"</div>";

				eMultiSelect.appendChild(eOption);

				iCount++;
			}
		}


		eOption = document.createElement('option');
		eOption.setAttribute('id','orgform');
		//<--added by nitin on 17 Apr 2009
		eOption.setAttribute('onmouseover','style.background=\'#fdffda\';');
		eOption.setAttribute('onmouseout','style.background=\'#eeeeee\';');
		eOption.setAttribute('style','padding-top:5px;padding-bottom:5px;border-top:1px #dddddd solid;');

		//eOption.innerHTML = 'No Organizer Found / Add an Organizer';
		eOption.innerHTML = 'Add New Organizer';
		eOption.style.backgroundColor = '#eeeeee';

		eOption.style.fontWeight = 'bold';

		eMultiSelect.appendChild(eOption);

		//iCount++;


		if (iCount < 1) {
// 			eMultiSelect.style.visibility = 'hidden';
// 			return; // No results found.
		}

		PositionElement();
		var iHeight = iCount*iSelectTextHeight; 
	
		if (iCount > 2) {
			if (iHeight > iMaxListHeight) { // Don't want it to tall on the page
				eMultiSelect.style.height = iMaxListHeight+'px';
			}
			else {
				eMultiSelect.style.height = iHeight+'px';
			}
		}

		eMultiSelect.style.visibility = 'visible';
	}

	Initiate(); // Do all setup when the object is created.

	/* 
	* This (or that) is the object returned with all public members and
	* functions included above when the contructor is instantiated.
	*/
    return that;
}

function rzCC(s)
{
   	// thanks http://www.ruzee.com/blog/2006/07/\
   	// retrieving-css-styles-via-javascript/
   	for(var exp=/-([a-z])/;
       	exp.test(s);
       	s=s.replace(exp,RegExp.$1.toUpperCase()));
   	return s;
}

function _setStyle(element, declaration)
{
   	if (declaration.charAt(declaration.length-1)==';')
     	declaration = declaration.slice(0, -1);
   	var k, v;
   	var splitted = declaration.split(';');
   	for (var i=0, len=splitted.length; i<len; i++)
	{
      		k = rzCC(splitted[i].split(':')[0]);
      		v = splitted[i].split(':')[1];
      		eval("element.style."+k+"='"+v+"'");
	}
}

