function getDefaultCountries(lang) {
	switch(lang) {
		case "en-us" :
			return new Array("United States","United Kingdom","Canada");
		case "de-de" :
			return new Array("Germany","Austria","Switzerland");
		case "es-es" :
			return new Array("Argentina","Colombia","Mexico","Spain","Venezuela");
		case "fr-fr" :
			return new Array("France", "Canada");
		case "it-it" :
			return new Array("Italy");
		case "nl-nl" :
			return new Array("Netherlands");
		case "cs-cz" :
			return new Array("Czech Republic");
		case "zh-cn" :
			return new Array("China","Hong Kong","Malaysia","Singapore","Taiwan, Republic of China");
		case "zh-tw" :
			return new Array("China","Hong Kong","Malaysia","Singapore","Taiwan, Republic of China");
		case "ja-jp" :
			return new Array("Japan");
		case "hu-hu" :
			return new Array("Hungary");
		case "pl-pl" :
			return new Array("Poland");
		case "pt-br" :
			return new Array("Brazil","Portugal");
		case "ru-ru" :
			return new Array("Russia");
		case "sv-se" :
			return new Array("Sweden");
		case "da-dk" :
			return new Array("Denmark");
		case "fi-fi" :
			return new Array("Finland");
		case "no-no" :
			return new Array("Norway");
			break;
	}
	return new Array();
}

// Taken from /common/util/langselect.php
/*function addDefaultsToCountry_original(lang) {
	if(!lang) lang = "en-us";
	var frm = document.getElementById("langselfrm");
	var defaults = getDefaultCountries(lang);
	
	if(defaults.length > 0) {
		var currentSelection = frm.country.selectedIndex;
		var optgrp = document.createElement("optgroup");
		optgrp.label = " ";
		optgrp.id = "defaultcountries";
		for(var i = defaults.length-1; i >= 0; i--) {
			var defaultOption = document.createElement("option");
			defaultOption.value = defaults[i][0];
			defaultOption.innerHTML = defaults[i][1];
			optgrp.insertBefore(defaultOption,optgrp.firstChild);
		}
		var blankOption = document.createElement("option");
		blankOption.value = "";
		blankOption.text = "";
		optgrp.insertBefore(blankOption,optgrp.firstChild);

		frm.country.insertBefore(optgrp,frm.country.firstChild);
		if(currentSelection == 0) frm.country.selectedIndex = 0;
		hasDefaultCountries = true;
	}
}*/

function addDefaultsToCountry(lang,qc) {
	if(!lang) lang = "en-us";
	
	var defaults = getDefaultCountries(lang);
	
	// Create an optgroup
	var optgrp = document.createElement("optgroup");
	optgrp.label = " ";
	optgrp.id = "defaultcountries";
	optgrp.style.paddingTop = '10px';
	
	// Loop through all the countries, pulling the default ones into the optgroup
	for(var i = sfrm[qc].options.length-1; i >= 0; i--) {
		if (in_array(sfrm[qc].options[i].value.split("||")[1], defaults)) {
			var optn = sfrm[qc].options[i].cloneNode(true);
			// (commented out the next two lines to leave it in the All Countries list too)
			//sfrm[qc].getElementsByTagName('optgroup')[0].removeChild(sfrm[qc].options[i]);
			//i--;
			optgrp.insertBefore(optn,optgrp.firstChild);
		}
	}
	// Put a blank option at the bottom of the optgroup for spacing
	var blankOption = document.createElement("option");
	blankOption.value = "";
	blankOption.text = "";
	optgrp.appendChild(blankOption);
	
	// Store the Choose a Country option for future use
	var choose_a_country = sfrm[qc].options[0].cloneNode(true);
	sfrm[qc].removeChild(sfrm[qc].options[0]);
	
	// Append the optgroup to the beginning of the country dropdown
	sfrm[qc].insertBefore(optgrp,sfrm[qc].firstChild);
	
	// Append the Choose a Country option before that
	sfrm[qc].insertBefore(choose_a_country,sfrm[qc].firstChild);
	
	// Also select it (otherwise IE6 selects the first one in the All Countries list by default)
	sfrm[qc].options[0].selected = true;
}

function addAllCountriesHeading(qc) {
	// Wraps the entire country list in an optgroup
	var optgrp = document.createElement("optgroup");
	optgrp.label = "All Countries";
	optgrp.id = "allcountries";
	while(sfrm[qc].options.length > 1) {
		var optn = sfrm[qc].options[1].cloneNode(true);
		sfrm[qc].removeChild(sfrm[qc].options[1]);
		optgrp.appendChild(optn);
	}
	sfrm[qc].appendChild(optgrp);
}

function in_array(needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 905.3120
    // discuss at: http://phpjs.org/functions/in_array
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}