/* Paper Free Billing */
/* Behold, my computer wizardry */


$(document).ready(function() {

	$("#alertMsg").click(function() { $(this).slideUp(600); });

	/* Sample Sender Functionality */
	var sampleSenderMsg = $("#sampleSenderMsg");
	var sampleSenderInput = $("#sampleSenderInput");
	
	
	$("#statusDialog").dialog({
		width: 400,
		autoOpen: false,
		resizable: false,
		modal: true
	});
	
	$("#statusDialog").click( function() { $(this).dialog("close"); });


	$("#sampleSenderEmail").focus( function()
	{
		if ($(this).val() == "name@address.com")
			$(this).val("");
			
		sampleSenderMsg.slideUp(500, function() { sampleSenderMsg.html(""); });
	});
	
	
	$("#sampleSenderButton").click( function() {
	
		var sampleSenderEmail = $("#sampleSenderEmail").val();
		var validEmail = ((sampleSenderEmail.indexOf(".") > 0) && (sampleSenderEmail.indexOf("@") > 0));

		if (!validEmail) {
			sampleSenderMsg.html("Please enter a valid email address!");
			sampleSenderMsg.slideDown(500);
			return false;
		}

		if (sampleSenderEmail == "name@address.com") {
			sampleSenderMsg.html("Please enter your own email address!");
			sampleSenderMsg.slideDown(500);
			return false;
		}
		
		sampleSenderMsg.slideUp(500, function() { sampleSenderMsg.html(""); });
		
		$.ajax({
			url: "emailSample",
			data: { sampleSenderEmail: sampleSenderEmail },
			success: function() {
				sampleSenderMsg.removeClass("ui-state-error").addClass("ui-state-highlight").html("Your Sample Invoice has been Sent!");
				sampleSenderInput.slideUp(500, function() { sampleSenderMsg.slideDown(500); });
				$("#sampleSenderButton").attr("disabled", "disabled"); /* quietly disable button after successful send */
				_gaq.push(['_setAccount', 'UA-15656369-1']);
				_gaq.push(['_trackPageview', '/sent-invoice.php']);
			}
		});

		return false;
	});


	/* Logout Dialog */

	$("#logoutButton").click( function() { $("#logoutDialog").dialog("open"); return false; });

	$("#logoutDialog").dialog( {
		bgiframe: true,
		resizable: false,
		autoOpen: false,
		width: 350,
		modal: true,
		buttons: {
			'Confirm Sign Out': function() {
				document.location = '/logout';
				$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		}
	});
	
});


var alphaNumeric = ".0123456789";

function SSLwindow(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=1,width=400,height=600');");
}


function abs (n) {
	return ((n<0) ? n*-1 : n);
}


function addCommas(nStr) {
	nStr += "";
	x = nStr.split(".");
	x1 = x[0];
	x2 = x.length > 1 ? "." + x[1] : "";

	var rgx = /(\d+)(\d{3})/;

	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, "$1" + "," + "$2");
	}

	return x1 + x2;
}


