function updateQuantities() {
    $$('#cart input[name=action]').first().setValue('update');
    return true;
}

function deleteProduct(id) {
    $(id).setValue(0);
    return updateQuantities();
}

function billing() {
    elem = $('billing')
    if (elem.getStyle('visibility') == 'hidden') {
        elem.setStyle({
            visibility : 'visible',
            display : 'block'
        });
    }
    else {
        elem.setStyle({
            visibility : 'hidden',
            display : 'none'
        });
    }
}

function changeRegion(prefix, stateText, regionText) {
    var selectedCountry = getSelectedValue($(prefix + '_country_input'));
    regionLabel = $(prefix + '_region_label');    

    if (selectedCountry == 'US') {
        regionLabel.update(stateText);
        $(prefix + '_state_input').setStyle({ display : '' });
        $(prefix + '_state_input').enable()
        $(prefix + '_ca_state_input').setStyle({ display : 'none' });
        $(prefix + '_ca_state_input').disable();
        $(prefix + '_region_input').setStyle({ display : 'none' });
        $(prefix + '_region_input').disable();
    }
    else if (selectedCountry == 'CA') {
        regionLabel.update(stateText);
        $(prefix + '_ca_state_input').setStyle({ display : '' });
        $(prefix + '_ca_state_input').enable();
        $(prefix + '_state_input').setStyle({ display : 'none' });
        $(prefix + '_state_input').disable()
        $(prefix + '_region_input').setStyle({ display : 'none' });
        $(prefix + '_region_input').disable();
    }
    else {
        regionLabel.update(regionText);
        $(prefix + '_state_input').setStyle({ display : 'none' });
        $(prefix + '_ca_state_input').setStyle({ display : 'none' });
        $(prefix + '_region_input').setStyle({ display : '' });
        $(prefix + '_region_input').enable();
        $(prefix + '_state_input').disable();
        $(prefix + '_ca_state_input').disable();
    }
}

function editAddress(stateText, regionText) {
    copyAddress('shipping', stateText, regionText);
    copyAddress('billing', stateText, regionText);
    return false;
}

function copyAddress(type, stateText, regionText) {
    old_email = $$('#oldaddress input[name=' + type + '_email]').first();
    old_recip = $$('#oldaddress input[name=' + type + '_recipient]').first();
    old_cpy = $$('#oldaddress input[name=' + type + '_company_name]').first();
    old_phone = $$('#oldaddress input[name=' + type + '_phone]').first();
    old_addr1 = $$('#oldaddress input[name=' + type + '_addr1]').first();
    old_addr2 = $$('#oldaddress input[name=' + type + '_addr2]').first();
    old_suburb = $$('#oldaddress input[name=' + type + '_suburb]').first();
    old_city = $$('#oldaddress input[name=' + type + '_city]').first();
    old_region = $$('#oldaddress input[name=' + type + '_region]').first();
    old_state = $$('#oldaddress input[name=' + type + '_state]').first();
    old_postcode = $$('#oldaddress input[name=' + type + '_postcode]').first();
    old_country = $$('#oldaddress input[name=' + type + '_country]').first();
    
    new_email = $$('#address input[name=' + type + '_email]').first();
    new_recip = $$('#address input[name=' + type + '_recipient]').first();
    new_cpy = $$('#address input[name=' + type + '_company_name]').first();
    new_phone = $$('#address input[name=' + type + '_phone]').first();
    new_addr1 = $$('#address input[name=' + type + '_addr1]').first();
    new_addr2 = $$('#address input[name=' + type + '_addr2]').first();
    new_suburb = $$('#address input[name=' + type + '_suburb]').first();
    new_city = $$('#address input[name=' + type + '_city]').first();
    new_region = $$('#address input[name=' + type + '_region]').first();
    new_postcode = $$('#address input[name=' + type + '_postcode]').first();

    copyInputValue(new_email, old_email);
    copyInputValue(new_recip, old_recip);
    copyInputValue(new_cpy, old_cpy);
    copyInputValue(new_phone, old_phone);
    copyInputValue(new_addr1, old_addr1);
    copyInputValue(new_addr2, old_addr2);
    copyInputValue(new_suburb, old_suburb);
    copyInputValue(new_city, old_city);
    copyInputValue(new_region, old_region);
    copyInputValue(new_postcode, old_postcode);
    
    if (old_state != null) {
        new_state = $$('#address select[name=' + type + '_state]');
        setSelectByValue(new_state.first(), old_state.readAttribute('value'));
        setSelectByValue(new_state.last(), old_state.readAttribute('value'));
    }
    
    if (old_country != null) {
        new_country = $$('#address select[name=' + type + '_country]').first();
        setSelectByValue(new_country, old_country.readAttribute('value'));
    }
    
    changeRegion(type, stateText, regionText);
}

function copyInputValue(destInput, srcInput) {
    if (destInput != null && srcInput != null) {
        destInput.writeAttribute('value', srcInput.readAttribute('value'));
    }
}

function copyValue(srcId, destId) {
    srcValue = getSelectedValue($(srcId));
    $(destId).writeAttribute('value', srcValue);
}

function getSelectedValue(select) {
    return select.selectedIndex >= 0 ? select.getValue() : undefined;
}

function setSelectByValue(destSelect, value) {
    if (destSelect != null) {
        for (i = 0; i < destSelect.options.length; i++) {
            if (destSelect.options[i].readAttribute('value') == value) {
                destSelect.options[i].selected = true;
                return;
            }
        }
    }
}

function limitTextArea(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
}

function validateBuy(button, multiSelectMin, multiSelectMax, groups, ajaxMode, baseUrl) {
    evt = new Object();
    evt.valid = true;
    evt.element = button;
    document.fire('yak:buyvalidate', { validationEvent : evt });
    
    if (!evt.valid) {
        return false;
    }
    
    frm = $(button).up('form');
    for (i = 1; i <= groups; i++) {
        count = 0;
        chkname = 'multiselect' + i + '[]';
        frm.select('input').each(function(a) {
            name = a.readAttribute('name');
            if (name == chkname && a.checked) {
                count++;
            }
        });
        if (count < multiSelectMin || count > multiSelectMax) {
            return false;
        }
    }
    
    if (ajaxMode) {
        ajaxBuy(button, baseUrl);
        return false;
    }
    
    return true;
}

function yakConfirmOrder(incTandC) {
    if (incTandC) {
        cb = $('tandcConfirmation');
        if (!cb.checked) {
            return false;
        }
    }
    return true;
}

function ajaxBuy(button, baseUrl) {
    btn = $(button);
    originalHTML = btn.innerHTML;
    btn.disabled = true;
    
    w = btn.getWidth();
    btn.setStyle({ width: w + 'px' })
    btn.innerHTML = 'Adding...';
    
    frm = btn.up('form');
    
    new Ajax.Request(baseUrl + '/index.php', {
        method: 'post',
        parameters: frm.serialize(),
        onSuccess: function(transport) {
            if ($$('li.yak_order_widget').size() > 0) {
                new Ajax.Request(baseUrl + '/wp-content/plugins/yak-for-wordpress/yak-view-order-widget.php', {
                    method: 'get',
                    onSuccess: function(transport) {
                        elem = $$('li.yak_order_widget');
                        elem.first().update(transport.responseText);
                    }
                });
            }
            btn.disabled = false;
            btn.innerHTML = originalHTML;
        }
    });
}
