var $j = jQuery.noConflict();
var id = 0;
var premier = "";
var nom ="";
var time = 0;

var LPays = {"1":"fr","2":"de","3":"lu", "4":"be","5":"ch"};
var LIPays = {"FR":"1","DE":"2","LU":"3", "BE":"4","CH":"5"}
/*
$j().ready(function(){
	$j('#RVille_Depart').change(function(){
		var chemin = "/communes/findcommunes/";
		$j.ajax({
			url:chemin,
			data:{c:$j('#RVille_Depart').val()},
			success:function(data){
				if(data != "KO"){
					$j('#RVille_Depart').val(data);
				}
			}
		});
	});
	$j('#RVille_Arrivee').change(function(){
		var chemin = "/communes/findcommunes/";
		$j.ajax({
			url:chemin,
			data:{c:$j('#RVille_Arrivee').val()},
			success:function(data){
				if(data != "KO"){
					$j('#RVille_Arrivee').val(data);
				}
			}
		});
	});
});
*/
function autoVille(mode, dom,valeur){
	 
    var query = dom.value;
    var id = dom.id
    var nom = '#' + id;
	if(!valeur) valeur=0;
	var chemin = "/communes/autocompletioncommunesrech/" + query;
    $j(nom).attr('autocomplete', 'off');
    if (mode == 'on') {
		$j(nom).flushCache();
        $j(nom).autocomplete(chemin, {
            inputClass: 'ville',
			extraParams:{pays:valeur},
            selectedFirst: true,
            scrollHeight: 193,
            scroll: true,
			max:10,
			delay:0,
			cacheLength : 1,
            formatItem: function(data, i, n, value){
				time=0;
                return value.split(":")[1]+ " - " +value.split(":")[2];
            },
            formatResult: function(data, value, max){
				if (time == 0) {
					premier = value;
					time = 1;
				}
                var retour = value.split(":")[1] ;
                return retour;
            }
        });
		$j(nom).result(function(event, data, formatted){
			remplir(formatted,id,nom);
        });
		$j(nom).blur(function() {
			if (premier != "") {
				remplir(premier,id,nom);
				premier = "";
			}
		});
		
    }
    else 
        if (mode == 'off') {
            $j(nom).flushCache();
            $j(nom).unautocomplete();
            $j(nom).unbind('.autocomplete');
            $j(nom).removeClass('ville');
        }
}

function remplir(formatted, id,nom){
	var identifiant = formatted.split(':')[0];
	if (identifiant != 0) {
		var complete = formatted.split(":")[1].substr(0,formatted.split(":")[1].length-1);
		var paren = complete.indexOf(' (');
		if (paren != -1) {
			var ville = complete.substr(0, paren);
			var paren2 = complete.indexOf(')');
			var tabcp = complete.split('(');
			var cp = tabcp[1].split(')')[0];
		}
		else {
			var ville = formatted.split(":")[1].substr(0,formatted.split(":")[1].length);
			var cp = '';
		}
		if (id == "RVille_Depart") {
			$j("#CVille_Depart").val(formatted.split(":")[0]);
			$j('#' + id).val(complete);
		}
		if (id == "RVille_Arrivee") {
			$j("#CVille_Arrivee").val(formatted.split(":")[0]);
			$j('#' + id).val(complete);
		}
		if (id == "PVille_Depart") {
			$j("#CPVille_Depart").val(formatted.split(":")[0]);
			$j('#' + id).val(complete);
		}
		if (id == "PVille_Arrivee") {
			$j("#CPVille_Arrivee").val(formatted.split(":")[0]);
			$j('#' + id).val(complete);
		}
		if (id == "Ville_Depart") {
			$j(nom).val(ville);
			$j('#CP_Depart').val(cp);
			$j('#ListePays_Ville_Depart option[value="' + LIPays[formatted.split(":")[2]] + '"]').attr('selected','selected');
		//$j("#villeD").val(formatted.split(":")[0]);
		}
		if (id == "Ville_Arrivee") {
			$j(nom).val(ville);
			$j('#CP_Arrivee').val(cp);
			$j('#ListePays_Ville_Arrivee option[value="' + LIPays[formatted.split(":")[2]] + '"]').attr('selected','selected');
		// $j("#villeA").val(formatted.split(":")[0]);
		}
		if (id == "Ville_Depart_Aire") {
			$j(nom).val(ville);
			$j('#CP_Depart_Aire').val(cp);
			$j('#ListePays_Ville_Depart option[value="' + LIPays[formatted.split(":")[2]] + '"]').attr('selected','selected');
		}
		if (id == "Ville_Arrivee_Aire") {
			$j(nom).val(ville);
			$j('#CP_Arrivee_Aire').val(cp);
			$j('#ListePays_Ville_Arrivee option[value="' + LIPays[formatted.split(":")[2]] + '"]').attr('selected','selected');
		}
		if (id == "Ville_Etape") {
			$j(nom).val(ville);
			$j('#CP_Etape').val(cp);
			$j('#ListePays_Ville_Etape option[value="' + LIPays[formatted.split(":")[2]] + '"]').attr('selected','selected');
		}
		if (id == "Ville_Point") {
			$j(nom).val(ville);
			$j('#CP_Point').val(cp);
			$j('#ListePays_Ville_Point option[value="' + LIPays[formatted.split(":")[2]] + '"]').attr('selected','selected');
		}
		if (id == "RVille") {
			$j(nom).val(ville);
			$j('#RCP').val(cp);
			$j('#hVille').val(formatted.split(':')[0]);
		}
		if (id == "Ville") {
			$j(nom).val(ville);
			$j('#CP').val(cp);
			$j('#ListePays_Inscription option[value="' + LIPays[formatted.split(" - ")[2]] + '"]').attr('selected','selected');
		}
			time = 0;
			premier = "";
	 	$j(nom).flushCache();
	}else{
		alert("Merci de vérifier votre saisie.");
		$j(nom).val('');
		$j('#'+id).focus();
	}
}

var $j = jQuery.noConflict();
var str = "La saisie doit &ecirc;tre compris entre {0} et {1} caract&egrave;res.";
var msg_required = "Ce champ est obligatoire.";
$j().ready(function(){
    $j("#formLogin").validate({
        rules: {
            identifiant: {
                required: true,
                minlength: 6,
                maxlength: 26
            },
            motdepass: {
                required: true,
                minlength: 6,
                maxlength: 26
            }
        },
        messages: {
            identifiant: {
                required: msg_required,
                minlength: jQuery.validator.format(str, "6", "26"),
                maxlength: jQuery.validator.format(str, "3", "26")
            },
            motdepass: {
                required: msg_required,
                minlength: jQuery.validator.format(str, "6", "26"),
                maxlength: jQuery.validator.format(str, "3", "26")
            }
        }
    });
	
});
function validationLogin(){
    if ($j('#formLogin').valid()) {
        $j('#okLogin').hide();
        $j("#contentLoading").show();
        $j('#formLogin').submit();
    }
}

function inscription(){
	$j('#liens_rapides').append($j("<div id='popup'></div>"));
	$j('#popup').load("/inscrit/inscription");
	$j('#popup').dialog({
        title: 'Inscription',
        bgiframe: true,
        modal: true,
//        height: 300,
        width: 450,
        closeOnEscape: false,
        draggable: false,
        resizable: false,
		buttons:{
			"OK" : function(){
				validationInscription();
			}
		},
        close: function(){
            $j('#popup').html('');
            $j(this).dialog('destroy');
        }
    });
	// $j('#popup').dialog('moveToTop');
}

function popupBienvenue(){
	
//	var regPays = /covivo\.eu/;
//	if (regPays.test(location.href)) {
//		$j('#liens_rapides').append($j("<div id='popup'><p>Votre site est en maintenance pour mieux vous servir.<br/>Nous sommes désolés de la gêne occasionnée.</p></div>"));
//		$j('#popup').dialog({
//			title: "Maintenance du site",
//			bgiframe: true,
//			modal: true,
//			//        height: 300,
//			width: 450,
//			closeOnEscape: false,
//			draggable: false,
//			resizable: false,
//			buttons: {
//				"OK": function(){
//					$j('#popup').html('');
//					$j('#popup').remove();
//					$j(this).dialog('close');
//				}
//			},
//			close: function(){
//				$j('#popup').html('');
//				$j('#popup').remove();
//				$j(this).dialog('destroy');
//			}
//		});
//	}
//	else {
//		$j('#liens_rapides').append($j("<div id='popup'><p>Inscrivez-vous et recevez votre équipement GPS de covoiturage.</p><img src='/images/Covibox_small.png' alt='Covibox, équipement GPS de covoiturage width='220' height='165' style='margin-top:5px;margin-left:105px; margin-right:105px;' /></div>"));
//		
//		$j('#popup').dialog({
//			title: "Inscrivez-vous !!",
//			bgiframe: true,
//			modal: true,
//			//        height: 300,
//			width: 450,
//			closeOnEscape: false,
//			draggable: false,
//			resizable: false,
//			buttons: {
//				"OK": function(){
//					$j('#popup').html('');
//					$j('#popup').remove();
//					$j(this).dialog('close');
//				}
//			},
//			close: function(){
//				$j('#popup').html('');
//				$j('#popup').remove();
//				$j(this).dialog('destroy');
//			}
//		});
//	}
}

function popup(url,titre,urlredirect){
		$j('#liens_rapides').append($j("<div id='popup'></div>"));
	$j('#popup').load(url);
	$j('#popup').dialog({
        title: titre,
        bgiframe: true,
        modal: true,
//        height: 300,
        width: 450,
        closeOnEscape: false,
        draggable: false,
        resizable: false,
		buttons:{
			"OK" : function(){
				$j(this).dialog('close');
				if(urlredirect!= null)
					location = urlredirect;
			}
		},
        close: function(){
            $j('#popup').html('');
            $j(this).dialog('destroy');
        }
    });
}

function majExp(){
	$j().ready(function(){
		$j('#droite').css('height',$j('#contenu').height());
		$j('#droite').css('background','url(/images/nouvellecharte/image_fond_gauche_interne.png) left bottom no-repeat');
	});
}

function majCG54(){
	$j().ready(function(){
		$j('#droite').css('height',$j('#contenu').height());
		$j('#droite').css('background','url(/images/partenaires/cg54/image_fond_gauche_interne.png) left bottom no-repeat');
	});
}


function refond(taille){
	$j('#contenu').css('height',taille);
	var RegDyn = /dynamique/gi;
	if(RegDyn.test(location.href)){
		majExp();
	}else{
		majCG54();
	}
}

