// JScript source code
// built a trim() function
String.prototype.trim = function() {
    // skip leading and trailing whitespace
    // and return everything in between
    var x=this;
    x=x.replace(/^\s*(.*)/, "$1");
    x=x.replace(/(.*?)\s*$/, "$1");
    return x;
}

//counts the number of characters in textfield or textarea
function countText(formName,objName,maxLength) {
	var obj = eval("document." + formName + "." + objName);
    if (obj){
        if(parseInt(maxLength) < parseInt(obj.value.length)){
            alert("You have exceeded the maximum number of characters.\nOnly " + maxLength + " characters are allowed.");
            obj.focus();
            return false;
        }else{
            return true;
        }
    }
}	

function maxChar(formName,objName,maxLength) {
	var obj = eval("document." + formName + "." + objName);
    if (obj){
        if(parseInt(maxLength) < parseInt(obj.value.length)){
            alert("You have exceeded the maximum number of characters.\nOnly " + maxLength + " characters are allowed.");
            obj.focus();
            return false;
        }else{
            return true;
        }
    }
}	

//validates keywords
function isValidKeyword(keyword_str){
	ctr = 0;
	isValid = true;
	for (ctr = 0; ctr < keyword_str.length; ctr++){
		tempchar = keyword_str.toUpperCase().charAt(ctr);
		if(!isValidKeywordChar(tempchar)) {
			isValid = false;
            break;
        }             
	}
	return isValid;
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isValidKeywordChar(c)
{   return (isLetter(c) || isDigit(c) || c == "." || c == "-")
}

function fieldEmpty(str){
	if (str == "") {
		alert("This is a required field.");
		return true;
	}
}

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

// to validate time - requires time input as string ex: 12:30 AM
function ReadUStime(Q) { var T // adaptable to other layouts
  if ((T = /^(\d\d):(\d\d)\s?(([ap])\.?m\.?)?$/i.exec(Q)) == null)
    { return -2 } // bad format
  if (T[3]!='') { // AM/PM
    if (T[1]>'12') { return -1 } // bad value   || T[1]=='00' ?
    T[1] = T[1]%12 + 12* /p/i.test(T[3]) } // to 24-h
  if (!ValidTime(T[1], T[2], 0)) { return -1 } // bad value
  return [ +T[1], +T[2] ] /* for strings, [ LZ(T[1]), T[2] ] */ }

function ShowUSTimeVal(S) { 
    //with (document.forms['Frm4']) {
        if ((S<-1) || (S==-1)){
            return false;
        }else{
            return true;
        }
        //USR.value = S<-1 ? 'Not dd:dd x.m.' : S==-1 ? 'Bad value' : S
        //Out.value = S<0 ? '??' : new Date(2000,0,1,S[0],S[1])
        //txtTime.focus() 
    //} 
}
  
function ValidTime(h, m, s){ 
    with (new Date(0,0,0,h,m,s))
    return ((getHours()==h) && (getMinutes()==m)) 
}  

function isValidTime(s){
    if (!ShowUSTimeVal(ReadUStime(s))){
        return false;
    }else{
        return true;
    }
}// ends isValidTime function
