function Calcular(EDFform){
	var retorno;
	retorno=FechaMasDias(EDFform)
	if(retorno==false){
		alert("Introduzca fecha válida");
		return;
	}
}
	
	

function FechaMasDias(EDFform){

	
	var dia=EDFform.day.options[EDFform.day.selectedIndex].value;
	var mes=EDFform.month.options[EDFform.month.selectedIndex].value;
	var anno=EDFform.year.value;
	var x =EDFform.x.options[EDFform.x.selectedIndex].value;
	var maxdia;

	dia  = 	eval(dia);
	mes  = 	eval(mes);
	anno = 	eval(anno);
	x =	eval(x);

	if (mes == 2){
		if ((anno%4)==0){
			maxdia = 29;
		}else{
			maxdia = 28;
		}
	}

	if (mes==1||mes==3||mes==5||mes==7||mes==8||mes==10||mes==12)
		maxdia = 31;
	if (mes==4||mes==6||mes==9||mes==11)
		maxdia = 30;
	if (dia>maxdia) {
			return false;
			}

		
		var diamf = dia + x - 14;
		var intinf=dia + x - 17;
		var intsup=dia + x - 11;
	
		fecha1=Calc(maxdia,mes,anno,diamf);
		fecha2=Calc(maxdia,mes,anno,intinf);
		fecha3=Calc(maxdia,mes,anno,intsup);
	
		
		document.EDFform.fecha1.value=fecha1;	
		document.EDFform.fecha2.value=fecha2;	
		document.EDFform.fecha3.value=fecha3;		
		
}	
	
	
function Calc(maxdia,mes,anno,date){
	if (date > maxdia){
		date = date - maxdia;
		if (mes == 12){
			anno = anno+1;
			mes = 1;
		}else{
			mes = mes + 1;
		}
		
	}

	if (date < 10) date = "0" + date;
	if (mes < 10) mes = "0"+ mes;
	fecha= date + "/" + mes + "/" + anno;
	return fecha;
}	
	

function CalcCycle(theForm) {

//Form verifying:
alert();
	var year1 = theForm.year1.options[theForm.year1.selectedIndex].value;
	var year2 = theForm.year2.options[theForm.year2.selectedIndex].value;

	/*if (year1  == "")  {
	           alert ("El año esta en blanco");
	        return;
	}

	if (year1 < 1000){
	           alert ("Inténtelo de nuevo\n\nIntroduzca los cuatro dígitos del año");
	        return;
	}
	if (year2 == "")  {
	           alert ("Inténtalo de nuevo\n\nEl año no ha sido introducido!");
	        return;
	}
	if (("" + parseInt(year2)) != year2)  {
	           alert ("Inténtalo de nuevo\n\nDebe escribir el año en números");
	        return;
	}
	if (year2 < 1000){
	           alert ("Inténtalo de nuevo\n\nEscribe los 4 números del año");
	        return;
	}*/


//Grab, format and calculate

	if (!(ValidateCycleDate(theForm.month1.options[theForm.month1.selectedIndex].value,year1,theForm.date1.options[theForm.date1.selectedIndex].value))) {
		alert("¡La fecha para el primer período no es válida!");
		return;
	}

	if (!(ValidateCycleDate(theForm.month2.options[theForm.month2.selectedIndex].value,year2,theForm.date2.options[theForm.date2.selectedIndex].value))) {
		alert("¡La fecha para el segundo período no es válida!");
		return;
	}
	Period1 =  Date.UTC(year1, theForm.month1.options[theForm.month1.selectedIndex].value, theForm.date1.options[theForm.date1.selectedIndex].value);

	Period2 =  Date.UTC(year2, theForm.month2.options[theForm.month2.selectedIndex].value, theForm.date2.options[theForm.date2.selectedIndex].value);

	CycleMS = (Period1 - Period2);

	if (Period1 > Period2){
			DayFormattedCycle = CycleMS / 86400000

				if ( DayFormattedCycle < 21 || DayFormattedCycle > 35 ) {
		   	   	   alert ("El resultado es" + DayFormattedCycle + " días. Está fuera del rango de días esperado (21-35).");
				}

			theForm.cycle.value = DayFormattedCycle
			}

			else {
			//updated to reflect correct dates
	      alert ("La primera fecha debe ser más reciente que la segunda.");
	      return;
		}

	}




function ValidateCycleDate(month,year,day) {
	var lmonth,lyear,lday;

	lmonth = parseInt(" " + month);
	lyear = parseInt(" " + year);
	lday = parseInt(" " + day);
	ldays = DaysInMonth(lmonth,lyear)

	if (lday > ldays) {
		return false;
	} else {
		return true;
	}
}


function DaysInMonth(month,year) {
	var days;

	switch (month) {

		case 1:
			days=31;
			break;

		case 3:
			days=31;
			break;

		case 5:
			days=31;
			break;

		case 7:
			days=31;
			break;

		case 8:
			days=31;
			break;

		case 10:
			days=31;
			break;

		case 12:
			days=31;
			break;

		case 4:
			days=30;
			break;
		case 6:
			days=30;
			break;
		case 9:
			days=30;
			break;
		case 11:
			days=30;
			break;
		case 2:

			if (IsLeapYear(year)) {
				days=29;
			}else{
				days=28;
			}
	}



	return days;


}

function IsLeapYear(lyear)  {

	if ( (lyear%4 == 0 && lyear%100 != 0) || (lyear%400 == 0) ) {
		return true;
	} else {
		return false;
	}
}
