function userinpuCheck(fobj) {
	if(requiredCheck(fobj)) {
	    return true;
	} else {
		return false;
	}
}

function checkform(fobj) {
	var msg = "";
	
	if(fobj.method.value == "insert") {
		msg = "この内容で登録します。よろしいですか？";
	} else if(fobj.method.value == "update") {
		msg = "この内容で更新します。よろしいですか？";
	} else if(fobj.method.value == "message") {
		msg = "この内容でメッセージを送信します。よろしいですか？";
	}
	
	if(fobj.msg) {
	    msg = fobj.msg.value;
	}

	if(requiredCheck(fobj)) {
		if(window.confirm(msg)){ 
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function loadmessage(method, controllname) {
	document.getElementById('msgbox').innerHTML = "<div class='msgbox'><img src='assets/img/ajax-loader.gif' /></div>";
	var num = Math.floor( Math.random() * (1001) );

	if(method == "insert") {
		setTimeout("timeoutinsert('" + controllname + "')", num);
	} else {
		setTimeout("timeoutupdate('" + controllname + "')", num);
	}
}

function timeoutinsert(controllname) {
	document.getElementById('msgbox').innerHTML = "<div class='msgbox'>" + controllname + "を登録しました。</div>";
}
function timeoutupdate(controllname) {
	document.getElementById('msgbox').innerHTML = "<div class='msgbox'>" + controllname + "を更新しました。</div>";
}

function loaderrormessage(errormsg) {
    alert(errormsg);
	document.getElementById('msgbox').innerHTML = "<div class='msgbox2'><img src='assets/img/ajax-loader2.gif' /></div>";
	var num = Math.floor( Math.random() * (1001) );
	
	setTimeout("createErrormsg('" + errormsg + "')", num);
}

function createErrormsg(errormsg) {
	document.getElementById('msgbox').innerHTML = "<div class='msgbox2'>" + errormsg + "</div>";
}

function msgSubmit(msg, form) {
	if(window.confirm(msg)){ 
		document.getElementById(form).submit();
	} else {
		return false;
	}
}


//チェック処理
function requiredCheck(fobj) {
	var flg = false;
	
	var findex = 0;
	for (i = 0; i < fobj.elements.length; i = i +1){
		if(fobj.elements[i].id.indexOf("_required",0) != -1) {
			if(!fobj.elements[i].value) {
				if(!flg) {
					findex = i;
				}
				flg = true;
				fobj.elements[i].style.backgroundColor="#ffc0cb";
			} else {
				fobj.elements[i].style.backgroundColor="#FFF";
			}
		}
	}
	
	if(flg) {
		alert("必須項目を入力してください。");
		
		try{
		    fobj.elements[findex].focus();
        }catch( e ){
        
        }
		return false;
	} else {
		return true;
	}
}

//チェック処理
function requiredCheck2(fobj) {
	var flg = false;
	
	var findex = 0;
	for (i = 0; i < fobj.elements.length; i = i +1){
		if(fobj.elements[i].id.indexOf("_req2",0) != -1) {
			if(!fobj.elements[i].value) {
				if(!flg) {
					findex = i;
				}
				flg = true;
				fobj.elements[i].style.backgroundColor="#ffc0cb";
			} else {
				fobj.elements[i].style.backgroundColor="#FFF";
			}
		}
	}
	
	if(flg) {
		alert("配送先が異なる場合は、必須入力になります。");
		fobj.elements[findex].focus();
		return false;
	} else {
		return true;
	}
}

//クリア処理
function clearCheck2(fobj) {
	for (i = 0; i < fobj.elements.length; i = i +1){
		if(fobj.elements[i].id.indexOf("_required2",0) != -1) {
			fobj.elements[i].style.backgroundColor="#FFF";
		}
	}
}



//データ削除
function deleteData(controller, id, title) {
	msg = "「" + title + "」　を削除してよろしいですか？";
	
	
    if(window.confirm(msg)){
		//xmlURL
		var xmlurl = "PNC/common/db/" + controller + "Controll.php?mode=delete&argid=" + id;
		new Ajax.Request(xmlurl, {
			method: "POST",
			onComplete:function (req){
				//$('debug').innerHTML = req.responseText;
				//画面リフレッシュ
				document.refresh.submit();
			}
		});
		return true;
	} else {
	    return false;
	}
}

//データ削除
function deleteData_front(controller, id, title) {
	Dialog.confirm("「" + title + "」　を削除してよろしいですか？", {
		windowParameters: {width:300, height:150,className:"alphacube"},
		okLabel: "はい", 
		cancelLabel: "いいえ", 
		ok:function(win){
			//xmlURL
			var xmlurl = "landcms/PNC/db/" + controller + "Controll.php?mode=delete&argid=" + id;
			new Ajax.Request(xmlurl, {
				method: "POST",
				onComplete:function (req){
					//$('debug').innerHTML = req.responseText;
					//画面リフレッシュ
					document.refresh.submit();
				}
			});
			return true;
		},
		cancel:function(win){
			return false;
		}
	});
}


function addfield() {
	// get the reference for the body
	var body = document.getElementsByTagName("body")[0];
	
	var tbl     = document.getElementById("fieldtable");
	var tblBody = document.createElement("tbody");
	
	//チェックボックス
	var row = document.createElement("tr");
	var cell = document.createElement("td");
	var cbox = document.createElement("input");
	cell.setAttribute("align", "center");
	
	/**
	cbox.setAttribute("type", "checkbox");
	cbox.setAttribute("name", "cbox");	
	cell.appendChild(cbox);
	**/
	
	cell.innerHTML = "<input type='checkbox' name='cbox' />";
	row.appendChild(cell);
	
	//フィールドIDテキストエリア
	cell = document.createElement("td");
	cell.setAttribute("align", "center");
	var input = document.createElement("input");
	input.setAttribute("type", "text");
	input.setAttribute("id", "_required");
	input.setAttribute("name", "othertitle[]");
	input.setAttribute("size", "30");
	cell.appendChild(input);
	row.appendChild(cell);
	
	//フィールドNAMEテキストエリア
	cell = document.createElement("td");
	cell.setAttribute("align", "center");
	input = document.createElement("input");
	input.setAttribute("type", "text");
	input.setAttribute("id", "_required");
	input.setAttribute("size", "30");
	input.setAttribute("name", "otherbody[]");
	cell.appendChild(input);
	row.appendChild(cell);
	
	tblBody.appendChild(row);
	tbl.appendChild(tblBody);
}

function deletefield(index) {

	var tbl = document.getElementById('fieldtable');
	
	var ri = tbl.rows.length;
	var removerow = new Array();

	var aa = document.form1.cbox;

	for(i = 0; i < aa.length; i++) {
		if(document.form1.cbox[i].checked) {
			removerow.push(i);
		}
	}


/**
	for(i = 0; i < ri; i++) {
		var checkstr = tbl.rows[i].cells[0].innerHTML;
alert(checkstr);
		//var dd = checkstr.indexOf("CHECKED", 0);
		
		//if(dd > 0) {
			//removerow.push(i);
		//}
	}
**/
	
	if(removerow.length == 0) {
		alert("削除対象のフィールドをチェックしてください");
	}
	
	//配列逆転
	removerow.reverse();
	
	for(i = 0; i < removerow.length; i++) {
		tbl.deleteRow(removerow[i])
	}
	//tbl.deleteRow(tbl.rows[i].rowIndex);
}

//iframe用
function LoadFrame(frid) {
	var IFR = document.getElementById(frid);
	IFR.height = (IFR.contentWindow)?IFR.contentWindow.document.body.scrollHeight:document.frames[frid].document.body.scrollHeight;
	if(IFR.height < 300) {
		IFR.height = 400;
	}
}

function gf_OpenNewWindow(pURL,pName,pSize){
	var wWidth,wHeight;
	var wSize,wFeatures;
	var wLeft,wTop,PositionX,PositionY;

	wWidth = window.screen.availWidth/2;
	wHeight = window.screen.availHeight/2;
	wSize = pSize.split(":");
	wLeft = wSize[0].split("=");
	wTop = wSize[1].split("=");
	PositionX = wWidth-wLeft[1]/2;
	PositionY = wHeight-wTop[1]/2;
	
	wFeatures = wSize+",left="+PositionX+",top="+PositionY;
	
	wWindow = window.open(pURL,pName,wFeatures+",scrollbars=yes,status=yes,resizable=yes");

	wWindow.focus();

}
