function submitForm (element_id) { 
    form = document.getElementById(element_id);
    form.submit();
}

function submitSearchForm () { 
    var categoryid = '';
    $('#searchFilterMenuContainer input[name="categoryid[]"]:checked').each(function () { 
               categoryid += $(this).attr('value') + ';';
            });

    $('#searchCategories').val(categoryid);
    submitForm('searchForm');
}

function productAdd (productid, quontity) { 
    /*
    var productid = $("#productid").val();
    var quontity = $("#quontity").val();
    */

    if (!quontity) quontity = 1;
    productUpdateCard(productid, quontity);
}

/**
 * Update shopping card information
 */
function productEdit (productid, element) { 
    var calculated_price = productUpdateCard(productid, element.value);
    var element_id = "#calculatedPrice_" + productid;
    if (calculated_price) 
        $(element_id).html(globalCalculatedPrice);
}

/**
 * Update shopping card ajax request method 
 */
function productUpdateCard(productid, quontity) { 
     $.ajax(
            {
            type:"GET", 
            async: false,
            url:"index.php", 
            data:"page=cash&action=product_add&productid=" + productid + "&quontity=" + quontity,
            dataType:"xml",
            success: function (xml, textStatus) {
                    if (textStatus != 'success') return;
                    productAddSuccess(xml);
               }
            }
            );
}

/**
 * Add product to shopping card
 */
function productAddSuccess(xml) { 
    $('return', xml).each(function() { 

            var code = $('code', this).text();
            var message = $('message', this).text();

            if (code == 'message' || code == 'error') 
                setError(code, message);

            if (code == 'error')
               return false;

            updateShoppingCard($('data',this));
 
            });
}

/**
 * Set error message returned by ajax request
 */
function setError(id, message) { 
    $('#' + id).html(message);
}

/**
 * Update UI shopping card data
 */
function updateShoppingCard (data) { 
    var price = $('card_price', data).text();
    var final_price = $('card_final_price', data).text();
    var amount = $('card_amount', data).text();
    var currency_label = $('currency_label', data).text();
    var shoppingCardPriceFinal = $('#shoppingCardPriceFinal');
    var transport_tax = $('transport_tax', data).text();
    var payment_tax = $('payment_tax', data).text();
    globalCalculatedPrice = $('product_calculated_price', data).text();

    if (amount > 0) { 
        $('#shoppingCardMenu').addClass('full');
    } else { 
        $('#shoppingCardMenu').removeClass('full');
    }

    $('#shoppingCardAmount').html(amount);
    $('#shoppingCardPrice').html(price);
    $('#cash_currency_label').html(currency_label);
    if (shoppingCardPriceFinal) 
        shoppingCardPriceFinal.html(final_price);

    if ($('#currentQuontity')) 
        $('#currentQuontity').html($('product_amount', data).text());
    if ($('#currentQuontityContainer'))
        $('#currentQuontityContainer').show();
    
    $('#payment_tax_element').html(payment_tax);
    $('#transport_tax_element').html(transport_tax);
}

/**
 * Remove item from shopping card 
 */
function deleteShoppingCardItem(productId) { 
    var successHandler = function (xml, textStatus) { 
    $('return', xml).each(function() { 
            var code = $('code', this).text();
            if (code == 'success') 
                window.location.reload();

            var message = $('message', this).text();
            setError(code, message);    
            });
    }

    shoppingCardActionHandler (productId, 'delete', successHandler);

}

/**
 * Add item amount to shopping card 
 */
function addShoppingCardItem (productId) { 
    var successHandler = function(xml, textStatus) { 
    $('return', xml).each(function() { 
            var code = $('code', this).text();
            if (code == 'success') { 
                updateShoppingCard(this);
                updateSingleProductInfo(this, productId);
                setError('error', '');
                return;
                }

            var message = $('message', this).text();
            setError(code, message);    
            });
    }

    shoppingCardActionHandler (productId, 'increace', successHandler);
}

/**
 * Shopping card action handler 
 */
function shoppingCardActionHandler (productId, card_event, successHandler) { 
     $.ajax(
            {
            type:"GET", 
            async: true,
            url:"index.php", 
            data:"page=cash&action=card_edit&productid=" + productId + "&event=" + card_event,
            dataType:"xml",
            success: successHandler
            }
            );
}

/**
 * Update single product shopping card information 
 */ 
function updateSingleProductInfo (data, productId) { 
    var productAmountId  = '#product_amount_' + productId;
    var productPriceId = '#calculatedPrice_' + productId;

    if ($(productAmountId).val()) 
      $(productAmountId).val($('product_amount', data).text());  
    
    if ($(productPriceId).html()) 
      $(productPriceId).html($('product_calculated_price', data).text());  
}

/**
 * Remove item amount from shopping card 
 */
function removeShoppingCardItem (productId) { 
    var successHandler = function(xml, textStatus) { 
    $('return', xml).each(function() { 
            var code = $('code', this).text();
            if (code == 'success') { 
                updateShoppingCard(this);
                updateSingleProductInfo(this, productId);
                setError('error', '');
                }

            var message = $('message', this).text();
            setError(code, message);    
            });
    }

    shoppingCardActionHandler (productId, 'decreace', successHandler);
}

/**
 * Control text field values via checkbox 
 *
 * @param object - Checkbox object
 * @param string - Text field id 
 */
function checkboxFieldEnable (obj, element_id) { 
    element_id = '#' + element_id;

    if ($(element_id).attr('type') == 'text' && !$(obj).is(':checked')) 
        $(element_id).attr('value', '');

    if (!$(obj).is(':checked')) { 
        $(element_id).attr('disabled', 'disabled');
    } else { 
        $(element_id).attr('disabled', '');
    }
}


