// destination search form validation controller
function isValidSearchForm(as_phoneNumber) {  
    if (!isValidGateway()) {
        return (false);
    }
    
    if (!isValidDestination()){
        return (false);
    }

    if (!isValidReturnDate()){
        return (false);
    }

    if (!isValidDepartureDate(as_phoneNumber)){
        return (false);
    }

    if (!isValidNumOfPax()){
        return (false);
    }

    if (!isValidTIDateRange(as_phoneNumber)){
        return (false);
    }
    
    if (!isChildAgeValid(as_phoneNumber)){
		return (false);		
	}

    return(true);
}

// destination search form validation controller
function isValidMatrixSearchForm(as_phoneNumber) {  
   
    if (!isValidReturnDate()){
        return (false);
    }

    if (!isValidDepartureDate(as_phoneNumber)){
        return (false);
    }
    
	if (!isValidTIDateRange(as_phoneNumber)){
        return (false);
    }
	
    return(true);
}


function isValidBPSearch(as_phoneNumber) {   
	//alert('test');
	//alert('type of  ' +  typeof(isValidGateway()) );
	
    if (!isValidGatewayNoDestTest()) {
        return (false);
    }
    
    if (!isValidReturnDate()){
        return (false);
    }

    if (!isValidDepartureDate(as_phoneNumber)){
        return (false);
    }

    if (!isValidNumOfPax()){
        return (false);
    }

    if (!isValidTIDateRange(as_phoneNumber)){
        return (false);
    }
	
	if (!isChildAgeValid(as_phoneNumber)){
		return (false);		
	}

    return(true);
}


// activity search form validation controller
function isValidActivitySearchForm() {

	setDestIndex();
	resumMask2('SearchForm');

	if (!hasValidActivities()) {
		return (false);
	}

    if (!isValidGateway()) {
        return (false);
    }

    if (!isValidDestination()) {
        return (false);
    }

	return(true);
}

// activity search form validation controller no resumMask2
function isValidPreSetActivitySearchForm() {
	setDestIndex();
	if (!hasValidActivities()) {
		return (false);
	}
    if (!isValidGateway()) {
        return (false);
    }
    if (!isValidDestination()) {
        return (false);
    }
	return(true);
}
// resort search form validation controller
function isValidResortSearchForm() {
    //if there are no text elements, this function
    //will throw an error in IE6
    if (!document.SearchForm.R)
        return true;

    if (!hasValidFormValue()) {
        return (false);
    }

    if (!hasValidChars()) {
        return (false);
    }

    return(true);
}

// destination search form validation controller
function isValidPromoForm() {  
    //o	If the departure gateway is set the same as the destination airport, the default value is chosen and a popup is given to the user that says, "Departure gateway must differ from your selected destination".
    var gatewayInfo;
    var gatewayCode;

    gatewayInfo = document.SearchForm.gateway.options[document.SearchForm.gateway.selectedIndex].value;
    gatewayCode = gatewayInfo.substring(0,3);

    if (gatewayCode == '') {
        showErrorMsg(document.SearchForm.gateway, 'Please select a departure city.');
        return (false);
    }
    return(true);
}


function endOfMonth(adt_date) {
    var li_month = new Date(adt_date).getMonth()  + 1 ; 
    switch (parseInt(li_month)) {
        case 1: //Jan
        case 3: //Mar
        case 5: //May
        case 7: //Jul
        case 8: //Aug
        case 10: //Oct
        case 12: //Dec
            return 31;
        case 4: //Apr
        case 6: //Jun
        case 9: //Sept
        case 11: //Nov
            return 30;
        case 2: //Feb
            return ( (((new Date(adt_date).getYear()) % 4) == 0) ? 29 : 28);
    } // end switch
} // end endOfMonth


function updateDepartMonth(as_formName) {
	//alert('updateDepartMonth');
	monthChange(document.forms[as_formName].departDate_year_month,document.forms[as_formName].departDate_Day);
}

function updateReturnMonth(as_formName) {
	//alert('updateReturnMonth');
	monthChange(document.forms[as_formName].returnDate_year_month,document.forms[as_formName].returnDate_Day);
}


function monthChange(ao_month, ao_day) {

    var ldt_selectedDate =  new Date(ao_month.options[ao_month.selectedIndex].value+ '/01');

    var li_DaysSelectedMonth = endOfMonth(ldt_selectedDate);

    var li_DaysActiveMonth = ao_day.options[ao_day.length - 1].value;

    var li_difference = li_DaysSelectedMonth - li_DaysActiveMonth;

	var li_selectedIndex = ao_day.selectedIndex;
	if (li_selectedIndex == -1) {
		li_selectedIndex = 0;
	}
	
    var li_selectedValue = ao_day.options[li_selectedIndex].value;

    if (li_DaysSelectedMonth > li_DaysActiveMonth) {
            for (var li_counter = 1; li_counter <= li_difference; li_counter++) { 
                var lo_newOption = new Option( parseInt(li_DaysActiveMonth) + li_counter, parseInt(li_DaysActiveMonth) + li_counter);
                ao_day.options[ao_day.length] = lo_newOption;
            }
    } 
    if  (li_DaysSelectedMonth < li_DaysActiveMonth) {            
            for (var li_counter = 1; li_counter <= -li_difference; li_counter++) { 
                ao_day.options[ao_day.length - 1] = null;
            }
    }        
    
    //alert ('nsnothing');
    //make sure that the user hasn't selected a date that no longer exists in the new month
    if (li_selectedValue > li_DaysSelectedMonth) {
        ao_day.selectedIndex = ao_day.length - 1;
        }
}

//this is a weird function but this is only for a calendar click, we expand 
//the month dropdown to the max number of days

function maxdepartDateLength(as_formName) {
    var ao_day = document.forms[as_formName].departDate_Day;
    
    var li_DaysActiveMonth = parseInt(ao_day.options[ao_day.length - 1].value);
    
    //alert('test!!!' + li_DaysActiveMonth);
    for (var li_counter = 1; li_counter <= (31 - li_DaysActiveMonth); li_counter++) { 
	
	var lo_newOption = new Option( li_DaysActiveMonth + li_counter, li_DaysActiveMonth + li_counter);
	ao_day.options[ao_day.length] = lo_newOption;
    }
}



//this is another weird function but this is only for a calendar click, we expand 
//the month dropdown to the max number of days

function maxreturnDateLength(as_formName) {
    var ao_day = document.forms[as_formName].returnDate_Day;
    
    var li_DaysActiveMonth = parseInt(ao_day.options[ao_day.length - 1].value);
    
    //alert('test!!!' + li_DaysActiveMonth);
    for (var li_counter = 1; li_counter <= (31 - li_DaysActiveMonth); li_counter++) { 
	
	var lo_newOption = new Option( li_DaysActiveMonth + li_counter, li_DaysActiveMonth + li_counter);
	ao_day.options[ao_day.length] = lo_newOption;
    }
}






// destination search validation
function isValidGateway(){
    //o	If the departure gateway is set the same as the destination airport, the default value is chosen and a popup is given to the user that says, "Departure gateway must differ from your selected destination".
    var gatewayInfo;
    var gatewayCode;
	var destinationCode

    gatewayInfo = document.SearchForm.gateway.options[document.SearchForm.gateway.selectedIndex].value;
    gatewayCode = gatewayInfo.substring(0,3);

    if (gatewayCode == '') {
        showErrorMsg(document.SearchForm.gateway, 'Please select a departure gateway.');
        return (false);
    }
    
	if (document.SearchForm.destinationcode.options) {
		destinationCode = document.SearchForm.destinationcode.options[document.SearchForm.destinationcode.selectedIndex].value;
    }
	else
	{
		destinationCode = document.SearchForm.destinationcode.value;
	}
	
    if (gatewayCode == destinationCode){
        showErrorMsg(document.SearchForm.gateway, 'Departure gateway must differ from your selected destination.');
        return (false);
    }
    
    return(true);
}


// destination search validation
function isValidGatewayNoDestTest(){
    //o	If the departure gateway is set the same as the destination airport, the default value is chosen and a popup is given to the user that says, "Departure gateway must differ from your selected destination".
    var gatewayInfo;
    var gatewayCode;

    gatewayInfo = document.SearchForm.gateway.options[document.SearchForm.gateway.selectedIndex].value;
    gatewayCode = gatewayInfo.substring(0,3);

    if (gatewayCode == '') {
        showErrorMsg(document.SearchForm.gateway, 'Please select a departure gateway.');
        return (false);
    }
    
    return(true);
}


function isValidDestination(){    
    //o	If the destination airport is set the same as the departure gateway, the default value is chosen and a popup is given to the user that says, "Your selected destination must differ from your departure gateway".
    var destinationCode;
    var gatewayInfo;
    var gatewayCode;

    gatewayInfo = document.SearchForm.gateway.options[document.SearchForm.gateway.selectedIndex].value;
    gatewayCode = gatewayInfo.substring(0,3);
	
	if (document.SearchForm.destinationcode.options) {
		destinationCode = document.SearchForm.destinationcode.options[document.SearchForm.destinationcode.selectedIndex].value;
    }
	else
	{
		destinationCode = document.SearchForm.destinationcode.value;
	}
	
    if (gatewayCode == destinationCode){
        showErrorMsg(document.SearchForm.destinationcode, 'Departure destination must differ from your selected gateway.');
        return (false);
    }
    
    if (destinationCode == '' || destinationCode == '--'){
        showErrorMsg(document.SearchForm.destinationcode, 'Please select a destination.');        
        return (false);
    }
    return(true);
}



function isValidReturnDate(){    
    // we no longer use the complicated of before and after august search dates....all dates inside the drop downs are valid
    // see 7574 for details
    return true;
    //o	When the current date is 7/31 or earlier and the return date is beyond 12/31 of the current year, the return date must be moved back to 12/31 of the current year and give user a popup that says, "Return date must be before the end of this year.".    
    //o	When the current date is after 7/31, and return date is after 12/31 of the following year, the return date must be moved back to 12/21 of the following year and give user a popup that says, "Return date must be before the end of next year.".
    //o	When the return date is set equal to or prior to the departure date, push the departure date back by 7 days from the return date.
    //o	When the departure date is set earlier than 21 days before the return date, bring the return date in such that is 21 days after departure date and give user a popup that says, "Please call 1-800-XXX-XXXX  for vacation stays longer the 21 days.".

    var currentDate;
    var returnDate;
    var returnYear;
    var returnMonth;
    var returnDay;
    var returnMonthYear;
    var midYearDate;
    var endYearDate;
    var endOfFollowingYearDate;
    
    currentDate = new Date();
    midYearDate = new Date(currentDate.getFullYear(), 6, 31);
    endYearDate = new Date(currentDate.getFullYear(), 12, 31);
    endOfFollowingYearDate = new Date(currentDate.getFullYear()+1, 12, 31);
    returnMonthYear = document.SearchForm.returnDate_year_month.options[document.SearchForm.returnDate_year_month.selectedIndex].value;
    returnYear      = returnMonthYear.substring(0,4);
    returnMonth     = returnMonthYear.substring(5,returnMonthYear.length);
    returnDay       = document.SearchForm.returnDate_Day.options[document.SearchForm.returnDate_Day.selectedIndex].value;   
    returnDate      = new Date(returnYear, returnMonth-1, returnDay);
    
    if (currentDate <= midYearDate && returnDate > endYearDate){
        //move return date to 12/31
        showErrorMsg(document.SearchForm.returnDate_year_month, 'Return date must be before the end of this year.'); 
        return (false);
    }
    else if (currentDate > midYearDate && returnDate > endOfFollowingYearDate){
        //move return date to 12/21/yyyy - 1
        showErrorMsg(document.SearchForm.returnDate_year_month, 'Return date must be before the end of next year.'); 
        return (false);
    }
    
    return(true);
}

function isValidDepartureDate(as_phoneNumber){
    //o	When the departure date is set equal to or after the return date, push the return date out by 7 days from the departure date and give user a popup that says, "Departure date must precede return date.".

	var departMonthYear;
    var returnMonthYear;
    var returnDate;
    var departDate;
    var currentDate;
	var dateOffset;
	dateOffset = 1000 * 60 * 60 * 24 * 7;
    var temp  = new Date()
	var year = temp.getYear();
	if (year < 2000)
	{
		year = year + 1900;
	} 
    var today = new Date(new Date(year, temp.getMonth(), temp.getDate()));   

    currentDate = new Date();
    departMonthYear = document.SearchForm.departDate_year_month.options[document.SearchForm.departDate_year_month.selectedIndex].value;
    returnMonthYear = document.SearchForm.returnDate_year_month.options[document.SearchForm.returnDate_year_month.selectedIndex].value;  
    returnDate = new Date(returnMonthYear.substring(0,4), returnMonthYear.substring(5,returnMonthYear.length)-1, document.SearchForm.returnDate_Day.options[document.SearchForm.returnDate_Day.selectedIndex].value);
    departDate = new Date(departMonthYear.substring(0,4), departMonthYear.substring(5,departMonthYear.length)-1, document.SearchForm.departDate_Day.options[document.SearchForm.departDate_Day.selectedIndex].value);
        
    var minDepartDate = new Date()
    minDepartDate.setDate(minDepartDate.getDate() + 4);
    minDepartDate.setHours(0);
    minDepartDate.setMinutes(0);
    minDepartDate.setSeconds(0);
    //var minDepartDate = new Date(today.valueOf() + (dateOffset));
    departDate.setSeconds(departDate.getSeconds() + 1);

    if (currentDate.valueOf() >= departDate.valueOf()){
        var message;
		var formattedDate;

		formattedDate = formatDate(minDepartDate,'MMM dd, yyyy');        
		message = 'Date of departure must be on or after ' + formattedDate + '.\nPlease call our Travel Specialists at ' + as_phoneNumber + ' if you\'d like to book prior to this date.';
		showErrorMsg(document.SearchForm.departDate_year_month, message);
        return (false);
    }
    else if (departDate.valueOf() < minDepartDate.valueOf()){        
		var message;
		var formattedDate;

		formattedDate = formatDate(minDepartDate,'MMM dd, yyyy');        
		message = 'Date of departure must be on or after ' + formattedDate + '.\nPlease call our Travel Specialists at ' + as_phoneNumber + ' if you\'d like to book prior to this date.';
		showErrorMsg(document.SearchForm.departDate_year_month, message);
        return (false);
    }
    else if (departDate.valueOf() >= returnDate.valueOf()){
        showErrorMsg(document.SearchForm.departDate_year_month, 'Departure date must precede return date.');
        return (false);        
    }  
    return(true);
}


function isValidLapChildToAdultRatio(childNumber)
	{
	
	
	adultPaxCount = document.SearchForm.PassengerAdultCount.options[document.SearchForm.PassengerAdultCount.selectedIndex].value;
	childPaxCount = document.SearchForm.PassengerChildCount.options[document.SearchForm.PassengerChildCount.selectedIndex].value;
	var childAge;
	var lapChildCount = 0;
	for(var i=0;i<childPaxCount;i++)
		{
		childAge = eval("document.all.childAge_"+(i+1)+".value");
		//alert(childAge);
		if ((childAge == 0) || (childAge == 1))
			{
			lapChildCount++;
			//alert('One lap child here!');
			}
		}
	//alert('lapchildren: ' + lapChildCount + ' adultPaxCount: ' + adultPaxCount);
	if (lapChildCount > adultPaxCount)
		{
		//alert(childNumber);
		eval("document.all.childAge_" + childNumber + ".selectedIndex = 2");
		showErrorMsg(document.SearchForm.gateway, 'There must be at least one adult for each child under the age of one.');
		}

	}

//if a child under 2 is selected and search is hit then display this message and disable the search
function isChildAgeValid (as_phonenumber)
	{
		childPaxCount = document.SearchForm.PassengerChildCount.options[document.SearchForm.PassengerChildCount.selectedIndex].value;
		//alert(childPaxCount);
		
		var childAge;
		var childAgeDD;
		var lapChildCount = 0;
		var j = 0;
		for(var i=0;i<childPaxCount;i++)
		{
			j = (i+1);
			childAgeDD = ("childAge_" + j);
			//alert(childAgeDD);
			childAge = eval("document.SearchForm." + childAgeDD + ".options[document.SearchForm." + childAgeDD + ".selectedIndex].value");
			//alert(childAge);
		    if (childAge == -1) 
			   {
			   lapChildCount++;
			   showErrorMsg(document.SearchForm.gateway, 'You must select an age for each child.');
			   return(false);
			   }
			else if ((childAge == 0) || (childAge == 1))
				{
				lapChildCount++;
				showErrorMsg(document.SearchForm.gateway, 'Note: Vacation Packages with Children under 2 years old cannot be booked online.  To book your vacation package with a child under 2 years, please call ' + as_phonenumber + '.');
				return(false);
				}
		}  
		return (true);
	}

//if not netscape display this disclaimer if a child under 2 is selected
function displaychilddisclaimer()
	{
		var childAge;
		var childPaxCount = document.SearchForm.PassengerChildCount.options[document.SearchForm.PassengerChildCount.selectedIndex].value;
		for(var i=0;i<childPaxCount;i++)
		{
			childAge = eval("document.all.childAge_"+(i+1)+".value");
			//alert(childAge);
			if ((childAge == 0) || (childAge == 1))
				{
				document.all.disclaimer.style.display='block';
				}
			else
				{
				eval("document.all.disclaimer.style.display='none'");
				}
		}
	}
	
function isValidNumOfPax(as_phonenumber){

    //Adults & children cannot total more than 7
    //Cannot be more <1 children than adults
    //When this is exception is encountered, the pax numbers are reset to their default values
    var adultPaxCount;
    var childPaxCount;
    
    adultPaxCount = document.SearchForm.PassengerAdultCount.options[document.SearchForm.PassengerAdultCount.selectedIndex].value;
    childPaxCount = document.SearchForm.PassengerChildCount.options[document.SearchForm.PassengerChildCount.selectedIndex].value;


    if (parseInt(adultPaxCount) + parseInt(childPaxCount) > 7){
		showErrorMsg(document.SearchForm.PassengerAdultCount, 'The maximum number of total passengers (adults and children) is 7. \r\nPlease call ' + as_phonenumber + ' to book more than 7 passengers.');
        document.SearchForm.PassengerAdultCount.selectedIndex = 1;
        document.SearchForm.PassengerChildCount.selectedIndex = 0;
		document.all.childageframe.style.display='none';
		
        document.all.cages0.style.display='none';
        document.all.label0.style.display='none';
        document.all.disclaimer.style.display='none';
        document.all.cages1.style.display='none';
        document.all.label1.style.display='none';
        document.all.disclaimer.style.display='none';
        document.all.cages2.style.display='none';
        document.all.label2.style.display='none';
        document.all.disclaimer.style.display='none';

        return (false);
    }
    else
    {
		if (parseInt(childPaxCount) > 0)
		{
			document.all.childageframe.style.display='block';
		
			document.all.cages0.style.display='block';
			document.all.label0.style.display='block';
			document.all.disclaimer.style.display='block';
			document.all.cages1.style.display='block';
			document.all.label1.style.display='block';
			document.all.disclaimer.style.display='block';
			document.all.cages2.style.display='block';
			document.all.label2.style.display='block';
			document.all.disclaimer.style.display='block';
        }
    }
        
    return(true);
}

function isValidNumOfPaxOrbHome(as_phonenumber){
    //Adults & children cannot total more than 9
    //Cannot be more <1 children than adults
    //When this is exception is encountered, the pax numbers are reset to their default values
    var adultPaxCount;
    var childPaxCount;
    
    adultPaxCount = document.SearchForm.PassengerAdultCount.options[document.SearchForm.PassengerAdultCount.selectedIndex].value;
    childPaxCount = document.SearchForm.PassengerChildCount.options[document.SearchForm.PassengerChildCount.selectedIndex].value;
    if (parseInt(adultPaxCount) + parseInt(childPaxCount) > 9){
		showErrorMsg(document.SearchForm.PassengerAdultCount, 'The maximum number of total passengers (adults and children) is 9. \r\nPlease call ' + as_phonenumber + ' to book more than 9 passengers.');
        document.SearchForm.PassengerAdultCount.selectedIndex = 1;
        document.SearchForm.PassengerChildCount.selectedIndex = 0;
     /*   document.all.cages0.style.display='none';
        document.all.label0.style.display='none';
        document.all.disclaimer.style.display='none';
        document.all.cages1.style.display='none';
        document.all.label1.style.display='none';
        document.all.disclaimer.style.display='none';
        document.all.cages2.style.display='none';
        document.all.label2.style.display='none';
        document.all.disclaimer.style.display='none';
	*/
        return (false);
    }
    
    return(true);
}
function showErrorMsg(oField, sMsg){
    alert(sMsg);
    oField.focus;
}



function adjustReturnDate(as_formName){
    var departDate;
    var departMonthYear;
    var newNewReturnDate;
    var index = -1;
    var i;
    var exceededDateBounds = false;
    
    if (('' + as_formName + '') == 'undefined') {
    	as_formName = 'SearchForm';
    }
    
    
    
    departMonthYear = document.forms[as_formName].departDate_year_month.options[document.forms[as_formName].departDate_year_month.selectedIndex].value;
    departDate = new Date(departMonthYear.substring(0,4), departMonthYear.substring(5,departMonthYear.length)-1, document.forms[as_formName].departDate_Day.options[document.forms[as_formName].departDate_Day.selectedIndex].value);
    newReturnDate = new Date(departDate.getFullYear(), departDate.getMonth(), departDate.getDate()+7)

    for (var i=0; i < document.forms[as_formName].returnDate_year_month.length; i++) {  
        if (document.forms[as_formName].returnDate_year_month.options[i].value == (newReturnDate.getFullYear() + "/" + (newReturnDate.getMonth()+1))){
            index = i;
        }
    }
    
    if (index == -1) {
        index = (document.forms[as_formName].returnDate_year_month.length - 1);
        exceededDateBounds = true;
    }
    
    document.forms[as_formName].returnDate_year_month.selectedIndex = index;
    
    updateReturnMonth(as_formName);
    
    //reset the index value so we can trap if the proper day is not found
    index=-1
    for (var i=0; i < document.forms[as_formName].returnDate_Day.length; i++) {  
        if (document.forms[as_formName].returnDate_Day.options[i].value == newReturnDate.getDate()){
           index = i;
           //i = document.forms[as_formName].returnDate_Day.length; //exit loop
        }        
    }   
    
    if ((index == -1) || (exceededDateBounds == true)) {
        index = (document.forms[as_formName].returnDate_Day.length-1);
    }
        
    //alert(index);
    //alert(newReturnDate.getDate());
    document.forms[as_formName].returnDate_Day.selectedIndex = index;
} 


function isValidTIDateRange(as_displayPhone){
    var departMonthYear;
    var returnMonthYear;
    var returnDate;
    var departDate;
    
    departMonthYear = document.SearchForm.departDate_year_month.options[document.SearchForm.departDate_year_month.selectedIndex].value;
    returnMonthYear = document.SearchForm.returnDate_year_month.options[document.SearchForm.returnDate_year_month.selectedIndex].value;
    
    returnDate = new Date(returnMonthYear.substring(0,4), returnMonthYear.substring(5,returnMonthYear.length)-1, document.SearchForm.returnDate_Day.options[document.SearchForm.returnDate_Day.selectedIndex].value);
    departDate = new Date(departMonthYear.substring(0,4), departMonthYear.substring(5,departMonthYear.length)-1, document.SearchForm.departDate_Day.options[document.SearchForm.departDate_Day.selectedIndex].value);  
    
    if(Math.floor((returnDate.getTime() - departDate.getTime()) / (1000 * 60 * 60 * 24) ) > 21){
       if ( (''+as_displayPhone+'') == 'undefined') {
       	showErrorMsg(document.SearchForm.returnDate_year_month, 'Please call for vacations lasting longer than 21 days.');
	} else {
	showErrorMsg(document.SearchForm.returnDate_year_month, 'Please call ' + as_displayPhone + ' to book a vacation lasting longer than 21 days.');
	}
       return (false);
    }
    
    return(true);
}

// resort search validation
function hasValidFormValue(){
    if ((document.SearchForm.R.value.length == 0) &&
           (document.SearchForm.C.value.length == 0) &&
           (document.SearchForm.S.value.length == 0)) {
		alert("Please enter at least one search phrase.");
		document.SearchForm.R.focus();
		return (false);
	}
	
	return(true);
}

function hasValidChars() {

	var invalidchars = "`~!@#$^&*_=+[{]}\\|;:\"<>/?"; 
	//Note: commas, parentheses & the wildcard (%) are permitted
	for (i=0; i<invalidchars.length; i++)
	{
		badchar = invalidchars.charAt(i);
		if (document.SearchForm.R.value.indexOf(badchar,0) >= 0)
		{
			alert('No special characters are permitted.\n Use \'%\' as a wildcard, if necessary.');
			document.SearchForm.R.value = "";
			document.SearchForm.R.focus();
		return (false);
			}
		if (document.SearchForm.C.value.indexOf(badchar,0) >= 0)
		{
			alert('No special characters are permitted.\n Use \'%\' as a wildcard, if necessary.');
			document.SearchForm.C.value = "";
			document.SearchForm.C.focus();
		return (false);
		}
		if (document.SearchForm.S.value.indexOf(badchar,0) >= 0)
		{
			alert('No special characters are permitted.\n Use \'%\' as a wildcard, if necessary.');
			document.SearchForm.S.value = "";
			document.SearchForm.S.focus();
			return (false);
		}
	}

	return(true);

}

// activity validation functions

//Form Validation on the activity value
function hasValidActivities()
{
	if (document['SearchForm'].maskvalue.value == 0)
	{
		alert("Please select at least one activity");
		return (false);
	} 
	return(true);
}

// Sum the checked activity values
function updateMask(ai_value, ab_checked, as_formName) {
	resumMask2(as_formName);
//	var li_activeValue = parseInt(document[as_formName].maskvalue.value);
//	if (ab_checked) {
//		li_activeValue += parseInt(ai_value);
//	} else {
//		li_activeValue -= parseInt(ai_value);
//	}
//	document[as_formName].maskvalue.value = li_activeValue;
}

// re-sum the activity values, when a user comes back to a page
function resumMask(ai_value, ab_checked, as_formName) {
	var li_activeValue = parseInt(document[as_formName].maskvalue.value);
	if (ab_checked) {
		li_activeValue += parseInt(ai_value);
	}
	document[as_formName].maskvalue.value = li_activeValue;
}
// sum checks on initial load
function resumMask2(as_formName){
	document[as_formName].maskvalue.value = 0;
    for (var i = 0; i < document[as_formName].elements.length; i++){
        var thisElement = document[as_formName].elements[i];
        if (thisElement.type == "checkbox") {
	       	if (thisElement.checked) {
				document[as_formName].maskvalue.value = parseInt(document[as_formName].maskvalue.value) + parseInt(thisElement.value);
			}
        }
    } return(true);
}

// reset the form values
function resetMask(as_formName) {
	document[as_formName].reset();
}

//set the index of the dest drop so we can use it later
function setDestIndex(as_formName){
	document.SearchForm.dindex.value = document.SearchForm.destinationcode.selectedIndex;
}

//check to see if the departure date is in the last 7 days of december.
//if so, notify the user that trips cannot be booked that far in advance
//and manage the return date accordingly.
function checkDepartDate(ai_lastAvailableYear, ai_phone, as_operationHours) {    
    var selectedYearMonth = document.SearchForm.departDate_year_month.options[document.SearchForm.departDate_year_month.selectedIndex].value;    
    var selectedDay = document.SearchForm.departDate_Day.options[document.SearchForm.departDate_Day.selectedIndex].value;    
    var selectedYear = selectedYearMonth.substr(0, 4);
    var selectedMonth = selectedYearMonth.substr(5, (selectedYearMonth.length+1));
    var dateRangeErrorMsg = "";
    var workDate = new Date();
    var thisYear = workDate.getFullYear();
    if(ai_lastAvailableYear == thisYear) {
        dateRangeErrorMsg =   "Please note that we can only book vacations online through the end of this year.  Please call " + ai_phone + " to continue booking your vacation with one of our Vacation Specialists, available from " + as_operationHours + "."
    } else {
        dateRangeErrorMsg =   "Please note that we can only book vacations online through the end of next year.  Please call " + ai_phone + " to continue booking your vacation with one of our Vacation Specialists, available from " + as_operationHours + "."
    }
    
    if ((selectedYear == ai_lastAvailableYear) && (selectedMonth == "12")) {
        if (parseInt(selectedDay) > 24) {
            alert(dateRangeErrorMsg);            
        }
    }    
}
