/* 
	Dynamic Officeselector
	
	made by Axxius
	last modified:
	    20030509 by Jeroen Zomer  - added posibility for HQs and 'Central Country pages'

	NOTE: add offices at the end of this file!

	USAGE:
	
	add the following line to your headsection: (mind the path!)
	<script type="text/javascript" src="officeselector.js"></script>

	build an officeselector in the following way: (again: mind the paths and filenames)
	
	<div class="contentBody">
	<!-- officeSelector -->
	<form name="officeSelectorForm" action="../content/offices/index.html" method="get">
		<select name="officeSelector" onchange="selectOffice()" class="flat">
			<option value="" selected="selected">select an office</option>
		</select>
		<script type="text/JavaScript">buildOfficeSelector('../offices/');</script>
		<noscript>
			<input type="submit" name="goButton" value="Go" />
			<a href="/content/offices/index.html">select from list</a>
  		</noscript>
	</form>
	<!-- /officeSelector -->
	</div>
	
	the function buildOfficeSelector() has an option path to the directory of files
*/
var nothing = 0;
var isHQ = 1;
var hideItem = 2;
function selectOffice() { // actionHandler
	var sel = document.officeSelectorForm.officeSelector;
	var uri = sel.options[ sel.selectedIndex ].value;
	if (uri.indexOf('.htm') < 0) return false; // skip hint
	document.location.href = uri;
	return true;
}

// helper functions 
function cityOrder( a, b) {	return (a.city.toUpperCase() < b.city.toUpperCase() )? -1 : 1; }
function countryOrder( a, b) { return (a.country.toUpperCase() < b.country.toUpperCase() )? -1 : 1; }
function isNN4() {
	if ( (navigator.appName == "Netscape" ) && ( parseInt( navigator.appVersion ) == 4 ) ) { return true; }
	else { return false; }
}

function buildOfficeSelector( pathToOffices ) { // build optionlist, pathToOffices is optional
	var i, j, c;
	var sel = document.officeSelectorForm.officeSelector;
	var nbsp = ' - ';
	
	if (pathToOffices == null) {
		pathToOffices = '' ;
	} 
	else if (pathToOffices.lastIndexOf('/') != (pathToOffices.length - 1) ) {
		pathToOffices += '/';
	}

	if ( !isNN4() ) { // NN crashes on advanced sorting
		officeList.sort( cityOrder ); 
		countryList.sort( countryOrder );
	}
	
	for (i = 0; i < countryList.length; i++ ) {
	
		sel.options[sel.options.length] = 
			new Option( countryList[i].country, pathToOffices + countryList[i].file, false, false);
			
		var c = countryList[i].country;
		
		for (j = 0; j < officeList.length; j++ ) {
			if ( officeList[j].country == c ) {
			
				sel.options[sel.options.length] = 
						new Option( nbsp+officeList[j].city, pathToOffices + officeList[j].file, false, false);
				}
		}
	}
}


var officeList = new Array();
var countryList = new Array();
function addoffice(o,c,h,p){addOffice(o,c,h,p);} // prevent typos
function addOffice( office, country, htmlfile, options ) {
    
    var hide = false;
    var HQ = false;   
    
    if (options) {
        hide = (options & hideItem ) ? true : false;
        HQ = (options & isHQ ) ? true : false;
    }
    
    if (!hide) {
    	var i = officeList.length;
    	officeList[i] = new Array();
    	officeList[i].city = office;
    	officeList[i].country = country.toUpperCase();
    	officeList[i].file = htmlfile;
    }
    
	var cLength = countryList.length
	for (var j = 0; j < cLength; j++) {
		if (countryList[j].country.toUpperCase() == country.toUpperCase() ) {
		    if (HQ) { // overwrite countries destination.
		        countryList[ j ].file = htmlfile;   
		    }
		    return true;
		}
	}
	// we now have a new country:
	countryList[ cLength ] = new Array();
	countryList[ cLength ].country = country.toUpperCase();
	countryList[ cLength ].file = htmlfile;
	return true;
}

// list of offices
// please make sure this list is in alphabetical order on the first item!!!
//
// syntax: addOffice( 'proper name', 'country', 'HTML-file', options);
//
// where options is one of the following : 'isHQ' or 'hideItem'
// if multiple options are required, just 'add them up', like this:  isHQ + hideItem.
//
// All functions and parameters are case sensitive!

addOffice( 'Amsterdam', 'Netherlands', 'amsterdam.html'); 
addOffice( 'Antwerp', 'Belgium', 'antwerp.html');
addOffice( 'Atlanta', 'United States', 'atlanta.html'); 
addOffice( 'Auckland', 'New Zealand', 'new_zealand.html'); 
addOffice( 'Bangkok', 'Thailand', 'bangkok.html'); 
addOffice( 'Beijing', 'China', 'beijing.html', isHQ); 
addOffice( 'Buenos Aires', 'Argentina', 'buenos_aires.html'); 
addOffice( 'Chicago', 'United States', 'chicago.html');
addOffice( 'Curacao', 'Curacao', 'curacao.html'); 
addOffice( 'Dallas', 'United States', 'dallas.html'); 
addOffice( 'Dublin', 'Ireland', 'dublin.html'); 
addOffice( 'Edinburgh', 'United Kingdom', 'edinburgh.html'); 
addOffice( 'Frankfurt', 'Germany', 'frankfurt.html'); 
addOffice( 'Hong Kong', 'Hong Kong', 'hongkong.html'); 
addOffice( 'Istanbul', 'Turkey', 'istanbul.html'); 
addOffice( 'Jakarta', 'Indonesia', 'jakarta.html'); 
addOffice( 'London', 'United Kingdom', 'london.html', isHQ); 
addOffice( 'Madrid', 'Spain', 'madrid.html'); 
addOffice( 'Mexico City', 'Mexico', 'mexico.html'); 
addOffice( 'Milan', 'Italy', 'milan.html'); 
addOffice( 'Moscow', 'Russia', 'moscow.html');
addOffice( 'Mumbai', 'India', 'india.html', isHQ); 
addOffice( 'New Delhi', 'India', 'india.html');
addOffice( 'New York', 'United States', 'new_york.html', isHQ); 
addOffice( 'Paris', 'France', 'paris.html'); 
addOffice( 'Warsaw', 'Poland', 'polska.html'); 
addOffice( 'San Francisco', 'United States', 'san_francisco.html'); 
addOffice( 'Santiago de Chile', 'Chile', 'santiago_de_chile.html'); 
addOffice( 'Sao Paulo', 'Brazil', 'sao_paulo.html'); 
addOffice( 'Shanghai', 'China', 'shanghai.html'); 
addOffice( 'Singapore', 'Singapore', 'singapore.html'); 
addOffice( 'Sydney', 'Australia', 'sydney.html'); 
addOffice( 'Taipei', 'Taiwan', 'taipei.html'); 
addOffice( 'Tokyo', 'Japan', 'tokyo.html'); 
addOffice( 'Toronto', 'Canada', 'toronto.html'); 
addOffice( 'Utrecht', 'Netherlands', 'utrecht.html', isHQ); 
addOffice( 'Washington DC', 'United States', 'washington_dc.html');

// example:
// addOffice( 'US Country Office', 'United States', 'unitedstates.html', isHQ+hideItem );


