/**
 * SIDEWIND 1.0
 * ===========
 *
 * Skapat av CD SOFT
 *
 * E-post:      info@cdsoft.se
 * Internet:    www.cdsoft.se
 *
 *
 * Copyright (C) 2006-2008 CD Soft AB
 *
 **/

///---------------------------

$(document).ready(function()
{
	$('ul.sub').hide();
	
	var path = jQuery.url.attr("path");
	
	if(path != '/')
	{
		var PathArray = path.split(/\//g);
		
		if(PathArray.length == 3)
		{
			var MenuId = PathArray[1];
	
			MenuId = MenuId.replace(/\s+/g,'');

//	MenuId = MenuId.replace(/\//g, "-");

			$("#" + MenuId).addClass("active").show('slow');
		
		}
		else if(PathArray.length == 4)
		{

			var MenuId = PathArray[1] + '-' + PathArray[2];
	
			MenuId = MenuId.replace(/\s+/g,'');

//	MenuId = MenuId.replace(/\//g, "-");

			$("#" + MenuId).parent().parent().addClass("active").show('slow');
		
		}
		
		if(path.substr(-4) == 'html')
		{
			$('#Forfragan').css('cursor', 'pointer');

			$('#Forfragan').click(function (e)
			{
				$("#dialog").load("/_ajax/forfragan.php?page=" + PathArray[3], function()
				{
					$('#StartDatum, #SlutDatum').datepicker({
				        beforeShow: customRange,
						minDate: '+1D',
						maxDate: '+1Y'
					});
				}).dialog(
				{
					height: 450,
					width: 600,
					title: $('title').text(),
					modal: true,
					buttons: {
						'Send': function()
						{
							if(!Validate())
							{
								return;
							}

							$.post("/_ajax/intresseanmalan.php", 
							{
								action: 'post',
								k_id_WPER: $('#k_id_WPER').val(),
								k_id_WITE: $('#k_id_WITE').val(),
								Namn: $('#Namn').val(),
								Telefon: $('#Telefon').val(),
								Epost: $('#Epost').val(),
								Plats: $('#Plats').val(),
								StartDatum: $('#StartDatum').val(),
								StartTid: $('#StartTid').val(),
								SlutDatum: $('#SlutDatum').val(),
								SlutTid: $('#SlutTid').val(),
								k_Personal: $("input[name='personal']:checked").val(),
								Meddelande: $('#Meddelande').val()
							},
							function(data)
							{
								$("#dialog").dialog('close');
							}, 'json');
						},
						'Close': function() {
							$(this).dialog('close');
						}
					},
					close: function(event, ui)
					{
						$(this).dialog('destroy');
						$('head style').remove();
					},
					open: function(event, ui)
					{
						var Buttons = $('button');
						
						for(i=0;i<Buttons.length;i++)
						{
							switch($(Buttons[i]).text())
							{
								case 'Close':
									$(Buttons[i]).attr('id', 'btnClose');
								break;

								case 'Send':
									$(Buttons[i]).attr('id', 'btnSend');
								break;
							}
						}
						
						$('#btnClose').text('').css('background-image', 'url(/_gfx/but_close.gif)').css('width', 67).css('height', 24).blur();
						$('#btnSend').text('').css('background-image', 'url(/_gfx/but_skicka.gif)').css('width', 67).css('height', 24).blur();
					}
				});
			});
		}
	}
	else
	{
	
	}
});

function customRange(input) 
{
	var _TheTime=new Date();

	var min = new Date(_TheTime.getYear(), _TheTime.getMonth(), _TheTime.getDay()); //Set this to your absolute minimum date
	var dateMin = min;
	var dateMax = null;
	var dayRange = 2; // Set this to the range of days you want to restrict to

	if (input.id == "StartDatum")
	{
		if ($("#SlutDatum").datepicker("getDate") != null)
		{
			dateMax = $("#SlutDatum").datepicker("getDate");
			dateMin = $("#SlutDatum").datepicker("getDate");
			dateMin.setDate(dateMin.getDate() - dayRange);
			if (dateMin < min)
			{
				dateMin = min;
			}
		}
		else
		{
			dateMin = new Date(); //Set this to your absolute minimum date
			dateMax = new Date(_TheTime.getFullYear()+1, _TheTime.getMonth()+2, _TheTime.getDay()); //Set this to your absolute maximum date
		}                      
	}
	else if (input.id == "SlutDatum")
	{
		dateMax = new Date(); //Set this to your absolute maximum date
		if ($("#StartDatum").datepicker("getDate") != null)
		{
			dateMin = $("#StartDatum").datepicker("getDate");
			dateMax = $("#StartDatum").datepicker("getDate");

			dateMax.setDate(dateMin.getDate()+365);
		}
	}

    return {
		minDate: dateMin, 
		maxDate: dateMax
	}; 
}


function Validate()
{
	var ErrorMessage = '';
	var Error = 0;

	if($('#Namn').val() == '')
	{
		ErrorMessage += 'Namn måste fyllas i.\n';
		Error = 1;
	}

	if($('#Telefon').val() == '')
	{
		ErrorMessage += 'Telefon måste fyllas i.\n';
		Error = 1;
	}

	if($('#Epost').val() == '')
	{
		ErrorMessage += 'Epost måste fyllas i.\n';
		Error = 1;
	}
	
	if(Error == 1)
	{
		alert(ErrorMessage);
		return false;
	}
	else
		return true;
}


function HideChildren(obj)
{
	$('ul.sub').hide();
	
	if(obj.hasClass("active"))
	{
		obj.removeClass("active");
		obj.siblings("ul").hide('slow');
	}
	else
	{
		obj.addClass("active");
		obj.siblings("ul").show('slow');
	}

}