/**
 * @package    EC.Dragon
 * @author     yabumaru
 * @copyright  2008 e-Agency
 * @license    MIT-style license.
 * @url        http://www.e-agency.co.jp/
 * @require    prototype.js
 * @version    1.0.0
 */
/* Section1 : Toggle */
/* Toggle */
var toggleSw = new Array();
function execToggle(zone, triangle, key, restore) {
	var name = 'isShow_' + key;

	if (toggleSw[name] == "undefined" || toggleSw[name] == null) {
		toggleSw[name] = 1;
	}

	if (restore == undefined) {
		toggleSw[name] = (toggleSw[name] + 1) % 2;
	} else {
		if (restore == true) {
			toggleSw[name] = eval( getCookie( name ) );
		} else {
			toggleSw[name] = 1;
		}
	}
	if ( isNaN(toggleSw[name]) ) { toggleSw[name] = 1; }
	setCookie(name, toggleSw[name]);

	if (restore != undefined && toggleSw[name] == 1) {
	} else {
		Element.toggle( $(zone) );
		$(triangle).src = (toggleSw[name] == 1) ? '/img/admin/triangleopen.gif' : '/img/admin/triangleclose.gif';
	}
}
/* Get Cookie Value */
function getCookie( key ) {
	key += "=";
	theCookie = document.cookie+";";
	start = theCookie.indexOf(key);
	if (start != -1) {
		end = theCookie.indexOf(";",start);
		return unescape( theCookie.substring(start + key.length, end) );
	}
	return false;
}
/* Set Cookie Value */
function setCookie(key, val) {
	if ((key != null) && (val != null)) {
		expDay = new Date();
		expDay.setTime(expDay.getTime() + (1 * 1000 * 60 * 60 * 24));
		expDay = expDay.toGMTString();
		document.cookie = key + "=" + escape(val) + ";expires=" + expDay;
		return true;
	}
	return false;
}
/* Section2 : Form Style */
window.onload = function() {
	function input_focused(){ this.style.backgroundColor="#ffc"; }
	function input_blured() { this.style.backgroundColor=""; }

	if (document.forms.length) {
		for (i = 0; i < document.forms.length; i++) {
			if ( document.forms[i] == null || typeof(document.forms[i]) != "object" ) {
				continue;
			}
			for(j = 0; j < document.forms[i].elements.length; j++) {
				if ( document.forms[i].elements[j] == null || typeof(document.forms[i].elements[j]) != "object" ) {
					continue;
				}
				if ( "type" in document.forms[i].elements[j] ) {
					if (document.forms[i].elements[j].type == "text") {
						document.forms[i].elements[j].onfocus= input_focused;
						document.forms[i].elements[j].onblur = input_blured;
					}
					if (document.forms[i].elements[j].type == "textarea") {
						document.forms[i].elements[j].onfocus= input_focused;
						document.forms[i].elements[j].onblur = input_blured;
					}
				}
			}
		}
	}
}
/* Section3 : Zip To Address */
function zip2address(res, pref, addr01, addr02) {
	var json = eval("(" + res + ")");
	if (json['state'] != '') {
		if (confirm('住所欄を以下の内容で置き換えてよろしいですか？\n\n住所：' + json['state'] + ' ' + json['addr01'] + ' ' + json['addr02'])) {
			for (var i in json) {
				if ( i === 'pref' )  { $(pref).value = json[i];   }
				if ( i === 'addr01' ){ $(addr01).value = json[i]; }
				if ( i === 'addr02' ){ $(addr02).value = json[i]; }
			}
		}
	} else {
		alert('該当する住所がみつかりませんでした。');
	}
}
