
$(document).ready(function () {
	localizeCalender();
	initStoreAvailabilityCheck();
});

//localize calender
function localizeCalender() {
	$.tools.dateinput.localize("de",  {
	   months:        'Januar,Februar,M&auml;rz,April,Mai,Juni,Juli,August,' +
	                   	'September,Oktober,November,Dezember',
	   shortMonths:   'Jan,Feb,M&auml;r,Apr,Mai,Jun,Jul,Aug,Sept,Okt,Nov,Dez',
	   days:          'Sonntag,Montag,Dienstag,Mittwoch,Donnerstag,Freitag,Samstag',
	   shortDays:     'So,Mo,Di,Mi,Do,Fr,Sa'
	});
}

//init overlay for store availability check and reservation on product detail
function initStoreAvailabilityCheck() { 
	$("#storeSelect").change(function() {
		if ($("#storeSelect").val() != null && $("#storeSelect").val() != '') {
			// construct and load the overlay and load the content from request into it
			var theOverlay = $("#reservationInStore").overlay({
				expose: {
					color: "#000", 
					opacity: 0.5 
				},
				top: "30%",
				left: 350,
				closeOnClick: false,
				api: true,
                onBeforeLoad: function() 
                {
                		var wrap = this.getContent().find(".contentWrap");
					  	wrap.html(pageProperties.reloadingHTML);
                },
				onLoad: function() { 
					// grab wrapper element inside content 
                	var wrap = this.getContent().find(".contentWrap");
					var form = $("#storeSelectForm");
					// load the content from request
					wrap.load(form.attr("action") + "?AjaxRequest=true&" + form.serialize(), function(){
						$("a.overlayClose,img.overlayBack", theOverlay.getContent()).click(function(){theOverlay.close()});
					}); 
				}
			});
			theOverlay.load();
		}
	  return false;
	});
}

function initCheckUserLoggedIn() {
	$("#checkUserLoggedIn").click(function() {
		var $this = $("#checkUserLoggedIn");
		var request = $this.attr("href") + "&AjaxRequest=true&" + $this.serialize();

		// construct and load the overlay and load the content from request into it
        var wrap = $("#reservationInStore").overlay().getContent().find(".contentWrap");
		// load the content from request 
		wrap.html(pageProperties.reloadingHTML);
		wrap.load(request, function()
		{
			$("a.overlayClose,img.overlayBack").click(function(){$("#reservationInStore").overlay().close()});
        });
		return false;
	});
}

function initReservationLogin() {
	$("#reservationLogin").submit(function() {
		var $this = $("#reservationLogin");
		var request = $this.attr("action") + "?AjaxRequest=true&" + $this.serialize();

		// construct and load the overlay and load the content from request into it
        var wrap = $("#reservationInStore").overlay().getContent().find(".contentWrap");
		// load the content from request 
		wrap.html(pageProperties.reloadingHTML);
		wrap.load(request, function()
		{
			$("a.overlayClose,img.overlayBack").click(function(){$("#reservationInStore").overlay().close()});
        });
		return false;
	});
}

function initDoReservationInStore() {
	$("#doReservationInStore").submit(function() {
		var $this = $("#doReservationInStore");
		var request = $this.attr("action") + "?AjaxRequest=true&" + $this.serialize();

		// construct and load the overlay and load the content from request into it
        var wrap = $("#reservationInStore").overlay().getContent().find(".contentWrap");
		// load the content from request 
		wrap.html(pageProperties.reloadingHTML);
		wrap.load(request, function()
		{
			$("a.overlayClose,img.overlayBack").click(function(){$("#reservationInStore").overlay().close()});
        });
		return false;
	});
}

//init overlay for logout
function initLogout() {
	var theOverlay = $("#logoutOverlay").overlay({
		expose: {
	        color: "#000",
	        opacity: 0.5
	    },
	    top: "30%",
	    left: 350,
	    closeOnClick: false,
		api: true,
        onLoad: function() {
            $("a.overlayClose,img.overlayBack", theOverlay.getContent()).click(function(){theOverlay.close()});
        }
	}).load();
  return false;
}

function initSubmitSurveyCustomer() {
$("#surveyCustomerForm").submit(function() {
	
	var numericExpression = /^[+]?[0-9\-()/ ]*$/;
	var invalid = false;
	if (document.getElementById('CustomerApproval').checked == false){
		document.getElementById('approval').style.color="#ff0000";
		invalid = true;
	}
	else{
		document.getElementById('approval').style.color="#000";
	}
	if (document.getElementById('surveyCustomerForm_PhoneNumber').value.length < 3 || 
		document.getElementById('surveyCustomerForm_PhoneNumber').value.match(numericExpression) == null)
	{
		document.getElementById('phone').style.color="#ff0000";
		invalid = true;	
	}
	else{
		document.getElementById('phone').style.color="#000";
	}
	if (invalid == true){
		return false;
	}
	
	var $this = $("#surveyCustomerForm");
	var request = $this.attr("action") + "?AjaxRequest=true&" + $this.serialize();

	// construct and load the overlay and load the content from request into it
    var wrap = $("#surveyOverlay").overlay().getContent().find(".contentWrap");
	// load the content from request 
	wrap.html(pageProperties.reloadingHTML);
	wrap.load(request, function()
	{
		$("a.overlayClose,img.overlayBack").click(function(){$("#surveyOverlay").overlay().close()});
    });
	return false;
});
}

// special kind of ajax overlayer for add to basket
function initAddToBasket() {
	$("#addToBasketButton").click(function() {
		// construct and load the overlay and load the content from request into it
		var theOverlay = $("#basketoverlay").overlay({
			expose: {
		        color: "#000",
		        opacity: 0.5
		    },
		    top: "30%",
	        left: 450,
		    closeOnClick: false,
			api: true,
	        onLoad: function() {
	            // grab wrapper element inside content
	            var wrap = this.getContent().find(".contentWrap");
                var form = $("#ProductDetailForm");
	            // load the content from request
	            wrap.load(form.attr("action") + "?AjaxRequest=true&" + form.serialize(), function(){
	            	$("a.close", theOverlay.getContent()).click(function(){theOverlay.close()});
	            });
	        }
		}).load();
	  return false;
	});

    $("a#addToBasketButton, a#addToBasketButton1").click(function() {
        var $this = $(this);
		var request = $this.attr("href") + $this.serialize() + "&AjaxRequest=true";
        // construct and load the overlay and load the content from request into it
		var theOverlay = $("#basketoverlay").overlay({
			expose: {
		        color: "#000",
		        opacity: 0.5
		    },
		    top: "30%",
	        left: 450,
		    closeOnClick: false,
			api: true,
	        onLoad: function() {
	            // grab wrapper element inside content
	            var wrap = this.getContent().find(".contentWrap");
	            // load the content from request
	            wrap.load(request, function(){
	            	$("a.close", theOverlay.getContent()).click(function(){theOverlay.close()});
	            });
	        }
		}).load();
	  return false;
	});
}
