function pop(target, url, name, width, height, scroll) {
    if (target) {
        var _p = window.open(
            url,
            name
        );
    } else {
        var _p = window.open(
            url,
            name,
            'height = ' + height +
            ', width = ' + width +
            ', top = ' + (screen.height - height) / 2 +
            ', left = ' + (screen.width - width) / 2 +
            ', scrollbars = ' + (scroll ? 'yes' : 'no') +
            ', resizable = no'
        );
    }

    if (parseInt(navigator.appVersion) >= 4 && !target) {
        _p.window.focus();
    }
}

showImage = function(url) {
    pop(false, url, 'preview_window', 800, 600, true);
    return false;
}

verticalImages = function(_list, _height) {
    var _l = document.getElementById(_list);

    var _col = _l.getElementsByTagName('IMG');
    for (var i = 0; i <= _col.length; i++) {
        if ((typeof _col[i] != 'undefined') &&
            (_col[i].className == 'gift_image')) {
            _col[i].onload = function() {
                _positionImage(this, _height);
            }
        }
    }
}

function posText (_list, _height) {
    _positionTextVertically(_list, _height);
}

_positionTextVertically = function(_list, _height) {
    var _l = document.getElementById(_list);
    var _hs = _l.getElementsByTagName('A');
    for (var i = 0; i <= _hs.length; i++) {
        if ((typeof _hs[i] != 'undefined') &&
            (_hs[i].className == 'gift_title')) {
                _positionText(_hs[i], _height);
        }
    }
}

showConfirm = function(msg) {
    if(confirm(msg)) {
        return true;
    } else {
        return false;
    }
}

toggleDeliveryFields = function(_check) {
    if (_check.checked) {
        document.getElementById('other_address_fields').style.display = 'block';
        toggleDeliveryType();
        toggleDeliveryHour();
    } else {
        document.getElementById('other_address_fields').style.display = 'none';
        document.getElementById('at_other_city').selectedIndex = 0;
        toggleDeliveryType();
        toggleDeliveryHour();
    }
}

toggleDeliveryType = function() {
    var _city = document.getElementById('city').options[document.getElementById('city').selectedIndex].value;
    var _at_other_city = document.getElementById('at_other_city').options[document.getElementById('at_other_city').selectedIndex].value;

    if (((_at_other_city == 1)) || ((_city == 1) && (document.getElementById('at_other_city').selectedIndex == 0)  && (document.getElementById('delivery_at_other_address').checked != true)) || ((_city == 1) && (_at_other_city == 1))) {
        document.getElementById('select_delivery_type_field').style.display = 'block';
        document.getElementById('choose_flower').style.display = 'block';

    } else {
        document.getElementById('select_delivery_type_field').style.display = 'none';
        document.getElementById('choose_flower').style.display = 'none';
        document.getElementById('delivery_type').selectedIndex = 0;
        document.getElementById('delivery_hour').selectedIndex = 0;
        document.getElementById('flower_src').value = '';
        document.getElementById('selected_flower').value = '';
        document.getElementById('flowers').style.display = 'none';
    }
    if(_city>1||_at_other_city>1){
        document.getElementById('economic_delivery').style.display = 'block';
    }else{
        document.getElementById('economic_delivery').style.display = 'none';
    }
}

toggleDeliveryHour = function() {
    var _city = document.getElementById('city').options[document.getElementById('city').selectedIndex].value;
    var _at_other_city = document.getElementById('at_other_city').options[document.getElementById('at_other_city').selectedIndex].value;

    if ((_at_other_city == 1) || (_city == 1 && (document.getElementById('delivery_at_other_address').checked != true)) || (_at_other_city == 1 && _city == 1)) {
        document.getElementById('delivery_hour_field').style.display = 'block';
    } else {
        document.getElementById('delivery_hour_field').style.display = 'none';
        document.getElementById('delivery_hour').selectedIndex = 0;
    }
}

toggleFlower = function(_check) {
    _toggleItem(_check, 'selected_flower', 'flowers');
}

toggleCard = function(_check) {
    _toggleItem(_check, 'selected_card', 'cards');
}

_toggleItem = function(_check, _required, _hide) {
    if (!_check.checked || !document.getElementById(_required).value) {
        document.getElementById(_hide).style.display = 'none';
    } else {
        document.getElementById(_hide).style.display = 'block';
    }
}

_filterText = function(_txt) {
    _txt = _txt.replace(/(<BR>|<br>|<br\/>|<BR\/>|<BR \/>|<br \/>)/g, '');

    _txt = _txt.replace(/ +/, ' ');
    _txt = _txt.replace(/^\s+/g, '');
    _txt = _txt.replace(/\s+$/g, '');

    return _txt;
}

// Helpers.

//function _rxHeight() {
//    var _nav = document.getElementById('navigation');
////    var _gifts = document.getElementById('gifts');
//    var _gifts = document.getElementById('central');
//    var _faqs = document.getElementById('faqs');
//    var max = 1850 + parseInt(_nav.offsetHeight);
//    max = (max < parseInt(_faqs.offsetHeight)) ? parseInt(_faqs.offsetHeight) : max;
//    if (_gifts.offsetHeight < max) {
//        ((navigator.appVersion.indexOf('MSIE') != -1) && !window.opera) ? _gifts.style.height = max + 'px' : _gifts.style.minHeight = max + 'px';
//        //if (document.getElementById('gads')) {
//            //document.getElementById('gads').style.position = 'absolute';
//        //}
//    }
//}

function _rxHeight() {

    var _nav = document.getElementById('navigation');
    var _exp_left = document.getElementById('banner_under_navigation');
    var _gifts = document.getElementById('central');
    var _exp_right = document.getElementById('banners');
    var _faqs = document.getElementById('faqs');
    //plase footer to the bottom
    var _footer = document.getElementById('footer');
    var _head_image = document.getElementById('head_image');
    var _above_footer = document.getElementById('gifts')

    var max_h = Math.max(_nav.offsetHeight, _gifts.offsetHeight, _faqs.offsetHeight);

    height = max_h;
    ((navigator.appVersion.indexOf('MSIE') != -1) && !window.opera) ? _gifts.style.height = height + 'px' : _gifts.style.minHeight = height + 'px';

    height = max_h - _nav.offsetHeight + _exp_left.offsetHeight;
    ((navigator.appVersion.indexOf('MSIE') != -1) && !window.opera) ? (_exp_left.style.height = height + 22 + 'px') : _exp_left.style.minHeight = height -30 + 'px';

    height = max_h - _faqs.offsetHeight + _exp_right.offsetHeight;
    ((navigator.appVersion.indexOf('MSIE') != -1) && !window.opera) ? _exp_right.style.height = height + 'px' : _exp_right.style.minHeight = height -10 + 'px';

    if (_footer.offsetHeight + _above_footer.offsetHeight + _head_image.offsetHeight +31 +30 +40 < max_h){
        _footer.style.marginTop = _gifts.offsetHeight - _footer.offsetHeight - _above_footer.offsetHeight - _head_image.offsetHeight -31 -40  + 'px';
    }
}

_rxPosition = function() {
    for (var it = 0; it <= arguments.length; it++) {
        var _img = document.getElementById(arguments[it]);
        if (_img) {
            _positionImage(_img);
        }
    }
}

_positionImage = function(_img, _height) {
    if (parseInt(_img.height) < _height) {
        _img.style.top = '50%';
//        _img.style.marginTop = '-' + parseInt(_img.height/2) + 'px';
        _img.style.marginTop = '-' + parseInt(_img.height/2) + 'px';
    }
}

function _positionText (_item, _height) {
    if (_item.offsetHeight < _height) {
        _item.style.paddingTop = '' + (parseInt((_height - _item.offsetHeight)/2) - 5) + 'px';
    }
}

_pop = function() {
    document.getElementById('customer_name').value = 'Wlado';
    document.getElementById('customer_family').value = 'Shalamanov';
    document.getElementById('city').selectedIndex = 2;
    document.getElementById('address').value = 'Bla bla bla, blab bla 44';
    document.getElementById('telephone').value = '383894899';
    document.getElementById('email').value = 'stfu@stfu.com';
}

__fixIE7 = function() {
    var agt = navigator.userAgent.toLowerCase();
    if (agt.indexOf('msie 7.') != -1) {
        document.getElementById('payment').style.marginLeft = '0';
    }
}
