// global variables
var SITE_DOMAIN_NAME = "www.saesayon.org";
var DEFAULT_ALERT_TITLE = "";

var isIE70 = ( navigator.appVersion.indexOf("MSIE 7") != -1 ) ? true : false;
//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------

/**
 * html, title, width, height, scroll parameter can remove sequentially
 * modalDialog(msg)
 * modalDialog(msg,html)
 * modalDialog(msg,html, title, width, height).......
 */
function modalConfirm(msg, html, title, width, height, scroll) {

	if (window.navigator.appName != "Microsoft Internet Explorer") {
		return confirm(msg);
	} else {
		var args = new Array();
		args["msg"] = msg;
		if (html==null||html=="") {
			html = "/common/confirm.html";
		}
		if (title==null||title=="") {
			args["title"] = DEFAULT_ALERT_TITLE;
		} else {
			args["title"] = title;
		}
		if (width==null||width=="") {
			width = "368";
		}
		if (height==null||height=="") {
			height = "278";
		}
		if (scroll==null||scroll=="") {
			scroll = "no";
		}
		return window.showModalDialog(html, args, "dialogWidth:"+width+"px;dialogHeight:"+height+"px; edge:sunken; center:yes; resizable:no; status:no; help:no; scroll:"+scroll+";");
	}
}

/**
 * focus to first form element
 */
function setBasicFocus() {
	if (document.forms.length > 0) {
		var field = document.forms[0];
		for (i = 0; i < field.length; i++) {
			if ((field.elements[i].type == "text")
			 || (field.elements[i].type == "textarea")
			 || (field.elements[i].type.toString().charAt(0) == "s")) {
				document.forms[0].elements[i].focus();
				break;
			}
		}
	}
}

function isCookie(name) {
	var acookie = document.cookie;
	var op = 0;
	var tempStr="";
	if (acookie.length > 0) {
		var cc = acookie.split(";");
		for(var i=0;i<cc.length;i++){
			tempStr = cc[i].replace();
			if(cc[i].match(name+"=done"))op = op + 1;
		}
	}
	if (op == 0) {
		return false;
	} else {
		return true;
	}
}

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(name,value,expiredays)
{
    var todayDate = new Date();
    todayDate.setDate( todayDate.getDate() + expiredays );

	if (getCookie(name + 'sEP') != "")	{
        deleteCookie(name);                         
        deleteCookie(name + 'sEP');                 
        document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + getCookie(name + 'sEP') + ";"
		document.cookie = name + "sEP=" + escape(getCookie(name + 'sEP')) + "; path=/; expires=" + getCookie(name + 'sEP') + ";"
	}
	else {
		document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"
		document.cookie = name + "sEP=" + escape(todayDate.toGMTString()) + "; path=/; expires=" + todayDate.toGMTString() + ";"
	}
}

function deleteCookie(name) {
	if (getCookie(name)) {
		document.cookie = name + "=; path=/; domain=" + SITE_DOMAIN_NAME + "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}


function trim(str) {
           return str.replace(/(^\s*)|(\s*$)/g, "");
      }

function OpenWin(name, url, width, height, top ,left) {

	if ( isIE70 == true ) {
		height += 30;
	}
	var MyLeft; 
	var MyTop;
	
	if (top == undefined)
	{
		MyTop = screen.height/2 - height/2;
	}
	if (left == undefined)
	{
		MyLeft = screen.width/2 - width/2;
	}

	MyLeft = left;
	MyTop = top;

	window.open(url, name, 'left='+MyLeft+',top='+MyTop+',width='+width+',height='+height+',toolbar=no,menubar=no,status=no,scrollbars=no,resizable=no,location=no');
}

function OpenWinScroll(name, url, width, height, top ,left) {

	if ( isIE70 == true ) {
		height += 30;
	}
	var MyLeft; 
	var MyTop;
	
	if (top == undefined)
	{
		MyTop = screen.height/2 - height/2;
	}
	if (left == undefined)
	{
		MyLeft = screen.width/2 - width/2;
	}

	MyLeft = left;
	MyTop = top;

	window.open(url, name, 'left='+MyLeft+',top='+MyTop+',width='+width+',height='+height+',toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=no,location=no');
}

/**
 * focus : pcd,paper
 */
 
function printpaper(pcd,paper) {
	
	OpenWinScroll("popprint","/common/printpaper.do?pcd="+pcd+"&paper="+paper,698,700,100,100);
	
}
function mailpaper(paper) {
	
	OpenWin("popmail","/common/mailpaper.do?paper="+paper,320,317,100,100);
	
}
function pdfview(id,target) {
	target.location.href = "/common/pdfview.do?id="+id;
}
function goCsMail() {
	document.location.href = "/common/csmail.do";
}

var tgs = new Array( 'div','td','tr','p','span');

var szs = new Array( 'xx-small','x-small','small','medium','large','x-large' );
var startSz = 1;

function resizeText( trgt,inc ) {
        if (!document.getElementById) return
        var d = document,cEl = null,sz = startSz,i,j,cTags;
        
        sz += inc;
        if ( sz < 0 ) sz = 0;
        if ( sz > 4 ) sz = 4;
        startSz = sz;
                
        if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

        cEl.style.fontSize = szs[ sz ];

        for ( i = 0 ; i < tgs.length ; i++ ) {
                cTags = cEl.getElementsByTagName( tgs[ i ] );
                for ( j = 0 ; j < cTags.length ; j++ ) {
                	cTags[ j ].style.fontSize = szs[ sz ];
                }
        }
}

function goMainPop(notice,status) {
	window.open("/common/popup.do?notice="+notice,"pop"+notice,status);
}