//calendar.js
var IE4 = (document.all) ? 1 : 0;
var isNS3 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 3);
var isIE = (navigator.appName == "Microsoft Internet Explorer");
var  selectedyear =2004;
var  MAX_INTERVAL_DAYS = 340;

var monthName = new Array();
var daysName = new Array();
var lablesName = new Array();

var prefix = "/sundor/";
function getMonthNumber(input) {
    if (input == 0) {
        return "1";
    }
    if (input == 1) {
        return "2";
    }
    if (input  == 2) {
        return "3";
    }
    if (input  == 3) {
        return "4";
    }
    if (input  == 4) {
        return "5";
    }
    if (input  == 5) {
        return "6";
    }
    if (input == 6) {
        return "7";
    }
    if (input  == 7) {
        return "8";
    }
    if (input  == 8) {
        return "9";
    }
    if (input  == 9) {
        return "10";
    }
    if (input == 10) {
        return "11";
    }
    if (input  == 11) {
        return "12";
    }
}

// Checks if browser is Netscape 2.0 since the options array properties don't work with Netscape 2.0x
function isBrowserSupp() {
    // Get the version of the browser
    version =  parseFloat( navigator.appVersion );
    
    if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    }
    else {
        return true;
    }                  
}

function isLeapYear(yrStr) {
    var leapYear=false;
    // every fourth year is a leap year
    if ((parseInt(yrStr, 10)%4) == 0) {
        leapYear=true;
    }
    return leapYear;
}

function getDaysInMonth(mthIdx, YrStr)
{
    // all the rest have 31
    var maxDays=31;
    // expect FEB. (of course)
    if (mthIdx==2) {
        if (isLeapYear(YrStr)) {
            maxDays=29;
        } else {
            maxDays=28;
        }
    }
    // thirty days hath...
    if (mthIdx==4 || mthIdx==6 || mthIdx==9 || mthIdx==11) {
        maxDays=30;
    }
    return maxDays;
}

// the function controls validity of dates within outbound and
// inboud date selection boxes.
function adjustDate(mthIdx, Dt, Yr) {
    var value=0;
    var numDays=0;
    numDays = getDaysInMonth(mthIdx, parseInt(Yr,10));
    if (Dt.options.selectedIndex <= numDays) {
        return 0;
    } else {
        Dt.options.selectedIndex = numDays;
    }
    
    return value;
} // end adjustDate method

//changes departure month when arrival month is changed
function inMonthChange(inM,inD,inY) {
    var tempMth;
    tempMth = 5;
    
    inY = 2004;
    
    if (!isBrowserSupp()) {
        return;
    }
    if (inM.options.value < tempMth) {
        inY = inY + 1;
    }

    adjustDate(inM.options.selectedIndex, inD,inY);
    return;
}

//changes departure day when arrival day is changed
function inDayChange(inD, inM, inY) {
     
    if (!isBrowserSupp()) {
        return;
    }
    var tempMth;
    
    tempMth = 5;
    inY = 2004;
    //if selected month is less the the current month, add 1 more year
    if (inM.options.value < tempMth) {
        inY = inY + 1;
    }
    adjustDate(inM.options.selectedIndex, inD,inY);
    return;
}

//Calendar Section
//calculation functions
function nextMonth(month) {
    if (month==12) {
        return 1;
    } else {
        return (month+1);
    }
}

function prevMonth(month) {
    var prevMonth = (month-1);
    if (month==1) {
        prevMonth = 12;
    }
    return prevMonth;
}

//increments or decrements month when it goes past JAN or DEC
function changeYear(direction,month,year) {
    var theYear = year;
    if (direction=="next") {
        if (month == 12) {
            theYear = (year+1);
        }
    }
    if (direction=="prev") {
        if (month == 1) {
            theYear = (year-1);
        }
    }
    return theYear;
}

//opens a new window for the calendar
function createCalendar(month,year,whichOne,checkDepYear, calendarArray) 
{
    monthName = calendarArray[0];
    daysName = calendarArray[1];
    lablesName = calendarArray[2];
    
    if (!isBrowserSupp()) {
        alert("Your browser does not support this feature");
        return;
    }
    
	calendarWindow = window.open("/sundor/dummy.html","Calendar","width=240,height=320,resizable=no,scrollbars=no");

    var mthIdx = month.options.selectedIndex;
    var mthVal = getMonthNumber(mthIdx);
	var yearVal = parseInt(year);
	
	
    // if the month value the user selected is less or equal to the current month then
    // set the year to current year plus one
//    if (mthVal <= CurrentMonth) {
//        yearVal = (yearVal + 1);
//    }

    //call the function to populate the window
    generateCalendar(calendarWindow,mthVal,yearVal,whichOne,checkDepYear)
} 

function tab(number) {
    if (number == 0) {
        return "\n";
    } else {
        returnString="\n";
        for (i = 0; i < number; i++) {
            returnString=returnString+"  ";
        }
        return returnString;
    }
}

var CurrentMonth;
var CurrentYear;
var CurrentDay;

//generates the meat of the calendar
function generateCalendar(target,month,year,whichOne,checkDepYear) {
    if (!isBrowserSupp()) {
        return;
    }

    //begin table for calendar
    
    var mthIdx = parseInt(month);
    var endday = getDaysInMonth(mthIdx, year);
    var index = (mthIdx-1);
    
    wholeDate = month + "/01/" + year;
    thedate = new Date(wholeDate);
    firstDay = thedate.getDay();

    selectedmonth = mthIdx;
    var today = new Date();
    var todayDay = today.getDate();    
    var todayMonth = today.getMonth();
    var todayYear = today.getFullYear();
    
    selectedyear = year;
    var lastDay = (endday + firstDay+1);
    
    target.document.open();
    var dir = "ltr";
    var suffix = "";
    var leftAlign = "left";
    var rightAlign = "right";
    if ("ינואר" == monthName[0] || "\u05d9\u05e0\u05d5\u05d0\u05e8" == monthName[0]) dir="rtl";
    if (dir=="rtl") {
    	suffix = "_heb";
    	leftAlign = "right";
    	rightAlign = "left";
    }
    calendar = "<html dir=" + dir + ">\n";
    calendar += "	<head>\n";
    calendar += "		<title>calendar</title>\n";
    calendar += "		<meta http-equiv=Content-Type content=\"text/html; charset=windows-1255\">\n"
    calendar += "		<link rel=\"stylesheet\" href=\"elal_air.css\" type=\"text/css\">\n";
    calendar += "		<link href=\"" + prefix + "css/ticket" + suffix + ".css\" rel=\"stylesheet\" type=\"text/css\">\n";    
    calendar += "	</head>\n";
    calendar += "	<body>\n";
    calendar += "		<script language='JavaScript'>\n";
    calendar += "			setTimeout(\"self.close()\",30000)\n";
    calendar += "		</script>\n";
    calendar += "		<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"pop_calendar\">\n";
    calendar += "			<tr>\n";
    calendar += "				<td><img src=\"../sundor/images/1x1.gif\" width=\"1\" height=\"55\">\n";
    calendar += "					<div class=\"pop_calendar_month\">\n";
    calendar += "						" + monthName[index] + " " + year;
    calendar += "						<img src=\"../sundor/images/1x1.gif\" width=\"25\" height=\"1\">\n";
    calendar += "					</div>\n";
    calendar += "					<img class=\"pop_calendar_hr\" src=\"../sundor/images/1x1.gif\" vspace=\"5\">\n";
    calendar += "					<table class=\"calendar\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">\n";
    calendar += "						<tr>\n";
    calendar += "							<th>"+daysName[0]+"</th>\n";
    calendar += "							<th>"+daysName[1]+"</th>\n";
    calendar += "							<th>"+daysName[2]+"</th>\n";
    calendar += "							<th>"+daysName[3]+"</th>\n";
    calendar += "							<th>"+daysName[4]+"</th>\n";
    calendar += "							<th>"+daysName[5]+"</th>\n";
    calendar += "							<th style=\"color:#FF0000;\">"+daysName[6]+"</th>\n";
    calendar += "						</tr>\n";
    calendar += "						<tr>\n";
    var line = 0;    
    var isFixedLine = false;
    for (var i = 1; i < lastDay; i++) {
        if (i <= firstDay) { 
            // 'empty' boxes prior to first day
            calendar += "					<td>&nbsp;</td>\n";
        } else {
            if (((i-firstDay)< todayDay) && (month == todayMonth+1) && (year == todayYear)) {
            //enter date number without link
            calendar += "					<td>\n";    
            calendar += "						" + (i-firstDay)+"\n";
            calendar += "					</td>\n";
            } else {
            // enter date number with link            
            calendar += "					<td>\n";
            calendar += "						<a href='JavaScript:self.close();opener.closeCalendar"+"(" + (i-firstDay) + "," + whichOne + "," + checkDepYear +")'>\n";
            calendar += "							" + (i-firstDay)+"\n";
            calendar += "						</a>\n";
            calendar += "					</td>\n";
        	}
        	if (i == 1) isFixedLine = true;
        }
        //must start new row after each week
        if (i % 7 == 0) {
            line += 1;
            if (i != (lastDay-1)) {
                calendar += "			</tr>\n";
                calendar += "			<tr>\n";
            }
        }
        if (i == (lastDay-1)) {
            calendar += "				</tr>\n";
        }
        if (i == (lastDay-1) && (i%7 == 0)) isFixedLine = true;
    }
    if (line == 4 || isFixedLine) {
    	calendar += "			<tr><td colspan=7>&nbsp;</td></tr>\n";
    }
    //end of days table
    calendar += "					</table>\n";
	calendar += "						<img src=\"../sundor/images/1x1.gif\" width=\"1\" height=\"15\"><img class=\"pop_calendar_hr\" src=\"../sundor/images/1x1.gif\" vspace=\"5\"><img src=\"../sundor/images/1x1.gif\" width=\"1\" height=\"5\"><br>\n";
    calendar += "					<table class=\"calendar_bottom\" border=\"0\" align=center cellpadding=\"0\" cellspacing=\"0\">\n";
    //next month and previous month buttons
    var goPrevMonth = prevMonth(mthIdx);
    var goNextMonth = nextMonth(mthIdx);
    var nextYear = changeYear("next",parseInt(month),parseInt(year));
    var prevYear = changeYear("prev",parseInt(month),parseInt(year));
    
    // maximum amount of browsable months from current month
    var QUERY_LIMIT = 11;
    
    var mthIdxTmp = mthIdx;        
    
    //if selected month is equal to the the current month		
    if (mthIdx == CurrentMonth+1  && year<=todayYear) {
        calendar += "				<tr>\n";
        calendar += "					<td> &nbsp; </td>\n";
    } else {            
        calendar += "				<tr>\n";
        calendar += "					<td align=" + leftAlign + ">\n";
        calendar += "						<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=" + leftAlign + ">\n";
        calendar += "							<tr>\n";
        calendar += "								<td>";            
        calendar += 									"<img src=\"" + prefix + "images/ticket/blue_but_r" + suffix + ".gif\">";
        calendar += 								"</td>\n";
        calendar += "								<td bgcolor=\"#A3DBF4\">";
        calendar += 									"<a class=\"bold c1\" href='javascript:opener.generateCalendar(self,"+goPrevMonth+","+prevYear+"," + whichOne + "," + checkDepYear +")'>";
        calendar += 										lablesName[1];
        calendar += 									"</a>";
        calendar += 								"</td>\n";
        calendar += "								<td>";
        calendar += 									"<img src=\"" + prefix + "images/ticket/blue_but_left" + suffix + ".gif\">";
        calendar += 								"</td>\n";
        calendar += "								<td><img src=\"" + prefix + "images/1x1.gif\" height=\"1\"></td>\n";
        calendar += "							</tr>\n";
        calendar += "						</table>\n";                                    
        calendar += "					</td>";
    }
    
    //if the selected month is less than the current month + 1 (+1 because of differences
    //between the month returned from the bean and the index value of the selected month)
    //then add 12 to the mthIdxTmp variable, this increases the year value by 1
    if (mthIdx <CurrentMonth+1 || year>todayYear) {		
       
        mthIdxTmp += 12;
        
    }
  
    //if the mthIdxTmp, minus the current month stored in the bean is less than 11 allow
    //displaying of the View Next link
    
    
    if ((mthIdxTmp - (CurrentMonth+1)) < QUERY_LIMIT) {
        calendar += "					<td align=" + rightAlign + ">\n";
        calendar += "						<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=" + rightAlign + ">\n";
        calendar += "							<tr align=" + rightAlign + ">\n";
        calendar += "								<td>";            
        calendar += 									"<img src=\"" + prefix + "images/ticket/blue_but_right" + suffix + ".gif\">";
        calendar += 								"</td>\n";
        calendar += "								<td bgcolor=\"#A3DBF4\" height=18px>";
        calendar += 									"<a class=\"bold c1\" href='javascript:opener.generateCalendar(self,"+goNextMonth+","+nextYear+"," + whichOne + "," + checkDepYear + ")'>";
        calendar += 										lablesName[0];
        calendar += 									"</a>";
        calendar += "								</td>\n";
        calendar += "								<td>";
        calendar += 									"<img src=\"" + prefix + "images/ticket/blue_but_l" + suffix + ".gif\">";
        calendar += 								"</td>";
        calendar += "								<td><img src=\"" + prefix + "images/1x1.gif\" height=\"1\"></td>\n";
        calendar += "							</tr>\n";
        calendar += "						</table>\n";
        calendar += "					</td>\n";
        calendar += "				</tr>";            
    } else {
        //otherwise the user has browsed 11 months and cannot browser further
        calendar += "					<td>\n";
        calendar += "					</td>\n";
        calendar += "				</tr>\n";
    }
    calendar += "				</table>\n";
    calendar += "			</td>\n";
    calendar += "		</tr>\n";
    calendar += "	</table>\n";
    calendar += "</body>\n";
    calendar += "</html>\n";
    target.document.close();
    
    
    target.document.write(calendar);
    target.document.close();
}
//changes date when a date is clicked


function closeCalendar(day,whichOne, checkDepYear) {
	    
    if (whichOne == 1){
        for ( i =0 ; i < document.booking_request.departYear.length ;i++){
            if   (document.booking_request.departYear.options[i].value==selectedyear)
             document.booking_request.departYear.options.selectedIndex=i;
        }
         document.booking_request.departMonth.options.selectedIndex=selectedmonth-1;
         document.booking_request.departYear.value = selectedyear;
         document.booking_request.departDay.options.selectedIndex=parseInt(day)-1;
         ErrorDepDate.style.display="none";
        return;
    } else if (whichOne == 2 ) {	
        if (checkDepYear) {
	        for ( j =0 ; j < document.booking_request.departYear.length ;j++){
	            if   (document.booking_request.departYear.options[j].value==selectedyear)
	             document.booking_request.departYear.options.selectedIndex=j;
	        }
        }
        document.booking_request.retMonthSelect.options.selectedIndex=selectedmonth-1;
        document.booking_request.retYearSelect.value = selectedyear;
        document.booking_request.retDaySelect.options.selectedIndex=parseInt(day)-1;
        ErrorRetDate.style.display="none";
    }
}

// Checks if browser is Netscape 2.0 since the options array properties don't work with Netscape 2.0x
function isBrowserSupp() {
    // Get the version of the browser
    version =  parseFloat( navigator.appVersion );
    
    if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    }
    else {
        return true;
    }                  
}


