var $j = jQuery.noConflict();

function updateQuantities() {
    $j('#cart').find('[name = action]').val('update'); 
    return true;
}

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

function billing() {
    elem = $j('#billing')
    if (elem.css('visibility') == 'hidden') {
        elem.css('visibility', 'visible');
        elem.css('display', 'block');
    }
    else {
        elem.css('visibility', 'hidden');
        elem.css('display', 'none');
    }
}

function changeRegion(prefix, stateText, regionText) {
    country = $j('#' + prefix + '_country_input');
    regionLabel = $j('#' + prefix + '_region_label');
    
    if (country.val() == 'US') {
        regionLabel.html(stateText);
        $j('#' + prefix + '_state_input').css('display', '');
        $j('#' + prefix + '_state_input').removeAttr('disabled');
        $j('#' + prefix + '_ca_state_input').css('display', 'none');
        $j('#' + prefix + '_ca_state_input').attr('disabled', 'disabled');
        $j('#' + prefix + '_region_input').css('display', 'none');
        $j('#' + prefix + '_region_input').val('');
    }
    else if (country.val() == 'CA') {
        regionLabel.html(stateText);
        $j('#' + prefix + '_ca_state_input').css('display', '');
        $j('#' + prefix + '_ca_state_input').removeAttr('disabled');
        $j('#' + prefix + '_state_input').css('display', 'none');
        $j('#' + prefix + '_state_input').attr('disabled', 'disabled');
        $j('#' + prefix + '_region_input').css('display', 'none');
        $j('#' + prefix + '_region_input').val('');
    }
    else {
        regionLabel.html(regionText);
        $j('#' + prefix + '_state_input').css('display', 'none');
        $j('#' + prefix + '_ca_state_input').css('display', 'none');
        $j('#' + prefix + '_region_input').css('display', '');
        $j('#' + prefix + '_state_input').attr('disabled', 'disabled');
        $j('#' + prefix + '_ca_state_input').attr('disabled', 'disabled');
    }
}

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

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

    copyInputValue(new_email, old_email);
    copyInputValue(new_recip, old_recip);
    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);
    if (old_state != null) {
        setSelectByValue(new_state, old_state.val());
    }
    copyInputValue(new_postcode, old_postcode);
    if (old_country != null) {
        setSelectByValue(new_country, old_country.val());
    }
    
    changeRegion(type, stateText, regionText);
}

function copyInputValue(destInput, srcInput) {
    if (destInput != null && srcInput != null) {
        destInput.val(srcInput.val());
    }
}

function copyValue(srcId, destId) {
    srcValue = $j(srcId).val();
    $j(destId).val(srcValue);
}

function setSelectByValue(destSelect, value) {
    if (destSelect != null) {
        $j('option', destSelect).each(function() {
            if ($j(this).val() == value) {
                $j(this).attr('selected', 'selected');
                return;
            }
        });
    }
}

function exportData(baseUrl) {
    var link = $j('export-data-link');
    
    var href = baseUrl;
    
    var status = $j('status');
    var year = $j('year_order_date');
    var month = $j('month_order_date');
    
    href += "&status=" + status.options[status.selectedIndex].value.replace(' ', '+');
    
    if (!$j.isEmptyObject(year)) {
        href += "&year=" + year.value;
        
        if (!$j.isEmptyObject(month)) {
            href += "&month=" + month.value;
        }
    }
    
    link.href = href;
    
    return true;
}

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

function validateBuy(button, multiSelectMin, multiSelectMax) {
    frm = $j(button).parents().find('form');
    ms = frm.find('[name = "multiselect[]"]');
    count = 0;
    for (i = 0; i < ms.length; i++) {
        if (ms[i].checked) {
            count++;
        }
    }
    if (count < multiSelectMin || count > multiSelectMax) {
        return false;
    }
    else {
        return true;
    }
}

function yakConfirmOrder(button, incTandC) {
    if (incTandC) {
        frm = $j(button).parents().find('form');
        checkbox = frm.find('[name = tandcConfirmation]');
        if (!checkbox.checked) {
            return false;
        }
    }
    return true;
}