// create a namespace
YAHOO.namespace("s_common_polo");
			
// define the namespace object
YAHOO.s_common_polo = function() {

	// shortcuts
	var $E = YAHOO.util.Event;
	var $D = YAHOO.util.Dom;
	var $ = $D.get;

	return {
		
		init: function(){
			
			return true;

		},
		
		// Recives three arguments, both are strings
		
		// 1.  The id of the country drop down that has fired off the onChange event
		// 2.	The id of the region drop down that is to recieve the regions once we have them
		// 3. The id of the region that should be the selected dropdown option (i.e. the value of the option )
		
		// you can work the rest out I am sure
		populate_region_drop_down: function(){
				
			//alert(arguments[0] + ' ' + arguments[1] + ' ' + arguments[2]);
			
			if(arguments[0] !=null){				
				var country_drop_down = document.getElementById(arguments[0]);
			}
			if(arguments[1] !=null){
				var region_drop_down = document.getElementById(arguments[1]);
			}
			if(arguments[2] !=null){
				var selected_region = arguments[2];
			}
		
			// define a failure object if the callback object response is failure
			var responseFailure = function(o){				
				return false;
			};
				
			// define a failure object if the callback object response is success
			var responseSuccess = function(o){	
						
				// move through the XML and get the data returned
				var root = o.responseXML; 
							
				var db_region_list= root.getElementsByTagName('getRegionDropDownValues');
				
				var number_of_regions = db_region_list.length;
				
				region_drop_down.options.length = 0;
				
				region_drop_down.options[0] =  new Option("Please Select One" , "-1") ;

				if(number_of_regions > 0){

					for(var i = 0; i < number_of_regions ; i++){
						
						region_drop_down.options[ i + 1 ] =  new Option( db_region_list[i].childNodes[1].firstChild.nodeValue  , db_region_list[i].childNodes[0].firstChild.nodeValue  ) ;
	
						if(selected_region == db_region_list[i].childNodes[0].firstChild.nodeValue){
							region_drop_down.selectedIndex = i + 1;
						}
					}
				}
			};
				
			// define a callback object to handle server response from the asyncRequest call
			var callback = { 
				success:responseSuccess, 
				failure:responseFailure, 
				argument:[] 
			};
				
			// use Connection Manager to get xml data
			var transaction = YAHOO.util.Connect.asyncRequest('GET', "xml/get_list.php?dataType=getRegionDropDownValues&fk_countries_ISO2="   + country_drop_down.value, callback);
		}
	};
}();

// intialise page object once the page has loaded
YAHOO.util.Event.on(window, 'load', YAHOO.s_common_polo.init, YAHOO.s_common_polo, true );
