$(document).ready(function()
{	
	$('#add-other').click(function() {
		// get the current counter 
		var counter = parseInt($('#other_counter').val());
		var html = '\
		<tr id="other-'+(counter+1)+'">\
			<td><input type="text" name="others['+(counter)+'][other_persons_title]" value="" style="width:25px"/></td>\
			<td><input type="text" name="others['+(counter)+'][other_persons_initials]" value="" style="width:65px"/></td>\
			<td><input type="text" name="others['+(counter)+'][other_persons_surname]" value="" style="width:70px"/></td>\
			<!--<td><span id="dob_'+(counter+1)+'_img"></span></td>-->\
			<td><input type="text" id="others_dob_'+(counter+1)+'" name="others['+(counter)+'][other_persons_dob]" value="" style="width:70px"/></td>\
			<td>\
				<select id="other_relationship_'+(counter+1)+'" name="others['+(counter)+'][other_persons_relationship]" style="width:70px">\
					<option value="spouse">Spouse / Partner</option>\
					<option value="child">Child</option>\
				</select>\
			</td>\
			<td><input type="checkbox" id="other_remove_'+(counter+1)+'" name="others['+(counter)+'][other_remove]" value="1" /></td>\
		</tr>';
		// add the html to the page
		$("#others-table").append('<tbody>'+html+'</tbody>');
		// set the new counter
		$('#other_counter').val(counter+1);
		// initalise the remove checkbox
		$('#other_remove_'+(counter+1)+'').click(function() {
			var count = $('#other_counter').val();
			if($('#other_remove_'+(counter+1)+':checked').length > 0) {
				if(confirm("Remove Selected Person?")) {
					$('#other-'+(counter+1)+'').remove();
					$('#other_counter').val(count-1);
				} else {
					$('#other_remove_'+(counter+1)+'').attr('checked', false);
				}
			}
		});
		$('#others_dob_'+(counter+1)+'').change(function()
		{
			if(isValidDate($('#others_dob_'+(counter+1)+'').val())) {
				document.getElementById('others_dob_'+(counter+1)+'').style.backgroundColor='#CDFDC1';
				//$('#dob_'+(counter+1)+'_img').empty();
				//$('#dob_'+(counter+1)+'_img').html('<img src="/images/tick.gif" width="15" height="15" />');
			} else {
				document.getElementById('others_dob_'+(counter+1)+'').style.backgroundColor='#FFA8A8';
				//$('#dob_'+(counter+1)+'_img').empty();
				//$('#dob_'+(counter+1)+'_img').html('<img src="/images/cross.gif" width="15" height="15" />');
			}
		});
		return false;
	});
	
	
	
		
	$('#depart_date').change(function() {
		if($('#depart_date').val() != "" && $('#return_date').val() != "") {
			$('#num_days').val(daydiff(parseDate($('#depart_date').val()), parseDate($('#return_date').val()))+1);
		}
	});
	
	$('#return_date').change(function() {
		if($('#depart_date').val() != "" && $('#return_date').val() != "") {
			$('#num_days').val(daydiff(parseDate($('#depart_date').val()), parseDate($('#return_date').val()))+1);
		}
	});	
	
	
	
	
	$('#insurance_type').change(function()
	{
		if($('#insurance_type').val() == "single") {
			$('#single').show(400);
			$('#multi').hide(400);
			if($('#insure_family:checked').val() != null) {
				$('#others').show(400);
			} else {
				$('#others').hide(400);
			}
		} else {
			$('#single').hide(400);
			$('#multi').show(400);
			if($('#multi_type_id').val() == 1) {
				$('#others').hide(400);
			} else {
				$('#others').show(400);
			}
		}
		return false;
	});
	
	
	
	
		
	$('#applicant_dob').change(function()
	{
		if(isValidDate($('#applicant_dob').val())) {
			var currentTime = new Date()
			var month = currentTime.getMonth() + 1
			var day = currentTime.getDate()
			var year = currentTime.getFullYear()
			var today = month + "/" + day + "/" + year;

			if(daydiff(parseDate($('#applicant_dob').val()),parseDate(today))>27393)
			{
				document.getElementById('applicant_dob').style.backgroundColor='#FFA8A8';
				alert("For persons over the age of 75 please contact Cherry Godfrey on 711666 to discuss your requirements.");
			}
			else
			{
				document.getElementById('applicant_dob').style.backgroundColor='#CDFDC1';
			}
			//$('#dob_img').empty();
			//$('#dob_img').html('<img src="/images/tick.gif" width="15" height="15" />');
		}
		else
		{
			document.getElementById('applicant_dob').style.backgroundColor='#FFA8A8';
			//$('#dob_img').empty();
			//$('#dob_img').html('<img src="/images/cross.gif" width="15" height="15" />');
		}
	});
	/*
	*/


	$('#multi_type_id').change(function()
	{
		if(($('#multi_type_id').val() == 2)||($('#multi_type_id').val() == 3)) {
			$('#others').show(400);
		} else {
			$('#others').hide(400);
		}
		return false;
	});


	//$('#others').hide();
	/*
	$('#insure_family').click(function()
	{
		if($('#insure_family:checked').val() != null) {
			$('#others').show(400);
		} else {
			$('#others').hide(400);
		}
		return false;
	});
	*/
	
	//$('#prev_policy').hide();
	/*
	$('#policy_renewal').click(function()
	{
		if($('#policy_renewal:checked').val() != null) {
			$('#prev_policy').show(400);
		} else {
			$('#prev_policy').hide(400);
		}
		return false;
	});
	*/
	
});

	function insureFamily()
	{
		if($('#insure_family:checked').val() != null) {
			$('#others').show(400);
		} else {
			$('#others').hide(400);
		}
		return false;
	};

	function policyRenewal()
	{
		if($('#policy_renewal:checked').val() != null) {
			$('#prev_policy').show(400);
		} else {
			$('#prev_policy').hide(400);
		}
		return false;
	};






// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isValidDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : DD/MM/YYYY")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}










function parseDate(str) {
	var mdy = str.split('/');
	return new Date(mdy[2], mdy[1], mdy[0]);
}
function daydiff(first, second) {
	//return (second-first)/(1000*60*60*24);
	var one_day 	= (1000 * 60 * 60 * 24);
    // Convert both dates to milliseconds
    var first_ms 	= first.getTime()
    var second_ms 	= second.getTime()
    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(first_ms - second_ms)
    // Convert back to days and return
    return Math.round(difference_ms/one_day)

}


