
var cmonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

function get_cmonth(mth)
{
	for(keyVar in cmonths)
	{
		if(cmonths[keyVar] == mth)
		{
			return keyVar;
		}
	}
	return false;
}

function get_scw_date(form, ffield)
{
	var date_pre = '';
	var tar = document.getElementById(ffield);
	
	scw_date = tar.value;
	if(scw_date.indexOf('-') != -1)
	{
		ndate_arr = scw_date.split('-');
		if(ndate_arr.length == 3)
		{
			tmpYear = new Number(ndate_arr[2]);
			tmpMonth = new Number(get_cmonth(ndate_arr[1]));
			tmpDay = new Number(ndate_arr[0]);
			if(tmpDay < 10)
			{
				date_pre = '0';
			}
			tmpDate = new Date(tmpYear, tmpMonth, tmpDay);
			newDate = date_pre + tmpDate.getDate() +'-'+ cmonths[tmpDate.getMonth()] +'-'+ tmpDate.getFullYear();
			return newDate;
		}
	}
	return false;
}

function convert_scw_date(scw_date)
{
	if(scw_date && scw_date.indexOf('-') != -1)
	{
		ndate_arr = scw_date.split('-');
		if(ndate_arr.length == 3)
		{
			tmpYear = new Number(ndate_arr[2]);
			tmpMonth = new Number(get_cmonth(ndate_arr[1]));
			tmpDay = new Number(ndate_arr[0]);
			
			newDate = new Date(tmpYear, tmpMonth, tmpDay);
			return newDate;
		}
	}
	return false;
}
function changeDate(form, cal_f, cal_t,chkfuture)
{
	var useFDate = false;
	var date_pre = '';
	var ValidateDate = true;
	cal_f_Date = convert_scw_date(get_scw_date(form, cal_f));
	cal_t_Date = convert_scw_date(get_scw_date(form, cal_t));
	
	if(cal_f_Date && cal_t_Date)
	{
		f_Date = new Date(cal_f_Date);
		t_Date = new Date(cal_t_Date);
		
		if(f_Date >= t_Date)
		{
			useFDate = true;
		}
	}
	else
	{
		if(cal_f_Date)
		{
			useFDate = true;
			f_Date = new Date(cal_f_Date);
			
		}
	}
	if(chkfuture == 'allowFuture'){
		ValidateDate= CheckPresentAndFuture(f_Date);
		if(!ValidateDate){
			var now = new Date();
			f_Date=new Date(now.getFullYear(),now.getMonth(),now.getDate());
			nowday=now.getDate();
			var Yday = new Date(now.getFullYear(),now.getMonth(),now.getDate()-1);
			var tarfromdt = document.getElementById(cal_f);
			if(nowday<10) nowday='0'+nowday;
			tarfromdt.value=nowday+'-'+cmonths[now.getMonth()]+'-'+now.getFullYear();
			alert('You must select a date greater than '+ Yday.format("dd-mmmm-yyyy"));
		}
	}
	if(chkfuture == 'allowPast'){
		ValidateDate= CheckPresentAndPast(f_Date);
		if(!ValidateDate){
			var now = new Date();
			f_Date=new Date(now.getFullYear(),now.getMonth(),now.getDate());
			nowday=now.getDate();
			var Yday = new Date(now.getFullYear(),now.getMonth(),now.getDate());
			var tarfromdt = document.getElementById(cal_f);
			if(nowday<10) nowday='0'+nowday;
			tarfromdt.value=nowday+'-'+cmonths[now.getMonth()]+'-'+now.getFullYear();
			alert('You must select a date less than today\'s date.');
		}
	}
	if(useFDate)
	{
		nDate = new Date(f_Date.getFullYear(), f_Date.getMonth(), (f_Date.getDate() + 1));
		f_Day = nDate.getDate();
		if(f_Day < 10)
		{
			date_pre = '0';
		}
		newDate = date_pre + f_Day +'-'+ cmonths[nDate.getMonth()] +'-'+ nDate.getFullYear();
		var tar = document.getElementById(cal_t);
		tar.value=newDate;
	}
}

function saveDateTo(DateTo,Tmpdt){
	//this is a hidden field in html page to store current value ToDate for reuse,if needed.
	var tar = document.getElementById(DateTo);
	var tmpdt = document.getElementById(Tmpdt);
	tmpdt.value=tar.value;
}

function checkDate(form, cal_f, cal_t,tmpdate)
{
	var useFDate = false;
	var date_pre = '';
	var tar = document.getElementById(cal_t);
	var tmpdt = document.getElementById(tmpdate);
	

	cal_f_Date = convert_scw_date(get_scw_date(form, cal_f));
	cal_t_Date = convert_scw_date(get_scw_date(form, cal_t));

	if(cal_f_Date && cal_t_Date)
	{
		f_Date = new Date(cal_f_Date);
		t_Date = new Date(cal_t_Date);
		
		if(t_Date >= f_Date)
		{
			useFDate = true;
		}
	}

	if(!useFDate){
		tar.value=tmpdt.value;
		alert('Please select a date greater than the From Date.');
	}
}

function CheckPresentAndFuture(chkDate){
	var now = new Date();
	
	var validDay = new Date(now.getFullYear(),now.getMonth(),now.getDate()-1);
	if(chkDate >= validDay){
		return true;
	}else{
		return false;	
	}
}

function CheckPresentAndPast(chkDate){
	var now = new Date();
	
	var validDay = new Date(now.getFullYear(),now.getMonth(),now.getDate());
	if(chkDate <= validDay){
		return true;
	}else{
		return false;	
	}
}
