Function.prototype.mybicBind = function(object) {  
var __method = this;
return function() {
return __method.apply(object, arguments);
}
};
function XMLHTTP(server_url, readyStateFunction)
{
this.server_url = server_url;	 
this.async = true;		
this.debug=0;
this.throttle=1;				
this.method = "POST";
this.format = "JSON";				
this.headers = new Array();			
this.abort_timeout = -1;	
this.failed_threshold = 3;		
this.ignoreCall = 0;
this.stopRequest = 0;				
this.jsErrorHandler = '';       
this.notAuthorizedHandler = '';    
this.net_down_func = this.down;		
this.version = '1.0.1';
this.req = null;	
this.debugID = 0;
this.errors = new Array();
this.queue = new Array();	
this.queue_in_process = 0;
this.currentCallIgnore=0;
this.readySateTimer = '';
this.callBack = '';	
this.IEObjCache = 0;
this.failed_requests=0	
this.readyStateFunction = (readyStateFunction) ? readyStateFunction : this.responseHandler;
this._msgQueueInfo = new Array();	
this.readyStateTimer   = '';   
this.poolTimerInterval = 50;    
}
XMLHTTP.prototype = {
getXMLHTTP:function() {
if (window.XMLHttpRequest) { this.req = new XMLHttpRequest(); }
else if (this.IEObjCache != 0) { alert(this.IEObjCache);try {this.req = new ActiveXObject(this.IEObjCache);}catch(e){} }
else if (window.ActiveXObject){
var progIDs = [ 'MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.3.0','Microsoft.XMLHTTP'];
for (var i = 0; i < progIDs.length; i++) {
try {
this.req = new ActiveXObject(progIDs[i]);
this.IEObjCache = progIDs[i];
break;
}
catch (ex) {}
}
} else {
if(this.debug == 1) { this.showDebug("<BR>FATAL ERROR: Could not create XMLHTTPRequest Object!<BR>");	}
}
return this.req;
},
call:function(queryVars, userCallback, queue_request) {
if(this.failed_requests >= this.failed_threshold) {
this.net_down_func('disable');
return false;
} else {
var currentVars;
var callback;
this.fullUrl = '';
if(this.throttle == 1 && queue_request != 'queue' || this.stopRequest == 1) {		// throttling keeps your requests in sync, so things aren't out of order
this.add2Queue(queryVars, userCallback);	
}
if(this.queue_in_process == 0) {
if(!this.getXMLHTTP()) {
return false;
}
if(this.throttle == 1) {
this.queue_in_process = 1;
var currentCall = this.queue.shift();	// get the current call to make
currentVars = currentCall.queryVars;
callback = currentCall.userCallback;
this.format = currentCall.format;
this.method = currentCall.method;
this.abort_timeout = currentCall.abortTimeout;
this.currentCallIgnore = currentCall.ignoreCall;
this.async = currentCall.async;
} else {
currentVars = queryVars;
callback = userCallback;
var ignoreCall=0;
}
this.callBack = callback;
if(this.format != 'JSON') {
currentVars = currentVars+'&json=false';
}
this.full_url = (this.method == "POST") ? this.server_url : this.server_url + '?'+ currentVars;
if(this.debug == 1 && this.currentCallIgnore != 1) {
try {
var matches = currentVars.match(/action=(\w+)&?/);
this.showDebug('new', 'MYBIC - CALLING: '+matches[1]);
this.showDebug("Server Page: "+this.server_url+"<BR>HTTP Method: "+this.method+"<BR>Encoding Format: "+this.format+"<BR>Query String: "+currentVars+"<BR>");
}catch(e){}
}
this.req.open(this.method, this.full_url, this.async);
if(this.headers){
for(var i in this.headers) {
if(i != '' && (this.headers[i] instanceof String)) {
try {
this.req.setRequestHeader( i, this.headers[i]);
if(this.debug == 1) { this.showDebug('Setting Custom Header: '+this.headers[i]+'<br>');}
} catch(e) {}
}
}
}
if(this.abort_timeout != -1) {
this.end_timer = setInterval(this.endCall.mybicBind(this), this.abort_timeout);
}
if(this.method == 'POST') {
this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
this.request = currentVars;
this.req.send(currentVars);
} else {
this.req.send(null);
}
if (this.readyStateFunction) {
this.readyStateTimer = window.setInterval(
this.readyPoolFunc.mybicBind(this),
this.poolTimerInterval
);
}
}
}
},
readyPoolFunc:function() {
if(this.req && this.req.readyState == 4) {
window.clearInterval(this.readyStateTimer);
this.readyStateTimer = null;
this.readyStateFunction();
}
},
responseHandler:function() {
if(this.req) {
try {
if (this.req.readyState == 4) {
if (this.req.status && this.req.status == 200) {
if(this.req.responseText.indexOf('ajax_msg_failed') != -1) {
this.showDebug("Fatal Error: mybic_server sent back ajax_msg_failed! - MSG: "+this.req.responseText+"<br/>");
if(this.req.responseText.indexOf('notauth') != -1) { 
if(this.abort_timeout != -1) { clearInterval(this.end_timer); }
if(this.notAuthorizedHandler == '') {
this.callBack('notauthorized'); 
} else {
try {
this.notAuthorizedHandler();
}catch(e) {
}
}
} else {
this.callBack(false); 
}
} else {
if(this.abort_timeout != -1) {
clearInterval(this.end_timer);
}
if(this.throttle == 1) {
var req = this._msgQueueInfo.shift();
var format = req.format;
} else {
var format = this.format;	
}
if(format == "JSON") {
try {
var myObject = JSON.parse(this.req.responseText);
if(document.getElementById(this.callBack)) {
document.getElementById(this.callBack).innerHTML = myObject;							
} else {
this.callBack(myObject);
}
} catch(e) {
if(this.jsErrorHandler == '') {
alert('An error occurred in your response function, NOT mybic related. Error Name: ' + e.name + '  Message:' + e.message);
} else {
try { this.jsErrorHandler(e); } catch(err) { alert('Error: your errorhandling function has an error - name: '+err.name + ' message: '+err.message)}
}
}
} else if(this.format == "XML") {
this.callBack(this.req.responseXML);	
} else {
try {
if(document.getElementById(this.callBack)) {
document.getElementById(this.callBack).innerHTML = this.req.responseText;
} else {
this.callBack(this.req.responseText);
}
} catch(e) {
if(this.jsErrorHandler == '') {
alert('An error occurred in your response function, NOT mybic related. Error Name: ' + e.name + '  Message:' + e.message);
} else {
try { this.jsErrorHandler(e); } catch(err) { alert('Error: your errorhandling function has an error - name: '+err.name + ' message: '+err.message)}
}
}
}
}
this.failed_requests = 0;
} else {
try{
this.showDebug("Fatal Error: MSG: "+this.req.responseText+" StatusText: "+this.req.statusText+"<br/>");
} catch(e){}
this.endCall();	
}
try {
if(this.debug == 1 && this.currentCallIgnore != 1) {
var str = this.req.responseText.replace(/(\<)/gi, '&lt;');
var str = str.replace(/(\>)/gi, '&gt;');
this.showDebug("HTTP Server Response:<br/> "+str+"<br>");
}
} catch(e) { }
this.restoreDefaults();
this.queue_in_process = 0;
this.req = null;
if(this.queue.length > 0) {
this.call('','','queue');
}
}
} catch(e) { 
}
}
},
loadScript:function(url, remove) {
try{
var scripts = document.getElementsByTagName('script');
s_len = scripts.length;
for(var i=0;i<s_len;i++){
var reg = new RegExp(url+"$");
if (reg.test(scripts[i].src)) {
var p2 = scripts[i];
p2.parentNode.removeChild(p2);
break;
}	
}
if(remove != 'remove') {
newScript = document.createElement("script");
newScript.setAttribute("type", "text/javascript");
newScript.setAttribute("src", url);
document.getElementsByTagName('head')[0].appendChild(newScript);
}
} catch(e) {
this.showDebug("MyBIC - loadScript failed URL: "+url+" ErrName: "+e.name+" Msg: "+e.message);	
}
},
add2Queue:function(queryVars, userCallback) {
var addAjax = new Array();
addAjax['queryVars'] = queryVars;
addAjax['userCallback'] = userCallback;
addAjax['ignoreCall'] = this.ignoreCall;
addAjax['abortTimeout'] = this.abort_timeout;
addAjax['format'] = this.format;
addAjax['method'] = this.method;
addAjax['async'] = this.async;
var opts = new Object();
opts.format = this.format;
this._msgQueueInfo.push(opts);
this.ignoreCall=0;
this.queue.push(addAjax);
},
restoreDefaults:function()
{
this.method = "POST";
this.format = "JSON";	
this.callback = "";
this.abort_timeout = 5000;
this.failed_threshold = 3;	
this.async = true;
},
getForm:function(formid) {
var formobj = document.getElementById(formid);
var fields = new Array();
var form_len = formobj.elements.length;
for (var x = 0; x < form_len; x++) {
switch(formobj.elements[x].type) {
case 'select-one':
fields.push(encodeURIComponent(formobj.elements[x].name)+'='+encodeURIComponent(formobj.elements[x].options[formobj.elements[x].selectedIndex].value));
break;
case 'select-multiple':
var obj = formobj.elements[x];
for(var y=0; y < formobj.elements[x].options.length; y++) {
if(formobj.elements[x].options[y].selected) {
if(formobj.elements[x].options[y].value == ''){
fields.push(encodeURIComponent(formobj.elements[x].name)+'='+encodeURIComponent(formobj.elements[x].options[y].text));
} else {
fields.push(encodeURIComponent(formobj.elements[x].name)+'='+encodeURIComponent(formobj.elements[x].options[y].value));
}
}
}
break;
case 'radio':
if(formobj.elements[x].checked) {
fields.push(encodeURIComponent(formobj.elements[x].name)+'='+encodeURIComponent(formobj.elements[x].value));
}
break;
case 'checkbox':
if(formobj.elements[x].checked) {
fields.push(encodeURIComponent(formobj.elements[x].name)+'='+encodeURIComponent(formobj.elements[x].value));
}
break;
default:
fields.push(encodeURIComponent(formobj.elements[x].name)+'='+encodeURIComponent(formobj.elements[x].value));
break;
}
}
var new_qstring = '&' + fields.join('&');
return new_qstring;
},
endCall:function() {
try{
this.net_down_func();
this.req.abort();
this.req = null;
clearInterval(this.end_timer);
clearInterval(this.readyStateTimer);
this.failed_requests++;
this.queue_in_process = 0;
if(this.queue.length > 0) {
this.call('','','queue');
}
if(this.debug == 1) { this.showDebug("Request Failed - Network Down! Current Failed Attempts: "+this.failed_requests+"<br>");}
} catch(e) {
clearInterval(this.end_timer);
this.net_down_func('disable');
}
this.req = null;
},
down:function(status) {
var notif_div = '<div id="mybic_notification" style="text-align:center;padding:20px;position:absolute;top:100px;left:100px;width:300px;border:thin solid black;background-color:#F8F021;">';
notif_div 	+= 	'<span id="mybic_notif_msg"> MSGHERE </span> <br><br><input type="button" value="OK" onclick="document.getElementById(\'mybic_notification\').style.display=\'none\';"></div>';
if(status == 'disable') {
var notif = 'A network issue has disabled network connections for this page. Please reload this page or contact the site administrator';
} else {
var notif = 'A network issue has occurred which canceled your last request';
}
try{
} catch(e) { 
	alert('Network Unavailable: Please re-load page or contact the site administrator');
}
},
loadCSS:function(url)
{
try{
// lets check to see if the script is already loaded, if so lets remove it and add a new one
var scripts = document.getElementsByTagName('link');
s_len = scripts.length;
if(s_len > 0) {
for(var i=0;i<s_len;i++){
var reg = new RegExp(url+"$");
if (reg.test(scripts[i].href)) {
var p2 = scripts[i];
p2.parentNode.removeChild(p2);
break;
}	
}
}
newScript = document.createElement("link");
newScript.setAttribute("type", "text/css");
newScript.setAttribute("rel", "stylesheet");
newScript.setAttribute("href", url);
document.getElementsByTagName('head')[0].appendChild(newScript);
     
} catch(e) {
if(this.debug==1) {this.showDebug("MyBIC - loadCSS failed URL: "+url+" ErrName: "+e.name+" Msg: "+e.message)};	
}
},
showDebug:function(msg, label, expand) {
if(this.debug == 1) {
if(!document.getElementById('mybic_debug')) {
var errs = document.createElement('div');
errs.id = 'mybic_errs';
var deb = document.createElement('div');
deb.id = 'mybic_debug';
deb.style.border = "thick solid black";
deb.style.backgroundColor = "#eeeeee";
deb.style.padding = "10px"; 
deb.style.margin = '75px 10px 10px 10px';
deb.style.width = '90%';
deb.style.position = 'absolute';
deb.style.zIndex = '999';
deb.innerHTML += 'MyBic Debugger: <a href="#" onclick="document.getElementById(\'mybic_errs\').style.display = (document.getElementById(\'mybic_errs\').style.display==\'none\') ? \'\':\'none\'; return false;" >hide/show me!</a>';
deb.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="document.getElementById(\'mybic_errs\').innerHTML = \'\'; return false;">Clear</a>';
deb.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="XMLHTTP.prototype.debug_expand(\'block\');return false;">Expand All</a>'; //TODO:
deb.innerHTML += '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="XMLHTTP.prototype.debug_expand(\'none\'); return false;">Contract All</a><br><br>';//TODO:
deb.appendChild(errs);
if(document.body) {
document.body.appendChild(deb);
} else {
document.lastChild.appendChild(deb);
}
}
var deb = document.getElementById('mybic_errs');
if(msg == 'new') {
this.debugID++;
var dimg = '<a style="color:white;font-size:1.1em;text-decoration:none" href="#" onclick="XMLHTTP.prototype.debug_expand(this);return false;">+</a>';
deb.innerHTML += '<div id="mybiclabel_'+this.debugID+'" style="display:block;border:thin solid #999999;padding:2px;background-color:#cccccc;">'+dimg+' label'+this.debugID+': '+label+'</div>';
} else {
deb.innerHTML +='<div class="mybic_debug'+this.debugID+'" style="padding:5px;display:none; border:thin solid white;">'+msg+'</div>';
} 
}   
},
debug_expand:function(el) {
var deb = document.getElementById('mybic_errs');
var deb_len = deb.childNodes.length;
if(el == 'none' || el == 'block') {
var label = "mybic_debug";
var links = deb.getElementsByTagName('a');
var links_len = links.length;
for(var q=0;q<links_len;q++) {
links[q].innerHTML = (el == 'none') ? '+' : '>';	
}
} else {
var label = el.parentNode.id;
label = label.split('_');
label = "mybic_debug"+label[1];
}
for(var i=0; i<deb_len; i++) {
try {
if(deb.childNodes[i].className.match(new RegExp("(^"+ label + ".*$)"))) {
if(el == 'none' || el == 'block') {
deb.childNodes[i].style.display = el;
} else {
if(deb.childNodes[i].style.display == 'block') {
el.innerHTML = '+';
deb.childNodes[i].style.display = 'none';
} else {
el.innerHTML = '>';
deb.childNodes[i].style.display = 'block';
}
}
}
} catch(e) {}
}
},
restart:function() {
this.stopRequest=0;
this.call('','','queue');
}
};
var JSON = function () {
var m = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
s = {
'boolean': function (x) {
return String(x);
},
number: function (x) {
return isFinite(x) ? String(x) : 'null';
},
string: function (x) {
if (/["\\\x00-\x1f]/.test(x)) {
x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return '\\u00' +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
});
}
return '"' + x + '"';
},
object: function (x) {
if (x) {
var a = [], b, f, i, l, v;
if (x instanceof Array) {
a[0] = '[';
l = x.length;
for (i = 0; i < l; i += 1) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a[a.length] = v;
b = true;
}
}
}
a[a.length] = ']';
} else if (x instanceof Object) {
a[0] = '{';
for (i in x) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a.push(s.string(i), ':', v);
b = true;
}
}
}
a[a.length] = '}';
} else {
return;
}
return a.join('');
}
return 'null';
}
};
return {
copyright: '(c)2005 JSON.org',
license: 'http://www.JSON.org/license.html',
stringify: function (v) {
var f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
return v;
}
}
return null;
},
parse: function (text) {
try {
return eval(text);
} catch (e) {
return false;
}
}
};
}();