
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
	  retValue = retValue.substring(1, retValue.length);
	  ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
	  retValue = retValue.substring(0, retValue.length-1);
	  ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
	  retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
	
isDOM = document.getElementById ? 1:0; 

function stripSpaces(x){
    while (x.substring(0,1) == ' ') x = x.substring(1);
    return x;
}

function empty(x){ if (x.length > 0) return false; else return true; }
    
function chkForChecked (f,fld,msg){
    var frm = f ;
    var e ;
    var j = 0 ;
    if(isDOM){
        e = document.getElementsByTagName('INPUT'); // returns a Node List
        for(var i=0; i<=e.length-1; ++i){
            if(e[i].name == fld) {
                if(j==0)j=i;
                if ( e[i].checked ) return true;  // one is checked after all!
            }
        }
    }else{
        e = f.elements[fld] ; // returns an array of elements
        for( var i=0; i < e.length; ++i ) { 
	    	if ( e[i].checked ) 
	    	{ 
	    	    return true;  // one is checked after all! 
	    	    break; 
	    	} 
	    }
    }
    alert( msg );
    e[j].focus() ;
    return false; 
}

function chkForEmail(f,fld,msg){
    var frm = f;
    var e = f.elements[fld]
	var val = e.value;
    var objRegExp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
    if (objRegExp.test(e.value)) return true;
    alert(msg);
    e.focus();
    return false;
}

function chkForIsNumeric (f,fld,msg) {
    var frm = f;
    var e = f.elements[fld]
    var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
    if (objRegExp.test(e.value)) return true; 
    alert(msg);
    e.focus();
    return false;
}

function chkForNonBlank (f,fld,msg){
    var frm = f ;
    var e = f.elements[fld]
    if (! empty(stripSpaces(e.value))){
	  e.value = trim(e.value);
	  return true; 
	}
      alert( msg );
	  e.value = '';
      e.focus() ;
      return false;
}

function chkForDate(f,fld,msg){
  var frm = f ;
  var e = f.elements[fld]
  var newDate = new Date();
  newDate.setTime(Date.parse(e.value));
  if (isNaN(parseFloat(newDate.getFullYear()))){
    alert( msg );
    e.focus() ;
    return (false);
  }
  return (true);
}

function chkForIsSelected (f,fld,msg){
    var frm = f ;
    var e = f.elements[fld]
    if ( ! e.selectedIndex == 0 ) return true; 
    alert( msg );
    e.focus() ;
    return false;
}

function chkNumeric (f,fld,msg){
  var frm = f ;
  var e = f.elements[fld]
  if(!isNaN(parseInt(e.value))) return true;
  alert( msg );
  e.focus() ;
  return false;
}

function validate(f){ 
  for( var i=0; i < fieldsToCheck.length; ++i ){ 
    if (! fieldsToCheck[i][1](f,fieldsToCheck[i][0],fieldsToCheck[i][2])){
        return false ;
    }
  }
return true ;
}

function List(pageName,frmName,fldName){
  var list = eval('document.' + frmName + '.' + fldName);
  var chosenItem = list.options[list.selectedIndex].value;
  var assignWindow = window.open("","","left=200 top=200 height=100 width=400");
  assignWindow.location = pageName + "&letter=" + chosenItem;
}

function sOpen(pageName,frmName){
  var list = eval('document.' + frmName + '.getProject');
  var chosenItem = list.options[list.selectedIndex].value;
  var chosenText = list.options[list.selectedIndex].text;
  document.location = pageName + "?ProjectID=" + chosenItem + "&tempnumber=" + chosenText;
}

function tOpen(pageName,frmName){
  var list = eval('document.' + frmName + '.bugNum');
  var chosenItem = list.options[list.selectedIndex].value;
  var chosenText = list.options[list.selectedIndex].text;
  document.location = pageName + "?bugID=" + chosenItem;
}
