function parseObject(){
    var a = $F('iarrival') != 'dd.mm.yyyy' ? $F('iarrival') : '';
    var d = $F('ideparture') != 'dd.mm.yyyy' ? $F('ideparture') : '';
    new Ajax.Request('json.php', {parameters: {id: $('id_casa').value, a: a, d: d, p: $('persons').value, c: $('cnames').value}, onCreate: showAjaxLoader, onComplete: getParseResponse});
}

function showAjaxLoader(){
    $('loader').show();
}

function getParseResponse(oReq){
    var json = oReq.responseText.evalJSON();
    $('iarrival').value = json.arrival;
    $('ideparture').value = json.departure;
    $('specialofferstext').update (json.specialofferstext);
    if (json.asaturdays) {
        $('iarrival').hide ();
        $('sarrspan').update();
        var sel = new Element('select', { 'id': 'sarrival', 'name': 'sarrival'});
        json.asaturdays.each (
            function(item, s) {
                var opt = new Element('option').update(item);
                sel.appendChild(opt);
                if (item == json.arrival) {
                    sel.selectedIndex = s;
                    $('iarrival').value = item;
                }
            }
        );
        $('sarrspan').appendChild(sel);
        $('sarrival').observe('change',
            function() {
                $('iarrival').value = $F('sarrival');
                parseObject();
            }
        );
    } else {
        $('iarrival').readOnly = true;
    }
    if (json.dsaturdays) {
        $('ideparture').hide ();
        $('sdepspan').update();
        var sel = new Element('select', { 'id': 'sdeparture', 'name': 'sdeparture'});
        json.dsaturdays.each (
            function(item, s) {
                var opt = new Element('option').update(item);
                sel.appendChild(opt);
                if (item == json.departure) {
                    sel.selectedIndex = s;
                    $('ideparture').value = item;
                }
            }
        );
        $('sdepspan').appendChild(sel);
        $('sdeparture').observe('change',
            function() {
                $('ideparture').value = $F('sdeparture');
                parseObject();
            }
        );
    } else {
        $('ideparture').readOnly = true;
    }
    $('inights').value = json.days;
    $('inights').readOnly = $F('inights') > 0 ? true : false;
    $('price').value = json.price;
    $('price').readOnly = $F('price') > 0 ? true : false;
    $('loader').hide();
    if (json.highseasontext != '') {
        alert(json.highseasontext);
    }
}

['iarrival', 'ideparture'].each(
    function(item){
        $(item).observe('click', function() { if (this.value == 'dd.mm.yyyy') this.value = ''; } )
        $(item).observe('blur', function() { if (this.value == '') this.value = 'dd.mm.yyyy'; } )
        $(item).observe('change', function() { parseObject(); } )
    }
);
$('persons').observe('change', function() { parseObject(); });
$('cnames').observe('change', function() { parseObject(); });

parseObject();
Event.observe(window, 'unload', Event.unloadCache, false);
