/**
#	Geodesic Solutions, LLC  2008
**/

var parent = [];
var countries = [];
var regionNames = [];

Event.observe(window, 'load', function () {
	initRegions();
});

function initRegions()
{
	for (var i = 0; i < regionNames.length; i++) {
		var regionElemName = regionNames[i][0];
		var subRegionElemName = regionNames[i][1];
		var country_dd = $(regionElemName);
		var state_dd = $(subRegionElemName);
		if (country_dd) {
			country_dd.observe('change', function (event) {
				var elem = Event.element(event);
				chooseCountry(elem);
			});
			
			if ( state_dd && country_dd.selectedIndex > 0 ) {
				var selected = state_dd.options[state_dd.selectedIndex].value;
				chooseCountry( country_dd, subRegionElemName, selected );
			} else {
				state_dd.disabled = true;
			}
		}
	}
}

function addCountry ( country_id, country_index )
{
	countries[country_index] = country_id;
}

function addRegion( parent_id, value, text )
{
	var new_array = new Array();

	if (parent[parent_id] == undefined) {
		parent[parent_id] = new Array();
	}
	new_array[0] = value;
	new_array[1] = text;
	parent[parent_id].push( new_array );
}

function populateState(state_element_id, parent_id, selected )
{
	parent_id = htmlentities(parent_id);
	//alert( parent_id + ' - '+state_element_id );
	dd = $(state_element_id);
	dd.update( new Element('option',{'value': 'none'}).update(selectAStateText) );
	
	for( var x=0; parent[parent_id] && x < parent[parent_id].length; x++ ) {
		//insert option for state.
		dd.insert(new Element('option',{'value': parent[parent_id][x][0]}).update(parent[parent_id][x][1]));
	}
	if (selected) {
		//select the state
		dd.value = selected;
	}
	dd.disabled = false;
}
function chooseCountry(country_id, next_dd, selected )
{
	if (next_dd) {
		var states = $(next_dd);
	} else {
		var states = null;
		for (var i = 0; i < regionNames.length && !states; i++) {
			if (regionNames[i][0] == country_id.identify()) {
				next_dd = regionNames[i][1];
				states = $(next_dd);
			}
		}
		if (!states) {
			//error getting states field.
			return;
		}
	}
	if (country_id.getValue() != 'none') {
		states.disabled = false;
		populateState(next_dd, country_id.getValue(), selected );
	} else {
		states.disabled = true
	}
}

/**
 * implements PHP's htmlentities() function in JavaScript
 * requires get_html_translation_table() below
 * 
 * Adapted from an original function by Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 */
gotTable = false;
function htmlentities (string) {
 
    var symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();

    
    //the table's kinda big...only want to get it once
	if(!gotTable) {
		if (false === (translation_table = get_html_translation_table())) {
			return false;
		}
		gotTable = true;
	}
   
    for (symbol in translation_table) {
        entity = translation_table[symbol];
        tmp_str = tmp_str.split(symbol).join(entity);
    }
    
    return tmp_str;
}

/**
 * translation table used by htmlentities() above
 * 
 * Adapted from an original function by Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 */
function get_html_translation_table(table, quote_style) {
    
    var entities = {}, histogram = {}, decimal = 0, symbol = '';
      
      // ascii decimals for better compatibility
      entities['38']  = '&amp;';
      entities['60']  = '&lt;';
      entities['62']  = '&gt;';
      entities['160'] = '&nbsp;';
      entities['161'] = '&iexcl;';
      entities['162'] = '&cent;';
      entities['163'] = '&pound;';
      entities['164'] = '&curren;';
      entities['165'] = '&yen;';
      entities['166'] = '&brvbar;';
      entities['167'] = '&sect;';
      entities['168'] = '&uml;';
      entities['169'] = '&copy;';
      entities['170'] = '&ordf;';
      entities['171'] = '&laquo;';
      entities['172'] = '&not;';
      entities['173'] = '&shy;';
      entities['174'] = '&reg;';
      entities['175'] = '&macr;';
      entities['176'] = '&deg;';
      entities['177'] = '&plusmn;';
      entities['178'] = '&sup2;';
      entities['179'] = '&sup3;';
      entities['180'] = '&acute;';
      entities['181'] = '&micro;';
      entities['182'] = '&para;';
      entities['183'] = '&middot;';
      entities['184'] = '&cedil;';
      entities['185'] = '&sup1;';
      entities['186'] = '&ordm;';
      entities['187'] = '&raquo;';
      entities['188'] = '&frac14;';
      entities['189'] = '&frac12;';
      entities['190'] = '&frac34;';
      entities['191'] = '&iquest;';
      entities['192'] = '&Agrave;';
      entities['193'] = '&Aacute;';
      entities['194'] = '&Acirc;';
      entities['195'] = '&Atilde;';
      entities['196'] = '&Auml;';
      entities['197'] = '&Aring;';
      entities['198'] = '&AElig;';
      entities['199'] = '&Ccedil;';
      entities['200'] = '&Egrave;';
      entities['201'] = '&Eacute;';
      entities['202'] = '&Ecirc;';
      entities['203'] = '&Euml;';
      entities['204'] = '&Igrave;';
      entities['205'] = '&Iacute;';
      entities['206'] = '&Icirc;';
      entities['207'] = '&Iuml;';
      entities['208'] = '&ETH;';
      entities['209'] = '&Ntilde;';
      entities['210'] = '&Ograve;';
      entities['211'] = '&Oacute;';
      entities['212'] = '&Ocirc;';
      entities['213'] = '&Otilde;';
      entities['214'] = '&Ouml;';
      entities['215'] = '&times;';
      entities['216'] = '&Oslash;';
      entities['217'] = '&Ugrave;';
      entities['218'] = '&Uacute;';
      entities['219'] = '&Ucirc;';
      entities['220'] = '&Uuml;';
      entities['221'] = '&Yacute;';
      entities['222'] = '&THORN;';
      entities['223'] = '&szlig;';
      entities['224'] = '&agrave;';
      entities['225'] = '&aacute;';
      entities['226'] = '&acirc;';
      entities['227'] = '&atilde;';
      entities['228'] = '&auml;';
      entities['229'] = '&aring;';
      entities['230'] = '&aelig;';
      entities['231'] = '&ccedil;';
      entities['232'] = '&egrave;';
      entities['233'] = '&eacute;';
      entities['234'] = '&ecirc;';
      entities['235'] = '&euml;';
      entities['236'] = '&igrave;';
      entities['237'] = '&iacute;';
      entities['238'] = '&icirc;';
      entities['239'] = '&iuml;';
      entities['240'] = '&eth;';
      entities['241'] = '&ntilde;';
      entities['242'] = '&ograve;';
      entities['243'] = '&oacute;';
      entities['244'] = '&ocirc;';
      entities['245'] = '&otilde;';
      entities['246'] = '&ouml;';
      entities['247'] = '&divide;';
      entities['248'] = '&oslash;';
      entities['249'] = '&ugrave;';
      entities['250'] = '&uacute;';
      entities['251'] = '&ucirc;';
      entities['252'] = '&uuml;';
      entities['253'] = '&yacute;';
      entities['254'] = '&thorn;';
      entities['255'] = '&yuml;';
        
    // ascii decimals to real symbols
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal)
        histogram[symbol] = entities[decimal];
    }
    
    return histogram;
}


