// 전역 변수
var errmsg = "";
var errfld;

// 필드 검사
function check_field(fld, msg) 
{
    if ((fld.value = trim(fld.value)) == "") 			   
        error_field(fld, msg);
    else
        clear_field(fld);
    return;
}

// 필드 오류 표시
function error_field(fld, msg) 
{
    if (msg != "")
        errmsg += msg + "\n";
    if (!errfld) errfld = fld;
    fld.style.background = "F2F2F2";
}

// 필드를 깨끗하게
function clear_field(fld) 
{
    fld.style.background = "#FFFFFF";
}

function trim(s)
{
	return s.replace(/^\s+|\s+$/g,"");
}

// 자바스크립트로 PHP의 number_format 흉내를 냄
// 숫자에 , 를 출력
function number_format(data) 
{
	
    var tmp = '';
    var number = '';
    var cutlen = 3;
    var comma = ',';
    var i;
   
    len = data.length;
    mod = (len % cutlen);
    k = cutlen - mod;
    for (i=0; i<data.length; i++) 
	{
        number = number + data.charAt(i);
		
        if (i < data.length - 1) 
		{
            k++;
            if ((k % cutlen) == 0) 
			{
                number = number + comma;
                k = 0;
			}
        }
    }

    return number;
}

// E-Mail check.  From manutd.com
function emailCheck (emailStr) {

	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */

	var checkTLD=1;

	/* The following is the list of known TLDs that an e-mail address must end with. */

	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */

	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] 
	
	! ?# $ % & ?* / = { | }
	*/

	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]\!\?#\$\%\&\?*\/\=\{\|\}";

	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/

	var validChars="\[^\\s" + specialChars + "\]";

	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */

	var quotedUser="(\"[^\"]*\")";

	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */

	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */

	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */

	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user

	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */

	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	/* Finally, let's start trying to figure out if the supplied address is valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {

	/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mould of a valid e-mail address. */

	alert("Email address seems incorrect (check @ and .'s)");
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).

	for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
	alert("Ths username contains invalid characters.");
	return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	alert("Ths domain name contains invalid characters.");
	return false;
	   }
	}

	// See if "user" is valid 

	if (user.match(userPat)==null) {

	// user is not valid

	alert("The username doesn't seem to be valid.");
	return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {

	// this is an IP address

	for (var i=1;i<=4;i++) {
	if (IPArray[i]>255) {
	alert("Destination IP address is invalid!");
	return false;
	   }
	}
	return true;
	}

	// Domain is symbolic name.  Check if it's valid.
	 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	alert("The domain name does not seem to be valid.");
	return false;
	   }
	}

	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	alert("The address must end in a well-known domain or two letter " + "country.");
	return false;
	}

	// Make sure there's a host name preceding the domain.

	if (len<2) {
	alert("This address is missing a hostname!");
	return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}




// popup
function popup_window(url, winname, opt)
{
    window.open(url, winname, opt);
}


// 큰이미지 창
function popup_large_image(it_id, width, height)
{
	var top = 10;
	var left = 10;
	url = "large_image.php?it_id=" + it_id;
	width = width + 50;
	height = height + 100;
	opt = 'scrollbars=no,width='+width+',height='+height+',top='+top+',left='+left;
	popup_window(url, "largeimage", opt);
}




function intro_large_image(game_num, width, height)
{
	var top = 10;
	var left = 10;
	url = "large_image.php?game_num=" + game_num;
	width = width + 20;
	height = height + 100;
	opt = 'scrollbars=no,width='+width+',height='+height+',top='+top+',left='+left;
	popup_window(url, "largeimage", opt);
}




// , delete
function no_comma(data)
{
	var tmp = '';
    var comma = ',';
    var i;

	for (i=0; i<data.length; i++)
	{
		if (data.charAt(i) != comma)
		    tmp += data.charAt(i);
	}
	return tmp;
}

// 삭제 검사 확인
function del(href) 
{
    if(confirm("정말 삭제 하시겠습니까?")) 
        document.location.href = href;
}

// 쿠키 입력
function set_cookie(name, value, expirehours) 
{
	var today = new Date();
	today.setTime(today.getTime() + (60*60*1000*expirehours));
	document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + today.toGMTString() + ";";
}

// 쿠키 얻음
function get_cookie(name) 
{
    var find_sw = false;
    var start, end;
    var i = 0;

	for (i=0; i<= document.cookie.length; i++)
	{
		start = i;
		end = start + name.length;

		if(document.cookie.substring(start, end) == name) 
		{
			find_sw = true
			break
		}
	}

    if (find_sw == true) 
	{
        start = end + 1;
        end = document.cookie.indexOf(";", start);

        if(end < start)
            end = document.cookie.length;

        return document.cookie.substring(start, end);
    }
    return "";
}

// 쿠키 지움
function delete_cookie(name) 
{
	var today = new Date();

	today.setTime(today.getTime() - 1);
	var value = getCookie(name);
	if(value != "")
		document.cookie = name + "=" + value + "; path=/; expires=" + today.toGMTString();
}


function MemoWin() {
	window.open('/memo/memo_list.php?case=get','STMemo','scrollbars=yes,width=478,height=420');
}

function MWMem(mid) {
	window.open('','STMemo','scrollbars=yes,width=478,height=420');
	document.STMemoF.memo_id.value = mid;
	document.STMemoF.target = "STMemo";
	document.STMemoF.action = "/memo/memo_write.php";
	document.STMemoF.submit();
}

function MWMem2(mno) {
	window.open('','STMemo','scrollbars=yes,width=478,height=420');
	document.STMemoF.memo_no.value = mno;
	document.STMemoF.target = "STMemo";
	document.STMemoF.action = "/memo/memo_write.php";
	document.STMemoF.submit();
}



// 문자 byte 값 알아내기.
function getByteLength(data) {
    var len = 0;
    var str = data.substring(0);

    if ( str == null ) return 0;

    for(var i=0; i < str.length; i++) {
        var ch = escape(str.charAt(i));

        if( ch.length == 1 ) len++;
        else if( ch.indexOf("%u") != -1 )  len += 2;//Db가 한글을 3byte로 인식하여 2->3
        else if( ch.indexOf("%") != -1 ) len += ch.length/3;
    }

    return len;
}


// 특수 문자 못하게
function NoSpecialChar(data) {
	for (var i = 0; i < data.length; i++) {
		ch_char = data.charAt(i);
		ch = ch_char.charCodeAt();
		if ( (ch >= 33 && ch <= 47) || (ch > 58 && ch <= 64) || (ch >= 91 && ch <= 96) || (ch >= 123 && ch <= 126)) {
			alert("문자 " + ch_char + "를 사용할 수 없습니다.");
			exit;
		}
	}
}

function resizeImage(num) {
	var width = document.getElementById('stImage'+num).width;
	if( width > 610 )
		document.getElementById('stImage'+num).width = 610;
}

function stImgWin(imgStr) {
	window.open('/RogpaImage.php?url='+imgStr, 'rogpa_img', 'resizable=yes,width=100,height=100');
}

// 영숫자만 입력
function EnglishNumCheck(str) {
	for(var i=0; i<str.length; i++) {
		if(str.charCodeAt(i) < 48 || (str.charCodeAt(i) > 57 && str.charCodeAt(i) < 65) || (str.charCodeAt(i) > 90 && str.charCodeAt(i) < 97) || str.charCodeAt(i) > 122) {
			alert("영어, 숫자만 쓸 수가 있습니다.");
			return false;
		}
	}
}

// 숫자만 입력
function NumCheck(str) {
	for (var i=0; i<str.length; i++) {
		if (str.charCodeAt(i) < 48 || str.charCodeAt(i) > 57) {
			alert("숫자만 쓸 수가 있습니다.");
			return false;
		}
	}
}


function ContentM(mnum,mHTML) {
	var cmv = document.MWN_form.CMethod.value;
	if (cmv == 0) cmv = '4';
	switch (mnum) {
	case 1:
		if (confirm("텍스트로 변환하면 HTML 정보를 잃게 됩니다.\n변환하겠습니까?")) {
			document.getElementById("WriteCase1").style.display = "block";
			document.getElementById("WriteCase2").style.display = "none";
			document.getElementById("ULayer1").style.display = "block";
			document.getElementById("ULayer4").style.display = "none";
			document.getElementById("Content11").style.display = "none";
			document.getElementById("Content12").style.display = "block";
			document.getElementById("Content21").style.display = "block";
			document.getElementById("Content22").style.display = "none";
			document.getElementById("Content41").style.display = "block";
			document.getElementById("Content42").style.display = "none";
			if (mHTML == 1) {
				document.getElementById("Content31").style.display = "block";
				document.getElementById("Content32").style.display = "none";
			}
			switch (cmv) {
			case '2':
				idSTEditor.document.body.innerHTML = document.getElementById("bi_content2").value;
				document.getElementById("bi_content2").value = STEditor.outputBodyText();
				document.MWN_form.CMethod.value = '1';
				break;
			case '3':
			case '4':
				document.getElementById("bi_content2").value = STEditor.outputBodyText();
				document.MWN_form.CMethod.value = '1';
				break;
			default:
				document.MWN_form.CMethod.value = '1'; break;
			}
		}
		break;
	case 2:
		if (confirm("HTML+텍스트로 변환하면 HTML 일부 잃게 됩니다.\n변환하겠습니까?")) {
			document.getElementById("WriteCase1").style.display = "block";
			document.getElementById("WriteCase2").style.display = "none";
			document.getElementById("ULayer1").style.display = "block";
			document.getElementById("ULayer4").style.display = "none";
			document.getElementById("Content11").style.display = "block";
			document.getElementById("Content12").style.display = "none";
			document.getElementById("Content21").style.display = "none";
			document.getElementById("Content22").style.display = "block";
			document.getElementById("Content41").style.display = "block";
			document.getElementById("Content42").style.display = "none";
			if (mHTML == 1) {
				document.getElementById("Content31").style.display = "block";
				document.getElementById("Content32").style.display = "none";
			}
			switch (cmv) {
			case '1':
				var TEMP_C = document.getElementById("bi_content2").value;
				TEMP_C = TEMP_C.replace(/&/g, "&amp;");
				TEMP_C = TEMP_C.replace(/\"/g, "&quot;");
				TEMP_C = TEMP_C.replace(/>/g, "&gt;");
				TEMP_C = TEMP_C.replace(/</g, "&lt;");
				TEMP_C = TEMP_C.replace(/\r\n/g, "</P>\n<P>");
				TEMP_C = "<P>" + TEMP_C + "</P>";
				document.getElementById("bi_content2").value = TEMP_C;
				document.MWN_form.CMethod.value = '2';
				break;
			case '4':
				document.getElementById("bi_content2").value = idSTEditor.document.body.innerHTML;
				document.MWN_form.CMethod.value = '2';
			case '3':
			default:
				document.MWN_form.CMethod.value = '2';
				break;
			}
		}
		break;
	case 3:
		if (confirm("HTML+텍스트로 변환하면 HTML 일부 잃게 됩니다.\n변환하겠습니까?")) {
			document.getElementById("WriteCase1").style.display = "block";
			document.getElementById("WriteCase2").style.display = "none";
			document.getElementById("ULayer1").style.display = "block";
			document.getElementById("ULayer4").style.display = "none";
			document.getElementById("Content11").style.display = "block";
			document.getElementById("Content12").style.display = "none";
			document.getElementById("Content21").style.display = "block";
			document.getElementById("Content22").style.display = "none";
			document.getElementById("Content41").style.display = "block";
			document.getElementById("Content42").style.display = "none";
			if (mHTML == 1) {
				document.getElementById("Content31").style.display = "none";
				document.getElementById("Content32").style.display = "block";
			}
			switch (cmv) {
			case '1':
				var TEMP_C = document.getElementById("bi_content2").value;
				TEMP_C = TEMP_C.replace(/&/g, "&amp;");
				TEMP_C = TEMP_C.replace(/\"/g, "&quot;");
				TEMP_C = TEMP_C.replace(/>/g, "&gt;");
				TEMP_C = TEMP_C.replace(/</g, "&lt;");
				TEMP_C = TEMP_C.replace(/\r\n/g, "</P>\n<P>");
				TEMP_C = "<P>" + TEMP_C + "</P>";
				document.getElementById("bi_content2").value = TEMP_C;
				document.MWN_form.CMethod.value = '3';
				break;
			case '2':
			case '4':
				document.getElementById("bi_content2").value = idSTEditor.document.body.innerHTML;
				document.MWN_form.CMethod.value = '3';
			default:
				document.MWN_form.CMethod.value = '3';
				break;
			}
		}
		break;
	case 4:
		if (confirm("에디터로 변환하겠습니까?")) {
			
			document.getElementById("WriteCase1").style.display = "none";
			document.getElementById("WriteCase2").style.display = "block";
			document.getElementById("ULayer1").style.display = "none";
			document.getElementById("ULayer4").style.display = "block";
			document.getElementById("Content11").style.display = "block";
			document.getElementById("Content12").style.display = "none";
			document.getElementById("Content21").style.display = "block";
			document.getElementById("Content22").style.display = "none";
			document.getElementById("Content41").style.display = "none";
			document.getElementById("Content42").style.display = "block";
			if (mHTML == 1) {
				document.getElementById("Content31").style.display = "block";
				document.getElementById("Content32").style.display = "none";
			}
			switch (cmv) {
			case '1':
				var TEMP_C = document.getElementById("bi_content2").value;
				TEMP_C = TEMP_C.replace(/&/g, "&amp;");
				TEMP_C = TEMP_C.replace(/\"/g, "&quot;");
				TEMP_C = TEMP_C.replace(/>/g, "&gt;");
				TEMP_C = TEMP_C.replace(/</g, "&lt;");
				TEMP_C = TEMP_C.replace(/[\r\n|\r|\n]/g, "</P>\n<P>");
				TEMP_C = "<P>" + TEMP_C + "</P>";

				idSTEditor.document.body.innerHTML = TEMP_C;
				document.MWN_form.CMethod.value = '4';
				break;
			case '2':
			case '3':
				idSTEditor.document.body.innerHTML = document.getElementById("bi_content2").value;
				document.MWN_form.CMethod.value = '4';
				break;
			default:
				document.MWN_form.CMethod.value = '4';
				break;
			}
		}
		break;
	}
}

function setPng24(obj) {
	obj.width=obj.height=1;
	obj.className=obj.className.replace(/\bpng24\b/i,'');
	obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ obj.src +"',sizingMethod='image');"
	obj.src='';
	return '';
}


