//todo: validation is not stopping submit in firefox

//default value to be displayed in search box if value is empty
var defaultSearchBoxText = 'Hotel, attraction, location or event';
var defaultSearchBoxVenueText = 'Location, town or city';
//id of the form containing controls to be validated
var formId = 'findhotel';
//id of the search box to be validated
var searchBoxId = 'search';
//colour to be used for default search box text
var defaultSearchBoxColour = '#505050';

//function to call any javascript needed to initialise controls on window load
function InitWideSearch()
{    
    ResetSearchBox(searchBoxId); 
    
    if ($(formId) != null)
    {
        Event.observe($(formId), 'submit', this.CheckSearchBoxData.bindAsEventListener(this));
    }
}

//function used by search box to hide / show advanced search options
//param 'id' -> A string representing the id of the control to update the classname
function SwitchSearchExtraVisible(id)
{
    var searchExtra = $(id);
    if (searchExtra.className == 'searchExtrasHidden')
    {
        searchExtra.className = 'searchExtras';
    }
    else
    {
        searchExtra.className = 'searchExtrasHidden';
    }
}

//function used by search box to hide / show search popups
//param 'id' -> A string representing the id of the control to update the classname
function SwitchSearchPopupVisible(id)
{
    var searchExtra = $(id);
    if (searchExtra.className == 'searchExtrasHidden')
    {
        searchExtra.className = 'searchExtrasPopup';
    }
    else
    {
        searchExtra.className = 'searchExtrasHidden';
    }
}

//function called on page load to add default value to search box if value is empty
// param 'id' -> A string representing the id of the search box
function ResetSearchBox(id)
{
    var control = $(id);
    
    if (control != null)
    {
        if (control.value == '')
        {
            control.style.color = defaultSearchBoxColour;
            control.value = defaultSearchBoxText;
        }
    }
}

//function called on focus of a search box to remove default value if required
//param 'control' -> An object representing the control to be updated
function SearchBoxFocus(control)
{
    if (control.value == defaultSearchBoxText)
    {
        control.value = '';
        control.style.color = "#000000";
    }
}

//function called on blur of a venue search box to add default value if required
//param 'control' -> An object representing the control to be updated
function VenueSearchBoxBlur(control)
{
    if (control.value == '')
    {
        control.style.color = defaultSearchBoxColour;
        control.value = defaultSearchBoxVenueText;
    }
}

//function called on focus of a venue search box to remove default value if required
//param 'control' -> An object representing the control to be updated
function VenueSearchBoxFocus(control)
{
    if (control.value == defaultSearchBoxVenueText)
    {
        control.value = '';
        control.style.color = "#000000";
    }
}

//function called on blur of a search box to add default value if required
//param 'control' -> An object representing the control to be updated
function SearchBoxBlur(control)
{
    if (control.value == '')
    {
        control.style.color = defaultSearchBoxColour;
        control.value = defaultSearchBoxText;
    }
}

//function called on change of room number drop down list to check that value is not greater than 3
//param 'control' -> An object representing the drop down list to be validated
//param 'id' -> A string representing the id of the popup warning panel to be displayed
function CheckNumberOfRooms(control, id)
{
    if (control.value > 3)
    {
        SwitchSearchPopupVisible(id);
        control.value = 1;
    }
}

function PromotionCodeCheck()
{
    var control = $('txtSearchPromotion');
    
    if (control.value != '')
    {
        return false;
    }
    else
    {
        return true;
    }
}

function GcciNumberCheck()
{
    var control = $('txtSearchGcci');
    
    if (control.value != '')
    {
        return false;
    }
    else
    {
        return true;
    }
}

function CorporateIdCheck()
{
    var control = $('txtSearchCorp');
    
    if (control.value != '')
    {
        return false;
    }
    else
    {
        return true;
    }
}

//function called when user clicks to enter corp id.
//ensures that corporate id cannot be entered in conjunction with promo code or gcci number
function CheckForPromoCodeOrGcci()
{
    if (!PromotionCodeCheck())
    {
        SwitchSearchPopupVisible('searchPromoCorpError');
    }
    else if (!GcciNumberCheck())
    {
        SwitchSearchPopupVisible('searchGcciCorpError');
    }
    else
    {
        SwitchSearchExtraVisible('searchExtraCorporate');
    }
}

//function called when user clicks to enter promo code.
//ensures that both corporate id and promo code cannot be entered
function CheckForCorporateIdPromo()
{
    if (!CorporateIdCheck())
    {
        SwitchSearchPopupVisible('searchPromoCorpError');
    }
    else
    {
        SwitchSearchExtraVisible('searchExtraPromotion');
    }
}

//function called when user clicks to enter gcci number.
//ensures that both corporate id and gcci number cannot be entered
function CheckForCorporateIdGcci()
{
    if (!CorporateIdCheck())
    {
        SwitchSearchPopupVisible('searchPromoCorpError');
    }
    else
    {
        SwitchSearchExtraVisible('searchExtraGcci')
    }
}

//function called when user submits corporate id / iata
//ensures that either corporate id or iata is selected
function CheckCorporateRadioChecked()
{
    var controlCorp = $('rbSearchCorp1');
    var controlIata = $('rbSearchCorp2');
    
    if (!controlCorp.checked && !controlIata.checked)
    {
        SwitchSearchPopupVisible('searchCorpIataError');
        return false;
    }
    else
    {
        return true;
    }
}

//function called when user submits corporate id
//ensures that corporate id is input in correct format
function CheckCorporateInput()
{
    if (CheckCorporateRadioChecked())
    {
        var control = $('txtSearchCorp');
        
        if (IsNumeric(control.value) && control.value.length == 8)
        {
            SwitchSearchPopupVisible('searchExtraCorporate');
        }
        else
        {
            SwitchSearchPopupVisible('searchCorpInputError');
        }
    }
}

function CheckGcciInput()
{
    var gcciControl = $('txtSearchGcci');
  
    if (gcciControl != null)
    {
        if (gcciControl.value.length == 16)
        {
            SwitchSearchPopupVisible('searchExtraGcci');
        }
        else
        {
            SwitchSearchPopupVisible('searchGcciError');
        }
    }
}

function ClearPromotion()
{
    $('txtSearchPromotion').value = '';
    SwitchSearchPopupVisible('searchExtraPromotion');
}

function ClearGcci()
{
    $('txtSearchGcci').value = '';
    SwitchSearchPopupVisible('searchExtraGcci');
}

function ClearCorporate()
{
    $('txtSearchCorp').value = '';
    SwitchSearchPopupVisible('searchExtraCorporate');
}

function CheckSearchBoxDataExceptions()
{
    var regionControlWide = $('orregion2'); 
    var regionControlThin = $('region'); 
    var categoryControl = $('searchCategory');
    
    var dropDowns = new Array();
    
    dropDowns[0] = regionControlWide;
    dropDowns[1] = regionControlThin;
    dropDowns[2] = categoryControl;
    
    var defaults = new Array();
    
    defaults[0] = '*';
    defaults[1] = '*';
    defaults[2] = '';
    
    for(i=0; i<dropDowns.length; i++)
    {
        if(dropDowns[i] != null)
        {            
            if ((dropDowns[i])[dropDowns[i].selectedIndex].value != defaults[i])
            {
                return true;
            }
        }
    }
    
    return false;
}

//function called on form submit to ensure that search criteria has been input
//param 'evt' -> object representing event to stop
function CheckSearchBoxData(evt) 
{
    var control = $(searchBoxId);
    
    if (control != null)
    {
        var value = control.value;
        
        if (value == defaultSearchBoxText)
        {
            if (CheckSearchBoxDataExceptions())
            {
                control.value = '';
            }
            else
            {
                DisplayError('Please enter hotel, attraction, location or event.');
                return Event.stop(evt);
            }
        }
        else if (value.length < 3)
        {
            DisplayError('Please enter 3 or more characters in the search box.');

		    return Event.stop(evt);
        }
        else
        {
            return true;
        }
    }
}

//function called to display friendly message when a validation error occurs
//param 'msg' -> A string representing the error message to display
function DisplayError(msg) {
	if (!this.errorBox) {							
		this.errorBox = document.createElement('div');
		this.errorBox.className = "error_box_location";

		this.errorBox.appendChild(document.createElement('h2'));
		this.errorBox.lastChild.className = "error_message";
		this.errorBox.lastChild.appendChild(document.createTextNode("There is a problem with your search"));

		this.errorBox.appendChild(document.createElement('p'));
		this.errorBox.lastChild.className = "error_message";
		this.errorBox.lastChild.appendChild(document.createTextNode(msg));
	} else {
		this.errorBox.lastChild.replaceChild(document.createTextNode(msg), this.errorBox.lastChild.firstChild);
	}

	var form = $(formId);
	var legend = $A(form.getElementsByTagName('legend')).first();
	
	legend.parentNode.insertBefore(this.errorBox, legend.nextSibling);
}

function checkKeyPress(control)
{
    if (window.event.keyCode == 13)
    {
        control.click();
    } 
}

function IsNumeric(sText)
{
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
    }
    return IsNumber;  
}

function showPackages()
{
    var packages = document.getElementsByClassName('packagesHidden');
    
    for (i = 0; i < packages.length; i++)
    {
        packages[i].className = 'packagesVisible';
    }
}

function hidePackages()
{
    var packages = document.getElementsByClassName('packagesVisible');
    
    for (i = 0; i < packages.length; i++)
    {
        packages[i].className = 'packagesHidden';
    }
}

//Add function to window.load event
Event.observe(window, 'load', InitWideSearch);

//Contact Lucy Smart Agent
function ContactSmartAgent(TelephoneNumber)
{
var question = document.getElementById('contact_question');
window.open('http://www.smartagent.co.uk/clients/bestwestern/smartAgent.html?question=' + question.value + '&variable=telephone_number,' + TelephoneNumber, 'SmartAgent', 'top=150,left=300,width=600,height=500,scrollbars=no,menubar=no,toolbar=no,resizable=no');
return true;
}

//SmartAgent Clear TextBox
function emptyTxt()
{
document.getElementById('contact_question').value = "";
document.getElementById('contact_question').style.color = "#000000";
}
function resetTxt()
{
    if(document.getElementById('contact_question').value == "")
    {
        document.getElementById('contact_question').value = "Type your question here";
        document.getElementById('contact_question').style.color = defaultSearchBoxColour;
    }
}

//SmartAgent Enter Catch
function EnterSmartAgent(e, TelephoneNumber){

if(e){
e = e 
} else {
e = window.event
} 

if(e.which){ 
var keycode = e.which
} else {
var keycode = e.keyCode 
}

if(keycode == 13) {
ContactSmartAgent(TelephoneNumber)
}
}

//Return UTC Date from values of month ddl and day ddl on search box
function GetDateFromDdls(monthValue, dayValue) { 
	var result = new Date();

    result.setMonth(monthValue.substr(4));  
	result.setUTCHours(0);
	result.setUTCMinutes(0);
	result.setUTCSeconds(0);
	result.setUTCDate(dayValue);
	result.setUTCFullYear(monthValue.substr(0,4));
	result.setUTCMonth(monthValue.substr(4) - 1);

	return result
}