function checkedValueById(id, mTrue, mFalse)
{
    return (document.getElementById(id).checked == true) ? mTrue : mFalse
}

function rand(iFirstValue, iLastValue)
{
    var iChoice = iLastValue - iFirstValue + 1;
    return Math.floor(Math.random() * iChoice + iFirstValue);
}

function checkOffSelect(sElementId)
{
    oElement = document.getElementById(sElementId);
    for(i=0; i<oElement.options.length; i++) {
        oElement.options.item(i).selected=false;
    }
}

function generateShortDescription(iLangId, iMaxLength, sIdTargetElement)
{
    var sDestinationElementId = 'short-description[' + iLangId + ']';
    var oShortDescArea = document.getElementById(sDestinationElementId);
    
    var sTargetElementId = sIdTargetElement + '[' + iLangId + ']';
    var oEditor = FCKeditorAPI.GetInstance(sTargetElementId);
    var sOrginalContent = oEditor.GetHTML(); // From FCKEditor
    
    var sStripTagsContent = sOrginalContent.replace(/<\/?[^>]+>/gi, '');
    if(sStripTagsContent.length > iMaxLength) {
        var sOutStringContent = sStripTagsContent.substr(0, iMaxLength-5);
        if(substr_count(sOutStringContent, '.') == 0) {
            oShortDescArea.value = sOutStringContent + '...';
        }
        else {
            oShortDescArea.value = sOutStringContent.substr(0, sOutStringContent.lastIndexOf('.')) + '.'; // obcinanie do kropki
        }
    }
    else {
        oShortDescArea.value = sStripTagsContent;
    }
}

function substr_count( haystack, needle, offset, length ) {
    // Count the number of substring occurrences
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_substr_count/
    // +       version: 801.3120
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: substr_count('Kevin van Zonneveld', 'e');
    // *     returns 1: 3
    // *     example 2: substr_count('Kevin van Zonneveld', 'K', 1);
    // *     returns 2: 0
    // *     example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10);
    // *     returns 3: false

    var pos = 0, cnt = 0;

    if(isNaN(offset)) offset = 0;
    if(isNaN(length)) length = 0;
    offset--;

    while( (offset = haystack.indexOf(needle, offset+1)) != -1 ){
        if(length > 0 && (offset+needle.length) > length){
            return false;
        } else{
            cnt++;
        }
    }

    return cnt;
}

/* licznik znakow do zajawki aktualnosci */
function char_counter(max, iLangId)
{   		
	var count = 0;
	var char_tmp = '';
	var string_tmp_max = '';

	for (var i = 0; i < $('short-description['+iLangId+']').value.length; i++)
	{
		char_tmp = $('short-description['+iLangId+']').value.substring(i, i+1);
		
		switch(char_tmp)
		{ 
			case '\n':
				count += 5;
				break;
				
			case '\r':
				count += 1;
				break;
				
			default:
				count++;
		}

		if (count>max) {
			$('short-description['+iLangId+']').value = string_tmp_max;
			break;
		}
		
		string_tmp_max+=char_tmp;		
	}
	
	if(max-count > 0)
		$('counter['+iLangId+']').innerHTML = ' '+(max-count);
	else
		$('counter['+iLangId+']').innerHTML = ' 0';
		
	setTimeout('char_counter('+max+', '+iLangId+')', 500);
}