function getElmById(searchStr,elm){
	_result = null;
	findId(searchStr,elm)
	return _result
}
function findId(searchStr,elm){
	if (_result)
		return;

	var x = elm;
	if (x.id == searchStr){
		_result = x
		return;
	}
		
	if (x.firstChild){
		findId(searchStr,x.firstChild)
		if (_result)
			return;
	}

	if (x.nextSibling)
		findId(searchStr,x.nextSibling)
}
function CSSRule(cName,styleObj){
	if(document.styleSheets.length == 0)
		return;
	for (var i=0;i<document.styleSheets.length;i++){
		for (var x=0;x<document.styleSheets[i].rules.length;x++)
			if (document.styleSheets[i].rules[x].selectorText == cName){
				Object.extend(document.styleSheets[i].rules[x].style,styleObj)
				return;
			}
	}
	var s='';
	delim = '';
	for (var i in styleObj){
		s += delim+i.underscore().dasherize()+':'+styleObj[i];
		delim = ';'
	}
	var myStyle = document.styleSheets[0];
	x = myStyle.addRule(cName,s)
}

function debug(obj){
	var msg=[];
	for (i in obj){
		try {msg[msg.length] = i+':'+obj[i];}
		catch(e) {msg[msg.length] = i+':'}
	}
	if (!window.debugBox){
		debugBox = document.createElement('div')
		debugBox.style.position = 'absolute';
		debugBox.style.top="10px";
		debugBox.style.left="10px";
		debugBox.style.border="1px solid black";
		debugBox.style.height="100px";
		debugBox.style.padding="10px";
		debugBox.style.backgroundColor="lightblue";
		document.body.appendChild(debugBox);
		debugBox.onclick=function(){debugBox.style.visibility="hidden"};
	}
	
	debugBox.innerHTML = msg.join('<br>')
	debugBox.style.visibility='visible';
}
function getElmsByName(searchString,scope){
	var x = scope.all
	var a=[];
	for (var i=0;i<x.length;i++)
		if (x[i].name == searchString)
			a[a.length]=x[i]
	return a
}
function createCookie(name,value,days,mydomain) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else 
		var expires = "";

	if (!mydomain)
		var mydomain = '';
	else
		mydomain = 'domain='+mydomain;
		
	document.cookie = name+"="+value+expires+"; path=/; "+mydomain;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isDate(a) {
    return typeof a == 'date';
}

function wait(millis) {
	date = new Date();
	var curDate = null;

	do { var curDate = new Date(); } 
		while(curDate-date < millis);
} 

function isFunction(a) {
    return typeof a == 'function';
}
function trim(str_var){
	// added trim function
	return str_var.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}
function mainmenu(){
	history.go('http://'+location.host+'/wwwroot/remaint/index.cfm');
	history.go('http://'+location.host+'/remaint/index.cfm');
	history.go('http://'+location.host+'/remaint/');
	setTimeout('window.location = "/wwwroot/remaint/index.cfm"',500);
}
function checklength(obj){
	setTimeout("updateCounter(textarea.value.length)",3);
	//var uselessKeys = /^9|16|17|35|36|37|8|46|91|38|66|39|40$/
	var uselessKeys = /^9|16|35|36|37|8|46|91|38|66|39|40$/
	if (uselessKeys.test(event.keyCode)) {
		return;
	}
	if (!event.keyCode == "17") {
		if (obj.value.length >= obj.maxlength) {
			return false;
		}
	}
}
function updateCounter(used){
	if (!textarea.maxlength)
		textarea.maxlength=textarea.maxLength;

	if (textarea.value.length > textarea.maxlength){
		textarea.value = textarea.value.substring(0,textarea.maxlength)
		used = textarea.maxlength
	}
	_from.innerHTML = 'used '+used+' ';
	_to.innerHTML = ' of '+textarea.maxlength;
}
function init_from_to(obj){
	textarea = obj;
	_from = document.getElementById(textarea.name+'_from');
	_to = document.getElementById(textarea.name+'_to');
	updateCounter(obj.value.length);
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function testEmail(str){
	var testString = str;
	var addressArray = testString.replace(/;/g, ",").split(",");

	if (addressArray.length > 1) {
		var emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
		for (i=0; i<=(addressArray.length-1); i++)
		{
			addressArray[i] = ltrim(addressArray[i]);
			if (emailRe.test(addressArray[i])) continue;
			else return emailRe.test(addressArray[i]);
		}
		return emailRe.test(addressArray[0]) + ';' + emailRe.test(addressArray[1]);
	} else {
		var emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
		return emailRe.test(str);
	}
}