
/*
함수 checkData()
	-폼데이터를 서브밋하기 전에 데이터 적합성 체크
	-입력 패러미터
		ObjName		:컨트롤 이름(txtTitle...)
		DataType	:문자열/숫자 구분.문자열이면 "S",숫자면 "N"(String/Number)
					 문자열이면 최대길이체크,숫자면 숫자여부체크
		IsEssential	:필수여부.필수입력사항이면 "Y",아니면 "N"
					 "Y"면서 값을 입력하지 않으면 return false
		MaxLen		:문자열일경우 최대길이(100을 지정하면 한글50자,영문100자)
					 입력값이 최대길이를 초과하면 return false
		msg			:사용자에게 보여주는 메시지에서 사용할 항목 이름
	-사용예
		<script language="javascript">
		function checkForm(){
			if(!checkData('form1','TextBox1','S','Y','10','제목')) return false;
			if(!checkData('form1','TextBox2','S','N','20','비고')) return false;
			if(!checkData('form1','TextBox3','N','Y','','가격')) return false;
			return true;
		}
		</script>
					
*/


function checkData(FormName,ObjName,DataType,IsEssential,MaxLen,msg){
   	var obj;
   	if(FormName){
   		obj=document.forms[FormName].elements[ObjName];
   	}else{
   		obj=eval("document.all."+ObjName);
   	}
   	//var obj=document.getElementsByName(ObjName);
   	//alert(obj.length);
   	
   	if(!obj){
   		alert("잘못된 객체입니다");
   		return false;
   	}
   	  
   	var sVal="";
   	if(obj.length==null){
   		sVal=obj.value;		
	}else{
		for(var i=0;i<obj.length;i++){
			//alert(obj[i].checked+"-"+obj[i].value);
			if(obj[i].checked || obj[i].selected){
				sVal=obj[i].value;
				break;
			}	
		}		
	}
	//alert(obj.name+"//"+obj.type+"//"+obj.length+"//"+sVal);
	if(sVal.indexOf("|")>-1){sVal=sVal.substring(0,sVal.indexOf("|"));}	
   	
   	DataType=DataType.toUpperCase();
   	IsEssential=IsEssential.toUpperCase();   	
   	
   	if(IsEssential=="Y"){
   		if(!checkEssential(sVal,msg)){
   			setFocus(obj);
   			return false;
   		}
   	}
   	
   	if(DataType=="S"){
   		if(!checkMaxLen(sVal,MaxLen,msg)){
   			setFocus(obj);
   			return false;
   		}
   		sVal = checkSpecialChar(sVal);    		
   	}
   	
   	if(DataType=="N"){
   		if(!checkNumeric(sVal,msg)){
   			setFocus(obj);
   			return false;
   		}   		
   	}
   	
   	return true;
}

function checkSpecialChar(str)
{	
	for(var i = 0; i < str.length; i++)
	{
		var output = str.charAt(i);
		if(output == "'")
		{			
			str = str.replace("'", "`");
		}
		if(output == '"')
		{			
			str = str.replace('"', '``');
		}
	}	
	return str;	
}
//값입력여부 체크
//입력값있으면 true,없으면 false
function checkEssential(str,msg){
	if(!str){
		alert(msg+'은(는) 필수입력사항입니다!');
		return false;
	}else{
		return true;	
	}
}

//숫자여부 체크
//숫자이면 true,아니면 false
function checkNumeric(str,msg){
	if(isNaN(str)){
		alert(msg+'에는 숫자만 입력할 수 있습니다!');
		return false;
	}else{
		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.type=="select-multiple"){return;}
	if(obj.style.display == "none") {return;}
	if(obj.length!=null){
		obj[0].focus();
	}else{
		obj.focus();
	}
}


/*
함수 yAjax()
	- ajax 사용
	- 입력 패러미터
		sMmethod	: 전송 방식 (GET , POST)
		sUrl    	: Request URL					 
		sAsync  	: 동기(false)/비동기(true) 설정 => 비동기일 경우 콜백 이벤트 이용 가능
		sContent	: POST 방식일 경우 전송할 Data
		sRequest	: Response Type 
		sFunction   : 콜백 함수 명
	- 사용예		
	    yAjax.open('GET','test.aspx',true,null,XML,'callback')				
*/

var yAjax = {

	getXmlHttp : function(){
		var xmlHttp = false;
	
		if (window.ActiveXObject){
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			if (xmlHttp ==  null){
				xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");            
			}
		}else if (window.XMLHttpRequest){
			xmlHttp = new XMLHttpRequest();
		}			
		return xmlHttp;		
	}
	,
	open : function( sMmethod, sUrl, sAsync, sContent, sRequest, sFunction){
		var xmlHttp = this.getXmlHttp();
		if( !xmlHttp ){
			alert("XMLHTTP Object 생성 에러입니다.");
			return;
		}
		
		xmlHttp.open(sMmethod, sUrl, sAsync);
		
		if( sMmethod=="POST" ){
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		}

		xmlHttp.onreadystatechange = function() {
	    	
		    if(xmlHttp.readyState == 4){ //COMPLETE(4)
		        	
					if( xmlHttp.status == 200 ){
		
						if( sRequest=="XML" ){
							eval(sFunction+'(xmlHttp.responseXML);');
						}else{
							eval(sFunction+'(xmlHttp.responseText);');
						}
					}else{
						alert("오류:"+xmlHttp.status);
					}       
				}
		}

		xmlHttp.send(sContent);		
	}
	,
	encode : function(paramValue){
		return encodeURIComponent(paramValue);
	}	
};

var g_SetAjaxResult_Element;

function SetAjaxResultToElement(url, element) {
	g_SetAjaxResult_Element = element;
	yAjax.open("GET", url, false, null, "TEXT", "SetAjaxResultCallback");
}

function SetAjaxResultCallback(resHtml) {
	g_SetAjaxResult_Element.innerHTML = resHtml;	
}




//스크롤 설정과 DIV설정 
function setDivShow(iframe_name, div_name, popup_url, ctl_pos){
	if(iframe_name != '' && popup_url != '')
		document.getElementById(iframe_name).src = popup_url;	//주소이동
			
	var topMenuTerm = 150;	//탑메뉴 높이는 빼야함 
	var setScrollPos = ctl_pos.y - parseInt(document.getElementById(iframe_name).height);
	if(setScrollPos < 0)
		setScrollPos = ctl_pos.y - topMenuTerm;
		
	//alert(document.body.scrollTop + ":" + setScrollPos);
	document.body.scrollTop = setScrollPos;	//스크롤위치
	document.getElementById(div_name).style.top = ctl_pos.y; //위치설정
	document.getElementById(div_name).style.left = ctl_pos.x;	//위치설정
	document.getElementById(div_name).style.display = ''; //DIV 활성화
}

//위치설정함수
function Point(iX, iY){
	this.x = iX;
	this.y = iY;
}

//콘트롤위치 찾기
function findCtlPos(ctl_obj){
 	var pos = null;
 	var curleft = 0;
 	var curtop = 0;
 	var ctl_height = parseInt(ctl_obj.offsetHeight);
 	
 	if(ctl_obj.offsetParent){ 
		while(ctl_obj.offsetParent){  	//부모노드가 있을때까지 돕니다(없다면 루트죠)
   		curleft += ctl_obj.offsetLeft;  //부모노드로 부터 X좌표를 구합니다.
   		curtop += ctl_obj.offsetTop;  	//부모노드로 부터 X좌표를 구합니다.
   		
   		ctl_obj = ctl_obj.offsetParent;    	//현재노드값에 부모노드를  대입합니다.
 	 	}
 	}
 	else if(ctl_obj.x && ctl_obj.x){ 
 		curleft += ctl_obj.x;
 		curtop += ctl_obj.y;
 	}
 	
 	curtop = parseInt(curtop)+ctl_height+1
 	pos = new Point(curleft, curtop);
 	
 	return pos;
}

// 공통_팝업 띄우기 
 function popupWindow(Url, windowName, scroll){ 
   if((scroll==null) || (scroll=="")){
	   scroll = "no";
   }

   //var win = window.open(Url,windowName,"toolbar=no,location=no,directory=no,status=no,menubar=no,scrollbars="+ scroll +",resizable=no,top=200,left=300,width="+ Width +",height="+ Height);
   //var win = window.open(Url, windowName, "toolbar=no, location=no");
   var win = window.open(Url, windowName, "status=no,toolbar=no,resizable=no,scrollbars="+scroll+",menubar=no, width=500, height=250, left=300, top=200"); 
   win.focus();
 }
 
 function popupWindow1(Url, windowName, width, height){
   //var win = window.open(Url,windowName,"toolbar=no,location=no,directory=no,status=no,menubar=no,scrollbars="+ scroll +",resizable=no,top=200,left=300,width="+ Width +",height="+ Height);
   //var win = window.open(Url, windowName, "toolbar=no, location=no");
   var win = window.open(Url, windowName, "status=no,toolbar=no,resizable=no,scrollbars=yes,menubar=no, width="+width+", height="+height+", left=300, top=200"); 
   win.focus();
 }
 //파일 업로드 
 function fileUpload(type, module, file_count, callback) {
  	popupWindow("/admin/_common/fileupload.do?method=select&Module=" + module + "&Type=" + type + "&file_count=" + file_count + "&callback=" + callback, 'fileupload', "");
 }


	
// --------------------------------------------------------
//	' Function Name	: radionInit
//	' Description	: SelctList 초기 값 셋팅
//	' Example		: selectInit("r_wedding","Y");
//	' @param		: object 이름, 비교 값
//	' -------------------------------------------------------- 
function selectInit(pObj, pValue){
	var obj = document.getElementById(pObj);
	var objCol = obj.options

	for (idx = 0 ; idx < objCol.length ; idx++){
		if(objCol[idx].value == pValue){
			objCol[idx].selected = true;
		}
	}
}

//쿠키 값 조회 
function getCookie(uName) {
	var flag = document.cookie.indexOf(uName+'=');
	if (flag != -1) { 
		flag += uName.length + 1
		end = document.cookie.indexOf(';', flag) 
	
		if (end == -1) end = document.cookie.length
		return decodeURIComponent(document.cookie.substring(flag, end))
	}
	else
	{
		return ""
	}
}
