
function updateStatusTextCharCounter(value, e, limit, button) {
	len = value.length;
    jQuery('#status-field-char-counter').html('' + (limit-len));
	if (len > limit) {
		if (jQuery("#" + button).attr('disabled') != 'disabled') {
			jQuery("#" + button).attr('disabled', 'disabled')
			jQuery("#" + button).addClass('update-button-disabled')
		}
	} else {
		if (jQuery("#" + button).attr('disabled') == true) {
			jQuery("#" + button).removeAttr('disabled');
			jQuery("#" + button).removeClass('update-button-disabled')
		}

		if (len > (limit - 10)) {
			jQuery('#status-field-char-counter').css('color', '#d40d12' );
		} else if (len > (limit - 20)) {
			jQuery('#status-field-char-counter').css('color', '#5c0002' );
		} else {
			jQuery('#status-field-char-counter').css('color', '#cccccc' );
		}
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function SelectAll(id) {
	sb = document.getElementById(id);
	for(var i =0; i < sb.options.length; i++) {
		sb.options[i].selected = true;
	}
}

function setClickable(id){
	
		jQuery('#' + id).click(function(){
		var textarea = '<div><textarea id="note_note_text" name="whoaties[description]" class="editable" rows="6" cols="60">' + jQuery(this).html() + '</textarea>';
		var button = '<div><input type="button" value="SAVE" class="saveButton" /> <input type="button" value="CANCEL" class="cancelButton" /></div></div>';
        var revert = jQuery(this).html();
        jQuery(this).after(textarea + button).remove();
        jQuery('.saveButton').click(function(){
            saveChanges(this, false, id);
        });
        jQuery('.cancelButton').click(function(){
            saveChanges(this, revert, id);
        });
    }).mouseover(function(){
        jQuery(this).addClass("editable");
    }).mouseout(function(){
        jQuery(this).removeClass("editable");
    });
};

function saveChanges(obj, cancel, id){
	if (!cancel) {
        var t = jQuery(obj).parent().siblings(0).val();
        jQuery.post("/notes/ajaxedit/" + id.replace('editInPlace', ''), {
            content: t
        }, function(txt){
            alert(txt);
        });
    }
    else {
        var t = cancel;
    }
    if (t == '') 
        t = '(click to add text)';
	jQuery(obj).parent().parent().after('<div id="' + id + '">' + t + '</div>').remove();
    setClickable(id);
}


window.HTable = {};

function MoveTo(whatform,Source, Target, IsReason) {
    var arTempSrc = [];
    var x = 0;  
    var licznik=0;
    for (var i = 0; i < Source.length; i++) {
        if (Source.options[i].selected) { 
            var intTargetLen = Target.length++;
            Target.options[intTargetLen].text = Source.options[i].text;
            Target.options[intTargetLen].value = Source.options[i].value;				
            if (IsReason) {	
                var selReason = document.forms["+whatform+"].Reasons.selectedIndex;					
                HTable[Source.options[i].value] = document.forms["+whatform+"].Reasons[selReason].value;					
            } else {
                for (var key in HTable) {
                    if (key == Source.options[i].value && HTable[key] != null) {
                        HTable[Source.options[i].value] = null;
                    }
                }
            }
        } else {                
            var objTempVal = new Object();
            objTempVal.text = Source.options[i].text;
            objTempVal.value = Source.options[i].value;
            arTempSrc[x] = objTempVal;
            x++;
        }
    }
    if (IsReason) {
        document.forms[whatform].res.value	= "";
        a = "";
        for (var n in HTable) {
            if (HTable[n] != null) {
                a = a+n+","+HTable[n]+"\r\n";
            }
        }
        document.forms[whatform].res.value = a;
    }
    Source.length = arTempSrc.length;   
    for (var i = 0; i < arTempSrc.length; i++) {
        Source.options[i].text = arTempSrc[i].text;
        Source.options[i].value = arTempSrc[i].value;
        Source.options[i].selected = false;
    }
}

function filter() {
    var id = document.getElementById('category_id').selectedIndex;
    var val = document.getElementById('category_id').options[id].value;
    location.href = '/spokecards/?category=' + val;
}

function marvel(id, action) {
    var pos = id.indexOf("_");
    if(pos != -1) {
        var cardId = id.substring(0, pos);
        var numb = id.substring(pos).replace('_', '');
        if(action = "enable") {
            enableStars(id, cardId, numb);
        } else {
            disableStars(id, cardId, numb);
        }
    }
}

function enableStar(id) {
    $(id).src = "/images/icons/star.png";
    marvel(id, "enable");
}

function disableStar(id) {
    $(id).src = "/images/icons/starinactive.png";
    marvel(id, "disable");
}

function enableStars(id, cardId, numb) {
    for(i = 1; i < numb; i++)
        setSrc(cardId + "_" + i, id);
}

function disableStars(id, cardId, numb) {
    for(i = numb; i > 0; i--)
        setSrc(cardId + "_" + i, id);
}

function setSrc(sid, id) {
    $(sid).src = $(id).src;
}


