/**
 * distributors.js - handles distributor assignments over all territories
 */

/**
 * kick off fetching of the distributor lists when the DOM is loaded
 */
$(document).ready(function() {
	// changeLanguage(document.forms.gfimaxform.country);
	// loads the list from the webservice
	refreshDistributorList();
});

/**
 * This function will update the list of available distributors for the
 * selected country and language. The showDistributorsList() will
 * handle the display one the query is completed.
 *
 * This function is called via:
 * * onchange on the country selection box in the signup forms
 * * onchange on the language selection box in the signup forms
 * * when the DOM is first loaded, see above
 */
function refreshDistributorList() {
	var countryBox = document.forms.gfimaxform.country;
	var countryCode = countryBox.options[countryBox.selectedIndex].value;
	
	// clear distributor selection
	var distributorsBox = document.forms.gfimaxform.distributor_choice;
	var referrerCodeEdit = document.forms.gfimaxform.referrer_code;
	distributorsBox.value = 0;
	referrerCodeEdit.value = '';
	// remove all previous dropdown box options
	distributorsBox.options.length = 0;
	
	// determine country and language code
	var language = document.forms.gfimaxform.language.value;
	var languageCode;
	switch (language) {
		case 'French':
		case 'Fran\u00E7ais':
		case 'Franz\u00F6sisch':
			languageCode = "fr";
			break;
		case 'Allemand':
		case 'German':
		case 'Deutsch':
			languageCode = "de";
			break;
		default:
			languageCode = "en";
			break;
	}
	// compose query URL
	var query = "country="+countryCode+"&language="+languageCode;
	var distributorsURL;
	if (document.location.hostname == 'hounddogiseasy') { 
		// testing
		distributorsURL = "http://hdog/siteforms/distributors.php?"+query+"&jsoncallback=?";
	} else {
		// production
		distributorsURL = "http://wwweurope1.systemmonitor.eu.com/siteforms/distributors.php?"+query+"&jsoncallback=?";
	}	
	// execute query
	$.getJSON(distributorsURL,
				function(json) {
					//distributors = json;
					showDistributorList(json);
				});
}

/**
 * This function is called when the webservice call completes. 
 * 
 * It will show the list of available distributors. The 'other' distributor 
 * will be selected first. For countries without a distributor, the dropdown 
 * box is hidden. 
 */
function showDistributorList(distributors) {
	// retrieve objects from the DOM
	var distributorsBox = document.forms.gfimaxform.distributor_choice;
	var referrerCodeEdit = document.forms.gfimaxform.referrer_code;
	
	// no list
	if ((distributors == undefined) || (distributors.length == 0)) {
		// hide the distributor box and show the referrer code box, 	
		removediv('distributor_choice_div');
		adddiv('referrer_code_div');
		distributorsBox.value = 0;
		referrerCodeEdit.value = '';
	} else {
		// a single distributor for a country
		if (distributors.length == 1) {
			// don't show distributor list
			removediv('referrer_code_div');
			removediv('distributor_choice_div');
		} else {
			// for >1 distributor, show distributor list
			adddiv('distributor_choice_div');
			removediv('referrer_code_div');
		}
	
		// remove all previous dropdown box options
		distributorsBox.options.length = 0;
		// fill with options from the distributor list
		for ( var distributorIndex in distributors) {
			distributorsBox.options[distributorIndex] = new Option(
					distributors[distributorIndex]['distributor'],	// label
					distributors[distributorIndex]['code']);			// value
		}
		// select a distributor
		if (distributors.length > 0) {
			var oQry = new Querystring();
			var referrerCode = oQry.get( 'referrer_code', '');
			if (referrerCode != '') {
				// if this form is recalled after an error, referrer_code is set in the query string
				// in this case re-select the previously selected distributor
				distributorsBox.value = referrerCode;
			} else {
				// otherwise select "other" variant
				// need to set via .selectedIndex, not .value, because referrer codes
				// are not unique
				distributorsBox.selectedIndex = distributors.length - 1;
			}
			applyDistributor();
		}
	}
}

/**
 * This function is called via the onchange function of the reseller dropdown.
 * This function will copy the referrer code associated with the reseller to the
 * referrer code form field.
 */
function applyDistributor() {
	var distributorsBox = document.forms.gfimaxform.distributor_choice;
	var referrerCodeEdit = document.forms.gfimaxform.referrer_code;
	referrerCodeEdit.value = distributorsBox.options[distributorsBox.selectedIndex].value;
}
