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

/*global $ Option document Querystring OTHER_DISTRIBUTOR  */

/**
 * This function is called via the change event of the reseller dropdown.
 * It is also called initially when a new distributor list is loaded.
 * This function will copy the referrer code associated with the reseller to the
 * referrer code form field.
 */
function applyDistributor() {
	var distributorsBox = $("#distributor_choice").get(0);
	var referrerCodeEdit = $("#referrer_code").get(0);
	referrerCodeEdit.value = distributorsBox.options[distributorsBox.selectedIndex].value;
}

/**
 * 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) {
	var distributorsBox = $("#distributor_choice").get(0);
	var referrerCodeEdit = $("#referrer_code").get(0);	
	// handle empty list in reply
	if ((distributors === undefined) || (distributors.length === 0)) {
		// hide the distributor box and show the referrer code box,
		$("#distributor_choice_div").hide();
		$("#referrer_code_div").show();
		distributorsBox.value = 0;
		referrerCodeEdit.value = '';
	} else {
		// a single distributor for a country
		if (distributors.length == 1) {
			// don't show distributor list
			$("#referrer_code_div").hide();
			$("#distributor_choice_div").hide();
		} else {
			// for >1 distributor, show distributor list
			$("#distributor_choice_div").show();
			$("#referrer_code_div").hide();
		}	
		// remove all previous dropdown box options
		distributorsBox.options.length = 0;
		// fill with options from the distributor list
		for ( var distributorIndex = 0; distributorIndex < distributors.length; distributorIndex++ ) {
			distributorsBox.options[distributorIndex] = new Option(
					distributors[distributorIndex].distributor,	// label
					distributors[distributorIndex].code);			// value
		}
		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;
			distributorsBox.options[distributorsBox.selectedIndex].text = OTHER_DISTRIBUTOR;
		}
		applyDistributor();
	}
}

/**
 * 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.
 */
function refreshDistributorList() {
	var countryBox = $("#country").get(0);
	var countryCode = countryBox.options[countryBox.selectedIndex].value;
	// clear distributor selection
	var distributorsBox = $("#distributor_choice").get(0);
	var referrerCodeEdit = $("#referrer_code").get(0);
	distributorsBox.value = 0;
	referrerCodeEdit.value = '';
	// remove all previous dropdown box options
	distributorsBox.options.length = 0;
	// determine country and language code
	var languageChoice = $("#language_choice").get(0);
	var languageCode = languageChoice.value;
	// 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) {
					showDistributorList(json);
				});
}

$(document).ready(function() {
	// fetch new list when language was changed
	$("#language_choice").change(function() {
		refreshDistributorList();
	});
	// apply when distributor was selected
	$("#distributor_choice").change(function() {
		applyDistributor();
	});
});

