/* this is the function to bookmark the calculator
   reviewed 12/09/07 by Sven Blackwood Soft starts inc.
   Worked on Internet Explorer 7.0 and Firefox 2.0 */

function BookmarkCalculator()

{

	title = window.document.title; 

	

	url = window.location;

	

	if (window.sidebar) // Mozilla Firefox Bookmark

	{

		window.sidebar.addPanel(title, url, "");

	}

	else if(window.external) // IE Favorite

	{

		window.external.AddFavorite(url, title);

	}

	else if(window.opera && window.print)

	{

		var elem = document.createElement('a');

		elem.setAttribute('href',url);

		elem.setAttribute('title',title);

		elem.setAttribute('rel','sidebar');

		elem.click();

	}

}

/* this is the function to send the calculator
   reviewed 12/09/07 by Sven Blackwood Soft starts inc. */

function SendCalculator()

{

	$("span.sendcalculator").show(500);
	scrollBy(0,300);
	

}

/* this is the function to add commas to the values
   reviewed 12/09/07 by Sven Blackwood Soft starts inc. 
   further reviewde 01/02/08 by Sven Blackwood Soft Starts inc.
   */

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;

}

/* this is the function calculate to the values
   it's divided in 3 cases pizza, qsr, and fc
   reviewed 12/09/07 by Sven Blackwood Soft starts inc. */

function Calculate(data, callback)

{

	$.ajax(

	{

	    url: "calc.php",

	    type: "POST",

	    data: data,

	    timeout: 5000,

	    error: function()

		{

			alert("Error");

	    },

	    success: function(data)

		{

			callback(data);

		}

	});

}



$(document).ready(function()

{

	$("input[type='text']").val("");

	$("select").each(function() { this.options.selectedIndex = 0; });

	$("#turnover span.turnoverpercent input").val("100");



	$("#turnover div.types input[type='text']").keydown(function(e)

	{

		if(e.shiftKey == true)

			return false;



		if(e.keyCode === 46)

		{

			$(this).val("");

			

			return false;

		}

		else if(e.keyCode == 190) //decimal

		{

			return true;

		}

		else if(e.keyCode > 31 && (e.keyCode < 48 || (e.keyCode > 57 && e.keycode < 96)))

		{

			return false;

		}

		else

		{

			return true;

		}

	});

    // this portion controls after we click if an industry was selected or not, if it was, it enables the selected option and goes to the second part.

	$("#turnover div.industry input[type='submit']").click(function()

	{

		var error = "Please select an industry.";

		var val = $(this).siblings("select option:selected").val();

			

		if(val === "")

		{

			if($(this).parent().find("span.error").length === 0)

				$(this).parent().append("<span class=\"error\">" + error + "</span>");

		}

		else

		{

			$(this).parent().find("span.error").remove();

			

			$("#turnover div.types div.pizza").hide();

			$("#turnover div.types div.qsr").hide();

			$("#turnover div.types div.fc").hide();

			$("#turnover div.types div." + val).show();

			$("#turnover div.types div." + val).children(":first").slideDown(500);

		}

	});

	

	$("#turnover span.turnoverpercent").append("<input class=\"calculate\" type=\"submit\" value=\"Calculate\" />");

	

	//pizza

	// here we select that is job categories that first appears.
	// this also handles the categories so the proceed button appears or not.

	$("#turnover div.types div.pizza div.categories div div:not(.last)").append("<input type=\"submit\" value=\"Proceed\" />");


    // after selecting how we will add the submit button we tell it what to do on the submission, and what are the error codes.

	$("#turnover div.types div.pizza div.job input[type='submit']").click(function()

	{

		var error = "Please select a job category.";

		var val = $(this).siblings("select option:selected").val();

		var o = $(this).siblings("select").get(0).options.selectedIndex;

		

		if(val === "")

		{

			if($(this).parent().find("span.error").length === 0)

				$(this).parent().append("<span class=\"error\">" + error + "</span>");

		}

		else

		{

			$(this).parent().find("span.error").remove();

			

			$("#turnover div.types div.pizza div.categories").children().hide();

			$("#turnover div.types div.pizza div.categories").show();



			//alert($("#turnover div.types div.pizza div.categories :nth-child(" + o + ")").attr("class"));



			var $children = $("#turnover div.types div.pizza div.categories").children(":first");

			

			for(var i = 1; i < o; i++)

				$children = $children.next();



			$children.show();

			$children.children(":first").slideDown(500);

			

			

		}

	});

	
	// This handles the payrate, hourly or salary.

	$("#turnover div.types div.pizza div.categories input[type='submit']").click(function()

	{

		var val = $(this).siblings("input[type='text']").val();

		var error = "Please complete the field.";

		

		if(val == undefined)

		{

			error = "Please select one.";

			val = $(this).siblings("select option:selected").val();

		}



		if(val === "")

		{

			if($(this).parent().find("span.error").length === 0)

				$(this).parent().append("<span class=\"error\">" + error + "</span>");

		}

		else

		{

			$(this).parent().find("span.error").remove();


			$(this).parent().next().slideDown(500);

			

			if($(this).parent().next().attr("class") == "outcome")

			   $(this).parent().parent().parent().find("span.extra").show(500);

		}

	});


    // this makes the calculation per se.
    $("#turnover div.types div.pizza div.categories input.calculate").click(function()

	{

		var $this = $(this);

		var $parent = $this.parent().parent();

	
        var job = $("#turnover div.pizza div.job select option:selected").val();
		
        var payamount = $parent.find("div.rateofpay input[type='text']").val();

		var employeenumber = $parent.find("div.employeenumber input[type='text']").val();

		var employeeleft = $parent.find("div.last input[type='text']").val();

		var turnoverpercent = $parent.find("span.turnoverpercent input[type='text']").val();

		var paytype;
		
		if(job==7)
		{
		  paytype="salary"
		}
		else if(job==7.85)
		{
	      paytype="company"
		}
		else
		{
		  paytype="hourly"
		}
		
		
		

		Calculate(

		{

			type: "pizza",

			job: job,

			employeenumber: employeenumber,

			employeeleft: employeeleft,

			paytype: paytype,

			payamount: payamount,

			turnoverpercent: turnoverpercent

			

		}, function(data)

		{

			var $data = $(data);



			var html = "", save = "";



			$data.find("item").each(function()

			{

				var id = $(this).attr("id");

				

				var val = $(this).text();



				var name = $("#turnover div.types div.pizza div.job select option:selected").text().toLowerCase();



				var info = "";

				// switch for the return in the cases.

				switch(id)

				{

					case "employeeturnover":

						info = "Your " + name + " annual employee turnover rate is " + AddCommas(parseInt(val)) + "%";

					break;

					case "employeepay":

						if($("#turnover div.pizza div.payhourly").length > 0)

						{

							//info = "Your Employee Hourly Loss Is <strong>$" + AddCommas(parseInt(val)) + "</strong>";

						}

						else if($("#turnover div.pizza div.paysalary").length > 0)

						{

							//info = "Your Employee Salary Loss Is <strong>$" + AddCommas(parseInt(val)) + "</strong>";

						}

					break;

					case "turnovercost":

						info = "Your " + name + " annual employee turnover loss is $" + AddCommas(parseInt(val)) + "";

					break;

					case "turnoversave":

						save = "You save $" + AddCommas(parseInt(val)) + " each year!";

					break;

				}



				if(info != "")

					html += "<span>" + info + "</span><br />";

			});

			

			$parent.find("span.results").slideDown(500);

			$parent.find("span.results").html(html);

			

			$parent.find("span.turnoverpercent").slideDown(500);

			$parent.find("span.outcome").html(save);

			

		});

	});

	// end of pizza case

	//qsr



	$("#turnover div.types div.qsr div.categories div div:not(.last)").append("<input type=\"submit\" value=\"Proceed\" />");



	$("#turnover div.types div.qsr div.job input[type='submit']").click(function()

	{

		var error = "Please select one";

		var val = $(this).siblings("select option:selected").val();

		var o = $(this).siblings("select").get(0).options.selectedIndex;

		

		if(val === "")

		{

			if($(this).parent().find("span.error").length === 0)

				$(this).parent().append("<span class=\"error\">" + error + "</span>");

		}

		else

		{

			$(this).parent().find("span.error").remove();

			

			$("#turnover div.types div.qsr div.categories").children().hide();

			$("#turnover div.types div.qsr div.categories").show();



			//alert($("#turnover div.types div.qsr div.categories :nth-child(" + o + ")").attr("class"));



			var $children = $("#turnover div.types div.qsr div.categories").children(":first");

			

			for(var i = 1; i < o; i++)

				$children = $children.next();



			$children.show();

			$children.children(":first").slideDown(500);

		}

	});

	

	$("#turnover div.types div.qsr div.categories input[type='submit']").click(function()

	{

		var val = $(this).siblings("input[type='text']").val();

		var error = "Please complete the field";

		

		if(val == undefined)

		{

			error = "Please select one";

			val = $(this).siblings("select option:selected").val();

		}



		if(val === "")

		{

			if($(this).parent().find("span.error").length === 0)

				$(this).parent().append("<span class=\"error\">" + error + "</span>");

		}

		else

		{

			$(this).parent().find("span.error").remove();			

			$(this).parent().next().slideDown(500);

			

			

			if($(this).parent().next().attr("class") == "outcome")

				$(this).parent().parent().parent().find("span.extra").show(500);

		}

	});



	$("#turnover div.types div.qsr div.categories input.calculate").click(function()

	{

		var $this = $(this);

		var $parent = $this.parent().parent();

	

		var job = $("#turnover div.qsr div.job select option:selected").val();

        var payamount = $parent.find("div.rateofpay input[type='text']").val();

        var employeenumber = $parent.find("div.employeenumber input[type='text']").val();

		var employeeleft = $parent.find("div.last input[type='text']").val();	

		var turnoverpercent = $parent.find("span.turnoverpercent input[type='text']").val();

		var paytype;
		
		if(job==7)
		{
		  paytype="salary"
		}
		else if(job==7.85)
		{
	      paytype="company"
		}		
		else
		{
	      paytype="hourly"
		}		

		Calculate(

		{

			type: "qsr",

			job: job,

			employeenumber: employeenumber,

			employeeleft: employeeleft,

			paytype: paytype,

			payamount: payamount,

			turnoverpercent: turnoverpercent

			

		}, function(data)

		{

			var $data = $(data);



			var html = "", save = "";



			$data.find("item").each(function()

			{

				var id = $(this).attr("id");

				

				var val = $(this).text();



				var name = $("#turnover div.types div.qsr div.job select option:selected").text().toLowerCase();



				var info = "";

				

				switch(id)

				{

					case "employeeturnover":

						info = "Your " + name + " annual employee turnover rate is " + AddCommas(parseInt(val)) + "%";

					break;

					case "employeepay":

						if($("#turnover div.qsr div.payhourly").length > 0)

						{

							//info = "Your Employee Hourly Loss Is <strong>$" + parseInt(val) + "</strong>";

						}

						else if($("#turnover div.qsr div.paysalary").length > 0)

						{

							//info = "Your Employee Salary Loss Is <strong>$" + parseInt(val) + "</strong>";

						}

					break;

					case "turnovercost":

						info = "Your " + name + " annual employee turnover loss is $" + AddCommas(parseInt(val)) + "";

					break;

					case "turnoversave":

						save = "You save $" + AddCommas(parseInt(val)) + " each year!";

					break;

				}



				if(info != "")

					html += "<span>" + info + "</span><br />";

			});

			

			$parent.find("span.results").slideDown(500);

			$parent.find("span.results").html(html);

			

			$parent.find("span.turnoverpercent").slideDown(500);

			$parent.find("span.outcome").html(save);

		});

	});

	

	//fc

		$("#turnover div.types div.fc div.categories div div:not(.last)").append("<input type=\"submit\" value=\"Proceed\" />");



	$("#turnover div.types div.fc div.job input[type='submit']").click(function()

	{

		var error = "Please select one";

		var val = $(this).siblings("select option:selected").val();

		var o = $(this).siblings("select").get(0).options.selectedIndex;

		

		if(val === "")

		{

			if($(this).parent().find("span.error").length === 0)

				$(this).parent().append("<span class=\"error\">" + error + "</span>");

		}

		else

		{

			$(this).parent().find("span.error").remove();			

			$("#turnover div.types div.fc div.categories").children().hide();

			$("#turnover div.types div.fc div.categories").show();
			

			//alert($("#turnover div.types div.fc div.categories :nth-child(" + o + ")").attr("class"));



			var $children = $("#turnover div.types div.fc div.categories").children(":first");

			

			for(var i = 1; i < o; i++)

				$children = $children.next();



			$children.show();

			$children.children(":first").slideDown(500);
	
		}

	});

	

	$("#turnover div.types div.fc div.categories input[type='submit']").click(function()

	{

		var val = $(this).siblings("input[type='text']").val();

		var error = "Please complete the field";

		

		if(val == undefined)

		{

			error = "Please select one";

			val = $(this).siblings("select option:selected").val();

		}



		if(val === "")

		{

			if($(this).parent().find("span.error").length === 0)

				$(this).parent().append("<span class=\"error\">" + error + "</span>");

		}

		else

		{

			$(this).parent().find("span.error").remove();			


			$(this).parent().next().slideDown(500);

			

			if($(this).parent().next().attr("class") == "outcome")

			$(this).parent().parent().parent().find("span.extra").show(500);

		}

	});



	$("#turnover div.types div.fc div.categories input.calculate").click(function()

	{

		var $this = $(this);

		var $parent = $this.parent().parent();

	

		var job = $("#turnover div.fc div.job select option:selected").val();

        var payamount = $parent.find("div.rateofpay input[type='text']").val();

        var employeenumber = $parent.find("div.employeenumber input[type='text']").val();

		var employeeleft = $parent.find("div.last input[type='text']").val();

		var turnoverpercent = $parent.find("span.turnoverpercent input[type='text']").val();

		var paytype;
		
		if(job==7)
		{
		  paytype="salary"
		}
		else if(job==7.85)
		{
	      paytype="company"
		}		
		else
		{
	      paytype="hourly"
		}		

		Calculate(

		{

			type: "fc",

			job: job,

			employeenumber: employeenumber,

			employeeleft: employeeleft,

			paytype: paytype,

			payamount: payamount,

			turnoverpercent: turnoverpercent

			

		}, function(data)

		{

			var $data = $(data);



			var html = "", save = "";



			$data.find("item").each(function()

			{

				var id = $(this).attr("id");

				

				var val = $(this).text();



				var name = $("#turnover div.types div.fc div.job select option:selected").text().toLowerCase();



				var info = "";

				

				switch(id)

				{

					case "employeeturnover":

						info = "Your " + name + " annual employee turnover rate is " + AddCommas(parseInt(val)) + "%";

					break;

					case "employeepay":

						if($("#turnover div.fc div.payhourly").length > 0)

						{

							//info = "Your Employee Hourly Loss Is <strong>$" + parseInt(val) + "</strong>";

						}

						else if($("#turnover div.fc div.paysalary").length > 0)

						{

							//info = "Your Employee Salary Loss Is <strong>$" + parseInt(val) + "</strong>";

						}

					break;

					case "turnovercost":

						info = "Your " + name + " annual employee turnover loss is $" + AddCommas(parseInt(val)) + "";

					break;

					case "turnoversave":

						save = "You save $" + AddCommas(parseInt(val)) + " each year!";

					break;

				}



				if(info != "")

					html += "<span>" + info + "</span><br />";

			});

			

			$parent.find("span.results").slideDown(500);

			$parent.find("span.results").html(html);

			

			$parent.find("span.turnoverpercent").slideDown(500);

			$parent.find("span.outcome").html(save);

		});

	});

	

	$("span.sendcalculator input[type='text']").blur(function()

	{

		var val = $(this).val();

		var error = "Please fill in the field";

	

		if(val === "")

		{

			if($(this).parent().find("span.error").length === 0)

				$(this).parent().append("<span class=\"error\">" + error + "</span>");

		}

		else

		{

			$(this).parent().find("span.error").remove();

		}

	});

	



	$("span.sendcalculator input[type='submit']").click(function()

	{

		var $form = $("span.sendcalculator");

		var emailto = $form.find(".emailto").val();

		var emailfrom = $form.find(".emailfrom").val();

		var name = $form.find(".name").val();

		var comments = $form.find(".comments").val();



		$("span.sendcalculator input[type='text']").each(function()

		{

			var val = $(this).val();

			var error = "Please fill in the field";

		

			if(val === "")

			{

				if($(this).parent().find("span.error").length === 0)

					$(this).parent().append("<span class=\"error\">" + error + "</span>");

			}

			else

			{

				$(this).parent().find("span.error").remove();

			}

		});



		if($form.find("span.error").length === 0)

		{

			$.ajax(

			{

			    url: "sendcalc.php",

			    type: "POST",

			    data:

				{

					emailto: emailto,

					emailfrom: emailfrom,

					name: name,

					comments: comments

				},

			    timeout: 5000,

			    error: function()

				{

					alert("Error");

			    },

			    success: function(data)

				{

					if(data == "true")

						$form.find("span.msg").html("Calculator sent!");

					else

						$form.find("span.msg").html("Sorry, calculator could not be sent");

						

					$form.find("span.msg").slideDown(500);

				}

			});

		}

	});

});

