function selectedSelectBox(selectBoxID, listBoxID, text)
{
	if(document.getElementById(selectBoxID))
	{
		if(text != 'reset')
		{
			document.getElementById(selectBoxID).innerHTML = text;
			document.getElementById(listBoxID).style.display = 'none';
		}
		else
		{
			alert('reset');
		}
	}
}

function showList(listid)
{
	if(document.getElementById(listid).style.display == 'block')
	{
		document.getElementById(listid).style.display = 'none';
	}
	else
	{
		document.getElementById(listid).style.display = 'block';
	}
}

//Main function to generate the drop down menu
function dropDown(targetEl, resultsEl, actionButton, callbackFunction)
{
	//assign the object
	objTarget = document.getElementById(targetEl);

	//hide the drop down by default
	hideDropDown(targetEl);

	//assign the button click to open/hide the dropdown
	assignButtonClick(objTarget, actionButton)

	//assign click functionality to each of individual child nodes
	assignChildNodeClick(objTarget, targetEl, resultsEl, callbackFunction)

	//create a hidden element on the document
	if(!document.getElementById('openDropDown'))
	{
		var hiddenElement = document.createElement('input');
		hiddenElement.setAttribute('type', 'hidden');
		hiddenElement.setAttribute('id', 'openDropDown');
		document.body.appendChild(hiddenElement);
	}
}

//function to hide the drop down menu.
function hideDropDown(target)
{
	objEL = document.getElementById(target);

	if(objEL)
	{
		if(objEL.style.display == 'block' || !objEL.style.display)    
		{
			objEL.style.display = 'none';
		}
	}
	else
	{
		alert('There is some problem with drop down with id : ' + target);
	}
}

//this function will bind the click event to show/hide the drop down
function assignButtonClick(target, actionButtonID)
{
	actionButton = document.getElementById(actionButtonID);
	actionButton.onclick = function() {
		currentOpenDD = document.getElementById('openDropDown').value
		if(currentOpenDD != '' && currentOpenDD == (target.id + ';;' + actionButtonID))
		{
			document.getElementById('openDropDown').value = '';
		}
		else if(currentOpenDD != '')
		{
			values = currentOpenDD.split(';;')
			hideDropDown(values[0])
			document.getElementById('openDropDown').value = target.id + ';;' + actionButtonID;
		}
		else
		{
			document.getElementById('openDropDown').value = target.id + ';;' + actionButtonID;
		}

		if(target.style.display == 'block' || !target.style.display)  
		{
			target.style.display = 'none';
		}
		else
		{
			target.style.display = 'block';
		}
	}
}

//function will bind each of the child nodes with the function to process clicks
function assignChildNodeClick(target, targetEl, resultsEl, callback)
{
	arrListElements = target.getElementsByTagName('LI');
	for(i=0;i<arrListElements.length;i++)
	{
		arrAnchorChild = arrListElements[i].getElementsByTagName('A');
		for(j=0; j<arrAnchorChild.length;j++)
		{
			currentNode = arrAnchorChild[j];
			currentNode.onclick = function() {
				if(callback)
				{
					callback.call(this, this, targetEl, resultsEl);
				}
				else
				{
					processClick(this, targetEl, resultsEl);
				}
			}
		}
	}
}

//function to select the current value and mark it as selected
function processClick(objAnchor, targetEl, resultsEl)
{
	document.getElementById(targetEl).style.display = 'none';
	document.getElementById(resultsEl).innerHTML = trim(objAnchor.innerHTML);
	var selected_value = trim(objAnchor.id);
	//alert(resultsEl); return false;
	if(document.getElementById('frmMemberTitle') && resultsEl == 'drop_box_1')
	{
		document.getElementById('frmMemberTitle').value =  trim(objAnchor.innerHTML);
	}
	else if(document.getElementById('frmStateName') && resultsEl == 'drop_box_2')
	{
		document.getElementById('frmStateName').value =  trim(objAnchor.innerHTML);
	}
	else if(document.getElementById('frmTopic') && resultsEl == 'drop_box_1')
	{
		document.getElementById('frmTopic').value =  trim(objAnchor.innerHTML);
	}
	arr = selected_value.split('_'); 
}

//bind the click function on the window and call callback function on each click
if (window.document.addEventListener){
  window.document.addEventListener('click', captureClick, false);
} else if (window.document.attachEvent){
  window.document.attachEvent('onclick', captureClick);
}

//callback function. called on each click on window
function captureClick(e)
{
	//capture the target element on which click action is performed
	if (window.event)
	{
		target = window.event.srcElement;
	}
	else
	{
		target = e.target ? e.target : e.srcElement;
	}

	if(document.getElementById('openDropDown').value != '')
	{
		arrValues = document.getElementById('openDropDown').value.split(';;')

		if(!checkParent(target, arrValues[0]))
		{
			if(target.id != arrValues[1])
			{
				hideDropDown(arrValues[0])
				document.getElementById('openDropDown').value = '';
			}
		}
	}
}

function loadEvents()
{
	//Load the drop down function as the script loads
	if(document.getElementById('drop_list_1') !=null){
	dropDown('drop_list_1', 'drop_box_1', 'drop_image_1');
	}
}

function extraCallback()
{
	var objAnchor = arguments[0];
	var targetElement = arguments[1];
	var resultElement = arguments[2];
		
	myParent = objAnchor.parentNode;
	
	arrListItems = myParent.parentNode.getElementsByTagName('LI');
	
	for(i=0; i<arrListItems.length;i++)
	{
		children = arrListItems[i].childNodes;
		if(children[0].nodeName == 'SPAN')
		{
			arrListItems[i].innerHTML = children[0].innerHTML
		}
	}

	var originalHTML = objAnchor.parentNode.innerHTML;
	objAnchor.parentNode.innerHTML = '<span>' + originalHTML + '</span>';
	
	arrImgItems = document.getElementById(resultElement).getElementsByTagName('IMG');
	arrImgItems[0].src = arrImgItems[0].src.replace(/flag(.*).gif/ig, 'flag_' + objAnchor.className + '.gif');
}


function addMultipleLoadEvents(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function() {
			if (oldonload)
			{
				oldonload();
			}
			func();
		}
	}
}

function checkParent(objEl, parentID)
{
//	alert(objEl+'<=====>'+parentID+'<=====>'+objEl.id+'<=====>'+objEl.class+'<=====>'+objEl.parentNode.id);	
	if(objEl.id != 'undefined')
	{
		if(objEl.parentNode.id)	
		{
			if(objEl.parentNode.id == parentID)
			{
				return true;
			}
			else if(objEl.parentNode.nodeName == 'BODY')
			{
				return false;
			}
			else
			{
				return checkParent(objEl.parentNode, parentID);
			}
		}
		else
		{
			//alert("IN ELSE");
		}
	}
	else
	{
		return true;	
	}
}

addMultipleLoadEvents(loadEvents);

function trim (str, charlist) {
    // Strips whitespace from the beginning and end of a string  
    // *     example 1: trim('    Kevin van Zonneveld    ');
    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: trim('Hello World', 'Hdle');
    // *     returns 2: 'o Wor'
    // *     example 3: trim(16, 1);
    // *     returns 3: 6
    var whitespace, l = 0, i = 0;
    str += '';
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}


