
if(typeof(surveyMonster)=="undefined")	
	surveyMonster={};

if(typeof(surveyMonster.widgets)=="undefined")
	surveyMonster.widgets={};


surveyMonster.widgets.loadDynamicProvinces = function(form,countryName,provinceName,hiddenProvinceName){

	this.form=form;
	this.country=this.form[countryName];
	this.provinceFE=this.form[provinceName];
	this.province=this.form[hiddenProvinceName];
	
	this.provinceURL="includes/api.asp?function=getProvinces&countryID=";
	
	this.defaultProvinceID=null;
	
};


surveyMonster.widgets.loadDynamicProvinces.prototype.enable = function(){

	YAHOO.util.Event.addListener(this.form, "submit", this.onSubmit,this,true);
	YAHOO.util.Event.addListener(this.provinceFE, "change", this._setProvince,this,true);
	YAHOO.util.Event.addListener(this.country, "change", this._setCountry,this,true);
	
	this._setCountry();
};


surveyMonster.widgets.loadDynamicProvinces.prototype.onSubmit = function(){
	this.province.value=this.provinceFE.value;	
};


surveyMonster.widgets.loadDynamicProvinces.prototype._setProvince = function()
{
	this.province.value=this.provinceFE.value;
};


surveyMonster.widgets.loadDynamicProvinces.prototype._setCountry = function()
{
	// If no country selected
	if(this.country.value=="0"){
		this.clearProvinces();
		this.noCountry();
		return;
	}
	
	var hasProvinces
	if(this.country[this.country.selectedIndex].getAttribute("hasProvinces")=="1")
		hasProvinces=true;
	else
		hasProvinces=false;
		
	this.setCountry(this.country.value,hasProvinces)
};


surveyMonster.widgets.loadDynamicProvinces.prototype.clearProvinces = function()
{
	// Clear the old provinces
	for(var i=this.provinceFE.options.length-1;i>=0;i--)
		this.provinceFE.remove(i)
};


surveyMonster.widgets.loadDynamicProvinces.prototype.noCountry = function()
{
	this.province.value='0';
	this.provinceFE.disabled=true;
	var newOpt = document.createElement('option');
		newOpt.value=0;
		newOpt.text="Select a Country first";
		newOpt.selected=true;
	this.provinceFE.options[this.provinceFE.options.length]=newOpt;
	
	this.showDropDown();
}


surveyMonster.widgets.loadDynamicProvinces.prototype.noProvinces = function()
{
	this.province.value='0';
	this.provinceFE.disabled=true;
	var newOpt = document.createElement('option');
		newOpt.value=0
		newOpt.text="No Provinces"
		newOpt.selected=true
	this.provinceFE.options[this.provinceFE.options.length]=newOpt;
		
	this.hideDropDown();
}


surveyMonster.widgets.loadDynamicProvinces.prototype.setCountry = function(countryId,hasProvinces)
{
	this.clearProvinces();
		
	// If the country has provinces
	if(hasProvinces){
	
		this.province.value='';
		var callback={};
		callback["success"]=function(data){
		
			var provinces=parseProvinces(data.responseXML);
			
			// Throw in the first object
			var newOpt = document.createElement('option');
				newOpt.value="";
				newOpt.text="Select a Province";
				this.provinceFE.options[this.provinceFE.options.length]=newOpt;				
			
			for(var x=0;x<provinces.length;x++){
				var newOpt = document.createElement('option');
				newOpt.value=provinces[x].id;
				newOpt.text=provinces[x].name;
				if(newOpt.value==this.defaultProvinceID){
					newOpt.selected=true;				
					this.province.value=this.defaultProvinceID;
				}
				this.provinceFE.options[this.provinceFE.options.length]=newOpt;
			}
				
			this.provinceFE.disabled=false;
			this.showDropDown();
			
			if(this.provinceFE.onchange)
				this.provinceFE.onchange()
		
		}
		callback["scope"]=this;
		
		var transaction = YAHOO.util.Connect.asyncRequest('GET',this.provinceURL+countryId,callback,null);	
	
	// if the country does not have provinces
	}else{
	
		this.noProvinces();
	  			
	}
	
	var parseProvinces = function(data){

		var provincesXML=data.getElementsByTagName("province");
		var toReturn=[];
		
			for(var x=0;x<provincesXML.length;x++){
				var provinceObj={};
				provinceObj.id			= provincesXML[x].getElementsByTagName("id")[0].childNodes[0].nodeValue
				provinceObj.name		= provincesXML[x].getElementsByTagName("name")[0].childNodes[0].nodeValue
				toReturn.push(provinceObj);
			}
			
			return toReturn;

	}
};


surveyMonster.widgets.loadDynamicProvinces.prototype.showDropDown = function(){
	this.provinceFE.parentNode.parentNode.style.display='';
}

surveyMonster.widgets.loadDynamicProvinces.prototype.hideDropDown = function(){
	this.provinceFE.parentNode.parentNode.style.display='none';
}
