/**
 * common.js
 *
 * Àü ÆäÀÌÁö °øÅë ½ºÅ©¸³Æ®
 *
 */

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

var IE = (navigator.userAgent.indexOf('MSIE') != -1) ? true : false;		// IE Check
var IE6 = (navigator.userAgent.indexOf('MSIE 6.0') != -1) ? true : false;		// IE Check
var IE7 = (navigator.userAgent.indexOf('MSIE 7.0') != -1) ? true : false;	// IE7 Check
var IE8 = (navigator.userAgent.indexOf('MSIE 8.0') != -1) ? true : false;	// IE8 Check
var FF = (navigator.userAgent.indexOf('Firefox') != -1) ? true : false;	// FF Check


/**
 * ¿£ÅÍÃ¼Å©
 * @obj °´Ã¼
 */
function EnterCheck(obj, act) {
	if (event.keyCode == 13) {
		event.returnValue = false;
		eval(act);
	} else
		return;
}

String.prototype.escapeXml = function() {
	var s = (this!=null) ? this : "";
	s = s.replace(/&/g,"&amp;");
	s = s.replace(/\'/g,"&#039;");
	s = s.replace(/\"/g,"&#34;");
	s = s.replace(/</g,"&lt;");
	s = s.replace(/>/g,"&gt;");
	return s;
}

String.prototype.getBytes = function() {
	var s = (this!=null) ? this : "";
	var bytes = 0;
	var c = "";
	var u = "";
	for (var i=0; i<s.length; i++) {
		c = s.charAt(i);
		u = escape(c);
		if (u.length < 4) { // ¹Ý°¢¹®ÀÚ : ±âº»ÀûÀÎ ¿µ¹®, ¼ýÀÚ, Æ¯¼ö±âÈ£
			bytes++; // + 1byte
		} else {
			var b = parseInt(c.charCodeAt(0));
			if (((b >= 65377)&&(b <= 65500))||((b >= 65512)&&(b <= 65518))) // ¹Ý°¢¹®ÀÚ À¯´ÏÄÚµå 10Áø¼ö ¹üÀ§ : ÇÑ±¹¾î, ÀÏº»¾î, Æ¯¼ö¹®ÀÚ
				bytes++; // + 1byte
			else // Àü°¢¹®ÀÚ : À§ Á¶°ÇÀ» Á¦¿ÜÇÑ ¸ðµç ¹®ÀÚ
				bytes += 2; // + 2byte
		}
	}
	return bytes;
}


// ¼ýÀÚ¸¸ ÀÔ·Â(onkeydown="handlerNum()")
function numberKey(obj) {
	e = window.event;
	value = e.keyCode;
	/* ¹é½ºÆäÀÌ½º,ÅÜ,¿£ÅÍ,È­»ìÇ¥,µ¨¸®Æ®,0~9,¿À¸¥ÂÊ0~9 */ 
	if (value == 8 || value == 9 || value == 13 || (value >= 37 &&  value <= 40) || value == 46 || (value >= 48 && value <= 57) || (value >= 96 && value <= 105)) { 
		return; 
	} else { 
		e.returnValue = false;
	} 

}

// ¼ýÀÚ, .¸¸ ÀÔ·Â(onkeydown="handlerNum()")
function numberDotKey(obj)  {
	e = window.event;
	if( (e.keyCode >= 48 && e.keyCode <= 57) || e.keyCode <= 46)
	{
		return;
	}else{
		e.returnValue=false;
	}
}

// ¼ýÀÚ, .¸¸ ÀÔ·Â(onkeydown="handlerNum()")
function numberZeroKey(obj) {
	e = window.event;
	if( (e.keyCode >= 48 && e.keyCode <= 57) || e.keyCode <= 46)
	{
		return;
	}else{
		e.returnValue=false;
	}
}


function calcHeight(iframe_id){
	try {
		var objIframeBody = document.getElementById(iframe_id);
		var innerBody = objIframeBody.contentWindow.document.body;

		var the_height = innerBody.scrollHeight + (innerBody.offsetHeight - innerBody.clientHeight);
		if(FF) the_height += 2;
		document.getElementById(iframe_id).height = the_height + 'px';

		if (typeof(parent.MovePage) == 'function') {
			parent.resizeCurrentFrame(null);
		}
	} catch(e) {
	}
}


/**
 Ajax Object
*****************************/
function Ajax(_url, _method, _async) {
	this.request = null;
	this.method = (_method==undefined)?"post":_method;
	this.url = (_url==undefined)?"":_url;
	this.async = (_async==undefined)?true:_async;
	this.parameter = "";
	this.header = {
		contentType : "application/x-www-form-urlencoded; charset=UTF-8"
	};
}
Ajax.prototype.getEncParam = function(_name, _value) {
	return encodeURIComponent(_name) + "=" + encodeURIComponent(_value);
};
Ajax.prototype.getTimeStamp = function() {
	return (new Date()).getTime();
};
Ajax.prototype.getReadyState = function() {
	return this.request.readyState;
};
Ajax.prototype.getStatus = function() {
	return this.request.status;
};
Ajax.prototype.getResponseText = function() {
	return this.request.responseText;
};
Ajax.prototype.getResponseXML = function() {
	return this.request.responseXML;
};
Ajax.prototype.appendParameter = function(_name, _value) {
	this.parameter = this.parameter.appendParameter(this.getEncParam(_name, _value));
};
Ajax.prototype.send = function() {
	this.request = null;
	try { this.request = new XMLHttpRequest(); } catch(e1) {
		try { this.request = new ActiveXObject('Msxml2.XMLHTTP'); } catch(e2) {
			try { this.request = new ActiveXObject('Microsoft.XMLHTTP'); } catch(e3) {
				this.request = null;
			}
		}
	}
	this.request.open(this.method, (this.method=="post")?this.url:this.url.appendParameter(this.parameter.appendParameter("__temp="+this.getTimeStamp())) , this.async);
	this.request.setRequestHeader("Content-Type", this.header.contentType);
	this.request.send((this.method=="post")?this.parameter+"&__temp="+this.getTimeStamp():"");
	var _onStatus200 = this.onStatus200;
	var _onError = this.onError;
	var _req = this.request;
	this.request.onreadystatechange = function() {
		if (_req.readyState==4) {
			if (_req.status==200) {
				_onStatus200();
			} else {
				_onError();
			}
		}
	};
};
Ajax.prototype.onStatus200 = function() { };
Ajax.prototype.onError = function() { };








/**
 Document Object
*****************************/
function $() {
	var obj = null;
	if (typeof arguments[0] == 'string') {
		obj = document.getElementById(arguments[0]);
	} else {
		obj = arguments[0];
	}
	if (obj==null) {
		obj = new Object;
		obj.isNull = true;
		//return obj;
	} else {
		obj.isNull = false;
	}
	
	obj.setStyle = function(_name, _value) {
		try {
			if (_name=="display") {
				_value = (""+_value).toLowerCase();
				if (_value=="false"||_value=="none") _value = "none";
				else _value = "";
			} else if (_name=="left"||_name=="top"||_name=="width"||_name=="height") {
				_value = (""+_value).toLowerCase();
				if (_value!=""&&_value.indexOf("px")<0) _value += "px";
			}
			this.style[_name] = _value;
		} catch(e) {
			//alert(_name);
		}
	}
	
	obj.getStyle = function(_name) {
		return this.style[_name];
	}
	
	obj.getW = function() {
		var w = this.offsetWidth;
		return w;
	}
	
	obj.getH = function() {
		var h = this.offsetHeight;
		return h;
	}
	
	obj.getX = function() {
		var posX = this.style.left;
		if (posX=="") {
			posX = 0;
			var tempObj = this;
			if (tempObj.offsetParent) {
				while (tempObj.offsetParent) {
					posX += tempObj.offsetLeft;
					tempObj = tempObj.offsetParent;
				}
			}
		} else {
			posX = parseInt(posX.toLowerCase().replace("px",""));
		}
		return posX;
	}
	
	obj.getY = function() {
		var posY = this.style.top;
		if (posY=="") {
			posY = 0;
			var tempObj = this;
			if (tempObj.offsetParent) {
				while (tempObj.offsetParent) {
					posY += tempObj.offsetTop;
					tempObj = tempObj.offsetParent;
				}
			}
		} else {
			posY = parseInt(posY.toLowerCase().replace("px",""));
		}
		return posY;
	}
	
	obj.setOpacity = function(_value) {
		if (parseFloat(_value)>1) _value = parseFloat(_value)/100;
		this.setStyle("filter","alpha(opacity=" + (_value*100) + ")");
		this.setStyle("opacity",_value);
		this.setAttribute("opacity",_value);
	}
	
	obj.getOpacity = function() {
		var value = this.getAttribute("opacity") || this.getStyle("opacity") || 0;
		return parseFloat(value);
	}
	
	obj.setFadeIn = function(_opacity, _speed, _afterAction, _isReCall) {
		try {
			if (!this.isNull) {
				if (_isReCall!=true&&this.getAttribute("fadeStatus")=="ing") {
					clearTimeout(this.fadeTimer);
				}
				if (_opacity==null) _opacity = 1.0;
				if (_opacity>1) _opacity = _opacity/100;
				if (_speed==null) _speed = 30;
				if (_afterAction==null) _afterAction = "";
				this.setAttribute("fadeStatus","ing");
				var nowOpacity = this.getOpacity();
				var targetOpacity = nowOpacity + 0.1;
				if (targetOpacity > _opacity) targetOpacity = _opacity;
				this.setOpacity(targetOpacity);
				if (targetOpacity<_opacity) {
					this.fadeTimer = setTimeout("$('"+this.id+"').setFadeIn("+_opacity+", "+ _speed+", '"+_afterAction.replaceQuot()+"', true)", _speed);
				} else {
					this.setAttribute("fadeStatus","");
					if (_afterAction!="") {
						try {
							eval(_afterAction);
						} catch(e) {
							
						}
					}
				}
			}
		}catch(e) {
			alert("KidoScript Error Form $().setFadeIn() : " + e);
		}
	}
	
	obj.setFadeOut = function(_opacity, _speed, _afterAction, _isReCall) {
		try {
			if (!this.isNull) {
				if (_isReCall!=true&&this.getAttribute("fadeStatus")=="ing") {
					clearTimeout(this.fadeTimer);
				}
				if (_opacity==null) _opacity = 1.0;
				if (_opacity>1) _opacity = _opacity/100;
				if (_speed==null) _speed = 30;
				if (_afterAction==null) _afterAction = "";
				this.setAttribute("fadeStatus","ing");
				var nowOpacity = this.getOpacity();
				var targetOpacity = nowOpacity - 0.1;
				if (targetOpacity < _opacity) targetOpacity = _opacity;
				this.setOpacity(targetOpacity);
				if (targetOpacity>_opacity) {
					this.fadeTimer = setTimeout("$('"+this.id+"').setFadeOut("+_opacity+", "+ _speed+", '"+_afterAction.replaceQuot()+"', true)", _speed);
				} else {
					this.setAttribute("fadeStatus","");
					if (_afterAction!="") {
						try {
							eval(_afterAction);
						} catch(e) {
							
						}
					}
				}
			}
		}catch(e) {
			alert("KidoScript Error Form $().setFadeOut() : " + e);
		}
	}
	
	
	obj._getNextPoint = function(_prevPont, _targetPoint, _speed) {
		var point = 0;
		var finishPointChecking = false;
		var checkMode = "";
		if (_targetPoint<0) {
			if (_prevPont<0) {
				if (Math.abs(Math.abs(_targetPoint)-Math.abs(_prevPont))<_speed) {
					point = _targetPoint;
					finishPointChecking = true;
				}
				checkMode = "A";
			} else {
				checkMode = "B";
			}
		} else {
			if (_prevPont>=0) {
				if (Math.abs(Math.abs(_targetPoint)-Math.abs(_prevPont))<_speed) {
					point = _targetPoint;
					finishPointChecking = true;
				}
				checkMode = "C";
			} else {
				checkMode = "D";
			}
		}
		
		if (!finishPointChecking) {
			var setPoint = 0;
			var gapPoint = Math.abs(_prevPont-_targetPoint);
			if (checkMode=="A"||checkMode=="C") {
				gapPoint = Math.abs(_prevPont-_targetPoint);
			} else {
				gapPoint = Math.abs(_prevPont+_targetPoint);
			}
			var minusOption = 1;
			if (_prevPont > _targetPoint) minusOption *= -1;
			if (gapPoint>_speed) setPoint = _speed*minusOption;
			else setPoint = gapPoint*minusOption;
			point = _prevPont+setPoint;
		}
		return point;
	}
	
	
	
	obj.resizeToMotionA = function(_w, _h, _afterAction, _isReCall) {
		if (!this.isNull) {
			var useMultiCommand = (this.getAttribute("useMultiCommandForResizeTo")=="true");
			if (useMultiCommand&&_isReCall!=true&&this.getAttribute("resizeStatus")=="ing") {
				clearTimeout(this.resizeToTimer);
				this.setAttribute("resizeStatus","");
			}
			if (_isReCall==true||this.getAttribute("resizeStatus")!="ing") {
				try {
					this.setAttribute("resizeStatus","ing");
					if (_afterAction==null) _afterAction = "";
					if (_afterAction=="start") {
						this.setStyle("display",true);
						_afterAction = "";
					}
					var speed = 10;
					var w = this.getW();
					var h = this.getH();
					if (_w==null||""+parseInt(_w)=="NaN") _w = w;
					if (_h==null||""+parseInt(_h)=="NaN") _h = h;
					if (_w<1) _w = 1;
					if (_h<1) _h = 1;
					var targetW = this._getNextPoint(w,_w,speed);
					var targetH = this._getNextPoint(h,_h,speed);
					this.setStyle("width",targetW);
					this.setStyle("height",targetH);
					if (w==_w&&h==_h) {
						this.setAttribute("resizeStatus","");
						if (_afterAction=="finish") {
							this.setStyle("display",false);
						} else if (_afterAction=="remove") {
							this.parentNode.removeChild(this);
						} else if (_afterAction!="") {
							try {
								eval(_afterAction);
							} catch(e) {
							
							}
						}
					} else {
						this.resizeToTimer = setTimeout("$('"+this.id+"').resizeToMotionA("+_w+","+ _h+", '"+_afterAction.replaceQuot()+"', true)", 15);
					}
				}catch(e) {
					alert("KidoScript Error Form $().resizeToMotionA() : " + e);
				}
			}
		}
	}
	
	
	obj.resizeTo = function(_w, _h, _afterAction, _speed, _isReCall) {
		if (!this.isNull) {
			var useMultiCommand = (this.getAttribute("useMultiCommandForResizeTo")=="true");
			if (useMultiCommand&&_isReCall!=true&&this.getAttribute("resizeStatus")=="ing") {
				clearTimeout(this.resizeToTimer);
				this.setAttribute("resizeStatus","");
			}
			if (_isReCall==true||this.getAttribute("resizeStatus")!="ing") {
				try {
					this.setAttribute("resizeStatus","ing");
					
					var nowW = this.getW();
					var nowH = this.getH();
					
					if (_w==null||""+parseInt(_w)=="NaN") _w = nowW;
					if (_h==null||""+parseInt(_h)=="NaN") _h = nowH;
					if (_speed==null||""+parseInt(_speed)=="NaN") _speed = 5;
					
					var targetW = _w;
					var targetH = _h;
					
					if (targetW<1) targetW = 1;
					if (targetH<1) targetH = 1;
					
					if (_afterAction==null) _afterAction = "";
					
					if (targetW==nowW&&targetH==nowH) {
						this.setAttribute("resizeStatus","");
						try {
							eval(_afterAction);
						} catch(e) {
						
						}
					} else {
						var denominator = _speed;
						var addW = 0;
						var addH = 0;
						if (targetW > nowW) {
							addW = Math.ceil((targetW - nowW) / denominator);
						} else {
							addW = (Math.ceil((nowW - targetW) / denominator))*-1;
						}
						if (targetH > nowH) {
							addH = Math.ceil((targetH - nowH) / denominator);
						} else {
							addH = (Math.ceil((nowH - targetH) / denominator))*-1;
						}
						var finalW = nowW + addW;
						var finalH = nowH + addH;
						if (finalW<1) finalW = 1;
						if (finalH<1) finalH = 1;
						this.setStyle("width", finalW);
						this.setStyle("height", finalH);
						this.resizeToTimer = setTimeout("$('"+this.id+"').resizeTo('"+_w+"', '"+_h+"', '"+_afterAction.replaceQuot()+"', '"+_speed+"', true)", 10);
					}
				}catch(e) {
					alert("KidoScript Error Form $().resizeTo() : " + e);
				}
			}
		}
	}
	
	obj.moveTo = function(_x, _y, _afterAction, _speed, _isReCall) {
		if (!this.isNull) {
			var useMultiCommand = (this.getAttribute("useMultiCommandForMoveTo")=="true");
			if (useMultiCommand&&_isReCall!=true&&this.getAttribute("moveStatus")=="ing") {
				clearTimeout(this.moveToTimer);
				this.setAttribute("moveStatus","");
			}
			if (_isReCall==true||this.getAttribute("moveStatus")!="ing") {
				try {
					this.setAttribute("moveStatus","ing");
					
					var nowPosX = this.getX();
					var nowPosY = this.getY();
					
					if (_x==null||""+parseInt(_x)=="NaN") _x = nowPosX;
					if (_y==null||""+parseInt(_y)=="NaN") _y = nowPosY;
					if (_speed==null||""+parseInt(_speed)=="NaN") _speed = 5;
					
					var targetPosX = _x;
					var targetPosY = _y;
					
					if (_afterAction==null) _afterAction = "";
					
					if (targetPosX==nowPosX&&targetPosY==nowPosY) {
						this.setAttribute("moveStatus","");
						try {
							eval(_afterAction);
						} catch(e) {
						
						}
					} else {
						var denominator = parseInt(_speed);
						var movePosX = 0;
						var movePosY = 0;
						if (targetPosX > nowPosX) {
							movePosX = Math.ceil((targetPosX - nowPosX) / denominator);
						} else {
							movePosX = (Math.ceil((nowPosX - targetPosX) / denominator))*-1;
						}
						if (targetPosY > nowPosY) {
							movePosY = Math.ceil((targetPosY - nowPosY) / denominator);
						} else {
							movePosY = (Math.ceil((nowPosY - targetPosY) / denominator))*-1;
						}
						
						this.setStyle("left", nowPosX + movePosX);
						this.setStyle("top", nowPosY + movePosY);
						this.moveToTimer = setTimeout("$('"+this.id+"').moveTo('"+_x+"', '"+_y+"', '"+_afterAction.replaceQuot()+"', '"+_speed+"', true)", 10);
					}
				}catch(e) {
					alert("KidoScript Error Form $().moveTo() : " + e);
				}
			}
		}
	}
	
	obj.moveToMotionA = function(_x, _y, _afterAction, _isReCall) {
		if (!this.isNull) {
			var useMultiCommand = (this.getAttribute("useMultiCommandForMoveTo")=="true");
			if (useMultiCommand&&_isReCall!=true&&this.getAttribute("moveStatus")=="ing") {
				clearTimeout(this.moveToTimer);
				this.setAttribute("moveStatus","");
			}
			if (_isReCall==true||this.getAttribute("moveStatus")!="ing") {
				try {
					this.setAttribute("moveStatus","ing");
					if (_afterAction==null) _afterAction = "";
					if (_afterAction=="start") {
						this.setStyle("display",true);
						_afterAction = "";
					}
					var speed = 10;
					var x = this.getX();
					var y = this.getY();
					if (_x==null||""+parseInt(_x)=="NaN") _x = x;
					if (_y==null||""+parseInt(_y)=="NaN") _y = y;
					if (x==_x&&x==_x) {
						this.setAttribute("moveStatus","");
						if (_afterAction!="") {
							try {
								eval(_afterAction);
							} catch(e) {
							
							}
						}
					} else {
						var targetX = this._getNextPoint(x,_x,speed);
						var targetY = this._getNextPoint(y,_y,speed);
						this.setStyle("left",targetX);
						this.setStyle("top",targetY);
						this.moveToTimer = setTimeout("$('"+this.id+"').moveToMotionA('"+_x+"', '"+_y+"', '"+_afterAction.replaceQuot()+"', true)", 10);
					}
				}catch(e) {
					alert("KidoScript Error Form $().moveToMotionA() : " + e);
				}
			}
		}
	}
	
	obj.moveToTarget = function(_targetId, _afterAction, _isReCall) {
		if (!this.isNull) {
			var useMultiCommand = (this.getAttribute("useMultiCommandForMoveTo")=="true");
			if (useMultiCommand&&_isReCall!=true&&this.getAttribute("moveStatus")=="ing") {
				clearTimeout(this.moveToTimer);
				this.setAttribute("moveStatus","");
			}
			if (_isReCall==true||this.getAttribute("moveStatus")!="ing") {
				try {
					if (!$(_targetId).isNull) {
						this.setAttribute("moveStatus","ing");
						var targetPosX = $(_targetId).getX();
						var targetPosY = $(_targetId).getY();
						
						var nowPosX = this.getX();
						var nowPosY = this.getY();
						
						if (_afterAction==null) _afterAction = "";
						
						if (targetPosX==nowPosX&&targetPosY==nowPosY) {
							this.setAttribute("moveStatus","");
							try {
								eval(_afterAction);
							} catch(e) {
							
							}
						} else {
							var denominator = 2.5;
							var movePosX = 0;
							var movePosY = 0;
							if (targetPosX > nowPosX) {
								movePosX = Math.ceil((targetPosX - nowPosX) / denominator);
							} else {
								movePosX = (Math.ceil((nowPosX - targetPosX) / denominator))*-1;
							}
							if (targetPosY > nowPosY) {
								movePosY = Math.ceil((targetPosY - nowPosY) / denominator);
							} else {
								movePosY = (Math.ceil((nowPosY - targetPosY) / denominator))*-1;
							}
							this.setStyle("left", nowPosX + movePosX);
							this.setStyle("top", nowPosY + movePosY);
							this.moveToTimer = setTimeout("$('"+this.id+"').moveToTarget('"+_targetId+"', '"+_afterAction.replaceQuot()+"', true)", 10);
						}
					} else {
						this.setAttribute("moveStatus","");
					}
				}catch(e) {
					alert("KidoScript Error Form $().moveToTarget() : " + e);
				}
			}
		}
	}
	
	return obj;
}

//open popup
function openPopupWindow(sUrl, nTop, nLeft, nWidth, nHeight, sScrollbars) {
		var nScrollbars = 0;
		if (sScrollbars=="Y") nScrollbars = 1;	window.open(sUrl,"_blank","top="+nTop+",left="+nLeft+",width="+nWidth+",height="+nHeight+",toolbar=0,menubar=0,scrollbars="+nScrollbars+",resizable=yes,status=yes");
}

String.prototype.replaceAll = function(str1, str2) {
	var temp_str = "";

	if (this.trim() != "" && str1 != str2) {
		temp_str = this.trim();

		while (temp_str.indexOf(str1) > -1) {
			temp_str = temp_str.replace(str1, str2);
		}
	}
	return temp_str;
}


/**
 * ÇÔ ¼ö ¸í : GetCookie()
 * ±â    ´É : ÄíÅ° °¡Á®¿À±â
 * ÆÄ¶ó¹ÌÅÍ : name -> ÄíÅ°¸í
 * ÀÛ ¼º ÀÏ : 2006-12-26 °íÀçÇÊ Ãß°¡
 *
 */
function GetCookie(name) {
	var nameOfCookie=name+'=';
	var x=0;
	while(x<=document.cookie.length) {
		var y=(x+nameOfCookie.length);
		if(document.cookie.substring(x,y)==nameOfCookie ) {
			if((endOfCookie=document.cookie.indexOf(';',y))==-1) endOfCookie = document.cookie.length;
			return unescape(document.cookie.substring(y,endOfCookie));
		}
		x=document.cookie.indexOf(' ',x)+1;
		if(x==0) break;
	}
	return '';
}


//view ¿­±â 
function openViewer(sUrl) {
		var nScrollbars = 0;
		//if (sScrollbars=="Y") nScrollbars = 1;

		sUrl = "http://uspchamber.com/common/viewer.php?image_url=" + sUrl;
		nTop = 100;
		nLeft = 100;
		nWidth = 500;
		nHeight = 500;

		window.open(sUrl,"_blank","top="+nTop+",left="+nLeft+",width="+nWidth+",height="+nHeight+",toolbar=0,menubar=0,scrollbars="+nScrollbars+",resizable=yes,status=yes");
}


