/*
 * Author: Francisco Moreno.
 * usednick_at_gmail_dot_com.
 * Copyright: Ekwest Interactive Solutions, Inc. 2006
 *  
 */

//Misc date related functions.
function continuous_dates(a,b) {
	if (b.getTime() == (new Date(a.getFullYear(),a.getMonth(),a.getDate()+1,a.getHours(),a.getMinutes())).getTime()) return 1; //Ok.
		else return 0; //err.
}

function continuous_dates_striso(a,b) { //This function expects 2 iso formated dates yyyy/mm/dd
	return continuous_dates(new Date(a), new Date(b));
}

//This function only cares about the month field.
function continuous_months(a,b) {
	if((a.getMonth()+1<12?a.getMonth()+1:1) == b.getMonth()) return 1;
		else return 0; 
}

//This function only cares about the year field.
function continuous_years(a,b) {
	if(a.getFullYear()+1 == b.getFullYear) return 1;
		else return 0; 
}

function compact_dates(dates) {
	var periods=Array();
	for(i=0; i<dates.length; i++) {
		var newp=Array(dates[i]);
		while(++i<dates.length && continuous_dates(dates[i-1],dates[i]))	continue;
		newp.push(dates[--i]);
		periods.push(newp);
	}
	
	return periods;
}

function parse_periods(periodsarr,format,tconnector,tformat,periodidx) {
	var str='';
	if(periodidx!=null) {
		if(periodsarr[periodidx][0].sortfunc(periodsarr[periodidx][0],periodsarr[periodidx][1]))
			str = 'From: '+periodsarr[periodidx][0].dateFormat(format)+"\tTo: "+periodsarr[periodidx][1].dateFormat(format);
		else
			str = periodsarr[periodidx][0].dateFormat(format);
		
		if(tformat!=null && tformat!='')
			str += tconnector + periodsarr[periodidx][0].dateFormat(tformat);
	} else
		for (var i=0; i<periodsarr.length; i++) {
			if(periodsarr[i][0].sortfunc(periodsarr[i][0],periodsarr[i][1])) 
				str += 'From: '+periodsarr[i][0].dateFormat(format)+"\tTo: "+periodsarr[i][1].dateFormat(format);
			else
				str += periodsarr[i][0].dateFormat(format);
			
			if(tformat!=null && tformat!='')
				str += tconnector + periodsarr[i][0].dateFormat(tformat)+"\n";
		}
	
	return str;
}

function parse_periods_arrs(periodsarr,format,tformat,periodidx) {
	var arr=Array();
	if(periodidx!=null) {
//		if(periodsarr[periodidx][0].sortfunc(periodsarr[periodidx][0],periodsarr[periodidx][1])) {
			arr.push(periodsarr[periodidx][0].dateFormat(format));
			arr.push(periodsarr[periodidx][1].dateFormat(format));
			arr.push(periodsarr[periodidx][0].dateFormat(tformat));
	} else
		for (var i=0; i<periodsarr.length; i++)
//			if(periodsarr[i][0].sortfunc(periodsarr[i][0],periodsarr[i][1])) {
				arr.push(new Array(periodsarr[i][0].dateFormat(format),periodsarr[i][1].dateFormat(format),periodsarr[i][0].dateFormat(tformat)));
//			} else
//				arr.push(new Array(periodsarr[i][0].dateFormat(format)));
	
	return arr;
}

