$(document).ready(function(){

	$('.cATempForm').click(function(){
		sLanguage = $(this).parent().attr('id');
		sLanguage = sLanguage.replace('iDivMiddleContentBody', '');
		sLanguage = sLanguage.replace('iDivMiddleContent', '');

		showWelcomeForm(sLanguage);
	});

});

function overlayClose(){
	$('#iDivOverlayBack').remove();
	$('#iDivMiddleContainer').css('z-index', '1');
	$('#iDivBackgroundMenu').css('z-index', '1');
}

var bRequired = true;

function dataValid(){
	bRequired = true;

	if(!validateRequired()){
		alert('*required fields must to be filled.');
		return false;
	}

	if(!validateEmail()){
		alert('Email address is invalid.');
		return false;
	}else
		return true;
}

function validateRequired(){
	$('input[type="text"]').each(function(){
		switch($(this).attr('id')){
			case 'iTextCompany':
			case 'iTextContactPerson':
			case 'iTextEmail':
			case 'iTextTel':
			case 'iTextNumber':
			case 'iTextStreet':
			case 'iTextPostCode':
			case 'iTextCity':
				if(this.value == "*required"){
					bRequired = false;
					break;
				}else
					break;

			default:
				break;
		}
	});
	return bRequired;
}

function validateEmail(){
	sEnteredEmail = $('#iTextEmail').val();

	apos = sEnteredEmail.indexOf("@");
	dotpos = sEnteredEmail.lastIndexOf(".");

	if (apos < 1 || dotpos - apos < 2)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function showWelcomeForm(sLanguage){
	aWelcomeFormFields = {'nl' : {'coupon'	: 'Activeer uw DIVAKAART',
									'brochure'	: 'Ontvang onze nieuwe brochure'
									},		
							'ge' : {'coupon'	: 'Aktivieren Sie Ihre DIVAKARTE',
									'brochure'	: 'Empfangen Sie unsere neue Broschüre'
									}
						};
	sOverlay  = '';

	sOverlay += '<div id="iDivOverlayBack" >';
	sOverlay += '	<div id="iDivOverlayContainer" >';
	sOverlay += '		<div id="iDivOverlayContent" >';
	sOverlay += '			<a id="iAOverlayClose" title="Close" href="#" ><div></div></a>';
	sOverlay += '			<table id="iTableOverlayForm" style="margin:15px auto auto;">';
	sOverlay += '				<tbody>';
	sOverlay += '					<tr>';
	sOverlay += '						<td style="text-align:center;"><input style="float:none;" id="iInputCoupon" type="button" value="' + aWelcomeFormFields[(sLanguage == 'ge')?'ge':'nl'].coupon + '" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td style="text-align:center;"><input style="float:none;" id="iInputBrochure" type="button" value="' + aWelcomeFormFields[(sLanguage == 'ge')?'ge':'nl'].brochure + '" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '				</tbody>';
	sOverlay += '			</table>';
	sOverlay += '		</div>';
	sOverlay += '	</div>';
	sOverlay += '</div>';

	$('#iDivMiddleContainer').css('z-index', '-1');
	$('#iDivBackgroundMenu').css('z-index', '-1');
	$('#iDivBackground').after(sOverlay);

	$('#iAOverlayClose').click(function(){
		overlayClose();
	});

	$('#iTableOverlayForm #iInputCoupon').click(function(){
		overlayClose();
		showCouponForm(sLanguage);
	});
	
	$('#iTableOverlayForm #iInputBrochure').click(function(){
		overlayClose();
		showBrochureForm(sLanguage);
	});

	resize_bg();
}

function showCouponForm(sLanguage){
	aCouponFormFields = {'nl' : {'company'	: 'Bedrijfsnaam:',
									'contact'	: 'Contactpersoon:',
									'tel'		: 'Telefoonnummer:',
									'email'		: 'E-mailadres:',
									'number'	: 'DIVAnummer(zie kaart):',
									'send'		: 'Verstuur'
									},

							'ge' : {'company'	: 'Firmenname:',
									'contact'	: 'Kontaktperson:',
									'tel'		: 'Telefonnummer:',
									'email'		: 'Emailadresse:',
									'number'	: 'DIVA-Nummer (siehe Karte):',
									'send'		: 'SENDEN'
									}
							};
	sOverlay  = '';

	sOverlay += '<div id="iDivOverlayBack" >';
	sOverlay += '	<div id="iDivOverlayContainer" >';
	sOverlay += '		<div id="iDivOverlayContent" >';
	sOverlay += '			<a id="iAOverlayClose" title="Close" href="#" ><div></div></a>';
	sOverlay += '			<table id="iTableOverlayForm" >';
	sOverlay += '				<tbody>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextCompany" >' + aCouponFormFields[(sLanguage == 'ge')?'ge':'nl'].company + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextCompany" type="text" name="iTextCompany" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextContactPerson" >' + aCouponFormFields[(sLanguage == 'ge')?'ge':'nl'].contact + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextContactPerson" type="text" name="iTextContactPerson" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextTel" >' + aCouponFormFields[(sLanguage == 'ge')?'ge':'nl'].tel + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextTel" type="text" name="iTextTel" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextEmail" >' + aCouponFormFields[(sLanguage == 'ge')?'ge':'nl'].email + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextEmail" type="text" name="iTextEmail" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextNumber" >' + aCouponFormFields[(sLanguage == 'ge')?'ge':'nl'].number + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextNumber" type="text" name="iTextNumber" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td colspan="4" ><input type="button" value="' + aCouponFormFields[(sLanguage == 'ge')?'ge':'nl'].send + '" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '				</tbody>';
	sOverlay += '			</table>';
	sOverlay += '		</div>';
	sOverlay += '	</div>';
	sOverlay += '</div>';

	$('#iDivMiddleContainer').css('z-index', '-1');
	$('#iDivBackgroundMenu').css('z-index', '-1');
	$('#iDivBackground').after(sOverlay);

	$('#iAOverlayClose').click(function(){
		overlayClose();
	});

	$('#iTableOverlayForm input[type="text"]').focus(function () {
		if (this.value == this.defaultValue){
			this.value = '';
		}

		if(this.value != this.defaultValue){
			this.select();
		}

		$(this).css('color', '#000');
	});

	$('#iTableOverlayForm input[type="text"]').blur(function (){
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
			$(this).css('color', '#ff0000');
		}
	});

	$('#iTableOverlayForm input[type="button"]').click(function(){
		if(dataValid()){

			var Url = '/index/couponformsave/data/';
			var aData = {	'company'	: $('#iTextCompany').val(),
							'contact'	: $('#iTextContactPerson').val(),
							'tel'		: $('#iTextTel').val(),
							'email'		: $('#iTextEmail').val(),
							'number'	: $('#iTextNumber').val()
						};
			$.ajax({
				url: Url + encodeURIComponent($.param(aData)),
				datatype: 'json',
				success: function(){
				}
			});

			alert((sLanguage == 'ge')?'Vielen Dank!':'Dank u wel!');
			overlayClose();
		}
		return false;
	});

	resize_bg();
}

function showBrochureForm(sLanguage){
	aBrochureFormFields = {'nl' : {'company'	: 'Bedrijfsnaam:',
									'contact'	: 'Contactpersoon:',
									'tel'		: 'Telefoonnummer:',
									'email'		: 'E-mailadres:',
									'street'	: 'Straatnaam en nummer:',
									'postcode'	: 'Postcode:',
									'city'		: 'Plaatsnaam:',
									'send'		: 'Verstuur'
									},

							'ge' : {'company'	: 'Firmenname:',
									'contact'	: 'Kontaktperson:',
									'tel'		: 'Telefonnummer:',
									'email'		: 'Emailadresse:',
									'street'	: 'Straßenname und -nummer:',
									'postcode'	: 'Postleitzahl:',
									'city'		: 'Ort:',
									'send'		: 'SENDEN'
									}
							};
	sOverlay  = '';

	sOverlay += '<div id="iDivOverlayBack" >';
	sOverlay += '	<div id="iDivOverlayContainer" >';
	sOverlay += '		<div id="iDivOverlayContent" >';
	sOverlay += '			<a id="iAOverlayClose" title="Close" href="#" ><div></div></a>';
	sOverlay += '			<table id="iTableOverlayForm" >';
	sOverlay += '				<tbody>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextCompany" >' + aBrochureFormFields[(sLanguage == 'ge')?'ge':'nl'].company + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextCompany" type="text" name="iTextCompany" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextContactPerson" >' + aBrochureFormFields[(sLanguage == 'ge')?'ge':'nl'].contact + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextContactPerson" type="text" name="iTextContactPerson" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextTel" >' + aBrochureFormFields[(sLanguage == 'ge')?'ge':'nl'].tel + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextTel" type="text" name="iTextTel" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextEmail" >' + aBrochureFormFields[(sLanguage == 'ge')?'ge':'nl'].email + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextEmail" type="text" name="iTextEmail" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextStreet" >' + aBrochureFormFields[(sLanguage == 'ge')?'ge':'nl'].street + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextStreet" type="text" name="iTextStreet" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextPostCode" >' + aBrochureFormFields[(sLanguage == 'ge')?'ge':'nl'].postcode + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextPostCode" type="text" name="iTextPostCode" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td><label for="iTextCity" >' + aBrochureFormFields[(sLanguage == 'ge')?'ge':'nl'].city + '</label></td>';
	sOverlay += '						<td colspan="3" ><input id="iTextCity" type="text" name="iTextCity" value="*required" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '					<tr>';
	sOverlay += '						<td colspan="4" ><input type="button" value="' + aBrochureFormFields[(sLanguage == 'ge')?'ge':'nl'].send + '" /></td>';
	sOverlay += '					</tr>';
	sOverlay += '				</tbody>';
	sOverlay += '			</table>';
	sOverlay += '		</div>';
	sOverlay += '	</div>';
	sOverlay += '</div>';

	$('#iDivMiddleContainer').css('z-index', '-1');
	$('#iDivBackgroundMenu').css('z-index', '-1');
	$('#iDivBackground').after(sOverlay);

	$('#iAOverlayClose').click(function(){
		overlayClose();
	});

	$('#iTableOverlayForm input[type="text"]').focus(function () {
		if (this.value == this.defaultValue){
			this.value = '';
		}

		if(this.value != this.defaultValue){
			this.select();
		}

		$(this).css('color', '#000');
	});

	$('#iTableOverlayForm input[type="text"]').blur(function (){
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
			$(this).css('color', '#ff0000');
		}
	});

	$('#iTableOverlayForm input[type="button"]').click(function(){
		if(dataValid()){

			var Url = '/index/brochureformsave/data/';
			var aData = {	'company'	: $('#iTextCompany').val(),
							'contact'	: $('#iTextContactPerson').val(),
							'tel'		: $('#iTextTel').val(),
							'email'		: $('#iTextEmail').val(),
							'street'	: $('#iTextStreet').val(),
							'postcode'	: $('#iTextPostCode').val(),
							'city'		: $('#iTextCity').val()
						};
			$.ajax({
				url: Url + encodeURIComponent($.param(aData)),
				datatype: 'json',
				success: function(){
				}
			});

			alert((sLanguage == 'ge')?'Vielen Dank!':'Dank u wel!');
			overlayClose();
		}
		return false;
	});

	resize_bg();
}
