function userinpuCheck(fobj) {
	if(requiredCheck(fobj)) {
	    return true;
	} else {
		return false;
	}
}

function checkform(fobj) {
	var msg = "";
	
	if(fobj.method.value == "order") {
	    msg = "注文を確定します。よろしいですか？";
	} else 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='../work/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='../work/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 userinputCheck(fobj) {
	var flg = false;
	flg = requiredCheck(fobj);

	if(flg == true) {
		//メールアドレスチェック
		if(fobj.email.value != fobj.email2.value) {
			alert("メールアドレスとメールアドレス確認の値が異なります。");
			fobj.email.style.backgroundColor="#ffc0cb";
			fobj.email2.style.backgroundColor="#ffc0cb";
			fobj.email.focus();
			flg = false;
 		}

		if(fobj.soufu[1].checked) {
			var sflg = true;
			//〒
			if(!fobj.szip.value) {
				fobj.szip.style.backgroundColor="#ffc0cb";
				fobj.szip.style.backgroundColor="#ffc0cb";
				fobj.szip.focus();
				sflg = false;
			}
			
			if(!fobj.saddress1.value) {
				fobj.saddress1.style.backgroundColor="#ffc0cb";
				fobj.saddress1.style.backgroundColor="#ffc0cb";
				fobj.saddress1.focus();
				sflg = false;
			}

			if(!fobj.saddress2.value) {
				fobj.saddress2.style.backgroundColor="#ffc0cb";
				fobj.saddress2.style.backgroundColor="#ffc0cb";
				fobj.saddress2.focus();
				sflg = false;
			}

			if(!fobj.sname.value) {
				fobj.sname.style.backgroundColor="#ffc0cb";
				fobj.sname.style.backgroundColor="#ffc0cb";
				fobj.sname.focus();
				sflg = false;
			}

			if(!fobj.sname_kana.value) {
				fobj.sname_kana.style.backgroundColor="#ffc0cb";
				fobj.sname_kana.style.backgroundColor="#ffc0cb";
				fobj.sname_kana.focus();
				sflg = false;
			}

			if(!fobj.stel.value) {
				fobj.stel.style.backgroundColor="#ffc0cb";
				fobj.stel.style.backgroundColor="#ffc0cb";
				fobj.stel.focus();
				sflg = false;
			}
			
			if(!sflg) {
				alert("送付先が異なる場合は送付先情報を入力してください。");
				return false;
			}
		}
	}
	

	
	return flg;
}


//チェック処理
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 = "../work/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 checkBusinessAcount(zenid) {
    if(!document.form1.acountid.value) {
        alert("アカウントIDを入力してください");
        document.getElementById("acountid_info").innerHTML = "";
        return;
    }

    //アカウントID
	var acountid = document.form1.acountid.value;

    if(zenid) {
        if(zenid == acountid) {
            return "";
        }
    }
	
	var request = GXmlHttp.create();
	var url = "online_check.php";
		url += "?acountid=" + acountid;
		url += "&mode=business";

	request.open("GET", url, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var rmsg = request.responseText;
			
			if(rmsg) {
			    alert(rmsg);
			    document.getElementById("acountid_info").innerHTML = "<font color=red>" + rmsg + "</font>";
			} else {
			    alert("使用可能です");
			    document.getElementById("acountid_info").innerHTML = "<font color=blue>使用可能です</font>";
			}
		}
	}
	
	request.send(null);
}

//アカウントチェック
function checkBlogerAcount(zenid) {
    if(!document.form1.acountid.value) {
        alert("アカウントIDを入力してください");
        document.getElementById("acountid_info").innerHTML = "";
        return;
    }

    //アカウントID
	var acountid = document.form1.acountid.value;
	
    if(zenid) {
        if(zenid == acountid) {
            return "";
        }
    }
	
	var request = GXmlHttp.create();
	var url = "online_check.php";
		url += "?acountid=" + acountid;
		url += "&mode=bloger";

	request.open("GET", url, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var rmsg = request.responseText;
			
			if(rmsg) {
			    alert(rmsg);
			    document.getElementById("acountid_info").innerHTML = "<font color=red>" + rmsg + "</font>";
			} else {
			    alert("使用可能です");
			    document.getElementById("acountid_info").innerHTML = "<font color=blue>使用可能です</font>";
			}
		}
	}
	
	request.send(null);
}

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();

}


//カタログ選択
function changeBusiness() {
	var bid = document.form1.businessid.value;

	if(bid == 0) {
        document.getElementById("productsbox").innerHTML = "";
        return;
	}
	
	var request = GXmlHttp.create();
	var url = "online_maneger.php?mode=business&businessid=" + bid;

	request.open("GET", url, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			//var xmlDoc = request.responseXML;
			var xmlDoc = GXml.parse(request.responseText);
			var inhtml = "<select name=\"productsid\" id=\"productsid_required\">";

			objects = xmlDoc.documentElement.getElementsByTagName("products");

			if(objects.length == 0) {
			    document.getElementById("productsbox").innerHTML = "商品はありません。";
			    return;
			}
			
			for( var i=0; i < objects.length ; i++ ) {
				var id = objects[i].getElementsByTagName("id")[0].firstChild.nodeValue;
				var name = objects[i].getElementsByTagName("name")[0].firstChild.nodeValue;
				inhtml += "<option value=\"" + id + "\">" + name + "</option>";
			}
			
			inhtml += "</select>";

			document.getElementById("productsbox").innerHTML = inhtml;
		}
	}
	
	request.send(null);
}

//カタログ選択
function changeBusinessFront() {
	var bid = document.form1.businessid.value;

	if(bid == 0) {
        document.getElementById("productsbox").innerHTML = "";
        return;
	}
	
	var request = GXmlHttp.create();
	var url = "system/online_maneger.php?mode=business&businessid=" + bid;

	request.open("GET", url, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			//var xmlDoc = request.responseXML;
			var xmlDoc = GXml.parse(request.responseText);
			var inhtml = "<select name=\"productsid\" id=\"productsid_required\">";

			objects = xmlDoc.documentElement.getElementsByTagName("products");

			if(objects.length == 0) {
			    document.getElementById("productsbox").innerHTML = "商品はありません。";
			    return;
			}
			
			for( var i=0; i < objects.length ; i++ ) {
				var id = objects[i].getElementsByTagName("id")[0].firstChild.nodeValue;
				var name = objects[i].getElementsByTagName("name")[0].firstChild.nodeValue;
				inhtml += "<option value=\"" + id + "\">" + name + "</option>";
			}
			
			inhtml += "</select>";
			document.getElementById("productsbox").innerHTML = inhtml;
		}
	}
	
	request.send(null);
}
