function countryWarp(sPrefix, sId) {
    if (document.getElementById) {
	oSelect = document.getElementById(sId);
	sLocation = oSelect.options[oSelect.selectedIndex].value ;
	if (document.getElementById("countryModes") ) {
	    if ((  document.getElementById("countryModes").value == "absolute" &&
		   oSelect.parentNode.parentNode.getAttribute('id') != 'regional-sites')
		|| sLocation.substring(0,7) == "http://") {
		// it's an absolute URL
		window.location.href = sLocation; 
		return false;
	    } else {
		// it's relative
		window.location.href = sPrefix + sLocation;
		return false;
	    }
	} else {
	    if (sLocation.substring(0,7) != "http://") {
		// If no http:// prefix then assume it's an Education site with URL relative to /.
		window.location.href = sPrefix + sLocation;
		return false;
	    } else {
		// else it's an absolute URL
		window.location.href = sLocation; 
		return false;
	    }
	}
    }
    return false;
}

function countrySelector(sPrefix, sId) {
    /* Gets the country code from the URL and sets the drop-down menu, or defaults to international. */
    sLocation = document.location.href;
    iIndex = sLocation.indexOf(sPrefix) + sPrefix.length;
    sLocation = sLocation.substring(iIndex + 1);
    iIndex = sLocation.indexOf("/");
    sCountry = "/" + sLocation.substring(0,iIndex+1);
    oSelect = document.getElementById(sId);

    /* Default choice for Mac is different because of the flat menu structure. */
    if (  (navigator.appName == "Microsoft Internet Explorer") &&
	  (navigator.appVersion.indexOf('MSIE 5') != -1) ) {
	iPleaseSelect = 1;
    } else {
	iPleaseSelect = 0;
    }

    for(i=0; i < oSelect.options.length; i++) {
	if (oSelect.options[i].value == sCountry) {
	    iPleaseSelect = i; 
	}
    }

    oSelect.selectedIndex = iPleaseSelect;

    return false;
}

function countryMenu(sPrefix) {
    /* This information is repeated in class.elt-page.inc/page->countries1_asTable(); */
    /* IE5/Mac Breaks the formatting if we use OPTGROUP so check for this and provide a flat 
     * menu instead of the hierarchical one. */
    if ( ((navigator.appName == "Microsoft Internet Explorer") &&
	  (navigator.appVersion.indexOf('MSIE 5') != -1) )) {
		

	sHTML  = "<form method=\"post\" action=\"#\">\n";
	sHTML += " <select id=\"countries1\" onChange=\"countryWarp('"+sPrefix+"', 'countries1')\">\n";
	/* URLs without http:// are interpreted by countryWarp() as relative to its first arg, else absolute. */

	sHTML += "  <option value=\"#\">------- Regional sites -------</option>\n";
	sHTML += "  <option value=\"/uk/\">UK/Europe/Middle East</option>\n";
	sHTML += "  <option value=\"/africa/education/\">Africa and the Caribbean</option>\n";
	sHTML += "  <option value=\"http://www.cambridge.edu.au/Education/\">Australia and New Zealand</option>\n";
	sHTML += "  <option value=\"/us/education/\">Americas</option>\n";

    } else {

	sHTML  = "<form method=\"post\" action=\"#\">\n";
	sHTML += " <select id=\"countries1\" onChange=\"countryWarp('"+sPrefix+"', 'countries1')\">\n";
	/* URLs without http:// are interpreted by countryWarp() as relative to its first arg, else absolute. */

	<!--sHTML += "  <optgroup class=\"heading\" label=\"Cambridge Education sites\">\n";-->
	sHTML += "  <option value=\"#\">------- Regional sites -------</option>\n";	sHTML += "  <option value=\"http://www.cambridge.org/uk/education/\">UK/Europe/Middle East</option>\n";
	sHTML += "  <option value=\"http://www.cambridge.org/africa/education/\">Africa and the Caribbean</option>\n";
	sHTML += "  <option value=\"http://www.cambridge.edu.au/Education/\">Australia and New Zealand</option>\n";
	sHTML += "  <option value=\"http://www.cambridge.org/us/education/\">Americas</option>\n";

	sHTML += "  </optgroup>\n";
	
	sHTML += " </select>\n";
	sHTML += "</form>\n";
    }
    document.write(sHTML);
    return false;
}
