var temp = '';


function getNumberOfWords(content)
{
	// Strip HTML
	content = content.replace(/(<([^>]+)>)/ig, '');

	// Trim
	content = content.replace(/^\s+|\s+$/g, "");

	// Replace special characters
	content = content.replace(/\r+/g, " ").replace(/\t+/g, " ").replace(/\n+/g, " ").replace(/&nbsp;/g, " ");

	// Replace multiple spaces and trim
	content = content.replace(/\s+/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, "");

	return (content.length > 0 ? content.split(" ").length : 0);
}

function setTemp(id)
{
	temp = document.getElementById(id).value;
}

// provjerava max_word_limit u elementu s id-om text_id, prikazuje ili skriva poruku (notice_id)
// disablea next button za korak dalje
// WRONG_MAX_WORDS - trenutni broj polja koji imaju prevelik broj rijeci
function SimpleWordCounter(text_id, notice_id, max_word_limit)
{
	//console.debug(SimpleWordCounter);
	//console.debug(text_id +','+ notice_id +','+ max_word_limit);
	// Get number of words
	var number_of_words = getNumberOfWords(document.getElementById(text_id).value);

	var msg = document.getElementById(notice_id);

	if (number_of_words > max_word_limit){
		if(msg && msg.style.display == 'none'){	
			msg.style.display='';
			WRONG_MAX_WORDS ++;
		}
	}
	else{
		if(msg && msg.style.display != 'none'){	
			msg.style.display='none';
			WRONG_MAX_WORDS --;
		}
	}

	// disableaj next button ako bar jedno polje ima prevelik broj rijeci
	next_button = document.getElementById('nextbutton');
	// fix - provjeri null (ako nije cijeli DOM loadan)
	if(next_button){
		if (WRONG_MAX_WORDS > 0)
			document.getElementById('nextbutton').disabled=true;
		else
			document.getElementById('nextbutton').disabled=false;
	}
	
	document.getElementById('counter_' + text_id).innerHTML = number_of_words + '/' + max_word_limit;
}

function num2money(n_value)
{
    // save the sign
    n_value = Math.abs(n_value);
  
    // round to 1/100 precision, add ending zeroes if needed
	var s_result = String(Math.round(n_value*1e2)%1e2	 + '00').substring(0,2);
	
	// FIX
	// ove 2 vrijednosti moraju biti jednake
	value1 = parseInt( (parseInt(n_value*100)) / 100 );
	value2 = parseInt( (Math.round(n_value*100)) / 100);
	if(value1 != value2)
		n_value = n_value + 1;
	
	//console.debug('round: ' + Math.round((n_value*1e2)%1e2));
	
	//var s_result = String((n_value*100)%100);
	//if(s_result.length == 1)
	//	s_result = '0' + s_result;
	
	//console.debug('s_result: ' + s_result);
	
    // separate all orders
    var b_first = true;
    var s_subresult;
    while (n_value >= 1)
    {
        s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value%1e3);		
        s_result = s_subresult.slice(-3) + (b_first ? ',' : '.') + s_result;		
        b_first = false;
        n_value = n_value/1e3;
    }
    // add at least one integer digit
    if (b_first)
        s_result = '0,' + s_result;
	
    // apply formatting and return
    return s_result;
}

function show_upload(show)
{
	if(show)
	{
		document.getElementById('upload_field').style.display = 'block';
	}
	else
	{
		document.getElementById('upload_field').style.display = 'none';
	}
}


function addEvent(elm, evType, fn, useCapture) {
	
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
