﻿var Browser			= {};
Browser.userAgent	= navigator.userAgent.toLowerCase();
Browser.Opera		= ((Browser.userAgent.indexOf('opera') != -1) || (typeof(window.opera) != 'undefined'));
Browser.Safari		= ((Browser.userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
Browser.Webtv		= (Browser.userAgent.indexOf('webtv') != -1);
Browser.IE			= ((Browser.userAgent.indexOf('msie') != -1) && (!Browser.Opera) && (!Browser.Safari) && (!Browser.Webtv));
Browser.IE4			= ((Browser.IE) && (Browser.userAgent.indexOf('msie 4.') != -1));
Browser.IE6			= ((Browser.userAgent.indexOf('msie 6.') != -1) && Browser.IE);
Browser.IE7			= ((Browser.userAgent.indexOf('msie 7.') != -1) && Browser.IE);
Browser.Mozila		= ((navigator.product == 'Gecko') && (!Browser.Safari));
Browser.Kon			= (Browser.userAgent.indexOf('konqueror') != -1);
Browser.Nes			= ((Browser.userAgent.indexOf('compatible') == -1) && (Browser.userAgent.indexOf('mozilla') != -1) && (!Browser.Opera) && (!Browser.Webtv) && (!Browser.Safari));
Browser.Nes4		= ((Browser.Nes) && (parseInt(navigator.appVersion) == 4));
Browser.Mac			= (Browser.userAgent.indexOf('mac') != -1);

/* ================= Convert ================= */
var Converter = 
{
	toInt : function(obj, defaultValue)
	{
		try 
		{
			return parseInt(obj);
		}catch(err) { return (!defaultValue) ? 0 : defaultValue; }
	},
	
	toBool : function(obj, defaultValue)
	{
		if (obj == "true" || obj == 1)
			return true;
		if (obj == "false" || obj == 0)
			return false;
	},
	
	toFloat : function(obj, defaultValue)
	{
		try 
		{
			return parseFloat(obj);
		}catch(err) { return (!defaultValue) ? 0 : defaultValue; }
	}
}


/* ================= Element ================= */

var Element = 
{
	get : function(id)
	{
		if (id && id.tagName)
			return id;
		return document.getElementById(id);
	},
	
	create : function(tagName, parentNode)
	{
		if (typeof(tagName) == "string")
			return document.createElement(tagName);
		else
			return Element._create(tagName, get(parentNode), {});
	},
	
	_create : function(childs, parent, rootParent)
	{
		var obj		= null;
		var count	= childs.length;
		for (var i = 0 ; i < count ; i++)
		{
			var child	= childs[i];
			var tag		= child[0];//.toUpperCase();
			var atts	= child[1];
			var _childs	= child[2];
			switch (tag)
			{
				case "TR" :
					obj = Element._createTR(parent);
					break;
				case "TD" :
					obj = Element._createTD(parent);
					break;
				case "OPTION" :
					obj = document.createElement(tag);
					parent.options.add(obj);
					break;
				case "UC" :
					eval("obj = new " + atts + "()");
					atts	= _childs;
					_childs = null;
					break;
				case "INPUT" :
					if (Browser.IE)
					{
						obj = document.createElement("<INPUT type='" + atts["type"] + "' name='" + atts["name"] + "'></INPUT>");
						atts["type"] = null;
						atts["name"] = null;
						delete atts["type"];
						delete atts["name"];
						if (obj && parent) parent.appendChild(obj);
						break;
					}
					
				default :
					obj = document.createElement(tag);
					if (obj && parent) parent.appendChild(obj);
					break;
			}
			if (obj)
			{
				for (var att in atts)
				{
					switch (att)
					{
						case "style" :
							var styles = atts[att];
							for (var s in styles)
							{
								switch (s)
								{
									case "opacity" :
										Element.makeOpacity(obj, styles[s]);
										break;
									default:
										obj.style[s] = styles[s];
										break;
								}
							}
							break;
						
						case "innerHTML" :
							if (Browser.IE)
							{
								var parent = obj.parentNode;
								if (parent)
								{
									parent.removeChild(obj);
									obj[att] = atts[att];
									parent.appendChild(obj);
								}
							}
							else
								obj[att] = atts[att];
							break;
							
						case "text" : 
							if (Browser.IE)
								obj.innerText = atts[att];
							else
								obj.textContent = atts[att];
							break;
							
						case "ID" :
							rootParent[atts[att]] = obj;
							break;
						
						case "validateControl" :
							obj[att] = rootParent[atts[att]];
							break;
						
						case "onsubmit" :
							obj.onclick		 = ValidateControl.registerValidate.eventHandle(ValidateControl, atts["group"], atts["onsubmit"]);
							atts["group"]	 = null;
							atts["onsubmit"] = null;
							delete atts["group"];
							delete atts["onsubmit"];
							break;
							
						default :
							obj[att] = atts[att];
							break;
					}
				}
				if (tag == "UC")
					obj.create(parent);
				if (_childs != null && _childs.length > 0)
					Element._create(_childs, obj, rootParent);
			}
		}
		return rootParent;
	},
	
	_createTR : function(table)
	{
		if (Browser.IE)
			return table.insertRow();
		else
		{
			var tr = document.createElement("TR");
			table.appendChild(tr);
			return tr;
		}	
	},
	
	_createTD : function(tr)
	{
		if (Browser.IE)
			return tr.insertCell();
		else
		{
			var td = document.createElement("TD");
			tr.appendChild(td);
			return td;
		}	
	},
	
	getSize : function(element)
	{
		element		= get(element);
		var size	= {
						width	: element.offsetWidth, 
						height	: element.offsetHeight
					  };
		return size;
	},
	
	setSize : function(element, newSize)
	{
		element = get(element);
		if (element == null)
			return;
		try
		{
			if (newSize.height) element.style.height	= newSize.height + "px";
			if (newSize.width)	element.style.width		= newSize.width + "px";
		}catch(err){}
	},
	
	setPosition : function(element, x, y)
	{
		element = get(element);
		element.style.left = x + "px";
		element.style.top = y + "px";
	},
	
	getPosition : function(element)
	{
		return [parseInt(element.style.left), parseInt(element.style.top)];
	},
	
	getRealOffsetPosition : function(element)
	{
		var valueT = 0, valueL = 0;
		do
		{
			valueT += element.offsetTop || 0;
			valueL += element.offsetLeft || 0;
			element = element.offsetParent;
			if (element)
			{
				//if ()
				//	break;
				//var p = element.style.position;
				//if (p == 'relative' || p == 'absolute')
				//	break;
			}
		}while (element && element.tagName != 'BODY');
		return [valueL, valueT];
	},
	
	show : function(element)
	{
		get(element).style.display = '';
	},
	
	hide : function(element)
	{
		get(element).style.display = 'none';
	},
	
	text : function(element)
	{
		var obj	= get(element);
		return obj.innerText || obj.textContent;
	},
	
	setText : function(element, value)
	{
		var obj	= get(element);
		if (Browser.IE)
			obj.innerText = value;
		else
			obj.textContent = value;
	},
	
	makeOpacity : function(element, opacity)
	{
		if (Browser.IE)
		{
			var filter = element.style['filter'], 
				style = element.style;
			if (opacity == 1 || opacity === '') 
			{
				style.filter = filter.replace(/alpha\([^\)]*\)/gi, '');
			} 
			else
			{ 
				if (opacity < 0.00001) value = 0;
				style.filter = filter.replace(/alpha\([^\)]*\)/gi, '') + 'alpha(opacity=' + (opacity * 100) + ')';
			}
		}
		else
			element.style.opacity = opacity;
	},
	
	refreshNode : function(element)
	{
		element = get(element);
		var p = element.parentNode;
		if (p)
		{
			p.removeChild(element);
			p.appendChild(element);
		}
	},
	
	isInDoc : function(element)
	{
		if (Browser.Mozila)
		{
			var curID	= element.id;
			element.id	= curID + "test_in_doc_123456";
			var result	= (document.getElementById(curID + "test_in_doc_123456") != null);
			element.id	= curID;
			return result;
		}
		else
			return document.body.contains(element);
	}
}

function get(id)
{
	return Element.get(id);
}

function gets(name)
{
	return document.getElementsByTagName(name);
}

function create(tag, parentNode)
{
	return Element.create(tag, parentNode);
}

function inherit(obj, base)
{
	for (var o in base)
		obj[o] = base[o];
}

function isFunction(func)
{
	return (func != null && typeof(func) == typeof(Function));
}

var Events =
{
	cancel : function(e)
	{
		if (!e)
			return;
		if (Browser.IE)
		{
			e.returnValue = false;
			e.cancelBubble = true;
			return e;
		}
		else
		{
			e.stopPropagation();
			e.preventDefault();
			return e;
		}
	},
	
	element : function(e)
	{
		return (e.target || e.srcElement);
	}
};


/* ================= String ================= */

String.format = function(template, object)
{
	return Template.evaluate(template, object, /(^|.|\r|\n)(\{(.*?)\})/);
}

/* ================= Date ================= */

Date.prototype.toCodeFormat = function(template)
{
	var f = function(num){return (num	< 10) ? '0' + num : num;};

	return String.format
	(
		"{MM}/{dd}/{yyyy}", 
		{
			dd		: f(this.getDate()),
			MM		: f(this.getMonth() + 1),
			yyyy	: this.getFullYear()
		}
	);
}

/* ================= Array ================= */

Array.prototype.select = function(func)
{
	var arr = [];
	for (var i = 0 ; i < this.length ; i++)
	{
		var item = func(this[i], i);
		if (item)
			arr.push(item);
	}
	return arr;
}


/* ================= Template ================= */

var Templates = 
{
	Pattern : /(^|.|\r|\n)(#\{(.*?)\})/	,			// #{ name }
	//		  /(^|.|\r|\n)(\<%=\s*(\w+)\s*%\>)/		// <%= %>
	
	evaluate: function(template, object, pattern)
	{
		template	= template.toString();
		pattern		= pattern || Template.Pattern;
		
		var result = '', source = template, match;

		while (source.length > 0) 
		{
			if (match = source.match(pattern)) 
			{
				result += source.slice(0, match.index);
				{
					var before = match[1];
					if (before == '\\') return match[2];
					result += before + ((o = object[match[3]]) ? o : '');
				};
				source  = source.slice(match.index + match[0].length);
			}
			else
				result += source, source = '';
		}
		return result;
	}
}

/* ================= Function ================= */

var $Array = function(iterable) 
{
	if (!iterable) return [];
	if (iterable.toArray) 
	{
		return iterable.toArray();
	} else {
		var results = [];
		for (var i = 0, length = iterable.length; i < length; i++)
			results.push(iterable[i]);
		return results;
	}
}

Function.prototype.handle = function() 
{
	var __method = this, args = $Array(arguments), object = args.shift();
	return function() 
	{
		return __method.apply(object, args.concat($Array(arguments)));
	}
}
	
Function.prototype.eventHandle = function(object) 
{
	var __method = this, args = $Array(arguments), object = args.shift();
	return function(event)
	{
		return __method.apply(object, [event || window.event].concat(args));
	}
}

/* ================= Function ================= */
var Classes = function(args)
{
	var _class = function(args)
	{
		for (var i in args)
			this[i] = args[i];
		this.init();
	}
	return _class;
}

/* ================= PopMenu ================= */

var PopupMenu = new Classes();
PopupMenu.prototype = 
{
	menu		: null,
	moved		: false,
	moveObj		: null,
	useMask		: false,
	resetPos	: false,
	
	init : function()
	{
		if (this.menu)
		{
			if (this.moved)
			{
				var tmp = create
				(
					[
						["DIV", {ID : "DocZone", style : {width : "100%", height : "100%", background : "#fff",
								 zIndex : 997, position : "fixed", top : "0px", left : "0px", display : "none",
								 opacity : 0.5
								}}],
						["DIV", {ID : "Zone", style : {width : "100%", height : "100%",
								 zIndex : 998, position : "fixed", top : "0px", left : "0px", display : "none"
								}},[
							["DIV", {ID : "Mask", style : {width : "1px", height : "1px", background : "#888",
								 zIndex : 999, position : "absolute", top : "0px", left : "0px", display : "none",
								 opacity : 0.5, cursor : "default", border : "1px solid #000"}}]
						]]
					],
					document.body
				);
				this.DocZone	= tmp.DocZone;
				this.Zone		= tmp.Zone;
				this.Mask		= tmp.Mask;
				this.menu.style.zIndex	 = 998;
			}
			
			this.menu = get(this.menu);
			this.menu.style.position = "absolute";
			
			if (this.moved)
			{
				Element.show(this.menu);
				if (this.x == null && this.y == null)
				{
					var pos = Element.getRealOffsetPosition(this.menu);
					this.x	= pos[0];
					this.y	= pos[1];
				}
				this.saveParent = this.menu.parentNode;
				this.saveParent.removeChild(this.menu);
				this.Zone.appendChild(this.menu);
			}
			Element.hide(this.menu);
		}
	},
	
	show : function(x, y)
	{
		this.saveDocMouseDown	= document.onmousedown;
		this.saveMenuMouseDown	= this.menu.onmousedown;
		document.onmousedown	= this._docMouseDown.handle(this);
		this.menu.onmousedown	= this._menuMouseDown.handle(this);
		
		if (this.moved)
		{
			if (!this.moveObj) this.moveObj = this.menu;
			
			this.saveDocMouseUp			= document.onmouseup;
			this.saveDocMouseMove		= document.onmousemove;
			this.saveMoveObjMouseDown	= this.moveObj.onmousedown;
			if (Browser.IE)
			{
				this.saveSelectStart	= document.onselectstart;
				document.onselectstart	= this._onSelectStart.handle(this);
			}
			
			document.onmouseup			= this._docMouseUp.handle(this);
			document.onmousemove		= this._docMouseMove.handle(this);
			this.moveObj.onmousedown	= this._moveObjMouseDown.handle(this);
			if (this.resetPos)
				this.savePos = Element.getPosition(this.menu);
			if (x == null || y == null)
				Element.setPosition(this.menu, this.x, this.y);
			else
				Element.setPosition(this.menu, x, y);
				
			Element.show(this.menu);
			Element.show(this.DocZone);
			Element.show(this.Zone);		
		}
		else
		{
			if (x != null && y != null)
				Element.setPosition(this.menu, x, y);
			Element.show(this.menu);
		}
		this._allowClose = true;
		JSViewLargeImage._size(799,500);
	},
	
	hide : function()
	{
		if (this.moved)
		{
			document.onmouseup			= this.saveDocMouseUp;
			document.onmousemove		= this.saveDocMouseMove;
			this.moveObj.onmousedown	= this.saveMoveObjMouseDown;
			this.saveDocMouseUp	= this.saveDocMouseMove = this.saveMoveObjMouseDown = null;
			if (Browser.IE)
			{
				document.onselectstart	= this.saveSelectStart;
				this.saveSelectStart	= null;
			}
			//this.Zone.removeChild(this.menu);
			//this.saveParent.appendChild(this.menu);
			//this.saveParent = null;
			Element.hide(this.DocZone);
			Element.hide(this.Zone);
			
			if (this.resetPos)
			{
				Element.setPosition(this.menu, this.savePos[0], this.savePos[1]);
				this.x = this.y = null;
			}
		}
		document.onmousedown	= this.saveDocMouseDown;
		this.menu.onmousedown	= this.saveMenuMouseDown;
		this.saveDocMouseDown	= this.saveMenuMouseDown = null;
		Element.hide(this.menu);
	},
	
	_menuMouseDown : function(e)
	{
		this._allowClose = false;
		if (this.saveMenuMouseDown) this.saveMenuMouseDown(e);
	},
	
	_moveObjMouseDown : function(e)
	{
		if (!e) e = window.event;
		if ((Browser.IE && e.button == 1) || (!Browser.IE && e.button == 0))
		{
			if (Event.element(e) == this.moveObj)
			{
				this._moving = true;
				this.lastX	= e.clientX;
				this.lastY	= e.clientY;
				this.width	= this.menu.offsetWidth;
				this.height	= this.menu.offsetHeight;
				this.x		= parseInt(this.menu.style.left);
				this.y		= parseInt(this.menu.style.top);
				if (this.useMask)
				{
					Element.setSize(this.Mask, {width : this.width, height : this.height});
					Element.setPosition(this.Mask, this.x, this.y);
					Element.show(this.Mask);
				}
				document.body.style.MozUserSelect		= "none";
				document.body.style.WebkitUserSelect	= "none";
				this.Zone.style.MozUserSelect			= "none";
				this.Zone.style.WebkitUserSelect		= "none";
			}
		}
		if (this.saveMoveObjMouseDown) this.saveMoveObjMouseDown(e);
	},
	
	_endMove : function()
	{
		this._moving = false;
		if (this.useMask)
			Element.hide(this.Mask);
		document.body.style.MozUserSelect		= "text";
		document.body.style.WebkitUserSelect	= "text";
		this.Zone.style.MozUserSelect			= "text";
		this.Zone.style.WebkitUserSelect		= "text";
	},
	
	_docMouseDown : function(e)
	{
		if (this._allowClose)
			this.hide();
		else
			this._allowClose = true;
		if (this.saveDocMouseDown) this.saveDocMouseDown(e);
	},
	
	_docMouseUp : function(e)
	{
		if (this._moving)
		{
			this._endMove();
			if (this.useMask)
				Element.setPosition(this.menu, this.x, this.y);
		}
	},
	
	_docMouseMove : function(e)
	{
		if (!e) e = window.event;
		if (this._moving && ((Browser.IE && e.button != 1) || (!Browser.IE && e.button != 0)))
		{
			this._endMove();
			return;
		}
		if (this._moving)
		{
			var curX	= e.clientX,
				curY	= e.clientY,
				x		= this.x + curX - this.lastX,
				y		= this.y + curY - this.lastY,
				w		= this.Zone.offsetWidth,
				h		= this.Zone.offsetHeight;
			if (x < 0) x = 0;
			if (x + this.width >= w) x = w - this.width;
			if (y < 0) y = 0;
			if (y + this.height >= h) y = h - this.height;
			this.lastX	= curX;
			this.lastY	= curY;
			this.x		= x;
			this.y		= y;
			if (this.useMask)
				Element.setPosition(this.Mask, x, y);
			else
				Element.setPosition(this.menu, x, y);
		}
	},
	
	_onSelectStart : function(e)
	{
		if (this._moving)
		{
			if (!e) e = window.event;
			Events.cancel(e);
		}
	},
	
	centerPage : function()
	{
		var		w	= this.Zone.offsetWidth,
				h	= this.Zone.offsetHeight;
		var		x	= (w - this.menu.offsetWidth) / 2,
				y	= (h - this.menu.offsetHeight) / 2;
		Element.setPosition(this.menu, x, y);
	}
}