//Map.js (v1.0.0.1)

var nActiveRegionID = 0;
var arRollover = new Array();
var arSelected = new Array();
var arRegions = new Array(); // for sorted Regions-only array
var arCountries = new Array();	// for sorted Countries-only array
var showAll = document.getElementById('ShowAll');
var selCountryDiv = document.getElementById('selCountryDiv');
var selCountry;
var showAllText = "";
var tooltip = document.getElementById("tooltipdiv");

if (tooltip != null)
{
	tooltip.style.display = "none";				
}

if (showAll != null)
{
	showAllText = showAll.innerHTML;
}

if(selCountryDiv != null)
{
	selCountry  = selCountryDiv.getElementsByTagName('select')[0];
}

function makeSortableArrays()
{
	var nCountry = 0;
	for (var x = 1; x <= arSubs.length; x++) 
	{
		// Put the Region into an array
		if (arSubs[x]) 
		{
			arRegions[x] = [arSubs[x][0][0],x];
			for (var y = 0; y < arSubs[x].length; y++) 
			{
                //Only Countries, not Regions
				if (y >= 1)
					{
					arCountries[nCountry] = [arSubs[x][y][0], arSubs[x][y][1]];
					nCountry++;
					}
			}
		}
	}
	arRegions = arRegions.sort(sortArray);
	arCountries = arCountries.sort(sortCountriesArray);
	
}

function dictRegions(regionName)
{
	for (y = 0; y < arSubs.length; y++)
	 {
		if (regionName == arSubs[y][0][1])
		{
			return y;
		}
	 }
}

function sortArray(a1, a2)
{
	return 	a1[0] < a2[0] ? -1 :
			a1[0] > a2[0] ? 1 : 0;
}

function sortCountriesArray(a1, a2)
{
    return a1[1] == 'WW' ? -1 :
            a2[1] == 'WW' ? 1 :
            a1[0] < a2[0] ? -1 :
			a1[0] > a2[0] ? 1 : 0;
}

function loadRegionalImages(x) 
{				    				    				    
	arSelected[x] = new Image(415,227);
	arSelected[x].src = '/oem/PublishingImages/Map/'+arSubs[x][0][1]+'_selected_.gif';
	arRollover[x] = new Array();
	arRollover[x][0] = new Image(415,227);
	arRollover[x][0].src = '/oem/PublishingImages/Map/'+arSubs[x][0][1]+'_rollover_.gif';
	for (y = 0; y < arSubs.length; y++)
	 {
		if (x != y) 
		{ // make sure we're not creating ww_select_ww_roll or similar redundencies
			if (arSubs[x][0][1] != 'cac') 
			{ // don't preload for Central America / Carribean
				arRollover[x][y] = new Image(415,227);
				arRollover[x][y].src = '/oem/PublishingImages/Map/'+arSubs[x][0][1]+'_select_'+arSubs[y][0][1]+'_roll_.gif';
			}
		}
	}
}		

function ShowToolTip(object,RegionAbbr)
{
	/*
	var nMORegionID = dictRegions(RegionAbbr);
	if (nMORegionID!=0) 
	{
	   tooltip.style.position = "absolute";
	   //tooltip.style.top = event.clientY + document.body.scrollTop - 20;
	   //tooltip.style.left = event.clientX + document.body.scrollLeft + 10;
	   tooltip.style.top = event.clientY + document.body.scrollTop;
	   tooltip.style.left = event.clientX + document.body.scrollLeft;
	   tooltip.innerHTML = arSubs[nMORegionID][0][0],-1;
	   tooltip.style.display = "block"; 
	   tooltip.style.index = 1;
	}
	*/
}

function Swapin(RegionAbbr)
{
	if (document.images) 
	{
		var nMORegionID = dictRegions(RegionAbbr);
		if (nMORegionID != nActiveRegionID) 
		{
			document.getElementById('wwimage').src = arRollover[nActiveRegionID][nMORegionID ].src;
		}
	}
}

function Swapout()
{
	if (document.images) 
	{
		document.getElementById('wwimage').src = arSelected[nActiveRegionID].src;
		tooltip.style.display = "none";						
	}
}				

function goRegion(RegionAbbr) 
{
	var oSelTitle = document.getElementById('selCountryTitle');
	//var oSel = document.getElementById('selCountryID');
	var oSel = selCountry;
	nActiveRegionID = dictRegions(RegionAbbr);
	oSel.innerHTML = '';
	loadRegionalImages(nActiveRegionID);
	document.getElementById('wwimage').src = arSelected[nActiveRegionID].src;
	if (nActiveRegionID==0) 
	{
		oSelTitle.innerHTML = sSelDefault;
		oSelTitle.style.color = "#333333";
		if (showAll != null)
        {
			showAll.innerHTML = "";
			//Fix. default highlight the "World Wide Partner Portal Home page"
			oSel.focus();
		}
		for (var s = 0; s < arCountries.length; s++) 
		{
			if (s == 0)
			{
                // If Worldwide not defined in the Countries option array and missing in first position
                if (arCountries[s][1].toLowerCase() != "ww")
                {
                    // Add hardcoded Worldwide entry
		            addOption(arWW[0],arWW[1]);
                }
                addOption(arCountries[s][0], arCountries[s][1]); //K2
			}
			else
			{

			   if (arCountries[s][0] != arCountries[s-1][0])
				{
					addOption(arCountries[s][0],arCountries[s][1]); //K2
				}
			}							
		}
	} 
	else 
	{
		addCountries(nActiveRegionID);

		// display title when selected
		oSelTitle.innerHTML = arSubs[nActiveRegionID][0][0],-1;
		if (showAll != null)
        {
			showAll.innerHTML = showAllText;
		}
		oSelTitle.style.color = "#F87311";			
	}

    if (RegionAbbr.toLowerCase() != 'ww')
	{
		//remove the worldwide option when a region is selected on a map
		var elms = document.getElementsByTagName('select');
				
		var elm = elms[0];
		
		if (elm.value.toLowerCase() == 'worldwide')
		{		
			elm.remove(0);
		}
	}
	
	oSel.selectedIndex = 0;
}

function addRegions(exclude1, exclude2) 
{
	//var oSel = document.getElementById('selCountryID');
	var oSel = selCountry;
	for (var r = 0; r < arRegions.length; r++)
{
		if (arRegions[r]) 
		{
			if (arRegions[r][1] != exclude1 && arRegions[r][1] != exclude2) 
			{
				addOption(arRegions[r][0],arRegions[r][1]);
			}
		}
	}	
}

function addCountries(RegionID) 
{
	for (var c = 1;c < arSubs[RegionID].length; c++) 
	{
		addOption(arSubs[RegionID][c][0],arSubs[RegionID][c][1]);
	}
}

function addOption(name, value) 
{
	//var oSel = document.getElementById('selCountryID');
	var oSel = selCountry;
	if (name == '' && value == 0) 
	{
		oSel.innerHTML = '';
	} 
	else 
	{
		oSel.options[oSel.options.length] = new Option(name, value);
	}
}

function goToSelected() 
{
	//var sel = document.getElementById('selCountryID');
	var sel = selCountry;
	c = sel.options[sel.options.selectedIndex].value;
	document.getElementById('siteID') = c;
	
	if (isNaN(c)) 
	{ 	// it's a URL
		document.location = sel.options[sel.options.selectedIndex].value;
	} 
	else 
	{ 	// it could be a ResourceID (> 100) or a RegionID (< 100)
		// this isn't scalable past 100 Regions ;-)
		if (c < 100) 
		{
			if (c > -1) goRegion(arSubs[c][0][1]);
			return;
		} 
		else 
		{	// if it's more than 100, then it's a ResourceID (e.g. 3923300 = WW Partner Home)
			document.location = c;
		}
	}
}

function redirectWithQS()
{
	var selects = document.getElementsByTagName('select');
	
	var countryElm = selects[0];
	var languageElm = selects[1];
	
	var country = countryElm.value;
	var lang = languageElm.value;
	
	document.location.href = '/oem/pages/Preferences.aspx?Country=' + country + '&Language=' + lang;
}

/* Following routines were copied from Webtrends.js and had the names prefixed with map */

function mapCreateCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    if (document.location.href.indexOf('www.microsoft.com') >= 0) {
        document.cookie = name + "=" + value + expires + ";domain=.microsoft.com;path=/";
    }
    else {
        document.cookie = name + "=" + value + expires + ";path=/";
    }

}

function mapReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
}

function mapGetArrayValue(array, idx) {
    var s = new String();
    s = array;
    if (array == undefined) return;
    //var arr = s.split(',');
    var arr = s.split('&');
    //alert('&');
    if (arr.length > idx) {
        var iarr = arr[idx].split('=');
        if (iarr.length > 1) {
            return iarr[1];
        }
    }
}

function mapSetDefaultLanguage(val) {
    var lng = '';

    for (var i = 0; i < arDefLang.length; i++) {
        //alert(arDefLang[i]);
        var arCtryCode = arDefLang[i].split('=');
        if (arCtryCode[0].toLowerCase() == val.toLowerCase()) {
            lng = arCtryCode[1].toLowerCase();
            break;
        }
    }

    //var elm = document.getElementById('ctl00_SPWebPartManager1_g_63aff401_ba43_47ef_a970_5019490bfd5e_selLanguageId');
    var elms = document.getElementsByTagName('select');
    var elm = elms[1];
    //alert(elm.options.length);
    for (var j = 0; j < elm.options.length; j++) {
        //alert(elm.options[j].value);
        if (elm.options[j].value.toLowerCase() == lng) {
            elm.selectedIndex = j;
            break;
        }
    }
}

/* OnLoad event to sort the arRegions and arCountries arrays and load all countries */

// Changed page name for index so name extensions could be added for test pages
if (document.location.href.toLowerCase().indexOf('/pages/preferences') >= 0) {
    try {
        //alert(eval(arSubs));
        if (arSubs) {
            // sort arrays for alphabetical listing of all countries
            makeSortableArrays();
            // on page load, load all countries (ww)
            goRegion('ww');
            /*var elms = document.getElementsByTagName('select');
			
            var elm = elms[1];
			
            elm.remove(0);*/

        }
    }
    catch (e) { }
}

