/**
 * @class Address
 * @classDescription フロント：お届け便検索用　住所系リストボックス取得
 * @author Petabit Corporation.
 * @since 2008-12-03
 * @version 1.0.0.0
 */

function getGoodsList(flg, add_param){

	month_select = jQuery("#month").val();
	day_select   = jQuery("#day").val();
	pref_select  = jQuery("#pref").val();
	city_select  = jQuery("#city").val();
	town_select  = jQuery("#town").val();
	goods_1   = jQuery("#goods_no_1").val();
	goods_2   = jQuery("#goods_no_2").val();

	//10月から12月に1月から3月のお届け日を選択した場合は翌年分と判定する。
	date = new Date();
	now_year = date.getFullYear();
	now_month =date.getMonth() + 1;
	
	if(now_month >= 10 && now_month <= 12 && month_select <= 3){
		jQuery("#year").val(now_year + 1);
	}else{
		jQuery("#year").val(now_year);
	}
	year_select = jQuery("#year").val();
	
	jQuery("#date_error").html("");
	jQuery("#address_error").html("");

	//未入力項目があれば処理終了
	if(year_select == null || month_select == null || day_select == null ||
	   pref_select == null || city_select == null || town_select == null 
	){
		return false;
	}

	var html = jQuery.ajax({
					type: "POST",
					url: URL,
					data: "year_select="   + year_select  + 
						  "&month_select=" + month_select + 
						  "&day_select="   + day_select   + 
						  "&pref_select="  + pref_select  + 
						  "&city_select="  + city_select  + 
						  "&town_select="  + town_select  + 
						  "&goods_no_1="   + goods_1   + 
						  "&goods_no_2="   + goods_2   + 
						  add_param,
					cache: false,
					async: false
			   }).responseText;

	options = html.split("||");

	//タグの変更
	if(flg == true && jQuery("#area_no").val() != options[2]){
		jQuery("#receiver_no").val("");
	}
	
	jQuery("#date_error").html(options[0]);
	if(options[0].length > 0){
		jQuery("td:has(#date_error)").addClass("error");
	}
	
	jQuery("#address_error").html(options[1]);
	if(options[1].length > 0){
		jQuery("td:has(#address_error)").addClass("error");
	}
	jQuery("#area_no").val(options[2]);
	jQuery("#goods_list").html(options[3]).fadeIn();
	
	//サーバー時間
	if(document.getElementById("watch") != null){
		getClock();
	}

	return false;

}

function moodalZip2Area(){
	zip = jQuery("#zip").val();

	if(zip.match("-")){
		sepa_zip = zip.split("-");
		zip1 = sepa_zip[0];
		zip2 = sepa_zip[1];
	}else{
		zip1 = zip.substr(0,3);
		zip2 = zip.substr(3,4);
	}
	
	//パラメータに郵便番号をくっつける
	url = "/lib/moodal_zip2area.php?zip1=" + zip1  + "&zip2=" + zip2;


	jQuery.facebox({ ajax: url });

	jQuery('#facebox .content').css('height', 300);
	jQuery('#facebox .content').css('width', jQuery('#facebox table').width() -30);
	jQuery('#facebox .content').css('overflowX', 'auto');
	jQuery('#facebox .content').css('overflowY', 'auto');
}

function moodalReceiver2Area(){
	
	url = "/lib/moodal_receiver2area.php";

	jQuery.facebox({ ajax: url })

	jQuery('#facebox .content').css('height', 300);
	jQuery('#facebox .content').css('width', jQuery('#facebox table').width() -30);
	jQuery('#facebox .content').css('overflowX', 'auto');
	jQuery('#facebox .content').css('overflowY', 'auto');
}

//郵便番号検索
function area2Address(area){

	var html = jQuery.ajax({
					type: "POST",
					url: "/lib/jquery_address.php",
					data: "type=area_delivery&area=" + area ,
					cache: false,
					async: false
			   }).responseText;

	options = html.split("||");

	document.getElementById("pref").value = options[0];
	jQuery("#city").html(options[1]).show();
	jQuery("#town").html(options[2]).show();

	getGoodsList(true, "");

	jQuery.facebox.close();
}

//お届け先から選択
function area2AddressForReceiver(area, receiver_no){

	area2Address(area);
	
	jQuery("#receiver_no").val(receiver_no);
	
}

//住所選択
function changeAddress(type){

	if(type == "pref2"){
		jQuery("#city").hide();
		jQuery("#town").hide();
	
	}else if(type == "city2"){
		jQuery("#town").hide();

	}
	
	pref_select = jQuery("#pref").val();
	city_select = jQuery("#city").val();
	town_select = jQuery("#town").val();

	var html = jQuery.ajax({
					type: "POST",
					url: "/lib/jquery_address.php",
					data: "type=" + type + "&pref_select=" + pref_select + "&city_select=" +city_select + "&town_select=" +town_select,
					cache: false,
					async: false
			   }).responseText;

	if(type == "pref2"){
		jQuery("#city").html(html).fadeIn("slow");
		jQuery("#town").html("").fadeOut("fast");
	
	}else if(type == "city2"){
		jQuery("#town").html(html).fadeIn("slow");

	}

	return false;

}
