	/**********************************************************************

	Scripts
		This file provide all javascript to support operations in the 
		website

	@Package:		Weshare
	@Link: 			http://www.internotredici.com
	@Author: 		Fabrizio Menghini Calderón <fabrizio@internotredici.com>
	@Copyright:		Copyright (c) 2007 Fabrizio Menghini Calderón
	@Change Log:

		[R001] ~ 2007-11-06: Creation

	**********************************************************************/




	$(document).ready
	(
		function()
		{
			/*
			==============================================
				Fix PNGs for IE 6
			==============================================
			*/

			if(($(document).jbrowser('version') < 7) && ($(document).jbrowser() == 'IE'))
			{
				$.ifixpng(JQ_ROOT_DIR + 'media/pixel.gif');
				$('img[@src$=.png]').ifixpng();
				$('input[@src$=.png]').ifixpng();
			}



			/*
			==============================================
				Hover button
			==============================================
			*/

			if(($(document).jbrowser('version') < 7) && ($(document).jbrowser() == 'IE'))
			{
				/* Disable hover functionality in IE 6 */
			}
			else
			{
				/* Change the hover button on mouse hover */
				$(".hover_button").hover ( function () 
					{
						if($(this).attr("src"))
						{
							/* Remove the png extension from the image name */
							var image_normal = $(this).attr("src"); 
							image_normal = image_normal.replace(/_hover.png/,"");
							image_normal = image_normal.replace(/.png/,"");
							image_normal = image_normal + "_hover.png";

							$(this).attr("src",image_normal); 
						}
					}, function () 
					{ 
						if($(this).attr("src"))
						{
							/* Remove the "_hover.png" substring from the image name */
							var image_hover = $(this).attr("src"); 
							image_hover = image_hover.replace(/_hover.png/,"");
							image_hover = image_hover.replace(/.png/,"");
							image_hover = image_hover + ".png";

							$(this).attr("src",image_hover); 
						}
					} 
				);


				/* Change the hover button on focus */
				$(".hover_button").focus ( function () 
					{ 
						if($(this).attr("src"))
						{
							/* Remove the png extension from the image name */
							var image_normal = $(this).attr("src"); 
							image_normal = image_normal.replace(/_hover.png/,"");
							image_normal = image_normal.replace(/.png/,"");
							image_normal = image_normal + "_hover.png";

							$(this).attr("src",image_normal); 
						}
					} 
				);

				/* Change the hover button on blur */
				$(".hover_button").blur ( function () 
					{ 
						if($(this).attr("src"))
						{
							/* Remove the "_hover.png" substring from the image name */
							var image_hover = $(this).attr("src"); 
							image_hover = image_hover.replace(/_hover.png/,"");
							image_hover = image_hover.replace(/.png/,"");
							image_hover = image_hover + ".png";

							$(this).attr("src",image_hover); 
						}
					} 
				);

				/* Unbind the functions */
				$(".hover_button").click ( function () 
					{ 
						var parent_href = $(this).parent().attr("href");
						if(parent_href.charAt(0) != "#")
							$(".hover_button").unbind();
					} 
				);
			}



			/*
			==============================================
				Bind enter key to submit forms
			==============================================
			*/

			$(document).keyup(function(event)
			{
				if(event.keyCode == 13)
				{
					if($("#mainForm").length > 0) { submitForm('mainForm'); }
					if($("#personForm").length > 0) { addPerson(); }
				}
			});

			$("#subscription_price").val(5 * $("#subscription_size").val() * $("#subscription_months").val());
		}
	);

	function submitForm(formId)
	{
		switch(formId)
		{
			case "uploadForm":
			{
				$("#uploadProgress").show();
				document.getElementById(formId).submit();
				break;
			}
			default:
			{
				document.getElementById(formId).submit();
				break;
			}
		}
	}

	function checkUsername()
	{
		var element_value = $('#person_username').val();

		// Username need to be valid
		var legalChars = /([a-z]|[A-Z]){1}([a-z]|[A-Z]|[0-9]|-|_){3,}/;
		$("#person_username + span > strong").text('');
    	if(!legalChars.test(element_value)) 
		{
			// Shows the error message
			$("#person_username + span > strong").text(' Username non valido');
			return(false);
		}

		// Remove error message
		$("#person_username + span > strong").text('');

		// Show the loading icon
		$("#person_username + span > img").toggleClass('SF_hidden');

		$.ajax
		(
			{
				url: '/services/usernameavailability/' + element_value + '/',
				type: 'GET',
				dataType: 'xml',
				timeout: 1000,
				cache: false,
				success: function(xml)
				{
					$("availability",xml).each(function()
					{
						if($("result",this).text() == "1")
							$("#person_username + span > strong").text(' Username non disponibile');
						else
							$("#person_username + span > strong").text('');
					});
				}
			}
		);	

		// Hides the loading icon
		if(!$("#person_username + span > img").hasClass('SF_hidden'))
			$("#person_username + span > img").addClass('SF_hidden');

	}

	function price_update()
	{
		if(isNaN($("#subscription_size").val()))
			$("#subscription_size").val($("#subscription_minimum").val());
			
		if(isNaN($("#subscription_months").val()))
			$("#subscription_months").val(12);

		if(($("#subscription_size").val() * 1) < ($("#subscription_minimum").val() * 1))
			$("#subscription_size").val($("#subscription_minimum").val());

		$("#subscription_price").val(5 * $("#subscription_size").val() * $("#subscription_months").val());
		$("#subscription_amount").val((5 * $("#subscription_size").val() * $("#subscription_months").val()));

		$("#subscription_item").val($("#subscription_size").val() + " Gb / " + $("#subscription_months").val() + " mesi");
		$("#subscription_custom").val($("#subscription_size").val() + ":" + $("#subscription_months").val());
	}
