
SGKClavierVirtuel = function(context, divId, urlImage, handleValidate, handleSubmitOk, handleSubmitKo)
{
	this._context = context;
	this._handleValidate = handleValidate;
	this._handleSubmitOk = handleSubmitOk;
	this._handleSubmitKo = handleSubmitKo;
	this._divId = divId;
	this._urlImage =  urlImage;
}

SGKClavierVirtuel.prototype.setHandleValidate = function(handleValidate)
{
	this._handleValidate = handleValidate;
}

// Coordonnees saisies par l'utilisateur
var PassworCoordinates = '';
SGKClavierVirtuel.prototype.getPasswordCoordinates = function()
{
	return PassworCoordinates;
}

// Retour du serveur pour generation du clavier
var XmlResponse = '';
SGKClavierVirtuel.prototype.getXmlResponse = function()
{
	return XmlResponse;
}

SGKClavierVirtuel.prototype.openClavierVirtuel = function(coordx, coordy)
{
	// Recupere une image de clavier en asynchrone et affiche la dialog
	//var url = this._context+'/clavier.clv?method=getInformation';
	var url = this._urlImage;
	var transaction = YAHOO.util.Connect.asyncRequest('POST', url, this);
	this.displayCV(coordx, coordy);
}

SGKClavierVirtuel.prototype.closeClavierVirtuel = function()
{
	this.hide();
	this.clean();
	document.getElementById(this._divId).style.display = "hide";
}


SGKClavierVirtuel.prototype.success = function(o)
{
	try { 
		datajson = JSON.parse(o.responseText); 
	} 
	catch (x) { 
		return; 
	} 
	
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version < "8"){
		// remplace l'image
		var img = document.getElementById("imageClavier");
		img.src = "/loginbox-portlet/s2/common/imgClavier.action?imageB64=" + datajson.imageB64;
	} else {
		// remplace l'image
		var img = document.getElementById("imageClavier");
		img.src = "data:image/png;base64," + datajson.imageB64;
	}

	XmlResponse = unescape(datajson.cryptogramme);
	var array = XmlResponse.split(";");
	// cree la map de coordonnees
	var coordsPrefix = "<area shape='rect' coords='";
	var buffer = "";

	for (var i = 1 ; i < array.length; i++) {
		//attention a la coherence du nom "sgkClavier" par rapport a la JSP
		buffer += "<area shape='rect' coords="+array[i]+" href='javascript:sgkClavier.clicked("+array[i]+")' />\n" ;
	}

	var	map = document.getElementById("claviermap");
	map.innerHTML = buffer;
}

SGKClavierVirtuel.prototype.failure = function(o)
{
	alert("Erreur technique, merci de recommencer");
}

SGKClavierVirtuel.prototype.clicked = function(x, y, z, t)
{
	document.getElementById("password").value += "*";
	document.getElementById("passwordAffiche").value = document.getElementById("password").value;
	PassworCoordinates += x+";"+y+";";
}

SGKClavierVirtuel.prototype.correct = function()
{
	var password = document.getElementById("password");
	if(password.value.length > 0){
		password.value = password.value.substring(0, password.value.length-1);
	}
	document.getElementById("passwordAffiche").value = document.getElementById("password").value;
	if(PassworCoordinates.length > 0){
		var tab = PassworCoordinates.split(";");
		PassworCoordinates = "";
		for (i = 0; i < tab.length - 3; i++) {
		  PassworCoordinates += tab[i] + ";" + tab[i+1] + ";";
		  i++;
		}
	}
}


SGKClavierVirtuel.prototype.clean = function()
{
	document.getElementById("password").value = "";
	document.getElementById("passwordAffiche").value = document.getElementById("password").value;
	PassworCoordinates = '';
}

SGKClavierVirtuel.prototype.hide = function()
{
	SGK.clavier.widget.hide();
}

SGKClavierVirtuel.prototype.validate = function()
{
	this._handleValidate();
}


SGKClavierVirtuel.prototype.erase = function()
{
	document.getElementById("password").value = "";
	document.getElementById("passwordAffiche").value = document.getElementById("password").value;
	PassworCoordinates = '';
}

/**
 * Cree et affiche le clavier virtuel
 */
SGKClavierVirtuel.prototype.displayCV = function(coordx, coordy)
{
	PassworCoordinates = '';
	this.erase();

    SGK.namespace('clavier');

    //on ne recree le widget que s'il n'existe pas
    if(SGK.clavier.widget == null)
	{
		SGK.clavier.widget = new YAHOO.widget.Dialog(this._divId,
		{
			effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25},
			modal: true,
			visible: true,
			xy:[coordx,coordy],
			constraintoviewport: true,
			draggable: true,
            close: true,
            postmethod : "form"
        });

		SGK.clavier.widget.callback.success = this._handleSubmitOk;
		SGK.clavier.widget.callback.failure = this._handleSubmitKo;

		// Ajout des raccourcis clavier ECHAP et ENTER
        SGK.clavier.widget.cfg.queueProperty("keylisteners",
            [
                new YAHOO.util.KeyListener(document, { keys : 13 }, {fn:this._handleValidate, scope:SGK.clavier.widget,correctScope:true}),
                new YAHOO.util.KeyListener(document, { keys : 27 }, {fn:SGK.clavier.widget.hide, scope:SGK.clavier.widget,correctScope:true})
            ]);
        SGK.clavier.widget.cfg.fireQueue();

		var handleManual = function(type, args, obj)
		{
				alert("Manual submission of " + this.id + " detected");
		}

		SGK.clavier.widget.manualSubmitEvent.subscribe(handleManual, SGK.clavier.widget, true);
		SGK.clavier.widget.render(document.body);
	}

	SGK.clavier.widget.show();
	document.getElementById(this._divId).style.display ='block';
};


/**
 * CLass de detection du navigateur
 */
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

