﻿/*
 * A minimum instance of Javascript Framework version 1.2
 * Filename JSMX.js
 * Copyright 2007 HoangKC - http://sulivandinh.googlepages.com
 * Email sulivandinh@gmail.com
 * JSMX is freely distributable under the terms of an MIT-style license.
 */
MX={};MX.Version="1.2";MX.Class=function(properties){var mxclass=function(){if(arguments[0]!="no-init"&&this._Initialize&&typeof(this._Initialize)=="function")this._Initialize.apply(this,arguments);};if(properties&&typeof properties!="string")mxclass.prototype=properties;mxclass.constructor=MX.Class;mxclass.prototype.InstanceOf=function(fnClass,recursive){var isInstance=false;recursive=recursive||false;fnClass=fnClass.replace(/\./g,"$");if(!recursive)return fnClass==this.type;var b=this.baseType.split(",");for(var i=0,n=b.length;i<n;++i){if(fnClass==b[i]){isInstance=true;break;}}return isInstance;};return mxclass;};MX.Class.Inheritance=function(){return MX.Class(MX.Class.Inherited(arguments[0],arguments[1]));};MX.Class.Empty=function(){};MX.Class.Inherited=function(){var _arguments=arguments;if(!_arguments[1])_arguments[1]=this;var _prototype=new _arguments[0]("no-init");if(_prototype.baseType)_prototype.baseType+=","+_prototype["type"];else _prototype.baseType=_prototype["type"];for(var property in _arguments[1]){if(property=="baseType")continue;var base=_prototype[property];_prototype[property]=MX.Class.Merge(base,_arguments[1][property]);}return _prototype;};MX.Class.Merge=function(base,sub){if(base&&base!=sub){var type=typeof sub;if(type!=typeof base)return sub;switch(type){case "function":var merged=function(){this.base=arguments.callee.base;return sub.apply(this,arguments);};merged.base=base;return merged;case "object":return MX.Class.Mix(base,sub);}}return sub;};MX.Class.Mix=function(){var mix={};for(var i=0;i<arguments.length;i++){for(var property in arguments[i]){var sub=arguments[i][property];var base=mix[property];if(base&&typeof sub=="object"&&typeof base=="object")mix[property]=MX.Class.Mix(base,sub);else mix[property]=sub;}}return mix;};MX.Class.Extend=function(){var _arguments=arguments;if(!_arguments[1])_arguments=[this,_arguments[0]];for(var property in _arguments[1]){if(_arguments[2]&&_arguments[2].indexOf(property)!= -1)continue;_arguments[0][property]=_arguments[1][property];}return _arguments[0];};MX.Class.Clone=function(object){return MX.Class.Extend({},object);};$Id=function(id){return document.getElementById(id);};MX.ToArray=function(iterator,start,end){var arr=[];start=start||0;end=end||iterator.length;if(start<=end)for(var i=start;i<end;++i)arr.push(iterator[i]);return arr;};MX.Delegate=function(){var Q=MX.ToArray(arguments,2);var I=arguments[0],bo=arguments[1];return function(){var aC=MX.ToArray(arguments);return bo.apply(I,Q.concat(aC))}};MX.XHRBase={getXHR:function(){return(window.XMLHttpRequest)?new XMLHttpRequest():this.activeX();},activeX:function(){var ax=false;if(window.ActiveXObject)try{ax=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{xhr=new ActiveXObject('Microsoft.XMLHTTP')}catch(ex){ax=false}};return ax;},Events:["Uninitialized","Loading","Loaded","Interactive","Complete"],Handlers:["onCreate","onSuccess","onFailure","onCancel","onException"],options:{method:"post",async:true,onSuccess:MX.Class.Empty,urlEncoded:true,encoding:"utf-8",contentType:"application/x-www-form-urlencoded",parameters:{},headers:{}},toQueryString:function(source){if(typeof source=="string")return source;var queryString=[];for(var property in source)queryString.push(encodeURIComponent(property)+"="+encodeURIComponent(source[property]));return queryString.join("&");}};MX.XHR=new MX.Class({type:"MX$XHR",_Initialize:function(options){this.xhr=MX.XHRBase.getXHR();this.options=MX.Class.Extend(MX.Class.Clone(MX.XHRBase.options),options);this.options.isSuccess=this.options.isSuccess||this._isSuccess;this.headers={};this.running=false;},send:function(url,params){if(this.running)return this;this.running=true;MX.Class.Extend(this.headers,this.options.headers);MX.Class.Extend(this.headers,{"X-Requested-With":"MX.XHR","X-MX-Framework":"Version "+MX.Version,"Accept":"text/javascript, text/html, application/xml, text/xml, */*"});var data=null;this.options.parameters=params||this.options.parameters;params=MX.XHRBase.toQueryString(this.options.parameters);if(this.options.method.toLowerCase()=="get")url+=((url.indexOf("?")!= -1)?"&":"?")+params;else{if(this.xhr.overrideMimeType)this.setHeader("Connection","close");data=params;if(this.options.urlEncoded){var encoding=(this.options.encoding)?'; charset='+this.options.encoding:'';this.setHeader('Content-type','application/x-www-form-urlencoded'+encoding);}}try{this.xhr.open(this.options.method.toUpperCase(),url,this.options.async);if(this.options.async)setTimeout(MX.Delegate(this,function(){this._respondState(1)}),10);this.xhr.onreadystatechange=MX.Delegate(this,this._onStateChange);for(var name in this.headers)try{this.xhr.setRequestHeader(name,this.headers[name]);}catch(e){};this._onHandling(0,null);this.xhr.send(data);if(!this.options.async&&this.xhr.overrideMimeType)this._onStateChange();}catch(e){this._onHandling(4,e);}},setHeader:function(name,value){this.headers[name]=value;},Abort:function(){if(!this.running)return this;this.running=false;this.xhr.abort();this.xhr.onreadystatechange=MX.Class.Empty;this.xhr=MX.XHRBase.getXHR();this._onHandling(3,null);return this;},_isSuccess:function(status){return(!status||((status>=200)&&(status<300)));},_respondState:function(readyState){},_onStateChange:function(){var readyState=this.xhr.readyState;if(readyState!=4|| !this.running)return;this.running=false;var status=0;try{status=this.xhr.status;}catch(e){};var response={"text":this.xhr.responseText,"xml":this.xhr.responseXML,"parameters":this.options.parameters};if(this.options.isSuccess(status))this._onHandling(1,response);else this._onHandling(2,response);this.xhr.onreadystatechange=MX.Class.Empty;},_onHandling:function(type){var name=MX.XHRBase.Handlers[type];if(this.options[name])this.options[name].call(null,arguments[1]);}});