var agent	= navigator.userAgent.toUpperCase();
var isMsie	= isMsie();
var isMsie6	= (isMsie && (agent.indexOf("MSIE 6") > 0));
var isPopup = isPopup();

function isMsie() {

	if(agent.indexOf("MSIE") < 0			// NETSCAPE & MOZILLA
		|| agent.indexOf("MAC") > 0			// MAC OS
		|| agent.indexOf("OPERA") > 0) {	// OPERA
		return false;
	}
	return true;
}

function isPopup() {
	try { 
		if(opener){
			return true;
		} else {
			return false;
		} 
	} catch (e){ 
		return false; 
	}
}

function pop3_win() {
	var url = "/viewExtMailWin.do";
	var option = "width=850,height=200,status=1,scrollbars=0,resizable=0";
	window.open(url, "pop3_win", option);
}

function imap_win() {
	var url = "/getimap.jsp";
	var option = "top=100,left=100,width=460,height=195,status=yes,scrollbars=yes,resizable=yes";
	window.open(url, "pop3_win", option);
}

function gotoURL(url) {
    this.location = url;
}

function gotoParentURL(url) {
	parent.location = url;
}

/*
 * Desc : Move write-page with parameters
 * Args : [to][type][folder][uid]
 */
function writeMail() {
	var _URL = document.URL;
	var url = "/write.do?";

	var argv = writeMail.arguments;

	if (argv.length > 0 && argv[0] != "") {
		var to = argv[0].replace(/\"\"/g, "\"");
		to = to.replace(/\&quot\;/g, "");
		if (isMsie) {
			url += "to=" + escape(to) +"&";
		} else {
			url += "to=" + encodeURI(to) +"&";
		}
	}
	if (argv.length > 1 && argv[1] != "") {
		url += "type=" + argv[1] +"&";
	}
	if (argv.length > 2 && argv[2] != "") {
		url += "folder=" + argv[2] +"&";		
	}
	if (argv.length > 3 && argv[3] != "") {
		url += "uid=" + argv[3] +"&";
	}
     if (argv.length > 4 && argv[4] != "") {
		url += "forwardingmode=" + argv[4] +"&";
	}
	if (_URL.indexOf("write.do") < 0 
		&& _URL.indexOf("send.do") < 0
		&& _URL.indexOf("Popup.do") < 0) {
		url += "returl="+escape(document.URL)+"&";
		
	}

	if (_URL.indexOf("Popup.do") < 0) {
		this.location = url;
	}
	else {
		opener.location = url;
	}
}

function writeMailPopup() {
    var url= "/writePopup.do";
    window.open(url, 'writePopup', 'location=no,toolbar=no,directories=no,status=no,width=820,height=650,resizable=yes,scrollbars=yes');
}

function help() {
    var url= "/help/" + locale + "/index.htm";
    openSimplePopup(url,700,550,false);
}

function qna() {
    var url= "/tbbs/content_list.do?bbsId=2";
    openSimplePopup(url,505,500,false);
}

function addmbox_win() {
    var url = "/webmail/mailbox_add.jsp";    
    openSimplePopup(url,505,220,false);
}

function printSize(num) {
	if (num > 1024 * 1024) {
    	var pstr = Math.floor((num*10) / (1024*1024));
    	return parseInt((pstr/10)) +"MB";
	} else if (num > 1024) {
    	var pstr = Math.floor((num*10) / (1024));
    	return parseInt((pstr/10)) +"KB";
	} else {
    	return num +"B";
	}
}

function trim(str) {
	return str.replace(/^\s*|\s*$/g,"");
}

function setCookie(name, value, expiredays) {
	var date = new Date();
	date.setDate( date.getDate() + expiredays );
	var pstr = name + "=" + escape(value)
		+ "; path=/; expires=" + date.toGMTString() + ";"
	document.cookie = pstr;
}

function getCookie(cookieName) {
	var str = "";
	allCookies = document.cookie.split('; ');
	for (var i = 0; i < allCookies.length; i++) {
		cookieArray = allCookies[i].split('=');
		if (cookieName == cookieArray[0]) {
			return unescape(cookieArray[1]);
		}
	}
	return str;
}

/**
 * pick out mail part from email string("xxx"<a@a.com>)
 * return : a@a.com
 **/
function get_email(email) {
    if(email.indexOf('<') < 0) {
        return email;
    }

    var s = email.indexOf('<') + 1;
    var e = (email.length - 1);
    return email.substr(s, (e-s));
}

/**
 * pick out name part from email string("xxx"<a@a.com>)
 * return : xxx
 **/
function get_name(email) {
    if(email.indexOf('<') < 0) {
        return "";
    }

    var s = 0;
    var e = email.indexOf('<');
    var name = email.substr(s, e);
    return name.replace(/[\"]/g, "");
}

/**
 * split email addresses str.
 * return email address array.
 * ex: str = "name1"<a@a.com>, "name2"<b@b.com>
 *     return = {""name1"<a@a.com>", ""name2"<b@b.com>"}
 **/
function get_email_array(str) {
	var addr_array = str.split(/\s*[,;\r\n]\s*/);
	var new_array = new Array;

	var j = 0;

	for(var i = 0; i < addr_array.length; i++) {
		var address = addr_array[i];

		// "kim, doyoung" <doyoung@terracetech.com>
		if(address.charAt(0) == '"'
		&& address.indexOf("\"", 1) < 0) {

			if(addr_array[i+1] != null) {
				addr_array[i+1] = address + "," + addr_array[i+1];
				continue;
			}
		}

		new_array[j] = address;
		j++;
	}

	return new_array;
}

function str_realLength(str) {
	var real_length = 0;
	var len = str.length;

	for (i = 0; i < len; i++) {
		ch = str.charCodeAt(i);
		if (ch >= 0xFFFFFF) {
			real_length += 4;
		}
		else if (ch >= 0xFFFF) {
			real_length += 3;
		}
		else if (ch >= 0xFF) {
			real_length += 2;
		}
		else {
			real_length++;
		}
	}

	return real_length;
}


/* ------------------------------------------------------------------
* Renewed Functions 
* ------------------------------------------------------------------*/

function JSONSort(array, pos, how, start, row) {
	if (!how)   how = "asc";
	if (!row)   row = array.length;
	if (!start) start = 0;

	for (var i = start; i < row ; ++i) {
		for (var j = i+1 ; j < row ; ++j) {
			if ((how == "asc" && (array[i][pos] > array[j][pos]))
					|| (how == "desc" && (array[i][pos] < array[j][pos]))) {
				var tmp = array[i];
				array[i] = array[j];
				array[j] = tmp;

			}
		}
	}
}

function toggleDisplay(obj) {
	if (obj) {
		if (obj.style.display == "none") {
			obj.style.display = "";
		} else {
			obj.style.display = "none";
		}
	}
}

/*
 * Move focus to next item
 * - obj: current object
 * - nextID: ID of object to focus
 */
function cntlFocus(curObj, nextObj) {
	if (nextObj == null) return;
	var maxLen = curObj.getAttribute("maxlength");
	if (curObj.value.length >= maxLen) {
		nextObj.focus();
	}
}

function winResize() {	
	var Dwidth,Dheight;
	
    var divEl = document.createElement("div");
    divEl.style.position = "absolute";
    divEl.style.left = "0px";
    divEl.style.top = "0px";
    divEl.style.width = "100%";
    divEl.style.height = "100%";
    document.body.appendChild(divEl);
    
    Dwidth = parseInt(document.body.scrollWidth);
  	Dheight = parseInt(document.body.scrollHeight);
  	
  	if(isMsie6){  		
  		window.resizeTo((Dwidth+(Dwidth-divEl.offsetWidth)),(Dheight+49));
  	} else {
  		window.resizeBy(Dwidth-divEl.offsetWidth, Dheight-divEl.offsetHeight);
  	}
  	  			    
    document.body.removeChild(divEl);	
   
}



function openSimplePopup(url,width,height,resize,xpos,ypos){
	var xp,yp;
	var resizeVal = 0;
	if(xpos){
		xp = xpos;
	} else {
		xp = (screen.width/2)-180;
	}
	
	if(ypos){
		yp = ypos;
	} else {
		yp = (screen.height/2)-200;
	}
	
	if(resize){
		resizeVal = 1;
	}

	window.open(url, "",
        "width="+width+",height="+height+",top="+yp+",left="+xp+",,resizable="+resizeVal);
}

/*
 * Check or Uncheck all checkbox items
 */
function checkAll(obj_checkAll, obj_checkBox) {
	if (!obj_checkBox) {
		return;
	}

	var flag = obj_checkAll.checked ? true : false;

	if (obj_checkBox.length) {
		for (var i = 0; i < obj_checkBox.length; i++) {
			obj_checkBox[i].checked = flag;
		}
	} else {
		obj_checkBox.checked = flag;
	}
}

function checkedCnt(obj) {
    var chkCnt = 0;

    if (!obj) {
        return 0;
    }

    if (obj.length) {
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].checked) {
                chkCnt++;
            }
        }
    }
    else {
        if(obj.checked) {
            chkCnt++;
        }
    }

	return chkCnt;
}

function BottomMove() {
	try {
		var showDiv = document.getElementById("progressLayer");
		windowHeight = document.body.clientHeight;
		windowWidth = document.body.clientWidth;
		scrollY = document.body.scrollTop + 62;
		scrollX = document.body.scrollLeft;
		showDiv.style.top = scrollY;
		showDiv.style.left = scrollX;
		showDiv.style.heigth = windowHeight;
		showDiv.style.width = windowWidth;
	} catch (e) {alert(e);}
}

var defaultProgressText;

function setDefaultProgressText(str){
	defaultProgressText = str
}
function viewProgress(str) {
	
	if(!str){
		document.getElementById("progressText").innerHTML=defaultProgressText;
	} else {
		document.getElementById("progressText").innerHTML=str;
	}	
	
	try {
		var showDiv = document.getElementById("progressLayer");
		
		showDiv.style.display = "";
		//setInterval("BottomMove()",500);
		// BottomMove();
	} catch (e) {alert(e);}
}

function hiddenProgress() {
	try {
		var showDiv = document.getElementById("progressLayer");
		showDiv.style.display = "none";
	} catch (e) {}
}

/* ---------- TOP MENU CONTROL ----------- */
function setTopMenuImg(menuName) {
	var imgSrc = "images/navi_mail_o.gif";		//default: mail

	if (menuName == "mail") {
		imgSrc  = "images/navi_mail_o.gif";
	} else if (menuName == "addr") {
		imgSrc  = "images/navi_addr_o.gif";
	} else if (menuName == "scheduler") {
		imgSrc  = "images/navi_diary_o.gif";
	} else if (menuName == "uconf") {
		imgSrc  = "images/navi_conf_o.gif";
	} else if (menuName == "bbs") {
		imgSrc  = "images/navi_board_o.gif";
	} else if (menuName == "webfolder") {
		imgSrc  = "images/navi_data_o.gif";
	} else if (menuName == "org") {
		imgSrc  = "images/navi_orgChart_o.gif";
	}

	if (!menuName || menuName == "") {
		menuName = "mail";
	}

	var i = "top_" + menuName;
	$(i).src = imgSrc;
}

function goMenu(menuName) {
	var url = "/listMessages.do";

	if (menuName == "mail") {
		url = "/listMessages.do";
	} else if (menuName == "addr") {
		url = "/viewUserAddr.do";
	} else if (menuName == "scheduler") {
		url = "/scheduler.do";
	} else if (menuName == "uconf") {
		url = "/viewUserInfo.do";
	} else if (menuName == "bbs") {
		url = "/tbbs/content_list.do?bbsId=1";
	} else if (menuName == "webfolder") {
		url = "/webfolder/explorer.do";
	} else if (menuName == "org") {
		url = "/viewSystemPer.do";
	} else if (menuName == "localMail") {
		url = hostInfo+"/localmail/localMail.jsp";
	}

	gotoURL(url);
}
/* --------------------------------------- */

function goFolder(folderName) {
	folderName = folderName.toLowerCase();

	if (folderName == "inbox") {
		folder = "Inbox";
	} else if (folderName == "sent") {
		folder = "Sent";
	} else if (folderName == "drafts") {
		folder = "Drafts";
	} else if (folderName == "reserved") {
		folder = "Reserved";
	} else if (folderName == "spam") {
		folder = "Spam";
	} else if (folderName == "trash") {
		folder = "Trash";
	} else if (folderName == "write") {
		gotoURL("/write.do");
		return;
	} else {
		folder = folderName;
	}

	gotoURL("/listMessages.do?folder=" + folder);
}

function keyEvent(evt) {
	var Code;
    if(!isMsie){
     Code = evt.which;
    }else{
     Code = event.keyCode;
    }
    
    return Code;
}

function linecheck(check,str){
	var checkedData=check.checked;
	if(checkedData){
		yellowline("tr_"+str);
	}	else{
		whiteline("tr_"+str);
	}
}

function yellowline(id) {
	if (typeof(id) == "object") {
		chgBgColor(id, '#FFFFCC');
	} else {
		chgBgColor(document.getElementById(id), '#FFFFCC');
	}
}
function grayline(id) {
	if (typeof(id) == "object") {
		chgBgColor(id, '#F6F6F6');
	} else {
		chgBgColor(document.getElementById(id), '#F6F6F6');
	}
}
function whiteline(id) {
	if (typeof(id) == "object") {
		chgBgColor(id, '#FFFFFF');
	} else {
		chgBgColor(document.getElementById(id), '#FFFFFF');
	}
}

function yellowline2(obj) {
    var trObj = (obj.parentNode).parentNode;
    if (obj.checked) {
		chgBgColor(trObj, '#FFFFCC');
    } else {
		chgBgColor(trObj, '#FFFFFF');
    }
}

function chgBgColor(obj, color) {
	obj.style.backgroundColor = color;
}

function ellipsisFolderName(folderName, depth) {
	var viewLength = 10;

	if (depth == 2) {
		viewLength = 8;
	} else if (depth == 3) {
		viewLength = 6;
	}

	if (folderName.length > viewLength) {
		folderName = folderName.substring(0, viewLength);
		folderName += "...";
	}

	return folderName;
}

function resetAdvSearchForm() {
	var f = document.advSearchForm;
	f.opRadio[1].checked = true;
	f.folder.options[0].selected = true;
	f.subject.value = "";
	f.from.value = "";
	f.to.value = "";
	f.since.value = "";
	f.before.value = "";
	f.term.options[0].selected = true;
}
