if (IXYS == undefined) var IXYS = {};

IXYS.Reps = {
	usCountryId: "210",
	$countries: undefined,
	$states: undefined,
	$divisions: undefined,

	init: function() {
		this.$countries = $('#ddlCountries');
		this.$states = $('#ddlUSStates');
		this.$divisions = $('#ddlDivisions');

		this.bindControls();

		var countryId = this.$countries.val(),
			usStateId = this.$states.val(),
			divisionId = this.$divisions.val();

		if (countryId != -1) { // the page was submitted, need to reset the controls
			this.$countries.change();
			if (usStateId != -1) this.$states.val(usStateId).change();
			if (divisionId != -1) this.$divisions.val(divisionId);
		}
	},

	bindControls: function() {
		var Self = this;

		Self.$countries.change(function() {
			var $this = $(this),
						arId = $this.val().split('|'), // id pattern is 123|1_2_3, country id + (optional) | + _ delimited lists of divisionIds
						countryId = arId[0],
						divisionIds = (arId.length > 1) ? arId[1].split('_') : [];

			if (countryId == Self.usCountryId) {
				Self.$states.show();

				Self.$divisions.hide();
				Self.$divisions[0].selectedIndex = 0;
			} else {
				Self.$states.hide();
				Self.$states[0].selectedIndex = 0;

				if (divisionIds.length == 0) {
					Self.$divisions.hide();
					Self.$divisions[0].selectedIndex = 0;
				}
				else {
					Self.prepareDivisions(divisionIds);
					Self.$divisions[0].selectedIndex = 0;
					Self.$divisions.show();
				}
			}
		});

		Self.$states.change(function() {
			var $this = $(this),
						arId = $this.val().split('|'), // id pattern is same as country id (above), but state id is a string: WA|1_2_3
						divisionIds = (arId.length > 1) ? arId[1].split('_') : [];

			if (divisionIds.length == 0) {
				Self.$divisions.hide();
				Self.$divisions[0].selectedIndex = 0;
			}
			else {
				Self.prepareDivisions(divisionIds);
				Self.$divisions[0].selectedIndex = 0;
				Self.$divisions.show();
			}
		});
	},

	prepareDivisions: function(ids) {
		var option, found;

		this.$divisions.find('option:not(:first)').each(function() {
			$option = $(this);
			found = false;
			for (var i = 0; i < ids.length; i++) {
				if (ids[i] == $option.val()) {
					$option.show();
					found = true;
					break;
				}
			}
			if (!found) $option.hide();
		});
	},

	submit: function() {
		var Self = this,
					countryId = Self.$countries.val(),
					stateId = Self.$states.val(),
					divisionId = Self.$divisions.val();

		if (countryId == -1) {
			alert("Please select a country");
			return false;
		}

		if (countryId == Self.usCountryId) {
			if (stateId == -1) {
				alert("Please select a state");
				return false;
			}
		}

		if ($('#ddlDivisions:visible').length && divisionId == -1) {
			alert("Please select a division");
			return false;
		}

		return true;
	},

	openBusinessCard: function(companyID) {
		var win = window.open("RepBusinessCard.aspx?cid=" + companyID, "winBizCard", "top=50,screenY=50,left=50,screenX=50,width=400,height=280,resizable=true,location=false,menubar=false,status=false,toolbar=false,scrollbars=true");
		win.focus();
	}
}

