
function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

var windowPopup = null;
function newWindow (URL,w,h,st,sc,rs,tb,mb) {
	if(windowPopup == null || windowPopup.closed) {
		var myleft = (screen.width - w)/2;
		var mytop = (screen.height - h)/2;
		top.document.windowPopup = window.open(URL, 'windowPopup', 'dependent=yes,toolbar='+tb+',menubar='+mb+',status='+st+',scrollbars='+sc+',resizable='+rs+',left='+myleft+',top='+mytop+',width=' + w + ',height=' + h);
		top.document.windowPopup.focus();
	} else {
		top.document.windowPopup.location = URL;
		top.document.windowPopup.focus();
		top.document.windowPopup.resizeTo(w,h);
	}
	windowPopup = top.document.windowPopup;
}

function isInteger(str) {
	newStr = parseInt(str);
	if( newStr != str )
		return false;
	return true;
}

function isFloat(str) {
	newStr = parseFloat(str);
	if( str != newStr )
		return false;
	return true;
}

function isDate(str) {
	var dateObj = new Date(str);
	var origDateAry = str.split("/");
	
	if ( origDateAry.length < 3 || isNaN(dateObj.valueOf()) )
		return false;
	
	var origMon = origDateAry[0];
	var origDay = origDateAry[1];
	var origYr = origDateAry[2];
	
	if ( origYr.length < 4 || (origMon-1) != dateObj.getMonth() || origDay != dateObj.getDate() || origYr != dateObj.getFullYear() )
		return false;

	return true;
}

function emailIsValid(str) {
	if( str.indexOf("@") < 1 || str.indexOf(".") < 1 )
		return false;
	return true;
}

function maxTextAreaLen(textArea, maxLen) {
	var sValue = textArea.value;
	var len = sValue.length;
	// any \n char needs to be counted twice because \n
	// converts to two chars: chr(13)chr(10) or \r\n
	cnt = countChars(sValue,'\n');
	len += cnt;
	if (len > maxLen) {
		textArea.value = sValue.substr(0,maxLen-cnt);
	}
}

function countChars( str, countChar ) {
    var count = 0;
    for( var ix = 0; ix < str.length; ix++ ) {
		if( str.charAt(ix) == countChar )
			count++;
	}
    return count;
}

// formats phone number into the mask NNN-NNN-NNNN xNNNN 
function formatPhone(fld) {
     chkstr = fld.value.replace(/\D/g, "");
	 // alert(chkstr);
     if (chkstr.length > 0 && chkstr.length < 10) {
	 	alert('Wrong number of digits in Phone Number, please re-enter.');
		return false;
	 }
     for (i=0; i < chkstr.length; i++) {
          if (i == 3) {
               fld.value = chkstr.substr(0,3) + "-";
          } else if (i == 6) {
               fld.value += chkstr.substr(3,3) + "-" + chkstr.substr(6, 4);
          } else if (i == 10) {
               fld.value += " x" + chkstr.substr(10);
          }
     }
     return true;
}

function mouseLeaves (element, evt) {
	if ( typeof evt.toElement != 'undefined' && evt.toElement && typeof element.contains != 'undefined' ) {
		return !element.contains(evt.toElement);
	} else if ( typeof evt.relatedTarget != 'undefined' && evt.relatedTarget ) {
		return !contains(element, evt.relatedTarget);
	}
}

function contains (container, containee) {
	while (containee) {
		if (container == containee) {
			return true; }
		containee = containee.parentNode;
	}
	return false;
}

function jsAsyncLoad(s_src, callback) {
	var x = document.getElementsByTagName('head')[0],
		s = document.createElement('script');
	s.type = 'text/javascript';
	s.async = true;
	s.src = s_src;
	if ( callback ) { s.onreadystatechange = rdyState; s.onload = doCallback; }
	x.appendChild(s);
	
	function rdyState() {
		var r = s.readyState;
		//console.log(r);
		if (r == "loaded" || r == "complete") doCallback();
	};
	function doCallback() {
		s.onreadystatechange = s.onload = null;
		if ( typeof callback != 'function' ) { try{ callback = eval(callback); } catch(err){ console.log(err.description); } }
		if ( typeof callback == 'function' ) callback();
	};
}
function cssAsyncLoad(c_src, callback) {
        var s = document.createElement('link');
        s.type = 'text/css';
        s.rel = 'stylesheet';
        s.href = c_src;
        var x = document.getElementsByTagName('head')[0];
        x.appendChild(s);
}
