
var date_from = null;
var date_to = null;

var prev_date_to = null;

function validateDCCALCForm(form){
	
	if (is_empty(form.date_of_hearing.value) || !is_date(form.date_of_hearing.value))
	{
		alert("Invalid Date of Hearing!\nDate should be dd/mm/yyyy.");
		form.date_of_hearing.focus();
		return false;
	}
		
	date_from = null;
	date_to = null;

	for(var i=0; i< form.elements.length;i++){
		var field = form.elements[i];
		if(field.name.match(/date_from/)){

			if(is_empty(field.value) || !is_date(field.value)){
				alert("Invalid From Date!\nDate should be dd/mm/yyyy.");
				field.focus();
				return false;
			}

			date_from = field;
			
			if(date_to != null || date_to != undefined)
			{
				if(isPastDate(date_from.value, date_to.value, 'dd-mm-yyyy')){
					alert('Date must not be overlapping.');
					date_from.focus();
					return false;
				}
			}

		}

		if(field.name.match(/date_to/)){

			if(is_empty(field.value) || !is_date(field.value)){
				alert("Invalid To Date!\nDate should be dd/mm/yyyy.");
				field.focus();
				return false;
			}

			date_to = field;

			if(isPastDate(date_to.value, date_from.value, 'dd-mm-yyyy')){
				alert('To date must be after From date.');
				date_to.focus();
				return false;
			}

		}

		

		if(field.name.match(/week_hours/)){

			if(is_empty(field.value) || !is_unsign_number(field.value)){
				alert('Invalid hours per week!');
				field.focus();
				return false;
			}
		}

	}

	return true;
}

function dccalc_new_row(obj){
	
	if(validateDCCALCForm(obj.form) == false) return false;

	var tr = $(obj).parent().parent();

	next_date = ''
	if(date_to != null || date_to != undefined){
		
		var myDate=new Date();
		var T = date_to.value.split(/[-\/]/);

		var month = parseInt(T[1]);
		month = month - 1;

		myDate.setFullYear(T[2],month,T[0]);
		
		myDate.setDate(myDate.getDate()+1);
		
		next_date = myDate.getDate()+'/'+(myDate.getMonth() + 1)+'/'+myDate.getFullYear();
		
	}

	$(tr).before('<tr>'+
						'<td>'+
							'<input name="date_from[]" size="12" maxlength="10" value="'+next_date+'">'+
						'</td>'+
						'<td>'+
							'<input name="date_to[]" size="12" maxlength="10" value="">'+
						'</td>'+
						'<td>'+
							'<input name="week_hours[]" size="12" maxlength="10" value="">'+
						'</td>'+
						'<td>'+
						'</td>'+
					'</tr>');

	//'<img src="wp-content/plugins/domestic_care_calculator/images/delete.png" style="cursor:pointer" onclick="dccalc_del_row(this)">' +
}



function dccalc_clr_row(obj){
	$(obj).parent().parent().clear();
}


function validateDCCALC_DRC_Form(form)
{

	if (is_empty(form.txtDateFrom.value) || !is_date(form.txtDateFrom.value))
	{
		alert("Invalid Date from!\nDate should be dd/mm/yyyy.");
		form.txtDateFrom.focus();
		return false;
	}

	if (is_empty(form.txtDateTo.value) || !is_date(form.txtDateTo.value))
	{
		alert("Invalid Date to!\nDate should be dd/mm/yyyy.");
		form.txtDateTo.focus();
		return false;
	}
}

function  submitDCCal_DRC(form)
{
	if (is_empty(form.txtDateFrom.value) || !is_date(form.txtDateFrom.value))
	{
		return false;
	}

	if (is_empty(form.txtDateTo.value) || !is_date(form.txtDateTo.value))
	{
		return false;
	}

	form.submit();
}


function validateDCCALC_TC_Form(form)
{

	if (is_empty(form.txtGross.value) || !is_double(form.txtGross.value))
	{
		alert("Invalid gross amount.");
		form.txtGross.focus();
		return false;
	}
}

function  submitDCCal_TC(form)
{
	if (is_empty(form.txtGross.value) || !is_double(form.txtGross.value))
	{
		return false;
	}

	form.submit();
}

function validateDCCALC_LE_Form(form)
{

	if (is_empty(form.dob.value) || !is_date(form.dob.value))
	{
		alert("Invalid Date of birth!\nDate should be dd/mm/yyyy.");
		form.dob.focus();
		return false;
	}
}

function  submitDCCal_LE(form)
{
	if (is_empty(form.dob.value) || !is_date(form.dob.value))
	{
		return false;
	}

	form.submit();
}

function validateDCCALC_AF_Form(form)
{

	if (is_empty(form.dob.value) || !is_date(form.dob.value))
	{
		alert("Invalid Date of birth!\nDate should be dd/mm/yyyy.");
		form.dob.focus();
		return false;
	}

	if (is_empty(form.doa.value) || !is_date(form.doa.value))
	{
		alert("Invalid Date of Accident!\nDate should be dd/mm/yyyy.");
		form.doa.focus();
		return false;
	}
	
	if (is_empty(form.doh.value) || !is_date(form.doh.value))
	{
		alert("Date of Hearing!\nDate should be dd/mm/yyyy.");
		form.doh.focus();
		return false;
	}

	if (is_empty(form.retire.value) || !is_unsign_number(form.retire.value))
	{
		alert("Retire should be a whole non zero positive number.");
		form.doh.focus();
		return false;
	}
}


function validateDCCALC_TC_11_Form(form)
{


	if (is_empty(form.txtTaxableIncome.value) || !is_unsign_number(form.txtTaxableIncome.value))
	{
		alert("please enter Taxable Income \n Retire should be a whole non zero positive number.");
		form.txtTaxableIncome.focus();
		return false;
	}
	if (is_empty(form.txtPropertyPrice.value) || !is_unsign_number(form.txtPropertyPrice.value))
	{
		alert("please enter Property Price \n Retire should be a whole non zero positive number. ");
		form.txtPropertyPrice.focus();
		return false;
	}
	if (is_empty(form.txtLoan.value) || !is_unsign_number(form.txtLoan.value))
	{
		alert(" please enter Loan Value \n Retire should be a whole non zero positive number.");
		form.txtLoan.focus();
		return false;
	}
	if (is_empty(form.txtInterest.value) || !is_double(form.txtInterest.value))
	{
		alert("please enter Interest Rate \n Retire should be a whole non zero positive number. ");
		form.txtInterest.focus();
		return false;
	}
	if (is_empty(form.txtCapitalGrowth.value) || !is_double(form.txtCapitalGrowth.value))
	{
		alert("please enter Capital Appreciation of Property per Annum. \n Retire should be a whole non zero positive number.  ");
		form.txtCapitalGrowth.focus();
		return false;
	}
	if (is_empty(form.txtRentalIncome.value) || !is_unsign_number(form.txtRentalIncome.value))
	{
		alert("please enter Rental Income Per Week \n Retire should be a whole non zero positive number.  ");
		form.txtRentalIncome.focus();
		return false;
	}
	if (is_empty(form.txtWeeksRented.value) || !is_unsign_number(form.txtWeeksRented.value))
	{
		alert(" Number of Weeks Rented per Year \n Retire should be a whole non zero positive number. ");
		form.txtWeeksRented.focus();
		return false;
	}
	if (is_empty(form.txtRentalExpenses.value) || !is_unsign_number(form.txtRentalExpenses.value))
	{
		alert("please enter  Expenses: Leasing Fees, Body Corporate, Rates, Repairs \n Retire should be a whole non zero positive number.");
		form.txtRentalExpenses.focus();
		return false;
	}
	if (is_empty(form.txtDepn.value) || !is_unsign_number(form.txtDepn.value))
	{
		alert("please enter  Depreciation \n Retire should be a whole non zero positive number. ");
		form.txtDepn.focus();
		return false;
	}
}

function validatePRECALCForm(form){
	
	if (is_empty(form.txtDate.value) || !is_date(form.txtDate.value))
	{
		alert("Invalid Date!\nDate should be dd/mm/yyyy.");
		form.txtDate.focus();
		return false;
	}

	if (is_empty(form.txtHDate.value) || !is_date(form.txtHDate.value))
	{
		alert("Invalid Hearing Date!\nDate should be dd/mm/yyyy.");
		form.txtHDate.focus();
		return false;
	}

	if(isPastDate(form.txtHDate.value, form.txtDate.value, 'dd-mm-yyyy')){
		alert('Hearing date must be after First Date.');
		form.txtDate.focus();
		return false;
	}
}
