/**
 * Variable
 */
var ISCONNECTED = 'isconnected';
var CONNECT = 'connect';
var DISCONNECT = 'disconnect';
var GloginLabel;
var GpasswordLabel;
var GsubmitLabel;
var Gdns;
var GelementIdToIncludeIn;
var GidPartner;
var GidPrivateWhiteLabel;


/**
 * Callback
 */ 
function logToRCPCallback(results) {
	
	var action = results.action;
	var RCPCustomerName = results.RCPCustomerName;
	var elementIdToIncludeIn = document.getElementById(GelementIdToIncludeIn);
	
	var html = []; 
	
	if(RCPCustomerName != "") {
		html.push('<div id="loginDiv">');
		html.push('<span id="loginImage1"></span>');
		html.push('<span id="customerName">'+RCPCustomerName+'</span>');
		html.push('&nbsp;');
		html.push('<a href="javascript:logToRCP(\''+DISCONNECT+'\')">D&eacute;connexion</a>');
		html.push('&nbsp;|&nbsp;');
		html.push('<a href="'+Gdns+'account/myaccount.do">Mon profil</a>');
		html.push('&nbsp;|&nbsp;');
		html.push('<a href="'+Gdns+'account/listpurchase.do">Mes s&eacute;jours</a>');
		html.push('&nbsp;|&nbsp;');
		html.push('<a href="'+Gdns+'account/listpassengergroup.do">Mes groupes de voyageurs</a>');
		html.push('<span id="loginImage2"></span>');
		html.push('</div>');
	}
	else {
		html.push('<div id="loginDiv">');
		html.push('<form name="loginForm">');
		html.push('<span id="loginImage1"></span>');
		html.push('<span id="loginLabel">'+GloginLabel+'</span>');
		html.push('<input type="text" id="login" name="login" value=""/ onkeydown="send(event)" />');
		html.push('<span id="passwordLabel">'+GpasswordLabel+'</span>');
		html.push('<input type="password" id="password" name="password" value="" onkeydown="send(event)" />');
		html.push('<input type="button" name="submit" value="'+GsubmitLabel+'" onclick="logToRCP(\''+CONNECT+'\');"/>');
		if(action == CONNECT)
			html.push('<span id="loginErrorText">'+GloginLabel+' ou '+GpasswordLabel+' non valide</span>');		
		html.push('&nbsp;&nbsp;');
		html.push('<a href="'+Gdns+'account/createAccount.jsp">Cr&eacute;er un compte</a>');
		html.push('&nbsp;|&nbsp;');
		html.push('<a href="'+Gdns+'accountPassword.jsp">Mot de passe oubli&eacute;</a>');
		html.push('<span id="loginImage2"></span>');
		html.push('</form>');
		html.push('</div>');
	}
	elementIdToIncludeIn.innerHTML = html.join('');
}


/**
 * Connection
 */
function logToRCP(action) { 
 
    var Get = YAHOO.util.Get, 
        tIds = {}, 
        current = null,
        login = '',
        password = '';
      
    if(document.getElementById('login') != null)
    	login = document.getElementById('login').value;
    if(document.getElementById('password') != null)
    	password = document.getElementById('password').value;
         
    var onSiteExplorerSuccess = function(o) {  		
        if (o.tId in tIds) { 
			YAHOO.log("The Get Utility has fired the success handler indicating that the " + 
          	"requested script has loaded and is ready for use.", "info", "example"); 
        } 
        else { 
			YAHOO.log("The Get utility has fired onSuccess but the webservice callback did not " + 
          	"fire.  We could retry the transaction here, or notify the user of the " + 
          	"failure.", "info", "example"); 
        } 
    } 
 
    var onSiteExplorerFailure = function(o) { 
    	YAHOO.log("An error occured while getting datas");
    } 
     
    // Function to retrieve data from RPC web service 
    var getSiteExplorerData = function(action) { 
    
        var sURL = "";
        
        if(action == CONNECT)
			sURL = Gdns+"LoginWidgetController?action="+action+"&login="+login+"&password="+password+"&elementIdToIncludeIn="+GelementIdToIncludeIn+"&callback=logToRCPCallback";
		if(action == ISCONNECTED)
        	sURL = Gdns+"LoginWidgetController?action="+action+"&callback=logToRCPCallback";
		if(action == DISCONNECT)
			sURL = Gdns+"LoginWidgetController?action="+action+"&callback=logToRCPCallback";
			
		if(GidPartner != null && GidPartner != "" && GidPartner != "undefined")
			sURL = sURL+"&idPartner="+GidPartner;
			
		if(GidPrivateWhiteLabel != null && GidPrivateWhiteLabel != "" && GidPrivateWhiteLabel != "undefined")
			sURL = sURL+"&idPrivateWhiteLabel="+GidPrivateWhiteLabel;

		sURL = sURL+"&ramdom="+Math.random() ;
		
        var transactionObj = Get.script(sURL, { 
            onSuccess: onSiteExplorerSuccess, 
            onFailure: onSiteExplorerFailure, 
            scope    : this 
        }); 

        current = transactionObj.tId;  
    } 
    
    getSiteExplorerData(action);
}




/**
 * Constructor for LoginWidget
 */
LoginWidget = function(elementIdToIncludeIn, loginLabel, passwordLabel, submitLabel, option) {   
	
	// Initailize variables
	GloginLabel = loginLabel;
	GpasswordLabel = passwordLabel;
	GsubmitLabel = submitLabel;
	GelementIdToIncludeIn = elementIdToIncludeIn;
	
	if(typeof option == 'string') {
		 Gdns = option;
	}
	else {
		if(option.dns != null)
			Gdns = option.dns;
		if(option.idPartner != null)
			GidPartner = option.idPartner;
		if(option.idPrivateWhiteLabel != null)
			GidPrivateWhiteLabel = option.idPrivateWhiteLabel;
	}

    // Add '/' at the end of the dns 
	if(Gdns != null && Gdns.charAt(Gdns.length-1) != '/') Gdns = Gdns+'/';
    
    // Initialize widget
    logToRCP(ISCONNECTED);
}; 

function send(e) {
	if(e.keyCode == 13)
		logToRCP(CONNECT);
}