// Edit Data
function editData(script_path, nid, target) {

	if (typeof(target) != 'undefined' && target != '') {
		document.forms["data"].target = target;
	}
	document.forms["data"].elements["nid"].value = nid;
	document.forms["data"].action = script_path;
	document.forms["data"].submit();
	
}

// Delete Data
function deleteData(script_path, nid, target) {

	if (typeof(target) != 'undefined' && target != '') {
		document.forms["data"].target = target;
	}
	if (confirm("削除します。よろしいですか？")) {
		document.forms["data"].elements["nid"].value = nid;
		document.forms["data"].action = script_path;
		document.forms["data"].submit();
	}
	
}

// submit
function doSubmit(formname, action, target) {

	if (typeof(target) != 'undefined' && target != '') {
		document.forms[formname].target = target;
	}
	if (typeof(action) != 'undefined' && action != '') {
		document.forms[formname].action = action;
	}
	
	document.forms[formname].submit();
}

// confirm and Submit
function doConfirmAndSubmit(formname, action, message, target) {

	if (confirm(message)) {
	
		if (typeof(target) != 'undefined' && target != '') {
			document.forms[formname].target = target;
		}
		if (typeof(action) != 'undefined' && action != '') {
			document.forms[formname].action = action;
		}
		
		document.forms[formname].submit();
		
	}
}


// checkbox select all
function selectAll(formname, targetitem, keyitem, flg) {
	
	if (document.forms[formname].elements[targetitem] == undefined) {
		return;
	}

	if (keyitem) {
		flg = document.forms[formname].elements[keyitem].checked;
	}

	if (document.forms[formname].elements[targetitem].length) {
		for (var i = 0; i < document.forms[formname].elements[targetitem].length; i++) {
			document.forms[formname].elements[targetitem][i].checked = flg;
		}
	} else {
		document.forms[formname].elements[targetitem].checked = flg;
	}

}

// zoom image
function imageOpen(url, x, y){
	iX = eval(x) + 20;
	iY = eval(y) + 20;

	window.open(url,"_blank","width=" + iX+ ",height=" + iY + ",resizable=yes,toolbar=no,menubar=no,location=no,status=no,scrollbars=no");
}

// zoom image
function pdfOpen(url){
	window.open(url,"_blank");
}


// ラジオボタンのチェックを外す
function uncheckRadio(radio) {
	for (var i = 0; i < document.forms[0].elements[radio].length; i++) {
		document.forms[0].elements[radio][i].checked = false;
	}
}

/***************************************************************************************
Common Code for Ajax
***************************************************************************************/
var httpObj;
var httpTgID;
var httpCnt;
var httpCntID;

function ajaxNextAction() {
	
	if (!ajaxAction) {
		ajaxResponseText = "";
		ajaxActionObject = ajaxActionArray.pop();
		
		if (ajaxActionObject) {
		
			ajaxAction = ajaxActionObject.action;
			
			var url = ajaxActionObject.url;
			var params = ajaxActionObject.params;
			var mode = ajaxActionObject.mode;
			var func = "";
			
			ajaxCreateHttpObject(url, params, mode, func);
		}
	}
}

function ajaxGetUrl(url, params) {

	try {
		// IEはキャッシュが残るのでタイムスタンプで対策
		var d = new Date();
		var ts = d.getTime();
		
		return url + "?" + params + "&ts=" + ts;
	} catch (e) {
	
	}
	
}

function ajaxCreateHttpObject(url, params, mode, func) {

	try {
	    if (window.XMLHttpRequest) {             // Mozilla, Firefox, Safari, IE7
	        httpObj = new XMLHttpRequest();
	    } else if (window.ActiveXObject) {       // IE5, IE6
	        try {
	            httpObj = new ActiveXObject("Msxml2.XMLHTTP");    // MSXML3
	        } catch(e) {
	            httpObj = new ActiveXObject("Microsoft.XMLHTTP"); // MSXML2まで
	        }
	    } else {
	        httpObj = false;
	    }
	    
		if (httpObj) {
		
			// IEはキャッシュが残るのでタイムスタンプで対策
			var d = new Date();
			var ts = d.getTime();
			
			if (mode == "POST") {
				httpObj.open("POST", url, true);
				httpObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=euc-jp');
				httpObj.send(params);
			} else {
				httpObj.open("GET", url + "?" + params + "&ts=" + ts, true);
				httpObj.send("");
			}
				
			
			httpObj.onreadystatechange = function() {
				
				if (httpObj.readyState == 4) {
					
					clearInterval(httpCntID);
					
					if (httpObj.status == 200) {
						ajaxResponseText = httpObj.responseText;
						setTimeout(ajaxAction + "()", 1);
					} else {
						//alert("サーバとの通信エラーが発生いたしました。(" + httpObj.status + ")\n再度お試しください。");
					}
					
				}
			}
			
			httpCnt = 5;
			httpCntID = setInterval("ajaxTimeOut()", 1000);
			
		} else {
		
			
		}
	} catch(e) {
		alert(e.message);
	}

}



function ajaxTimeOut() {

	try {
		httpCnt--;
		if (httpCnt < 1) {
		
			clearInterval(httpCntID);
			httpObj.abort();
			//alert("サーバとの通信エラーが発生いたしました。再度お試しください。");
	
		}
	} catch(e) {
	
	}
}

