﻿function SetObjectTag(val) {
    document.write(val);
}

function URLEncode(s) {
    s = s.replace(/ /g, "%20");
    s = s.replace(/\+/g, "%2B");
    s = s.replace(/&/g, "%26");
    s = s.replace(/=/g, "%3D");
    return s;
}

function exposeRandom(from, to, expose)
{
	var data = new Array();
	var num = 0;
	var checker = true;

	// 순서가 잘못 들어왔을 경우
	if(from > to)
	{
		var temp = to;
		from = temp;
		to = from;
	// 같은 값이 들어온 경우
	}
	else if(from == to)
	{
		return data;
	}

	while(true)
	{
		if(data.length == expose) break;

		num = parseInt(from + Math.random() * to);
		checker = true;
		for(var i=0; i < data.length; i ++)
		{
			if(data[i] == num)
				checker = false;
		}

		if(checker == true)
			data[data.length] = num;
	}

	return data;
}

function RandomExpose(arrayObj, exposeCount) {
    var loop = 0;
    var rand = 0;
    var existCheck = false;
    var exposeDatas = [];


    for (var i = 0; i < arrayObj.length; i++) {
        if (exposeCount <= exposeDatas.length) break;

        loop = 0;
        while (true) {

            if (loop > arrayObj.length) break;
            rand = Math.round(Math.random() * 10) % arrayObj.length;

            existCheck = false;
            for (var j = 0; j < exposeDatas.length; j++) {
                if (exposeDatas[j] == arrayObj[rand]) {
                    existCheck = true;
                }
            }

            if (existCheck == false) {
                exposeDatas[exposeDatas.length] = arrayObj[rand];
                break;
            }
            loop++;
        }
    }

    return exposeDatas;
}

function memberLoginCheck(redirectUrl) {
    var url = "";
    // ie6,7, firefox 2,3 - O :: ie8 - X
    if (redirectUrl == undefined || redirectUrl == "")
        url = "/login/login.aspx?url=" + location.href.toString().replace("&", "^");
    else
        url = redirectUrl;
    
    location.href = url;
}

function makeSrartPage(obj) {

    obj.style.behavior = "url(#default#homepage)";
    obj.setHomePage("http://www.kormedi.com");
}

function makeFavorite() {
    window.external.AddFavorite('http://www.kormedi.com', '코메디닷컴-건강을 위한 정직한 지식');
}

function fStrCut(data, len, tail) {
    data = data.replace(/&quot;/gi, "");
    data = data.replace(/&#39;/gi, "");

    // utf8
    return data.length > len ? data.substring(0, len) + tail : data;
    // ansi type
    // return fStrLen(data, len, tail);
}

function fStrLen(str, len, tail) {

    if (!tail) {
        tail = "";
    }
    var l = 0, c = 0, l2 = 0, u = "", s = "";
    if (len > 0) {
        l2 = len;
    }

    for (var i = 0; u = str.charCodeAt(i); i++) {
        if (u > 127) {
            l += 2;
        } else {
            l++;
            if (l2) {
                s += str.charAt(i);
                if (l >= l2) {
                    if (l > l2) {
                        s = s.slice(0, -1);
                    }
                    return s + tail;
                }
            }
        }
    }
    return l2 ? s : l;
}

function imgresize(imgobj, mx) {
    if (imgobj.width == 0) {
        setTimeout(function() { imgresize(imgobj, mx); }, 100);
    }
    else if (imgobj.width > mx) {
        imgobj.width = mx;
        //alt 태그를 붙입니다.

        imgobj.alt = "클릭하시면 원본이미지를 보실수 있습니다";
        imgobj.onclick = function() { imgenlarge(imgobj.src); };
        imgobj.style.cursor = "pointer";
    }
}

function imgenlarge(imgsource) {
    var hwnd_window = window.open("", "wndPhotoenlarge", "width=800,height=600,resizable=yes,status=yes,scrollbars=yes");
    var phtml = "<html><body><img src=\"" + imgsource + "\" alt='클릭하시면 창을 닫습니다' onclick='window.close();' style='cursor:hand'></body></html>";

    hwnd_window.document.write(phtml);
}

function NumberOnly(obj) {
    obj.value = obj.value.replace(/\D/g, '');
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; } 
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function MM_showHideLayers() { //v9.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3)
        with (document) if (getElementById && ((obj = getElementById(args[i])) != null)) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}

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 setCookie(cookieName, cookieValue, expireDate) {
    var today = new Date();
    //today.setDate( today.getDate() + parseInt( expireDate ) );
    today.setMonth = today.getMonth();
    today.setDate = today.getDate();
    today.setHours(23);
    today.setMinutes(59);
    today.setSeconds(60);

    document.cookie = cookieName + "=" + escape(cookieValue) + "; path=/; expires=" + today.toGMTString() + ";";


}
