var isDOM = (document.getElementById ? true : false);  
var isIE4 = ((document.all && !isDOM) ? true : false); 
var isNS4 = (document.layers ? true : false); 


function getRef(id)
{
	if (isDOM) return document.getElementById(id); 
	if (isIE4) return document.all[id]; 
	if (isNS4) return document.layers[id]; 
}

function getSty(id)
{ 
	return (isNS4 ? getRef(id) : getRef(id).style); 
}

function replace(string,text,by)
{
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


function openWinCentered(loc, winname, widthh, heightt, params)
{
	tp=Math.ceil((screen.height-heightt)/2);
	lf=Math.ceil((screen.width-widthh)/2);
	if (params.length >0) params = "," + params;

	newwin = window.open(loc,winname,"width="+widthh+",height="+heightt+",top="+tp+",left="+lf+params);
	newwin.opener = window;
	newwin.focus();
}

function PopupWindow(s_url, s_winname, i_width, i_height)
{
	i_tp = Math.ceil((screen.height - i_height) / 2) - 50;
	i_lf = Math.ceil((screen.width - i_width) / 2);
				
	newwin = window.open(s_url, s_winname, "width=" + i_width + ", height=" + i_height + ", top=" + i_tp + ", left=" + i_lf + ', menubar=no, scrollbars=yes, toolbar=no, location=no, directories=no, resizable=yes, status=yes');
	newwin.opener = window;
	newwin.focus();
	return newwin;
}


function js_CheckTextareaConstraints(el, maxlen)
{
	if(el.value.length > maxlen){
		alert('Sorry, you cannot enter more than ' + maxlen + ' characters into this field'
			+ '\r\nConsider revising, fragment below will be truncated'
			+ '\r\n\r\n' + el.value.substring(maxlen));
		el.value = el.value.substring(0, maxlen)
	}
}

function js_textbox_validatenumber(el)
{
	if (!el.value) return false;

	s_value = el.value;
	dotcount = 0;
	var Chars = "0123456789";

	if(Chars.indexOf(s_value.charAt(0)) == -1 && s_value.charAt(0)!='-')s_value = s_value.substring(1);

	for (var i = 1; i < s_value.length; i++) {
		if (Chars.indexOf(s_value.charAt(i)) == -1){
			if(s_value.charAt(i)=='.'){
				dotcount++;
				if(dotcount > 1)s_value = js_deletechar(s_value, i);
			}else s_value = js_deletechar(s_value, i);
		}
	}

	el.value = s_value;
} 


function js_deletechar(s_string, i_delindex)
{
	return s_string.substring(0, i_delindex) + s_string.substring(i_delindex + 1);
}


function addbookmark()
{
	window.external.AddFavorite(document.location, document.title);
}