﻿var Site = function(){};

Site.checkCP = function() {
	if ($('#code_postal').length != 0) {
		$.form.addField('code_postal', 'cp', {
			required: true,
			type: 'int',
			minLength: '2',
			maxLength: '5',
			tipValue: 'Code postal / Département',
			errorMessage: 'Veuillez saisir un code postal ou un département valide.'
		});
	
		$('#code_postal').submit(function(){
			if (!$.form.checkForm('code_postal')) {
				return(false);
			}
			else {
				
				$.getJSON("./?/prospectus/getMagasins/?cp="+$('#code_postal input[@name=cp]').val(), function(json){
					if (json == false) {
						alert('Erreur de connection a la base.');
					}
					else {
						$('#magasins select[@name=magasins]').empty();
						$('#magasins select[@name=magasins]').append(json);
						$('#magasins select[@name=magasins]').show();
					}
				});
				
				return(false);
			}
		});
	}
};

Site.checkMagasin = function() {
	if ($('#magasins').length != 0) {
		
		$('#magasins select[@name=magasins]').change(function(){
			$('#magasins').submit();
		});
		
		$.form.addField('magasins', 'magasins', {
			required: true,
			type: 'string',
			errorMessage: 'Veuillez choisir un magasin.'
		});
		
		$('#magasins').submit(function(){
			if (!$.form.checkForm('magasins')) {
				return(false);
			}
		});
	}
};

Site.checkNewsletter = function() {
	if ($('#newsletter').length != 0) {
		
		$.form.addField('newsletter', 'newsletter', {
			required: true,
			type: 'string',
			tipValue: 'E-mail',
			errorMessage: 'Veuillez renseigner votre adresse e-mail.'
		});
		
		$.form.addField('newsletter', 'code_postal', {
			required: true,
			type: 'int',
			minLength : 5,
			maxLength: 5,
			tipValue: 'Code postal',
			errorMessage: 'Veuillez renseigner votre code postal complet.'
		});
		
		$('#newsletter').submit(function(){
			if (!$.form.checkForm('newsletter')) {
				return(false);
			}
			else {
				$.getJSON("./?/user/setInscription/?email="+$('#newsletter input[@name=newsletter]').val()+"&code_postal="+$('#newsletter input[@name=code_postal]').val(), function(json){
					if (json == false) {
						alert('Erreur de connection a la base.');
					}
					else {
						alert(json);
					}
				});
				
				return(false);
			}
		});
	}
};

Site.warning = function () {
	if ($('#warning').length != 0) {
		$('#left a.banner').click( function() {
			var attr = $(this).attr('id');
			$.getJSON("./?/prospectus/getLinkProspectusById/?id="+attr, function(json){
				if (json == false || json == "ERROR") {
					if ($('#carte_flash').length != 0) {
						$('#warning #carte_flash').remove();
						$('#warning').append('<div id="carte_flash">ERREUR : la carte ne peut être affiché.</div>');
					}
					else {
						$('#warning div.options a.link_mag').hide();
					}
				}
				else {
					if ($('#carte_flash').length != 0) {
						$('#warning #carte_flash').remove();
						$('#warning').append('<div id="carte_flash">ERREUR : la carte ne peut être affiché.</div>');
						
						var flashvars = {
								id_prospectus: json['id_prospectus']
								};

						swfobject.embedSWF("carte/carte.swf", "carte_flash", "765", "630", "10.0.0","javascript/swfobject/expressInstall.swf", flashvars);
					}
					else {
						$('#warning div.options a.link_mag').attr('href',json["url"]);
						$('#warning div.options a.link_mag').click( function() {
							$.window.popup(this.href, {name:json['titre'], width:json['catalogue_width'], height:json['catalogue_height']});
							return(false);
						})
					}
				}
			});
			
			$('#warning').show();
		});
		
		$('#warning div.close a').click(function() { Site.closeWarning(); });
	}
}

Site.closeWarning = function () {
	$('#warning').hide();
}
