/*  Copyright Mihai Bazon, 2002, 2003  |  http://dynarch.com/mishoo/
 * ---------------------------------------------------------------------------
 *
 * The DHTML Calendar
 *
 * Details and latest version at:
 * http://dynarch.com/mishoo/calendar.epl
 *
 * This script is distributed under the GNU Lesser General Public License.
 * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html
 *
 * This file defines helper functions for setting up the calendar.  They are
 * intended to help non-programmers get a working calendar on their site
 * quickly.  This script should not be seen as part of the calendar.  It just
 * shows you what one can do with the calendar, while in the same time
 * providing a quick and simple method for setting it up.  If you need
 * exhaustive customization of the calendar creation process feel free to
 * modify this code to suit your needs (this is recommended and much better
 * than modifying calendar.js itself).
 */

// $Id: calendar-setup.js,v 1.25 2005/03/07 09:51:33 mishoo Exp $

Calendar.setup = function (params) {
	function param_default(pname, def) { if (typeof params[pname] == "undefined") { params[pname] = def; } };

	param_default("inputField",     null);
	param_default("displayArea",    null);
	param_default("button",         null);
	param_default("eventName",      "click");
	param_default("ifFormat",       "%Y/%m/%d");
	param_default("daFormat",       "%Y/%m/%d");
	param_default("singleClick",    true);
	param_default("disableFunc",    null);
	param_default("dateStatusFunc", params["disableFunc"]);	// takes precedence if both are defined
	param_default("dateText",       null);
	param_default("firstDay",       null);
	param_default("align",          "Br");
	param_default("range",          [2009, 2009]);
	param_default("weekNumbers",    true);
	param_default("flat",           null);
	param_default("flatCallback",   null);
	param_default("onSelect",       null);
	param_default("onClose",        null);
	param_default("onUpdate",       null);
	param_default("date",           null);
	param_default("showsTime",      false);
	param_default("timeFormat",     "24");
	param_default("electric",       true);
	param_default("step",           2);
	param_default("position",       null);
	param_default("cache",          false);
	param_default("showOthers",     false);
	param_default("multiple",       null);

	var tmp = ["inputField", "displayArea", "button"];
	for (var i in tmp) {
		if (typeof params[tmp[i]] == "string") {
			params[tmp[i]] = document.getElementById(params[tmp[i]]);
		}
	}
	if (!(params.flat || params.multiple || params.inputField || params.displayArea || params.button)) {
		alert("Calendar.setup:\n  Nothing to setup (no fields found).  Please check your code");
		return false;
	}

	function onSelect(cal) {
		var p = cal.params;
		var update = (cal.dateClicked || p.electric);
		if (update && p.inputField) {
			p.inputField.value = cal.date.print(p.ifFormat);
			if (typeof p.inputField.onchange == "function")
				p.inputField.onchange();
		}
		if (update && p.displayArea)
			p.displayArea.innerHTML = cal.date.print(p.daFormat);
		if (update && typeof p.onUpdate == "function")
			p.onUpdate(cal);
		if (update && p.flat) {
			if (typeof p.flatCallback == "function")
				p.flatCallback(cal);
		}
		if (update && p.singleClick && cal.dateClicked)
			cal.callCloseHandler();
	};

	if (params.flat != null) {
		if (typeof params.flat == "string")
			params.flat = document.getElementById(params.flat);
		if (!params.flat) {
			alert("Calendar.setup:\n  Flat specified but can't find parent.");
			return false;
		}
		var cal = new Calendar(params.firstDay, params.date, params.onSelect || onSelect);
		cal.showsOtherMonths = params.showOthers;
		cal.showsTime = params.showsTime;
		cal.time24 = (params.timeFormat == "24");
		cal.params = params;
		cal.weekNumbers = params.weekNumbers;
		cal.setRange(params.range[0], params.range[1]);
		cal.setDateStatusHandler(params.dateStatusFunc);
		cal.getDateText = params.dateText;
		if (params.ifFormat) {
			cal.setDateFormat(params.ifFormat);
		}
		if (params.inputField && typeof params.inputField.value == "string") {
			cal.parseDate(params.inputField.value);
		}
		cal.create(params.flat);
		cal.show();
		return false;
	}

	var triggerEl = params.button || params.displayArea || params.inputField;
	triggerEl["on" + params.eventName] = function() {
		var dateEl = params.inputField || params.displayArea;
		var dateFmt = params.inputField ? params.ifFormat : params.daFormat;
		var mustCreate = false;
		var cal = window.calendar;
		if (dateEl)
			params.date = Date.parseDate(dateEl.value || dateEl.innerHTML, dateFmt);
		if (!(cal && params.cache)) {
			window.calendar = cal = new Calendar(params.firstDay,
							     params.date,
							     params.onSelect || onSelect,
							     params.onClose || function(cal) { cal.hide(); });
			cal.showsTime = params.showsTime;
			cal.time24 = (params.timeFormat == "24");
			cal.weekNumbers = params.weekNumbers;
			mustCreate = true;
		} else {
			if (params.date)
				cal.setDate(params.date);
			cal.hide();
		}
		if (params.multiple) {
			cal.multiple = {};
			for (var i = params.multiple.length; --i >= 0;) {
				var d = params.multiple[i];
				var ds = d.print("%Y%m%d");
				cal.multiple[ds] = d;
			}
		}
		cal.showsOtherMonths = params.showOthers;
		cal.yearStep = params.step;
		cal.setRange(params.range[0], params.range[1]);
		cal.params = params;
		cal.setDateStatusHandler(params.dateStatusFunc);
		cal.getDateText = params.dateText;
		cal.setDateFormat(dateFmt);
		if (mustCreate)
			cal.create();
		cal.refresh();
		if (!params.position)
			cal.showAtElement(params.button || params.displayArea || params.inputField, params.align);
		else
			cal.showAt(params.position[0], params.position[1]);
		return false;
	};

	return cal;
};

  function catcalc(cal) {
	var date = cal.date;
    var time = date.getTime();
	//recojo la fecha inicial 
	var fecha = document.forms[0].finicial.value;
	//La descompongo en un array 
	var array_fecha = fecha.split("/") 
	//separo el mes en una variable
    var semanas = parseInt(document.forms[0].semanas.value);
	var mes_ini; 
//	var destino_curso = document.forms[0].destino.value;
	mes_ini = array_fecha[1]; 
        // use the _other_ field
        var field = document.getElementById("ffinal");
        if (field == cal.params.inputField) {
            field = document.getElementById("finicial");
        	time -= (semanas * Date.WEEK); // quita el nº de semanas a la fecha seleccionada
        var date_i = new Date(time); // alert (date_i.getDay());
		if (date_i.getDay() == 4) time += (4 * Date.DAY); // si el calendario me devuelve un jueves - bug
		else time += (3 * Date.DAY); // adelanta el día al lunes
        } else {
            time += (semanas * Date.WEEK); // añade el nº de semanas a la fecha seleccionada
        var date_f = new Date(time);
		if (date_f.getDay() == 0) time -= (2 * Date.DAY); // si el calendario me devuelve un domingo - bug
		else time -= (3 * Date.DAY); // retrasa el día al viernes
       }
        var date2 = new Date(time);
        field.value = date2.print("%d/%m/%Y");
    }

// this function returns true if the passed date is special
// Indica los lunes que no es posible incorporarse al curso
function lunesOff(year, month, day) {
// Estructura de control para cargar los días de incorporación en el calendario en función del destino.
var destino_curso = document.forms[0].destino.value;
var aloja_curso = document.forms[0].op_aloja.value;
var semanas_curso = parseInt(document.forms[0].semanas.value);
var comidas_aloja = parseInt(document.forms[0].comidas.value);
if (destino_curso == 'bournebraidley' && aloja_curso == 'Residencia Purbeck House') {
	var apaga_mon = {
    6 : [ 6, 13 ]          //    days off in July for Bournemouth Braidley
	};
} else if (destino_curso == 'toronto' && aloja_curso == 'Residencia Woodsworth') {
if (semanas_curso == 3) {
	var apaga_mon = {
    6 : [ 6, 13 ],          //    days off in July for Bournemouth Braidley
	7 : [ 10, 17, 24, 31 ]             // days off in August for Bristol
	};
} else if (semanas_curso == 6) {
	var apaga_mon = {
    6 : [ 6, 20, 27 ],          //    days off in July for Bournemouth Braidley
	7 : [ 3, 10, 17, 24, 31 ]             // days off in August for Bristol
	};
}
} else if (destino_curso == 'bristol' && aloja_curso == 'Residencia') {
if (semanas_curso == 3) {
	var apaga_mon = {
	6 : [ 6 ],             // days off in July for Bristol
	7 : [ 24, 31 ]             // days off in August for Bristol
	};
} else if (semanas_curso == 6) {
	var apaga_mon = {
	6 : [ 6 ],             // days off in July for Bristol
	7 : [ 3, 10, 17, 24, 31 ]             // days off in August for Bristol
	};
}
} else if (destino_curso == 'dublinkildare' && aloja_curso == 'Residencia-apartamentos Archway Court') {
if (semanas_curso == 3) {
	var apaga_mon = {
	7 : [ 31 ]             // days off in August for Bristol
	};
} else if (semanas_curso == 6) {
	var apaga_mon = {
	7 : [ 10, 17, 24, 31 ]             // days off in August for Bristol
	};
}
} else if (destino_curso == 'liverpool' || destino_curso == 'losangeles' && aloja_curso == 'Residencia UCLA' || destino_curso == 'montpellier' && aloja_curso == 'Residencia CUP') {
if (semanas_curso == 3) {
	var apaga_mon = {
	5 : [ 1, 8, 15 ,22 ],             // days off in June
	7 : [ 17, 24, 31 ]             // days off in August
	};
} else if (semanas_curso == 6) {
	var apaga_mon = {
	5 : [ 1, 8, 15 ,22 ],             // days off in June
	6 : [ 27 ],             // days off in July
	7 : [ 3, 10, 17, 24, 31 ]             // days off in August
	};

}
} else if (destino_curso == 'bostonbackbay' && aloja_curso == 'Residencia Suffolk') {
if (semanas_curso == 3) {
	var apaga_mon = {
	5 : [ 1, 8, 15 ,22 ],             // days off in June
	7 : [ 17, 24, 31 ]             // days off in August
	};
} else if (semanas_curso == 6) {
	var apaga_mon = {
	5 : [ 1, 8, 15 ,22 ],             // days off in June
	6 : [ 27 ],             // days off in July
	7 : [ 3, 10, 17, 24, 31 ]             // days off in August
	};

}
} else if (destino_curso == 'nuevayork' && aloja_curso == 'Residencia The New Yorker') {
if (semanas_curso == 3) {
	var apaga_mon = {
	7 : [ 3, 10, 17, 24 ],             // days off in August
   10 : [ 16, 23, 30 ]              // special days in November
	};
} else if (semanas_curso == 6) {
	var apaga_mon = {
	7 : [ 3, 10, 17, 24 ],             // days off in August
    9 : [ 26 ]              // special days in October
	};

}
} else if (destino_curso == 'cork' && aloja_curso == 'Apartamentos') {
if (semanas_curso == 3) {
	var apaga_mon = {
	5 : [ 1, 8 ],             // days off in June
	7 : [ 17, 24, 31 ]             // days off in August
	};
} else if (semanas_curso == 6) {
	var apaga_mon = {
	5 : [ 1, 8 ],             // days off in June
	6 : [ 27 ],             // days off in July
	7 : [ 3, 10, 17, 24, 31 ]             // days off in August
	};

}
} else if (destino_curso == 'oxfordst') {
if (semanas_curso == 3) {
	var apaga_mon = {
	5 : [ 1, 8, 15 ],             // days off in June
	7 : [ 10, 17, 24, 31 ]             // days off in August
	};
} else if (semanas_curso == 6) {
	var apaga_mon = {
	5 : [ 1, 8, 15 ],             // days off in June
	6 : [ 20, 27 ],             // days off in July
	7 : [ 3, 10, 17, 24, 31 ]             // days off in August
	};

}
} else if (destino_curso == 'margate' && aloja_curso == 'Residencia-apartamentos') {
if (semanas_curso == 3) {
	var apaga_mon = {
	5 : [ 1, 8, 15 ,22 ],             // days off in June
	7 : [ 17, 24, 31 ]             // days off in August
	};
} else if (semanas_curso == 6) {
	var apaga_mon = {
	6 : [ 27 ],             // days off in July
	7 : [ 3, 10, 17, 24, 31 ]             // days off in August
	};

}
} else if (destino_curso == 'paris' && comidas_aloja == '14') {
var apaga_mon = {
	5 : [ 29 ],             // days off in June
	6 : [ 6, 13, 20, 27 ],             // days off in July
	7 : [ 3, 10, 17, 24 ]             // days off in August
};
} else {
// Estructura de control para indicar los lunes que no es posible incorporarse al curso en función del nº de semanas.
if (semanas_curso == 3) {
var apaga_mon = {
   10 : [ 16, 23, 30 ]              // special days in November
};
} else if (semanas_curso == 6) {
var apaga_mon = {
   9 : [ 26 ]              // special days in October
};
}

}
    var m = apaga_mon[month];
    if (!m) return false;
    for (var i in m) if (m[i] == day) return true;
    return false;
}

// Indica los viernes que no es posible terminar el curso
function viernesOff(year, month, day) {
// Estructura de control para cargar los días de incorporación en el calendario en función del destino.
var destino_curso = document.forms[0].destino.value;
var aloja_curso = document.forms[0].op_aloja.value;
var semanas_curso = parseInt(document.forms[0].semanas.value);
var comidas_aloja = parseInt(document.forms[0].comidas.value);
if (destino_curso == 'bournebraidley' && aloja_curso == 'Residencia Purbeck House') { 
	var apaga_fri = {
    7 : [ 21, 28 ]          //    days off in August for Bournemouth Braidley
	};
} else if (destino_curso == 'toronto' && aloja_curso == 'Residencia Woodsworth') { 
if (semanas_curso == 3) {
	var apaga_fri = {
    7 : [ 28 ]          //    days off in August for Bournemouth Braidley
	};
} else if (semanas_curso == 6) {
	var apaga_fri = {
    7 : [ 7, 14, 28 ]          //    days off in August for Bournemouth Braidley
	};
}
} else if (destino_curso == 'bristol' && aloja_curso == 'Residencia') { //cerrada del 13/12/2008 al 04/01/2009
if (semanas_curso == 3) {
	var apaga_fri = {
	6 : [ 3, 10, 17, 24 ],             // days off in July for Bristol
    8 : [ 11, 18, 25 ]          //    days off in September for Bristol
	};
} else if (semanas_curso == 6) {
	var apaga_fri = {
	6 : [ 3, 10, 17, 24, 31 ],             // days off in July
    7 : [ 7, 14 ],          //    days off in August for Bristol
    8 : [ 11, 18, 25 ]          //    days off in September for Bristol
	};
}
} else if (destino_curso == 'dublinkildare' && aloja_curso == 'Residencia-apartamentos Archway Court') { //cerrada del 13/12/2008 al 04/01/2009
if (semanas_curso == 3) {
	var apaga_fri = {
	6 : [ 3, 10, 17 ],             // days off in July
    8 : [ 18, 25 ]          //    days off in September
	};
} else if (semanas_curso == 6) {
	var apaga_fri = {
	6 : [ 3, 10, 17, 24, 31 ],             // days off in July
    7 : [ 7 ],          //    days off in August
    8 : [ 18, 25 ]          //    days off in September
	};
}
} else if (destino_curso == 'liverpool' || destino_curso == 'losangeles' && aloja_curso == 'Residencia UCLA' || destino_curso == 'montpellier' && aloja_curso == 'Residencia CUP') { //cerrada del 13/12/2008 al 04/01/2009
if (semanas_curso == 3) {
	var apaga_fri = {
	6 : [ 3, 10 ]             // days off in July
	};
} else if (semanas_curso == 6) {
	var apaga_fri = {
	6 : [ 3, 10, 17, 24, 31 ]             // days off in July
	};
}
} else if (destino_curso == 'bostonbackbay' && aloja_curso == 'Residencia Suffolk') { //cerrada del 13/12/2008 al 04/01/2009
if (semanas_curso == 3) {
	var apaga_fri = {
	6 : [ 3, 10 ]             // days off in July
	};
} else if (semanas_curso == 6) {
	var apaga_fri = {
	6 : [ 3, 10, 17, 24, 31 ]             // days off in July
	};
}
} else if (destino_curso == 'nuevayork' && aloja_curso == 'Residencia The New Yorker') { //cerrada del 13/12/2008 al 04/01/2009
if (semanas_curso == 3) {
	var apaga_fri = {
    8 : [ 4, 11 ]          //    days off in September
	};
} else if (semanas_curso == 6) {
	var apaga_fri = {
    9 : [ 2 ]          //    days off in October
	};
}
} else if (destino_curso == 'toronto' && aloja_curso == 'Residencia CCNM') { //cerrada del 13/12/2008 al 04/01/2009
if (semanas_curso == 3) {
	var apaga_fri = {
    8 : [ 4, 11, 18 ]          //    days off in September
	};
} else if (semanas_curso == 6) {
	var apaga_fri = {
    9 : [ 2, 9 ]          //    days off in October
	};
}
} else if (destino_curso == 'cork' && aloja_curso == 'Apartamentos') { //cerrada del 13/12/2008 al 04/01/2009
if (semanas_curso == 3) {
	var apaga_fri = {
	};
} else if (semanas_curso == 6) {
	var apaga_fri = {
	6 : [ 3, 10, 17 ]             // days off in July
	};
}
} else if (destino_curso == 'oxfordst') { //cerrada del 13/12/2008 al 04/01/2009
if (semanas_curso == 3) {
	var apaga_fri = {
	6 : [ 3, 10 ],             // days off in July
    7 : [ 28 ]          //    days off in August
	};
} else if (semanas_curso == 6) {
	var apaga_fri = {
	6 : [ 3, 10, 17, 24 ],             // days off in July
    7 : [ 28 ]          //    days off in August
	};
}
} else if (destino_curso == 'margate' && aloja_curso == 'Residencia-apartamentos') { //cerrada del 13/12/2008 al 04/01/2009
if (semanas_curso == 3) {
	var apaga_fri = {
	6 : [ 3, 10, 17 ]             // days off in July
	};
} else if (semanas_curso == 6) {
	var apaga_fri = {
	6 : [ 3, 10, 17, 24, 31 ],             // days off in July
    7 : [ 7 ]          //    days off in September for Bristol
	};
}
} else if (destino_curso == 'paris' && comidas_aloja == '14') {
	var apaga_fri = {
	6 : [ 17, 24, 31 ],             // days off in July
    7 : [ 7, 14, 21, 28 ],          //    days off in August
    8 : [ 4, 11 ]          //    days off in September
	};
} else {
// Estructura de control para indicar los viernes que no es posible finalizar al curso en función del nº de semanas.
if (semanas_curso == 3) {
var apaga_fri = {
   5 : [ 5, 12 ]          //    days off in November
};
} else if (semanas_curso == 6) {
var apaga_fri = {
   6 : [ 3 ]          //    days off in November
};
}

}
    var m = apaga_fri[month];
    if (!m) return false;
    for (var i in m) if (m[i] == day) return true;
    return false;
}

  function destacaLunes(date, y, m, d) {
var destino_curso = document.forms[0].destino.value;
var aloja_curso = document.forms[0].op_aloja.value;
var semanas_curso = parseInt(document.forms[0].semanas.value);
// Estructura de control para cargar los días de incorporación en el calendario en función del destino.
if (destino_curso == 'bournebraidley' && aloja_curso == 'Residencia Purbeck House') {
	var mesi_lun = 6;
	var mesf_lun = 6;
} else if (destino_curso == 'bristol' && aloja_curso == 'Residencia' || destino_curso == 'dublinkildare' && aloja_curso == 'Residencia-apartamentos Archway Court') {
	var mesi_lun = 6;
	var mesf_lun = 7;
} else if (destino_curso == 'liverpool' || destino_curso == 'oxfordst' || destino_curso == 'cork' && aloja_curso == 'Apartamentos' || destino_curso == 'bostonbackbay' && aloja_curso == 'Residencia Suffolk' || destino_curso == 'losangeles' && aloja_curso == 'Residencia UCLA' || destino_curso == 'montpellier' && aloja_curso == 'Residencia CUP') {
	var mesi_lun = 5;
	var mesf_lun = 7;
} else if (destino_curso == 'margate' && aloja_curso == 'Residencia-apartamentos' || destino_curso == 'toronto' && aloja_curso == 'Residencia Woodsworth') {
	var mesi_lun = 6;
	var mesf_lun = 7;
} else if (destino_curso == 'nuevayork' && aloja_curso == 'Residencia The New Yorker') {
if (semanas_curso == 3) {
	var mesi_lun = 7;
	var mesf_lun = 10;
} else if (semanas_curso == 6) {
	var mesi_lun = 7;
	var mesf_lun = 9;
}
} else if (destino_curso == 'toronto' && aloja_curso == 'Residencia CCNM') {
if (semanas_curso == 3) {
	var mesi_lun = 8;
	var mesf_lun = 10;
} else if (semanas_curso == 6) {
	var mesi_lun = 8;
	var mesf_lun = 9;
}
} else {
// Estructura de control para cargar los días de incorporación en el calendario en función del nº de semanas.
if (semanas_curso == 3) {
	var mesi_lun = 5;
	var mesf_lun = 10;
} else if (semanas_curso == 6) {
	var mesi_lun = 5;
	var mesf_lun = 9;
}
}
    //Resalta todos los lunes y deshabilita los demás días
    if (lunesOff(y, m, d) || date.getDay() != 1) return "apagado";
	//Oculta todos los días fuera del plazo de incorporación
	else if (date.getMonth() < mesi_lun) return "apagado"; 
	else if (date.getMonth() > mesf_lun) return "apagado"; 
    else return false; // other dates are enabled
  };
  
  function destacaViernes(date, y, m, d) {
var destino_curso = document.forms[0].destino.value;
var aloja_curso = document.forms[0].op_aloja.value;
var semanas_curso = parseInt(document.forms[0].semanas.value);
// Estructura de control para cargar los días de conclusión en el calendario en función del destino.
if (destino_curso == 'bournebraidley' && aloja_curso == 'Residencia Purbeck House' || destino_curso == 'toronto' && aloja_curso == 'Residencia Woodsworth') {
	var mesi_vie = 7;
	var mesf_vie = 7;
} else if (destino_curso == 'bristol' && aloja_curso == 'Residencia' || destino_curso == 'dublinkildare' && aloja_curso == 'Residencia-apartamentos Archway Court') { 
	var mesi_vie = 6;
	var mesf_vie = 8;
} else if (destino_curso == 'liverpool' || destino_curso == 'margate' && aloja_curso == 'Residencia-apartamentos' || destino_curso == 'oxfordst' || destino_curso == 'cork' && aloja_curso == 'Apartamentos' || destino_curso == 'bostonbackbay' && aloja_curso == 'Residencia Suffolk' || destino_curso == 'losangeles' && aloja_curso == 'Residencia UCLA' || destino_curso == 'montpellier' && aloja_curso == 'Residencia CUP') { 
	var mesi_vie = 6;
	var mesf_vie = 7;
} else if (destino_curso == 'toronto' && aloja_curso == 'Residencia CCNM' || destino_curso == 'nuevayork' && aloja_curso == 'Residencia The New Yorker') { 
if (semanas_curso == 3) {
	var mesi_vie = 8;
	var mesf_vie = 10;
} else if (semanas_curso == 6) {
	var mesi_vie = 9;
	var mesf_vie = 10;
}
} else {
// Estructura de control para cargar los días de incorporación en el calendario en función del nº de semanas.
if (semanas_curso == 3) {
	var mesi_vie = 5;
	var mesf_vie = 10;
} else if (semanas_curso == 6) {
	var mesi_vie = 6;
	var mesf_vie = 10;
}
}
    //Resalta todos los viernes y deshabilita los demás días
      if (viernesOff(y, m, d) || date.getDay() != 5) return "apagado";
	//Oculta todos los días fuera del plazo de finalización
	else if (date.getMonth() < mesi_vie) return "apagado"; 
	else if (date.getMonth() > mesf_vie) return "apagado"; 
    else return false; // other dates are enabled
  };

