var simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 
function simpleEncode(valueArray, maxValue) {
  var chartData = ['s:'];
    for (var i = 0; i < valueArray.length; i++) {
      var currentValue = valueArray[i];
      if (!isNaN(currentValue) && currentValue >= 0) {
      chartData.push(simpleEncoding.charAt(Math.round((simpleEncoding.length-1) * currentValue / maxValue)));
      }
        else {
        chartData.push('_');
        }
    }
  return chartData.join('');
}


function checkeYN(objName){
	var obj = document.getElementsByName(objName);
	for (var i=0; i<obj.length; i++){
		if(obj[i].checked == true){
			return i;
		}		
		
	}
	return -1;
}



var focusTagID = "";
function setFocus(){
	document.getElementById(focusTagID).focus();
}


function extAlertForFocus(title, msg, focusID){
	Ext.Msg.alert(
				title, 
				msg, 
				function(){
							if (focusID && focusID.length > 0){
								focusTagID = focusID;
								setFocus();
							}
				}
	);
}

function autoCheckBox(name, value){
	var checkname = document.getElementsByName(name);
	
	for(i=0; i<checkname.length; i++){
		if(checkname[i].value == value){
			checkname[i].checked = true;
		}
	}
}

var checkExtProgress = false;
var extProgress = function(title, msg){
       Ext.MessageBox.show({
          msg: title,
          progressText: msg,
          width:300,
          wait:true,
          waitConfig: {interval:200},
          animEl: 'startProcess'
      });
      var isHide = function(){
      		if (checkExtProgress){
      			checkExtProgress = false;
      			Ext.MessageBox.hide();
      		}
      		else
      			setTimeout(isHide, 1000);
      		return;
      };
      isHide();
   };
   
   
   
function isNetscape(){
	var name = navigator.appName;
	var ver = navigator.appVersion;
	
	if (name == "Netscape")
		return true;
	else
		return false;
	
}

/**
 * ##################################################
 * AJAX
 * ##################################################
 */

function f_create_ajax() { }

f_create_ajax.prototype = {

	exec : function(url , pars , fn_oncomplete , fn_onfailure) {
		var ajax = new Ajax.Request(
			url,
			{
				method: 'get', 
				parameters: pars, 
				onComplete: fn_oncomplete,
				onFailure : fn_onfailure
			}
		);
	}
	
}

 
function cf_xml_root(xmlrequest) {
	var root_element = xmlrequest.responseXML.documentElement;
	if(root_element == null) {
		alert("Document Element is null");
	}
	return root_element;
}

function cf_xml_elements(xmlrequest, nodename) {
	var root_element = cf_xml_root(xmlrequest);
	return root_element.getElementsByTagName(nodename);
}

function cf_xml_text(xmlrequest, nodename) {
	var elements = cf_xml_elements(xmlrequest, nodename);
	var textElement = elements[0].firstChild;
	var text = isNetscape() ? textElement.textContent : textElement.text;
	return text;
}

function cf_element_text(textElement) {
	if(textElement == null)
		return "";
	return isNetscape() ? textElement.textContent : textElement.text;
}

function cf_create_option(obj_select, value, text) {
	var option = new Option();
	option.value = value;
	option.text = text;
	obj_select.options.add(option);
}

function cf_remove_option(obj_select) {
	var length = obj_select.options.length
    for(var i = 0; i < length; i++) {
        obj_select.remove(0);
    }
}

function cf_show_calendar(domId, posiId) {
	var cal = new Ext.DatePicker(
		{
			dayNames : ['일','월','화','수','목','금','토'], 
		   	monthNames : ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
		   	format : "Y년 m월 d일",
		   	disabledDays : null,
		   	disabledDaysText : "사용불가",
		   	disabledDates : null,
		   	disabledDatesText : "사용불가",
		   	minValue : null,
		   	maxValue : null,
		   	minText : "필드 안의 날짜는 반드시 {0}의 이후이어야 한다. ",
		   	maxText : "필드 안의 날짜는 반드시 {0}의 이전이어야 한다. ",
		   	invalidText : "{0} 은 유효한 날짜가 아닙니다. - 반드시 {1}과 같은 형식이어야 합니다.",
		   	nextText : "다음 달(Ctrl+Right)",
		   	prevText : "이전 달(Ctrl+Left)",
		   	todayText : "오늘은 {0}" 
		}
	);
	cal.setValue(new Date());
	cal.render(posiId);
	cal.on('select', function(cal, date)
		{
		Ext.get(domId).dom.value = formatDate(date);
		cal.destroy();
		}
	);
}

function f_swfprint_map(furl, cityCode, fwidth, fheight, transoption, id)	{
	id = id || "";
	if(id == ""){
		document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+ fwidth +'" height="' + fheight +'" align="middle">');
	}else{
		document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" id="' + id + '"  width="'+ fwidth +'" height="' + fheight +'" align="middle">');
	}

	document.write('<param name="movie" value="'+ furl +'"/>');
	document.write('<param name="flashVars" value="cityCode='+ cityCode +'"/>');
	document.write('<param name="wmode" value="transparent"/>');
	/*if (transoption == "t")	{
		document.write('<param name="wmode" value="transparent"/>');
	} else if	(transoption == "o")	{
		document.write('<param name="wmode" value="opaque"/>');
	}*/
	document.write('<!-- Hixie method -->');
	document.write('<!--[if !IE]> <-->');
	document.write('<object type="application/x-shockwave-flash" data="'+ furl +'" width="'+ fwidth +'" height="' + fheight +'"');
	document.write(' wmode="transparent"');
	/*if (transoption == "t")	{
		document.write(' wmode="transparent"');
	} else if	(transoption == "o")	{
		document.write(' wmode="opaque"');
	}*/
	document.write(' flashVars="cityCode='+cityCode+'"');
	document.write('></object>');
	document.write('<!--> <![endif]-->');
	document.write('</object>');
}

function f_swfprint(furl, xmlURL, fwidth, fheight, transoption, id)	{
	id = id || "";
	if(id == ""){
		document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+ fwidth +'" height="' + fheight +'" align="middle">');
	}else{
		document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" id="' + id + '"  width="'+ fwidth +'" height="' + fheight +'" align="middle">');
	}

	document.write('<param name="movie" value="'+ furl +'"/>');
	document.write('<param name="flashVars" value="xmlURL='+ xmlURL +'"/>');
	document.write('<param name="wmode" value="transparent"/>');
	/*if (transoption == "t")	{
		document.write('<param name="wmode" value="transparent"/>');
	} else if	(transoption == "o")	{
		document.write('<param name="wmode" value="opaque"/>');
	}*/
	document.write('<!-- Hixie method -->');
	document.write('<!--[if !IE]> <-->');
	document.write('<object type="application/x-shockwave-flash" data="'+ furl +'" width="'+ fwidth +'" height="' + fheight +'"');
	document.write(' wmode="transparent"');
	/*if (transoption == "t")	{
		document.write(' wmode="transparent"');
	} else if	(transoption == "o")	{
		document.write(' wmode="opaque"');
	}*/
	document.write(' flashVars="xmlURL='+ xmlURL +'"');
	document.write('></object>');
	document.write('<!--> <![endif]-->');
	document.write('</object>');
}


function doDisplay(elementId, flg){
	var obj = document.getElementById(elementId);
	if (obj){
		if ( !flg )
			obj.style.display = "none";
		else
			obj.style.display = "";
	}
}

function doDisplay(elementId){
	var obj = document.getElementById(elementId);
	if (obj){
		var display = obj.style.display;
		if ( obj.style.display == "" )
			obj.style.display = "none";
		else
			obj.style.display = "";
	}
}


function setInnerTextProperty() {
	if(typeof HTMLElement != "undefined" && typeof HTMLElement.prototype.__defineGetter__ != "undefined") {
		HTMLElement.prototype.__defineGetter__("innerText",function() {
			if(this.textContent) {
				return(this.textContent)
			} 
			else {
				var r = this.ownerDocument.createRange();
				r.selectNodeContents(this);
				return r.toString();
			}
		});
		
		HTMLElement.prototype.__defineSetter__("innerText",function(sText) {
			this.innerHTML = sText
		});
	}
}



/*
' ------------------------------------------------------------------
' Description : 입력한 글자수를 체크
' Argument    : Object Name(글자수를 제한할 컨트롤)
' Return      : 
' ------------------------------------------------------------------
*/
function checkByte(tagId, maxByte, returnResultTagId)
{
	setInnerTextProperty();
	
	var obj = document.getElementById(tagId);
	
	if ( obj ){
		var ls_str     = obj.value; // 이벤트가 일어난 컨트롤의 value 값
		var li_str_len = ls_str.length;  // 전체길이
		
		// 변수초기화
		var li_max      = maxByte; // 제한할 글자수 크기
		var i           = 0;  // for문에 사용
		var li_byte     = 0;  // 한글일경우는 3 그밗에는 1을 더함
		var last_byte   = 0;  // 한글일경우는 3 그밗에는 1을 더함
		var li_len      = 0;  // substring하기 위해서 사용
		var ls_one_char = ""; // 한글자씩 검사한다
		var ls_str2     = ""; // 글자수를 초과하면 제한할수 글자전까지만 보여준다.
		
		for(i=0; i< li_str_len; i++){
			// 한글자추출
			ls_one_char = ls_str.charAt(i);
			
			// 한글이면 3를 더한다.
			if (escape(ls_one_char).length > 4){
			   li_byte += 3;
			   last_byte = 3;
			}
			// 그밗의 경우는 1을 더한다.
			else{
				last_byte = 1;
				li_byte++;
			}
			
			// 전체 크기가 li_max를 넘지않으면
			if(li_byte <= li_max){
				li_len++;
			}
			else{
				break;
			}
		}
		
		
		// 전체길이를 초과하면
		if(li_byte > li_max){
			Ext.Msg.alert("알림", li_max + "Byte를 초과 입력할수 없습니다.", function(){ 
				ls_str2 = ls_str.substr(0, li_len);
				obj.value = ls_str2;
				document.getElementById(returnResultTagId).innerText = li_byte - last_byte;
				obj.focus();
				} 
			);
		}
		else{
			document.getElementById(returnResultTagId).innerText = li_byte;
		}

		
	}
}

