/**
 * Check if string is empty. If errorMsg is passed script will display it when
 * empty string. Can be used with FORM tag:
 * <FORM ... onsubmit="return CheckStringNotEmpty(document.formName.fieldName.value, 'error message')">
 * 
 * @param string str      string to check
 * @param string errorMsg error message that should be displayed
 * @return bool           True if string not empty, false otherwise
 */
function CheckStringNotEmpty(str, errorMsg)
{
    if (str == '')
    {
        if (errorMsg != '') alert(errorMsg);
        return false;
    }
    return true;
}

function ChangeQty(_form, _name, _delta)
{
  for (var i=0;i<_form.length;i++)
    if (_form[i].name == _name)
    {
        _form[i].value = Number(_form[i].value) + _delta;
        if (_form[i].value < 1) _form[i].value = '';
    }
      
}

function ResetValue(_form, _name)
{
  for (var i=0;i<_form.length;i++)
    if (_form[i].name == _name)
        _form[i].value = '';
}

function URLconfirmation($question, $url) {
    var answer = confirm($question);
    if (answer){
            window.location = $url;
            return true;
    }
    else return false;
}

function SwitchFormCheckboxes(form, state)
{
    for (var i=0; i<form.length; i++)
    {
        if (form[i].type=='checkbox')
        {
            form[i].checked=state;
        }
  }
}

/**
 * Switches to new url. Used for example in select boxes where strings cannot be
 * concatenated
 */
function ChangeURL(url) {
    window.location.href = url;
}

function CardPartView(action, id) {
    $.get(RELATIVE_URL+"/card/ajax"+action, {id: id}, function(data) {
             $("#card_view_"+action).html(data);
        });
}

function AddToWishlist(id, category){
    $.get(RELATIVE_URL+"/wishlist/ajaxAdd/", {id: id, category: category}, function(data) {
             $("#wishlist").html(data);
        });
}

function RemoveFromWishlist(id, category){
    $.get(RELATIVE_URL+"/wishlist/ajaxRemove/", {id: id, category: category}, function(data) {
             $("#wishlist").html(data);
        });
}

function SendTradeQtyRequest(card, type, delta, section) {
    $.get(RELATIVE_URL+"/trade/ajaxQty/", {cardId: card, type: type, delta: delta, section: section}, function(data) {
             $('#'+section).html(data);
        });
}

function LoadRequestedURL(section, url) {
    jQuery.ajaxSetup({async:false});
    $(section).addClass(section, 'loading');
    if (url.length > 0) {
        $.get(url, {}, function(data) {
             $(section).html(data);
        });
    } else $(section).html('');
    $(section).removeClass(section, 'loading');
    jQuery.ajaxSetup({async:true});
}

function CollectionGameChosen() {
    gameid = $('#select-game').val();
    if (gameid > 0) {
        $.get(RELATIVE_URL+"/collection/ajaxGame", {game: gameid}, function(data) {
                 $('#collection-sets').html(data);
                 $('#collection-options').html('');
            });
    }
}

function CollectionSetChosen() {
    setid = $('#select-set').val();
    if (setid > 0) {
        $.get(RELATIVE_URL+"/collection/ajaxSet", {set: setid}, function(data) {
                 $('#collection-options').html(data);
            });
    }
}
