function zs_search(){
	var zip_value;
	if( typeof( zs_zipcode ) != 'undefined' ){
		zip_value = document.getElementById( zs_zipcode ).value;
	}else if( typeof( zs_zipcode_first ) != 'undefined' && typeof( zs_zipcode_last ) != 'undefined' ){
		zip_value = document.getElementById( zs_zipcode_first ).value + document.getElementById( zs_zipcode_last ).value;
	}

	if( typeof( zs_site_id ) != 'undefined' && zs_site_id != null && zs_check( zip_value ) ){
		var target = document.createElement( 'script' );
		target.charset = 'utf-8';

		var zs_src = (("https:" == document.location.protocol) ? "https://" : "http://");
		zs_src += 'zipserver.jp/php/api/zs.php?site_id=' + encodeURIComponent( zs_site_id ) + '&zipcode=' + encodeURIComponent( zip_value ) + '&uri=' + encodeURIComponent( document.location );
		target.src = zs_src;

		document.body.appendChild(target);
	}
}

function zs_check( zip_value ){
  if( ! zip_value ) return false;
  if( 0 == zip_value.length ) return false;
  
  if( ! zip_value.match( /^[0-9]{3}[-]?[0-9]{0,4}$/ ) )
  {
    return false;
  }
  
  return true;
}


function zs_select( i ){
	zs_set_address( zip_data.address[ i ] );
	zs_erase_list();
}

function zs_erase_list(){
	if( typeof( zs_ziplist ) != 'undefined' && document.getElementById( zs_ziplist ) != null ){
		document.getElementById( zs_ziplist ).innerHTML = '';
	}
}

function zs_set_address( obj )
{
	//zipcode
	if( typeof( zs_zipcode ) != 'undefined' && document.getElementById( zs_zipcode ) != null ){
		if( document.getElementById( zs_zipcode ).value.indexOf( '-' ) > 0 ){
			document.getElementById( zs_zipcode ).value = obj.zipcode.substr(0, 3) + '-' + obj.zipcode.substr(3);
		}else{
			document.getElementById( zs_zipcode ).value = obj.zipcode;
		}
	}

	if( typeof( zs_zipcode_first ) != 'undefined' && document.getElementById( zs_zipcode_first ) != null ) document.getElementById( zs_zipcode_first ).value = obj.zipcode.substr(0, 3);
	if( typeof( zs_zipcode_last ) != 'undefined' && document.getElementById( zs_zipcode_last ) != null )   document.getElementById( zs_zipcode_last ).value = obj.zipcode.substr(3);

	//address
	if( typeof( zs_address )      != 'undefined' && document.getElementById( zs_address ) != null )      document.getElementById( zs_address ).value = obj.city + obj.town + obj.house_number;
	if( typeof( zs_address_hira ) != 'undefined' && document.getElementById( zs_address_hira ) != null ) document.getElementById( zs_address_hira ).value = obj.city_hira + obj.town_hira;
	if( typeof( zs_address_kata ) != 'undefined' && document.getElementById( zs_address_kata ) != null ) document.getElementById( zs_address_kata ).value = obj.city_kata + obj.town_kata;
                                                                                                                                      
	//prefecture
	if( typeof( zs_prefecture ) != 'undefined' && document.getElementById( zs_prefecture ) != null ){
		if( document.getElementById( zs_prefecture ).type != 'text' ){
			if( typeof( zs_code_type ) != 'undefined' && code_type == 'JIS' ){
				document.getElementById( zs_prefecture ).value = obj.pref_code_JIS;
			}else if( typeof( zs_code_type ) != 'undefined' && code_type == 'JARL' ){
				document.getElementById( zs_prefecture ).value = obj.pref_code_JARL;
			}else{
				document.getElementById( zs_prefecture ).value = obj.prefecture;
			}
		}else{
			document.getElementById( zs_prefecture ).value = obj.prefecture;
		}
	}
	if( typeof( zs_prefecture_hira ) != 'undefined' && document.getElementById( zs_prefecture_hira ) != null ) document.getElementById( zs_prefecture_hira ).value = obj.prefecture_hira;
	if( typeof( zs_prefecture_kata ) != 'undefined' && document.getElementById( zs_prefecture_kata ) != null ) document.getElementById( zs_prefecture_kata ).value = obj.prefecture_kata;



	//clear city
	if( typeof( zs_city )         != 'undefined' && document.getElementById( zs_city )         != null ) document.getElementById( zs_city ).value         = '';
	if( typeof( zs_city_hira )    != 'undefined' && document.getElementById( zs_city_hira )    != null ) document.getElementById( zs_city_hira ).value    = '';
	if( typeof( zs_city_kata )    != 'undefined' && document.getElementById( zs_city_kata )    != null ) document.getElementById( zs_city_kata ).value    = '';
	//clear town
	if( typeof( zs_town )         != 'undefined' && document.getElementById( zs_town )         != null ) document.getElementById( zs_town ).value         = '';
	if( typeof( zs_town_hira )    != 'undefined' && document.getElementById( zs_town_hira )    != null ) document.getElementById( zs_town_hira ).value    = '';
	if( typeof( zs_town_kata )    != 'undefined' && document.getElementById( zs_town_kata )    != null ) document.getElementById( zs_town_kata ).value    = '';
	//clear company
	if( typeof( zs_company )      != 'undefined' && document.getElementById( zs_company )      != null ) document.getElementById( zs_company ).value      = '';
	if( typeof( zs_company_hira ) != 'undefined' && document.getElementById( zs_company_hira ) != null ) document.getElementById( zs_company_hira ).value = '';
	if( typeof( zs_company_kata ) != 'undefined' && document.getElementById( zs_company_kata ) != null ) document.getElementById( zs_company_kata ).value = '';
	//clear house_number
	if( typeof( zs_house_number ) != 'undefined' && document.getElementById( zs_house_number ) != null ) document.getElementById( zs_house_number ).value = '';


	//city
	if( typeof( zs_city )         != 'undefined' && document.getElementById( zs_city ) != null )         document.getElementById( zs_city ).value      = document.getElementById( zs_city ).value + obj.city;
	if( typeof( zs_city_hira )    != 'undefined' && document.getElementById( zs_city_hira ) != null )    document.getElementById( zs_city_hira ).value = document.getElementById( zs_city_hira ).value + obj.city_hira;
	if( typeof( zs_city_kata )    != 'undefined' && document.getElementById( zs_city_kata ) != null )    document.getElementById( zs_city_kata ).value = document.getElementById( zs_city_kata ).value + obj.city_kata;
	//town
	if( typeof( zs_town )         != 'undefined' && document.getElementById( zs_town ) != null )         document.getElementById( zs_town ).value      = document.getElementById( zs_town ).value + obj.town;
	if( typeof( zs_town_hira )    != 'undefined' && document.getElementById( zs_town_hira ) != null )    document.getElementById( zs_town_hira ).value = document.getElementById( zs_town_hira ).value + obj.town_hira;
	if( typeof( zs_town_kata )    != 'undefined' && document.getElementById( zs_town_kata ) != null )    document.getElementById( zs_town_kata ).value = document.getElementById( zs_town_kata ).value + obj.town_kata;
	//company
	if( typeof( zs_company )      != 'undefined' && document.getElementById( zs_company ) != null )      document.getElementById( zs_company ).value = document.getElementById( zs_company ).value + obj.company;
	if( typeof( zs_company_hira ) != 'undefined' && document.getElementById( zs_company_hira ) != null ) document.getElementById( zs_company_hira ).value = document.getElementById( zs_company_hira ).value + obj.company_hira;
	if( typeof( zs_company_kata ) != 'undefined' && document.getElementById( zs_company_kata ) != null ) document.getElementById( zs_company_kata ).value = document.getElementById( zs_company_kata ).value + obj.company_kata;

	//house_number
	if( typeof( zs_house_number ) != 'undefined' && document.getElementById( zs_house_number ) != null ) document.getElementById( zs_house_number ).value = document.getElementById( zs_house_number ).value + obj.house_number;
}


function zips( data ){
	zip_data = data;
	if( typeof( zs_ziplist ) == 'undefined' || zip_data.address.length == 1 ){
		zs_set_address( zip_data.address[0] );
	}else if( zip_data.address.length > 1 ){
		var list = '';
		list += '<div style="width:400px; background-color:#EEEEEE; padding:5px 10px 10px 10px">';
		list += '<div style="font-size:80%; color:#FF0000">住所を選択してください。　<a href="javascript:void(0);" style="text-decoration: none; font-size:120%; color:#000000" onClick="zs_erase_list();">[×]</a></div>';
		list += '<div id="zip_select" style="height:150px; background-color:#FFFFFF; overflow:auto; margin: 5px 0px 0px 0px">';
		for(var i = 0; i < zip_data.address.length; i++ ){
			var zipcode = '〒' + zip_data.address[i].zipcode.substr(0, 3) + '-' + zip_data.address[i].zipcode.substr(3);
			var prefecture = zip_data.address[i].prefecture;
			var city = zip_data.address[i].city;
			var town = zip_data.address[i].town;
			var house = zip_data.address[i].house_number;
			list += '<div style="cursor: pointer; font-size:80%; margin: 5px 0px 0px 5px" onClick="zs_select(' + i + ');">' + zipcode + ' ' + prefecture + city + town + ' ' + house + '</div>';
		}
		list += '</div></div>';
		if( typeof( zs_ziplist ) != 'undefined' && document.getElementById( zs_ziplist ) != null ){
			document.getElementById( zs_ziplist ).innerHTML = list;
			document.getElementById( 'zip_select' ).focus();
		}
	}
}

// 会員情報
zs_site_id = '10017';

function zipsearch_delivery(){
  zs_zipcode_first = 'delivery_zip1';
  zs_zipcode_last = 'delivery_zip2';
  zs_prefecture = 'delivery_prefecture';
  zs_address = 'delivery_address';
  zs_city	= 'delivery_addr1';
  zs_town = 'delivery_addr2';
  zs_ziplist = 'delivery_ziplist';
  zs_search();
}

function zipsearch_member(){
  zs_zipcode_first = 'address_zip1';
  zs_zipcode_last = 'address_zip2';
  zs_prefecture = 'prefecture';
  zs_address = 'address';
  zs_ziplist = 'ziplist';
  zs_city	= 'm_addr1';
  zs_town = 'm_addr2';
  zs_search();
}

function zipsearch_present(){
  zs_zipcode_first = 'present_zip1';
  zs_zipcode_last = 'present_zip2';
  zs_prefecture = 'present_pref';
  zs_address = 'present_address';
  zs_ziplist = 'present_ziplist';
  zs_city	= 'present_addr1';
  zs_town = 'present_addr2';
  zs_search();
}

function zipsearch_work(){
  zs_zipcode_first = 'work_zip1';
  zs_zipcode_last = 'work_zip2';
  zs_prefecture = 'work_prefecture';
  zs_address = 'work_address';
  zs_ziplist = 'work_ziplist';
  zs_city	= 'work_addr1';
  zs_town = 'work_addr2';
  zs_search();
}
