var imgObj = null;

function appendScript(src) {
	var obj = document.createElement("script");
	obj.type = "text/javascript";
	obj.src = src;

	document.body.appendChild(obj);
}

function disableSubmit(frm) {
	for (var i = 0; i < frm.elements.length; i++) {
		if ("SUBMIT" != frm.elements[i].type.toUpperCase()) {
			continue;
		}
		if ("BACK" == frm.elements[i].name.toUpperCase()
				|| "BACK_BUTTON" == frm.elements[i].className.toUpperCase()) {
			continue;
		}

		frm.elements[i].disabled = true;
	}
	return true;
}

function addEventEx(elm, event_name, func) {
	if (elm.addEventListener) {
		elm.addEventListener(event_name,func,false);
		return true;
	} else if (elm.attachEvent){
		return elm.attachEvent('on'+event_name,func);
	} else {
		var old_func=elm["on"+event_name];
		if (typeof old_func!="function") {
			elm["on"+event_name] = function(event){func(event)};
		} else {
			elm["on"+event_name] = function(event){old_func(event);func(event)};
		}
	}
}

function showImage(src) {
	var div = document.createElement("div");
	var size = getDocumentSize();
	var scrollPosition = getScrollPosition();

	div.style.width = size['width'] + "px";
	div.style.height = size['height'] + "px";
	div.style.backgroundColor = "white";
	div.style.position = "absolute";
	div.style.top = "0";
	div.style.left = "0";
	div.style.hidden = false;
	div.style.visible = true;
	div.style.display = "block";
	div.id = "show_div";

	var img = document.createElement("img");
	img.src = "./img/loading.gif";
	img.style.position = "absolute";
	img.style.top = (scrollPosition['top'] + 50) + "px";
	img.style.left = ((size['width'] - 100) / 2) + "px";

	div.appendChild(img);

	setOpacity(div, 0.5);

	document.body.appendChild(div);

	imgObj = new Image();
	imgObj.src = src;

	addEventEx(div, "click", function() {closeImg();});
	addEventEx(imgObj, "load", function() {onloadDesignImage();});


}

function onloadDesignImage() {
	var size = getDocumentSize();
	var scrollPosition = getScrollPosition();

	var img = document.createElement("img");
	img.src = imgObj.src;
	img.style.position = "absolute";
	img.style.top = (scrollPosition['top'] + 50) + "px";
	img.style.left = ((size['width'] - imgObj.width) / 2) + "px";
	img.id = "show_img";

	addEventEx(img, "click", function() {closeImg();});
	document.body.appendChild(img);
}

function closeImg() {
	var obj = document.getElementById("show_div");
	if (obj) {
		obj.parentNode.removeChild(obj);
	}
	obj = document.getElementById("show_img");
	if (obj) {
		obj.parentNode.removeChild(obj);
	}
}

//JavaScript ソース
function getClientSize() {
	// ユーザーエージェント
     var ua = navigator.userAgent;
     // 合致した部分の先頭文字の添え字
     var nHit = ua.indexOf("MSIE");
     // IE かどうか
     var bIE = (nHit >=  0);
     // バージョンが 6 かどうか
     var bVer6 = (bIE && ua.substr(nHit+5, 1) == "6");
     // 標準モードかどうか
     var bStd = (document.compatMode && document.compatMode=="CSS1Compat");

     var ret = new Array();
     if (bIE) {
          if (bVer6 && bStd) {
        	  ret['width'] = document.documentElement.clientWidth;
        	  ret['height'] = document.documentElement.clientHeight;
          } else {
        	  ret['width'] = document.body.clientWidth;
        	  ret['height'] = document.body.clientHeight;
          }
     } else {
    	 ret['width'] = window.innerWidth;
    	 ret['height'] = window.innerHeight;
     }

     return ret;
}

function setOpacity(elem, op){
	 // IE6.0, IE7.0
	 elem.style.filter = 'alpha(opacity=' + (op * 100) + ')';
	 // Firefox, Netscape
	 elem.style.MozOpacity = op;
	 // Chrome, Safari, Opera
	 elem.style.opacity = op;
}

function getScrollPosition() {
	var ret = new Array();

	ret['top'] = document.documentElement.scrollTop || document.body.scrollTop;
	ret['left'] = document.documentElement.scrollLeft || document.body.scrollLeft;

	return ret;
}

function getDocumentSize() {
	var ret = new Array();

	ret['height'] = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : 0;
	ret['width'] = (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : 0;

	if (document.body.scrollHeight
			&& ret['height'] < document.body.scrollHeight) {
		ret['height'] = document.body.scrollHeight;
	}

	if (document.body.clientWidth
			&& ret['width'] < document.body.clientWidth) {
		ret['width'] = document.body.clientWidth;
	}

	if (document.documentElement.clientHeight
			&& ret['height'] < document.documentElement.clientHeight) {
		ret['height'] = document.documentElement.clientHeight;
	}

	if (document.body.clientHeight
			&& ret['height'] < document.body.clientHeight) {
		ret['height'] = document.body.clientHeight;
	}
	return ret;
}


function showDialog(urlStr, targetWindow, windowWidth, windowHeiht, windowStyle) {

	argStyle = "";

	if (windowStyle) {
		argStyle = windowStyle;
	} else {
		argStyle = argStyle + "left=0,";
		argStyle = argStyle + "top=0,";
		argStyle = argStyle + "scrollbars=yes,";
		argStyle = argStyle + "toolbar=no,";
		argStyle = argStyle + "status=yes,";
		argStyle = argStyle + "directories=no,";
		argStyle = argStyle + "titlebar=no,";
		argStyle = argStyle + "resizable=yes,";
		argStyle = argStyle + "fullscreen=no,";
	}

	if (windowWidth) {
		argStyle = argStyle + "width=" + windowWidth + "px,";
	} else {
		argStyle = argStyle + "width=400px,";
	}

	if (windowHeiht) {
		argStyle = argStyle + "height=" + windowHeiht + "px,";
	} else {
		argStyle = argStyle + "height=400px,";
	}

	if (!targetWindow) {
		targetWindow = "{{$smarty.const.PROJECT_NAME}}_window";
	}

	w =  window.open(urlStr, targetWindow, argStyle);
	if (w) {
		w.focus();
	}
}


