
/******************************** query variables ********************************************/

var arrayname='region';

var querystring = window.location.search.substring(1); 
var url = new String(window.location);
var index = url.indexOf(".html");
var url = url.substring(0, index);
var index1 = url.lastIndexOf("_");

var regionid = url.substring(index1 + 1);
if(isNaN(regionid))
	regionid = "";

var countryid = getQueryVariable("country", querystring);
//regionid = "4";
//countryid = "144";

if(regionid == null)
	regionid = "";
if(countryid == null)
	countryid = "";



/****************************************************************************/


// Code to create the select boxes
document.write ('<div class="sp5">');
document.write('<form name="regionForm">');
document.write('<table width="600" border="0" cellspacing="0" cellpadding="0"><tr><td nowrap width="250">');
document.write('Region : <SELECT style="width:190" NAME="regionSelect" VERYSILLY="NO" SIZE="1" onChange="changeDropdown(regionSelectIndex, this, marketcode)"></SELECT>');
document.write('</td><td nowrap width="250">&nbsp;Country : ');		
document.write('<SELECT style="width:190" NAME="countrySelect" VERYSILLY="NO" onChange="changeDropdown(countrySelectIndex, this, marketcode)" SIZE="1"></SELECT>');
document.write('</td><td nowrap width="50">&nbsp;&nbsp;<a href="javascript:regionHandler(marketcode)"><img src="/images/about/listed/btn_go.gif" border="0"></a></td><td width="70">&nbsp;</td></tr></table>');
document.write('</form>');
document.write ('</div>');



/******************************** javascript functions ********************************************/


function regionsort(a, b){

	var aname = a[1].toLowerCase();
	var bname = b[1].toLowerCase();

    if(aname.indexOf("the") == 0) {
	    aname = aname.substring(3);
		aname = fnTrim(aname);
	}
    if(bname.indexOf("the") == 0) {
		bname = bname.substring(3);
		bname = fnTrim(bname);
	}

	if(aname < bname)
		return -1;
	else if(aname > bname)
		return 1;
	else
		return 0;	
		
}

/*
 * Function to populate the initial values in the industry, supersector, sector and subsector drop downs.
 */
function poplateDropDowns(menuname,arrayname,rows, market, selectedid)
{
	var obj = document.forms["regionForm"].elements[menuname];
	var eventArray = eval(arrayname);
	menuSelect=obj.form.elements[menuname];
	var nOptions;
	//alert(menuname);
	//alert(menuSelect);

	menuSelect.length=0;

	if(rows<eventArray.length)
	{
	nOptions = rows;
	}
	
	if(rows>eventArray.length)
	{
	nOptions = eventArray.length;
	}

	if ((rows==0) || (rows == eventArray.length))
	{
	nOptions = eventArray.length;
	}
	
	var optionName = "";	
	if(menuname == "regionSelect")
		optionName = "Select One";
	if(menuname == "countrySelect")
		optionName = "Select One";

	document.forms["regionForm"].elements[menuname][0]=new Option(optionName,"-1");

	var varname = "";

	for(i=1;i<=nOptions;i++)
	{
		if(eventArray[i-1][0].length>40)	
		{
			document.forms["regionForm"].elements[menuname][i]=new Option(eventArray[i-1][0].substring(0,40), eventArray[i-1][1]);

			varname = "window.lc_" + market + "_" + arrayname + "_" + eventArray[i-1][1] + "_count";
			if(window.eval(varname) != undefined) {
				if(eval(varname) == 0)
					document.forms["regionForm"].elements[menuname].options[i].style.cssText = "color:cfcfcf";
			} else
					document.forms["regionForm"].elements[menuname].options[i].style.cssText = "color:cfcfcf";

			if(menuname == "regionSelect" && selectedid == eventArray[i-1][1])
				document.forms["regionForm"].elements[menuname].options[i].selected = true;
		}
		else
		{
			document.forms["regionForm"].elements[menuname][i]=new Option(eventArray[i-1][0], eventArray[i-1][1]);
			varname = "window.lc_" + market + "_" + arrayname + "_" + eventArray[i-1][1] + "_count";
			if(window.eval(varname) != undefined) {
				if(window.eval(varname) == 0)
					document.forms["regionForm"].elements[menuname].options[i].style.cssText = "color:cfcfcf";
			} else
					document.forms["regionForm"].elements[menuname].options[i].style.cssText = "color:cfcfcf";

			if(menuname == "regionSelect" && selectedid == eventArray[i-1][1])
				document.forms["regionForm"].elements[menuname].options[i].selected = true;
		}
		
	}


}

/*
 * Generate the drop down options according to the selected industry / supersector / sector / subsector
 */
function changeDropdownValues(fieldname, elementname, market, eventtype)
{
	if(eval("eventtype") == undefined)
		eventtype = "";

	var obj = document.forms["regionForm"].elements[0];
	var arrayname='region';
	var menuname="";

	if(fieldname=='regionSelect')
	{
		arrayname=arrayname+"_"+obj.form.elements['regionSelect'].value;
		menuname='countrySelect';	
		for(;document.forms["regionForm"].elements[menuname].length;)
		{
			document.forms["regionForm"].elements[menuname][0]=null;
		}
	} else if(fieldname=='countrySelect') {
		return;
	}

	//alert(fieldname + "," + arrayname)
	var eventArray = eval(arrayname);
	var nOptions = eventArray.length;
	var k=0;
	if(fieldname=='regionSelect')
		document.forms["regionForm"].elements[menuname][k++]=new Option("View All","-1");	

	var varname = "";
	for(i=0;i<nOptions;i++)
	{
		document.forms["regionForm"].elements[menuname][k++]=new Option(eventArray[i][0], eventArray[i][1]);
		varname = "lc_" + market + "_" + elementname+ "_" + eventArray[i][1] + "_count";
		if(window.eval(varname) != undefined) {
			if(window.eval(varname) == 0)
				document.forms["regionForm"].elements[menuname].options[k-1].style.cssText = "color:cfcfcf";
		}
	}

	if(fieldname=='regionSelect') {
		var selobj = document.forms["regionForm"].elements["countrySelect"];
		var selindex = 0;
		for(i = 0; i < selobj.length;i++) {
			if(selobj.options[i].value == countryid) {
				selindex = i;
				break;
			}
		}
		if(eventtype == "pageload") {
			document.forms["regionForm"].elements["countrySelect"].options[selindex].selected = true;
			changeDropdownValues('countrySelect', market, eventtype);
		} else
			changeDropdownValues('countrySelect', market);
	}

}

//This function is to remove the null values in the select box
function delNul(lst){
    i = 0;
    while (i<lst.options.length) {
        if (lst.options[i].value==-1)
            lst.options[i]=null
        else
            i = i + 1;
    }
}

/*
 * On changing the option of the drop down, call the necessary function to generate the remaining drop downs.
 */
function changeDropdown(selectIndex, element, market) {

	var ind = document.forms["regionForm"].elements[element.name].selectedIndex;
	var val = document.forms["regionForm"].elements[element.name].options[ind].value;

	if(val == -1 && element.name == "regionSelect") {
		poplateDropDowns("countrySelect", "country","0", market);

		regionSelectIndex = document.forms["regionForm"].elements["regionSelect"].selectedIndex;
		countrySelectIndex = document.forms["regionForm"].elements["countrySelect"].selectedIndex;
		return;
	} 

	if(val == -1 && element.name == "countrySelect") {
		regionSelectIndex = document.forms["regionForm"].elements["regionSelect"].selectedIndex;
		return;
	}

	if(document.forms["regionForm"].elements[element.name].options[ind].style.cssText == "") {
		if(element.name == "regionSelect")
			changeDropdownValues(element.name, "country", market);
		regionSelectIndex = document.forms["regionForm"].elements["regionSelect"].selectedIndex;
		countrySelectIndex = document.forms["regionForm"].elements["countrySelect"].selectedIndex;
	} else {
		document.forms["regionForm"].elements[element.name].options[selectIndex].selected = true;
	}

}

/*
 * On click on the go button load the page with respect to the selected Industry, Super Sector, Sector and SubSector.
 */
function regionHandler(market) { 
	var iRegionIndex = document.regionForm.regionSelect.selectedIndex;  
	var selectedRegion = document.regionForm.regionSelect.options[iRegionIndex].value; 	
	var iCountryIndex = document.regionForm.countrySelect.selectedIndex;  
	var selectedCountry = document.regionForm.countrySelect.options[iCountryIndex].value; 	

	var url = window.location.search.substring(1); 
	var sLstComp = getQueryVariable("ListedComp", url);
	if (sLstComp == null) {
	  sLstComp = "All";
	}
	
	var regionid = "";
	var countryid = "";

	if (iCountryIndex != 0) { 
		for(i = 0; i < country.length; i++) {
			if(country[i][1] == selectedCountry) {
				regionid = country[i][2];
				break;
			}
		}

		//alert( "/about/listed/lc_" + market + "_region_" + regionid + ".html?country="+selectedCountry);
		window.location.href = "/about/listed/lc_" + market + "_region_" + regionid + ".html?country="+selectedCountry; 
	} else if(iRegionIndex != 0 && iCountryIndex == 0) { 
		for(i = 0; i < region.length; i++) {
			if(region[i][1] == selectedRegion) {
				regionid = region[i][2];
				break;
			}
		}

		//alert( "/about/listed/lc_" + market + "_region_" + regionid + ".html");
		window.location.href = "/about/listed/lc_" + market + "_region_" + regionid + ".html"; 
	} else {
		alert ('Please select Region/Country');
	}
}




/****************************************************************************/

// Used to include the country js files according to the selected region / country values for nyse and arca

if(regionid != "") {
	var include_countryid = "";
	if(countryid != "") {
		var idexists = false;
		for(i = 0; i < country_id.length; i++) {
			if(country_id[i] == countryid) {
				idexists = true;
				break;
			}
		}

		if(idexists)
			include_countryid = countryid + ",";
	} else if(regionid != "") {

		var idexists = false;
		for(i = 0; i < region_id.length; i++) {
			if(region_id[i] == regionid) {
				idexists = true;
				break;
			}
		}
		
		if(idexists) {
			var include_regionid = regionid;
			var region_array = eval("region_" + include_regionid);
			for(j = 0; j < region_array.length; j++) {
				include_countryid += region_array[j][1] + ",";
			}
		}
	}

	if(include_countryid.length > 0)
		include_countryid = include_countryid.substring(0, include_countryid.length - 1);
	//alert(include_countryid)

	var strcountry = include_countryid.split(",");
	for(i = 0; i < strcountry.length; i++) {
		//alert("<scr" + "ipt src='/about/listed/lc_" + marketcode + "_country_" + strcountry[i] + "_companies.js' language='JavaScript' type='text/javascript'>" + "</scr" + "ipt>")
		document.write("<scr" + "ipt src='/about/listed/lc_" + marketcode + "_country_" + strcountry[i] + "_companies.js' language='JavaScript' type='text/javascript'>" + "</scr" + "ipt>");
	}
}

/****************************************************************************/

// if industry id is empty then the four drop downs will be populated with the initial values, else populated with the respective industry / supersector / sector / subsector selection
if(regionid != "") {
	poplateDropDowns("regionSelect", "region","0", marketcode, regionid);
	changeDropdownValues("regionSelect", "country", marketcode, "pageload");
} else {
	poplateDropDowns("regionSelect", "region","0", marketcode, regionid);
	poplateDropDowns("countrySelect", "country","0", marketcode, countryid);
}

/****************************************************************************/

//Below values are used to set back the option to older one if the selected option does not have any company in it
var regionSelectIndex = document.forms["regionForm"].elements["regionSelect"].selectedIndex;
var countrySelectIndex = document.forms["regionForm"].elements["countrySelect"].selectedIndex;

var country_text = "";
if(regionid != "") {
	var selobj = document.forms["regionForm"].elements["regionSelect"];
	var selindex = 0;
	for(i = 0; i < selobj.length;i++) {
		if(selobj.options[i].value == regionid) {
			selindex = i;
			break;
		}
	}
	country_text += document.forms["regionForm"].elements["regionSelect"].options[selindex].text + ",";
}
if(countryid != "") {
	var selobj = document.forms["regionForm"].elements["countrySelect"];
	var selindex = 0;
	for(i = 0; i < selobj.length;i++) {
		if(selobj.options[i].value == countryid) {
			selindex = i;
			break;
		}
	}
	country_text += document.forms["regionForm"].elements["countrySelect"].options[selindex].text + ",";
}


if(country_text.length > 0) {
	country_text = country_text.substring(0, country_text.length - 1);
	document.write ("<table width='600' border='0' cellspacing='0' cellpadding='0'><tr><td><span class='fontbold'>");
	var country_text = country_text.split(",");
//	for(i = 0; i < industry_text.length; i++) {
//		if(i == 0)
//			document.write(industry_text[i]);
//		else
//			document.write(" > " + industry_text[i]);
//	}
	document.write(country_text[country_text.length - 1]);
	document.write ("</span></td></tr></table>");
}


