months_31 = new Array();
months_31[0] = 1
months_31[1] = 3
months_31[2] = 5
months_31[3] = 7
months_31[4] = 8
months_31[5] = 10
months_31[6] = 12

months_30 = new Array();
months_30[0] = 4
months_30[1] = 6
months_30[2] = 9
months_30[3] = 11

var bisextil_days = 29;

function febDays(y){
	if ((y%4) == 0) bisextil_days = 29;
	else bisextil_days = 28;
	return bisextil_days;
}

function IsBisextil(obj,id,type){
	febDays(obj.value);
	myFormElement = obj.form["month_" + id];
	if(myFormElement.value == 2) daysInMonth(myFormElement,id,type);
	else setDateForm(obj,type);
}

function daysInMonth(obj,id,type){
	var nb_days = 0;
	var myMonth = obj.value;
	for (i=0; i<months_31.length ; i++){
		if (months_31[i]==myMonth) nb_days=31;
	}
	if (!nb_days){
		for (i=0; i<months_30.length ; i++){
			if (months_30[i]==myMonth) nb_days=30;
		}
	}
	if (!nb_days) nb_days = febDays(obj.form["year_" + id].value);

	var myFormElement = obj.form["day_" + id];

	try{
		var check = myFormElement.value;
		myFormElement.options.length = 0;
		for (i=0;i<nb_days;i++){
			myFormElement.options.length = i;
			if(i < 9) tmpVal = "0" + (i+1);
			else tmpVal = (i+1);
			
			myFormElement.options[i] = new Option(tmpVal);
			myFormElement.options[i].value = tmpVal;
		}
		if (myFormElement.options[check-1]) myFormElement.options[check-1].selected = true;
		else myFormElement.options[i-1].selected = true;
	}
	catch(e){}
	setDateForm(obj,type);
}
function setDateForm(obj,type){
	dt = obj.parentNode.childNodes[2].value + "-" + obj.parentNode.childNodes[1].value + "-" + obj.parentNode.childNodes[0].value + " " + obj.parentNode.childNodes[4].value + ":" + obj.parentNode.childNodes[5].value + ":" + obj.parentNode.childNodes[6].value;
	if (type != "string") dt = "{ts '" + dt + "'}";
	obj.parentNode.childNodes[7].value = dt;
}
function setDateFieldTo(obj,fld,dat){
	oYears = obj.form["year_" + fld].options;
	for(var i=0;i<oYears.length;i++){
		if(oYears[i].value == dat.getFullYear()){
			oYears[i].selected = true;
			break;
		}
	}
	obj.form["month_" + fld].options[dat.getMonth()].selected = true;
	obj.form["day_" + fld].options[dat.getDate()-1].selected = true;
	setDateForm(obj,'date');
}
function DateAdd(timeU,byMany,dateObj) {
	var millisecond=1;
	var second=millisecond*1000;
	var minute=second*60;
	var hour=minute*60;
	var day=hour*24;
	var year=day*365;
	
	var newDate;
	var dVal=dateObj.valueOf();
	switch(timeU) {
		case "ms": newDate=new Date(dVal+millisecond*byMany); break;
		case "s": newDate=new Date(dVal+second*byMany); break;
		case "mi": newDate=new Date(dVal+minute*byMany); break;
		case "h": newDate=new Date(dVal+hour*byMany); break;
		case "d": newDate=new Date(dVal+day*byMany); break;
		case "y": newDate=new Date(dVal+year*byMany); break;
	}
	return newDate;
}
function DateDiff(p_Interval, dt1, dt2){
	var iDiffMS = dt2.valueOf() - dt1.valueOf();
	var dtDiff = new Date(iDiffMS);


	var nYears  = dt2.getUTCFullYear() - dt1.getUTCFullYear();
	var nMonths = dt2.getUTCMonth() - dt1.getUTCMonth() + (nYears!=0 ? nYears*12 : 0);
	var nQuarters = parseInt(nMonths/3);
	
	var nMilliseconds = iDiffMS;
	var nSeconds = parseInt(iDiffMS/1000);
	var nMinutes = parseInt(nSeconds/60);
	var nHours = parseInt(nMinutes/60);
	var nDays  = parseInt(nHours/24);
	var nWeeks = parseInt(nDays/7);

	var iDiff = 0;		
	switch(p_Interval.toLowerCase()){
		case "yyyy": return nYears;
		case "q": return nQuarters;
		case "m": return nMonths;
		case "y": 		
		case "d": return nDays;
		case "w": return nDays;
		case "ww":return nWeeks;		
		case "h": return nHours;
		case "n": return nMinutes;
		case "s": return nSeconds;
		case "ms":return nMilliseconds;
		default: return "invalid interval: '" + p_Interval + "'";
	}
}