var WtbPullDown = function()
{
	this.wtb_redirect_php_dir = "./redirect/";
	this.wtb_redirect_php_name = "redirect.php";
	this.wtb_tag_loading = "wtbXMLloading";
	this.wtb_tag_menu = "wheretobuyform";
	this.wtb_btn_id = "selectsubmit";
	this.wtb_get_xml = false;
}




WtbPullDown.prototype =
{
	wtb_redirect_php_dir:"",
	wtb_redirect_php_name:"",
	wtb_tag_loading:"",
	wtb_tag_menu:"",
	wtb_btn_id:"",
	wtb_get_xml:false,

	wtbStartLoadXML : function(){

		$(this.wtb_tag_loading).style.display = "";

		var owner = this;

		new Ajax.Request(
			this.wtb_redirect_php_dir + 'wtb.xml',
			{
				method: 'get', 
				onComplete: function(request){owner.wtbLoadSuccessWTBXML(request)},
				onFailure: function(request){owner.wtbLoadFailWTBXML(request)}
			}
		);

	},
	wtbLoadSuccessWTBXML:function (request){
		var xmlRoot = request.responseXML.documentElement;
		var regions = xmlRoot.getElementsByTagName("region");
		var region_num = regions.length;
		var r_cnt = 0;
		var c_cnt = 0;
		var countries;
		var country_num;
		var ie_flg = /*@cc_on!@*/0;

		var setHtml = '';
		setHtml += '<dl>\n';
		//setHtml += '<dt>Select Region</dt>\n';
		setHtml += '<dd>\n';
		setHtml += '<select id="wtb_region">\n';
		setHtml += '<option value="ps">- Select Region &nbsp;-</option>\n';

		for( r_cnt = 0; r_cnt < region_num; r_cnt++){
			setHtml += '<option value="'+ regions[r_cnt].getAttribute("code") + '">' + regions[r_cnt].getAttribute("name") + '</option>\n';
		}

		setHtml += '</select>\n';
		setHtml += '</dd>\n';
		//setHtml += '<dt>Select country</dt>\n';
		setHtml += '<dd id="wtb_country">\n';
		setHtml += '<select id="ps">\n';
		setHtml += '<option value="#">- Select Country -</option>\n';
		setHtml += '</select>\n';

		for( r_cnt = 0; r_cnt < region_num; r_cnt++){
			countries = regions[r_cnt].getElementsByTagName("country");
			country_num = countries.length;

			setHtml += '<select id="' + regions[r_cnt].getAttribute("code") + '" style="display:none;">\n';
			for( c_cnt = 0; c_cnt < country_num; c_cnt++){
				setHtml += '<option value="'+ countries[c_cnt].getAttribute("name") + '">' + countries[c_cnt].getAttribute("name") + '</option>\n';
			}
			setHtml += '</select>\n';
		}

		setHtml += '</dd>\n';
		setHtml += '</dl>\n';
		setHtml += '<p class="btn_search"><a href="#" id="' + this.wtb_btn_id + '">Search</a></p>\n';

		//$(wtb_tag_loading).hide();
		$(this.wtb_tag_loading).style.display = "none";
		$(this.wtb_tag_menu).innerHTML = setHtml;
		//$(wtb_tag_menu).show();
		$(this.wtb_tag_menu).style.display = '';

		var owner = this;

		Event.observe( $("wtb_region"), "change", function(){owner.wtbChangeRegion();});
		for( r_cnt = 0; r_cnt < region_num; r_cnt++){
			if( ie_flg ){
				Event.observe( $(regions[r_cnt].getAttribute("code")), "change", function(){owner.wtbChangeCountry(event.srcElement)});
			}else{
				Event.observe( $(regions[r_cnt].getAttribute("code")), "change", function(){owner.wtbChangeCountry(this)});
			}
		}

		this.wtb_get_xml = true;
	},

	wtbLoadFailWTBXML : function(request){

		var setHtml = '';
		setHtml += '<dl>\n';
		setHtml += '<dt>Select Region</dt>\n';
		setHtml += '<dd>\n';
		setHtml += '<p>Go to next page to select a country/region</p>\n';
		setHtml += '<p class="link"><a href="http://www.biz.konicaminolta.com/where_to_buy/index.html">Go to HTML Page</a></p>\n';
		setHtml += '</dd>\n';
		setHtml += '</dl>\n';

		//$(wtb_tag_loading).hide();
		$(this.wtb_tag_loading).style.display = 'none';
		$(this.wtb_tag_menu).innerHTML = setHtml;
		//$(wtb_tag_menu).show();
		$(this.wtb_tag_menu).style.display = '';
	},

	wtbChangeRegion : function(){

		if( !this.wtb_get_xml ) return;

		var region_cd = $F("wtb_region");
		var country_selectars = $("wtb_country").childNodes;
		var country_selectars_num = country_selectars.length;
		var cnt;
		var disp_cnt = 0;
		var ps_flg = false;

		for( cnt = 0; cnt < country_selectars_num; cnt++){
			
			if( country_selectars[cnt].nodeName != "SELECT" ) continue;

			if( country_selectars[cnt].id == region_cd){
				country_selectars[cnt].style.display = "";
				//country_selectars[cnt].show();
				disp_cnt = cnt;
				if( region_cd == "ps") ps_flg = true;
			}
			else{
				country_selectars[cnt].style.display = "none";
				//country_selectars[cnt].hide();
			}
		}

		if( ps_flg ){
			$(this.wtb_btn_id).href = "#";
		}
		else{
			country_selectars[disp_cnt].selectedIndex = 0;
			$(this.wtb_btn_id).href = this.wtb_redirect_php_dir + this.wtb_redirect_php_name + '?rc=' + region_cd + '&cn=' + encodeURIComponent(country_selectars[disp_cnt].value);
		}

	},

	wtbChangeCountry : function(src_element){

		if( !this.wtb_get_xml ) return;

		var region_cd = $F("wtb_region");
		$(this.wtb_btn_id).href = this.wtb_redirect_php_dir + this.wtb_redirect_php_name + '?rc=' + region_cd + '&cn=' + encodeURIComponent(src_element.value);
	}
}

