function checkDates(f) {
    f.arrivalDate.value = f.arrivalDateYear.value + "-" + f.arrivalDateMonth.value + "-" + f.arrivalDateDay.value;
    f.departureDate.value = f.departureDateYear.value + "-" + f.departureDateMonth.value + "-" + f.departureDateDay.value;
    return process(document.SearchForm,'/promotions/search/promo_search_results.html','/promotions/search/search_error.html');
}

function selectStateCountry() {
    var fCity = document.SearchForm.city;
    var fCountry = document.SearchForm.country;
    var fState = document.SearchForm.stateProv;
    for (var i = 0; i < cityArray.length; i++) {
        if (cityArray[i] == fCity.options[fCity.selectedIndex].value) {
            for (var j = 0; j < fCountry.length; j++) {
                if(countryArray[i] == fCountry.options[j].value) {
                    fCountry.options[j].selected = true;
                    break;
                }
            }
            for (var k = 0; k < fState.length; k++) {
                if (stateArray[i] == fState.options[k].value) {
                    fState.options[k].selected = true;
                    break;
                }
            }
            break;
        }
    }
}

function selectCountry() {
    for (var j = 0; j < cityArray.length; j++) {
        if (stateArray[j] == document.SearchForm.stateProv.value) {
            document.SearchForm.country.value = countryArray[j];
            break;
        }
    }
}

function validatePromoForm(f) {
    return checkLocation(f);
}

function checkLocation(f) {
    var validFlag = false;
    var Index = getIndexInArray(f.city.value,cityArray);
    if(f.country.value == 'US' && (f.city.value == '' || f.city.value == null) && (f.stateProv.value == '' || f.stateProv.value == null)) {
        displayError(1);
    } else if(f.city.value == '' || f.city.value == null || f.country.value == '' || f.country.value ==null) {
        displayError(0);
    } else if(checkValidSequence(f,Index)) {
        displayError(0);
    } else if (okDates(f)) {
       // displayError(1);
         fillInDates(f);
        validFlag = true;
    }



    if(validFlag) {
        f.submit();
    }
}

function checkValidSequence(f,index) {
    if(f.city.value == cityArray[index] && f.stateProv.value == stateArray[index] && f.country.value == countryArray[index]) {
        return false;
    } else {
        return true;
    }
}

function getIndexInArray(value,array) {
    var i = 0;
        for ( var i in array ) {
            if(cityArray[i] == value) {
                return i;
            }
        }
}

function displayError(errorIndex) {
    document.getElementById('errorMessage').innerHTML = errorsArray[errorIndex];
    document.getElementById('errorMessage').style.display = 'block';
}

function hideError() {
    document.getElementById('errorMessage').style.display = 'none';
}

