Ptg = {
	user: {
		address: '',
		number: '',
		box: '',
		zip: '',
		city: ''
	}
};

Module = {};

var nbCredits = 0;

function testCookies() {
	document.cookie = 'CookieTest=Enabled';

	var allcookies = document.cookie;
	var pos = allcookies.indexOf('CookieTest=');
	if (pos != -1) {
		var start = pos + 11;
		var end = allcookies.indexOf(';', start);
		if (end == -1) end = allcookies.length;
		var value = allcookies.substring(start,end);
		value = unescape(value);

		return (value == 'Enabled');
	}
	return false;
}

String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

Module.showFormWindow = function (id, step) {
	(new Ajax.Request('/common/inc/getCoregsAutoForm.php', {
		method: 'post',
		parameters: {id: id, step: step},
		asynchronous: false,
		onSuccess: function (transport) {
			var response = transport.responseText.evalJSON();
			if (response.success) {
				if (!response.complete) {
					Dialog.confirm(response.form, {
						className: 'alphacube',
						width: 400,
						height: null,
						okLabel: 'OK',
						cancelLabel: 'Cancel',
						onOk: function () {
							//check if all the fields have a value
							var valid = true;
							var passwords = [];
							for (i=0; i<document.module_coregs_auto_modal_form.elements.length; i++) {
								var elm = document.module_coregs_auto_modal_form.elements[i];
								if ((elm.type != 'hidden') && (elm.value.trim() == '')) {
									elm.style.border = '#ff0000 1px solid';
									valid = false;
								} else {
									if (elm.type == 'password') {
										passwords.push(elm);
									} else {
										elm.style.border = '#7f9db9 1px solid';
									}
								}
							}

							if (passwords.length == 2) {
								if (passwords[0].value == passwords[1].value) {
									passwords[0].style.border = '#7f9db9 1px solid';
									passwords[1].style.border = '#7f9db9 1px solid';
								} else {
									passwords[0].style.border = '#ff0000 1px solid';
									passwords[1].style.border = '#ff0000 1px solid';
									valid = false;
								}
							}

							if (valid) {
								$('module_ca' + id + '_toDo').style.display = 'none';
								$('module_ca' + id + '_underProccess').style.display = 'block';

								//save data in our database
								(new Ajax.Request('/common/inc/saveFormData.php', {
									method: 'post',
									parameters: $('module_coregs_auto_modal_form').serialize(true),
									asynchronous: false
								}));

								//post data to the partner
								$('module_coregs_auto_modal_form').submit();

								setTimeout('Module.CoregsAutoProccessStep(' + id + ',' + (step + 1) + ')', 2000);
								Dialog.closeInfo();
							}
						}
					});
				} else {
					//insert the form in the document and post it to the partner
					$('placeholder').innerHTML = response.form;
					$('module_coregs_auto_modal_form').submit();
					$('placeholder').innerHTML = '';

					setTimeout('Module.CoregsAutoProccessStep(' + id + ',' + (step + 1) + ')', 2000);
				}
			}
		},
		onFailure: function () {
		}
	}));
}

Module.saveData = function (type, data, elm1) {
	Module.formCoreg_186_fr = '<form id="formCoreg_186" name="formCoreg_186">'
							+ '<table style="margin: auto; border-collapse:separate" border="0" cellpadding="2" cellspacing="0">'
							+ '<tr><td colspan="2" align="left">Quelle est votre place/fonction dans votre ménage ?</td></tr>'
							+ '<tr><td colspan="2" align="left"><select name="field1">'
							+ '<option value=""></option>'
							+ '<option value="1">Chef de famille</option>'
							+ '<option value="2">Partenaire du chef de famille</option>'
							+ '<option value="3">Enfant</option>'
							+ '<option value="4">Autre personne de la famille</option>'
							+ '<option value="5">Autre personne (pas membre de la famille)</option>'
							+ '</select></td></tr>'
							+ '</table>'
							+ '</form>';

	Module.formCoreg_186_nl = '<form id="formCoreg_186" name="formCoreg_186">'
							+ '<table style="margin: auto; border-collapse:separate" border="0" cellpadding="2" cellspacing="0">'
							+ '<tr><td colspan="2" align="left">Wat is uw plaats/functie in uw huishouden ?</td></tr>'
							+ '<tr><td colspan="2" align="left"><select name="field1">'
							+ '<option value=""></option>'
							+ '<option value="1">Hoofdkostwinne(ste)r</option>'
							+ '<option value="2">Partner van de hoofdkostwinne(ste)r</option>'
							+ '<option value="3">Kind</option>'
							+ '<option value="4">Ander inwonend familielid</option>'
							+ '<option value="5">Ander gewonend persoon</option>'
							+ '</select></td></tr>'
							+ '</table>'
							+ '</form>';

	Module.formCoreg_276_fr = '<form id="formCoreg_276" name="formCoreg_276">'
							+ '<table style="margin: auto; border-collapse:separate" border="0" cellpadding="2" cellspacing="0">'
							+ '<tr><td colspan="2" align="left">Veuillez choisir votre mot de passe (6 char. minimum) pour votre inscription à Photobox</td></tr>'
							+ '<tr><td colspan="2" align="left"><input type="password" name="field1" minlength="6" /></td></tr>'
							+ '</table>'
							+ '</form>';

	Module.formCoreg_276_nl = '<form id="formCoreg_276" name="formCoreg_276">'
							+ '<table style="margin: auto; border-collapse:separate" border="0" cellpadding="2" cellspacing="0">'
							+ '<tr><td colspan="2" align="left">Gelieve uw paswoord in te geven (minimum 6 tekens) voor uw inschrijving bij Photobox</td></tr>'
							+ '<tr><td colspan="2" align="left"><input type="password" name="field1" minlength="6" /></td></tr>'
							+ '</table>'
							+ '</form>';

	Module.formCoreg_286_fr = '<form id="formCoreg_286" name="formCoreg_286">'
							+ '<table style="margin: auto; border-collapse:separate" border="0" cellpadding="2" cellspacing="0">'
							+ '<tr><td colspan="2" align="left">Veuillez remplir votre adresse</td></tr>'
							+ '<tr><td align="left">Adresse:</td><td align="left"><input type="text" name="field1" value="' + (Ptg.user.address ? Ptg.user.address : '') + '" /></td></tr>'
							+ '<tr><td align="left">Numéro:</td><td align="left"><input type="text" name="field2" value="' + (Ptg.user.number ? Ptg.user.number : '') + '" /></td></tr>'
							+ '<tr><td align="left">Boîte:</td><td align="left"><input type="text" name="field3" value="' + (Ptg.user.box ? Ptg.user.box : '') + '" optional="optional" /></td></tr>'
							+ '<tr><td align="left">Code postal:</td><td align="left"><input type="text" name="field4" value="' + (Ptg.user.zip ? Ptg.user.zip : '') + '" /></td></tr>'
							+ '<tr><td align="left">Ville:</td><td align="left"><input type="text" name="field5" value="' + (Ptg.user.city ? Ptg.user.city : '') + '" /></td></tr>'
							+ '</table>'
							+ '</form>';

	Module.formCoreg_286_nl = '<form id="formCoreg_286" name="formCoreg_286">'
							+ '<table style="margin: auto; border-collapse:separate" border="0" cellpadding="2" cellspacing="0">'
							+ '<tr><td colspan="2" align="left">Gelieve het adres in te vullen</td></tr>'
							+ '<tr><td align="left">Adres:</td><td align="left"><input type="text" name="field1" value="' + (Ptg.user.address ? Ptg.user.address : '') + '" /></td></tr>'
							+ '<tr><td align="left">Nummer:</td><td align="left"><input type="text" name="field2" value="' + (Ptg.user.number ? Ptg.user.number : '') + '" /></td></tr>'
							+ '<tr><td align="left">Bus:</td><td align="left"><input type="text" name="field3" value="' + (Ptg.user.box ? Ptg.user.box : '') + '" optional="optional" /></td></tr>'
							+ '<tr><td align="left">Postcode:</td><td align="left"><input type="text" name="field4" value="' + (Ptg.user.zip ? Ptg.user.zip : '') + '" /></td></tr>'
							+ '<tr><td align="left">Woonplaats:</td><td align="left"><input type="text" name="field5" value="' + (Ptg.user.city ? Ptg.user.city : '') + '" /></td></tr>'
							+ '</table>'
							+ '</form>';

	Module.formCoreg_290_fr = '<form id="formCoreg_290" name="formCoreg_290">'
							+ '<table style="margin: auto; border-collapse:separate" border="0" cellpadding="2" cellspacing="0">'
							+ '<tr><td colspan="2" align="left">Cochez vos bulletins d’informations préférés</td></tr>'
							+ '<tr><td colspan="2" align="left"><input type="checkbox" name="field1" optional="optional" />Ecrans Plats, Home cinema, DVD-Blu Ray</td></tr>'
							+ '<tr><td colspan="2" align="left"><input type="checkbox" name="field2" optional="optional" />Appareils Photo et caméscopes</td></tr>'
							+ '<tr><td colspan="2" align="left"><input type="checkbox" name="field3" optional="optional" />Electroménager</td></tr>'
							+ '</table>'
							+ '</form>';

	Module.formCoreg_290_nl = '<form id="formCoreg_290" name="formCoreg_290">'
							+ '<table style="margin: auto; border-collapse:separate" border="0" cellpadding="2" cellspacing="0">'
							+ '<tr><td colspan="2" align="left">Gelieve de gewenste nieuwsbrieven aan te vinken</td></tr>'
							+ '<tr><td colspan="2" align="left"><input type="checkbox" name="field1" optional="optional" />Flatscreens, Homecinema, DVD-Blu Ray</td></tr>'
							+ '<tr><td colspan="2" align="left"><input type="checkbox" name="field2" optional="optional" />Fototoestellen en camcorders</td></tr>'
							+ '<tr><td colspan="2" align="left"><input type="checkbox" name="field3" optional="optional" />Huishoudtoestellen</td></tr>'
							+ '</table>'
							+ '</form>';

	Module.formCoreg_292_nl = '<form id="formCoreg_292" name="formCoreg_292">'
							+ '<table style="margin: auto; border-collapse:separate" border="0" cellpadding="2" cellspacing="0">'
							+ '<tr><td colspan="2" align="left">Gelieve het adres in te vullen</td></tr>'
							+ '<tr><td align="left">Adres:</td><td align="left"><input type="text" name="field1" value="' + (Ptg.user.address ? Ptg.user.address : '') + '" /></td></tr>'
							+ '<tr><td align="left">Nummer:</td><td align="left"><input type="text" name="field2" value="' + (Ptg.user.number ? Ptg.user.number : '') + '" /></td></tr>'
							+ '<tr><td align="left">Bus:</td><td align="left"><input type="text" name="field3" value="' + (Ptg.user.box ? Ptg.user.box : '') + '" optional="optional" /></td></tr>'
							+ '<tr><td align="left">Postcode:</td><td align="left"><input type="text" name="field4" value="' + (Ptg.user.zip ? Ptg.user.zip : '') + '" /></td></tr>'
							+ '<tr><td align="left">Woonplaats:</td><td align="left"><input type="text" name="field5" value="' + (Ptg.user.city ? Ptg.user.city : '') + '" /></td></tr>'
							+ '</table>'
							+ '</form>';

	if ((type == 'Coregs') && !data.extra_data) {
		if ((data.id == 186) || (data.id == 276) || (data.id == 286) || (data.id == 290) || (data.id == 292)) {
			//coreg GFK
			Dialog.confirm(eval('Module.formCoreg_' + data.id + '_' + LANGUAGE), {
				className: 'alphacube',
				width: 400,
				height: null,
				okLabel: 'OK',
				cancelLabel: 'Cancel',
				destroyOnClose: true,
				onCancel: function () {
					document['formCoreg_' + data.id].parentNode.removeChild(document['formCoreg_' + data.id]);
				},
				onOk: function () {
					//check if all the fields have a value
					var valid = true;
					data.extra_data = {};
					for (i=0; i<document['formCoreg_' + data.id].elements.length; i++) {
						var elm = $(document['formCoreg_' + data.id].elements[i]);
						if ((!elm.attributes.getNamedItem('optional') && (!elm.getValue() || (elm.getValue().trim() == ''))) || (elm.attributes.getNamedItem('minlength') && (elm.getValue().trim().length < elm.attributes.getNamedItem('minlength').value))) {
							elm.style.border = '#ff0000 1px solid';
							valid = false;
						} else {
							elm.style.border = '#7f9db9 1px solid';
							eval('data.extra_data.' + elm.name + ' = \'' + elm.getValue() + '\'');
						}
					}

					if (valid) {
						document['formCoreg_' + data.id].parentNode.removeChild(document['formCoreg_' + data.id]);
						Dialog.closeInfo();
						Module.saveData('Coregs', data, elm1);
					}
				}
			});
			return;
		}
	}

	(new Ajax.Request('/common/inc/saveData.php', {
		method: 'post',
		parameters: {type: type, data: Object.toJSON(data)},
		asynchronous: false,
		onSuccess: function (transport) {
			if (elm1) {
				elm1.disabled = 'disabled';
				if (type == 'Coregs') {
					if (typeof(_gaq) != "undefined") _gaq.push(['_trackPageview', '/' + COUNTRY + LANGUAGE + '/tracking/coreg']);
				}
			}
			var response = transport.responseText.evalJSON();

			eval('Module.' + type + '(response.data)');
		},
		onFailure: function () {
		}
	}));
};

Module.SetDobMask2 = function () {
	new MaskedInput('#coreg_dob', '3#/1#/29##');
}

Module.SaveDataCoregs = function (id, elm, tracking) {
	(new Ajax.Request('/common/inc/getCoregsForm.php', {
		method: 'post',
		asynchronous: false,
		onSuccess: function (transport) {
			var response = transport.responseText.evalJSON();
			if (response.success) {
				if (!response.complete) {
					Module.SetDobMask2.defer();
					Dialog.confirm(response.form, {
						className: 'alphacube',
						width: 400,
						height: null,
						okLabel: 'OK',
						cancelLabel: 'Cancel',
						onOk: function () {
							//check if all the fields have a value
							var valid = true;
							var data = {id: id};
							for (i=0; i<document.module_coregs_modal_form.elements.length; i++) {
								var elm = document.module_coregs_modal_form.elements[i];
								if (elm.value.trim() == '') {
									elm.style.border = '#ff0000 1px solid';
									valid = false;
								} else {
									elm.style.border = '#7f9db9 1px solid';
									eval('data.' + elm.name + ' = \'' + elm.value + '\'');
								}
							}

							if (valid) {
								Module.saveData('Coregs', data);
								elm.disabled = 'disabled';
								if (typeof(_gaq) != "undefined") _gaq.push(['_trackPageview', tracking]);
								Dialog.closeInfo();
							}
						}
					});
				} else {
					Module.saveData('Coregs', {id: id});
					elm.disabled = 'disabled';
					if (typeof(_gaq) != "undefined") _gaq.push(['_trackPageview', tracking]);
				}
			}
		},
		onFailure: function () {
		}
	}));
};

Module.wonCredits = function (id, credits) {
	$('module_'+id+'_toWin').style.display='none';
	$('module_'+id+'_isWin').style.display='';
	$('module_'+id+'_toDo').style.display='none';
	$('module_'+id+'_isDone').style.display='';
	
	nbCredits = nbCredits + credits;
	$('module_nb_credits').innerHTML = nbCredits;
	if (nbCredits>1) {
		$('module_credit').style.display='none';
		$('module_credits').style.display='';
	} else {
		$('module_credit').style.display='';
		$('module_credits').style.display='none';
	}

	if (parent.end_action) parent.end_action();
}

Module.checkEmail = function (email) {
	return email.match(/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|name|aero|biz|info|mobi|jobs|museum)\b$/im);
};

Module.numbersOnly = function (myfield, e, dec) {
	var key;
	var keychar;

	if (window.event) {
		key = window.event.keyCode;
	} else if (e) {
		key = e.which;
	} else {
		return true;
	}
	keychar = String.fromCharCode(key);

	// control keys
	if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) {
		return true;
	}
	// numbers
	if (("0123456789").indexOf(keychar) > -1) {
		return true;
	}

	return false;
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel))) n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function displayPopup(urlPopup, widthPopup, heightPopup, top) {
	if (widthPopup==0) widthPopup=f_clientWidth()-100;
	if (heightPopup==0) heightPopup=f_clientHeight()-100;
	if (typeof(top) == "undefined") top = 50;

	Dialog.info('', {
		showEffect: Element.show,
		hideEffect: Element.hide,
		className: 'alphacube',
		width: widthPopup,
		url: urlPopup,
		height: heightPopup,
		zIndex: 999,
		closable: true,
		top: top,
		onClose: function () {
			if (parent.end_action) parent.end_action();
		}
	});
}
function displayPopupModules(urlPopup, widthPopup, heightPopup) {
	if (widthPopup==0) widthPopup=f_clientWidth()-100;
	if (heightPopup==0) heightPopup=f_clientHeight()-100;

	Dialog.info('', {
		showEffect: Element.show,
		hideEffect: Element.hide,
		className: 'alphacube',
		width: widthPopup,
		url: urlPopup,
		height: heightPopup,
		zIndex: 999,
		closable: true,
		top: 50,
		onClose: function () {
			if (refreshPoints) {
				refreshPoints();
			}
		}
	});
}

if (typeof(Class) != 'undefined') {
	var InputTooltip = Class.create({
		initialize: function (id, tooltipClass, position, offset, text) {
			this.elm = $(id);

			if (position == 'right') {
				this.tooltip = new Element('span', {
					'class': tooltipClass,
					style: 'display: none; left: ' + (this.elm.offsetLeft + this.elm.offsetWidth + offset) + 'px;'
				});
			} else {
				this.tooltip = new Element('span', {
					'class': tooltipClass,
					style: 'display: none; right: ' + (this.elm.offsetWidth + offset) + 'px;'
				});
			}

			this.tooltip.update(text);
			this.elm.insert({after: this.tooltip});

			var self = this; 
			this.elm.observe('focus', function () { self.tooltip.show(); });
			this.elm.observe('blur', function () { self.tooltip.hide(); });
		}
	});

	var GiftsHorizontalSlideShow = Class.create({
		initialize: function (options) {
			this.options = options;
			this.slideShow = $(this.options.id);
			this.giftsCount = $$('#' + this.options.id + ' li').length;
			this.step();
		},

		step: function () {
			var gifts = $$('#' + this.options.id + ' li');
			if (gifts.length > 4) {
				var elm = Element.remove(gifts[0]);
				gifts[this.giftsCount - 1].insert({after: elm});
				this.slideShow.style.left = 0;

				var self = this;
				new Effect.Move(
					this.slideShow,
				{
					x: this.options.offset,
					afterFinish: function () {
						self.step();
					},
					duration: 5,
					transition: Effect.Transitions.linear
				});
			}
		}
	});

	var GiftsSlideShow = Class.create({
		initialize: function (options) {
			this.options = options;
			this.giftsCount = this.options.images.length;
			this.currentGiftIdx = 0;

			if (this.giftsCount > 1) {
				this.preloadNextGift();

				var self = this;
				setTimeout(function () { self.step(); }, this.options.duration);
			}
		},

		getNextIdx: function () {
			var nextIdx = this.currentGiftIdx + 1;
			if (nextIdx == this.giftsCount) nextIdx = 0;
			return nextIdx;
		},

		preloadNextGift: function () {
			var nextIdx = this.getNextIdx()
			if ($(this.options.idImagePrefix + nextIdx)) return;

			var o = {
				src: this.options.images[nextIdx],
				id: this.options.idImagePrefix + nextIdx,
				alt: ''
			};
			Object.extend(o, this.options.imageAttributes);

			var img = new Element('img', o);
			img.hide();
			if (this.options.idTextPrefix) {
				var txt = new Element(this.options.textElement || 'div', {
					id: this.options.idTextPrefix + nextIdx,
					'class': this.options.textClass,
					style: 'display: none'
				});
				txt.update(this.options.texts[nextIdx]);

				$(this.options.idTextPrefix + this.currentGiftIdx).insert({after: txt});
			}
			$(this.options.idImagePrefix + this.currentGiftIdx).insert({after: img});
		},

		step: function () {
			var nextIdx = this.getNextIdx()

			$(this.options.idImagePrefix + nextIdx).appear({ duration: this.options.fadeDuration });
			$(this.options.idImagePrefix + this.currentGiftIdx).fade({ duration: this.options.fadeDuration });
			if (this.options.idTextPrefix) {
				$(this.options.idTextPrefix + nextIdx).appear({ duration: this.options.fadeDuration });
				$(this.options.idTextPrefix + this.currentGiftIdx).fade({ duration: this.options.fadeDuration });
			}

			this.currentGiftIdx = nextIdx;

			this.preloadNextGift();

			var self = this;
			setTimeout(function () { self.step(); }, this.options.duration);
		}
	});
}