function SaveTip (id) {
	var savings = $('#total_savings').html();
	$.post('/action.php', { action: 'save_tip', id: id, savings: savings }, function(data) {
		$('#my_savings').html(data);
		$('#save_tip').hide();
		$('#saved_badge').show();
	});
}

function get_hostname_from_url(url) {

    return url.match(/:\/\/(.[^/]+)/)[1];
}

function Validate (form) {
	var req_fields = $(form).find(".required");
	var valid = true;

	$(form).find("input,select,textarea").removeClass('left_blank');
	
	for (i=0; i<req_fields.length; i++) {
		var field_name = $(req_fields[i]).attr('name');
		
		if ($(req_fields[i]).val() == '') {
			$(req_fields[i]).addClass('left_blank');
			valid = false;
		}
	}
	
	if (valid == false) {
		DisplayError("You left a required field blank");
	}
	
	return valid;
}

function DisplayError (text) {
	if (text) $("#error").html(text);
	$("#error").animate({ opacity: 1.0 }, 0).slideDown(200).animate({ opacity: 1.0 }, 3000).animate({ opacity: 0 }, 1000).slideUp();
}

function DisplayMessage (text) {
	if (text) $("#error").html(text);
	$("#message").animate({ opacity: 1.0 }, 0).slideDown(200).animate({ opacity: 1.0 }, 1000).animate({ opacity: 0 }, 1000).slideUp();
}

function ViewDialog (title,id) {
	CreateDialog(title,600);
	$("#dialog").load("in/dialog.php?id="+id, function() {
		ShowDialog();
	}).dialog("open");
}

function CreateDialog (title, width) {
	ResetDialog();
	$("#dialog").dialog( { modal: true, title: title, autoOpen: false, width: width, height: $(window).height()-200 } );
}

function ResetDialog () {
	if ($("#dialog").length == 0) $("body").append('<div id="dialog"><div class="spinner">Loading...</div></div>');
	$("#dialog").html('');
	$("#dialog").dialog('destroy');
}

function ShowDialog() {
	var content_height = $("#dialog div").height();
	var max_height = $(window).height() - 200;
	var height = (content_height > max_height) ? max_height : content_height+60;
	if (height < max_height) { 
		$("#dialog").dialog("option","height",height);
	}
}


//////////////////////
$(document).ready(function(){

$("ul#navbar li").click(function() {
if ($(this).find("a").attr("href").indexOf(".aspx") == -1)
				window.location=$(this).find("a").attr("href") + 'index.aspx';
                else
                window.location=$(this).find("a").attr("href") ;
                 return false;
});


//////////
$("a").click(function() {
				
var my_str=$(this).attr("href");
var the_length=my_str.length;
var last_char = my_str.charAt(the_length - 1);
var hn = window.location.hostname;

//alert($(this).attr("href"));
//alert(last_char);

var pathArray = my_str.split( '/' );
var host = pathArray[2];



if (host == location.hostname)
{
				if(last_char == '/')
				{window.location=$(this).attr("href") + 'index.aspx'; return false;}
				else
				{window.location=$(this).attr("href") + '.aspx'; return false;}
				
	}			
				
				
			});

});
