﻿
///////////////////////////////////////////////////////////////////////////////////////////
// AJAX 관련 자스~~~
///////////////////////////////////////////////////////////////////////////////////////////

// request 객체
//var req = null;
var request = null;

function create_request() {
		var request = null;
		try {
				request = new XMLHttpRequest();
		} catch (trymicrosoft) {
				try {
						request = new ActiveXObject("Msxml12.XMLHTTP");
				} catch (othermicrosoft) {
						try {
								request = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (failed) {
								request = null;
						}
				}
		}
		if (request == null)
				alert("Error creating request object!");
		else
				return request;
}

function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
				window.onload = func;
		} else {
				window.onload = function() {
						oldonload();
						func();
				}
		}
}

function showToolTip(e,text){
	if(document.all)e = event;

	var obj = document.getElementById('bubble_tooltip');
	var obj2 = document.getElementById('bubble_tooltip_content');
	obj2.innerHTML = text;
	obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0;
	var leftPos = e.clientX - 100;
	if(leftPos<0)leftPos = 0;
	obj.style.left = leftPos + 'px';
	obj.style.top = e.clientY - obj.offsetHeight -1 + st + 'px';

}

function hideToolTip()
{
	document.getElementById('bubble_tooltip').style.display = 'none';

}

function stripeTables() {
		if (!document.getElementsByTagName) return false;
		var tables = document.getElementsByTagName("table");
		for (var i=0; i<tables.length; i++) {
						var odd = false;
						var rows = tables[i].getElementsByTagName("tr");
						for (var j=0; j<rows.length; j++) {
										if (odd == true) {
														addClass(rows[j],"odd");
														odd = false;
										} else {
														odd = true;
										}
						}
		}
}

function highlightRows() {
		if(!document.getElementsByTagName) return false;
		var rows = document.getElementsByTagName("tr");
		for (var i=0; i<rows.length; i++) {
						rows[i].oldClassName = rows[i].className
						rows[i].onmouseover = function() {
										addClass(this,"highlight");
						}
						rows[i].onmouseout = function() {
										this.className = this.oldClassName
						}
		}
}

function insertAfter(newElement,targetElement) {
				var parent = targetElement.parentNode;
				if (parent.lastChild == targetElement) {
								parent.appendChild(newElement);
				} else {
								parent.insertBefore(newElement,targetElement.nextSibling);
				}
}

function addClass(element,value) {
				if (!element.className) {
								element.className = value;
				} else {
								newClassName = element.className;
								newClassName+= " ";
								newClassName+= value;
								element.className = newClassName;
				}
}


function replaceText(el, text) {

				if (el != null) {
								clearText(el);
								var newNode = document.createTextNode(text);
								el.appendChild(newNode);
				}
}

function clearText(el) {
				if (el != null) {
								if (el.childNodes) {
												for (var i = 0; i < el.childNodes.length; i++) {
																var childNode = el.childNodes[i];
																el.removeChild(childNode);
												}
								}
				}
}

function getText(el) {
				var text = "";
				if (el != null) {
								if (el.childNodes) {
												for (var i = 0; i < el.childNodes.length; i++) {
																var childNode = el.childNodes[i];
																if (childNode.nodeValue != null) {
																				text = text + childNode.nodeValue;
																}
												}
								}
				}
				return text;
}

function displayLoading(element) {
				while (element.hasChildNodes()) {
								element.removeChild(element.lastChild);
				}
				var image = document.createElement("img");
				image.setAttribute("src","/images/loading.gif");
				image.setAttribute("alt","Loading...");
				element.appendChild(image);
}

function fadeUp(element,red,green,blue) {
				if (element.fade) {
								clearTimeout(element.fade);
				}
				element.style.backgroundColor = "rgb("+red+","+green+","+blue+")";
				if (red == 255 && green == 255 && blue == 255) {
								return;
				}
				var newred = red + Math.ceil((255 - red)/10);
				var newgreen = green + Math.ceil((255 - green)/10);
				var newblue = blue + Math.ceil((255 - blue)/10);
				var repeat = function() {
								fadeUp(element,newred,newgreen,newblue)
				};
				element.fade = setTimeout(repeat,100);
}

///////////////////////////////////////////////////////////////////////////////////////////
// FORM 관련 자스~~~
///////////////////////////////////////////////////////////////////////////////////////////
function resetFields(whichform) {
		for (var i=0; i<whichform.elements.length; i++) {
				var element = whichform.elements[i];

				if (element.type == "text") {
						if (element.type == "submit") continue;
						if (!element.defaultValue) continue;
						element.onfocus = function() {
								if (this.value == this.defaultValue) {
										this.value = "";
								}
						}
						element.onblur = function() {
								if (this.value == "") {
										this.value = this.defaultValue;
								}
						}
				}
		}
}

function validateForm(whichform) {
		for (var i=0; i<whichform.elements.length; i++) {
				var element = whichform.elements[i];
				if (element.className.indexOf("required") != -1) {
						if (!isFilled(element)) {
								alert("Please fill in the "+element.name+" field.");
								element.focus();
								return false;
						}
				}
				if (element.className.indexOf("email") != -1) {
						if (!isEmail(element)) {
								alert("The "+element.name+" field must be a valid email address.");
								element.focus();
								return false;
						}
				}
				if (element.className.indexOf("double") != -1) {
						if (!isDouble(element.value)) {
								alert("The "+element.name+" field must be a valid Number(0~9,dot).");
								element.focus();
								return false;
						}
				}
				if (element.className.indexOf("number") != -1) {
						if (!isNumeric(element.value)) {
								alert("The "+element.name+" field must be a valid Number(0~9).");
								element.focus();
								return false;
						}
				}
		}
		return true;
}

// insert form
function prepareForms() {
		for (var i=0; i<document.forms.length; i++) {
				var thisform = document.forms[i];
				resetFields(thisform);
				thisform.onsubmit = function() {
						return validateForm(this);
				}
		}
}

function checkAfterDate(field, defaultDate) {		//필드의 날짜가 오늘이후의 날짜가 아니면 입력 못하도록 설
	dd= new Date();
	dd= new Date(dd.getFullYear(),(dd.getMonth()),dd.getDate());
	tmp=field.value.split('/');
	dd2=new Date(tmp[0],(tmp[1]-1),tmp[2]); /*구할 날짜 */
	if(dd2<dd)
	{
		alert(MSG_DATE_MISS_TODAY_BEFORE);
		field.value = defaultDate;
	}
}

function checkAfterDateKR(field, defaultDate) {		//필드의 날짜가 오늘이후의 날짜가 아니면 입력 못하도록 설
	dd= new Date();
	dd= new Date(dd.getFullYear(),(dd.getMonth()),dd.getDate());
	tmp=field.value.split('/');
	dd2=new Date(tmp[0],(tmp[1]-1),tmp[2]); /*구할 날짜 */
	if(dd2<dd)
	{
		alert(MSG_KR_DATE_MISS_TODAY_BEFORE);
		field.value = defaultDate;
	}
}

// modify form
function prepareModifyForms() {
		for (var i=0; i<document.forms.length; i++) {
				var thisform = document.forms[i];
				thisform.onsubmit = function() {
						return validateForm(this);
				}
		}
}

function isFilled(field) {
		if (field.value.length < 1 || field.value == field.defaultValue) {
				return false;
		} else {
				return true;
		}
}

function isEmail(field) {
		if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
				return false;
		} else {
				return true;
		}
}


///////////////////////////////////////////////////////////////////////////////////////////
// Paging 관련 자스~~~
///////////////////////////////////////////////////////////////////////////////////////////
var listUrl = "";
//페이지 이동
function goPage(row) {
	var lform = document.list;
	lform.targetRow.value = row;
		lform.target = "_self";
	if(listUrl != "") lform.action = listUrl;
	lform.submit();
}

//정렬
function goOrderByPage(row,orderBy) {
	var lform = document.list;
	lform.targetRow.value = row;
	lform.lafjOrderBy.value = orderBy;
		lform.target = "_self";
	if(listUrl != "") lform.action = listUrl;
	lform.submit();
}

//페이지 바로 가기
function changePage(mySelect) {
	var lform = document.list;
	lform.targetRow.value = mySelect.value;
		lform.target = "_self";
	if(listUrl != "") lform.action = listUrl;
	lform.submit();
}
//paging 관련 자바스트립트 마침

function isWhitespace(s){
				var whitespace = " \t\n\r";
				for (var i=0; i<s.length; i++) {
								 // Check that current character isn't whitespace.
								var c = s.charAt(i);
								if (whitespace.indexOf(c) == -1) {
												return false;
								}
				}
				// All characters are whitespace.
				return true;
}

// This function is used for removing any leading white spaces in a string
function LTrim(s) {
				// trim lefthand spaces
				if(s.length >1) {
						while(''+s.charAt(0)==' ') {
										s = s.substring(1,s.length);
						}
				}

				return s;
}

// This function is used for removing any trailing white spaces in a string
function RTrim(s) {
	//trim righthand spaces
	if(s.length >1) {
		while(''+s.charAt(s.length-1)==' ') {
			s = s.substring(0,s.length-1);
		}
	}

	return s;
}

// This function will trim both leading and trailing spaces.
function Trim(s) {
		return LTrim(RTrim(s));
}


// This function checks whether a given string contains only numbers or not
// Returns true if the passed string numeric only and false otherwise
function isNumeric(string) {
	if(!string) return false;

	var Chars = "-0123456789";

	for(var i=0;i<string.length;i++) {
		if(Chars.indexOf(string.charAt(i))==-1)
		return false;
	}

	return true;
}

function isDouble(string) {
	if(!string) return false;

	var Chars = ".0123456789";

	for(var i=0;i<string.length;i++) {
		if(Chars.indexOf(string.charAt(i))==-1)
		return false;
	}

	return true;
}

// for checking isEmpty
function isEmpty(value) {
	var s = value;
	s = LTrim(s);
	s = RTrim(s);

	if( s.length == 0 ) {
		return true;
	}
	return false;
}

// This function validates the e-mail id passed as a string.
function isSpecialChar(string) {
	var Chars = "`~!@#$%^&*()|?><'\".,";
		var c;
	for(var i=0;i<string.length;i++) {
		c =  string.charAt(i) ;
		if(Chars.indexOf(c)!=-1) {
			return true;
		}
	}
	return false;
}

function isValidText(text) {
	if(text == null  || text.length == 0  ) {
				return false;
	} else {
		if(isWhitespace(text)==true || isSpecialChar(text)==true) {
			return false;
		} else {
			return true;
		}
	}
}

function popClose(){
	self.opener = self;
	window.close();
}

function TestHyperTextArea(){
	//alert(document.getElementById("area1").contentWindow.document.body.innerHTML);
	var form = document.HyperTextAreaExample;

	form.action = "/global.HyperText.laf";
	form.submit();
}

String.prototype.replaceAll = function(str1, str2) {
	var temp_str = "";

	if (str1 != str2) {
		temp_str = this;

		while (temp_str.indexOf(str1) > -1) {
				temp_str = temp_str.replace(str1, str2);
		}
	}

	return temp_str;
}

/*
*	이미지 실제 크기 가져오기
*/
function get_image_size(id) {
	img = document.body.appendChild(document.createElement('img'))
	img.src = id.src;
	var w = img.offsetWidth;
	var h = img.offsetHeight;
	document.body.removeChild(img);
	return {width:w,height:h};
}
/*
* 실제 이미지를 넘겨서 계산하도록 수정 -2008.3.13 조호형-
*/
function get_image_size_byname(img_nm) {

	img = document.body.appendChild(document.createElement('img'));
	img.src = img_nm;

	var w = img.offsetWidth;
	var h = img.offsetHeight;
	document.body.removeChild(img);
	return {width:w,height:h};
}


/*
*	input box 에 통화량 형식으로 표현하기
*/
function setChar2Money(obj){
	//숫자인지 확인
	num = "";
	for( i=0; i< obj.value.length; i++){
		if (obj.value.charAt(i) < '0' || obj.value.charAt(i) > '9'){
			if(obj.value.charAt(i) != ',' && obj.value.charAt(i) != '.'){
				alert("숫자만 입력 가능합니다.");
				obj.focus();
				//obj.select();
				return;
			}
		}else{
			num += obj.value.charAt(i);
			//alert(num);
		}
	}

	//num = obj.value;

	if (num < 0) { num *= -1; var minus = true;}
	else var minus = false;

	var dotPos = (num+"").split(".");
	var dotU = dotPos[0];
	var dotD = dotPos[1];
	var commaFlag = dotU.length%3;

	if(commaFlag) {
		var out = dotU.substring(0, commaFlag) ;
		if (dotU.length > 3) out += ",";
	}
	else var out = "";

	for (var i=commaFlag; i < dotU.length; i+=3) {
		out += dotU.substring(i, i+3) ;
		if( i < dotU.length-3) out += ",";
	}

	if(minus) out = "-" + out;
	if(dotD){
		obj.value = out + "." + dotD;
	}else{
		obj.value = out;
	}
	return false;
}


/*
*	길이제한 스크립트
*
*/
function chkLength(obj,limit) {

	if (obj.value.length > limit) {
		alert("under " + limit + 'words only!');
		obj.value = obj.value.substring(0,limit);
	}
}

/*
*	FCKeditor 공백 제거하고 보내기 위해... 조호형.
*   정규표현식을 이용한 공백 체크
*/
function isNull( text )
{
 if( text == null ) return true;  

 var result = text.replace(/(^\s*)|(\s*$)/g, "");  
 if( result )  
	return false;
 else  
	return true;
}
