var iframeload = false;
var secList;
var lastHTML = '';
var lastID = '';
var lastAction = '';
var lastTarget = '';
function isEditor(spcode) {
	if ((spcode == undefined) || (getCookie('personalcode') == spcode) || ((getCookie('user') & 256) == 256)){
		return (((typeof(editor) != "undefined") && ((getCookie('user') & editor) != 0)) || ((getCookie('user') & 256) == 256));
	} else {
		return false;
	}
}
function pushHTML(ID, action) {
	if(lastID == ID){return true};
	if (lastID != ''){
		secdataClose(lastID);
	}
	lastHTML = GetHTML(ID);
	lastID = ID;
	lastAction = document.userForm.action;
	lastTarget = document.userForm.target;
	if (action == undefined){
		document.userForm.action = cgidir + 'filewrite.cgi';
	} else {
		document.userForm.action = action;
	}
	document.userForm.target = 'uploadFrame';
	return false;
}
function editImage(ID, dir, sec, src, filename, action) {
	if (!isEditor()){return}
	if (pushHTML(ID, action)){return}
	var hMes = lastHTML;
	hMes += '<input type="hidden" name="mode" value="image">';
	hMes += '<input type="hidden" name="sec" value="' + sec + '">';
	hMes += '<input type="file" name="' + sec + '">';
	hMes += '<input type="hidden" name="filename" value="' + filename + '"><br>';
	hMes += '<div align="center">回転[';
	hMes += '<input type="radio" name="rotate" value="0" CHECKED>無し';
	hMes += '<input type="radio" name="rotate" value="90">右90度';
	hMes += '<input type="radio" name="rotate" value="180">180度';
	hMes += '<input type="radio" name="rotate" value="270">左90度';
	hMes += ']<br>';
	hMes += '縮小[';
	hMes += '<input type="radio" name="base" value="" onClick="dispType(0)" CHECKED>無し';
	hMes += '<input type="radio" name="base" value="width" onClick="dispType(1)">横';
	hMes += '<input type="radio" name="base" value="height" onClick="dispType(2)">縦';
	hMes += '<input type="radio" name="base" value="max" onClick="dispType(3)">最大';
	hMes += ']';
	hMes += '<input type="text" name="size" maxlength="3" value="100" disabled="true" style="width:30px">';
	hMes += '<input type="radio" name="type" value="%" disabled="true" CHECKED>%';
	hMes += '<input type="radio" name="type" value="px" disabled="true">px';
	hMes += '</div>';
	hMes += '<input type="submit" value="更新" onClick="iframeload=true">';
	hMes += '<input type="button" value="閉じる" onClick="secdataClose(\'' + ID + '\')">';
	hMes += '<iframe name="uploadFrame" style="display: none" onload="reloadCheck()"></iframe>';
	DispHTML(ID, hMes);
}
function reloadCheck(){
	if (iframeload){
		location.reload();
	}
}
function dispType(index) {
	document.userForm['size'].disabled = (index == 0);
	document.userForm['type'][0].disabled = ((index == 0) || (index == 3));
	document.userForm['type'][1].disabled = (index == 0);
	if (index == 3){
		document.userForm['type'][1].checked = true;
	}
}
function editText(ID, dir, sec, action) {
	if (!isEditor()){return}
	if (pushHTML(ID, action)){return}
	loadSecData(dir + 'yg' + nowpage, ID, sec);
}
function loadSecData(file, ID, sec){
	var xmlhttp = createXMLHttp();
	if (xmlhttp){
		xmlhttp.onreadystatechange = function () {
			loadSecDataRes(xmlhttp, file, ID, sec);
		};
    if ( typeof ActiveXObject!="undefined" ) {
			xmlhttp.open('GET', cgidir + file,false);
		} else {
			xmlhttp.open('GET', cgidir + file);
		}
		xmlhttp.send(null);
	} else {
		alert("サーバーにアクセスできませんでした");
	}
}
function loadSecDataRes(xmlhttp, file, ID, sec){
	var i = 0;
	var index = -1;
	var str = '';
	var atr;
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
		secList = xmlhttp.responseText.split('\n');
		while (i < secList.length){
			if (secList[i].indexOf('[' + sec + ']') == 0){
				index = i;
				i++;
				while ((i < secList.length) && (secList[i].indexOf('[') != 0)){
					str += secList[i] + '\n';
					i++;
				}
				break;
			}
			i++;
		}
		if (index >= 0){
			atr = secList[index].split(',');
			var hMes = '';
			switch (atr[2]){
				case 'TEXT':
					hMes += '<input type="text" name="' + sec + '" size="' + atr[3] + '" maxlength="' + atr[4] + '" value="' + str + '">';
					break;
				case 'TEXTAREA':
					hMes += '<textarea name="' + sec + '" cols="' + atr[3] + '" rows="' + atr[4] + '">' + str + '</textarea>';
					break;
			}
			hMes += '<input type="button" value="更新" onClick="secfileSave(\'' + file + '\', \'' + sec + '\')">';
			hMes += '<input type="button" value="閉じる" onClick="secdataClose(\'' + ID + '\')">';
			DispHTML(ID, hMes);
		}
	}
}
function editDBText(ID, tableName, field, name, type, arg1, arg2, key, spcode) {
	if (!isEditor(spcode)){return}
	if (pushHTML(ID)){return}
	loadDBData(ID, tableName, field, name, type, arg1, arg2, key);
}
function loadDBData(ID, tableName, field, name, type, arg1, arg2, key){
	var xmlhttp = createXMLHttp();
	if (xmlhttp){
		xmlhttp.onreadystatechange = function () {
			loadDBDataRes(xmlhttp, ID, tableName, field, name, type, arg1, arg2, key);
		};
   	if ( typeof ActiveXObject!="undefined" ) {
			xmlhttp.open('POST', cgidir + 'selectwsdl.cgi', true);
		} else {
			xmlhttp.open('POST', cgidir + 'selectwsdl.cgi');
		}
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send("table=" + tableName + "&field=" + field + "&where=" + encodeURL("WHERE f_code='" + key + "'") + "&quot=");
	} else {
		alert("サーバーにアクセスできませんでした");
	}
}
function loadDBDataRes(xmlhttp, ID, tableName, field, name, type, arg1, arg2, key){
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
		if (xmlhttp.responseText.substr(0, 6) == 'error:'){
			alert(xmlhttp.responseText.substr(6));
		} else {
			var hMes = '';
			switch (type){
				case 'text':
					hMes += '<input type="text" name="' + name + '" size="' + arg1 + '" maxlength="' + arg2 + '" value="' + xmlhttp.responseText + '">';
					break;
				case 'textarea':
					hMes += '<textarea name="' + name + '" cols="' + arg1 + '" rows="' + arg2 + '">' + xmlhttp.responseText + '</textarea>';
					break;
			}
			hMes += '<input type="button" value="更新" onClick="DBSave(\'' + tableName + '\', \'' + field + '\', \'' + key + '\', \'' + name + '\')">';
			hMes += '<input type="button" value="閉じる" onClick="secdataClose(\'' + ID + '\')">';
			DispHTML(ID, hMes);
		}
	}
}
function DBSave(tableName, field, key, name){
	if (key != ''){
		var xmlhttp = createXMLHttp();
		if (xmlhttp){
			xmlhttp.onreadystatechange = function () {
				dataSaveRes(xmlhttp);
			};
	   	if ( typeof ActiveXObject!="undefined" ) {
				xmlhttp.open('POST', cgidir + 'gethttp.cgi', true);
			} else {
				xmlhttp.open('POST', cgidir + 'gethttp.cgi');
			}
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlhttp.send("host=www.crewcrew.net&para=" + encodeURL("/cgi/execsql.cgi?&execfile=update.sql&table=" + tableName + "&f_code=" + key + "&field=" + field + "&value='" + encodeURL(document.userForm[name].value) + "'"));
		} else {
			alert("サーバーにアクセスできませんでした");
		}
	}
}
function secdataClose(ID){
	DispHTML(ID, lastHTML);
	lastHTML = '';
	lastID = '';
	document.userForm.action = lastAction;
	document.userForm.target = lastTarget;
}
function secfileSave(file, sec){
	var i = 0;
	var str = '';
	while (i < secList.length){
		if (secList[i].indexOf('[' + sec + ']') == 0){
			str += secList[i] + '\n';
			i++;
			str += document.userForm[sec].value + '\n';
			while ((i < secList.length) && (secList[i].indexOf('[') != 0)){
				i++;
			}
			str += secList[i] + '\n';
		} else {
			str += secList[i] + '\n';
		}
		i++;
	}
	secdataSave(file, str);
	if(file != loadfile){
		secdataSave(loadfile, str);
	}
}
function secdataSave(file, str){
	var xmlhttp = createXMLHttp();
	if (xmlhttp){
		xmlhttp.onreadystatechange = function () {
			dataSaveRes(xmlhttp);
		};
	  if ( typeof ActiveXObject!="undefined" ) {
			xmlhttp.open('POST', cgidir + 'filewrite.cgi', false);
		} else {
			xmlhttp.open('POST', cgidir + 'filewrite.cgi');
		}
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send('filename=' + file + '&mode=create&data=' + str);
	} else {
		alert("サーバーにアクセスできませんでした");
	}
}
function dataSaveRes(xmlhttp){
	var i;
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
		if (xmlhttp.responseText == 'OK'){
			location.reload();
		} else {
//			alert(xmlhttp.responseText);
		}
	}
}
function imagetype(id, file, width, height, flag){
	if (isImage(file)){
		DispHTML(id, imageset(id, file, width, height, flag));
	} else {
		DispHTML(id, moovieset(id, file, width, height, flag));
	}
}
function imageset(id, file, width, height, flag){
	if (width != '0'){
		width = ' width="' + width + '"';
	} else {
		width = '';
	}
	if (height != '0'){
		height = ' height="' + height + '"';
	} else {
		height = '';
	}
	return '<img src="' + file + '" border="0"' + width + height + '><br clear="all">';
}
function moovieset(id, file, width, height, flag){
	if (width == '0'){
		width = '480';
	}
	if (height == '0'){
		height = '360';
	}
	return '<embed src="' + file + '" AutoStart="' + flag + '" Loop="false" width="' + width + '" height="' + height + '">';
}

