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_swapImage_1(id, newImages, changedId, changedImages) {
	//id=要顯色的物件,newImages=要顯色的圖檔,changedId=被暗色的物件,changedImages=被暗色的圖檔
		document.getElementById(id).src=newImages;
		document.getElementById(changedId).src=changedImages;
	}

function MM_swapImgRestore_1(id, newImages, changedId, changedImages) {
	document.getElementById(id).src=newImages;
	document.getElementById(changedId).src=changedImages;
}

function isEmpty(val) {
	if (val == null) {
		return true;
	} else {
		val = trim(val);
		if (val.length == 0) {
			return true;
		}      
	}  //if (val == null)
	return false;
}  //isEmpty()

function trim(str)
{
	return str.replace(/(^\s*)|(\s*$)/g,"");
}
/*
function (name){
	var datas = document.cookie.split(";");
	for( var i = 0 ; i < datas.length ; i++ )
	{
		var values = trim(datas[i]).split("=",2);
		if( values[0] == name )
			return values[1];
	}
}
*/
function getLoingCookie()
{
	var name = "otpw";
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 )
	{
		var end = document.cookie.indexOf( "=", len );
		if ( end == -1 )
		{
			end = document.cookie.length;
		}
	}
	return unescape( document.cookie.substring( len, end ) );
}
function loginCheck(path){
	if(getLoingCookie()==null)
	{
		//登入前
		document.write('<a ');
		document.write('target="_top" href="'+path+'/login/login.jsp" class="link">登入</a>');
	}else{
		//登入後
		document.write ('<a ');
		document.write('target="_top" href="'+path+'/login/logout.jsp"  class="link">登出</a>');
	}
}
function DrawImage(ImgD,FitWidth,FitHeight){     
    var image=new Image();image.src=ImgD.src;     
    if(image.width>0 && image.height>0){         
        if(image.width/image.height>= FitWidth/FitHeight){            
            if(image.width>FitWidth){ ImgD.width=FitWidth;ImgD.height=(image.height*FitWidth)/image.width;             
            }else{ ImgD.width=image.width;ImgD.height=image.height; }         
        }else{             
            if(image.height>FitHeight){ ImgD.height=FitHeight;ImgD.width=(image.width*FitHeight)/image.height;             
            }else{ ImgD.width=image.width;ImgD.height=image.height; }         
        }     
    }
}
function send(form,action,method){
	form.method.value=method;
	form.action=action;
	form.submit();
}

function callCloseArea(id, closeAreaId){

	var r = confirm("確定關閉")
	if (r == true){
    	var divId = document.getElementById(id);
		if(undefined != divId){
			divId.style.display = 'none';
		}
	
		$.ajax({
		    type: "POST",
		    url: "main.do",
		    data: 'method=closeArea&closeAreaId=' + closeAreaId,
		    success: function(msg){
		        alert(msg);
		    }
		});
    }else{
    	//return false;
    }
}

function getCookie(name){
	var datas = document.cookie.split(";");
	for( var i = 0 ; i < datas.length ; i++ )
	{
		var values = trim(datas[i]).split("=",2);
		if( values[0] == name )
			return values[1];
	}
	return null;
}
function removeCookie(name,path,domain)
{
    var strCookie=name+"=";
    if(!path) path="/";
    strCookie+="; path="+path;
    if(domain) {
        strCookie+="; domain="+domain;
    }
    strCookie+="; expires=Thu,1 Jan 1970 00:00:01 UTC";
    if(getCookie(name)){
        document.cookie=strCookie;
    }
}
function getUserLoginURL(){
	var channelurl
	if( top != this && document.referrer != null )
	{
		channelurl = document.referrer;
	}
	else
	{
		channelurl = location.href;
	}
	channelurl = channelurl.replace(/&/g,"|");
	channelurl = channelurl.replace(/"/g,"%22");
	channelurl = channelurl.replace(/'/g,"%27");
	return channelurl;
}
function check_email ( emailstring )
{
   var ampIndex = emailstring.indexOf("@");
   var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
   // find a dot in the portion of the string after the ampersand only
   var dotIndex = afterAmp.indexOf(".");
   // determine dot position in entire string (not just after amp portion)
   dotIndex = dotIndex + ampIndex + 1;
   // afterAmp will be portion of string from ampersand to dot
   afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
   // afterDot will be portion of string from dot to end of string
   var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
   var beforeAmp = emailstring.substring(0,(ampIndex));
   var regex = /\;|#|\$|%|\^|&|\*|\(|\)|\+|\\|\/|\?|>|<|\{|\}|\,|\[|\]|\`|\|/;
   // index of -1 means "not found"
   if ((emailstring.indexOf("@") != "-1") &&
      (emailstring.length > 5) &&
      (afterAmp.length > 0) &&
      (beforeAmp.length > 1) &&
      (afterDot.length > 1) &&
      ! (regex.test(emailstring)) ) {
	  return true;
   } else {
      return false;
   }
}

function changCheckBox( fromId, isCheck, name ){
	var form = document.getElementById(fromId);
	if ( form ){
		var chk = form[name];
		if ( chk ){
			if ( chk[0] ){
				for( var i = 0; i < chk.length; i++ ){
					chk[i].checked = isCheck;
				}
			} else {
				chk.checked = isCheck;
			}
		}
	}
}
function changCheckBox( fromId, isCheck, name ){
	var form = document.getElementById(fromId);
	if ( form ){
		var chk = form[name];
		if ( chk ){
			if ( chk[0] ){
				for( var i = 0; i < chk.length; i++ ){
					chk[i].checked = isCheck;
				}
			} else {
				chk.checked = isCheck;
			}
		}
	}
}

function hasChecked( fromId, name ){
	var form = document.getElementById(fromId);
	if ( form ){
		if ( form[name] ){
			if ( form[name][0] ){
				for(var i = 0; i < form[name].length; ++i){
					if ( form[name][i].checked ){
						return true;
					}
				}		
			} else {
				return form[name].checked;
			}
		}
	}
	return false;
}
