/*Set the location information*/

Ext.onReady(function(){

	var form = new Ext.form.Form({
							name: 'countrySelectForm',
							id: 'countrySelectForm',
							url: 'home.php',
							method: "post",
							labelWidth: 1 //to hide the labels
					});

	 //datastore for country dropdown combo
				 var ds_countries = new Ext.data.Store({
						proxy: new Ext.data.HttpProxy({url: 'ajax-locations.php'}),
						//baseParams
						baseParams: {action:'returncountries'},
						// the return will be XML, so lets set up a reader
						reader: new Ext.data.XmlReader({
						record: 'country',
						id: 'iso'
						}, ['iso','name', 'regionid'])
					});

			var countryBox = new Ext.form.ComboBox({
					fieldLabel: '',
					hiddenName:'country_id',
					name: 'country_id',
					store: ds_countries,
					displayField:'name',
					valueField: 'iso',
					typeAhead: true,
					mode: 'local',
					minChars: '2',
					//editable: 'false',
					triggerAction: 'all',
					emptyText:'Select your country...',
					selectOnFocus:true,
					width:200
				});

				countryBox.on("select", function(c,r,i){
						var regionid = r.get("regionid");
						Ext.pageData.userCountry = r.get("name");
						Ext.pageData.userCountryID = r.get("iso");
						Ext.pageData.userRegion = regionid;

						Ext.Ajax.request({
							url:'ajax-user-data.php',
							params: {action:'setRegionInfo', countryname:Ext.pageData.userCountry, region_id:Ext.pageData.userRegion, countryiso:Ext.pageData.userCountryID},
							method:'post',
							success: function(response){
								var jsontext = Ext.util.JSON.decode(response.responseText);
								if(jsontext.success != false){ //success
								//Ext.Msg.alert("Success","Your changes were saved successfully.");
								//ds.reload();
								}else { //failure
								//Ext.Msg.alert("Error",jsontext.errors.msg);
								//ds.reload();
								}
							},
							failure: function(response){
								//var jsontext = Ext.util.JSON.decode(response.responseText);
								//Ext.Msg.alert("Error","There was an error sending the data to the server. Please try again.");
							}
						}); 
				});

				ds_countries.on("load", function(){
					if(Ext.pageData.userCountryID != "null" || Ext.pageData.userCountryID != ""){
					//	Ext.Msg.alert("",Ext.pageData.userCountryID);
						countryBox.setValue(Ext.pageData.userCountryID);
					}
				});
				ds_countries.load();

		form.column({id:'countrypanel', width:210, labelSeparator:'',labelAlign:'right'});
			form.add(countryBox);
		form.end();

	/*	form.addButton("Go",function(){
			form.submit();
		});*/
		form.render('location-select');
});