
// getElementById wrapper for x-browser comp.
if(document.all && !document.getElementById) {
	document.getElementById = function(id) {
		return document.all[id];
	}
}

// sorting algorithm for sortSelect(selectToSort)
function sortFuncAsc(record1, record2) {
	var value1 = record1.optText.toLowerCase();
	var value2 = record2.optText.toLowerCase();
	if (value1 > value2) return(1);
	if (value1 < value2) return(-1);
	return(0);
}

// sorts the options of a select object
function sortSelect(selectToSort) {
	var myOptions = [];
	for (var loop=0; loop<selectToSort.options.length; loop++) {
		myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
	}

	myOptions.sort(sortFuncAsc);

	selectToSort.options.length = 0;
	for (var loop=0; loop<myOptions.length; loop++) {
		var optObj = document.createElement('option');
		optObj.text = myOptions[loop].optText;
		optObj.value = myOptions[loop].optValue;
		selectToSort.options.add(optObj);
	}
}

// passes selected options from fromList select object to toList select object
function passVals(fromList,toList) {
	if (fromList.options.selectedIndex != -1) {
		var sel = false;
		for (i=0;i<fromList.options.length;i++) {
			var current = fromList.options[i];
			if (current.selected) {
				sel = true;
				txt = current.text;
				val = current.value;
				toList.options[toList.length] = new Option(txt,val);
				fromList.options[i] = null;
				i--;
			}
		}
	}
	sortSelect(fromList);
	sortSelect(toList);
}

// looks for stxt string in the selOptions named array
function selOption(selobj, stxt) {
	selobj.selectedIndex = -1;
	var found = false;
	var foundIndex = -1;
	for (i=0;i<selobj.options.length;i++) {
		if ((!found) && (selobj.options[i].text.substr(0,stxt.length).toLowerCase()==stxt.toLowerCase())) {
			found = true;
			foundIndex = i;
		}
	}
	selobj.selectedIndex = foundIndex;
	if (stxt = '') selobj.selectedIndex = -1;
}

function isInArray(the_needle, the_haystack){
        var the_hay = the_haystack.toString();
        if(the_hay == ''){
            return false;
        }
        var the_pattern = new RegExp(the_needle, 'g');
        var matched = the_pattern.test(the_haystack);
        return matched;
}
    
function compileSimpleSelection(selobj, finalobj, separator) {
	var finalval = '';
	var i;
	for (i=0;i<selobj.options.length;i++) {
		finalval = finalval+selobj.options[i].value+separator;
	}
	tempsplit = new Array();
	finalsplit = new Array();
	tempsplit = finalval.split(';');
	for (i=0;i<tempsplit.length;i++) {
//		if ((tempsplit[i]!='') && (!isInArray(tempsplit[i],finalsplit))) {
			finalsplit.push(tempsplit[i]);
//		}
	}
	finalsplit.sort();
	finalval = finalsplit.join(';');
	finalobj.value = finalval;
}

function compileSelection(nonregobj, selobj, finalobj, separator) {
	var finalval = '';
	var i;
	for (i=0;i<nonregobj.options.length;i++) {
		finalval = finalval+nonregobj.options[i].value+separator;
	}
	for (i=0;i<selobj.options.length;i++) {
		finalval = finalval+selobj.options[i].value+separator;
	}
	tempsplit = new Array();
	finalsplit = new Array();
	tempsplit = finalval.split(';');
	for (i=0;i<tempsplit.length;i++) {
		if ((tempsplit[i]!='') && (!isInArray(tempsplit[i],finalsplit))) {
			finalsplit.push(tempsplit[i]);
		}
	}
	finalsplit.sort();
	finalval = finalsplit.join(';');
	finalobj.value = finalval;
}

function filterJump(url,fval) {
	if (fval==0) {
		window.open(url,'mywindow','width=600,height=400,left=100,top=100,screenX=100,screenY=100,toolbar=no, location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no, resizable=yes');
		return true;
	} else {
		return false;
	}
}

function addOption(selobj, value, caption) {
	selobj[selobj.length] = new Option(caption, value);
}

function delOption(selobj) {
	if (selobj.selectedIndex >= 0) {
		selobj.options[selobj.selectedIndex]=null;
		selobj.selectedIndex=0;
	}
}

function numberFormat(nStr,prefix){
    var prefix = prefix || '';
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1))
        x1 = x1.replace(rgx, '$1' + ' ' + '$2');
    return prefix + x1 + x2;
}

function IsNumeric(sText) {
	var ValidChars = "0123456789.-";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

function IsPositiveNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

function strReplace(s, r, w){
	return s.split(r).join(w);
}

function w(url,name,width,height) {
	x = (640 - width)/2, y = (480 - height)/2;
    if (screen) {
        y = (screen.availHeight - height)/2;
        x = (screen.availWidth - width)/2;
    }
	window.open(url,name,'width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no, resizable=yes');
}

function autoResize(wrapperName) {
	var content = document.getElementById(wrapperName);
	var newWidth = content.offsetWidth+10;
	var newHeight = content.offsetHeight + 60;
    if (screen) {
        nx = (screen.availWidth - newWidth)/2;
        ny = (screen.availHeight - newHeight)/2;
    } else {
		nx = (640 - newWidth)/2; 
		ny = (480 - newHeight)/2;
	}
	window.resizeTo( newWidth, newHeight);
	window.moveTo(nx, ny);
	return true;
}

function iframeResize(bodyid, iframeid) {
	var content = document.getElementById(bodyid);
	var newHeight = content.offsetHeight + 60;
	parent.document.getElementById(iframeid).style.height = newHeight+'px';
	return true;
}

function confirmDelete(msg) {
	return confirm(msg);
}

// sets cookie
function setCookie(NameOfCookie, value, expiredays, path, domain, secure) {
	var ExpireDate = new Date();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays*24*3600*1000));
	document.cookie = NameOfCookie +"="+ escape(value) + ((expiredays == null)? "": ";expires="+ ExpireDate.toGMTString()) +((path == null)? "": (";path=" + path)) +((domain == null) ? "" : (";domain=" + domain)) +((secure == true) ?";secure":"");
}

// [k] gets cookie
function getCookie(NameOfCookie) {
	if(document.cookie.length > 0) {
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1) {
			begin += NameOfCookie.length + 1;
			end = document.cookie.indexOf(";",begin);
			if(end == -1) end = document.cookie.length;
			//alert('Cookie: '+NameOfCookie+'='+unescape(document.cookie.substring(begin,end)));
			return unescape(document.cookie.substring(begin,end));
		}
	}
	return null;
}
// opens menu or if its open, jumps to the url, saves open menu
function oc(link, section) {
	so = document.getElementById(section);
	if (so.style.visibility=='visible') {
		setCookie('nea_oc', section, 100, '/');
		document.location.href=link;
	} else {
		setCookie('nea_oc', section, 100, '/');
		so.style.visibility='visible';
		so.style.display='block';
		so.style.height='';
	}
}

// opens menu saved in cookie
function ocinit() {
	initedoc = getCookie('nea_oc');
	if (initedoc != null) {
		oc('', initedoc);
	}
}
