/*
 * 
 *  JS API (Javascript Simplificated API) ver. 0.0.1
 * 
 */

/***
 *** JSAPI v 0.1
 ***/

var jsApi = {
	version : '0.1',
	clear : function() {},
	uA : navigator.userAgent.toLowerCase(),
	IE_BROWSER: (/msie/.test(navigator.userAgent.toLowerCase()) && 
				 ! /opera/.test(navigator.userAgent.toLowerCase()) 
				),
	IE6_BROWSER: (	/msie/.test(navigator.userAgent.toLowerCase()) && 
					!(  /opera/.test(navigator.userAgent.toLowerCase()) || 
						/msie 7/.test(navigator.userAgent.toLowerCase())
			 		 )
				 ),
	donothing : function() {}
}

/***
 *** Object Class Extend
 ***/

Object.prototype.inspect = 
	function(obj) {
		try {
			if (obj == undefined) return 'undefined';
			else if (obj == null) return 'null';
			else return obj.toString();
		} catch (err) {
			if (err instanceof RangeError) return ' ';
			throw err;
		}
	}

//
// does not work on IE lte 6
// not tested on IE 7
//
Object.prototype.add_propertries = 
	function(propertry, value) {
		if (propertry.constructor == Array) {
			try {
				for (p in propertry) 
					if (propertry[p]) this[propertry[p]] == value[p];
			} catch (err) {
				throw err;
			}
		} else {
			try {
				this[propertry] = value;
			} catch (err) {
				throw err;
			}
		}
	}

//
// should work on everything
//	
Object.prototype.add_propertries_to = 
	function(propertry, value, toObj) {
		if (Object.inspect(toObj) == 'undefined' || Object.inspect(toObj) == null) return;
		if (propertry.constructor == Array) {
			try {
				for (p in propertry) 
					if (propertry[p]) toObj[propertry[p]] == value[p];
			} catch (err) {
				throw err;
			}
		} else {
			try {
				toObj[propertry] = value;
			} catch (err) {
				throw err;
			}
		}
	}
	
//
// does not work on IE lte 6
// not tested on IE 7
//
Object.prototype.add_event = 
	function(propertry, value) {
		try {
			this[propertry] = value;
		} catch (err) {
			throw err;
		}
	}

//
// should work on everything
//	
Object.prototype.add_event_to = 
	function(propertry, value, toObj) {
		if (Object.inspect(toObj) == 'undefined' || Object.inspect(toObj) == null) return;
		try {
			toObj[propertry] = value;
		} catch (err) {
			throw err;
		}
	}

/***
 *** Array Class Extend
 ***/

Array.prototype = {}

/***
 *** String Class Extend
 ***/

String.prototype.replaceAll = 
	function () {}
	
String.prototype.matchAll = 
	function (pattern) {
		s = pattern.toString();
		if (s.match(/\/ig$/)) a = 0; 
		else if (s.match(/\/i$/)) s += 'g';
		else if (s.match(/\/g$/)) s += 'i';
		else s += 'ig';
		return s;
		//r = new RegExp(s);
		//return r.exec(this.toString());
		//if (pattern.charAt(this.length - 1) == 'g') return this.match(pattern);
		//else return 'a';
	}

/***
 *** Number Class Extend
 ***/

Number.prototype = {}

/***
 *** Date Class Extend
 ***/

Date.prototype = {}

/***
 *** Function Class Extend
 ***/

Function.prototype = {}

/***
 *** RegExp Class Extend
 ***/

RegExp.prototype = {}

/***
 *** Boolean Class Extend
 ***/

Boolean.prototype = {}

/***
 *** Ajax Class
 ***/

var AjaxRequest = {
	_GET : 		'get',
	_POST : 	'post',
	_IMG :		'img',
	_JS :		'js'
}

var AjaxResponse = {
	UNSENT : 	0,	// 0 UNSENT
	OPEN :		1,	// 1 OPEN
	SENT :		2,	// 2 SENT
	LOADING : 	3,	// 3 LOADING 
	DONE :		4	// 4 DONE
}

function Ajax(ObjectName){

	this.hash = {};	
	this.httpRequest = false;
	
	if (window.XMLHttpRequest){ // if !IE
		try {
			this.httpRequest = new XMLHttpRequest();
		} catch (e) {
			alert("Error [non IE / XMLHttpRequest]:\n " + e);
		}
		if (this.httpRequest.overrideMimeType) 
			this.httpRequest.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject){ // if IE
		try {
			this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { 
				alert("Error [IE / Microsoft.XMLHTTP/Msxml2.XMLHTTP]:\n " + e); 
			}
		}
	}
	if (!this.httpRequest) {
		alert('Your browser does not support Ajax. Please select another browser.');
		return false;
	}
	this.ObjectName = ObjectName;
	this.ObjectVertion = "0.4 beta";
	this.handleResponse = null;

}

Ajax.prototype = {
	
	// XMLHttpRequest Functions
	open: function (method, url, async, user, password) {
		this.httpRequest.open(method, url, async, user, password);
	},
	
	abort: function () { 
		this.abort(); 
	},
	
	setRequestHeader: function (header, value_) {
		this.httpRequest.setRequestHeader(header, value_);
	},
	
	send: function (data) {
		this.httpRequest.send(data);
	},
	
	getResponseHeader: function (header) {
		return this.httpRequest.getResponseHeader(header);
	},
	
	getAllResponseHeaders: function () {
		return this.httpRequest.getAllResponseHeaders();
	},
	
	// XMLHttpRequest _get Functions
	readyState: function () {
		return this.httpRequest.readyState;
	},
	
	responseText: function () {
		return this.httpRequest.responseText;
	},
	
	responseXML: function () {
		return this.httpRequest.responseXML;
	},
	
	status: function() {
		return this.status();
	},
	
	statusText: function () {
		return this.httpRequest.statusText;
	},
	
	// Ajax Added Functions	
	sendRequest: function( requestMethod, requestLink, requestParams, handleResponse, async, user, pass) {
		if (!this.httpRequest) {
			alert("Error in "+this.ObjectName+" object: \n No request object created.");
			return;
		}
		if (handleResponse != null) this.handleResponse = handleResponse;
		if ( this.handleResponse == null ) {
			alert("Error in "+this.ObjectName+" object: \n Ajax object has not requestHandle.");
			return;
		}		
		if (requestParams && requestParams.constructor == Array) {
			par = requestParams; 
			requestParams = "";
			for (i = 0; i < par.length; i++) if (par[i]) 
				if (par[i].constructor == Object) requestParams += "&" + par[i].name + "=" + par[i].value;
				else requestParams += "&" + i + "=" + par[i];
		}
		requestParams += "&ajax4time=" + new Date().getTime();
		//alert(requestLink+((requestLink.indexOf("?") < 0)?"?":"")+requestParams);
		//alert(requestMethod);
		//
		// IMG
		//
		if (requestMethod == AjaxRequest._IMG)  {
			img = document.createElement('img');
		    img.onload = this.handleResponse;
		    img.src = requestLink+((requestLink.indexOf("?") < 0)?"?":"")+requestParams;
		} else
		//
		// POST
		//
		if (requestMethod == AjaxRequest._POST) {
			try {
				this.httpRequest.onreadystatechange = this.handleResponse;
				this.httpRequest.open(requestMethod, requestLink, async, user, pass);
				this.httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				this.httpRequest.setRequestHeader("Content-length", requestParams.length);
				this.httpRequest.setRequestHeader("Connection", "close");
				this.httpRequest.send(requestParams);
			} catch (e) {
				alert("Error in "+this.ObjectName+" object: \n" + e);
			}
		// GET or anything else
		} else 
			try {
				this.httpRequest.onreadystatechange = this.handleResponse;
				this.httpRequest.open(requestMethod, requestLink+((requestLink.indexOf("?") < 0)?"?":"")+requestParams, async, user, pass);
				this.httpRequest.send("");
			} catch (e) {
				alert("Error in "+this.ObjectName+" object: \n" + e);
			}
	},
	
	sendRequest_POST: function (requestLink, requestParams, handleResponse) {
		this.sendRequest(AjaxRequest._POST, requestLink, requestParams, handleResponse, true);
	},

	sendRequest_GET: function (requestLink, requestParams, handleResponse) {
		this.sendRequest(AjaxRequest._GET, requestLink, requestParams, handleResponse, true);
	},
	
	sendRequest_IMG: function (requestLink, requestParams, handleResponse) {
		this.sendRequest(AjaxRequest._IMG, requestLink, requestParams, handleResponse, true);
	}
}

/*
Browser				window.innerHeight	document.body.clientHeight	document.documentElement.clientHeight
Opera 9.5+ strict	window				document					window
Opera 9.5+ quirks	window				window						document
Opera 7-9.2			window				window						document
Opera 6				window				window						N/A
Mozilla strict		window				document					window
Mozilla quirks		window				window						document
KHTML				window				document					document
Safari				window				document					document
iCab 3				window				document					document
iCab 2				window				window						N/A
IE 6+ strict		N/A					document					window
IE 5-7 quirks		N/A					window						0
IE 4				N/A					window						N/A
ICEbrowser			window				window						document
Tkhtml Hv3			N/A					window						document
Netscape 4			window				N/A							N/A
*/

/***
 *** window Class Extended
 ***/

window.getSize = 
	function () {
		wWidth  = document.body.clientWidth;
		wHeight = document.body.clientHeight;
		return { Width: wWidth, Height: wHeight }
	}

window.getSizeWithScrolBar = 
	function () {
		wWidthScrollBar  = (jsApi.IE_BROWSER) ? document.documentElement.offsetWidth  : undefined;
		wHeightScrollBar = (jsApi.IE_BROWSER) ? document.documentElement.offsetHeight : undefined
		return { Width: wWidthScrollBar, Height: wHeightScrollBar }
	}

/***
 *** document Class Extended
 ***/

//
// Mouse Extension
//

MouseButtons = {
	BUTTON_LEFT: 1,
	BUTTON_RIGHT: 2,
	BUTTON_CENTER: 3
}

document.getMouseButton = 
	function(e) {
		if (jsApi.IE_BROWSER) {
			//return event.button;
			switch (event.button) {
				case 1: return 1; break
				case 2: return 3; break
				case 4: return 2; break
			}
		}
		else if (!jsApi.IE_BROWSER && e) return e.which;
		return -1;
	}

document.getMousePos = 
	function(e) {
		if (jsApi.IE_BROWSER) {
			mX = event.clientX + document.body.scrollLeft
			mY = event.clientY + document.body.scrollTop
		} else {
			mX = e.pageX;
			mY = e.pageY;
		}
		return { X: mX, Y: mY }
	}

//
// KeyPad Extension
//
	
KeyValues = {
	KEY_BACKSPACE:	8,
	KEY_ENTER:		13,
	KEY_ALT:		18,
	KEY_CTRL:		17,
	KEY_SHIFT:		16,
	KEY_UP:			38,
	KEY_DOWN:		40,
	KEY_LEFT:		37,
	KEY_RIGHT:		39,
	KEY_END:		35,
	KEY_HOME:		36,
	KEY_PGUP:		33,
	KEY_PGDOWN:		34,
	KEY_DEL:		46,
	KEY_INS:		45
}

document.getKeyPressed = 
	function (e) {
		if (!e) e = event;
		if (e.charCode) return e.charCode;
		else return e.keyCode;
	}

document.getSize = 
	function () {
		if (document.body && document.body.scrollHeight) {
			this.Height = document.body.scrollHeight;
			this.Width = document.body.scrollWidth;
		} else {
			this.Height = document.documentElement.scrollHeight;
			this.Width = document.documentElement.scrollWidth;
		}
		return { Width: this.Width, Height: this.Height }
	}
	
document.getScroll = 
	function() {
		if( !jsApi.IE_BROWSER ) {
			// !IE
			this.scrollY = window.pageYOffset;
			this.scrollX = window.pageXOffset;
		} else {
			// IE
			this.scrollY = document.body.scrollTop || document.documentElement.scrollTop;
			this.scrollX = document.body.scrollLeft || document.documentElement.scrollLeft;
		}
		return { X: this.scrollX, Y: this.scrollY };
	}

//
// Environmental Extension
//

document.envGET = 
	function (id) {
		url = (location)?location.href:((document.location)?document.location:window.location);
		url = String(url);
		url = url.substr(url.indexOf("?")+1);
		url = url.split("&");
		for (i = 0; i < url.length; i++) {
			url[i] = url[i].split("=");
			if (id && url[i][0] == id) return url[i][1];
		}
		if (id) return undefined;
		else return url;
}
