/* 
@name: /sso/registerCell.js
@desc: Triangolazione del cellulare 
@authors: Marco Toci
@lastauthor: Marco Toci (email: marco.toci@staff.dada.net - skype: marco.toci) Tue Feb 27 09:58:07 CET 2007 
*/
var registerCell = Class.create();
registerCell.prototype = {
	initialize: function (gP) {
    var opzioni = {
        setName: 'root',
        rowName: 'result',
        statusAttr: null,
        okValue:	null,
        errorAttr: null,
		  randomizeURL: true
    };
	 
	 this.config={
		 scriptBaseUrl: '/sso/',
		 registerScript: 'register_aj.php',
		 confirmScript: 'confirm_aj.php'
     };
	  
	 this.generalParams=gP;

	 this.ds = new DataSetHolder(opzioni);
	 this.ds.addListener( "onDataChange", this );
    this.ds.addListener( "onDataError", this );
	 this.dati=new Array();
	} ,
	
	
  askForPin: function  ( myform ) {
	 var cell=myform.cell.value;
	 if (myform.cell_1){
		 cell += myform.cell_1.value;
	 }
	 	 if (myform.cell_2){
		 cell += myform.cell_2.value;
	 }
	 this.pendingCell=cell;
	 if (myform.op){
		 var op= myform.op.value;
	 }else{
		 var op='';
	 }

    var pars ="service="+this.generalParams['service']+
              "&numero2="+escape(this.pendingCell)+
              "&id_operatore="+op;
	if (this.generalParams['tipo']){
		pars += "&tipo="+this.generalParams['tipo'];
	}
	if (this.generalParams['wkflcode']){
		pars += "&wkflcode="+this.generalParams['wkflcode'];
	}
	if (this.generalParams['id_infoservice']){
		pars += "&id_infoservice="+this.generalParams['id_infoservice'];
	}
	if (this.generalParams['id_wapdld']){
		pars += "&id_wapdld="+this.generalParams['id_wapdld'];
	}
	if (this.generalParams['section']){
		pars += "&section="+this.generalParams['section'];
	}
	if (this.generalParams['action']){
		pars += "&action="+this.generalParams['action'];
	}
	if (this.generalParams['flight_id']){
		pars += "&flight_id="+this.generalParams['flight_id'];
	}
	if (this.generalParams['trlgver']){
		pars += "&trlgver="+this.generalParams['trlgver'];
	}
	if (this.generalParams['landing_id']){
		pars += "&landing_id="+this.generalParams['landing_id'];
	}
	if (this.generalParams['community_id']){
		pars += "&community_id="+this.generalParams['community_id'];
	}
	 var pinUrl= this.config['scriptBaseUrl']+
					this.config['registerScript'];
		
		
					
		this.ds.bindTo(pinUrl, pars.replace(/\+/,"%2b"), 'get');
    this.showWaitForPinMessage();
    
	 return ;
  },

  askPinResponse: function (){
    
    
   var status=this.dati['status']?this.dati['status']:'error';
	 var code=this.dati['code']?this.dati['code']:0;
	 var returl=this.dati['returl'];
    switch (status){
      case 'ok':
			if (code==302 && returl){
				this.doRedirect(returl);
			}else if (this.dati['xmlrpc']){
			  this.XmlRpcActivate();
			}else{
				this.showInsertPinForm(this.dati['msg_title'], this.dati['msg_desc']);
			}
			break;
      case 'error':
        this.askPinError();
        break;
     default:
        this.askPinError();
        break;
    }
  },

  XmlRpcActivate: function (){
		var rpc = new xmlRPC();
		rpc.notSetCookies=1;
		rpc.parent=this;
		rpc.startChecking(this.dati['cellulare']); 
		this.showXMLRPCWaitMessage();
  },

  
    XmlRpcResponseOk: function (){
		this.hideAllMessages();
		var cell= this.dati['cellulare'];
		var msg_title= this.dati['msg_ok_title']?this.dati['msg_ok_title']:'';
		var msg_desc= this.dati['msg_ok_desc']?this.dati['msg_ok_desc']:'';
		this.elementUpdate('dadanetuser_cell',cell );
		this.elementUpdate('responseTitle',msg_title );
		Element.show('responseTitle');
		this.elementUpdate('responseDesc', msg_desc);
		Element.show('responseDesc');
		if (this.generalParams['substeps']){
			if($('step_subscription_confirm')){
				 Element.hide('step_subscription_confirm');
			 }
			 if($('step_subscription_confirm_ok')){
				 Element.show('step_subscription_confirm_ok');
			 }
		}
  },

  
  confirmPinResponse: function (){
    var status=this.dati['status']?this.dati['status']:'error';
    switch (status){
      case 'ok':
        this.showCompleteMessage();
        break;
      case 'error':
        this.confirmPinError();
        break;
     default:
        this.confirmPinError();
        break;
    }
  },
  
  confirmPin: function (password){
	  var pars ="service="+this.generalParams['service']+
              "&password="+escape(password);
		if (this.pendingCell){
			pars += "&cellulare="+this.pendingCell;
		}else if (this.generalParams['pendingCell']){
			pars += "&cellulare="+this.generalParams['pendingCell'];
		}
		
		if (this.dati['is_in_club']){
			pars += "&is_in_club="+this.dati['is_in_club'];
		}else if (this.generalParams['is_in_club']){
			pars += "&is_in_club="+this.generalParams['is_in_club'];
		}
		
		if (this.dati['phone_company']){
			pars += "&phone_company="+this.dati['phone_company'];
		}else if (this.generalParams['phone_company']){
			pars += "&phone_company="+this.generalParams['phone_company'];
		}
		if (this.dati['id_operatore']){
			pars += "&id_operatore="+this.dati['id_operatore'];
		}else if (this.generalParams['id_operatore']){
			pars += "&id_operatore="+this.generalParams['id_operatore'];
		}
		
		if (this.generalParams['tipo']){
			pars += "&tipo="+this.generalParams['tipo'];
		}
		if (this.generalParams['id_infoservice']){
			pars += "&id_infoservice="+this.generalParams['id_infoservice'];
		}
		if (this.generalParams['id_wapdld']){
			pars += "&id_wapdld="+this.generalParams['id_wapdld'];
		}
		if (this.generalParams['section']){
			pars += "&section="+this.generalParams['section'];
		}
		if (this.generalParams['trlgver']){
			pars += "&trlgver="+this.generalParams['trlgver'];
		}
		if (this.generalParams['landing_id']){
			pars += "&landing_id="+this.generalParams['landing_id'];
		}
		if (this.generalParams['community_id']){
			pars += "&community_id="+this.generalParams['community_id'];
		}

		
	  var pinUrl = this.config['scriptBaseUrl']+
						this.config['confirmScript'];
					this.ds.bindTo(pinUrl, pars.replace(/\+/,"%2b"), 'get');
    this.showWaitForConfirmMessage();
	 return ;
  },
    XMLRPCconfirm: function (){
	  var pars ="service="+this.generalParams['service']+
              "&cellulare="+this.dati['cellulare']+
              "&xmlrpc="+1+
				  "&is_in_club="+this.dati['is_in_club']+
				  "&phone_company="+this.dati['phone_company'];

	  var pinUrl = this.config['scriptBaseUrl']+
						this.config['confirmScript'];
					this.ds.bindTo(pinUrl, pars.replace(/\+/,"%2b"), 'get');
    this.showWaitForConfirmMessage();
	 return ;
  },

  showXMLRPCWaitMessage: function(){
	this.hideAllMessages();
	var msg_title= this.dati['msg_title']?this.dati['msg_title']:'';
	var msg_desc= this.dati['msg_desc']?this.dati['msg_desc']:'';
	if (this.generalParams['substeps']){
		if($('step_subscription')){
		 Element.hide('step_subscription');
		}
		if($('step_subscription_confirm')){
		 Element.show('step_subscription_confirm');
		}
	}
	if ($('xmlRpcWaitMessage')){
	   Element.show('xmlRpcWaitMessage');
	}else{
		this.elementUpdate('responseTitle',msg_title );
		Element.show('responseTitle');
		this.elementUpdate('responseDesc', msg_desc);
		Element.show('responseDesc');
	}
  },
  
  
  //-----------------------------------Listener
  onDataChange: function(objEvt){
	 this.dati=objEvt.target.getItemAt(0);
    switch(this.dati['action']){
			case 'askPin':
				this.askPinResponse();
				break;
			case 'confirmPin':
				this.confirmPinResponse();
				break;
		}
  },

  onDataError: function(){
   this.hideAllMessages();
   Element.show('generalError');
  },
  
  //---------------------------------presentazione
	hideAllMessages: function(){
		Element.hide('opInProgress');
		Element.hide('insertPin');
		Element.hide('responseTitle');
		Element.hide('responseDesc');
		Element.hide('errorTitle');
		Element.hide('errorDesc');
		Element.hide('insertCell');
		Element.hide('generalError');
		if ($('xmlRpcWaitMessage')){
			Element.hide('xmlRpcWaitMessage');
		}
	},  

	showCompleteMessage: function(){
		this.hideAllMessages();
		var msg_title= this.dati['msg_title']?this.dati['msg_title']:'';
		var msg_desc= this.dati['msg_desc']?this.dati['msg_desc']:'';
		var cell= this.dati['cellulare'];
		this.elementUpdate('dadanetuser_cell',cell );
		this.elementUpdate('responseTitle',msg_title );
		Element.show('responseTitle');
		this.elementUpdate('responseDesc', msg_desc);
		Element.show('responseDesc');
		if (this.generalParams['substeps']){
			if($('step_subscription_confirm')){
				 Element.hide('step_subscription_confirm');
			 }
			 if($('step_subscription_confirm_ok')){
				 Element.show('step_subscription_confirm_ok');
			 }
		}
		
		if (this.dati['is_in_club'] && this.generalParams['registerDoneUriInClub'] ){
			this.doRedirect(this.generalParams['registerDoneUriInClub']);
		}else if (this.generalParams['registerDoneUri']){
			this.doRedirect(this.generalParams['registerDoneUri']);
		}
	},

  
  
	doRedirect: function(url_to){
		if (opener) {
			if(opener.closed){
				window.setTimeout("window.location.href='"+url_to+"';",5000);
			}else{
				window.setTimeout("opener.focus();window.close();",5000);
				opener.location.href = url_to;
			}
		}else{
			window.setTimeout("window.location.href ='"+url_to+"';",5000);
		}
	},
  
  
  
  showWaitForConfirmMessage: function(){
	 this.hideAllMessages();
    Element.show('opInProgress');
  },

  showWaitForPinMessage: function(){
	 this.hideAllMessages();
    Element.show('opInProgress');
  },

  showInsertPinForm: function(title,desc){
	 this.hideAllMessages();
    this.elementUpdate('responseTitle', title);
    this.elementUpdate('responseDesc', desc);
	 Element.show('responseTitle');
    Element.show('responseDesc');
    Element.show('insertPin');
	 if (this.generalParams['substeps']){
		 if($('step_subscription')){
			 Element.hide('step_subscription');
		 }
		 if($('step_subscription_confirm')){
			 Element.show('step_subscription_confirm');
		 }
	 }
  },
  
  elementUpdate: function(element, content){
	  if ($(element)){
		  $(element).innerHTML=content;
	  }
  },

  askPinError: function(){
	 var msg_title= this.dati['msg_title']?this.dati['msg_title']:'';
	 var msg_desc= this.dati['msg_desc']?this.dati['msg_desc']:'';
	 var code=this.dati['code']?this.dati['code']:'10'; //10 e' l'errore generico
	 this.hideAllMessages();
		switch (code){
			case '500':
				// Pin richiesto troppe volte
				this.elementUpdate('errorTitle', msg_title);
				this.elementUpdate('errorDesc', msg_desc);
				Element.show('errorTitle');
				Element.show('errorDesc');
			  break;
			 case '412':
			 case '402':
				// Utente già iscritto
				this.elementUpdate('responseTitle', msg_title);
				this.elementUpdate('responseDesc', msg_desc);
				Element.show('responseTitle');
				Element.show('responseDesc');
				Element.show('insertPin');
				if (this.generalParams['substeps']){
					 if($('step_subscription')){
						 Element.hide('step_subscription');
					 }
					 if($('step_subscription_confirm')){
						 Element.show('step_subscription_confirm');
					 }
				 }
			  break;
			default:    
				Element.show('insertCell');
				this.elementUpdate('errorTitle', msg_title);
				this.elementUpdate('errorDesc', msg_desc);
				Element.show('errorTitle');
				Element.show('errorDesc');
				Element.hide('insertPin');
				if (this.generalParams['substeps']){
					 if($('step_subscription_confirm')){
						 Element.hide('step_subscription_confirm');
					 }
					 if($('step_subscription')){
						 Element.show('step_subscription');
					 }
				 }
				break;
		}
	},

    confirmPinError: function(){
    var msg_title= this.dati['msg_title']?this.dati['msg_title']:'';
    var msg_desc= this.dati['msg_desc']?this.dati['msg_desc']:'';
    var code=this.dati['code']?this.dati['code']:'10'; //10 e' l'errore generico
	 this.hideAllMessages();
    switch (code){
		 case 400:
		 		Element.show('insertPin');
				this.elementUpdate('responseTitle', msg_title);
				this.elementUpdate('responseDesc', msg_desc);
				Element.show('responseTitle');
				Element.show('responseDesc');
			  break;
      default:    
			Element.show('insertPin');
			this.elementUpdate('errorTitle', msg_title);
			this.elementUpdate('errorDesc', msg_desc);
			Element.show('errorTitle');
			Element.show('errorDesc');
			break;
    }
  },

  
	  __xmlRPCDone: function(dadanetuser){
		  this.XMLRPCconfirm(dadanetuser);
	  }  
	  
  
};


