﻿/*
===============================================================================
* common
===============================================================================
*/
//Trim()
 String.prototype.trim = function()
{
 return this.replace(/(^\s*)|(\s*$)/g, "");
}

// main_image_change
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_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 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_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];}
}

// flashWrite(file array, width, height, ID, background, variable, window mode) 
function flashWrite(url,w,h,id,bg,vars,win){ 

 // flash code
 var flashStr= 
 "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='"+w+"' height='"+h+"' id='"+id+"' align='middle'>"+ 
 "<param name='allowScriptAccess' value='always' />"+ 
 "<param name='movie' value='"+url+"' />"+ 
 "<param name='FlashVars' value='"+vars+"' />"+ 
 "<param name='wmode' value='"+win+"' />"+ 
 "<param name='menu' value='false' />"+ 
 "<param name='quality' value='high' />"+ 
 "<param name='bgcolor' value='"+bg+"' />"+ 
 "<embed src='"+url+"' FlashVars='"+vars+"' wmode='"+win+"' menu='false' quality='high' bgcolor='"+bg+"' width='"+w+"' height='"+h+"' name='"+id+"' align='middle' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />"+ 
 "</object>"; 

 // output flash 
 document.write(flashStr); 

} 

// image pop up
function view(what) { 
var imgwin = window.open("",'WIN','scrollbars=no,status=no,toolbar=no,resizable=0,location=no,menu=no,width=10,height=10'); 
imgwin.focus(); 
imgwin.document.open(); 
imgwin.document.write("<html>\n"); 
imgwin.document.write("<head>\n"); 
imgwin.document.write("<title>:: RemoteCall Image View ::</title>\n"); 

imgwin.document.write("<sc"+"ript>\n"); 
imgwin.document.write("function resize() {\n"); 
imgwin.document.write("pic = document.il;\n"); 
//imgwin.document.write("alert(eval(pic).height);\n"); 
imgwin.document.write("if (eval(pic).height) { var name = navigator.appName\n"); 
imgwin.document.write(" if (name == 'Microsoft Internet Explorer') { myHeight = eval(pic).height + 40; myWidth = eval(pic).width + 12;\n"); 
imgwin.document.write(" } else { myHeight = eval(pic).height + 9; myWidth = eval(pic).width; }\n"); 
imgwin.document.write(" clearTimeout();\n"); 
imgwin.document.write(" var height = screen.height;\n"); 
imgwin.document.write(" var width = screen.width;\n"); 
imgwin.document.write(" var leftpos = width / 2 - myWidth / 2;\n"); 
imgwin.document.write(" var toppos = height / 2 - myHeight / 2; \n"); 
imgwin.document.write(" self.moveTo(leftpos, toppos);\n"); 
imgwin.document.write(" self.resizeTo(myWidth, myHeight);\n"); 
imgwin.document.write("}else setTimeOut(resize(), 100);}\n"); 
imgwin.document.write("</sc"+"ript>\n"); 

imgwin.document.write("</head>\n"); 
imgwin.document.write('<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bgcolor="#FFFFFF">\n'); 

imgwin.document.write("<img border=0 src="+what+" xwidth=100 xheight=9 name=il onload='resize();' onClick='self.close();' style='cursor:hand;' alt='close' align='center'>\n"); 
imgwin.document.write("</body>\n"); 
imgwin.document.close(); 

}

//
function setCookie( name, value, expiredays )
{
    var todayDate = new Date();
    todayDate.setDate( todayDate.getDate() + expiredays );
    document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";" + domainstr
}

function GetCookie (name) {  
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;   
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
        return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;   
    }
    return null; 
}

// 
function winOpen (doc, wname, top, left, width, height) {
  window.open(doc, wname, 'scrollbars=no,resizable=yes,top='+top+',left='+left+',width='+width+',height='+height);
}

function winOpen2 (doc, wname, top, left, width, height) {
  window.open(doc, wname, 'scrollbars=yes,resizable=no,top='+top+',left='+left+',width='+width+',height='+height);
}

// string empty CHECK 
function isempty(s) {   
    return ((s == null) || (s.length == 0))
}

function isValidlength(date_val, len, len2) {
    return ((date_val.length >= len) && (date_val.length <= len2));
}

function currency(obj) {
  	if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode == 13) || (event.keyCode == 45) || (event.keyCode == 40) || (event.keyCode == 41)) {
  	} else {
  		event.returnValue = false
  	}
}

function objDetectBrowser()
{
	var strUA, s, i;
	this.isIE = false;		// 인터넷 익스플로러인지를 나타내는 속성
	this.isNS = false;		// 넷스케이프인지를 나타내는 속성
	this.version = null;	// 브라우저 버전을 나타내는 속성

	// Agent 정보를 담고 있는 문자열.
	// 이 값이 궁금한 사람은 alert 문을 이용하여 strUA 값을 확인하기 바란다!
	strUA = navigator.userAgent;

	s = "MSIE";

	// Agent 문자열(strUA) "MSIE"란 문자열이 들어 있는지 체크
	if ((i = strUA.indexOf(s)) >= 0)
	{
		this.isIE = true;
		// 변수 i에는 strUA 문자열 중 MSIE가 시작된 위치 값이 들어있고,
		// s.length는 MSIE의 길이 즉, 4가 들어 있다.
		// strUA.substr(i + s.length)를 하면 strUA 문자열 중 MSIE 다음에
		// 나오는 문자열을 잘라온다.
		// 그 문자열을 parseFloat()로 변환하면 버전을 알아낼 수 있다.
		this.version = parseFloat(strUA.substr(i + s.length));
		return;
	}

	s = "Netscape6/";
	// Agent 문자열(strUA) "Netscape6/"이란 문자열이 들어 있는지 체크
	if ((i = strUA.indexOf(s)) >= 0)
	{
		this.isNS = true;
		this.version = parseFloat(strUA.substr(i + s.length));
		return;
	}

	// 다른 "Gecko" 브라우저는 NS 6.1로 취급.
	s = "Gecko";
	if ((i = strUA.indexOf(s)) >= 0)
	{
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}

function mousedownPage(e)
{
	div1 = document.getElementById('CalendarLayer');
	div1.style.display = 'none';
	div1.obj = null;
}

function checkData(obj,DataType,IsEssential,MaxLen,msg){	
   	if(!obj){
   		alert("Wrong Object - "+msg);
   		return false;
   	}
   	  
   	var sVal=obj.value;
	//alert(obj.name+"//"+obj.type+"//"+obj.length+"//"+sVal);
   	
   	DataType=DataType.toUpperCase();
   	IsEssential=IsEssential.toUpperCase();   	
   	
   	if(IsEssential=="Y"){
   		if(!checkEssential(sVal,msg)){
   			setFocus(obj);
   			return false;
   		}
   	}
   	
   	if(DataType=="S"){
   		if(MaxLen){
   			if(!checkMaxLen(sVal,MaxLen,msg)){
   				setFocus(obj);
   				return false;
   			}
   		}
   	}
   	
   	if(DataType=="N"){   		
   		if(!checkNumeric(sVal,msg)){
   			setFocus(obj);
   			return false;
   		}   		
   	}
   	
   	if(DataType=="E" && sVal!=""){
   		if(!checkEmailString(sVal,msg)){
   			setFocus(obj);
   			return false;
   		}   		
   	}

	if(DataType=="P" && sVal!=""){
   		if(!checkPhoneString(sVal,msg)){
   			setFocus(obj);
   			return false;
   		}   		
   	}
   	
   	return true;
}

//값입력여부 체크
//입력값있으면 true,없으면 false
function checkEssential(str,msg){
	if(!str){
		alert('Input '+msg+'!');
		return false;
	}else{
		return true;	
	}
}

//숫자여부 체크
//숫자이면 true,아니면 false
function checkNumeric(str,msg){
	str=removeComma(str);
	
	if(isNaN(str)){
		alert('Wrong '+msg+'!');
		return false;
	}else{
		return true;	
	}
}

//유효한 이메일주소인지 확인
//s:이메일 주소
function checkEmailString(s,msg){
    var mailexp = /[a-z0-9]{2,}@[a-z0-9-]{2,}\.[a-z0-9]{2,}/i; 
    if(!mailexp.test(s)){
    //	if(!/^[\w\.-]+@\w+(\.\w+)$/.test(s)){
		alert('Wrong '+msg);
		return false;
	}
	return true;
}

//유효한 전화번호인지 확인
//s:전화번호
function checkPhoneString(s,msg){
	var pattern=/^0\d{1,2}-\d{3,4}-\d{4}$/;
	if(!s.match(pattern)){
		alert('Wrong '+msg);
		return false;
	}
	return true;
}

//최대길이 체크
//입력값이 최대길이를 초과하면 false
function checkMaxLen(str,MaxLen,msg){
	if(parseInt(getLength(str))>parseInt(MaxLen)){
		//alert("["+msg+"] 한글은 "+(MaxLen/2)+"자, 영문.숫자.공백은 "+MaxLen+"자를 초과할수 없습니다.");
		return false;
	}else{
		return true;	
	}
}

//문자열길이 구하기(한글은 2자리로 계산)
function getLength(str){
	if(str==""){return 0;}
	
	var len=0;	
	
   	for(var i=0;i<str.length;i++){
     	var chr=str.charCodeAt(i);
     	
		if(chr>0 && chr<255){
			len=len+1;
		}else{
			len=len+2;
		}
   }
   
   return len;
}

//컨트롤에 포커스주기
function setFocus(obj){
	if(!obj){return;}
	if(obj.disabled==true){return;}
	if(obj.type=="hidden"){return;}

	if(obj.length!=null){
		obj[0].focus();
	}else{
		obj.focus();
	}
}

//콤마(,) 제거
function removeComma(val){
	while(val.indexOf(",")>=0){
		val=val.replace(",","");
	}
	return val;
}

function ChkString1(psString){
    if(psString == ""){return;}
    //var psString = obj.value;
    var pslen = psString.length;
    for (i=0; i <= pslen -1 ; i++){
		ch = psString.substring(i,i+1);
		if (!(ch >= "a"  && ch <= "z")  && !(ch >= "0"  && ch <= "9") && !(ch == "_") && !(ch=="-")){
			return false;
		}
	}
    for(i =0; i < pslen - 1; i ++){
        psString = psString.replace(/--/g, "");
        if(psString.length != pslen){
            return false;
        }
    }
    return true;
}

function ChkString2(psString){
    if(psString == ""){return 0;}
    var pslen = psString.length;
	
	for(i =0; i < pslen - 1; i ++){
        psString = psString.replace(/--/g, "");
        psString = psString.replace(/%/g, "");
        psString = psString.replace(/'/g, "");
        psString = psString.replace(/"/g, "");
        psString = psString.replace(/</g, "");
        psString = psString.replace(/>/g, "");
        
        if(psString.length != pslen){
            return false;
        }
    }
    return true;
}

function ChkString3(psString){
    if(psString == ""){return;}
    //var psString = obj.value;
    var pslen = psString.length;
    for (i=0; i <= pslen -1 ; i++){
		ch = psString.substring(i,i+1);
		if (!(ch >= "a"  && ch <= "z")  && !(ch >= "A"  && ch <= "Z")  && !(ch >= "0"  && ch <= "9") && !(ch == "_") && !(ch=="-")){
			return false;
		}
	}
    for(i =0; i < pslen - 1; i ++){
        psString = psString.replace(/--/g, "");
        if(psString.length != pslen){
            return false;
        }
    }
    return true;
}

// mouse event block
function mousedown_event(e) { 
    evt = e || event;
    try{
        if (document.all){
            if(evt.button == 2 || evt.button == 3) {     
                event_stop_check(evt);
                return false;
            }
        }else{
            if(evt.which == 3 || evt.which == 2) {   
                event_stop_check(evt);
                return false; 
            }
        }
    }catch(ex){
        return false;
    }
} 
//keyboard event block
function keydown_event(e){ 
    evt = e || event;
    try{
        var key_value = event_stop_check(evt);
        return key_value;
    }catch(ex){
        return false;
    }
}
// event stop check
function event_stop_check(evt){ 
    try{
        if(window.event){
            var result = window.event.keyCode;
            if(!((result>=48 && result<=57) || result==8 || result==45 || result==46 || result==189)){
                return false;
            }
        }else{
            var result = evt.which;
            if(!((result>=48 && result<=57) || result==8 || result==45 || result==46 || result==109)){
                evt.preventDefault();
                return false;
            }
        }
    }catch(ex){
        alert(ex.message);
    }
}

//event check
function event_check(this_s){
    if(!this_s){this_s = window.document;}
    if(document.attachEvent){
        this_s.attachEvent("onkeydown", keydown_event );
        this_s.attachEvent("onmousedown", mousedown_event );
        this_s.attachEvent("onselectstart", event_stop_check ); 
        this_s.attachEvent("ondragstart", event_stop_check ); 
        this_s.attachEvent("oncontextmenu", event_stop_check );
    }else{
        window.captureEvents(Event.MOUSEDOWN); 
        window.captureEvents(Event.ONKEYDOWN); 
        window.captureEvents(Event.ONCONTEXTMENU); 
        this_s.addEventListener("keydown", keydown_event , true);
        this_s.addEventListener("mousedown", mousedown_event , false);
        // this_s.addEventListener("dragstart", event_stop_check , false);  //FF Not Support
        // this_s.addEventListener("selectstart", event_stop_check , false);   //FF Not Support
        //style sheet support
        // window.document.body.style.MozUserFocus='ignore'; 
        window.document.body.style.MozUserInput='disabled';
        window.document.body.style.MozUserSelect='none';
        this_s.addEventListener("contextmenu", event_stop_check , false);
    }
}

