jQuery(document).ready(function() 
{

	jQuery("#dialog_ostoskori").dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,	
		'close': function(event, ui) 
			{ 
				// If last action was ostoskori refresh, make sure we are on the hinnasto-page when closing
				// This is to prevent edits in lahjakortti-page that might not affect all correct places
				if(jQuery('#lastaction').html() == 'edit' || jQuery('#lastaction').html() == 'remove')
				{
					if(top.location == 'http://www.bellabella.fi/web/lahjakortit/')
					{
						top.location.reload(true);
					}
				}
			},
		modal: true,
		height: 630,
		width: 850
	});



	jQuery('#lahjakorttitilaus').bind('submit',function()
	{
		var totalcheck = true;
		if(!checkField('receiver_name')) { totalcheck = false; }
		if(!checkField('receiver_address')) { totalcheck = false; }
		if(!checkField('receiver_postnumber')) { totalcheck = false; }
		if(!checkField('receiver_postplace')) { totalcheck = false; }
		if(!checkField('receiver_phone')) { totalcheck = false; }		
		
		if(!checkField('payer_name')) { totalcheck = false; }
		if(!checkField('payer_address')) { totalcheck = false; }
		if(!checkField('payer_postnumber')) { totalcheck = false; }
		if(!checkField('payer_postplace')) { totalcheck = false; }
		if(!checkField('payer_country')) { totalcheck = false; }		
		if(!checkField('payer_phone')) { totalcheck = false; }			
		if(!checkField('payer_email')) { totalcheck = false; }	
		
		
		if(jQuery("input.gifttype:checked").val() == 'value')
		{
			if(!checkField('giftcard_value')) { totalcheck = false; }
			else
			{
				var thevalue = jQuery('#giftcard_value').val();
				if(thevalue ==  parseInt(thevalue,10))
				{
					
				}
				else
				{
					jQuery('#giftcard_value').next().show();
					totalcheck = false;
				}
			}
			
			
		}
		
		
		
		
		if(jQuery('#receiver_message').val().length > 250)
		{
			jQuery('#receiver_message').next().show();
			totalcheck = false;
			
		}
		
		if(!totalcheck)
		{
			alert('Kaikkia pakollisia kenttiä ei ole täytetty, tai kenttien täytössä on virheitä. \nOle hyvä ja täytä puuttuvat kentät jatkaaksesi tilausta');	
			return false;
		}
		else
		{
			return true;	
		}
	});

	function checkField(elem)
	{
		var $checkelem = jQuery('#' + elem);
		if($checkelem.val() == '' || typeof($checkelem.val()) == 'undefined')
		{
			// alert('fail: ' + $checkelem.attr('id');
			$checkelem.next().show();
			return false;
		}
		else
		{
			$checkelem.next().hide();
			return true;	
		}
	}









	
	function refresh_mini_ostoskori()
	{
		jQuery('#ostoskori').html('ostoskoria päivitetään...').load('http://www.bellabella.fi/mini_ostoskori.php',
			function()
			{
				jQuery('#ostoskori_link a').bind('click',function()
					  {
						var postdata = {};
						postdata.action = 'view';
						loadOstoskori('Avataan ostoskori...',postdata);
						return false;
					  });
			}
		);

	}
	
	
	function loadOstoskori(loadtext,postdata)
	{
		jQuery("#dialog_ostoskori").html(loadtext).dialog('open');

		jQuery('#dialog_ostoskori').load('http://www.bellabella.fi/ostoskori.php', postdata, function() 
		{ 
			bindOstoskoriButtons();
			refresh_mini_ostoskori();
		});
	}

	function addHinnastoButtons()
	{
		jQuery('#post-102 table td:last-child').html('<div class="hinnastobutton">&nbsp;</div>');
		bindHinnastoButtonHandlers()
	}
	function bindHinnastoButtonHandlers()
	{
		jQuery('#post-102').bind('click',function(e)
		{
			var $clickbutton = jQuery(e.target);
			if($clickbutton.is('.hinnastobutton'))
			{
				var shinta = $clickbutton.parent().prev().html();
				var stuote = $clickbutton.parent().prev().prev().html();
				var stuoteryhma = $clickbutton.parents('table').prev().html();
				// alert('tuote: ' + tuote + ', hinta: ' + hinta);

				var postdata = {};
				postdata.action = 'add';
				postdata.tuote = stuote;
				postdata.hinta = shinta;
				postdata.tuoteryhma = stuoteryhma;		
				
				loadOstoskori('Lisätään tuotetta ostoskoriin...',postdata);
			}
			

		});
	}
	
	function bindOstoskoriButtons()
	{
		jQuery('.poista_link').bind('click',function()
		{
			var poistoid = jQuery(this).attr('tuoteid');
			var postdata = {};
			postdata.action = 'remove';
			postdata.tuoteid = poistoid;

			loadOstoskori('Poistetaan tuotetta ostoskorista...',postdata);
						
			return false;
			
		});
		
		jQuery('#jatkaostoksia_link').bind('click',function()
		{
			jQuery("#dialog_ostoskori").dialog('close');
			return false;
		});
		
		jQuery('.update_ostoskori_link').bind('click',function()
		{
			var updatestring = '';
			var separator = '';
			jQuery('input.tuotemaarafield').each(
				function()
				{
					var tuoteid = jQuery(this).attr('tuoteid');
					var maara = jQuery(this).val();
					
					updatestring = updatestring + separator + tuoteid + '=' + maara;
					
					separator = ';';
					
				});

			var postdata = {};
			postdata.action = 'edit';
			postdata.updatestring = updatestring;

			loadOstoskori('Päivitetään ostoskoria...',postdata);

			return false;
		});
	}


	addHinnastoButtons();
	refresh_mini_ostoskori();

	jQuery('#viewostoskori_link').bind('click',function()
		  {
			var postdata = {};
			postdata.action = 'view';
			loadOstoskori('Avataan ostoskori...',postdata);
			return false;
		  });
						   
});