﻿
you9ri = function()
{
}
you9ri.Common = function()
{
}
you9ri.Common.prototype = {
	getClientBounds : function() {
		/// <summary>
		/// Gets the width and height of the browser client window (excluding scrollbars)
		/// </summary>
		/// <returns type="Sys.UI.Bounds">
		/// Browser's client width and height
		/// </returns>

		var clientWidth;
		var clientHeight;
		switch(Sys.Browser.agent) {
			case Sys.Browser.InternetExplorer:
				clientWidth = document.documentElement.clientWidth;
				clientHeight = document.documentElement.clientHeight;
				break;
			case Sys.Browser.Safari:
				clientWidth = window.innerWidth;
				clientHeight = window.innerHeight;
				break;
			case Sys.Browser.Opera:
				clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
				clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
				break;
			default:  // Sys.Browser.Firefox, etc.
				clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
				clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
				break;
		}
		return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
	},
	getLocation : function(element) {
		/// <summary>Gets the coordinates of a DOM element.</summary>
		/// <param name="element" domElement="true"/>
		/// <returns type="Sys.UI.Point">
		///   A Point object with two fields, x and y, which contain the pixel coordinates of the element.
		/// </returns>

		// workaround for an issue in getLocation where it will compute the location of the document element.
		// this will return an offset if scrolled.
		//
		if (element === document.documentElement) {
			return new Sys.UI.Point(0,0);
		}

		// Workaround for IE6 bug in getLocation (also required patching getBounds - remove that fix when this is removed)
		if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7) {
			if (element.window === element || element.nodeType === 9 || !element.getClientRects || !element.getBoundingClientRect) return new Sys.UI.Point(0,0);

			// Get the first bounding rectangle in screen coordinates
			var screenRects = element.getClientRects();
			if (!screenRects || !screenRects.length) {
				return new Sys.UI.Point(0,0);
			}
			var first = screenRects[0];

			// Delta between client coords and screen coords
			var dLeft = 0;
			var dTop = 0;

			var inFrame = false;
			try {
				inFrame = element.ownerDocument.parentWindow.frameElement;
			} catch(ex) {
				// If accessing the frameElement fails, a frame is probably in a different
				// domain than its parent - and we still want to do the calculation below
				inFrame = true;
			}

			// If we're in a frame, get client coordinates too so we can compute the delta
			if (inFrame) {
				// Get the bounding rectangle in client coords
				var clientRect = element.getBoundingClientRect();
				if (!clientRect) {
					return new Sys.UI.Point(0,0);
				}

				// Find the minima in screen coords
				var minLeft = first.left;
				var minTop = first.top;
				for (var i = 1; i < screenRects.length; i++) {
					var r = screenRects[i];
					if (r.left < minLeft) {
						minLeft = r.left;
					}
					if (r.top < minTop) {
						minTop = r.top;
					}
				}

				// Compute the delta between screen and client coords
				dLeft = minLeft - clientRect.left;
				dTop = minTop - clientRect.top;
			}

			// Subtract 2px, the border of the viewport (It can be changed in IE6 by applying a border style to the HTML element,
			// but this is not supported by ASP.NET AJAX, and it cannot be changed in IE7.), and also subtract the delta between
			// screen coords and client coords
			var ownerDocument = element.document.documentElement;
			return new Sys.UI.Point(first.left - 2 - dLeft + ownerDocument.scrollLeft, first.top - 2 - dTop + ownerDocument.scrollTop);
		}

		return Sys.UI.DomElement.getLocation(element);
	},

	setLocation : function(element, point) {
		/// <summary>
		/// Sets the current location for an element.
		/// </summary>
		/// <param name="element" type="Sys.UI.DomElement" domElement="true">
		/// DOM element
		/// </param>
		/// <param name="point" type="Object">
		/// Point object (of the form {x,y})
		/// </param>
		/// <remarks>
		/// This method does not attempt to set the positioning mode of an element.
		/// The position is relative from the elements nearest position:relative or
		/// position:absolute element.
		/// </remarks>
		Sys.UI.DomElement.setLocation(element, point.x, point.y);
	},
	
	print : function(msg){
		document.body.appendChild(document.createTextNode(msg));
		document.body.appendChild(document.createElement("br"));
	}
}

/********************************************************************/
/*																	*/
/*																	*/
/*						Modal PopUp									*/
/*																	*/
/*																	*/
/********************************************************************/
you9ri.ModalPopup = function()
{
	this._backgroundId = null;
	this._contentsId = null;

	this._backgroundElement = null;
	this._contentsElement = null;
	this._relativeOrAbsoluteParentElement = null;
	this._scrollHandler = null;
	this._resizeHandler = null;
	this._isIE6 = false;
	this._xCoordinate = -1;
	this._yCoordinate = -1;
}
you9ri.ModalPopup.prototype ={
	init : function(backgroundId , contentsId)
	{
		this._backgroundId = backgroundId;
		this._contentsId = contentsId;
		
		this._backgroundElement = $get(backgroundId);
		this._contentsElement = $get(contentsId);
		
		if (this._isIE6) {
			this._contentsElement.style.position = 'absolute';
			this._backgroundElement.style.position = 'absolute'; 
			// find the relative or absolute parent
			var tempRelativeOrAbsoluteParent = this._contentsElement.parentNode;
			while (tempRelativeOrAbsoluteParent && (tempRelativeOrAbsoluteParent != document.documentElement)) {
				if((tempRelativeOrAbsoluteParent.style.position != 'relative') && (tempRelativeOrAbsoluteParent.style.position != 'absolute')) {
					tempRelativeOrAbsoluteParent = tempRelativeOrAbsoluteParent.parentNode;
				} else {
					this._relativeOrAbsoluteParentElement = tempRelativeOrAbsoluteParent;
					break;
				}
			}                       
		}        
	    
		this._scrollHandler = Function.createDelegate(this, this._onLayout);
		this._resizeHandler = Function.createDelegate(this, this._onLayout);
		$addHandler(window, 'resize', this._resizeHandler);
		$addHandler(window, 'scroll', this._scrollHandler);
	},
    dispose : function() {
        this._backgroundElement.style.display = 'none';
        this._contentsElement.style.display = 'none';
        if (this._scrollHandler) {
            $removeHandler(window, 'scroll', this._scrollHandler);
        }
        if (this._resizeHandler) {
            $removeHandler(window, 'resize', this._resizeHandler);
        }
        this._scrollHandler = null;
        this._resizeHandler = null;
    },
	show : function (){
		this._backgroundElement.style.display = "inline";
		this._contentsElement.style.display = "inline";
		this._layout();	
	},
	_onLayout : function(e) {
		if (e.type === 'scroll') {
			this._layout();
		} else if (e.type === 'resize') {
			this._layout();
		} else {
			this._layoutBackgroundElement();
		}
	},
	_layout : function() {
		var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
               
		var clientBounds = $you9ri_common.getClientBounds();
		var clientWidth = clientBounds.width;
		var clientHeight = clientBounds.height;
        
		this._layoutBackgroundElement();

		var xCoord = 0;
		var yCoord = 0;
		if(this._xCoordinate < 0) {
			var foregroundelementwidth = this._contentsElement.offsetWidth? this._contentsElement.offsetWidth: this._contentsElement.scrollWidth;
			xCoord = ((clientWidth-foregroundelementwidth)/2);
			if (this._contentsElement.style.position == 'absolute') {
				xCoord += scrollLeft;
			}
			this._contentsElement.style.left = xCoord + 'px';
            
		} else {
			if(this._isIE6) {
				this._contentsElement.style.left = (this._xCoordinate + scrollLeft) + 'px';
				xCoord = this._xCoordinate + scrollLeft;
			}
			else {
				this._contentsElement.style.left = this._xCoordinate + 'px';
				xCoord = this._xCoordinate;
			}
		}
		if(this._yCoordinate < 0) {
			var foregroundelementheight = this._contentsElement.offsetHeight? this._contentsElement.offsetHeight: this._contentsElement.scrollHeight;
			yCoord = ((clientHeight-foregroundelementheight)/2); 
			if (this._contentsElement.style.position == 'absolute') {
				yCoord += scrollTop;
			}
			this._contentsElement.style.top = yCoord + 'px';
          
		} else {
			if(this._isIE6) {
				this._contentsElement.style.top = (this._yCoordinate + scrollTop) + 'px';
				yCoord = this._yCoordinate + scrollTop;
			}
			else {
				this._contentsElement.style.top = this._yCoordinate + 'px';
				yCoord = this._yCoordinate;
			}
		}
		this._layoutForegroundElement(xCoord, yCoord);
		this._layoutBackgroundElement();
	},
	_layoutForegroundElement : function(xCoord, yCoord) {
        
		if (this._isIE6 ) {
			var foregroundLocation = $you9ri_common.getLocation(this._contentsElement);  
			var relativeParentLocation = $you9ri_common.getLocation(this._relativeOrAbsoluteParentElement);
			var getLocationXCoord = foregroundLocation.x;
			if (getLocationXCoord != xCoord) {
				// offset it by that amount
				this._contentsElement.style.left = (xCoord - relativeParentLocation.x) + 'px';
			} 
                        
			var getLocationYCoord = foregroundLocation.y;
			if (getLocationYCoord != yCoord) {
				// offset it by that amount
				this._contentsElement.style.top = (yCoord - relativeParentLocation.y) + 'px';
			} 
		}
	},
	_layoutBackgroundElement : function() {
		if(this._isIE6) { 
			var backgroundLocation = $you9ri_common.getLocation(this._backgroundElement);
			var backgroundXCoord = backgroundLocation.x;
			if (backgroundXCoord != 0) {
				this._backgroundElement.style.left = (-backgroundXCoord) + 'px';
			} 
			var backgroundYCoord = backgroundLocation.y;
			if (backgroundYCoord != 0) {
				this._backgroundElement.style.top = (-backgroundYCoord) + 'px';
			}         
		}
		var clientBounds = $you9ri_common.getClientBounds();
		var clientWidth = clientBounds.width;
		var clientHeight = clientBounds.height;
		this._backgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth)+'px';
		this._backgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight)+'px';
	}
}

/********************************************************************/
/*																	*/
/*																	*/
/*						file Upload									*/
/*																	*/
/*																	*/
/********************************************************************/
you9ri.ajaxFileUpload = function()
{
	this._id = "";
	this._btnId = null;
	this._iframeId = "";
	this._frm = new Object();
	this._frmAction = "";
	this._frmTarget = "";
	this._origin_frmAction = "";
	this._origin_frmTarget = "";
	this._savePath = "";
	this._fileCssClass = "";
	this._width = null;
	this._height = null;
	this._exceptFile = null;
	this._allowFile =null;
	
	this._fileId = "";
	this._fileChangeHandler = null;
	this._submitHandler = null;
	this._isRequired = true;
}
you9ri.ajaxFileUpload.prototype = {
	init : function(settings)
	{
		this._id = settings.controlId;
		this._fileId = this._id + "_file";
		this._savePath = settings.savePath;
		this._frm = document.forms[0];
		this._frmAction = settings.action;
		this._frmTarget = settings.target;
		this._origin_frmAction = this._frm.action;
		this._origin_frmTarget = this._frm.target;
		this._savePath = settings.savePath;
		if(settings.fileCssClass != null) 
			this._fileCssClass = settings.fileCssClass;
		if(settings.width != null)
			this._width = settings.width;
		if(settings.height != null)
			this._height = settings.height;
		if(settings.allowFile != null)
			this._allowFile = settings.allowFile;
		if(settings.exceptFile != null)
			this._exceptFile = settings.exceptFile;
		if(settings.btnId != null)
		{
			this._btnId = settings.btnId;
			this._submitHandler = Function.createDelegate(this, this._submit);
			$addHandler($get(this._btnId), 'click', this._submitHandler);
		}
		if(settings.isRequired != null)
			this._isRequired = settings.isRequired;
			
		this._createSavePathTag();
		this._createFileTag();
		//this._createIframe();
	},
	_createSavePathTag :function()
	{
		var obj = $get(this._id);
		var savePathTag = document.createElement("input");
		
		savePathTag.type = "hidden";
		savePathTag.id = this._id + "_path";
		savePathTag.name = this._id + "_path";
		savePathTag.value = this._savePath;
		obj.appendChild(savePathTag);
	},
	_createIframe : function()
	{
		var obj = $get(this._id);
		var iframeTag = document.createElement("iframe");
		iframeTag.id = this._iframeId;
		iframeTag.name = this._iframeId;
		//iframeTag.style.display = "none";
		obj.appendChild(iframeTag);
	},
	_createFileTag: function()
	{
		var obj = $get(this._id);
		var fileTag = document.createElement("input");
		
		fileTag.type = "file";
		fileTag.id = this._fileId;
		fileTag.name = this._fileId;
		fileTag.className = this._fileCssClass;
		if(this._width != null) fileTag.style.width = this._width;
		if(this._height != null) fileTag.style.height = this._height;
		obj.appendChild(fileTag);
		this._fileChangeHandler = Function.createDelegate(this, this._checkFile);
		$addHandler(fileTag, 'change', this._fileChangeHandler);
		
	},
	_reset : function()
	{
        $removeHandler($get(this._fileId), 'change', this._fileChangeHandler);
		$get(this._id).removeChild($get(this._fileId));
        this._fileChangeHandler = null;
		this._createFileTag();
	},
	_checkFile : function()
	{
		var blnCheck = true;
		var fileName = $get(this._fileId).value;
		if(this._allowFile != null)
		{
			blnCheck =new RegExp(this._allowFile, 'gi').test(fileName);
		}
		if(this._exceptFile != null)
		{
			blnCheck = !(new RegExp(this._exceptFile, 'gi').test(fileName));
		}
		if(!blnCheck)
		{
			alert("파일 형식이 올바르지 않습니다.");
			this._reset();
		}
	},
	_submit : function ()
	{
		var bln = true;
		if($get(this._fileId).value == "")
		{
			if(this._isRequired)
			{
				alert("첨부파일이 없습니다");
				bln = false;
			}
		}
		else
		{
			this._frm.target = this._frmTarget;
			this._frm.action= this._frmAction + "?Id=" + this._id; 
			this._frm.submit();
		}
		return bln;
	},
	uploadcomplete : function()
	{
		this._frm.target = this._origin_frmTarget;
		this._frm.action = this._origin_frmAction;
		this._reset();
	}
}

var $you9ri_common = new you9ri.Common();

