/**
 * JavaScript for the sign-up form
 */
 
YAHOO.namespace("cedars.SignUp");


/**
 * Reprsents the sign-up form.
 *
 * Once this object is instantiated, it will set up all the JavaScript event
 * handlers for the sign-up form including the AJAX-based country and
 * elements[this.PROVINCE_ELEMENT] filters.
 */
 
YAHOO.cedars.SignUp.Form = function(id) {
	this.form = document.getElementById(id);
		
	YAHOO.util.Event.addListener(this.form.elements[this.COUNTRY_ELEMENT], 'change',
		function(e, form) {
			form.filterProvinces(this.value);
		},
		this
	);
	
	this.form.elements[this.COUNTRY_ELEMENT].value = this.DEFAULT_COUNTRY;
	this.filterProvinces(this.DEFAULT_COUNTRY);
	
	// IgnitionWeb inserts a blank in all SELECT fields...
	with (this.form.elements[this.LANGUAGE_ELEMENT]) {
		if (options[0].value == '') {
			remove(0);
		}
	}
}

YAHOO.cedars.SignUp.Form.prototype.DEFAULT_COUNTRY = 'CA';
YAHOO.cedars.SignUp.Form.prototype.DEFAULT_PROVINCE = 'QC';
YAHOO.cedars.SignUp.Form.prototype.PROVINCE_ELEMENT = 'profile_data[contact][province]';
YAHOO.cedars.SignUp.Form.prototype.COUNTRY_ELEMENT =  'profile_data[contact][country]';
YAHOO.cedars.SignUp.Form.prototype.LANGUAGE_ELEMENT =  'iw_user[language_pref]';

/**
 * Sends an AJAX request to retrieve the elements[this.PROVINCE_ELEMENT] list for the selected
 * country
 *
 * @param countryId the country selected by the user
 */
YAHOO.cedars.SignUp.Form.prototype.filterProvinces = function(countryCode) {
	this.provinceCallback.form = this.form;
	this.provinceCallback.owner = this;	
	YAHOO.cedars.ajax.asyncRequest('get', 'location', this.provinceCallback, {list:'provinces', country: countryCode});
	this.form.elements[this.PROVINCE_ELEMENT].disabled = true;
}

/**
 * A callback that refreshes the elements[this.PROVINCE_ELEMENT] list
 */
YAHOO.cedars.SignUp.Form.prototype.provinceCallback = {
	success: function(o) {
		var provinceList = YAHOO.cedars.ajax.toObject(o);
		var countryCode = this.form.elements[this.owner.COUNTRY_ELEMENT].value;
		this.form.elements[this.owner.PROVINCE_ELEMENT].innerHTML = '';
		if (!YAHOO.cedars.isEmpty(provinceList)) {
			var provinceCode;
			for (provinceCode in provinceList) {
				var option = document.createElement('option');
				option.text = provinceList[provinceCode];
				option.value = provinceCode;
				option.innerHTML = provinceList[provinceCode];
				this.form.elements[this.owner.PROVINCE_ELEMENT].appendChild(option);
			}
			this.form.elements[this.owner.PROVINCE_ELEMENT].disabled = false;
			if (provinceList[this.owner.DEFAULT_PROVINCE]) {
				this.form.elements[this.owner.PROVINCE_ELEMENT].value = this.owner.DEFAULT_PROVINCE;			
			}
			$('sign-up-field-province').style.display = '';
			$('sign-up-field-province_other').style.display = 'none';			                 
			$('sign-up-field-label-province').innerHTML = YAHOO.cedars.ajax.getText('province_label_' + countryCode, 'site') + ' *';	
		} else {
			var option = document.createElement('option');
			option.value = '';
			option.text = option.innerHTML = '';
			this.form.elements[this.owner.PROVINCE_ELEMENT].appendChild(option);		
			$('sign-up-field-province').style.display = 'none';
			$('sign-up-field-province_other').style.display = '';			
		}
		
		
	}
}
