/* =====================================
 * メイン・ユーティリティ
 * Version	1.0.3
 * Update	2010-03-11
===================================== */

// ポップアップ ========================

var open_window = function(href, width, height) {
	if(!width) width = 800;
	if(!height) height = 680;
	var win = window.open(href, 'win', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height);
	win.focus();
	return false;
}

var close_window = function() {
	window.close();
}


// ローディング処理 ====================

var loading = function(id, url, form) {
	loadData(id, url);
	setTimeout("_form_submit('" + form + "')", 1000);
	return false;
}

var _form_submit = function(form_name) {
	var form = eval("document." + form_name);
	form.submit();
}


// =====================================

var display_index = 100;
var dd_start_flag = true;

var openWindow = function(id, url, x, y) {
	loadData(id, url);
	$(id).style.zIndex = display_index++;
	if(dd_start_flag == true) {
		startDragAndDrop(id, x, y);
		dd_start_flag = false;
	}
	return false;
}


var closeWindow = function(id) {
	$(id).innerHTML = "";
	$(id).style.zIndex = -1;
	return false;
}

var loadData = function(id, url) {
	url += (url.match(/\?/) ? "&" : "?");
	url += "cache=" + (new Date()).getTime();
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(httpObj) {
			$(id).innerHTML = httpObj.responseText;
		},
		onFailure: function(httpObj) {
			$(id).innerHTML = "エラーが発生しました";
		}
	});
}


// ドラッグ処理 ========================

DragObj = new Object();
DragObj.dragFlag = false;
DragObj.targetObj = null;
DragID = null;

function startDragAndDrop(divID, x, y) {
	DragID = divID;
	$(DragID).style.position = "absolute";
	$(DragID).style.left = x;
	$(DragID).style.top = y;
	$(DragID).onmousedown = DragObj.dragStart;
	$(DragID).onmouseup = DragObj.dragEnd;
	$(DragID).style.zIndex = display_index++;
	Event.observe(window.document, "mousemove", DragObj.dragProc, false);
}

DragObj.dragStart = function(event) {
	DragObj.dragFlag = true;
	DragObj.targetObj = $(DragID);
	DragObj.offsetX = mouseX - parseInt(DragObj.targetObj.style.left);
	DragObj.offsetY = mouseY - parseInt(DragObj.targetObj.style.top);
	return false;
}

DragObj.dragEnd = function() {
	DragObj.dragFlag = false;
}

DragObj.dragProc = function(event) {
	mouseX = Event.pointerX(event);
	mouseY = Event.pointerY(event);
	if(!DragObj.dragFlag) return;
	DragObj.targetObj.style.left = mouseX - DragObj.offsetX;
	DragObj.targetObj.style.top = mouseY - DragObj.offsetY;
	return false;
}


// =============================================================================

var page_moving = function() {
	var hash = "";
	var pram = window.location.search;
	if(!pram) return false;
	pram = pram.substring(1);
	var prams = pram.split("&");
	for (var i = 0; i < prams.length; i++) {
		var ary = prams[i].split("=");
		var key = ary[0];
		var val = ary[1];
		if(key == "_h") {
			hash = val;
		}
	}
	if(hash) {
		window.location.hash = hash;
	}
}

if(window.addEventListener) {
	window.addEventListener("load", page_moving, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", page_moving);
}


// =============================================================================


var text_focus_animation = "/common/img/form/text_yellow.gif";
var text_focus_yellow = "/common/img/form/text_yellow.gif";
var text_focus_gray = "/common/img/form/text_gray.gif";
var text_focus_pink = "/common/img/form/text_pink.gif";

var text_focus = function() {
	var tag; var i; var obj; var reg;
	tag = document.getElementsByTagName("input");
	for(i = 0; i < tag.length; i++) {
		obj = tag[i];
		if(obj.type == "text" || obj.type == "password") {
			reg = eval("/focus/gi");
			if(reg.test(obj.className)) {
				obj.BGC = obj.style.backgroundColor;
				if(obj.value) {
					obj.style.backgroundColor = "#fff";
				}
				obj.onfocus = text_check_focus;
				obj.onblur = text_check_blur;
			}
		}
	}
	tag = document.getElementsByTagName("textarea");
	for(i = 0; i < tag.length; i++) {
		var obj = tag[i];
		reg = eval("/focus/gi");
		if(reg.test(obj.className)) {
			obj.BGC = obj.style.backgroundColor;
			if(obj.value) {
				obj.style.backgroundColor = "#fff";
			}
			obj.onfocus = text_check_focus;
			obj.onblur = text_check_blur;
		}
	}
}

var text_check_focus = function(event) {
	var e = fwsEvent.get(event);
	if(e.value) {
		e.style.backgroundColor = "#fff";
		e.style.backgroundImage = "none";
	} else {
		e.style.backgroundColor = e.BGC;
		var css = e.className;
		if(css.match(/focus_yellow/i)) {
			e.style.backgroundImage = "url(" + text_focus_yellow + ")";
		} else if(css.match(/focus_gray/i)) {
			e.style.backgroundImage = "url(" + text_focus_gray + ")";
		} else if(css.match(/focus_pink/i)) {
			e.style.backgroundImage = "url(" + text_focus_pink + ")";
		} else {
			e.style.backgroundImage = "url(" + text_focus_animation + ")";
		}
	}
}

var text_check_blur = function(event) {
	var e = fwsEvent.get(event);
	if(e.value) {
		e.style.backgroundColor = "#fff";
	} else {
		e.style.backgroundColor = e.BGC;
	}
	e.style.backgroundImage = "none";
}

if(window.addEventListener) {
	window.addEventListener("load", text_focus, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", text_focus);
}


// =============================================================================

