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);
	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')){

		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');
	}
	if(document.getElementById('drop_list_2') !=null){
	dropDown('drop_list_2', 'drop_box_2', 'drop_image_2');
	}
	if(document.getElementById('drop_list_3') !=null){
	dropDown('drop_list_3', 'drop_box_3', 'drop_image_3');
	}
	if(document.getElementById('drop_list_4') !=null){
	dropDown('drop_list_4', 'drop_box_4', 'drop_image_4');
	}
	if(document.getElementById('drop_list_5') !=null){
	dropDown('drop_list_5', 'drop_box_5', 'drop_image_5');
	}
	if(document.getElementById('drop_list_6') !=null){
	dropDown('drop_list_6', 'drop_box_6', 'drop_image_6');
	}
	if(document.getElementById('drop_list_7') !=null){
	dropDown('drop_list_7', 'drop_box_7', 'drop_image_7');
	}
	if(document.getElementById('drop_list_8') !=null){
	dropDown('drop_list_8', 'drop_box_8', 'drop_image_8');
	}	
	if(document.getElementById('drop_list_9') !=null){
	dropDown('drop_list_9', 'drop_box_9', 'drop_image_9');
	}	
    if(document.getElementById('drop_list_10') !=null){
	dropDown('drop_list_10', 'drop_box_10', 'drop_image_10');
	}
    if(document.getElementById('drop_list_11') !=null){
	dropDown('drop_list_11', 'drop_box_11', 'drop_image_11');
	}
    if(document.getElementById('drop_list_12') !=null){
	dropDown('drop_list_12', 'drop_box_12', 'drop_image_12');
	}
    if(document.getElementById('drop_list_13') !=null){
	dropDown('drop_list_13', 'drop_box_13', 'drop_image_13');
	}
    if(document.getElementById('drop_list_14') !=null){
	dropDown('drop_list_14', 'drop_box_14', 'drop_image_14');
	}
    if(document.getElementById('drop_list_15') !=null){
	dropDown('drop_list_15', 'drop_box_15', 'drop_image_15');
	}
    if(document.getElementById('drop_list_16') !=null){
	dropDown('drop_list_16', 'drop_box_16', 'drop_image_16');
	}
    if(document.getElementById('drop_list_17') !=null){
	dropDown('drop_list_17', 'drop_box_17', 'drop_image_17');
	}
    if(document.getElementById('drop_list_18') !=null){
	dropDown('drop_list_18', 'drop_box_18', 'drop_image_18');
	}
    if(document.getElementById('drop_list_19') !=null){
	dropDown('drop_list_19', 'drop_box_19', 'drop_image_19');
	}
    if(document.getElementById('drop_list_20') !=null){
	dropDown('drop_list_20', 'drop_box_20', 'drop_image_20');
	}
    if(document.getElementById('drop_list_21') !=null){
	dropDown('drop_list_21', 'drop_box_21', 'drop_image_21');
	}
    if(document.getElementById('drop_list_22') !=null){
	dropDown('drop_list_22', 'drop_box_22', 'drop_image_22');
	}
    if(document.getElementById('drop_list_23') !=null){
	dropDown('drop_list_23', 'drop_box_23', 'drop_image_23');
	}
    if(document.getElementById('drop_list_24') !=null){
	dropDown('drop_list_24', 'drop_box_24', 'drop_image_24');
	}
    if(document.getElementById('drop_list_25') !=null){
	dropDown('drop_list_25', 'drop_box_25', 'drop_image_25');
	}
    if(document.getElementById('drop_list_26') !=null){
	dropDown('drop_list_26', 'drop_box_26', 'drop_image_26');
	}
    if(document.getElementById('drop_list_27') !=null){
	dropDown('drop_list_27', 'drop_box_27', 'drop_image_27');
	}
    if(document.getElementById('drop_list_28') !=null){
	dropDown('drop_list_28', 'drop_box_28', 'drop_image_28');
	}
    if(document.getElementById('drop_list_29') !=null){
	dropDown('drop_list_29', 'drop_box_29', 'drop_image_29');
	}
    if(document.getElementById('drop_list_30') !=null){
	dropDown('drop_list_30', 'drop_box_30', 'drop_image_30');
	}
    if(document.getElementById('drop_list_31') !=null){
	dropDown('drop_list_31', 'drop_box_31', 'drop_image_31');
	}
    if(document.getElementById('drop_list_32') !=null){
	dropDown('drop_list_32', 'drop_box_32', 'drop_image_32');
	}
    if(document.getElementById('drop_list_33') !=null){
	dropDown('drop_list_33', 'drop_box_33', 'drop_image_33');
	}
    if(document.getElementById('drop_list_34') !=null){
	dropDown('drop_list_34', 'drop_box_34', 'drop_image_34');
	}
    if(document.getElementById('drop_list_35') !=null){
	dropDown('drop_list_35', 'drop_box_35', 'drop_image_35');
	}
    if(document.getElementById('drop_list_36') !=null){
	dropDown('drop_list_36', 'drop_box_36', 'drop_image_36');
	}
    if(document.getElementById('drop_list_37') !=null){
	dropDown('drop_list_37', 'drop_box_37', 'drop_image_37');
	}
    if(document.getElementById('drop_list_38') !=null){
	dropDown('drop_list_38', 'drop_box_38', 'drop_image_38');
	}
    if(document.getElementById('drop_list_39') !=null){
	dropDown('drop_list_39', 'drop_box_39', 'drop_image_39');
	}
    if(document.getElementById('drop_list_40') !=null){
	dropDown('drop_list_40', 'drop_box_40', 'drop_image_40');
	}
    if(document.getElementById('drop_list_41') !=null){
	dropDown('drop_list_41', 'drop_box_41', 'drop_image_41');
	}
    if(document.getElementById('drop_list_42') !=null){
	dropDown('drop_list_42', 'drop_box_42', 'drop_image_42');
	}
    if(document.getElementById('drop_list_43') !=null){
	dropDown('drop_list_43', 'drop_box_43', 'drop_image_43');
	}
    if(document.getElementById('drop_list_44') !=null){
	dropDown('drop_list_44', 'drop_box_44', 'drop_image_44');
	}
    if(document.getElementById('drop_list_45') !=null){
	dropDown('drop_list_45', 'drop_box_45', 'drop_image_45');
	}
    if(document.getElementById('drop_list_46') !=null){
	dropDown('drop_list_46', 'drop_box_46', 'drop_image_46');
	}
    if(document.getElementById('drop_list_47') !=null){
	dropDown('drop_list_47', 'drop_box_47', 'drop_image_47');
	}
    if(document.getElementById('drop_list_48') !=null){
	dropDown('drop_list_48', 'drop_box_48', 'drop_image_48');
	}
    if(document.getElementById('drop_list_49') !=null){
	dropDown('drop_list_49', 'drop_box_49', 'drop_image_49');
	}
    if(document.getElementById('drop_list_50') !=null){
	dropDown('drop_list_50', 'drop_box_50', 'drop_image_50');
	}
    if(document.getElementById('drop_list_51') !=null){
	dropDown('drop_list_51', 'drop_box_51', 'drop_image_51');
	}
    if(document.getElementById('drop_list_52') !=null){
	dropDown('drop_list_52', 'drop_box_52', 'drop_image_52');
	}
    if(document.getElementById('drop_list_53') !=null){
	dropDown('drop_list_53', 'drop_box_53', 'drop_image_53');
	}
    if(document.getElementById('drop_list_54') !=null){
	dropDown('drop_list_54', 'drop_box_54', 'drop_image_54');
	}

}

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)
{
	if(objEl.parentNode.id == parentID)
	{
		return true;
	}
	else if(objEl.parentNode.nodeName == 'BODY')
	{
		return false;
	}
	else
	{
		return checkParent(objEl.parentNode, parentID);
	}
}

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 : '';
}

