﻿/// <reference path="jquery-1.3.2-vsdoc.js">


function GetScrollTop() {
	return (document.documentElement.scrollTop > document.body.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
}
function GetScrollLeft() {
	return (document.documentElement.scrollLeft > document.body.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
}

function MoveScrollTop() {
	document.documentElement.scrollTop = 0;
}


function GetMousePos(e) {

	var event = e || window.event;

	var mouseX = event.clientX;
	var mouseY = event.clientY;

	var dd = document.documentElement;
	var db = document.body;
	if (dd) {
		mouseX += dd.scrollLeft;
		mouseY += dd.scrollTop;
	} else if (db) {
		mouseX += db.scrollLeft;
		mouseY += db.scrollTop;
	}
	return { x: mouseX, y: mouseY };
};


function GetClickLayerPos(e , layer) {

    var pos = GetMousePos(e);
	
	//마우스 커서와의 갭
	var gap_w = 10;
	var gap_h = 10;
	
	var resx = pos.x;
	var resy = pos.y;

	if (pos.x + $(layer).width() + gap_w > $(document.body).width()) 
		resx = $(document.body).width() - $(layer).width() - gap_w;
	else
		resx = pos.x + gap_w;

	if (pos.y + $(layer).height() + gap_h > $(document.body).height()) 
		resy = pos.y - $(layer).height() - gap_h;
	else
		resy = pos.y + gap_h;

	return { x: resx, y: resy };
}


// flashWrite(파일경로, 가로, 세로, 아이디, 배경색, 변수, 윈도우모드) 
function flush_swf(url, w, h, id, bg, vars, win) {
	// 플래시 코드 정의 
	var flashStr =
"<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='" + w + "' height='" + h + "' id='" + id + "' align='middle'>" +
"<param name='allowScriptAccess' value='always' />" +
"<param name='movie' value='" + url + "' />" +
"<param name='FlashVars' value='" + vars + "' />" +
"<param name='wmode' value='" + win + "' />" +
"<param name='menu' value='false' />" +
"<param name='quality' value='high' />" +
"<param name='bgcolor' value='" + bg + "' />" +
"<embed src='" + url + "' FlashVars='" + vars + "' wmode='" + win + "' menu='false' quality='high' bgcolor='" + bg + "' width='" + w + "' height='" + h + "' name='" + id + "' align='middle' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />" +
"</object>";
	// 플래시 코드 출력 
	document.write(flashStr);
}

function setCookie(name, value, expiredays) {
	var todayDate = new Date();
	todayDate.setDate(todayDate.getDate() + expiredays);
	document.cookie = name + "=" + escape(value) + "; path=/; expires=" + todayDate.toGMTString() + ";"
}

function getCookie(name) {
	var nameOfCookie = name + "=";
	var x = 0;
	while (x <= document.cookie.length) {
		var y = (x + nameOfCookie.length);
		if (document.cookie.substring(x, y) == nameOfCookie) {
			if ((endOfCookie = document.cookie.indexOf(";", y)) == -1)
				endOfCookie = document.cookie.length;
			return unescape(document.cookie.substring(y, endOfCookie));
		}
		x = document.cookie.indexOf(" ", x) + 1;
		if (x == 0)
			break;
	}
	return "";
}

function WindowOpen(url, nm, width, height, scroll, resize, left , top) {

	if (scroll == null) scroll = "auto";
	if (resize == null) resize = "no";

	if (left == null || top == null) {
		left = (window.screen.width - width) / 2;
		top = (window.screen.height - height) / 2;
	}
	
	var status = "height=" + height + ",width=" + width + ",left=" + left + ",top=" + top + ",resizable=" + resize + ",scrollbars=" + scroll + ",status=yes";

	var winAppm = window.open(url, nm, status);
	winAppm.focus();
}


//체크 되어있는 항목의 체크박스를 Reverse 한다.
function CheckBox_AllCheckReverse(clickObj, chkid) {
	var chk = document.getElementsByTagName('INPUT');

	for (i = 0; i < chk.length; i++)
		if (chk[i].type == "checkbox" && chk[i].id.indexOf(chkid) > 0) chk[i].checked = !chk[i].checked;
}

//체크 되어있는 항목의 값들을 반환한다, 
function GetCheckBoxCheckedValues(chkid) {
	var values = "";

	var chk = document.getElementsByTagName('INPUT');

	for (i = 0; i < chk.length; i++) {
		if (chk[i].type == "checkbox" && chk[i].id.indexOf(chkid) > 0 && chk[i].checked)
			values += chk[i].value + ",";
	}

	if (values.length > 0)
		values = values.substring(0, values.length - 1);

	return values;
	
	
}



//이미지의 상태가 모두 다운로드 되었는지 확인해준다.
function CheckImageStateCompleted(Container) {

	if (typeof (Container) == "string") {
		Container = document.getElementById(Container);
	}

	var imgs = Container.getElementsByTagName('IMG');


	
	for (var i = 0; i < imgs.length; i++) {
		var img = imgs[i];		
		if (img.readyState != "complete") {
			return false;
		}
	}
	return true;
}


//객체하위의 이미지 싸이즈를 제한하고 클릭시 새창으로 보여준다.
function ImageMaxWidthFix(Container, maxWidth) {

	if (maxWidth == 0)
		return;
	
	if (typeof (Container) == "string") {
		Container = document.getElementById(Container);
	}

	if (!CheckImageStateCompleted(Container)) {
		setTimeout(function() { ImageMaxWidthFix(Container, maxWidth); }, 300);
	}
	else {

		var imgs = Container.getElementsByTagName('IMG');
		
		for (var i = 0; i < imgs.length; i++) {
			var img = imgs[i];
			var imgWidth = (img.style.width == "") ? 0 : parseInt(img.style.width.replace("px", ""));
			imgWidth = (img.width > imgWidth) ? img.width : imgWidth
			
			if (imgWidth > maxWidth) {
				img.width = maxWidth;
				img.style.width = maxWidth + "px";
			}
		}
	}
}


function resizeFrame(iframeObj) {


	
	if (typeof (iframeObj) == "string") {
		iframeObj = document.getElementById(iframeObj);
	}

	var doc = iframeObj.contentWindow.document;
	var innerBody = doc.body;
	if(innerBody != null)
	{
		var innerHeight = innerBody.scrollHeight + (innerBody.offsetHeight - innerBody.clientHeight);
		iframeObj.style.height = innerHeight;

		if (CheckImageStateCompleted(doc)) {

			resizeOldEvent = innerBody.onresize;
			innerBody.onresize = function() {
				resizeFrame(iframeObj); resizeOldEvent;
			}

			/* onresize 를 잘 인지하지 못하여. onclick 와 같이 사용함 */
			clickOldEvent = innerBody.onclick;
			innerBody.onclick = function() {
				resizeFrame(iframeObj); clickOldEvent;
			};
		}
		else {
			setTimeout(function() { resizeFrame(iframeObj); }, 300);
		}
	}
}


function TabMenuChange(obj, isTabOnly) {

	if (obj == null) {
		obj = event.srcElement;
	}
	else if (typeof (obj) == "string") {
		obj = document.getElementById(obj);	
	}
	

	if (isTabOnly == null)
		isTabOnly = false;

	var clickTab;
	var tblMenu;
	var divBody;

	while (true) {
		obj = obj.parentNode;

		if (obj.tagName == "TD") {
			clickTab = obj;
			continue;
		}

		if (obj.tagName == "TABLE") {
			tblMenu = obj;
			break;
		}
	}



	var idx = 0;
	var tabs = tblMenu.getElementsByTagName("TD");
	for (var i = 0; i < tabs.length; i++) {
		if (clickTab == tabs[i]) {
			idx = i
			tabs[i].className = "sel";
		}
		else {
			tabs[i].className = "hide";
		}
	}


	if (!isTabOnly) {

		$(tblMenu).parent().find(".body").children().each(function(i) {
			if (i == idx) {
				$(this).show();
			}
			else {
				$(this).hide();
			}
		});

	}
}


function iframePageChange(id, src) {

	var iframe = document.getElementById(id);

	if (iframe.tmpSrc == 'undefined' || iframe.tmpSrc != src) {
		iframe.src = src;
		iframe.tmpSrc = src;
	}
	else
		resizeFrame(iframe);

}



function QueryString(param) {
	if (document.location.search) {
		var params = document.location.search.substr(1).split('&');
		for (var i = 0; i < params.length; i++) {
			if (params[i].indexOf("=") > 0) {
				var value = params[i].split('=');
				if (value[0] == param) return value[1];
			}
			else {
				if (params[i] == param) return params[i];
			}
		}
	}
	return null;
}


function StringConvertJson(str) {
    return eval('(' + str + ')');
   }



function copyToClipboard(text,msg) {

	if (window.clipboardData) {
		window.clipboardData.setData("Text", text);
	}
	else {
		try {
			netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
			var clipboard = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
			var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
			trans.addDataFlavor("text/unicode");

			var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString)
			str.data = text;
			trans.setTransferData("text/unicode", str, text.length * 2);
			clipboard.setData(trans, null, Components.interfaces.nsIClipboard.kGlobalClipboard);
		}
		catch (e) {
			alert("브라우저가 클립보드 사용을 지원하지 않습니다.");
			return false;
		}
	}

	if (msg != null)
		alert(msg);

	return true; 
}



function StringFormat(text) {
	if (arguments.length <= 1) return text;

	for (var i = 0; i <= arguments.length - 2; i++) {
		text = text.replace(new RegExp("\\{" + i + "\\}", "gi"), arguments[i + 1]);
	}

	return text;
}



function PagePrint(Container) {

	if (typeof (Container) == "string") {
		Container = document.getElementById(Container);
	}

	var W = Container.offsetWidth;        //screen.availWidth;
	var H = Container.offsetHeight;        //screen.availHeight; 
	var features = "menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,width=" + W + ",height=" + H + ",left=0,top=0";
	var PrintPage = window.open("about:blank", Container.id, features);
	PrintPage.document.open();
	PrintPage.document.write("<html><head><link rel='StyleSheet' href='../css/common.css' type='text/css' />\n<title></title>\n</head>\n<body>" + Container.innerHTML + "\n</body></html>");
	PrintPage.document.close();
	PrintPage.document.title = document.domain;
	PrintPage.print(PrintPage.location.reload());
}






function isMobileAccess() {
	var uAgent = navigator.userAgent.toLowerCase();
	var mobilePhones = new Array('iphone', 'ipod', 'android', 'blackberry', 'windows ce', 'nokia', 'webos', 'opera mini', 'sonyericsson', 'opera mobi', 'iemobile');
	for (var i = 0; i < mobilePhones.length; i++) {
		if (uAgent.indexOf(mobilePhones[i]) != -1)
			return true;
	}
	
	return false;
}




function sendTwitter(title, url) {
	var wp = window.open("http://twitter.com/home?status=" + encodeURIComponent(title) + " " + encodeURIComponent(url), 'twitter', '');
	if (wp) {
		wp.focus();
	}
}
function sendMe2Day(title, url, tag) {
	var wp = window.open("http://me2day.net/posts/new?new_post[body]=" + encodeURIComponent(title) + " " + encodeURIComponent(url) + "&new_post[tags]=" + encodeURIComponent(tag), 'me2Day', '');
	if (wp) {
		wp.focus();
	}
}
function sendFaceBook(title, url) {
	var wp = window.open("http://www.facebook.com/sharer.php?u=" + url + "&t=" + encodeURIComponent(title), 'facebook', '');
	if (wp) {
		wp.focus();
	}
}

function goYozmDaum(link, prefix, parameter) {
	var href = "http://yozm.daum.net/api/popup/prePost?link=" + encodeURIComponent(link) + "&prefix=" + encodeURIComponent(prefix) + "&parameter=" + encodeURIComponent(parameter);
	var a = window.open(href, 'yozmSend', 'width=466, height=356');
	if (a) {
		a.focus();
	}
}


function MenuImageOn(id) {
	document.getElementById(id).src = document.getElementById(id).src.replace('_off', '_on');
}




function TabMenu(tab_sel, tab_hide, body) {

	$(tab_sel).click(function() {

		//전부초기화
		$(tab_sel).show();
		$(tab_hide).hide();
		$(body).hide();

		var clickTab = this;
		$(tab_sel).each(function(i) {
			if (this == clickTab) {
				$(this).hide();
				$(tab_hide).filter(":eq(" + i + ")").show();
				$(body).filter(":eq(" + i + ")").show();
			}
		});

		return false;
	});

	$(tab_hide).click(function() {
		return false;
	});


	var isViewIdx = 0;
	tab_hide.each(function(i) {
		if ($(this).css("display") != "none") {
			isViewIdx = i;
			return false;
		}
	});

	$(tab_sel).filter(":eq(" + isViewIdx + ")").trigger("click");
}

function UlTabMenu(Container) {

	if (typeof (Container) == "string")
		Container = $("#" + Container);

	var tab_sel = $(Container).find("ul.tab li.sel");
	var tab_hide = $(Container).find("ul.tab li.hide");
	var body = $(Container).find("div.cont div.contIn");

	TabMenu(tab_sel, tab_hide, body);
}



function rblChkColor(Container, colorCode) {

	if (typeof (Container) == "string")
		Container = $("#" + Container);

	$(Container).find("input").each(function(i) {

		if ($(this).attr("type") != "radio" && $(this).attr("type") != "checkbox") {
			return;
		}

		if ($(this).attr("checked"))
			$(this).next().css("background", colorCode);
		else
			$(this).next().css("background", "");


		$(this).click(function() {

			//초기화
			//$(Container).find("input[type='radio']").next().css("background", "");
			$(this).parent().find("label").css("background", "");

			if ($(this).attr("checked"))
				$(this).next().css("background", colorCode);
			else
				$(this).next().css("background", "");

		});
	});
}



//특정 호텔의 판매대행 싱크를 비동기로 실행한다.
function AgentSellSnyc(id_hotel) {
	$.ajax({
	url: "../Page/AgentSellSnyc.ashx?id_hotel=" + id_hotel
		, async: true
		, dataType: "text"
		, success: function(res) {
		}
	});

}



Number.prototype.Comma = function() {
	if (this == 0) return 0;

	var reg = /(^[+-]?\d+)(\d{3})/;
	var n = (this + '');

	while (reg.test(n)) n = n.replace(reg, '$1' + ',' + '$2');

	return n;
};

// 문자열 타입에서 쓸 수 있도록 format() 함수 추가
String.prototype.Comma = function() {
	var num = parseFloat(this);
	if (isNaN(num)) return "0";

	return num.format();
}; 



