// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function formatDateForDB (unformattedDate) {
	var arrDate = unformattedDate.split("/");
	dbDate = arrDate[2] + '-' + arrDate[1] + '-' + arrDate[0];
	return dbDate;
}

function getRecurrency () {
	var now = new Date();
	var year = now.getUTCFullYear();
	switch($('frm_recurrency').getValue()) {
		case 'Kies periode':
			
			var timespan = '';
		break;
		case 'weekly':
			var timespan = 'week ' + getWeek(year, now.getMonth(), now.getDate());
		break;
		case 'monthly':
			var month=new Array(12);
			month[0] = 'januari';
			month[1] = 'februari';
			month[2] = 'maart';
			month[3] = 'april';
			month[4] = 'mei';
			month[5] = 'juni';
			month[6] = 'juli';
			month[7] = 'augustus';
			month[8] = 'september';
			month[9] = 'oktober';
			month[10] = 'november';
			month[11] = 'december';
			var timespan = month[now.getMonth()] + ' ' + year;
		break;
		case 'quarterly':
			if (now.getMonth() >= 0 && now.getMonth() <= 2) { 
				var timespan = '1e kwartaal ' + year;  
			} else if (now.getMonth() >= 3 && now.getMonth() <= 5) {
				var timespan = '2e kwartaal ' + year;
			} else if (now.getMonth() >= 6 && now.getMonth() <= 8) {
				var timespan = '3e kwartaal ' + year;
			} else if (now.getMonth() >= 9 && now.getMonth() <= 11) {
				var timespan = '4e kwartaal ' + year;
			}
		break;
		case 'yearly':
			var timespan = year;
		break;
	}
	$('recurrency').update(timespan);
}


// Calculate weeknumber
// From:http://www.codeproject.com/KB/cs/gregorianwknum.aspx
// Parameters year (full, i.e. 2008), month (0 to 11), day (day of the month)

function getWeek(year,month,day){
    month += 1;
    var a = Math.floor((14-(month))/12);
    var y = year+4800-a;
    var m = (month)+(12*a)-3;
    var jd = day + Math.floor(((153*m)+2)/5) + (365*y) + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400) - 32045;
    var d4 = (jd+31741-(jd%7))%146097%36524%1461;
    var L = Math.floor(d4/1460);
    var d1 = ((d4-L)%365)+L;
    NumberOfWeek = Math.floor(d1/7) + 1;
    return NumberOfWeek;        
}