function commonTrim(str){
	while( str.charAt(0) == " " ){
		str = str.substr(1,str.length)
	}
	return str;
}


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;
}

//
// returns the count of countChar char in the string str
//
function countChars( str, countChar )
  {
    var count = 0;
    for( var ix = 0; ix < str.length; ix++ ) {
		if( str.charAt(ix) == countChar )
			count++;
	}
    return count;
  }

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;
}


//
// forces the max len of maxLen to a textArea.
// just include onKeyUp="javascript:maxTextAreaLen(this, 256);" 
// where 256 is the max len u want for the text area in question
//
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);
	}
}

// this function auto sizes the height of a window to the height of the content */
function autoSizeWindowHeight(staticWidth) {
	var myHeight;
	var myWidth = staticWidth;
	var fudgeHeight=65;  // fudge factor to count title bar, statusbar etc.
	
	 if (window.document.height) {
		myHeight = window.document.height + fudgeHeight;
	 }
	 else if (document.all) {
		if (window.document.compatMode && window.document.compatMode != 'BackCompat') {
			myHeight  = window.document.documentElement.scrollHeight + fudgeHeight;
		}
		else {
			myHeight  = window.document.body.scrollHeight + fudgeHeight;
		}
	 }
	window.resizeTo(myWidth,myHeight);
}

// Strips everything except numbers and 'also' from string
function cleanNumber(str, also) {
     chkstr = str;
     good="1234567890" + also;
	 str = "";
	 
     for (i=0; i < chkstr.length+1; i++) {
          tst = chkstr.charAt(i);
          if (good.indexOf(tst) >= 0) {
          	str += chkstr.charAt(i);
          }
     }
	 
     return(str);
}

// formats phone number into the mask NNN-NNN-NNNN xNNNN 
function formatPhone(fld) {

     chkstr = cleanNumber(fld.value, "");
	 // alert(chkstr);
	 
     if (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 MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
