var dtCh= "/";

function update_fecha_out()
	{
	update_anyos();
	if (document.frm.cm_noches.value && isDate(document.frm.cm_mesini.value+"/"+document.frm.cm_diaini.value+"/"+document.frm.anoini.value))
		{
		fout=new Date(Date.UTC(parseInt(document.frm.anoini.value), parseInt(document.frm.cm_mesini.value, 10)-1, parseInt(document.frm.cm_diaini.value, 10))+parseInt(document.frm.cm_noches.value, 10)*24*60*60*1000);
		//fout = new Date(document.frm.anoini.value + '/' + parseInt(document.frm.cm_mesini.value, 10) + '/' + document.frm.cm_diaini.value);
		//fout = fout.setTime(parseInt(fout.getTime()+parseInt(document.frm.cm_noches.value*24*60*60*1000)));
		//fout = new Date(fout);
		for (i=0; i < document.frm.cm_diafin.length; i++)
			{
			if (parseInt(document.frm.cm_diafin.options[i].value, 10)==fout.getDate()) 
			{
				document.frm.cm_diafin.options[i].selected=true;
			}
			}
		for (i=0; i < document.frm.cm_mesfin.length; i++)
			{
			if (parseInt(document.frm.cm_mesfin.options[i].value, 10)==fout.getMonth()+1) 
			{
				document.frm.cm_mesfin.options[i].selected=true;
			}
			}
		}
	update_anyos();
	}
	
function update_cm_noches()
	{
	cambio=false;
	update_anyos();
	if (isDate(document.frm.cm_mesfin.value+"/"+document.frm.cm_diafin.value+"/"+document.frm.anofin.value) && isDate(document.frm.cm_mesini.value+"/"+document.frm.cm_diaini.value+"/"+document.frm.anoini.value))
		{
		noches=(Date.UTC(parseInt(document.frm.anofin.value), parseInt(document.frm.cm_mesfin.value, 10)-1, parseInt(document.frm.cm_diafin.value, 10))-Date.UTC(parseInt(document.frm.anoini.value), parseInt(document.frm.cm_mesini.value, 10)-1, parseInt(document.frm.cm_diaini.value, 10)))/(24*60*60*1000);
		if (noches>0)
			{
			for (i=0; i < document.frm.cm_noches.length; i++)
				{
				if (parseInt(document.frm.cm_noches.options[i].value, 10)==noches) 
					{
					document.frm.cm_noches.options[i].selected=true;
					cambio=true;
					}
				}
			}
		}
	if (cambio==false) document.frm.cm_noches.options[0].selected=true;
	}


function isInteger(s)
	{
	var i;
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
	}

function stripCharsInBag(s, bag)
	{
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++){
	var c = s.charAt(i);
	if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
	}

function daysInFebruary (year)
	{
  return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}

function DaysArray(n)
	{
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
	}

function isDate(dtStr)
	{
	// Format: mm/dd/yyyy
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth, 10)
	day=parseInt(strDay, 10)
	year=parseInt(strYr, 10)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

function mostrar_wait() {
var theIframe = document.getElementById? document.getElementById('wait'): document.all? document.all['wait']: null;
var theContent = document.getElementById? document.getElementById('contenido'): document.all? document.all['contenido']: null;
if (theIframe) {
	theContent.style.display="none";
	theIframe.style.display="inline";
	theIframe.src="/inicio/waiting.jsp";
}
else alert ("Su navegador no soporta elementos dinámicos, imprescindible para visualizar correctamente la página.");
}

NameDays = new Array(7)
NameDays[1] = " Domingo"
NameDays[2] = " Lunes"
NameDays[3] = " Martes"
NameDays[4] = " Miércoles"
NameDays[5] = " Jueves"
NameDays[6] = " Viernes"
NameDays[7] = " Sábado"

function DiaSemanaLetra(oneDate) {
	var DiaActual = NameDays[oneDate.getDay() +1];
	return DiaActual;
}
