﻿// JScript 파일
/// <reference path="jquery-1.4.2-vsdoc.js" />

function changeNaverAppUrl(selecterString) {
	$(selecterString).each(function (index, element) {
		var url = $.trim($(this).attr("href"));

		if (url != null && url.length > 0 && url.indexOf("(") < 0 && url.indexOf("#") < 0) {
			url = makeNaverAppUrl(url);

			$(this).attr("href", url);

		}
	});
}

function makeNaverAppUrl(url) {
	try {
		if (top.location.host.toLowerCase() == "devapp.somangnote.com") {
			url = makeNaverAppUrlCore(url);
		}
	}
	catch (exception) {
		url = makeNaverAppUrlCore(url);
	}

	return url;
}

function makeNaverAppUrlCore(url) {
	if (url.toLowerCase().indexOf("?isnaverapp") < 0
		|| url.toLowerCase().indexOf("&isnaverapp") < 0) {

		if (url.indexOf("?") > -1) {
			url = url.replace("?", "?isNaverApp=1&");
		}
		else {
			url = url + "?isNaverApp=1";
		}
	}
	return url;
}

function changeLocation(url) {
	location.href = makeNaverAppUrl(url);
}

function showProgressBox() {
	try {
		document.body.style.cursor = "wait";

		var dialogHtml
					= "처리중...";
		$("#commonProgressBox").html(dialogHtml);

		$("#commonProgressBox").dialog("destroy");

		$("#commonProgressBox").attr("title", "처리중...");

		$("#commonProgressBox").dialog({
			modal: true
			, dialogClass: "hidden_phrase"
			//					, beforeclose: function(event, ui) { $("#commonProgressBox").dialog("destroy"); }
		});
	}
	catch (error) { alert(error); }

	return false;
}
function hideProgressBox() {
	try {
		document.body.style.cursor = "default";

		$("#commonProgressBox").dialog("destroy");
	}
	catch (error) { }

	return false;
}

function getDialogPosition(targetObj) {
	var dialogPosition = $(targetObj).offset();
	
	if (self.pageYOffset != null) {
		dialogPosition.top = dialogPosition.top - self.pageYOffset + targetObj.offsetHeight;
		dialogPosition.left = dialogPosition.left - self.pageXOffset;
	}
	else if (document.documentElement != null) {
		dialogPosition.top = dialogPosition.top - document.documentElement.scrollTop + targetObj.offsetHeight;
		dialogPosition.left = dialogPosition.left - document.documentElement.scrollLeft;
	}
	
	return dialogPosition;
}	

function getMenuTop(clickedObj) {
	var top = 0;

	var tmpObj = clickedObj;

	while (tmpObj.tagName != "HTML" && tmpObj.tagName != "BODY") {
		top += tmpObj.offsetTop;
		tmpObj = tmpObj.offsetParent;
	}

	top += clickedObj.offsetHeight;

	return top;
}

function getMenuLeft(clickedObj) {
	var left = 0;

	var tmpObj = clickedObj;

	while (tmpObj.tagName != "HTML" && tmpObj.tagName != "BODY") {
		left += tmpObj.offsetLeft;
		tmpObj = tmpObj.offsetParent;
	}

	return left;
}

function goLoginPage() {
	var loginURL
		= "/Member/LoginForm.aspx?redirectUrl=" + encodeURIComponent(location.href);

	top.location.href = makeNaverAppUrl(loginURL);
}

function goLogOut() {
    top.location.href = makeNaverAppUrl("/Member/LoginForm.aspx?isLogOut=1");
}

function goLoginPageAfterConfirm(message, isCloseWindowIfFalse) {
	if (isCloseWindowIfFalse == null) isCloseWindowIfFalse = false;

	if (confirm(message)) {
		goLoginPage();
	}
	else {
		if (isCloseWindowIfFalse) {
			window.close();
		}
	}
}


function setCookie(name, value, expireDuration, domain) {
	var exDate = new Date();

	var cookieString
		= name + "=" + encodeURIComponent(value) + "; "
		+ "path=/; "

	if (expireDuration != null && expireDuration > 0) {
		exDate.setDate(exDate.getDate() + expireDuration);

		cookieString
			+= "expires=" + exDate.toGMTString() + ";";
	}

	if (domain != null) {
		cookieString
			+= "domain=" + domain + "; "
	}

	document.cookie = cookieString;
}

function getCookie(name) {
	var pattern = name + "=([^;]+)[;]+";
	var re = new RegExp(pattern,"ig");
	re.exec(document.cookie + ";");
	
	return decodeURIComponent(RegExp.$1);
}


function openMsgWindow(receiverID, isReload, rootMsgID) {
	url = "/Common/WriteMessageForm.aspx?receiverID=" + receiverID;
	if (rootMsgID != null && rootMsgID > 0) url += "&rootMsgID=" + rootMsgID;
	if (isReload != null) url += "&isReload=" + isReload;
	
	openWindowCenter(url, 364, 380, "msgform");
}


function openWindowCenter(openURL, w, h, windowName) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = "height="+h+",width="+w+",top="+wint+",left="+winl

	win = window.open(openURL, windowName, winprops);
}

function openWindowCenterScroll(openURL, w, h, windowName) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = "scrollbars=1,height="+h+",width="+w+",top="+wint+",left="+winl

	win = window.open(openURL, windowName, winprops);
}


function resizePopupWindow(maxWidth, maxHeight) {
	if (maxWidth == null) maxWidth = 1000;
	if (maxHeight == null) maxHeight = 700;

	var width = document.body.scrollWidth + (document.body.offsetWidth - document.body.clientWidth) + 11;
	var height = document.body.scrollHeight + (document.body.offsetHeight - document.body.clientHeight) + 27;

	if (width > 1000) {
		width = maxWidth;
	}
	if (height > 700) {
		height = maxHeight;
	}
	window.resizeTo(width, height);
}

function resizeImageWidth(imgElement, maxWidth) {
	if (imgElement != null && imgElement.width != 0) {
		if (imgElement.width > maxWidth) {

			var newWidth = maxWidth;
			var newHeight = (imgElement.height * newWidth) / imgElement.width;

			imgElement.style.width = newWidth;
			imgElement.style.height = newHeight;
		}
	}
	else {
		var tmpImg = new Image();
		tmpImg.src = imgElement.src;

		imgElement.width = tmpImg.width;
		imgElement.height = tmpImg.height;
	}
}

function resizeImage(imgElement, maxWidth, maxHeight, resizeCount) {
	if (imgElement != null) {
		var tempImg = new Image();

		tempImg.src = imgElement.src;

		if (tempImg.width > maxWidth || tempImg.height > maxHeight) {
			var maxRatio = maxWidth / maxHeight;
			var imgRatio = tempImg.width / tempImg.height;

			//if (tempImg.width > tempImg.height)
			if (maxRatio < imgRatio) {
				var newWidth = maxWidth;
				var newHeight = (tempImg.height * newWidth) / tempImg.width;

				imgElement.width = newWidth;
				imgElement.height = newHeight;
			}
			else {
				var newHeight = maxHeight;
				var newWidth = (tempImg.width * newHeight) / tempImg.height;

				imgElement.width = newWidth;
				imgElement.height = newHeight;
			}
		}
		else if (tempImg.width == 0 || tempImg.height == 0) {
			imgElement.width = maxWidth;
			imgElement.height = maxHeight;
		}
		else {
			imgElement.width = tempImg.width;
			imgElement.height = tempImg.height;
		}

		imgElement.align = "absmiddle";
	}
}


function checkEmail(email) {
	var bReturn = false;

	bReturn = email.search(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/gi) != -1;

	return bReturn;
}



function getRadioValue(chkObj) {
    var radioValue = "";

    if (chkObj.length == null) {
        if (chkObj.checked) {
            radioValue = chkObj.value;
        }
    }
    else {
        for (i = 0; i < chkObj.length; i++) {
            if (chkObj[i].checked) {
                radioValue = chkObj[i].value;
                break;
            }
        }
    }

    return radioValue;
}

function checkAll(chkObj, checked) {
	if (chkObj.length == null) {
		chkObj.checked = checked;
	}
	else {
		for (i = 0; i < chkObj.length; i++) {
			chkObj[i].checked = checked;
		}
	}
}

function getCheckedValueList(chkObj) {
	var idList = "";

	if (chkObj.length == null) {
		if (chkObj.checked) {
			idList = chkObj.value;
		}
	}
	else {
		for (i = 0; i < chkObj.length; i++) {
			if (chkObj[i].checked) {
				idList =
					idList
					+ "," + chkObj[i].value;
			}
		}

		if (idList.length > 0) {
			idList = idList.substring(1);
		}
	}

	return idList;
}


function isChecked(chkObj) {
	var returnValue = false;

	if (chkObj.length == null) {
		returnValue = chkObj.checked;
	}
	else {
		for (var i = 0; i < chkObj.length; i++) {
			if (chkObj[i].checked) {
				returnValue = true;
				break;
			}
		}
	}

	return returnValue;
}



function getStringLength(value) {
	var length = 0;

	for (ixx = 0; ixx < value.length; ixx++) {
		var codeNum = value.charCodeAt(ixx);

		if (codeNum < 128) { length++; }
		else { length = length + 2; }
	}

	return length;
}

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 '';
}



function moveNextElement(checkElement, nextElement, size) {
	if (checkElement.value.length == size) {
		nextElement.focus();
	}
}

function checkChildRadioBox(parentObj) {
	var radioObj = parentObj.getElementsByTagName("input");

	if (radioObj != null) {
		for (var i = 0; i < radioObj.length; i++) {
			if (radioObj[0].type == "radio") {
				radioObj[0].checked = true;
				break;
			}
		}
	}
}

