function fnValidate(frm)
{
var departDate = frm.txtDate.value
var departDay = departDate.substring(0, 2)
var departMonth = departDate.substring(3,5).toUpperCase()
var departYear = departDate.substring(6).toUpperCase()
var departMonthNum = getMonthNum(departMonth)
var depMonth = getMonth(departMonth);
var todayDate = new Date();
var inDate = new Date();
	
	if (frm.departureCity.value == "")
	{
		alert("Please select the departing city");
		frm.elements["departureCity"].focus();
		return false;
	}	
	if (frm.arrivalCity.value == "")
	{
		alert("Please select the arriving city");
		frm.elements["arrivalCity"].focus();
		return false;
	}	
	if (frm.txtDate.value == "")
	{
		alert("Please enter date");
		frm.elements["txtDate"].focus();
		return false;
	}
	if (!checkdep(frm.txtDate))
	{
		frm.elements["txtDate"].focus();
		return false;
	}	
	if (departDate != "")
	{
		inDate.setDate(departDay);
		inDate.setMonth(departMonth - 1);
		inDate.setYear(departYear);
		var dayDiff = (inDate - todayDate) / (1000 * 60 *60 *24);
		if (inDate < todayDate)
		{
			alert("Please select a date later than today's date.");
			frm.elements["txtDate"].focus();
			return false;
		}
		if (dayDiff > 331)
		{
			alert("Please select a date within 331 days");
			frm.elements["txtDate"].focus();
			return false;
		}		
		if (!checkLeapYear(departDay,departMonthNum,departYear))
		{
			alert("Please enter a valid day. ");
			frm.elements["txtDate"].focus();
			return false;
		}
		
	}
	frm.inputDate.value=departDay + depMonth;
	frm.action = "/AbacusFlightSchedule/FlightScheduleDisplay.aspx";
	frm.submit();

}
function fnClear(frm)
{
	frm.txtDate.value = "";
	frm.Carrier.value = "";
	frm.departureCity.value = "ADL";
	frm.arrivalCity.value = "ADL";

}

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   }
   return this
}

function isIntegerInRange (s, a, b)
{   if (isEmptyStr(s)) return false;

    if (!isInt(s, false)) return false;

    var num = parseInt (s,10);
    return ((num >= a) && (num <= b));
}

function isEmptyStr(s)
{   return ((s == null) || (s.length == 0))
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

//--------------------------------------------------------------
//To validate string contain alphabet only or white space
//--------------------------------------------------------------
function isAlphabet(s) {

	for (var i=0; i < s.length; i++){
		//Check that current character is alphabet.
		var c = s.charAt(i);

		if (!((c >= "a") && (c <= "z"))){
			if (!((c >= "A") && (c <= "Z"))){
				if (c != " "){
					return false;
				}
			}
		}
	}
	return true;
}

//--------------------------------------------------------------
//To validate string contain alphabet only 
//--------------------------------------------------------------
function isAlpha(s) {

	for (var i=0; i < s.length; i++){
		//Check that current character is alphabet.
		var c = s.charAt(i);

		if (!((c >= "a") && (c <= "z"))){
			if (!((c >= "A") && (c <= "Z"))){
					return false;
			}
		}
	}
	return true;
}

//--------------------------------------------------------------
// Check is s within the range of a and b
//--------------------------------------------------------------
function isIntegerInRange (s, a, b)
{   if (isEmptyStr(s)) return false;

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInt(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s,10);
    return ((num >= a) && (num <= b));
}

//--------------------------------------------------------------
// Check valid month
//--------------------------------------------------------------
function isMonth (s)
{
	if (isEmptyStr(s)) return false;
	return isIntegerInRange (s, 1, 12);
}

//--------------------------------------------------------------
// Return number for month
//--------------------------------------------------------------
function getMonthNum (s)
{
var monthNum = 0
if (!isEmptyStr(s)) 
{	
	if (s == "JAN") 
		monthNum = 1;
	if (s == "FEB") 
		monthNum = 2;
	if (s == "MAR") 
		monthNum = 3;
	if (s == "APR") 
		monthNum = 4;
	if (s == "MAY") 
		monthNum = 5;
	if (s == "JUN") 
		monthNum = 6;
	if (s == "JUL") 
		monthNum = 7;
	if (s == "AUG") 
		monthNum = 8;
	if (s == "SEP") 
		monthNum = 9;
	if (s == "OCT") 
		monthNum = 10;
	if (s == "NOV") 
		monthNum = 11;
	if (s == "DEC") 
		monthNum = 12;
}
		
	return monthNum;
}

//--------------------------------------------------------------
// Return number for month
//--------------------------------------------------------------
function getMonth (s)
{
var month = 0
if (!isEmptyStr(s)) 
{	
	if (s == "01") 
		month = "JAN";
	if (s == "02") 
		month = "FEB";
	if (s == "03") 
		month = "MAR";
	if (s == "04") 
		month = "APR";
	if (s == "05") 
		month = "MAY";
	if (s == "06") 
		month = "JUN";
	if (s == "07") 
		month = "JUL";
	if (s == "08") 
		month = "AUG";
	if (s == "09") 
		month = "SEP";
	if (s == "10") 
		month = "OCT";
	if (s == "11") 
		month = "NOV";
	if (s == "12") 
		month = "DEC";
}
		
	return month;
}


//--------------------------------------------------------------
//Check that s is an integer string
//--------------------------------------------------------------
function isInt (s)

{   var i;

    if (isEmptyStr(s)) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isNumber(c)) return false;
    }

    // All characters are numbers.
    return true;
}

//--------------------------------------------------------------
//Check check if the day of feb is correct based on leap year.
//--------------------------------------------------------------

function checkLeapYear(dd,mon,year)
{	
	
	// February check
	if (mon == 2) 
	{ 
	  if (dd == 29) 
	  { // leap year?
		if ( (year % 4) != 0) 
			return false;
			
		if ( ( (year % 100) == 0) && ( (year % 400) != 0) )	
			return false;
	  }
	}// correct date
	
	return true;		
}

//--------------------------------------------------------------
//Check that the character is 0 to 9
//--------------------------------------------------------------
function isNumber (c)
{   return ((c >= "0") && (c <= "9"))
}


