
/////////////////////////////////////////////////////////////////
//	Autor: Tomeu
//	Ultima revision: 07/03/2003
//	Ultimo en revisar: Joan Fullana
//	Descripcion:1.He anyadido la posibilidad de tener opcion null para 
//				 dias y meses. He anyadido la posibilidad de especificar
//				 funciones para el evento onChange del listbox de dia y mes
//				2.Opcion de de manipular anyos.
//				3.Incorporar mètode per retornar dia de sa 7mana.
/////////////////////////////////////////////////////////////////


function dateObject(name)
{
	this.name = name;
	this.day = null;
	this.month = null;
	this.year = null;

	this.daysObject = null;
	this.monthsObject = null;
	this.yearsObject = null;
	
	this.dayNullAble = false;
	this.dayNullText;
	this.dayNullValue;
	
	this.monthNullAble;
	this.monthNullText;
	this.monthNullValue;
	
	this.changed= false;
	// Metodos
	
	this.load = dte_load;
	this.add = dte_add;
	this.monthText = dte_monthText;
	this.takeDate = dte_takeDate;
	this.dayOfWeek = dte_dayOfWeek;
	this.getDate = dte_getDate;

	this.onChangeDay = null;
	this.onChangeMonth = null;
	this.onChangeFunction = null;
	
	// Inicializacion

	var today = new Date();
	this.tday = today.getDate();
	this.tmonth = today.getMonth()+1;
	this.tyear = today.getYear();
	this.yearsFrom = today.getYear();
	this.yearsTo = today.getYear();

	return;	
}

	
function dte_load()
{
	var maxDays = 31;
	if (this.monthsObject!=null)
	{
		var indice = 0;
		if (this.monthNullAble)
		{
			this.monthsObject.options[indice] = new Option(this.monthNullText, this.monthNullValue)
			indice++;
		}
		for (var i=1;i<=12;i++)
		{
			var name="";
			switch (i)
			{
				case 1: name= "Enero"; break;
				case 2: name= "Febrero"; if(i==this.month) maxDays = (this.year%4==0) ? 29 : 28; break;
				case 3: name= "Marzo"; break;
				case 4: name= "Abril"; if(i==this.month) maxDays = 30; break;
				case 5: name= "Mayo"; break;
				case 6: name = "Junio"; if(i==this.month) maxDays = 30; break;
				case 7: name = "Julio"; break;
				case 8: name = "Agosto"; break;
				case 9: name = "Septiembre"; if(i==this.month) maxDays = 30; break;
				case 10: name = "Octubre"; break;
				case 11: name = "Noviembre"; if(i==this.month) maxDays = 30; break;
				case 12: name = "Diciembre";
			}
			this.monthsObject.options[indice] = new Option(name,i);
			indice++;
		}
		
		if (this.monthNullAble)
			this.monthsObject.selectedIndex=this.month;
		else
			this.monthsObject.selectedIndex=this.month-1;
		if (this.onChangeFunction==null)
			 this.monthsObject.onchange = new Function("dte_changeMonth('" + this.name + "');");
		else
			 this.monthsObject.onchange = new Function("dte_changeMonth('" + this.name + "');" + this.onChangeFunction + ";"); 
				
	}
	
	if (this.daysObject!=null)
	{
		var indice = 0;
		if (this.dayNullAble)
		{
			this.daysObject.options[indice] = new Option(this.dayNullText, this.dayNullValue);
			indice++;
		}

		for (var i=1; i<=maxDays; i++)
		{
			this.daysObject.options[indice] = new Option((String(i).length==1) ? "0"+i: i,i);
			indice++;			
		}
		
		if (this.dayNullAble)
			this.daysObject.selectedIndex=this.day;
		else
			this.daysObject.selectedIndex=this.day-1;
		if (this.onChangeFunction==null)
			this.daysObject.onchange = new Function("dte_changeDay('" + this.name + "');");
		else
			this.daysObject.onchange = new Function("dte_changeDay('" + this.name + "');" + this.onChangeFunction + ";");	
				
	}
	if (this.yearsObject!=null)
	{
		for (var i=0;i<=(this.yearsTo-this.yearsFrom);i++){
			this.yearsObject.options[i] = new Option(this.yearsFrom+i,this.yearsFrom+i);
			if ((this.yearsFrom+i)==this.year){
				this.yearsObject.selectedIndex=i
			}
		}
		this.yearsObject.onchange = new Function("dte_changeYear('" + this.name + "')");
	}
		
	return;
}


function dte_changeMonth(nameObject)
{
	thisObject = eval(nameObject)
	thisObject.month = thisObject.monthsObject.options[thisObject.monthsObject.selectedIndex].value;
	thisObject.changed=true;
	dte_changeYear(nameObject);
	
	if (thisObject.daysObject!=null)
	{
		var maxDays = 31;
		switch (String(thisObject.monthsObject.options[thisObject.monthsObject.selectedIndex].value))
		{
			case "2": maxDays = (thisObject.year%4==0) ? 29 : 28; break;
			case "4": case "6": case "9": case "11": maxDays = 30; break;
			case thisObject.monthNullValue: maxDays = 31; break;
		}

		if (thisObject.dayNullAble)
		{
			var tope = 29;
			var indice = 28;
		}
		else
		{
			var tope = 28;
			var indice = 27;
		}

		thisObject.daysObject.options.length = tope;

		for (var i=28;i<=maxDays;i++)
		{
			thisObject.daysObject.options[indice] = new Option((String(i).length==1) ? "0"+i: i, i);
			indice++;
		}
		
		if (thisObject.dayNullAble)
		{
			if (thisObject.day > maxDays)
			{
				thisObject.daysObject.selectedIndex= maxDays;
				thisObject.day = thisObject.daysObject.options[thisObject.daysObject.selectedIndex].value;
			}	
		}
		else
		{			
			if (thisObject.day > thisObject.daysObject.options.length)
				thisObject.daysObject.selectedIndex=thisObject.daysObject.options.length-1;
			else
				thisObject.daysObject.selectedIndex=thisObject.day-1;
				
			thisObject.day = thisObject.daysObject.options[thisObject.daysObject.selectedIndex].value;
		}
	}
	
	if (thisObject.onChangeMonth != null)
		thisObject.onChangeMonth();
	
	return;
}
	
	
function dte_changeDay(nameObject)
{
	thisObject = eval(nameObject)
	thisObject.day = thisObject.daysObject.options[thisObject.daysObject.selectedIndex].value;
	thisObject.changed=true;
	dte_changeYear(nameObject);

	if (thisObject.onChangeDay != null)
		thisObject.onChangeDay();

	return;
}


function dte_changeYear(nameObject)
{
	var Obj = eval(nameObject)
	if (Obj.yearsObject==null)
	{
		if ((Obj.month < Obj.tmonth))
			{
				if ((Obj.month == Obj.tmonth) && (Obj.day > Obj.tday))
					Obj.year = Obj.tyear;
				else
					Obj.year = Obj.tyear + 1;
			}
		else
			Obj.year = Obj.tyear;	
	}
	else
		Obj.year = Obj.yearsObject.options[Obj.yearsObject.selectedIndex].value;
	return;
}


function dte_add(numDays){
	this.day = parseInt(this.day) + numDays;
	var maxDays = 31;
	switch (this.month.toString())
		{
		case "2": maxDays = (this.year%4==0) ? 29 : 28; break;
		case "4": case "6": case "9": case "11": maxDays = 30;
		}
	if (this.day>maxDays)
		{
		this.month = parseInt(this.month) + 1;
		this.day = parseInt(this.day) - parseInt(maxDays);
		if (this.month==13)
			{
			this.month = 1;
			this.year = parseInt(this.year) + 1;
			}
		}
	return;
}

	
function dte_takeDate(format){
	//Modificat: J.Fullana (09/02/2004)
	
	//format --> (1) dd/mm/yyyy  
	//format --> (2) yyyymmdd  
	//format --> (3) dd-mmm-yyy
	  
	switch (format){
	case 1:
		return dosDigits(this.day) +"/"+ dosDigits(this.month) +"/"+ this.year
	case 2:
		return this.year + dosDigits(this.month) + dosDigits(this.day);
	case 3:
		return dosDigits(this.day) +"-"+ getNombreMes(parseInt(this.month),2) +"-"+ this.year
	}
	return;
}

function dte_monthText(){
	return getNombreMes(parseInt(this.month),1)
}

function dte_dayOfWeek(){
	//***********
	// modifiat: 07/02/2003 - J. Fullana
	// retorna: DG -> 1
	//			DL -> 2
	//			DM -> 3
	//			DX -> 4
	//			DJ -> 5
	//			DV -> 6
	//			DS -> 7
	//***********
	
	var data = new Date(this.year,(this.month-1)%12,this.day)

	return data.getDay()+1;
}

function dte_getDate(){
	return new Date(this.year,this.month-1,this.day)
}

//function que retorna un String de 2 digits
//creat: J.Fullana (09/02/2004)
function dosDigits(num){
	var str = num.toString();
	switch (str.length){
	case 1:
		return "0"+ str;
	case 2: 
		return str;
	default:
		return str.substring(0,2);
	}
}


//retorna el nom del mes en diferents formats
//creat: J.Fullana (09/02/2004)
function getNombreMes(mes,format){
	//format --> (1) "Enero, Febrero, ..."
	//format --> (2) "Jan, Feb, ..."
	switch(format){
	case 1:
		switch (mes){
		case 1: return "Enero"; 
		case 2: return "Febrero";
		case 3: return "Marzo"; 
		case 4: return "Abril";
		case 5: return "Mayo"; 
		case 6: return "Junio";
		case 7: return "Julio"; 
		case 8: return "Agosto"; 
		case 9: return "Septiembre";
		case 10: return "Octubre"; 
		case 11: return "Noviembre";
		case 12: return "Diciembre";
		}
	case 2:
		switch (mes){
		case 1: return "Jan"; 
		case 2: return "Feb";
		case 3: return "Mar"; 
		case 4: return "Apr";
		case 5: return "May"; 
		case 6: return "Jun";
		case 7: return "Jul"; 
		case 8: return "Aug"; 
		case 9: return "Sep";
		case 10: return "Oct"; 
		case 11: return "Nov";
		case 12: return "Dec";
		}
	}
}
