﻿dojo.require("dojo.fx");

function StateProvinceControl(obj) {
	this.cList = dojo.byId(obj.countrySelect);
	this.sList = dojo.byId(obj.stateSelect);
	this.label = dojo.byId(obj.stateLabel);
	//this.hidden = dojo.byId(obj.stateHidden);
	this.hidden = dojo.byId(obj.stateSelect + '_req');
	this.country = this.cList[this.cList.selectedIndex].value;
	this.countries = {};
	this.waiting = false;
	dojo.connect(this.cList, 'onchange', dojo.hitch(this, this.update));
	dojo.connect(this.cList, 'onkeyup', dojo.hitch(this, this.update));
};

StateProvinceControl.prototype.update = function() {
	if(this.waiting === false) {
		if(this.country != this.cList[this.cList.selectedIndex].value) {
				
			this.country = this.cList[this.cList.selectedIndex].value;
				
			if(this.country != "") {
				if(typeof this.countries[this.country] == 'undefined') {		
						
					this.disableStates();
						
					this.countries[this.country] = null;
						
					this.waiting = true;
						
					dojo.io.script.get({
						url : TE_getHomePageURL() + "/_includes/scripts/TE_countrystate.asp?t=p&c=" + this.country,
						callbackParamName : "callback",
						preventCache : true,
						timeout : 10000,
						handle : dojo.hitch(this, function(response, ioArgs) {
							if(response.dojoType == 'timeout') {
								// timed out
							} else {
								this.countries[this.country] = response;
									
								if(this.count(this.countries[this.country]) !== 0) {
									this.addStates();
								}
									
								this.waiting = false;
							}
						})
					});
						
					/*dojo.xhrGet({
						url : TE_getHomePageURL() + "/_includes/scripts/TE_countrystate.asp?c=" + this.country,
						handleAs : 'json',
						preventCache : true,
						timeout : 10000,
						handle : dojo.hitch(this, function(response, ioArgs) {
							if(response.dojoType == 'timeout') {
								// timed out
							} else {
								this.countries[this.country] = response;
									
								if(this.count(this.countries[this.country]) !== 0) {
									this.addStates();
								}
									
								this.waiting = false;
							}
						})
					});*/
						
				} else {
					this.disableStates();
						
					if(this.count(this.countries[this.country]) !== 0) {
						this.addStates();
					}
				}
			} else {
				this.disableStates();
			}
		}
	} else {
		setTimeout(dojo.hitch(this, this.update), 500);
	}
};
	
StateProvinceControl.prototype.count = function(obj) {
	var cnt = 0;
		
	for(var i in obj) {
		cnt++;
	}
		
	return cnt;
};

StateProvinceControl.prototype.addStates = function() {
	for(var i = this.sList.options.length - 1; i > 0; i--) {
		this.sList.removeChild(this.sList.options[i]);
	}
	
	for(var i = 0; i < this.countries[this.country].length; i++) {
		this.sList.appendChild(newNode('option', {'value':this.countries[this.country][i]['STATE_PROVINCE_CDE'], 'text':this.countries[this.country][i]['STATE_PROVINCE_NM']}));
	}
	
	dojo.addClass(this.label, 'TE_required');
	this.hidden.value = "true";
	this.sList.disabled = false;
};

StateProvinceControl.prototype.disableStates = function() {
	dojo.removeClass(this.label, 'TE_required');
	this.hidden.value = "false";
	this.sList.selectedIndex = 0;
	this.sList.disabled = true;
};

function newNode(node) {
	var x = document.createElement(node);
	var attr = arguments[1];
	if(typeof attr != 'undefined' && new RegExp(/Object/).test(attr.constructor)) {
		for(var i in attr) {
			if(i == 'text') {
				x.appendChild(document.createTextNode(attr[i]));
			} else {
				x[i] = attr[i];
			}
		}
	}
	return x;
}

dojo.addOnLoad(function() {
	new StateProvinceControl({
		countrySelect : 'cust_country',
		stateSelect : 'cust_state',
		stateLabel : 'cust_state_label'
	});
	var x = dojo.query('fieldset', dojo.byId('raychemSampleRequest'))[0];
	if(typeof x != 'undefined') {
		x.appendChild(newNode('input', {'type':'hidden', 'name':'nobot', 'value':'not a bot'}));
	}
});
