function getPageSizeForRecommendation() {

	var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

jQuery(document).ready(function() {	
	
	//select all the a tag with name equal to modal
	jQuery('#headernavmisc #miscright a:eq(1)').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		//Get the A tag
		var id = jQuery(this).attr('href');
		
		// load captcha
		jQuery('#recommendationcaptchaimage').empty();
		var img = new Image();
		jQuery(img).load(function() {
		}).attr({'src':'fileadmin/sconto/scripts/scontocaptcha.php?s='+Math.random(), 'id':'capid'});
		jQuery('#recommendationcaptchaimage').append(img);
		jQuery('#recommendationcaptchaimage').append('<br />Code nicht lesbar? <a href="#" style="text-indent: 24px; font-size: 9px;" id="recommendationreloadcaptcha">Hier klicken!</a>');
		
		// Use link to reload captcha
		jQuery('#recommendationreloadcaptcha').click(function(e) {
			e.preventDefault();

			// reload captcha
			jQuery('#capid').remove();
			var img = new Image();
			jQuery(img).load(function() {
			}).attr({'src':'fileadmin/sconto/scripts/scontocaptcha.php?s='+Math.random(), 'id':'capid'});
			jQuery('#recommendationcaptchaimage').prepend(img);
		});
		
		//Get the screen height and width
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
	
		//Set height and width to mask to fill up the whole screen
        var arrayPageSize = getPageSizeForRecommendation();
		jQuery('#recommendationmask').css({'width':arrayPageSize[0] + 'px','height':arrayPageSize[1] + 'px'});
		
		//transition effect		
		jQuery('#recommendationmask').fadeIn(200);	
		jQuery('#recommendationmask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
		
		//Set the popup window to center
		jQuery('.recommendationwindow').css('top',  winH/2-jQuery('#recommendationbox').height()/2 + jQuery(window).scrollTop()-36);
		jQuery('.recommendationwindow').css('left', winW/2-jQuery('#recommendationbox').width()/2);
	
		//transition effect
		jQuery('#recommendationbox').fadeIn(500);
		
		return false;
	});
	
	jQuery('#recommendationsubmit').click(function(e) {
		// cancel the default form submit
		e.preventDefault();
		
		// reset all errors
		jQuery('#recommendationform input').removeClass('error');
		
		// some error checking
		var sendername = jQuery('#recommendationsendername').val();
		var senderemail = jQuery('#recommendationsenderemail').val();
		var recipientemail = jQuery('#recommendationrecipientemail').val();
		var message = jQuery('#recommendationmessage').val();
		var captcha = jQuery('#recommendationcaptcha').val();
		var errors = false;
		var captchaok = false;
		
		if(sendername == '') {
			errors = true;
			jQuery('#recommendationsendername').addClass('error');
		}
		
		if (!isValidEmailAddress(senderemail)) {
			errors = true;
			jQuery('#recommendationsenderemail').addClass('error');
		}

		if (!isValidEmailAddress(recipientemail)) {
			errors = true;
			jQuery('#recommendationrecipientemail').addClass('error');
		}

		// check captcha
		jQuery.get('fileadmin/sconto/scripts/checkCaptcha.php', {code:captcha}, function(data) {
			if (data != 'yes') {
				errors = true;
				jQuery('#recommendationcaptcha').addClass('error');
			}
			else {
				// proceed if there were no errors
				if(!errors) {
					var dataString = 'sendername=' + sendername + '&senderemail=' + senderemail + '&recipientemail=' + recipientemail + '&message=' + message + '&url=' + window.location;
					jQuery.ajax({
						type: 'POST',
						url: 'fileadmin/sconto/scripts/sendform.php',
						data: dataString,
						success: function() {
							jQuery('#recommendationcontent').fadeOut(400, function() {
								jQuery('#recommendationbox').append('<div id="recommendationsuccess">Vielen Dank!</div>');
							});
						}
					});
					
					return false;
				} else {
					// if there were errors, display a short message
					jQuery('#required').html('Bitte korrigieren Sie die obigen Felder!');
				}
			}
		});		
	});
	
	//if close button is clicked
	jQuery('.recommendationwindow .recommendationclose').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		jQuery('#recommendationmask, .recommendationwindow').hide();
		
		// restore and clear out form
		jQuery('#recommendationbox').remove('#recommendationsuccess');
		jQuery('#required').html('Felder mit * sind Pflichtfelder');
		jQuery('#recommendationsendername').val('');
		jQuery('#recommendationsenderemail').val('');
		jQuery('#recommendationrecipientemail').val('');
		jQuery('#recommendationmessage').val('');
		jQuery('#recommendationcaptcha').val('');
		jQuery('#recommendationform input').removeClass('error');
		jQuery('#recommendationcontent').show();
	});		
	
	//if mask is clicked
	jQuery('#recommendationmask').click(function () {
		jQuery(this).hide();
		jQuery('.recommendationwindow').hide();

		// restore and clear out form
		jQuery('#recommendationbox').remove('#recommendationsuccess');
		jQuery('#required').html('Felder mit * sind Pflichtfelder');
		jQuery('#recommendationsendername').val('');
		jQuery('#recommendationsenderemail').val('');
		jQuery('#recommendationrecipientemail').val('');
		jQuery('#recommendationmessage').val('');
		jQuery('#recommendationcaptcha').val('');
		jQuery('#recommendationform input').removeClass('error');
		jQuery('#recommendationcontent').show();

	});			
	
});

