/****************************************************
 *													*
 *					Date functions					*
 *													*
 ****************************************************/

// Global Variables
var date_format='yyyy-MM-dd';//MySQL default
var date_order='yMd';
var date_seperator='-';

 
/**
 * Take the day, month, year
 * Formats the response based on the set date format
 */
function format_date(d, m, y) {
	 var mDig=(date_format.lastIndexOf('M')-date_format.indexOf('M'))+1;
	 var dDig=(date_format.lastIndexOf('d')-date_format.indexOf('d'))+1;
	 var yDig=(date_format.lastIndexOf('y')-date_format.indexOf('y'))+1;
	 
	 var m2 = '00' + m;
	 m2 = m2.substr(m2.length - mDig);
	 var d2 = '00' + d;
	 d2 = d2.substr(d2.length - dDig);
	 var y2=''+y;
	 y2=y2.substr(y2.length - yDig);

	var s=date_seperator;
	var o=date_order;
	var returndate='';

	for(var i=0; i<o.length; i++){
		var ch=o.substring(i,i+1);
		if(ch=='y'){
			returndate+=(s+y2);	
		}
		else if(ch=='M'){
			returndate+=(s+m2);	
		}
		else{
			returndate+=(s+d2);	
		}
	}
	var r2=returndate.substring(returndate.length-date_format.length);
	return returndate.substring(returndate.length-date_format.length);

}

function format_dateObject(date){
	return format_date(date.getDate(), date.getMonth() + 1, date.getFullYear());
}

/**
 * Takes a date as a String
 * Parses the date based on the set date format
 * Returns a Date Object
 */
function makeDate(d){
	 var m1=date_format.indexOf('M');
	 var m2=date_format.lastIndexOf('M')+1;
	 var d1=date_format.indexOf('d');
	 var d2=date_format.lastIndexOf('d')+1;
	 var y1=date_format.indexOf('y');
	 var y2=date_format.lastIndexOf('y')+1;
	 
	 var month=d.substring(m1,m2);
	 var day=d.substring(d1,d2);
	 var year = '20' + d.substring(y1,y2);
		  year = year.substr(year.length - 4);
	 
	 var ped=new Date();
	 ped.setFullYear(year,((month*1)-1),day);
	 return ped;
}
/**
 * Takes a date as a String and a format
 * Parses the date based on the passed in format
 * Returns a Date Object
 */
function makeDateF(d,format){
	 var m1=format.indexOf('M');
	 var m2=format.lastIndexOf('M')+1;
	 var d1=format.indexOf('d');
	 var d2=format.lastIndexOf('d')+1;
	 var y1=format.indexOf('y');
	 var y2=format.lastIndexOf('y')+1;
	 
	 var month=d.substring(m1,m2);
	 var day=d.substring(d1,d2);
	 var year = '20' + d.substring(y1,y2);
		  year = year.substr(year.length - 4);
	 
	 var ped=new Date();
	 ped.setFullYear(year,((month*1)-1),day);
	 return ped;
}

// Sets the date format
function setDateFormat(f){
	date_format=f;
	date_order=getOrder();
	date_seperator=getSeperator();
}

// Gets the character used to seperate the day, month, and year in a date
function getSeperator(){
	for(var i=0; i<date_format.length; i++){
		var ch=date_format.substring(i,i+1);
		if(ch!='y'&&ch!='M'&&ch!='D'){
			return ch;
		}
	}
}

// Returns which order the day, month, year are in
function getOrder(){
	var m=date_format.indexOf('M');
	var d=date_format.indexOf('d');
	var y=date_format.indexOf('y');
	
	if(y<m && y<d){
		if(m<d){
			return 'yMd';
		}
		else {
			return 'ydM';
		}
	}
	else if(m<y && m<d){
		if(y<d){
			return 'Myd';
		}
		else {
			return 'Mdy';
		}
	}
	else {
		if(m<y){
			return 'dMy';
		}
		else {
			return 'dyM';
		}
	}
}

/*
 * Takes a day, month, year
 * Returns whether or not this is in the past
 */
function past(d,m,y){
	 var today = new Date();
	 var pd=new Date();
	 pd.setFullYear(y,((m*1)-1),d);
	 return ((pd<today) ? true : false);
}

//Takes a day month and year and dates a Date object
function aDate(year,month,day){
	var dt=new Date();
	dt.setFullYear(year,((month*1)-1),day);
	return dt;
}

function isValidDate(theDate){
	var objRegExp = /^\d{2}(\-)\d{2}(\-)\d{4}$/
	
	//check to see if in correct format
	if(!objRegExp.test(theDate)){
		return false; //doesn't match pattern, bad date
	}
	else {
		var date_array = theDate.split('-');
		var month = date_array[0]*1 - 1;
		var day = date_array[1];
		var year = date_array[2];
	
		var source_date = new Date(year,month,day);
	
		if(year != source_date.getFullYear()){
			return false;
		}
		if(month != source_date.getMonth()){
			return false;
		}
	
		if(day != source_date.getDate()){
			return false;
		}
	}
	return true;
}

function equalDates(date1,date2){
	return (date1.getFullYear()==date2.getFullYear()&&date1.getMonth()==date2.getMonth()&&date1.getDate()==date2.getDate())?true:false;
}
