﻿jQuery.noConflict();

jQuery(document).ready(function () {

    jQuery(function () {
        jQuery("#AdvanceFromDate").datepicker();
    });
    
    jQuery("input[name='QuickPropertyStatus']").change(function () {

        /* Selected Currency Code */
        var currencyCode = jQuery('#QuickPropertyCurrencyCode').val();

        /* letting or for sale */
        var value = jQuery("input[name='QuickPropertyStatus']:checked").val();

        doCurrencyChange(currencyCode, value, false);
    });

    jQuery("#QuickPropertyCurrencyCode").change(function () {

        /* Selected Currency Code */
        var currencyCode = jQuery('#QuickPropertyCurrencyCode').val();

        /* letting or for sale */
        var value = jQuery("input[name='QuickPropertyStatus']:checked").val();

        doCurrencyChange(currencyCode, value, false);
    });

    jQuery("#AdvanceCurrencyCode").change(function () {

        /* Selected Currency Code */
        var currencyCode = jQuery('#AdvanceCurrencyCode').val();

        doCurrencyChange(currencyCode, 1, true);
    });
});


function doCurrencyChange(_currencyCode, _value, _isAdvanced) {
    
    if (_value == 1) {
        doCurrencyPricesCall('GetPriceListForSale', _currencyCode, _isAdvanced);
    }
    else if (_value == 2) {
        doCurrencyPricesCall('GetPriceListForLetting', _currencyCode, _isAdvanced);
    }
}

function doCurrencyPricesCall(_method, _selectedCurrency, isAdvanced) {

    var _fromDropDown;
    var _toDropDown;
    var _div;

    if (!isAdvanced) {
        _fromDropDown = jQuery("#QuickPriceStart");
        _toDropDown = jQuery("#QuickPriceEnd");
        _div = jQuery('div.quicksearch');

    }else{
        _fromDropDown = jQuery("#AdvancePriceFrom");
        _toDropDown = jQuery("#AdvancePriceTo");
        _div = jQuery('div.advancedsearch');
    }

    /* Block The selection area */
    _div.block({
        message: '<img src="/Content/WebsiteImages/ajax-loader.gif" />'
    });

    jQuery.ajax({
        url: '/Currency/' + _method + '?selectedCurrency=' + _selectedCurrency,
        success: function (data) {
            _fromDropDown.empty();
            _toDropDown.empty();

            _fromDropDown.append('<option value="0" selected="selected">From</option>');
            _toDropDown.append('<option value="0" selected="selected">To (' + _selectedCurrency + ')</option>');

            jQuery(data).each(function (index, value) {
                _fromDropDown.append('<option value="' + value.OriginalValue + '">' + jQuery().number_format(value.Price, { numberOfDecimals: 2, decimalSeparator: '.', thousandSeparator: ',', symbol: '' }) + '</option>');
                _toDropDown.append('<option value="' + value.OriginalValue + '">' + jQuery().number_format(value.Price, { numberOfDecimals: 2, decimalSeparator: '.', thousandSeparator: ',', symbol: '' }) + '</option>');
            });

        complete: 
            {
                _div.unblock();
            }
        }
    });   
}


function onTextBoxEnter(e, _text) {

    if (e.value == _text) {
        e.value = "";
    }
}

function onTextBoxExit(e, _text) {

    if (e.value == "") {
        e.value = _text;
    }
}


function validateContactForm() {

    var firstname = jQuery("#FirstName").val();
    var lastname = jQuery("#LastName").val();
    var email     = jQuery("#Email").val();
    var phone     = jQuery("#Phone").val();
    var message   = jQuery("#Message").val();

    firstname = jQuery.trim(firstname);
    lastname  = jQuery.trim(lastname);
    email     = jQuery.trim(email);
    phone     = jQuery.trim(phone);
    message   = jQuery.trim(message);

    if (firstname === null || firstname.length === 0) {

        jQuery("#FirstName").addClass("input-validation-error");

        return false;
    }

    if (lastname === null || lastname.length === 0) {

        jQuery("#LastName").addClass("input-validation-error");

        return false;
    }

    if (email === null || email.length === 0) {

        jQuery("#Email").addClass("input-validation-error");

        return false;
    }

    if (phone === null || phone.length === 0) {

        jQuery("#Phone").addClass("input-validation-error");

        return false;
    }

    if (message === null || message.length === 0) {

        jQuery("#Message").addClass("input-validation-error");

        return false;
    }

    document.forms['ContactForm'].submit();
}
