﻿var xmlairports = null;
var theorigin = '';
var thedestination = '';
var foundOrigin = null;
var foundDest = null;
var strResponse = null;
var rtComplete = null;
var Cities = new Array(16);
Cities[0] = new Array(8);
Cities[0] [0] = '181';//Las Vegas
Cities[0] [1] = 1;
Cities[0] [2] = 1;
Cities[0] [3] = 1;
Cities[0] [4] = 1;
Cities[0] [5] = 1;
Cities[0] [6] = '<b>Las Vegas, NV (LAS)</b><input type="hidden" id="Destination" name="Destination" value="LAS">';
Cities[0] [7] = 'LAS, Las Vegas, NV';

Cities[1] = new Array(8);
Cities[1] [0] = 279;//Reno
Cities[1] [1] = 1;
Cities[1] [2] = 1;
Cities[1] [3] = 0;
Cities[1] [4] = 0;
Cities[1] [5] = 1;
Cities[1] [6] = '<b>Reno, NV (RNO)</b><input type="hidden" id="Destination" name="Destination" value="RNO">';
Cities[1] [7] = 'RNO, Reno, NV';

Cities[2] = new Array(8);
Cities[2] [0] = 241;//Orlando
Cities[2] [1] = 1;
Cities[2] [2] = 1;
Cities[2] [3] = 0;
Cities[2] [4] = 0;
Cities[2] [5] = 1;
Cities[2] [6] = '<b>Orlando, FL (MCO)</b><input type="hidden" id="Destination" name="Destination" value="MCO">';
Cities[2] [7] = 'MCO, Orlando, FL';

Cities[3] = new Array(8);
Cities[3] [0] = 418;//Laughlin
Cities[3] [1] = 0;
Cities[3] [2] = 1;
Cities[3] [3] = 0;
Cities[3] [4] = 0;
Cities[3] [5] = 0;
Cities[3] [6] = '<b>Las Vegas, NV (LAS)</b><input type="hidden" id="Destination" name="Destination" value="LAS">';
Cities[3] [7] = 'LAS, Las Vegas, NV';

Cities[4] = new Array(8);
Cities[4] [0] = 420;//Mesquite
Cities[4] [1] = 0;
Cities[4] [2] = 1;
Cities[4] [3] = 0;
Cities[4] [4] = 1;
Cities[4] [5] = 0;
Cities[4] [6] = '<b>Las Vegas, NV (LAS)</b><input type="hidden" id="Destination" name="Destination" value="LAS">';
Cities[4] [7] = 'LAS, Las Vegas, NV';

Cities[5] = new Array(8);
Cities[5] [0] = 426;//Oahu
Cities[5] [1] = 1;
Cities[5] [2] = 1;
Cities[5] [3] = 0;
Cities[5] [4] = 0;
Cities[5] [5] = 1;
Cities[5] [6] = '<b>Oahu (HNL) </b><input type="hidden" id="Destination" name="Destination" value="HNL">';
Cities[5] [7] = 'HNL, Oahu, HI';

Cities[6] = new Array(8);
Cities[6] [0] = 198;//Maui
Cities[6] [1] = 1;
Cities[6] [2] = 1;
Cities[6] [3] = 0;
Cities[6] [4] = 0;
Cities[6] [5] = 1;
Cities[6] [6] = '<b>Maui (OGG) </b><input type="hidden" id="Destination" name="Destination" value="OGG">';
Cities[6] [7] = 'OGG, Maui, HI';

Cities[7] = new Array(8);
Cities[7] [0] = 427;//Big Island
Cities[7] [1] = 1;
Cities[7] [2] = 1;
Cities[7] [3] = 0;
Cities[7] [4] = 0;
Cities[7] [5] = 1;
Cities[7] [6] = '<b>Big Island (KOA) </b><input type="hidden" id="Destination" name="Destination" value="KOA">';
Cities[7] [7] = 'KOA, Big Island, HI';

Cities[8] = new Array(8);
Cities[8] [0] = 167;//Kauai
Cities[8] [1] = 1;
Cities[8] [2] = 1;
Cities[8] [3] = 0;
Cities[8] [4] = 1;
Cities[8] [5] = 1;
Cities[8] [6] = '<b>Kauai (LIH) </b><input type="hidden" id="Destination" name="Destination" value="LIH">';
Cities[8] [7] = 'LIH, Kauai, HI';

Cities[9] = new Array(8);
Cities[9] [0] = 428;//Lanai
Cities[9] [1] = 1;
Cities[9] [2] = 1;
Cities[9] [3] = 0;
Cities[9] [4] = 0;
Cities[9] [5] = 0;
Cities[9] [6] = '<b>Lanai (LNY) </b><input type="hidden" id="Destination" name="Destination" value="LNY">';
Cities[9] [7] = 'LNY, Lanai, HI';

Cities[10] = new Array(8);

Cities[11] = new Array(8);
Cities[11] [0] = 189;//Los Angeles
Cities[11] [1] = 1;
Cities[11] [2] = 1;
Cities[11] [3] = 0;
Cities[11] [4] = 0;
Cities[11] [5] = 1;
Cities[11] [6] = '<b>Los Angeles (LAX) </b><input type="hidden" id="Destination" name="Destination" value="LAX">';
Cities[11] [7] = 'LAX, Los Angeles, CA';

Cities[12] = new Array(8);
Cities[12] [0] = 293;//San Diego
Cities[12] [1] = 1;
Cities[12] [2] = 1;
Cities[12] [3] = 0;
Cities[12] [4] = 1;
Cities[12] [5] = 1;
Cities[12] [6] = '<b>San Diego (SAN) </b><input type="hidden" id="Destination" name="Destination" value="SAN">';
Cities[12] [7] = 'SAN, San Diego, CA';

Cities[13] = new Array(8);
Cities[13] [0] = 294;//San Francisco
Cities[13] [1] = 1;
Cities[13] [2] = 1;
Cities[13] [3] = 0;
Cities[13] [4] = 0;
Cities[13] [5] = 1;
Cities[13] [6] = '<b>San Francisco (SFO) </b><input type="hidden" id="Destination" name="Destination" value="SFO">';
Cities[13] [7] = 'SFO, San Francisco, CA';

Cities[14] = new Array(8);
Cities[14] [0] = 429;//Anaheim
Cities[14] [1] = 1;
Cities[14] [2] = 1;
Cities[14] [3] = 0;
Cities[14] [4] = 0;
Cities[14] [5] = 1;
Cities[14] [6] = '<b>Anaheim (SNA) </b><input type="hidden" id="Destination" name="Destination" value="SNA">';
Cities[14] [7] = 'SNA, Anaheim, CA';

Cities[15] = new Array(8);
Cities[15] [0] = 247;//Palm Springs
Cities[15] [1] = 1;
Cities[15] [2] = 1;
Cities[15] [3] = 0;
Cities[15] [4] = 0;
Cities[15] [5] = 1;
Cities[15] [6] = '<b>Palm Springs (PSP) </b><input type="hidden" id="Destination" name="Destination" value="PSP">';
Cities[15] [7] = 'PSP, Palm Springs, CA';

	
function checkvals()
{
    var txtLength = 0;
    var searchRecordCount = 0;
    
//var theorigin = document.getElementById(fieldPrefix + 'origin').value;
//		if (theorigin == '' & document.getElementById('Product_0').checked==true)
//		{
//			alert("Please enter an origin city.");
//			document.getElementById(fieldPrefix + 'origin').focus();
//			return false;
//		}  
            
    if (fieldPrefix.indexOf('searchForm')> 0)
    {
    
        if(document.getElementById('Product_1') != null && document.getElementById(fieldPrefix + 'toDestination') != null)
        {
            if((document.getElementById('Product_1').checked==true && document.getElementById(fieldPrefix + 'toDestination').length < 3))
             {
	            alert("Please enter a destination city.");
	            document.getElementById(fieldPrefix + 'toDestination').focus();
	            return false;
	         }	 
        }
	    if (document.getElementById(fieldPrefix + 'origin') != null)
	    {   
	        theorigin = document.getElementById(fieldPrefix + 'origin').value;
	        theorigin = theorigin.toUpperCase();
	        theorigin = theorigin.replace(/ /g,"");
	        theorigin = theorigin.replace(/,/g,"");
	        theorigin = theorigin.replace(/\./g,"");
	        txtLength = theorigin.length;		        
	     }		     
	     if (theorigin != '' && txtLength < 3)
	     {
	        alert("Please enter an origin city.");
	        document.getElementById(fieldPrefix + 'origin').focus();
	        return false;
	     }
	     if (theorigin == '' && (document.getElementById('Product_0').checked==true)) // || document.getElementById('Product_5').checked==true))
         {	        
	        alert("Please enter an origin city.");
	        document.getElementById(fieldPrefix + 'origin').focus();
	        return false;
         }	         		     
             		     
	     if (document.getElementById(fieldPrefix + 'toDestination') != null)
	     {   
	        thedestination = document.getElementById(fieldPrefix + 'toDestination').value;
	        thedestination = thedestination.toUpperCase();
	        thedestination = thedestination.replace(/ /g,"");
	        thedestination = thedestination.replace(/,/g,"");
	        thedestination = thedestination.replace(/\./g,"");
	        txtLength = thedestination.length;
	     }

	     if (thedestination != '' && txtLength < 3)
	     {
	        alert("Please enter a destination city.");
	        document.getElementById(fieldPrefix + 'toDestination').focus();
	        return false;
	     }
	     if (thedestination == '' && (document.getElementById('Product_0').checked==true) && searchForm[1].selection == '') // || document.getElementById('Product_5').checked==true))
         {
	        alert("Please enter a destination city.");
	        document.getElementById(fieldPrefix + 'toDestination').focus();
	        return false;
         }
	         if(searchForm[0].selection == '' && (document.getElementById('Product_0').checked==true || document.getElementById('Product_5').checked==true))
		     {
			    alert("Please enter an origin city.");
			    document.getElementById(fieldPrefix + 'origin').focus();
			    return false;
         }
		     if(searchForm[1].selection == '' && (document.getElementById('Product_0').checked==true || document.getElementById('Product_5').checked==true))
		     {
//			    alert("Please enter a destination city.");
//			    document.getElementById(fieldPrefix + 'todestination').focus();
//			    return false;
		     }
		     if(searchForm[0].selection == searchForm[1].selection && (document.getElementById('Product_0').checked==true || document.getElementById('Product_5').checked==true))
		     {
		        alert('Departure city and arrival city cannot be the same.');
		        document.getElementById(fieldPrefix + 'origin').focus();
			    return false;
		     }
        
        foundOrigin = false;
        foundDest = false;
        rtComplete = false;
        //if (((document.getElementById('Product_0').checked==true || document.getElementById('Product_5').checked==true) && (fieldPrefix.indexOf('searchForm')> 0)) || ((document.getElementById('Product_0').checked==true || document.getElementById('Product_2').checked==true) && (fieldPrefix.indexOf('refineSearch')> 0)))
        if (((document.getElementById('Product_0').checked==true) && (fieldPrefix.indexOf('searchForm')> 0)) || ((document.getElementById('Product_0').checked==true || (document.getElementById('Product_2') != null && document.getElementById('Product_2').checked==true)) && (fieldPrefix.indexOf('refineSearch')> 0)))
        {
            checkAirport();
            checkComplete();
	    }
	    return true;
    }
    if (fieldPrefix.indexOf('refineSearch')> 0)
    {		    
	     
	     if (document.getElementById(fieldPrefix + 'origin') != null)
	    {   
	        theorigin = document.getElementById(fieldPrefix + 'origin').value;
	        theorigin = theorigin.replace(/ /g,"");
	        theorigin = theorigin.replace(/,/g,"");
	        theorigin = theorigin.replace(/\./g,"");
	        txtLength = theorigin.length;
	     }
	     if (theorigin != '' && txtLength < 3)
	     {
	        alert("Please enter an origin city.");
	        document.getElementById(fieldPrefix + 'origin').focus();
	        return false;
	     }
	     if (theorigin == '' && ((document.getElementById('Product_0') != null && document.getElementById('Product_0').checked==true) || (document.getElementById('Product_2') != null && document.getElementById('Product_2').checked==true)))
        {	        
	        alert("Please enter an origin city.");
	        document.getElementById(fieldPrefix + 'origin').focus();
	        return false;
        }
//	        if(searchForm[0].selection == '' && (document.getElementById('Product_0').checked==true || document.getElementById('Product_5').checked==true))
//		     {
//			    alert("Please enter an origin city.");
//			    document.getElementById(fieldPrefix + 'origin').focus();
//			    return false;
//		     }
	    return true;
    }
}
function checkComplete()
{
    if(rtComplete)
    {
        if (foundOrigin == false)
        {
            alert("Origin airport code could not be found.xx");
            document.getElementById(fieldPrefix + 'origin').focus();
            return false;
        }
        if (foundDest == false)
        {
//            alert("Destination airport code could not be found.");
//            document.getElementById(fieldPrefix + 'toDestination').focus();
//            return false;
        }
    }
    else
    {
        setTimeout("checkComplete()", 500);
    }
}
function checkAirport()
{   
    xmlairports = null;
	xmlairports = (!window.XMLHttpRequest) ? (ActiveXObject ? (new ActiveXObject("Microsoft.XMLHTTP")) : "") : (new XMLHttpRequest());
	var url = "../_ajax/webservices/autoComplete.asmx/getAirports";
	if (xmlairports != null)
	{
	    xmlairports.onreadystatechange = new Function("stateChange()");	    
	    xmlairports.open("GET", url, true);
	    xmlairports.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlairports.send(null);
    }
    else
    {
        alert("problem with XMLHTTP");
    }
    return true;
}


function stateChange()
{   
    var checkOriginCode = "&lt;code&gt;"+theorigin.substring(0,3)+"&lt;/code&gt;";
    var checkDestCode = "&lt;code&gt;"+thedestination.substring(0,3)+"&lt;/code&gt;";            
    var checkXML = "";
    if (xmlairports.readyState==4)
    {  
        try 
        {
            if (xmlairports.status==200)        
            { 
              checkXML = xmlairports.responseText;
              if (checkXML.indexOf(checkOriginCode) > 0)
              {
                 foundOrigin = true;
              }              
              if (checkXML.indexOf(checkDestCode) > 0)
              {
                 foundDest = true;
              }
              rtComplete = true;
            }
        }
        catch(e)
        {
        }
    }
    else
    {
        //alert("Problem retrieving XML data");
    }  
    return true;
}


function SelectProduct(ID) 
{
    switch (ID) 
    {
        case '0':{document.getElementById('Product_0').checked=true;break;}
        case '1':{document.getElementById('Product_1').checked=true;break;}
        case '2':{document.getElementById('Product_2').checked=true;break;}
        case '3':{document.getElementById('Product_3').checked=true;break;}
        case '4':{document.getElementById('Product_4').checked=true;break;}
        //case '5':{document.getElementById('Product_5').checked=true;break;}
        default:{document.getElementById('Product_1').checked=true;}
    }
}
	
    function AirfareOnly(ID) 
    {
    switch (ID) 
    {
//			case '0':{
//			            if(document.getElementById('OneWay').checked==true)
//			            {
//			                document.getElementById('OneWay').checked=false;
//			                break;
//			            }
//			            else
//			            {
//			                document.getElementById('OneWay').checked=true;
//			                break;
//			            }
//			         }			        
//			case '1':{
//			            if(document.getElementById('Nonstop').checked==true)
//			            {
//			                document.getElementById('Nonstop').checked=false;
//			                break;
//			            }
//			            else
//			            {
//			                document.getElementById('Nonstop').checked=true;
//			                break;
//			            }
//			         }
//			case '2':{
//			            if(document.getElementById('Refundable').checked==true)
//			            {
//			                document.getElementById('Refundable').checked=false;
//			                break;
//			            }
//			            else
//			            {
//			                document.getElementById('Refundable').checked=true;
//			                break;
//			            }
//			         }
    }
}
	
	
function openAirportCodes(NewWindowFile) 
{
    window.open(NewWindowFile, 'AirportCodes', 'width=500,height=400,scrollbars=YES,resizable=YES')
}

/***********************************************
 Some base functions used from:
 Fool-Proof Date Input Script with DHTML Calendar
 by Jason Moon - webmaster@moonfam.net
 ************************************************/

var DefaultDateFormat = 'MM/DD/YYYY'; // If no date format is supplied, this will be used instead
var HideWait = 3; // Number of seconds before the calendar will disappear
var Y2kPivotPoint = 76; // 2-digit years before this point will be created in the 21st century
var FontSize = 11; // In pixels
var FontFamily = 'Tahoma';
var CellWidth = 18;
var CellHeight = 16;
var ImageURL = 'Search/images/calendar.gif';
var NextURL = '/images/Search/next.gif';
var PrevURL = '/images/Search/prev.gif';
var CalBGColor = 'white';
var TopRowBGColor = 'buttonface';
var DayBGColor = 'lightgrey';
// Global variables
var ZCounter = 100;
var Today = new Date();
var WeekDays = new Array('S','M','T','W','T','F','S');
var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var MonthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

// Write out the stylesheet definition for the calendar
with (document) 
{
    writeln('<style>');
    writeln('td.calendarDateInput {letter-spacing:normal;line-height:normal;font-family:' + FontFamily + ',Sans-Serif;font-size:' + FontSize + 'px;}');
    writeln('select.calendarDateInput {letter-spacing:.06em;font-family:Verdana,Sans-Serif;font-size:11px;}');
    writeln('input.calendarDateInput {letter-spacing:.06em;font-family:Verdana,Sans-Serif;font-size:11px;}');
    writeln('</style>');
}

// Mouse-over for the previous/next month buttons
function NeighborHover(Cell, Over) 
{
    if (Over) 
    {
        VirtualButton(Cell, false);
    }
    else 
    {
        Cell.style.border = 'buttonface 1px solid';
    }
    return true;
}

// Highlights the buttons
function VirtualButton(Cell, ButtonDown) 
{
    if (ButtonDown) 
    {
        Cell.style.borderLeft = 'buttonshadow 1px solid';
        Cell.style.borderTop = 'buttonshadow 1px solid';
        Cell.style.borderBottom = 'buttonhighlight 1px solid';
        Cell.style.borderRight = 'buttonhighlight 1px solid';
    }
    else 
    {
        Cell.style.borderLeft = 'buttonhighlight 1px solid';
        Cell.style.borderTop = 'buttonhighlight 1px solid';
        Cell.style.borderBottom = 'buttonshadow 1px solid';
        Cell.style.borderRight = 'buttonshadow 1px solid';
    }
}

// Displays a message in the status bar when hovering over the calendar days
function DayCellHover(Cell, Over, Color) {
   Cell.style.backgroundColor = (Over) ? DayBGColor : Color;
}

// Returns the number of days in a month (handles leap-years)
function GetDayCount(SomeYear, SomeMonth) {
   return ((SomeMonth == 1) && ((SomeYear % 400 == 0) || ((SomeYear % 4 == 0) && (SomeYear % 100 != 0)))) ? 29 : MonthDays[SomeMonth];
}

// Sets the form elements after a day has been picked from the calendar
function PickDisplayDay(themonth, theday, theyear, basename, nextname) {
	eval('document.getElementById(\'' + fieldPrefix + basename + '_ConCat\').value =\''+(++themonth)+'/'+theday+'/'+theyear+'\'');
	HideCalendar(basename);
	var newMonth = 0;
	var newDay = 0;
	var newYear = 0;
	var dayCount1 = GetDayCount(theyear, themonth-1);
	if((theday+2) > dayCount1){
	    if((theday+1) > dayCount1){
	        if(themonth+1 > 12){
	          newMonth = 1;
	          newDay = 2;
	          newYear = theyear+1;
	        }else{
	          newMonth = themonth+1;
	          newDay = 2;
	          newYear = theyear;
	        }
	    }else{
	        if(themonth+1 > 12){
	          newMonth = 1;
	          newDay = 1;
	          newYear = theyear+1;
	        }else{
	          newMonth = themonth+1;
	          newDay = 1;
	          newYear = theyear;
	        }
	    }
	}else{
      newMonth = themonth;
      newDay = theday + 2;
      newYear = theyear;
	}
	eval('if(nextname == \'EndDate_ConCat\'){ document.getElementById(\'' + fieldPrefix + nextname + '\').value =\''+(newMonth)+'/'+newDay+'/'+newYear+'\'}');
	eval('if(document.getElementById(\'' + fieldPrefix + nextname + '\').type == \'text\'){ document.getElementById(\'' + fieldPrefix + nextname + '\').select(); document.getElementById(\'' + fieldPrefix + nextname + '\').focus()}');
		
}

// Is the specified select-list behind the calendar?
function BehindCal(SelectList, CalLeftX, CalRightX, CalTopY, CalBottomY, ListTopY) {
   var ListLeftX = GetTagPixels(SelectList, 'LEFT');
   var ListRightX = ListLeftX + SelectList.offsetWidth;
   var ListBottomY = ListTopY + SelectList.offsetHeight;
   return (((ListTopY < CalBottomY) && (ListBottomY > CalTopY)) && ((ListLeftX < CalRightX) && (ListRightX > CalLeftX)));
}

// Gets the absolute pixel position of the supplied element
function GetTagPixels(StartTag, Direction) {
   var PixelAmt = (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
   while ((StartTag.tagName != 'BODY') && (StartTag.tagName != 'HTML')) {
      StartTag = StartTag.offsetParent;
      PixelAmt += (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
   }
   return PixelAmt;
}

// For IE, hides any select-lists that are behind the calendar
function FixSelectLists(Over,basename) {
    if (navigator.appName == 'Microsoft Internet Explorer') 
    {
        if(Over){
            hideAllSelects();
            hideFlash();
        }
        else
        {
            showAllSelects();
            showFlash();
        }
    }else
    {
        if(Over)
            hideFlash();
        else
            showFlash();
    }
}

// Builds the HTML for the calendar days
function BuildCalendarDays(themonth, theday, theyear, basename, displaymonth, displayyear, nextname) {
	var Rows = 5;
    var dayCount = GetDayCount(displayyear, displaymonth);
	var FirstDate = new Date(displayyear, displaymonth, 1);
	var firstDay = FirstDate.getDay();
	var nextmonth = displaymonth;
	var nextyear = displayyear;
	if(nextmonth == 11){
		nextmonth = 0;
		nextyear++;
	}else{
		nextmonth++;
	}
	var prevmonth = displaymonth;
	var prevyear = displayyear;
	if(prevmonth == 0){
		prevmonth = 11;
		prevyear--;
	}else{
		prevmonth--;
	}
	var HTML ='<table width="' + (CellWidth * 7) + 'px" cellspacing="0" cellpadding="1">' + String.fromCharCode(13);
		HTML += '<tr style="background-color:' + TopRowBGColor + ';">';
			HTML +='<td style="background-color:#afafaf;" id="StartDate_Previous_ID"';
			if(displayyear == Today.getFullYear()){
				if (displaymonth > Today.getMonth()){
					HTML +=' onMouseOut="NeighborHover(this,false)" onMouseOver="NeighborHover(this,true)" onMouseDown="VirtualButton(this,true)" onMouseUp="VirtualButton(this,false)" onclick="document.getElementById(\'' + basename + '_DayTable_ID\').innerHTML = BuildCalendarDays('+themonth+','+theday+','+theyear+',\''+basename+'\','+prevmonth+','+prevyear+',\''+nextname+'\')"';
				}
			}else{
				HTML +=' onMouseOut="NeighborHover(this,false)" onMouseOver="NeighborHover(this,true)" onMouseDown="VirtualButton(this,true)" onMouseUp="VirtualButton(this,false)" onclick="document.getElementById(\'' + basename + '_DayTable_ID\').innerHTML = BuildCalendarDays('+themonth+','+theday+','+theyear+',\''+basename+'\','+prevmonth+','+prevyear+',\''+nextname+'\')"';
			}

			HTML +=' style="cursor:default" align="center" class="calendarDateInput" style="height:' + CellHeight + 'px" >';
				HTML +='<img src="' + PrevURL + '" width="5px" height="9">';
			HTML +='</td>';
			HTML +='<td id="StartDate_Current_ID" style="cursor:default" align="center" class="calendarDateInput" style="height:' + CellHeight + 'px" colspan="5">'+ MonthNames[displaymonth] +' '+displayyear+'</td>';
			HTML +='<td style="background-color:#afafaf;" id="StartDate_Next_ID" onMouseOut="NeighborHover(this,false)" onMouseOver="NeighborHover(this,true)" onMouseDown="VirtualButton(this,true)" onMouseUp="VirtualButton(this,false)" onclick="document.getElementById(\'' + basename + '_DayTable_ID\').innerHTML = BuildCalendarDays('+themonth+','+theday+','+theyear+',\''+basename+'\','+nextmonth+','+nextyear+',\''+nextname+'\')" style="cursor:default" align="center" class="calendarDateInput" style="height:' + CellHeight + 'px" >';
			HTML +='<img src="' + NextURL + '" width="5px" height="9"></td>';
		HTML +='</tr>';
		HTML +='<tr>';
			for (var w=0;w<7;w++) HTML +='<td width="18px" align="center" class="calendarDateInput" style="height:' + CellHeight + 'px;width:' + CellWidth + 'px;font-weight:bold;border-top:1px solid dimgray;border-bottom:1px solid dimgray;">' + WeekDays[w] + '</td>';
		HTML +='</tr>';
	HTML +='</table>';

   if (((dayCount == 31) && (firstDay > 4)) || ((dayCount == 30) && (firstDay == 6))) Rows = 6;
   else if ((dayCount == 28) && (firstDay == 0)) Rows = 4;
   HTML += '<table width="' + (CellWidth * 7) + 'px" cellspacing="0" cellpadding="1" style="cursor:default">';
   for (var j=0;j<Rows;j++) {
      HTML += '<tr>';
      for (var i=1;i<=7;i++) {
         Day = (j * 7) + (i - firstDay);
         if ((Day >= 1) && (Day <= dayCount)) {
            if ((theyear == displayyear) && (themonth == displaymonth) && (Day == theday)) {
               TextStyle = 'color:white;font-weight:bold;'
               BackColor = DayBGColor;
            }
            else {
               TextStyle = 'color:black;'
               BackColor = CalBGColor;
            }
            if ((displayyear == Today.getFullYear()) && (displaymonth == Today.getMonth()) && (Day == Today.getDate())) TextStyle += 'border:1px solid darkred;padding:0px;';
            HTML += '<td align="center" class="calendarDateInput"';
			if ((displayyear == Today.getFullYear()) && (displaymonth == Today.getMonth())){
				if(Day >= Today.getDate()){
					HTML += ' onMouseOver="DayCellHover(this,true,\'' + BackColor + '\')" onMouseOut="DayCellHover(this,false,\'' + BackColor + '\')" onclick="PickDisplayDay('+displaymonth+','+Day+','+displayyear+',\''+basename+'\',\''+nextname+'\')" ';
				}else{
					TextStyle = 'color:#c3c2c2;';
				}
			}else{
				if(displaymonth < Today.getMonth()){
					if(displayyear < Today.getFullYear()){
						TextStyle = 'color:#c3c2c2;';
					}else{
						HTML += ' onMouseOver="DayCellHover(this,true,\'' + BackColor + '\')" onMouseOut="DayCellHover(this,false,\'' + BackColor + '\')" onclick="PickDisplayDay('+displaymonth+','+Day+','+displayyear+',\''+basename+'\',\''+nextname+'\')" ';
					}
				}else{
					if(displayyear < Today.getFullYear()){
						TextStyle = 'color:#c3c2c2;';
					}else{
						HTML += ' onMouseOver="DayCellHover(this,true,\'' + BackColor + '\')" onMouseOut="DayCellHover(this,false,\'' + BackColor + '\')" onclick="PickDisplayDay('+displaymonth+','+Day+','+displayyear+',\''+basename+'\',\''+nextname+'\')" ';
					}
				}
			}
			HTML += ' style="cursor:default;height:' + CellHeight + 'px;width:' + CellWidth + 'px;' + TextStyle + ';background-color:' + BackColor + '">' + Day + '</td>';
         }
         else HTML += '<td class="calendarDateInput" style="height:' + CellHeight + 'px">&nbsp;</td>';
      }
      HTML += '</tr>';
   }
   return HTML += '</table><table width="' + (CellWidth * 7) + 'px" cellspacing="0" cellpadding="1"><TR><TD align="center" style="cursor:pointer" onclick="HideCalendar(\''+basename+'\')">Cancel</TD></TR></TABLE>';
}

// Show or hide the calendar
function ShowCalendar(basename,nextname)
{
	eval('thedate = document.getElementById(\'' + fieldPrefix + basename + '_ConCat\').value');
	if(document.getElementById(fieldPrefix+'StartDate_ConCat').value != 'mm/dd/yyyy' && document.getElementById(fieldPrefix+'EndDate_ConCat').value == 'mm/dd/yyyy')
		thedate = document.getElementById(fieldPrefix+'StartDate_ConCat').value;
	var firstslash = thedate.search('/');
	var themonth = thedate.substring(0,firstslash);
	var thedayyear = thedate.substring(firstslash+1,thedate.length);
	var secondslash = thedayyear.search('/');
	var theday = thedayyear.substring(0,secondslash);
	var theyear = thedayyear.substring(secondslash+1,thedayyear.length);
	if (isNaN (themonth) || isNaN (theday) || isNaN (theyear)){
		theday = Today.getDate();
		themonth = Today.getMonth();
		theyear = Today.getFullYear();
	}
	if (theday > 0 && themonth > 0 && theyear > 0){
		if (theyear.length == 4 && themonth > 0 && themonth <= 12){
			if(theday <= GetDayCount(theyear, themonth-1)){
				themonth = --themonth;
				theyear = theyear;
				theday = theday;
			}else{
				theday = Today.getDate();
				themonth = Today.getMonth();
				theyear = Today.getFullYear();
			}
		}else{
			theday = Today.getDate();
			themonth = Today.getMonth();
			theyear = Today.getFullYear();
		}
	}else{
		theday = Today.getDate();
		themonth = Today.getMonth();
		theyear = Today.getFullYear();
	}
	eval('document.getElementById(\''+basename+'_DayTable_ID\').innerHTML = BuildCalendarDays(themonth,theday,theyear,basename,themonth,theyear,nextname)');
	eval('document.getElementById(\'' + basename + '_ID\').style.position=\'absolute\'');
	eval('document.getElementById(\'' + basename + '_ID\').style.zIndex=\'100\'');
	eval('document.getElementById(\'' + basename + '_ID\').style.visibility = \'visible\'');
	FixSelectLists(true,basename);
}
//Hides a calendar
function HideCalendar(basename)
{
	eval('document.getElementById(\'' + basename + '_ID\').style.visibility = \'hidden\'');
	FixSelectLists(false,basename);
}

function hideFlash() {
    if (document.getElementById('flashcontent') != null)
        document.getElementById('flashcontent').style.display = 'none';
}

function showFlash() {
    if (document.getElementById('flashcontent') != null)
        document.getElementById('flashcontent').style.display = '';
}

function getLeft(obj) {
    var xPos = obj.offsetLeft;

    var tempEl = obj.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function getTop(obj) {
    var yPos = obj.offsetTop;
    var tempEl = obj.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}
