// overLIB Calender 0.1
// Copyright Erik Bosrup 2001. Not covered by the Artistic License. All rights reserved.
// Partially based on work found in the public domain. Author unknown.
// BUG: Doesn't work in NS4 (shows error message placed in overDiv). Tested with IE6 and MZ0.9 on W2K

var showingCal;
var wayTarget;
var monthArray = new Array();

var today = new Date();
var year = today.getYear();
if (year < 1900) year += 1900; // For NS
today = new Date("January 1, " + year)
var weekDay = today.getDay()
	
createCal();

function createCal() {
	lx = 0;
	for (x = 0; x < 2; x++) {
		monthArray[lx++] = createMonth("Januari", 31, 1);
		
		if ( ( (year % 4) == 0 ) && ( (year % 100)!= 0) || ( (year % 400) == 0) ) {
			monthArray[lx++] = createMonth("Februari", 29, 2);
		} else {
			monthArray[lx++] = createMonth("Februari", 28, 2);
		}
		
		monthArray[lx++] = createMonth("Mars", 31, 3)
		monthArray[lx++] = createMonth("April", 30, 4)
		monthArray[lx++] = createMonth("Maj", 31, 5)
		monthArray[lx++] = createMonth("Juni", 30, 6)
		monthArray[lx++] = createMonth("Juli", 31, 7)
		monthArray[lx++] = createMonth("Augusti", 31, 8)
		monthArray[lx++] = createMonth("September", 30, 9)
		monthArray[lx++] = createMonth("Oktober", 31, 10)
		monthArray[lx++] = createMonth("November", 30, 11)
		monthArray[lx++] = createMonth("December", 31, 12);
		
		year++;
	}
}


function createMonth(nameOfMonth, lengthOfMonth, monthNumber) {
	var back;
	var day = 1;
	
	back = "<table border=0 bgcolor=\"#ffffff\" width=\"100%\"><tr><td colspan=\"7\" align=\"center\" bgcolor=\"#dddddd\" class=\"caltitle\">" + nameOfMonth + " " + year + "</td><tr>";
	back += createWeekdayTitle("M")
	back += createWeekdayTitle("T")
	back += createWeekdayTitle("O")
	back += createWeekdayTitle("T")
	back += createWeekdayTitle("F")
	back += createWeekdayTitle("L")
	back += createWeekdayTitle("S")
	back += "</tr><tr>";

	// Fill out until month starts
	for (var i = 1; i < weekDay; i++) {
		back += "<td></td>";
	}

	// First line of days
	for (var i = weekDay; i < 8; i++) {
		back += createWeekday(year, monthNumber, day);
		day++
	}
	back += "<tr>";

	// All other lines
	while (day <= lengthOfMonth) {
		for (var i = 1; i <= 7 && day <= lengthOfMonth; i++) {
			back += createWeekday(year, monthNumber, day);
			day++
		}
		back += "</tr><tr>";
			weekDay=i
	}

	back += "</tr><tr><td colspan=\"3\"><a href=\"javascript:setCalPrev();\" class=\"cal\">Tidigare</a></td><td></td><td colspan=\"3\" align=\"right\"><a href=\"javascript:setCalNext();\" class=\"cal\">Senare</a></tr>";
	back += "</table>";
	
	return back;
}


function createWeekdayTitle(nameOfDay){
	return "<td align=center class=\"calhead\" width=\"14%\"> "+nameOfDay+" </td>";
}
	
function createWeekday(year, month, day) {
	return "<td align=center class=\"cal\"><a class=\"cal\" href=\"javascript:setDate(" + year + "," + month + "," + day + ");\">"+day+"</a></td>";
}


function setDate(year, month, day) {
	if (month < 10) month = "0" + month;
	if (day < 10) day = "0" + day;
		
	var fr = document.forms.bestagent;
	var mbox;
	var dbox;
		
	var tmpValidationMonth;
	var tmpValidationDay;
	if (wayTarget == 'home') {
		mbox = fr.homeleavemonth;
		dbox = fr.homeleaveday;
		tmpValidationMonth = fr.destleavemonth;
		tmpValidationDay = fr.destleaveday;
		
	} else {
		mbox = fr.destleavemonth;
		dbox = fr.destleaveday;
		tmpValidationMonth = fr.homeleavemonth;
		tmpValidationDay = fr.homeleaveday;
	}
	
	var found = false;
	var tmpvalMonth;
	var tmpvalDay;
	for (var y = 0; y < mbox.options.length; y++) {
		if (mbox.options[y].value == year + "-" + month) {
			tmpvalMonth = y;
			found = true;
		}
	}
		
	if (found) {
		for (var y = 0; y < dbox.options.length; y++) {
			if (dbox.options[y].value == day) {
				tmpvalDay = y;
			}
		}
	}
	/*
	try {
		if (wayTarget == 'home') {
			if (( (tmpvalMonth == tmpValidationMonth.selectedIndex && tmpvalDay >= tmpValidationDay.selectedIndex) || (tmpvalMonth > tmpValidationMonth.selectedIndex) )) {
				if (document.forms.bestagent.returntrip[0].checked == true) {
					alert("Din avresa måste vara innan hemresan.");
					return;
				}
			}
		} else {
			if (( (tmpValidationMonth.selectedIndex == tmpvalMonth && tmpValidationDay.selectedIndex >= tmpvalDay) || (tmpValidationMonth.selectedIndex > tmpvalMonth) )) {
				if (document.forms.bestagent.returntrip[0].checked == true) {
					alert("Din avresa måste vara innan hemresan.");
					return;
				}
			}
		}
	} catch (ex) {
		//If catch, do nothing. Let server side take care of the issue.
	}
	*/
	mbox.selectedIndex = tmpvalMonth;
	dbox.selectedIndex = tmpvalDay;
	try {
        	if (wayTarget == 'home') {
        		if (( (tmpvalMonth == tmpValidationMonth.selectedIndex && tmpvalDay >= tmpValidationDay.selectedIndex) || (tmpvalMonth > tmpValidationMonth.selectedIndex) )) {
                       		var daysofmonth = getDaysOfMonth(month)-1; //-1 since we use the indexes.
                                if ((fr.homeleaveday.selectedIndex + 7) <= daysofmonth) {
                 			fr.destleaveday.selectedIndex = fr.homeleaveday.selectedIndex+7;
        	        		fr.destleavemonth.selectedIndex = fr.homeleavemonth.selectedIndex;
        	        	} else {
                 			fr.destleaveday.selectedIndex = fr.homeleaveday.selectedIndex + (7 - daysofmonth -1);
        	        		fr.destleavemonth.selectedIndex = fr.homeleavemonth.selectedIndex+1;
        	        	}
                                nd();
        			return;
         		}
		}
	} catch (ex) {
	}
	nd();
}

function getDaysOfMonth(month) {
         if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" || month == "12") { return 31; }
         if (month == "02") { return 28; }
         return 30;
}

function setCal(id) {
	if (ns4) cal = document.overdiv.document.caldiv.document;
	if (ie4) cal = self.caldiv.style
	if (ns6) cal = self.document.getElementById("caldiv");

	txt = monthArray[id] + "\n";
	
        if (ns4) {
        	alert("ns4");
       	        var lyr = document.overDiv.document.caldiv.document;
               	lyr.write(txt)
               	lyr.close()
        } else if (ie4) {
		document.all["caldiv"].innerHTML = txt
	} else if (ns6) {
		range = self.document.createRange();
		range.setStartBefore(cal);
		domfrag = range.createContextualFragment(txt);
		while (cal.hasChildNodes()) {
			cal.removeChild(cal.lastChild);
		}
		cal.appendChild(domfrag);
	}
	
	return true;
}
	
function setCalTarget(way) {
	wayTarget = way;
}

function setCalAuto() {
	showingCal = setCalFromDrop();
	setCal(showingCal);
}

function setCalPrev() {
	if (showingCal > 0) {
		showingCal--;
	}

	setCal(showingCal);
}

function setCalNext() {
	if (showingCal < monthArray.length-1) {
		showingCal++;
	}

	setCal(showingCal);
}
	
function setCalFromDrop() {
	var fr = document.forms.bestagent;

	if (wayTarget == 'home') {
		var mbox = fr.homeleavemonth;
	} else {
		var mbox = fr.destleavemonth;
	}
		
	cMon = mbox.options[mbox.selectedIndex].value;
	temp = cMon.split('-');

	cYear = parseInt(temp[0]);
	cMon = temp[1];
		
	if (cMon.charAt(0) == '0') {
		cMon = cMon.charAt(1);
	} 
	cMon = parseInt(cMon);
		
	rightnow = new Date();
	thisyear = rightnow.getYear();
	if (thisyear < 1900) thisyear += 1900; // For NS

	if (cYear == thisyear) {
		doMe = cMon - 1;
	} else {
		doMe = cMon + 11;
	}
	return doMe;
}
