/*
@name: /htdocs/sso/xmlRPC.js
@desc: Oggetto javascript per xmlRPC su iscrizione
@authors: Marco Biondi
@lastauthor: Marco Biondi
*/
var xmlRPC = Class.create();
xmlRPC.prototype = {
	initialize: function(options) {
		this.options = {
				timeout: 5000,
				uri: '/cgi-bin/sso/check_user.chm',
				uri_orange: '/cgi-bin/hotw/check_user_orange.chm',
				method: 'POST',
				addParam: null,
				errMsg: 'Maximum number of tries exceeded. Reload the page!',
				maxTry: 100,
				randomizeURL: false
			};

		Object.extend(this.options, options || {});

		this._number = null;
		this._AjaxR = null;
		this._tries = 0;
	},


	stopChecking: function () {
		this._go=0;
	},
	
	
	startChecking: function (thisdn, dest, tipo, id_wapdld, operator) {
		if (!this._go){
			this._go=1;
			this._number = thisdn;
			this.g_dest = dest;
			this.g_tipo = tipo;
			this.g_id_wapdld = id_wapdld;
			if (operator == 18){
				this.uri=this.options.uri_orange;
			}else{
				this.uri=this.options.uri;
			}
			this._doTimeout.bind(this);
			this._doNextReq();
		}
	},
	
	

	_doTimeout: function()
	{
		var urlBind = this.uri;

		if(this._AjaxR)
		{
			delete(this._AjaxR);
		}
		
		if(this.options.randomizeURL)
		{
			var rnd = "d=" + Date.parse(new Date()) + "&r=" + Math.random();
			if(urlBind.indexOf('?') >= 0 )
			{
				urlBind += '&'+rnd;
			}
			else
			{
				urlBind += '?'+rnd;
			}
		}

		var opts = {
					onComplete:		this._onReqComplete.bind(this),
					onFailure:		this._onReqFailure.bind(this),
					onException:	this._onReqException.bind(this),
					method: this.options.method
				};
		opts['parameters']='';
		if (this._number){
			opts['parameters'] = "cellulare=" + escape(this._number);
		}
		if (this.g_dest){
			opts['parameters']+= "&destinatario=" + escape(this.g_dest);
		}
		if (this.g_tipo){
			opts['parameters']+= "&tipo=" + escape(this.g_tipo);
		}
		if (this.g_id_wapdld){
			opts['parameters']+= "&id_wapdld=" + escape(this.g_id_wapdld);
		}
		if (this.options.check_billing_mode){
			opts['parameters']+= "&check_billing_mode=" + escape(this.options.check_billing_mode);
		}
		if (this.options.check_token_extra_gt){
			opts['parameters']+= "&token_extra_gt=" + escape(this.options.token_extra_gt);
		}
		if (this.options.subscribed_service){
			opts['parameters']+= "&subscribed_service=" + escape(this.options.subscribed_service);
		}
		
		if (this.notSetCookies){
			opts['parameters']+= "&notsetcookies=" + this.notSetCookies;
		}
		

		if(this.options.addParam){
			opts['parameters'] += '&'+this.options.addParam;
		}
		opts['parameters']=opts['parameters'].replace(/\+/,"%2b");
		opts['parameters']=opts['parameters'].replace(/^\&/,"");
		this._AjaxR = new Ajax.Request( urlBind, opts );
	},

	_doNextReq: function() {
		if (this._go==1){
			if(this._tries <= this.options.maxTry)
			{
				window.setTimeout(this._doTimeout.bind(this), this.options.timeout);
				this._tries++;
			}
			else{
				if(this.options.errorPage ){
					document.location=this.options.errorPage;
                }else if ( typeof(this.options.errorFunction) == 'function' ) {
                    this.options.errorFunction();
				}else{
					alert(this.options.errMsg);
				}
				this._go=0;
			}
		}
	},

	_onReqFailure: function(req, json) {
		if(_JSDebug || _ISStaging)
			console.debug("HTTP request failed: %1.o", req);
		this._doNextReq();
	},

	_onReqException: function(aReq, errObj) {
		if(_JSDebug || _ISStaging)
			console.debug("HTTP request thrown an exception: %1.o", errObj);
		this._doNextReq();
	},

	_onReqComplete: function(req) {
		var docRoot = req.responseXML;
		var valueTags = docRoot.getElementsByTagName("isinclub");
		var valTag = valueTags[0];
		var value = valTag.childNodes.item(0).nodeValue;

		if ( value == "true" )
		{
			
			if (this.parent){
				if(typeof(this.parent.__xmlRPCDone) == 'function'){
						valueTags = docRoot.getElementsByTagName("dadanetuser");
						valTag = valueTags[0];
						value = valTag.childNodes.item(0).nodeValue;
						this.parent.__xmlRPCDone(value);
				}
			}else if(typeof(__xmlRPCDone) == 'function'){
				__xmlRPCDone(this.caller);
			}else if (this.options.altDestUrl){
				document.location=this.options.altDestUrl;
			}else{
				var form = document.forms['completedform'].submit();
			}
		}
		else
		{
			this._doNextReq();
		}
	}
}
