$(document).ready(function () {
	//addOptionPopin("Ville", "Ville", "citySelect");
});

function onZipCodeChangePopin(zipCode) {			
	var zipCodePrecedent = document.getElementById("zipOld");					
	zipCodePrecedent.value= zipCode;
}

function getCitiesByZipCodePopin(zipcode,countrySelected,storeId,catalogId,langId) {
	var zipCodePrecedent = document.getElementById("zipOld");
	if(zipcode != null && zipcode != '' && zipCodePrecedent.value != zipcode) {
		var exp = new RegExp("\\d{5}"); // 5 chiffres pour la france
		if(exp.test(zipcode)) {
			if(countrySelected == 'FR') {
				loadCitiesPopin(zipcode,storeId,catalogId,langId);
			} else {
			 	$("select#citySelect").empty();
			}
		} 
	} else if(zipcode == null) {
	  	return false;
	}
}

function loadCitiesPopin(zipcode,storeId,catalogId,langId) {
	$("select#citySelect").empty();
	$.ajax( { 
      	type: "POST", 
	    url: "/webapp/wcs/stores/servlet/ListCitiesPopinByZipCodeView", 
      	data: "zipCode=" + zipcode +"&storeId=" + storeId + "&catalogId=" + catalogId + "&langId=" + langId, 
      	success: 
          function(msg) { 
	          var content = msg.responseText;
			  var start = 0;
			  var end = content.indexOf('|', start);
			  var nbRead = 0;	         	
			  while(end != -1) {
			     var ligne = content.substring(start,end);
			     var mid = ligne.indexOf(',');
			     var codeINSEE = ligne.substring(0,mid);
			     var label = ligne.substring(mid+1,ligne.length);
			     addOptionPopin(codeINSEE, label, "citySelect");
			     start = end + 1;
			     end = content.indexOf('|', start);
			     ++nbRead;
			  }
			  $('#citySelect').removeAttr('disabled');
          }
  	});
}

function updateMaxLengthPopin(lang){
			limitMaxLengthPopin("#zipCode",5);
		}

function limitMaxLengthPopin(id,len){
	if($(id).val().length > len-1){
		var str = $(id).val().substr(0,len);
		$(id).val(str);
	}else{
		$("select#citySelect").empty();
		addOptionPopin("-", "", "citySelect");
		$('#citySelect').attr('disabled', 'disabled');
		$('#validOk').attr('disabled', 'disabled');
	}
}

function addOptionPopin(codeINSEE, label, idListBox) {
	$('#citySelect option[value=Ville]').remove();
	listBox = document.getElementById(idListBox);
	if(listBox != null){
		opt = document.createElement('option');
		optTxt = document.createTextNode(label);
		att = document.createAttribute("value");
		att.nodeValue = codeINSEE;
		opt.appendChild(optTxt);
		opt.setAttributeNode(att);
		listBox.appendChild(opt);
	}
}

function confirmMagasinSelectionPopin(){
	if($('#citySelect').val() != '-'){
	var uriCourant = document.location.pathname;
	if(uriCourant.indexOf('?') != -1){
		var tab = uriCourant.indexOf('?');
		if(tab.indexOf('/webapp/wcs/stores/servlet/') != -1){
			uriCourant = tab.replace('/webapp/wcs/stores/servlet/','');
		}
	}else{
		if(uriCourant.indexOf('/webapp/wcs/stores/servlet/') != -1){
			uriCourant = uriCourant.replace('/webapp/wcs/stores/servlet/','');
		}else{
			uriCourant= 'TopCategoriesDisplay';
		}
	}
	var result = "";
	$("#attachMag input[@type=text]").each(function() {
		result += $(this).attr("name")+"="+$(this).val()+"&";
	});
	$("#attachMag input[@type=hidden]").each(function() {
		result += $(this).attr("name")+"="+$(this).val()+"&";
	});
	$.ajax({
		type:"POST",
		url: "/webapp/wcs/stores/servlet/AttachMagasinByCityAndZipCodeCmd", 
		data: result,
		success:function(msg) {
			var content = msg.responseText;
			$("#magContainer").html(content);
			initFavoriteFfmcenterName();
			$("#currentPage").attr("value",uriCourant);
		}
	});
}
}

function getValueSelect(){
	$('#attachMag input[type=hidden]').attr({value : ''});
	var val = $("#citySelect option:selected" ).text();
	var valInsee = $("#citySelect option:selected" ).val();
	document.getElementById("codeInsee").value = valInsee;
	$('#validOk').attr('disabled', false);
}

function logonPopin(){
	var myForm = $("#Logon");
	var result = "";
	$("#Logon input[@type=text]").each(function() {
		result += $(this).attr("name")+"="+$(this).val()+"&";
	})
	$("#Logon input[@type=hidden]").each(function() {
		result += $(this).attr("name")+"="+$(this).val()+"&";
	})
	$.ajax({
		type:"POST",
		url: myForm.attr("action"),
		data: result,
		success:function(msg) {
			var content = msg.responseText;
			document.getElementById("blocUserInfo").innerHTML = content;
		}
	});
}
function logOffUser(langId,storeId,catalogId,URL){
	var uriCourant = document.location.pathname;
	if(uriCourant.indexOf('?') != -1){
		var tab = uriCourant.indexOf('?');
		if(tab.indexOf('/webapp/wcs/stores/servlet/') != -1){
			uriCourant = tab.replace('/webapp/wcs/stores/servlet/','');
		}
	}else{
		if(uriCourant.indexOf('/webapp/wcs/stores/servlet/') != -1){
			uriCourant = uriCourant.replace('/webapp/wcs/stores/servlet/','');
		}
	}
	$.ajax({
		type:"POST",
		url: "Logoff",
		data: "langId="+langId+"&storeId="+ storeId +"&catalogId="+catalogId+"&URL="+ URL,
		success:function(msg) {
			var content = msg.responseText;
			$("#currentPage").attr("value",uriCourant);
			document.getElementById("blocUserInfo").innerHTML = content;
		}
	});
}

function checkValuePopin(){
	var valSeelcted = $('#citySelect').val();
	if(valSeelcted == 'Ville'){
		$("#zipCode").attr("value","");
		$("select#citySelect").empty();
		addOptionPopin("-", "", "citySelect");
	}
}

