﻿/*
if(!ZACKSZ){ ZACKSZ = {}; }
if(!ZACKSZ._a){ ZACKSZ._a = []; }
ZACKSZ._a.push({    
    Date:'2009-07-13',
    A:'z_Base.js',
    A:'zack@zacksz.com',
    R:'0'
});
*/



/*START*/

Object.extend = function(dest, source, replace) {
	for(var prop in source) {
		if(replace == false && dest[prop] != null) { continue; }
		dest[prop] = source[prop];
	}
	return dest;
};

Object.extend(Function.prototype, {
	apply: function(o, a) {
		var r, x = "__fapply";
		if(typeof o != "object") { o = {}; }
		o[x] = this;
		var s = "r = o." + x + "(";
		for(var i=0; i<a.length; i++) {
			if(i>0) { s += ","; }
			s += "a[" + i + "]";
		}
		s += ");";
		eval(s);
		delete o[x];
		return r;
	},
	bind: function(o) {
		if(!Function.__objs) {
			Function.__objs = [];
			Function.__funcs = [];
		}
		var objId = o.__oid;
		if(!objId) {
			Function.__objs[objId = o.__oid = Function.__objs.length] = o;
		}

		var me = this;
		var funcId = me.__fid;
		if(!funcId) {
			Function.__funcs[funcId = me.__fid = Function.__funcs.length] = me;
		}

		if(!o.__closures) {
			o.__closures = [];
		}

		var closure = o.__closures[funcId];
		if(closure) {
			return closure;
		}

		o = null;
		me = null;

		return Function.__objs[objId].__closures[funcId] = function() {
			return Function.__funcs[funcId].apply(Function.__objs[objId], arguments);
		};
	}
}, false);

Object.extend(Array.prototype, {
	push: function(o) {
		this[this.length] = o;
	},
	addRange: function(items) {
		if(items.length > 0) {
			for(var i=0; i<items.length; i++) {
				this.push(items[i]);
			}
		}
	},
	clear: function() {
		this.length = 0;
		return this;
	},
	shift: function() {
		if(this.length == 0) { return null; }
		var o = this[0];
		for(var i=0; i<this.length-1; i++) {
			this[i] = this[i + 1];
		}
		this.length--;
		return o;
	}
}, false);

Object.extend(String.prototype, {
	trimLeft: function() {
		return this.replace(/^\s*/,"");
	},
	trimRight: function() {
		return this.replace(/\s*$/,"");
	},
	trim: function() {
		return this.trimRight().trimLeft();
	},
	endsWith: function(s) {
		if(this.length == 0 || this.length < s.length) { return false; }
		return (this.substr(this.length - s.length) == s);
	},
	startsWith: function(s) {
		if(this.length == 0 || this.length < s.length) { return false; }
		return (this.substr(0, s.length) == s);
	},
	split: function(c) {
		var a = [];
		if(this.length == 0) return a;
		var p = 0;
		for(var i=0; i<this.length; i++) {
			if(this.charAt(i) == c) {
				a.push(this.substring(p, i));
				p = ++i;
			}
		}
		a.push(s.substr(p));
		return a;
	}
}, false);

Object.extend(String, {
	format: function(s) {
		for(var i=1; i<arguments.length; i++) {
			s = s.replace("{" + (i -1) + "}", arguments[i]);
		}
		return s;
	},
	isNullOrEmpty: function(s) {
		if(s == null || s.length == 0) {
			return true;
		}
		return false;
	}
}, false);

if(typeof addEvent == "undefined")
	addEvent = function(o, evType, f, capture) {
		if(o == null) { return false; }
		if(o.addEventListener) {
			o.addEventListener(evType, f, capture);
			return true;
		} else if (o.attachEvent) {
			var r = o.attachEvent("on" + evType, f);
			return r;
		} else {
			try{ o["on" + evType] = f; }catch(e){}
		}
	};
	
if(typeof removeEvent == "undefined")
	removeEvent = function(o, evType, f, capture) {
		if(o == null) { return false; }
		if(o.removeEventListener) {
			o.removeEventListener(evType, f, capture);
			return true;
		} else if (o.detachEvent) {
			o.detachEvent("on" + evType, f);
		} else {
			try{ o["on" + evType] = function(){}; }catch(e){}
		}
	};


 Object.extend(window, {
	$: function() {
		var elements = [];
		for(var i=0; i<arguments.length; i++) {
			var e = arguments[i];
			if(typeof e == 'string') {
				e = document.getElementById(e);
			}
			if(arguments.length == 1) {
				return e;
			}
			elements.push(e);
		}
		return elements;
	},
	Class: {
		create: function() {
			return function() {
				if(typeof this.initialize == "function") {
					this.initialize.apply(this, arguments);
				}
			};
		}
	}
    }, false);

var zAJAX = function() {
    /* Private Vars */
	var _iRan = 0;
	var _bInit = false;
	var oRequest = null;
	var onSuccess = null;
	var onFailure = null;
	
	var bOpenRequest = false;
	function init()
	{
		/*-- Intilizes Class-- */
		_bInit = true;
	}
	init.mGetCnt = function(){ return _iRan; } 
	
    function mGetTransport()
    {
        xmlhttp=null;
        if (window.XMLHttpRequest)
        {// code for all new browsers
          xmlhttp=new XMLHttpRequest();
        } else if (window.ActiveXObject)
        {// code for IE5 and IE6
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlhttp;
    }          	
	init.Request = function(sPath, oArgs)
	{
	    bOpenRequest = true;
	    var parameters = oArgs.parameters;
	    onSuccess = null;
	    onFailure = null;
	    onSuccess = oArgs.onSuccess;
	    onFailure = oArgs.onFailure;
        oRequest = mGetTransport();
        try{
            switch (parameters.request) {
                case 'login':{
                    var sUser = Url.encode(parameters.uid);
                    var sPswd = Url.encode(parameters.pwd);
                    oRequest.open(oArgs.method.toUpperCase(), sPath + '?request=login&uid=' + sUser + '&pwd=' + sPswd, true);
                }break;
                case 'prefs':{
                    var name = Url.encode(parameters.name);
                    var value = Url.encode(parameters.value);
                    var st = Url.encode(parameters.st);
                    oRequest.open(oArgs.method.toUpperCase(), sPath + '?name=' + name + '&value=' + value + '&st=' + st, true);
                }break;
                default:{
                    oRequest.open(oArgs.method.toUpperCase(), sPath, true);
                }break;
            }

        } catch (e) {
            // Catch invalid URLs
            oRequest = null;
            if(onFailure){ onFailure(); }
            return;
        }
        // Set the callback for when the downloading is completed (or failed)
        oRequest.onreadystatechange = oCallBack;

        // Start the download
        try {
            oRequest.send(null);
        } catch (e) {
            // Catch errors sending the request
            oRequest = null;
            if(onFailure){ onFailure(); }
            return;
        }	    
	    
	}
	function oCallBack() {
      // Verify that the download completed
      if (oRequest.readyState != 4){ return; } 

      // Verify that the download was successful
      if (oRequest.status != 200) {
        oRequest = null;
        if(onFailure){ onFailure(); }
      }else{
        var sRet = oRequest.responseText.trim()
        oRequest = null;
        if(onSuccess){ onSuccess(sRet); }
      }
    }
	return init;
}();


var oRequest_ = null;
var parseUrl = function(url) {
		// parse the iframe url to extract the key = value pairs from it
		var ret = new Object();
		var hashParams = url.replace(/#.*$/, '').split('&');
		var param = key = val = '';
		for (i = 0 ; i < hashParams.length; i++) {
			param = hashParams[i];
			key = param.substr(0, param.indexOf('='));
			val = param.substr(param.indexOf('=') + 1);
			ret[key] = val;
		}
		return ret;
	}
function toJson(obj) {
 switch (typeof obj) {
  case 'object':
   if (obj) {
    var list = [];
    if (obj instanceof Array) {
     for (var i=0;i < obj.length;i++) {
      list.push(toJson(obj[i]));
     }
     return '[' + list.join(',') + ']';
    } else {
     for (var prop in obj) {
      list.push('"' + prop + '":' + toJson(obj[prop]));
     }
     return '{' + list.join(',') + '}';
    }
   } else {
    return 'null';
   }
  case 'string':
   return '"' + obj.replace(/(["'])/g, '\\$1') + '"';
  case 'number':
  case 'boolean':
   return new String(obj);
 }
}


var Url = {
        // public method for url encoding
    encode : function (string) {
		var s = String(escape(this._utf8_encode(string)));
		s = s.replace('@', '%40');
		return s;
    },
        // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },
        // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) { utftext += String.fromCharCode(c); }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },
        // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }
        return string;
    }
}
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); }

function StringBuilder(sJoin) {this.v = []; this.sJoin = sJoin;}
StringBuilder.prototype.append = function(s) {return (s) ? this.v[this.v.push(s)-1] : false;}
StringBuilder.prototype.appendLine = function(s) {return (s) ? this.v[this.v.push(s + "\r\n")-1] : false;}
StringBuilder.prototype.clear = function() {this.v.clear();}
StringBuilder.prototype.reverse = function() {this.v.reverse();}
StringBuilder.prototype.toString = function() {return this.v.join(this.sJoin || '');}		    

var DOM = function() {
    /* Private Vars */
	var _iRan = 0;
	var _bInit = false;
	var oDiv = document.createElement('div');
    var oSpan = document.createElement('span');
	var oBr = document.createElement('br');
	var oA = document.createElement('a');
	var oTxt = document.createElement('textarea');
	var oStrong = document.createElement('strong');
	oStrong.style.fontFamily = "Arial, Times New Roman";
	oStrong.style.fontSize = "12";
	var oImg = document.createElement('img');
	var oIn = document.createElement('input');
	function ezId(obj, sId) 
	{
		if (sId != '') {obj.id = sId;}
		if (sId != '') {obj.setAttribute('id', sId);}
	}
    function ezFloat(obj, sFloat) {
        var sTest = document.body.style.styleFloat;
        if (sTest == '') {
            obj.style.styleFloat = sFloat;
        } else {
            obj.style.cssFloat = sFloat;
        }
    }	
	function init()
	{
		/*-- Intilizes Class-- */
		_bInit = true;
	}
	init.mGetCnt = function(){ return _iRan; } 
	init.mFloat = function(obj, sFloat){ ezFloat(obj, sFloat); }
	init.mIn = function(o)
	{
	    if(!o){ o = {}; }
		var oCreated = oIn.cloneNode(true);
		_iRan++;
		if(o.ID){ ezId(oCreated, o.ID); }
		return oCreated;        
	}
	init.mBr = function(obj)
	{
		var o = obj || {};
		var oCreated = oBr.cloneNode(true);
		_iRan++;
		if(o.oPar){ o.oPar.appendChild(oCreated); }
		return oCreated;        
	}
	init.mA = function(obj)
	{
		var o = obj || {};
		var oCreated = oA.cloneNode(true);
		_iRan++;
		if(o.oPar){ o.oPar.appendChild(oCreated); }
		return oCreated;        
	}	
	init.mTxt = function(obj)
	{
		var o = obj || {};
		var oCreated = oTxt.cloneNode(true);
		_iRan++;
		if(o.oPar){ o.oPar.appendChild(oCreated); }
		return oCreated;        
	}                
	init.mStrong = function(obj)
	{
		var o = obj || {};
		var oCreated = oStrong.cloneNode(true);
		_iRan++;
		if(o.oPar){ o.oPar.appendChild(oCreated); }
		return oCreated;        
	}                                            
	init.mImg = function(obj)
	{
		var o = obj || {};
		var oCreated = oImg.cloneNode(true);
		_iRan++;
		if(o.oPar){ o.oPar.appendChild(oCreated); }
		if(o.ID){ ezId(oCreated, o.ID); }
		return oCreated;        
	}
	init.mDiv = function(obj)
	{
		var o = obj || {};
		var oCreated = oDiv.cloneNode(true);
		_iRan++;
		if(o.oPar){ o.oPar.appendChild(oCreated); }
		if(o.ID){ ezId(oCreated, o.ID); }
		if(o.sCl){ oCreated.className = o.sCl; }
		return oCreated;        
	}
    init.mSpan = function(obj)
    {
        var o = obj || {};
        var oCreated = oSpan.cloneNode(true);
        _iRan++;
        if(o.oPar){ o.oPar.appendChild(oCreated); }
        if(o.ID){ ezId(oCreated, o.ID); }
        if(o.sCl){ oCreated.className = o.sCl; }
        return oCreated;        
    }
	return init;
}();


function mAddEvent(obj, evType, fn, useCapture){
/* 26 May 2007: Rev 00 */
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, useCapture);
        return true;
    } else if (obj.attachEvent){
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } 
}

function mRemoveEvent(obj, evType, fn, useCapture){
/* 26 May 2007: Rev 00 */
    if (obj.removeEventListener){
        obj.removeEventListener(evType, fn, useCapture);
        return true;
    } else if (obj.detachEvent){
        var r = obj.detachEvent("on"+evType, fn);
        return r;
    } 
} 


/*
    Using Jasvscript to read query string, 
    We should really enable the PHP and do this server side so you can use the PHP scripting engine to do checks and clean up stuff.
    --START
*/

  var _sRootPath = '';
  var _sXMLRootPath = '';
  var _sXMLPath = '';
  var _sBasePath = this.document.location.protocol + '//' + this.document.location.host;
  function GetRoot() {
    var arPaths = this.document.location.href.split(/\?[A-Za-z_]+=/);
    if(arPaths.length > 1)
    {
      _sRootPath = mGetPath(arPaths[0]);  
      _sXMLRootPath = mGetPath(arPaths[1]); 
      _sXMLPath = arPaths[1];
    }
  }
  
  function mGetPath(sURL)
  {
    var arSplit = sURL.split("/");
    var sRet = new StringBuilder("/");
    var iCnt = arSplit.length;
    sRet.append('http:/');
    for(var i = 1; i < (iCnt-1); i++) { sRet.append(arSplit[i]); }
    return sRet.toString();
  
  }
  GetRoot();

/* -- END --*/
