// JavaScript Document
$(document).ready(function()
{			 	
	$('#info_yos').click(function()
	{
		var msg = "<h5>Years of Service:</h5>";
			msg += "<p>The years service refers to the number of <b>consecutive</b> years you have been with the company.</p>";
			msg += "<p><font color='#FF0000'>Please note: You cannot claim more than 20 years of service.</font></p>"
		$('#dialog').html(msg);
		$('#dialog').dialog('open');
	
	});
	
	$('#info_avg').click(function()
	{
		var msg = "<h5>Average weeks pay:</h5>";
			msg += "<p>If you are working normal hours each week and your pay does not change from week to week your average gross weekly pay will be your basic gross weekly wage unless overtime was part of your normal weekly work it will not be included.</p>";
			msg += "<p>If your weekly pay changes e.g. piecework or shift work your average gross weekly wage is calculated based on the average over the 12 weeks before the date you were given notice of your redundancy</p>";
			$('#dialog').html(msg);
			$('#dialog').dialog('open');
	});
});	

function validate () {
	var YearsService = $('#YearsService').val();
	var YearsOld = $('#YearsOld').val();
	var WeeksPay = $('#WeeksPay').val();
	var frequency = $('#frequency').val();
	var minAge=17;
	var msg = "<p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>Please check the following errors:-<ul>";
	var res = true;
	
	if(YearsService.length == 0 || YearsService.length == null)
	{
		msg += "<li>Please provide the number of years you have consecutively worked for.</li>";
		//msg += "-Please provide the number of rooms in your home.\n\n";
		res = false;
	}
	
	if(YearsOld.length == 0 || YearsOld.length == null)
	{
		msg += "<li>Please provide us with you current age.</li>";
		//msg += "-Please provide the number of rooms in your home.\n\n";
		res = false;
	}
	
	if(WeeksPay.length == 0 || WeeksPay.length == null)
	{
		msg += "<li>Please provide you salary depending on your dropdown option.</li>";
		//msg += "-Please provide the number of rooms in your home.\n\n";
		res = false;
	}
	if(YearsOld.length > 0){
		if(YearsOld < minAge){
			res = false;
			var tempAge = minAge-1;
			msg += "<li>You need to be older than "+tempAge+" to be able to claim any redundancy.</li>";
		}
	}
	msg += "</ul></p>";
	if(res == false)
	{
		$('#dialog').html(msg);
		$('#dialog').dialog('open');
		return false;
	}
	else{
		var YrsOld = YearsOld;
		var InService = YearsService;
		// sort out the salary pay to read as weekly pay if they are weekly/monthly/annauly basis				
		var WeeksPay = salary_divider(WeeksPay, frequency);		
		var YrsOver41=0;
		var Yrs22to41=0;
		var YrsUnder22=0;
		var WeeksDue=0;
		var PayDue=0;
		var maxPay=400; // Sep 09 rate
		var minService=2;
		var maxService=20;		
		var tempTotal = 0;
		var data = false;
		if (WeeksPay == 0){data = false;}
		else {  
			if (InService > 20){InService = 20;};
			if (YrsOld > 41){YrsOver41 = YrsOld-41;}
			if (YrsOver41 > InService) {YrsOver41 = InService;}
			if (InService - YrsOver41 > 0) { if (YrsOld > 22){Yrs22to41 = YrsOld-YrsOver41-22;}}
			if (YrsOver41+Yrs22to41 > InService) {Yrs22to41=InService-YrsOver41;}
			if (InService - YrsOver41 - Yrs22to41 > 0) { YrsUnder22 = YrsOld-YrsOver41-Yrs22to41-minAge+minService;}
			if (YrsOver41+Yrs22to41+YrsUnder22 > InService) {YrsUnder22 = InService-YrsOver41-Yrs22to41;}
			WeeksDue=(YrsOver41*1.5) + (Yrs22to41) + (YrsUnder22 * 0.5);
			if (WeeksPay > maxPay) {WeeksPay = maxPay;}
			tempTotal = (WeeksDue*WeeksPay)*100;				
			tempTotal = Math.ceil(tempTotal);
			PayDue= tempTotal/100;
		}
		
		if(WeeksPay > maxPay){
			var WeeksPayText = "Maximum weekly redundancy pay entitlement:";
		}
		else{
			var WeeksPayText = "Weekly Redundancy pay entitlement:";
		}
		$('#WeeksDue').html(WeeksDue);
		$('#PayDue').html(PayDue);		
		$('#WeeksPayText').html(WeeksPayText);
		$('#ResultWeeksPay').html(WeeksPay);
		$('#result_area').show('slow');
		return data;
	}
}

function salary_divider(pay, frequency){
	var result = 0;
	if(IsNumeric(pay) || frequency.length > 0){
		switch(frequency){
			case "Weekly":
				result = pay;
				break;
			case "2 Weekly":
				result = Math.ceil(pay / 2);
				break;			
			case "4 Weekly":
				result = Math.ceil(pay / 4);
				break;			
			case "Monthly":
				result = Math.ceil(pay / 4.333);
				break;			
			case "Annually":
				result = Math.ceil(pay / 52);
				break;			
		}
	}
	else{
		result = false;
	}
	return result;
}

function IsNumeric(input){
   return (input - 0) == input && input.length > 0;
}
