﻿// JavaScript Document
/*Base-----------------------------------------------------------------------*/
Object.inherit = function( child, parent ){
	for( property in parent ){
		child[ property ] = parent[ property ];
	}
}
var $break = new Object();
var $continue = new Object();
var IEnumerable = {
	foreach: function( iterator ){
		var index = 0;
		try{
			this.__each( function( value ){ try{ iterator( value, index++ ); }catch( e ){ if( e != $continue ) throw e;}} );
		}
		catch( e ){
			if( e != $break ) throw e;
		}
	}
}
Object.inherit( Array.prototype, {
								__each: function( iterator ){
									for( var i = 0; i < this.length; i++ ){
										iterator( this[i] );
									}
								}
							});

/*Try-----------------------------------------------------------------------*/
var Try = {
	calls: function(){
		var returnValue;
		for( var i = 0; i < arguments.length; i++ ){
			var caller = arguments[i];
			try{
				returnValue = caller();
				break;
			}
			catch(e){}
		}
		return returnValue;
	}
}

/*ICollection-----------------------------------------------------------------------*/
var ICollection = {
	clear: function(){
		this.length = 0;
		return this;
	}
}
Object.inherit( ICollection.constructor.prototype, IEnumerable );

/*IList-----------------------------------------------------------------------*/
var IList = {
	add: function( value ){
		this.push( value );
		return this.length - 1;
	},
	addAt: function( value, index ){
		if( index < 0 || index > this.length )
			throw 'the index is out of the range';
		this.copyTo( index, this, index + 1 );
		this.splice( index, 1, value );
	},
	remove: function( value ){
		var index = 0;
		for( var i = 0; i < this.length; i++ ){
			if( this[i] == value )
				index = i;
		}
		this.copyTo( index + 1, this, index );
		this.length--;
	},
	removeAt: function( index ){
		if( index < 0 || index > this.length )
			return;
		this.copyTo( index + 1, this, index );
		this.length--;
	},
	__copyTo: function( index, length, destination, index2 ){
		if( length > this.length ) length = this.length;
		if( index < 0 || index2 < 0 ) throw 'the index is out of the range';
		if( destination == null ) throw 'destination array can not be null';
		if( destination.constructor.toString().indexOf( 'Array' ) == -1 ) throw 'destination array is not a Array Object';
		var temp = this.slice( index, length );
		temp.foreach(
								 function( value ){
									 destination[index2++] = value;
								 }
								 );
	},
	copyTo: function(){
		var argLen = arguments.length;
		if( argLen == 4 ){
			this.__copyTo( arguments );
		}
		if( argLen == 3 ){
			this.__copyTo( arguments[0], this.length, arguments[1], arguments[2] );
		}
		if( argLen == 2 ){
			this.__copyTo( arguments[0], this.length, arguments[1], arguments[1].length );
		}
		if( argLen == 1 ){
			this.__copyTo( 0, this.length, arguments[0], arguments[0].length );
		}
	},
	contains: function( value ){
		var con = false;
		this.foreach( function( value2 ){ if( value2 == value ) con = true; } );
		return con;
	}
}
Object.inherit( IList.constructor.prototype, ICollection );
Object.inherit( Array.prototype, IList );

/*Hashtable-----------------------------------------------------------------------*/
var IDictionary = {
	__each: function( iterator ){
		var index = 0;
		for( key in this ){
			var value = this[key]
			if( typeof( value ) == 'function' ) continue;
			if( key == '__keys' || key == '__values' ) continue;
			if( value == null ) continue;
			if( index < this.__keys.length ){
				var pair = [ key, value ];
				pair.key = key;
				pair.value = value;
				iterator( pair );
			}
		}
	},
	add: function( key, value ){
		this.__keys.add( key );
		this.__values.add( value );
		this[key] = value;
	},
	remove: function( key ){
		this.__keys.remove( key );
		this.__values.remove( this[key] );
		this[key] = null;
	},
	removeAt: function( index ){
		this.remove( this.__keys[ index ] );
	},
	contains: function( value ){
		return this.__values.contains( value );
	},
	keys: function(){
		return this.__keys;
	},
	values: function(){
		return this.__values;
	},
	clear: function(){
		this.__values.clear();
		this.__keys.clear();
	},
	toHash: function(){
		var spliter = Hashtable.hashSpliter;
		if( spliter == null ){
			spliter = '&';
		}
		var hash = '';
		this.foreach( function( pair ){
													 hash += pair.key + '=' + pair.value + spliter;
													 });
		hash = hash.trim( '&' );
		return hash;
	}
}
var Hashtable = function(){
	this.__keys = new Array();
	this.__values = new Array();
}
Object.inherit( IDictionary.constructor.prototype, IEnumerable );
Object.inherit( Hashtable.prototype, IDictionary );
Hashtable.hashSpliter = '&';
Hashtable.entrySpliter = '=';
Hashtable.fromHash = function( code ){
	var table = new Hashtable();
	var entries = code.split( Hashtable.hashSpliter );
	var pair;
	entries.foreach( function( value ){
														pair = value.split( Hashtable.entrySpliter );
														table.add( pair[0], pair[1] );
														});
	return table;
}

/*String Extend-------------------------------------------------------*/
Object.inherit( String.prototype, {
			escape: function(){
				return escape( this );
			},
			unescape: function(){
				return unescape( this );
			},
			encodeURI: function(){
				return encodeURI( this );
			},
			decodeURI: function(){
				return decodeURI( this );
			},
			encodeURIComponent: function(){
				return encodeURIComponent( this );
			},
			decodeURIComponent: function(){
				return decodeURIComponent( this );
			},
			trim: function(){
				var temp = this;
				if( arguments.length == 0 ){
					temp = temp.replace( /\s*$/gi, '' );
					temp = temp.replace( /^\s*/gi, '' );
					return temp;
				}
				else{
					var reg = new RegExp( '^' + arguments[0], 'g' );
					temp = temp.replace( reg, '' );
					reg = new RegExp( arguments[0] + '$', 'g' );
					temp = temp.replace( reg, '' );
					return temp;
				}
			},
			trimStart: function(){
				var temp = this;
				if( arguments.length == 0 ){
					temp = temp.replace( /^\s*/gi, '' );
					return temp;
				}
				else{
					var reg = new RegExp( '^' + arguments[0], 'g' );
					temp = temp.replace( reg, '' );
					return temp;
				}
			},
			trimEnd: function(){
				var temp = this;
				if( arguments.length == 0 ){
					temp = temp.replace( /\s*$/gi, '' );
					return temp;
				}
				else{
					var reg = new RegExp( arguments[0] + '$', 'g' );
					temp = temp.replace( reg, '' );
					return temp;
				}
			},
			translateHTML: function(){
					var temp = this;
					temp = temp.replace( /</g, '&lt;' );
					temp = temp.replace( />/g, '&gt;' );
					temp = temp.replace( /\n/g, '<br />' );
					temp = temp.replace( /\t/g, '&nbsp;&nbsp;&nbsp;' );
					temp = temp.replace( /"/g, '&quot;' );
					temp = temp.replace( /&/g, '&amp;' );
					return temp;
			}
});
String.format = function( input ){
	if( input == null || input.length < 1 ){
		return '';
	}
	var temp = input;
	var index = 0;
	var reg;
	while( true ){
		if( temp.indexOf( '{' + index + '}' ) > -1 ){
			reg = new RegExp( '\\{' + index + '\\}', 'gi' );
			temp = temp.replace( reg, arguments[ index + 1 ] );
		}
		else{
			break;
		}
		index++;
	}
	return temp;
}

/*Number Extend-------------------------------------------------------*/
Number.fromPixel = function( num ){
	if( typeof( num ) == 'string' ){
		var reg = /px/gi;
		num = num.replace( reg, '' );
	}
	return num;
}
Number.isNumber = function( num ){
	if( num == null ){
		return false;
	}
	var reg = /^\d+.?\d?$/gi;
	if( reg.test( num.toString() ) ){
		return true;
	}
	return false;
}

/*Boolean Extend-------------------------------------------------------*/
Boolean.parse = function( b ){
	b = b.toLowerCase();
	if( b == 'true' ) return true;
	if( b == 'false' ) return false;
}

/*location Extend-------------------------------------------------------*/
if( location.href.indexOf( '?' ) > -1 ){
	var queryString = location.href.substring( location.href.indexOf( '?' ) + 1 );
	if( queryString != null && queryString.length > 0 ){
		location.query = Hashtable.fromHash( queryString );
	}
}
else{
	location.query = null;
}


var IsIE = (document.all && navigator.userAgent.indexOf('MSIE')>0)?true:false;

/*Date Extend-------------------------------------------------------*/
var DateTime = {
	getDateText: function(){
		return this.toLocaleString().replace( /[^\d]+/ig, '' );
	}
}
Object.inherit( Date.prototype, DateTime );

Date.Now = function(){
	return new Date();
}

/*window Extend-------------------------------------------------------*/
window.clientWidth = function(){
	return document.documentElement.clientWidth;
}
window.clientHeight = function(){
	return document.documentElement.clientHeight;
}
window.contentWidth = function(){
	if( IsIE ){
		if( document.body.currentStyle.marginLeft != null ){
			return document.body.offsetWidth + Number.fromPixel( document.body.currentStyle.marginLeft ) * 2;
		}
		return document.body.offsetWidth;
	}
	else{
		return document.documentElement.offsetWidth;
	}
}
window.contentHeight = function(){
	if( IsIE ){
		if( document.body.currentStyle.marginTop != null ){
			return document.body.offsetHeight + Number.fromPixel( document.body.currentStyle.marginTop ) * 2;
		}
		return document.body.offsetHeight;
	}
	else{
		return document.documentElement.offsetHeight;
	}
}
window.DialogOption = 'center:yes;resizable:no;scroll:yes;status:no;';
window.showDialog = function( url, name, width, height, newWindow ){
	if( newWindow ){
		window.open( url, name, String.format( 'menubar=no,location=no,resizable=no,scrollbars=yes,status=no,screenx={0},screeny={1},left={0},top={1},innerWidth={2},innerHeight={3}', (screen.width - width) /2, (screen.height - height)/2, width, height  ) );
		return;
	}
	if( url != null ){
		if( width == null ){
			width = 600;
		}
		if( height == null ){
			height = 400;
		}
		if( IsIE ){
			window.showModalDialog( url, this, window.DialogOption + 'dialogWidth:' + width + 'px' + ';dialogHeight:' + height + 'px;' );		
		}
		else{
			window.open( url, name, String.format( 'menubar=no,location=no,resizable=no,scrollbars=yes,status=no,screenx={0},screeny={1},left={0},top={1},innerWidth={2},innerHeight={3}', (screen.width - width) /2, (screen.height - height)/2, width, height  ) );
		}
	}
}
if( IsIE ){
	window.getSelection = function(){
		return document.selection;
	}
}
window.getSelectRange = function(){
	if( IsIE ){
		return window.getSelection().createRange().text;
	}
	else{
		if( arguments.length > 0 ){
			var input;
			var start;
			var end;
			if( typeof( arguments[0] ) == 'string' ){
				input = document.getElementById( arguments[0] );
			}
			else{
				input = arguments[0];
			}
			if( input != null ){
				start = input.selectionStart;
				end = input.selectionEnd;
				return input.value.substring( start, end );
			}
		}
		return window.getSelection().getRangeAt(0);
	}
}
window.loadedEventHandler = new Array();
window.onload =  function() { windowonload.raiseEvents(); }

var windowonload = {
		events: [],
		bind: function( iterator ){
			if( typeof( iterator ) == 'function' ){
				this.events.push( iterator );
			}
		},
		remove: function( iterator ){
			this.events.remove( iterator );
		},
		raiseEvents: function(){
			this.events.foreach( function( iterator ){
																			iterator();
																		}
																		)
		}
}

/*common functon-------------------------------------------------------*/
function $(){
	if( arguments.length == 1 ){
		if( typeof( arguments[0] ) == 'string' ){
			return document.getElementById( arguments[0] );
		}
		return arguments[0];
	}
	var elements = new Array();
	var elt;
	for( var i = 0; i < arguments.length; i++ ){
		elt = arguments[i];
		if( typeof( elt ) == 'string' ){
			elt = document.getElementById( elt );
		}
		
		elements.push( elt );
	}
	return elements;
}
function $V(){
	if( arguments.length == 1 ){
		if( typeof( arguments[0] ) == 'string' ){
			return $( arguments[0] ).value;
		}
		return arguments[0].value;
	}
	var elements = new Array();
	for( var i = 0; i < arguments.length; i++ ){
		if( typeof( arguments[i] ) == 'string' ){
			elements.push( $( arguments[i] ).value );
		}
		else{
			elements.push( arguments[i].value );
		}
	}
	return elements;
}
function $F( form ){
	var table = new Hashtable();
	if( typeof( form ) == 'string' ){
		form = $( form );
	}
	fill( table, form, 'input' );
	var removes = new Array();
	table.foreach( function( pair ){
													var type = pair.value.type.toLowerCase();
													if( type == 'button' || type == 'submit' ){
														removes.push( pair.key );
													}
													else{
														table[ pair.key ] = pair.value.value;
													}
													});
	removes.foreach( function( value ){
														table.remove( value );
														});
	return table;
}
function fill( table, node, tag ){
	if( table == null || node == null ) return;
	if( node.nodeType != 1 ) return;
	var nodes = node.childNodes;
	
	for( var i = 0; i < nodes.length; i++ ){
		if( nodes[i].nodeType == 1 && nodes[i].tagName.toLowerCase() == tag.toLowerCase() ) table.add( nodes[i].id, nodes[i] );
		
		fill( table, nodes[i], tag );
	}
}

function $PF( node ){
	node = $( node );
	if( node == null ){
		return;
	}
	if( node.tagName.toLowerCase() == 'form' ){
		return node;
	}
	return $PF( node.parentNode );
}


/*FireFox Extend-------------------------------------------------------*/
function __firefox(){
    HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style);
    window.constructor.prototype.__defineGetter__("event", __window_event);
    Event.prototype.__defineGetter__("srcElement", __event_srcElement);
		HTMLElement.prototype.__defineGetter__("innerText", __element_text );
		HTMLElement.prototype.__defineSetter__("innerText", __element_set_text );
}
function __element_set_text( value ){
	this.textContent = value;
}
function __element_text(){
	return (this.textContent);
}

function __element_style(){
    return(this.style);
}

function __window_event(){
    return(__window_event_constructor());
}

function __event_srcElement(){
    return this.target;
}

function __window_event_constructor(){
    if(document.all)
        return(window.event);
    var _caller=__window_event_constructor.caller;
    while(_caller!=null){
        var _argument=_caller.arguments[0];
        if(_argument){
            var _temp=_argument.constructor;
            if(_temp.toString().indexOf("Event")!=-1)
                return(_argument);
        }
        _caller=_caller.caller;
    }
    return(null);
}

if(window.addEventListener){
    __firefox();
}

/*document Extend-------------------------------------------------------*/
document.getElementsByClassName = function( className, parentElement ){
	var children = ( $(parentElement) || document.body ).getElementsByTagName('*');
	var elements = new Array();
	for( var i = 0; i < children.length; i++ ){
		if( children[i].className == className ){
			elements.push( children[i] );
		}
	}
	return elements;
}

/*Element Object-------------------------------------------------------*/
if( !window.Element ){
	Element = {};
}
Element.currentPopup = null;
Element.hide = function( ){
	if( arguments.length == 1 ){
		$( arguments[0] ).style.display = 'none';
	}
	else{
		var nodes = new Array();
		for( var j = 0; j < arguments.length; j++ ){
			nodes.push( $( arguments[j] ) );
		}
		for( var i = 0; i < nodes.length; i++ ){
			nodes[i].style.display = 'none';
		}
	}
}
Element.show = function( ){
	if( arguments.length == 1 ){
		if( $(arguments[0]).tagName.toLowerCase() == 'div' ){
			$( arguments[0] ).style.display = 'block';
		}
		else{
			$( arguments[0] ).style.display = '';
		}
	}
	else{
		var nodes = new Array();
		for( var j = 0; j < arguments.length; j++ ){
			nodes.push( $( arguments[j] ) );
		}
		for( var i = 0; i < nodes.length; i++ ){
			if( nodes[i].tagName.toLowerCase() == 'div' ){
				nodes[i].style.display = 'block';
			}
			else{
				nodes[i].style.display = '';
			}
		}
	}
}
Element.showModal = function( node ){
	if( node != null ){
		node = $( node );
		var bcolor = '#383838';
		if( arguments[1] != null ){
			bcolor = arguments[1];
		}
		var backDiv = document.createElement( 'div' );
		document.body.appendChild( backDiv );
		var w = window.clientWidth() < window.contentWidth() ? window.contentWidth() : window.clientWidth();
		var h = window.clientHeight() < window.contentHeight() ? window.contentHeight() : window.clientHeight();
		backDiv.id = 'popupBackDiv';
		backDiv.style.backgroundColor = bcolor;
		backDiv.style.position = 'absolute';
		backDiv.style.width = w + 'px';
		backDiv.style.height = h + 'px';
		backDiv.style.left = '0px';
		backDiv.style.top = '0px';
		backDiv.style.zIndex = 1;
		if( IsIE ){
			backDiv.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=50,startX=0,startY=0,finishX=100,finishY=100);';
		}
		else{
			backDiv.style.opacity = 0.5;
		}
		Element.show( node );
		node.style.position = 'absolute';
		node.style.left = ( window.clientWidth() - node.offsetWidth ) / 2 + document.documentElement.scrollLeft + 'px';
		node.style.top = ( window.clientHeight() - node.offsetHeight ) / 2 + document.documentElement.scrollTop + 'px';
		node.style.zIndex = 999;
		Element.currentPopup = node;
	}
}
Element.hidePopup = function( ){
	var node = Element.currentPopup;
	if( arguments[0] != null ){
		node = $( arguments[0] );
	}
	if( node != null ){
		node.style.left = -1000 + 'px';
		node.style.top = -1000 + 'px';
		node.style.position = '';
		Element.hide( node );
	}
	if( $( 'popupBackDiv' ) != null ){
		document.body.removeChild( $( 'popupBackDiv' ) );
	}
}
Element.showModeless = function( node ){
	if( node != null ){
		node = $( node );
		var w = window.clientWidth() < window.contentWidth() ? window.contentWidth() : window.clientWidth();
		var h = window.clientHeight() < window.contentHeight() ? window.contentHeight() : window.clientHeight();
		Element.show( node );
		node.style.position = 'absolute';
		node.style.left = ( window.clientWidth() - node.offsetWidth ) / 2 + document.documentElement.scrollLeft + 'px';
		node.style.top = ( window.clientHeight() - node.offsetHeight ) / 2 + document.documentElement.scrollTop + 'px';
		node.style.zIndex = 999;
		Element.currentPopup = node;
	}
}

Element.popup = {}
Element.popup.display = '';
Element.popup.position = '';
Element.popup.left= '';
Element.popup.top = '';

Element.showTip = function( source, node, px, py ){
	node = $( node );
	source = $( source );
	if( node != null && source != null ){
		Element.popup.display = node.style.diaplay;
		Element.popup.position = node.style.position;
		Element.popup.left = node.style.left;
		Element.popup.top = node.style.top;
		node.style.display = 'block';
		node.style.position = 'absolute';
		if( px == null ){
			px = source.offsetWidth - 0.3 * source.offsetWidth;
		}
		if( py == null ){
			py = source.offsetHeight + 3;
		}
		var posX = Element.positionLeft( source );
		var posY = Element.positionTop( source );
		var w = window.clientWidth() < window.contentWidth() ? window.contentWidth() : window.clientWidth();
		var h = window.clientHeight() < window.contentHeight() ? window.contentHeight() : window.clientHeight();
		if( posX + px + node.offsetWidth > w  ){
			node.style.left = posX + 0.7 * source.offsetWidth - node.offsetWidth + 'px';
		}
		else{
			node.style.left =  posX + px + 'px';
		}
		if( posY + py + node.offsetHeight > h  ){
			node.style.top = posY - 3 - node.offsetHeight + 'px';
		}
		else{
			node.style.top = posY + py + 'px';
		}
		node.style.zIndex = 10;
	}
}
var getPositionTop = function( node ){
	if( node == null ){
		return 0;
	}
	return node.offsetTop + getPositionTop( node.offsetParent );
}
var getPositionLeft = function( node ){
	if( node == null ){
		return 0;
	}
	return node.offsetLeft + getPositionLeft( node.offsetParent );
}
Element.positionLeft = function( node ){
	node = $( node );
	return getPositionLeft( node );
}
Element.positionTop = function( node ){
	node = $( node );
	return getPositionTop( node );
}


var Ajax = {
	getTransport: function(){
		return Try.calls(
			function (){ return new ActiveXObject('Msxml2.XMLHTTP') },
			function (){ return new ActiveXObject('Microsoft.XMLHTTP') },
			function (){ return new XMLHttpRequest() }
		);
	},
	activeRequestCount: 0
}
Ajax.Responders = {
	responders: [],
	listener: null,
	bind: function(req){
		this.responders.push( req );
		if( this.listener  == null || this.listener == undefined ){
			this.listener = setTimeout( this.startListening, 10 );
		}
	},
	remove: function(req){
		this.responders.remove(req);
	},
	startListening: function(){
		if( Ajax.activeRequestCount < 1 ){
			clearTimeout( Ajax.Responders.listener );
			Ajax.Responders.listener = null;
			return;
		}
		var responser;
		for( var i = 0; i < Ajax.Responders.responders.length; i++ ){
			responser = Ajax.Responders.responders[i];
			var currentState = responser.xmlHttp.readyState;
			if( responser.readyState < currentState ){
				for( var j = 0; j < currentState - responser.readyState; j++ ){
					responser.onreadystatechange( j + responser.readyState + 1);
					if( j + responser.readyState + 1 == 4 && responser.responseIsSuccess() ){
						responser.oncomplete();
					}
					else if( j + responser.readyState + 1 == 4 && responser.responseIsFailure() ){
						responser.onfailure();
					}
				}
				responser.readyState = currentState;
			}
		}
		Ajax.Responders.listener = setTimeout( Ajax.Responders.startListening, 10 );
	}
}

Ajax.Events = [ 'Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete' ];

Ajax.Requestor = function( url, options ){ this.beginTransport.call(this, url, options ); }

Object.inherit( Ajax.Requestor.prototype, {
		beginTransport: function( url, options ){
			this.url = url;
			this.readyState = -1;
			this.headers = new Hashtable();
			this.options = { method: 'GET', asynchronism: true };
			if( options != undefined && options != null ){
				this.setOptions( options );
			}
			this.xmlHttp = Ajax.getTransport();
		},
		send: function( params ){
			this.xmlHttp.open( this.options.method, this.url, this.options.asynchronism );
			this.oncreate();
			for( var i = 0; i < this.headers.keys().length; i++ ){
				this.xmlHttp.setRequestHeader( this.headers.keys()[i], this.headers.values()[i] );
			}
			this.xmlHttp.send( params );
		},
		onreadystatechange: function( state ){
			if( typeof(this.options['on'+Ajax.Events[state]]) == 'function' ){
				this.options['on'+Ajax.Events[state]]( this.xmlHttp );
			}
		},
		oncreate: function(){ Ajax.activeRequestCount++; Ajax.Responders.bind( this ); },
		oncomplete: function(){ this.xmlHttp = null; Ajax.Responders.remove( this ); Ajax.activeRequestCount--; },
		onfailure: function(){this.oncomplete();if( typeof( this.options.onFailure ) == 'function' )this.options.onFailure(); },
		setOptions: function( options ){
			if( options.method != undefined && options.method.toLowerCase() == 'post' ){
				this.options.method = options.method;
			}
			if( options.asynchronism != undefined && !options.asynchronism ){
				this.options.asynchronism = false;
			}
			for( var i = 0; i < Ajax.Events.length; i++ ){
				var e = Ajax.Events[i];	
				if( options['on'+e] != undefined && options['on'+e] != null   ){
					this.options['on'+e] = options['on'+e];
				}
			}
			if( options.onFailure != undefined ){
				this.options.onFailure = options.onFailure;
			}
		},
		responseIsSuccess: function(){
			return this.xmlHttp.status == undefined || this.xmlHttp.status == 0 || ( this.xmlHttp.status >= 200 && this.xmlHttp.status < 300 );
		},
		responseIsFailure: function(){
			return !this.responseIsSuccess();
		},
		responseText: function(){
			return this.xmlHttp.responseText;
		},
		responseXml: function(){
			return this.xmlHttp.responseXML;
		},
		evalResponseText: function(){
			return eval( this.responseText() );
		},
		setRequestHeader: function( name, value ){
			this.headers.add( name, value );
		}
	} 
)
