// JavaScript Document

function ClearValue(elementId) {
	var strText = document.getElementById(elementId).value.toString();
	
	if (strText.indexOf("unesite ") != "-1" || strText.indexOf("Korisnicko ime") != "-1")
	{
		document.getElementById(elementId).value = '';	
	}
}

function Clear(elementId) {
	document.getElementById(elementId).value = '';	
}

function ResetValue(elementId, text) {
	if (document.getElementById(elementId).value.toString() == '')
	{
		document.getElementById(elementId).value = text;
	}
}

function SetToPassword(elementId) {
	if (document.getElementById(elementId).value.toString() != 'passwort')
	{
		document.getElementById(elementId).type = 'password';
	}
}

function ChangeClass(elementId, className) {
	document.getElementById(elementId).className = className;
}

function isnull(value) {
	if ((typeof(value) == 'undefined') || (value === null))
		return true;
	else 
		return false;
}

function popup(url, name, width, height)
{
	if ((typeof(name) == 'undefined') || (name == 'NaN') || (name == 'null') || (name === null))
		name = 'popup';

	if ((typeof(width) == 'undefined') || (width == 'NaN') || (width == 'null') || (width === null))
		width = 715;
	
	if ((typeof(height) == 'undefined') || (height == 'NaN') || (height == 'null') || (height === null))
		height = 650;
	
	var left = (screen.width  - width)/2;
	var top = (screen.height -80 - height)/2;

	params = 'width=' + width + ', height=' + height + ', top=' + top + ', left=' + left;
	params += ', directories=no';
	params += ', location=no';
	params += ', menubar=no';
	params += ', resizable=no';
	params += ', scrollbars=1';
	params += ', status=no';
	params += ', toolbar=no';
	params += ', modal=yes';
		
		
	wndPopUp = window.open(url, name, params);
	if(wndPopUp) { wndPopUp.focus(); }
}

function new_window(url, name, width, height) {
	if ((typeof(name) == 'undefined') || (name == 'NaN') || (name == 'null') || (name === null))
		name = 'popup';

	if ((typeof(width) == 'undefined') || (width == 'NaN') || (width == 'null') || (width === null))
		width = 715;
	
	if ((typeof(height) == 'undefined') || (height == 'NaN') || (height == 'null') || (height === null))
		height = 650;
	
	var left = (screen.width  - width)/2;
	var top = (screen.height -80 - height)/2;

	var params = 'width=' + width + ', height=' + height + ', top=' + top + ', left=' + left;
		params += ', directories=no';
		params += ', location=no';
		params += ', menubar=no';
		params += ', resizable=no';
		params += ', scrollbars=1';
		params += ', status=no';
		params += ', toolbar=no';
		params += ', modal=yes';

	wndPopUp = window.open(url, name, params);
	if(wndPopUp) { wndPopUp.focus(); }
	return false;
}

function clearSelect(elementId) {
	document.getElementById(elementId).length = 0;
}

function clearSelectExt(elementId, value, option) {
	clearSelect(elementId);
	document.getElementById(elementId).options[0] = new Option(option, value);
	
}	

function utf8_encode (string) {
	string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");
	var utftext = "";
	var start, end;
	var stringl = 0;
	start = end = 0;
	stringl = string.length;
	for (var n = 0; n < stringl; n++) {
		var c1 = string.charCodeAt(n);
		var enc = null;
		if (c1 < 128) {
			end++;
		} else if((c1 > 127) && (c1 < 2048)) {
			enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
		} else {
			enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
		}
		if (enc != null) {
			if (end > start) {
				utftext += string.substring(start, end);
			}
			utftext += enc;
			start = end = n+1;
		}
	}
	if (end > start) {
		utftext += string.substring(start, string.length);
	}
 
	return utftext;
}

function utf8_decode (str_data) {
	var tmp_arr = [], i = ac = c1 = c2 = c3 = 0;
	str_data += '';
	while ( i < str_data.length ) {
		c1 = str_data.charCodeAt(i);
		if (c1 < 128) {
			tmp_arr[ac++] = String.fromCharCode(c1);
			i++;
		} else if ((c1 > 191) && (c1 < 224)) {
			c2 = str_data.charCodeAt(i+1);
			tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
			i += 2;
		} else {
			c2 = str_data.charCodeAt(i+1);
			c3 = str_data.charCodeAt(i+2);
			tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}
	}
	return tmp_arr.join('');
}

function windowHeight() {

	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;

	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;

	// IE 5
	if (document.body)
		return document.body.clientHeight;

	return 0;
} 	
	
	
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}		

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;

  number = Math.pow(10, numOfDecimal);
  if (isnull(argvalue))	
	argvalue = '0';
	
  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
	argvalue = "0" + argvalue;

  if (addzero == true) {
	if (argvalue.indexOf(".") == -1)
	  argvalue = argvalue + ".";

	while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
	  argvalue = argvalue + "0";
  }

  return argvalue;
}

function in_scope(val, valid_chars) {
   var ret=true;
   var char;
	
   for (i = 0; i < val.length && ret == true; i++) { 
      char = val.charAt(i); 
      if (valid_chars.indexOf(char) == -1) {
         ret = false;
      }
   }
   return ret;
}

function is_number(val) {
	var valid_chars = "0123456789.";
	return in_scope(val, valid_chars);
}

function is_int(val) {
	var valid_chars = "0123456789";
	return in_scope(val, valid_chars);
}

