/*Funcion que actualiza la fs a partir del nš de noches*/
var mseg1Noche=1000*60*60*24;

//la funcion "IsUnsignedInteger(YourNumber)" chequea si "YourNumber" es un numero entero sin signo valido
//La variable "YourNumber" es una cadena de caracteres
function IsUnsignedInteger(YourNumber) {
	var Template = /^d+$/ //Formato de numero entero sin signo
	return (Template.test(YourNumber)) ? 1 : 0 //Compara "YourNumber" con el formato "Template" y si coincidevuelve verdadero si no devuelve falso
}





function isLeapYear (Year) {
	if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
		return true;
	} else {
		return false;
   }
}





//function isLeapYear(YourYear) {
//	if (IsUnsignedInteger(YourYear)) {
//		return ((YourYear % 4 == 0 && YourYear % 100 != 0) || (YourYear % 400 == 0)) ? 1 : 0// Si "YourYear" es un aņo es bisiesto devuelve verdadero si no devuelve falso
//	} else return 0 //Si "YourYear" no es un numero entero sin signo valido devuelve falso
//}

function getDaysInMonth(month,ani) {
 var days;
 if ((month==0)||(month==2)||(month==4)||(month==6)||(month==7)||(month==9)||(month==11)) {
  days=31;
 } else if ((month==3) || (month==5) || (month==8) || (month==10)) {
  days=30;
 } else if (month==1) { 
 	var a=isLeapYear(ani);
  	if (a) {
   		days=29;
  	} else {
   		days=28;
  	}
 }
 return (days);
}

function actualizar_Fechas(form){
    if ( !isNaN(parseInt(form.day_in.value,10)) && !isNaN(parseInt(form.month_in.value,10)) && !isNaN(parseInt(form.year_in.value,10)) && !isNaN(parseInt(form.noches.options[form.noches.selectedIndex].text,10)) ){

	var dia= parseInt(form.day_in.value,10);
	var mes= parseInt(form.month_in.value,10);
	var any= parseInt(form.year_in.value,10);
	dia= dia + parseInt(form.noches.options[form.noches.selectedIndex].text,10);
	if (dia > getDaysInMonth(mes-1,any)){
		dia = dia - getDaysInMonth(mes-1,any);
		mes ++;
		if (mes > 12){
			mes = mes - 12;
			any ++;
		}
	}
	
	form.day_out.value = (dia*1)>9?dia:("0"+dia);
	form.month_out.value =(mes*1)>9?mes:("0"+mes);
	form.year_out.value = any.toString(10);
	form.tdt.value=dia+"/"+mes+"/"+any.toString(10);
	}
	return;
}

/*Funcion que actualiza el nš de noches a partir de la fE y la fS*/
function actualizar_Noches(form){
	if ( !isNaN(parseInt(form.day_out.value,10)) && !isNaN(parseInt(form.month_out.value,10)) && !isNaN(parseInt(form.year_out.value,10)) && !isNaN(parseInt(form.day_in.value,10)) && !isNaN(parseInt(form.month_in.value,10)) && !isNaN(parseInt(form.year_in.value,10))){

		var diaE= parseInt(form.day_in.value,10);
		var mesE= parseInt(form.month_in.value,10);
		var anyE= parseInt(form.year_in.value,10);
		var diaS= parseInt(form.day_out.value,10);
		var mesS= parseInt(form.month_out.value,10);
		var anyS= parseInt(form.year_out.value,10);
		var nits;

		if ( ( (anyS>anyE) || (anyS==anyE && mesS>mesE) ) || ((anyS==anyE && mesS==mesE) && diaS>diaE) ){
			if (diaE==diaS && mesE==mesS && anyE==anyS) {
				nits=0;
			} else {
				nits=0;
				while (diaE!=diaS || mesE!=mesS || anyE!=anyS){
					diaE++; 
					nits++;
					if (diaE > getDaysInMonth(mesE-1,anyE)){
						diaE = diaE - getDaysInMonth(mesE-1,anyE);
						mesE ++;
						if (mesE > 12){
							mesE = mesE - 12;
							anyE ++;
						}
					}
				}//end while
			}
		} else //fs es anterior a fe!
			nits=0;

		if (nits > 0 && nits < form.noches.length)
			form.noches.selectedIndex = nits;
		else
			form.noches.selectedIndex = 0;
	}
	return nits;
}
