
function JPSpan_Serialize(Encoder){this.Encoder=Encoder;this.typeMap=new Object();};JPSpan_Serialize.prototype={typeMap:null,
addType:function(cname,callback){this.typeMap[cname]=callback;},
serialize:function(v){switch(typeof v){case 'object':
if(v===null){return this.Encoder.encodeNull();}
var c=v.constructor;if(c!=null){if(c==Array){return this.Encoder.encodeArray(v,this);}else{var match=c.toString().match(/\s*function(.*)\(/);if(match==null){return this.Encoder.encodeObject(v,this,'JPSpan_Object');}
var cname=match[1].replace(/\s/,'');if(this.typeMap[cname]){return this.typeMap[cname](v,this,cname);}else{var match=cname.match(/Error/);if(match==null){return this.Encoder.encodeObject(v,this,'JPSpan_Object');}else{return this.Encoder.encodeError(v,this,'JPSpan_Error');}
}
}
}else{return this.Encoder.encodeNull();}
break;case 'string':
return this.Encoder.encodeString(v);break;case 'number':
if(Math.round(v)==v){return this.Encoder.encodeInteger(v);}else{return this.Encoder.encodeDouble(v);};break;case 'boolean':
if(v==true){return this.Encoder.encodeTrue();}else{return this.Encoder.encodeFalse();};break;default:
return this.Encoder.encodeNull();break;}
}
}
function JPSpan_Encode_Xml(){this.Serialize=new JPSpan_Serialize(this);};JPSpan_Encode_Xml.prototype={contentType:'text/xml; charset=UTF-8',
encode:function(data){return '<?xml version="1.0" encoding="UTF-8"?><r>'+this.Serialize.serialize(data)+'</r>';},
encodeInteger:function(v){return '<i v="'+v+'"/>';},
encodeDouble:function(v){return '<d v="'+v+'"/>';},
encodeString:function(v){return '<s>'+v.replace(/&/g,'&amp;').replace(/</g,'&lt;')+'</s>';},
encodeNull:function(){return '<n/>';},
encodeTrue:function(){return '<b v="1"/>';},
encodeFalse:function(){return '<b v="0"/>';},
encodeArray:function(v,Serializer){var indexed=new Array();var a='';for(var i=0;i<v.length;i++){indexed[i]=true;a+='<e k="'+i+'">'+Serializer.serialize(v[i])+'</e>';};for(var prop in v){if(indexed[prop]){continue;};a+='<e k="'+prop+'">'+Serializer.serialize(v[prop])+'</e>';};return '<a>'+a+'</a>';},
encodeObject:function(v,Serializer,cname){var o='';for(var prop in v){o+='<e k="'+prop+'">'+Serializer.serialize(v[prop])+'</e>';};return '<o c="'+cname.toLowerCase()+'">'+o+'</o>';},
encodeError:function(v,Serializer,cname){var e=new Object();if(!v.name){e.name=cname;e.message=v.description;}else{e.name=v.name;e.message=v.message;};return this.encodeObject(e,Serializer,cname);}
}
function JPSpan_Client_Error(e,code){e.name='Client_Error';e.code=code;return e;};function JPSpan_HttpClient(){};JPSpan_HttpClient.prototype={xmlhttp:null,
userhandler:null,
timeout_id:null,
init:function(){try{this.xmlhttp=new XMLHttpRequest();}catch(e){var MSXML_XMLHTTP_PROGIDS=new Array(
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP'
);var success=false;for(var i=0;i < MSXML_XMLHTTP_PROGIDS.length&&!success;i++){try{this.xmlhttp=new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);success=true;}catch(e){}
}
if(!success){throw JPSpan_Client_Error(
new Error('Unable to create XMLHttpRequest.'),
1000
);}
}
},
call:function(request){if(!this.xmlhttp){this.init();}
if(this.callInProgress()){throw JPSpan_Client_Error(
new Error('Call in progress'),
1001
);};request.type='sync';request.prepare(this.xmlhttp);this.xmlhttp.setRequestHeader('Accept-Charset','UTF-8');request.send();if(this.xmlhttp.status==200){return this.xmlhttp.responseText;}else{var errorMsg='['+this.xmlhttp.status
+'] '+this.xmlhttp.statusText;var err=new Error(errorMsg);err.headers=this.xmlhttp.getAllResponseHeaders();throw JPSpan_Client_Error(err,1002);}
},
asyncCall:function(request,handler){var callName=null;if(arguments[2]){callName=arguments[2];}
if(!this.xmlhttp){this.init();}
if(this.callInProgress()){throw JPSpan_Client_Error(
new Error('Call in progress'),
1001
);};this.userhandler=handler;if(this.userhandler.onInit){try{this.userhandler.onInit(callName);}catch(e){this.displayHandlerError(e);}
}
request.type='async';request.prepare(this.xmlhttp);this.xmlhttp.setRequestHeader('Accept-Charset','UTF-8');var self=this;this.timeout_id=window.setTimeout(function(){self.abort(self,callName);},request.timeout);this.xmlhttp.onreadystatechange=function(){self.stateChangeCallback(self,callName);}
request.send();},
callInProgress:function(){switch(this.xmlhttp.readyState){case 1:
case 2:
case 3:
return true;break;default:
return false;break;}
},
abort:function(client,callName){if(client.callInProgress()){client.xmlhttp.abort();var errorMsg='Operation timed out';if(callName){errorMsg+=': '+callName;}
if(client.userhandler.onError){var ex=JPSpan_Client_Error(new Error(errorMsg),1003);try{client.userhandler.onError(ex,callName);}catch(e){client.displayHandlerError(e);}
}
}
},
displayHandlerError:function(e){var errorMsg="Error in Handler\n";if(e.name){errorMsg+='Name: '+e.name+"\n";};if(e.message){errorMsg+='Message: '+e.message+"\n";}else if(e.description){errorMsg+='Description: '+e.description+"\n";};if(e.fileName){errorMsg+='File: '+e.fileName+"\n";};if(e.lineNumber){errorMsg+='Line: '+e.lineNumber+"\n";};alert(errorMsg);},
stateChangeCallback:function(client,callName){switch(client.xmlhttp.readyState){case 1:
if(client.userhandler.onOpen){try{client.userhandler.onOpen(callName);}catch(e){client.displayHandlerError(e);}
}
break;case 2:
if(client.userhandler.onSend){try{client.userhandler.onSend(callName);}catch(e){client.displayHandlerError(e);}
}
break;case 3:
if(client.userhandler.onProgress){try{client.userhandler.onProgress(callName);}catch(e){client.displayHandlerError(e);}
}
break;case 4:
window.clearTimeout(client.timeout_id);try{switch(client.xmlhttp.status){case 200:
if(client.userhandler.onLoad){try{client.userhandler.onLoad(client.xmlhttp.responseText,callName);}catch(e){client.displayHandlerError(e);}
}
break;case 0:
break;default:
if(client.userhandler.onError){try{var errorMsg='['+client.xmlhttp.status
+'] '+client.xmlhttp.statusText;var err=new Error(errorMsg);err.headers=this.xmlhttp.getAllResponseHeaders();client.userhandler.onError(JPSpan_Client_Error(err,1002),callName);}catch(e){client.displayHandlerError(e);}
}
break;}
}catch(e){}
break;}
}
}
function JPSpan_Request(encoder){this.encoder=encoder;}
JPSpan_Request.prototype={encoder:null,
serverurl:'',
requesturl:'',
body:'',
args:null,
type:null,
http:null,
timeout:20000,
addArg:function(name,value){if(!this.args){this.args=[];}
var illegal=/[\W_]/;if(!illegal.test(name)){this.args[name]=value;}else{throw JPSpan_Client_Error(
new Error('Invalid parameter name ('+name+')'),
1004
);}
},
reset:function(){this.serverurl='';this.requesturl='';this.body='';this.args=null;this.type=null;this.http=null;this.timeout=20000;},
build:function(){},
prepare:function(http){},
send:function(http){}
};function JPSpan_Request_RawPost(encoder){var oParent=new JPSpan_Request(encoder);oParent.build=function(){try{this.body=this.encoder.encode(this.args);}catch(e){throw JPSpan_Client_Error(e,1006);};this.requesturl=this.serverurl;};oParent.prepare=function(http){this.http=http;this.build();switch(this.type){case 'async':
try{this.http.open('POST',this.requesturl,true);}catch(e){throw JPSpan_Client_Error(new Error(e),1007);};break;case 'sync':
try{this.http.open('POST',this.requesturl,false);}catch(e){throw JPSpan_Client_Error(new Error(e),1007);};break;default:
throw JPSpan_Client_Error(
new Error('Call type invalid '+this.type),
1005
);break;};this.http.setRequestHeader('Content-Length',this.body.length);this.http.setRequestHeader('Content-Type',this.encoder.contentType);};oParent.send=function(){this.http.send(this.body);};return oParent;};function JPSpan_RemoteObject(){}
JPSpan_RemoteObject.prototype={Async:function(userHandler){this.__initResponseHandler(this,userHandler);this.__callState="async";},
Sync:function(){this.__responseHandler=null;this.__callState="sync";},
GetXMLHttp:function(){if(!this.__client){this.__initClient();}
return this.__client.xmlhttp;},
clientErrorFunc:function(e){alert(this.__drawErrorMsg('Client_Error',e));},
timeout:20000,
serverErrorFunc:function(e){var errorMsg=this.__drawErrorMsg('Server_Error',e);if(e.file&&e.line){errorMsg+="\nServer script: "
+e.file+" on line "+e.line;}
alert(errorMsg);},
applicationErrorFunc:function(e){alert(this.__drawErrorMsg('Application_Error',e));},
__drawErrorMsg:function(type,e){try{var errorMsg='['+e.name+']';if(e.code){errorMsg+='['+e.code+']';}
errorMsg+=' '+e.message;}catch(ex){var errorMsg='['+type+'] ';if(e.code){errorMsg+='['+e.code+']';}
errorMsg+=' '+e.message;}
if(e.client&&e.call){errorMsg+="\nMethod called: "
+e.client+"."+e.call+"()";}
return errorMsg;},
__serverurl:null,
__request:null,
__client:null,
__responseHandler:null,
__callState:'sync',
__remoteClass:'',
__initClient:function(){this.__client=new JPSpan_HttpClient();},
__initResponseHandler:function(self,userHandler){self.__responseHandler=new Object();self.__responseHandler.context=self;self.__responseHandler.userHandler=userHandler;self.__responseHandler.onInit=function(callName){var initFunc=callName+'Init';if(this.userHandler[initFunc]){try{this.userHandler[initFunc]();}catch(e){self.__client.displayHandlerError(e);}
}
},
self.__responseHandler.onOpen=function(callName){var openFunc=callName+'Open';if(this.userHandler[openFunc]){try{this.userHandler[openFunc]();}catch(e){self.__client.displayHandlerError(e);}
}
},
self.__responseHandler.onSend=function(callName){var sendFunc=callName+'Send';if(this.userHandler[sendFunc]){try{this.userHandler[sendFunc]();}catch(e){self.__client.displayHandlerError(e);}
}
},
self.__responseHandler.onProgress=function(callName){var progressFunc=callName+'Progress';if(this.userHandler[progressFunc]){try{this.userHandler[progressFunc]();}catch(e){self.__client.displayHandlerError(e);}
}
},
self.__responseHandler.onLoad=function(response,callName){try{dataFunc=eval(response);try{data=dataFunc();if(this.userHandler[callName]){try{this.userHandler[callName](data);}catch(e){self.__client.displayHandlerError(e);}
}else{alert('Your handler must define a method '+callName);}
}catch(e){e.client=self.__responseHandler.context.__remoteClass;e.call=callName;if(e.name=='Server_Error'){this.context.serverErrorFunc(e);}else{var errorFunc=callName+'Error';if(this.userHandler[errorFunc]){try{this.userHandler[errorFunc](e);}catch(e){self.__client.displayHandlerError(e);}
}else{this.context.applicationErrorFunc(e);}
}
}
}catch(e){e.name='Server_Error';e.code=2006;e.response=response;e.client=self.__responseHandler.context.__remoteClass;e.call=callName;this.context.serverErrorFunc(e);}
};self.__responseHandler.onError=function(e,callName){e.client=self.__responseHandler.context.__remoteClass;e.call=callName;this.context.clientErrorFunc(e);};},
__call:function(url,args,callName){if(!this.__client){this.__initClient();}
this.__request.reset();this.__request.serverurl=url;this.__request.timeout=this.timeout;for(var i=0;i < args.length;i++){this.__request.addArg(i,args[i]);};if(this.__callState=="async"){return this.__asyncCall(this.__request,callName);}else{return this.__syncCall(this.__request);}
},
__asyncCall:function(request,callName){try{this.__client.asyncCall(request,this.__responseHandler,callName);}catch(e){this.clientErrorFunc(e);}
return;},
__syncCall:function(request){try{var response=this.__client.call(request);try{var dataFunc=eval(response);try{return dataFunc();}catch(e){if(e.name=='Server_Error'){this.serverErrorFunc(e);}else{this.applicationErrorFunc(e);}
}
}catch(e){e.name='Server_Error';e.code=2006;e.response=response;this.serverErrorFunc(e);}
}catch(e){this.clientErrorFunc(e);}
}
};function getpassword(){var oParent=new JPSpan_RemoteObject();if(arguments[0]){oParent.Async(arguments[0]);}
oParent.__serverurl='http://cofacio.com/server.php?getpassword';oParent.__remoteClass='getpassword';oParent.__request=new JPSpan_Request_RawPost(new JPSpan_Encode_Xml());oParent.get_password=function(){var url=this.__serverurl+'/get_password/';return this.__call(url,arguments,'get_password');};return oParent;}
function checkusername(){var oParent=new JPSpan_RemoteObject();if(arguments[0]){oParent.Async(arguments[0]);}
oParent.__serverurl='http://cofacio.com/server.php?checkusername';oParent.__remoteClass='checkusername';oParent.__request=new JPSpan_Request_RawPost(new JPSpan_Encode_Xml());oParent.check_username=function(){var url=this.__serverurl+'/check_username/';return this.__call(url,arguments,'check_username');};return oParent;}
function userupdates(){var oParent=new JPSpan_RemoteObject();if(arguments[0]){oParent.Async(arguments[0]);}
oParent.__serverurl='http://cofacio.com/server.php?userupdates';oParent.__remoteClass='userupdates';oParent.__request=new JPSpan_Request_RawPost(new JPSpan_Encode_Xml());oParent.get_user_updates=function(){var url=this.__serverurl+'/get_user_updates/';return this.__call(url,arguments,'get_user_updates');};oParent.delete_notification=function(){var url=this.__serverurl+'/delete_notification/';return this.__call(url,arguments,'delete_notification');};return oParent;}
function markasviewed(){var oParent=new JPSpan_RemoteObject();if(arguments[0]){oParent.Async(arguments[0]);}
oParent.__serverurl='http://cofacio.com/server.php?markasviewed';oParent.__remoteClass='markasviewed';oParent.__request=new JPSpan_Request_RawPost(new JPSpan_Encode_Xml());oParent.mark_as_shown=function(){var url=this.__serverurl+'/mark_as_shown/';return this.__call(url,arguments,'mark_as_shown');};return oParent;}
function deletenotification(){var oParent=new JPSpan_RemoteObject();if(arguments[0]){oParent.Async(arguments[0]);}
oParent.__serverurl='http://cofacio.com/server.php?deletenotification';oParent.__remoteClass='deletenotification';oParent.__request=new JPSpan_Request_RawPost(new JPSpan_Encode_Xml());oParent.delete_notification=function(){var url=this.__serverurl+'/delete_notification/';return this.__call(url,arguments,'delete_notification');};return oParent;}
function gettwitter(){var oParent=new JPSpan_RemoteObject();if(arguments[0]){oParent.Async(arguments[0]);}
oParent.__serverurl='http://cofacio.com/server.php?gettwitter';oParent.__remoteClass='gettwitter';oParent.__request=new JPSpan_Request_RawPost(new JPSpan_Encode_Xml());oParent.get_twget_twitteritter=function(){var url=this.__serverurl+'/get_twget_twitteritter/';return this.__call(url,arguments,'get_twget_twitteritter');};return oParent;}
