﻿utils = {};

stringformat = function (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;
}

trim = function (str) {
    str = str.replace(/^\s*/, '').replace(/\s*$/, '');
    return str; 
}


fn_SendTwitter = function (m, u) {
    var url = "http://twitter.com/home?status=" + encodeURIComponent(m) + " " + encodeURIComponent(u);
    var pop = window.open(url, "SendTwitter", "");
    if (pop) pop.focus();
}

fn_SendFaceBook = function (m, u) {
    var url = "http://www.facebook.com/sharer.php?u=" + u + "&t=" + encodeURIComponent(m);
    var pop = window.open(url, "SendFaceBook", "");
    if (pop) pop.focus();
}

fn_ScrapUrl = function () { 
    if (window.clipboardData) {
        window.clipboardData.setData('Text', location.href);
        alert("보고 계신 이벤트의 주소가 복사되었습니다.\nCtrl+V로 붙여 넣으세요.");        
    }
}

fn_Authorization = function () {
    location.href = "/Members/Login?reurl=" + encodeURIComponent(location.href);
}

windowopen = function (u, n, w, h) {
    l = (screen.width) ? (screen.width - w) / 2 : 0;
    t = (screen.height) ? (screen.height - h) / 2 : 0;
    op = stringformat("height={0},width={1},top={2},left={3},scrollbars=yes", h, w, t, l);
    window.open(u, n, op);
}

windowresize = function () {
    //w = document.body.scrollWidth + 22;
    //h = document.body.scrollHeight + 25;
    w = document.body.scrollWidth + 6;
    h = document.body.scrollHeight + 30;
    window.self.resizeTo(w, h);
}

fn_CalTextLen = function (s) {
    var nbytes = 0;
    for (i = 0; i < s.length; i++) {
        var ch = s.charAt(i);
        if (escape(ch).length > 4) { nbytes += 2; }
        else if (ch == '\n') { if (s.charAt(i - 1) != '\r') { nbytes += 1; } }
        else if (ch == '<' || ch == '>') { nbytes += 4; }
        else { nbytes += 1; }
    }
    return nbytes;
}

fn_ChkTextLen = function (text, len) {
    if (fn_CalTextLen(text) > len) return false;
    else return true;
}


function getCookie(name) {
    var Found = false
    var start, end
    var i = 0

    while (i <= document.cookie.length) {
        start = i
        end = start + name.length
        if (document.cookie.substring(start, end) == name) {
            Found = true
            break
        }
        i++
    }

    if (Found == true) {
        start = end + 1
        end = document.cookie.indexOf(";", start)
        if (end < start) end = document.cookie.length

        return document.cookie.substring(start, end)
    }

    return ""
}

function setCookie(name, value, expiredays) {
    var todayDate = new Date();
    todayDate.setDate(todayDate.getDate() + expiredays);

    //document.cookie.domain = "inavi.com"
    document.cookie = name + "=" + escape(value) + "; path=/; expires=" + todayDate.toGMTString() + ";"

    return;
}


function deleteCookie(cookieName) {
    var expireDate = new Date();

    //어제 날짜를 쿠키 소멸 날짜로 설정한다.
    expireDate.setDate(expireDate.getDate() - 1);
    document.cookie = cookieName + "= ; expires=" + expireDate.toGMTString() + "; path=/";
}

popup_setCookie = function (name, value, expiredays) {
    var todayDate = new Date();

    todayDate.setDate(todayDate.getDate() + expiredays);
    document.cookie = name + "=" + escape(value) + "; path=/; expires=" + todayDate.toGMTString() + ";";
    return;
}

closeWin = function (name, obj_id) {
    popup_setCookie(obj_id, 'done', 1);
    closeWinOne(obj_id);
}

closeWinOne = function (obj_id) {
    var obj = document.getElementById(obj_id);
    //obj.style.display = "none";
    $(stringformat("#divPop{0}", obj_id)).remove();
}

fn_CreatePopUp = function (url, name, top, left, width, height) {
    if (getCookie(name) != "done") $(stringformat("#divPop{0}", name)).load(url);
    else $(stringformat("#divPop{0}", name)).remove();
}

fn_cutHtml = function(str) {
    var str = form.org.value.replace(new RegExp("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>", "gim"), "");
    str = str.replace(new RegExp("&nbsp;", "gim"), " ");
    return str;
}  
