//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// EXTRANET CONNECTION UTILITIES
//
// Author : L. Bonhomme
// date : 2009-10-06
// V1.0
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

$(document).ready(function(){  

	var str = "";
	
	if (navigator.appName.substring(0,9) !='Microsoft') str += "<br/>";

	str += "<form id='login_form' action='#' method='post' style='margin-top: 0px;border-left:3px solid #D3D3D3;padding-left:5px;'>";
	str += "<label id='loginMsg' style='display:block;width:100%;text-align:center;color:red;font-size:0.9em;'>&nbsp;</label>";
	str += "<label style='display:block;width:100%;text-align:center;margin-bottom: 6px;font-weight:bold;'>Accès Abonnés</label>";
	str += "<label for='userName' style='width:40%;float:left;text-align:left;font-size:0.9em;'>Identifiant</label>";
	str += "<input id='userName' type='text' name='userName' value='' style='width:58%;border:1px inset #CCC;'/>";
	str += "<label for='userPass' style='width:40%;float:left;clear:both;text-align:left;margin-top:3px;font-size:0.9em;'>Mot de passe</label>";
	str += "<input id='userPass' type='password' name='userPass' value='' style='width:58%;border:1px inset #CCC;margin-top: 3px;'/>";
	str += "<input type='button' name='button' value='Connexion' onClick='connectXnet()' style='display:block;margin:8px auto 5px auto;'/>";
	str += "</form>";
	$("#extralogin").html(str);	

	
	
	// Launch connect on submit
	if ($("#userName").length > 0) {
		$("#userName").keyup(function(e) {
			if (e.keyCode == 13) connectXnet();
		});
	}
	
	if ($("#userPass").length > 0) {
		$("#userPass").keyup(function(e) {
			if (e.keyCode == 13) connectXnet();
		});
	}		
});


//*************************************************
// Extranet connection via JQuery's Ajax call
// Nb : use JSONP for cross-domain compatibility
//*************************************************	 
function connectXnet()
{	
    var itemsList = {
            login: $("#userName").val(),
            password: $("#userPass").val(),
            requester: "corp",
    		requesterUri: window.location.href
    		};

    $.ajax({         
    	dataType:'jsonp',         
    	url: 'http://extranet.infolegale.fr/login/xlogin',         
    	data: itemsList,             
    	success:function(json) { 
    		if (json.success == true) { 
    			if (json.data.substring(0,7) == 'http://') {
    				window.location = json.data;
    			}
    		}
    		else {
    			$('#loginMsg').html('Paramètres incorrects');
    		}
    	}
    	}); 
}




