Function.prototype.defaults = function()
{
	var _f = this;
	var _a = Array(_f.length-arguments.length).concat(Array.prototype.slice.apply(arguments));

	return function() {
		return _f.apply(_f, Array.prototype.slice.apply(arguments).concat(_a.slice(arguments.length, _a.length)));
	}
}

String.prototype.intVal = function() {
	var n = parseInt( this );
	return n ? n : 0;
}

Math.bound = function( v, a, b ) {
	return Math.max( Math.min( v, b ), a );
}

var Events = Array();

function LaunchEvents() {
	for( var i = 0; i < Events.length; i ++ )
		Events[i]();
}

function AddEvent( Event ) {
	Events.push( Event );
}

window.onload = LaunchEvents;

var GetXmlHttpObject = function( dep )
{
	var xmlHttp = null;
	var activeXObjects = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];
	for( var i = 0; i < activeXObjects.length; i++ )
		try { xmlHttp = new ActiveXObject( activeXObjects[i] ); break; } catch(e) {}
	if( xmlHttp == null )
		try{ var xmlHttp = new XMLHttpRequest(); } catch(e) { alert( "This browser does not support XMLHttpRequest." ); return; }

	if( dep ) {
		if( this._xmlHttp && this._xmlHttp.readyState != 0 )
			this._xmlHttp.abort(); 
		this._xmlHttp = xmlHttp;
	}

	return xmlHttp;
}.defaults( true );

var ajaxSubmit = function( url, data, func, dep ) {
	xmlHttp = GetXmlHttpObject( dep );
	if( xmlHttp == null ) {
		alert("Your browser does not support AJAX!"); return; }

	var p = "";
	for( key in data ) {
		if( p != "" ) p += "&";
		p += encodeURIComponent( key ) + "=" + encodeURIComponent( data[key] ); }

	xmlHttp.open( "POST", url, true );
	xmlHttp.setRequestHeader( "content-type", "application/x-www-form-urlencoded" );
	xmlHttp.setRequestHeader( "content-length", p.length );
	xmlHttp.setRequestHeader( "connection", "close" );

	if( func )
		xmlHttp.onreadystatechange = function() { return function () { if( xmlHttp.readyState == 4 && xmlHttp.status == 200 ) func( xmlHttp.responseText ); }; }(func);

	xmlHttp.send( p );
}.defaults( true );

