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

function getGoodsList(flg, add_param, ajaxFlg){

	var ajaxFlg = ajaxFlg || "on";

	if(ajaxFlg == "on" ){
		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();
		}

	}else{

//コメントアウトしたAjax処理はphpとjsに処理を移行しました
//2010/10/01 ohara
//		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(document.getElementById("watch") != null){
			getClock();
		}
	
//		//未入力項目があれば処理終了
//		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();
	
	}
	


	return false;

}

function moodalZip2Area(actionUrl,ajaxFlg,flowType){
	
	var actionUrl = actionUrl || "";
	var ajaxFlg   = ajaxFlg || "on";
	var flowType  = flowType || "";
	
	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);
	}
	
	if(ajaxFlg == "on"){
		//パラメータに郵便番号をくっつける
		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');

	}else{

		//パラメータに郵便番号をくっつける
		//url = "/lib/moodal_zip2area.php?zip1=" + zip1  + "&zip2=" + zip2;
		url = actionUrl + "?zip1=" + zip1  + "&zip2=" + zip2  + "&zip_select=zip#selectAddress";
		
		if(flowType == "template"){
			document.entry_adr.action = url;
			document.entry_adr.submit();
		}else if(flowType == "edit"){
			document.edit.action = url;
			document.edit.submit();
		}else{
			document.delivery.action = url;
			document.delivery.submit();
		}
	
//		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(ajaxFlg){
	
	var actionUrl = GetFileName(location.href);
	var ajaxFlg   = ajaxFlg || "on";

	
	if(ajaxFlg == "on"){
		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');

	}else{
		//url = "/lib/moodal_receiver2area.php";
		url = actionUrl + "?rsv_select=rsv#selectAddress";
		document.delivery.action = url;
		document.delivery.submit();

//		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,ajaxFlg,flowType){

	var actionUrl = GetFileName(location.href);
	var ajaxFlg   = ajaxFlg || "on";
	var flowType  = flowType || "";
	
	if(ajaxFlg == "on"){

		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("||");

                /*****************************/
                /* 地域番号追加 [2010/12/02] */

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

                var area_no = options[0];

                var param = "&area_select=" + area_no;

                getGoodsList(true, param);

                /*****************************/

		jQuery.facebox.close();

	}else{

		actionUrl = actionUrl + "#selectAddress";

		document.getElementById("area_no").value = area;
		if(flowType == "template"){
			document.entry_adr.action = actionUrl;
			document.entry_adr.submit();
		}else if(flowType == "entry"){
			document.entry.action = actionUrl;
			document.entry.submit();
		}else if(flowType == "edit"){
			document.edit.action = actionUrl;
			document.edit.submit();
		}else{
			document.delivery.action = actionUrl;
			document.delivery.submit();
		}
	
//	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("||");
//	window.alert(options);
//	document.getElementById("pref").value = options[0];
//	jQuery("#city").html(options[1]).show();
//	jQuery("#town").html(options[2]).show();
//
//	getGoodsList(true, "");
	//window.alert(options);
	//jQuery.facebox.close();
	
	}
}

//お届け先から選択
function area2AddressForReceiver(area, receiver_no, ajaxFlg){
	
	var actionUrl = GetFileName(location.href);
	var ajaxFlg   = ajaxFlg || "on";
    if(actionUrl == ""){
    	actionUrl = "index.php";
    }
	if(ajaxFlg == "on"){
		area2Address(area);
		
		jQuery("#receiver_no").val(receiver_no);

	}else{
		url = actionUrl + "#selectAddress";
		
		document.getElementById("receiver_no").value = receiver_no;
		document.getElementById("area_no").value = area;
		document.delivery.action = url;
		document.delivery.submit();
	}
	
}

//住所選択
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;

}

function GetFileName(file_url){
    file_url = file_url.substring(file_url.lastIndexOf("/")+1,file_url.length);
    //拡張子も取り除く場合は次の行のコメントアウトをはずしてください
    file_url = file_url.substring(0,file_url.indexOf("?"));

    return file_url;
}

