function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 
function HasSpecialChar(strText){
	var arrSpecial = new Array();
	var flgExist = false;
	arrSpecial[0] = '>';
	arrSpecial[1] = '<';
	arrSpecial[2] = '#';
	arrSpecial[3] = '"';
	arrSpecial[4] = '\'';	
	for (var i = 0; i <= arrSpecial.length; i++) {
    	if (strText.indexOf(arrSpecial[i])!=-1){
			flgExist = true;
		}
	}
	return flgExist;
}
function isValidTractorUtn(objUTN,strTcUTNPattern){
	//objUTN is the text box of tractor UTN and strTcUTNPattern is the Trucking company UTN pattern if not exist a 0 must be passed
	if (strTcUTNPattern == 0){
		if (objUTN.value.length != 4)
		{
	    	alert('Invalid UTN.UTN must be 4 digit number');
		    objUTN.focus();
		    return false;
		}
	
		if (isNaN(objUTN.value)) {
          alert('Invalid UTN.');
		  objUTN.focus();
		  return false; 
	    }
		return true;
	}else{
		//To store the length of utn if equal 2 the show message of confirmation		
		flgEnteredUtnLength = objUTN.value.length;
		if (objUTN.value.length == 2)
		{
			objUTN.value = strTcUTNPattern + addTractor.utn.value;
		}
		if ((!isAlphanumeric(objUTN.value)) || (objUTN.value.length != 4)){
		  alert('Invalid UTN.');
		  objUTN.focus();
		  return false; 
		}
				
		if (isNaN(objUTN.value.charAt(3))) {
		  alert('Invalid UTN pattern. The last character must be a number.');
		  objUTN.focus();
		  return false; 
		}
		
		if (objUTN.value.substring(0, 2) != strTcUTNPattern){
			if (isNaN(objUTN.value.charAt(1))) {
			  alert('Invalid UTN pattern. The first two characters must be numbers or the company utn pattern.');
			  objUTN.focus();
			  return false; 
			}
			/*
			alert('The first two characters must be the UTN pattern for the trucking company');
		    objUTN.focus();
			return false; 
			*/
		}
		
		if (flgEnteredUtnLength == 2){
			alert('Your UTN number is ' + objUTN.value);
		}
		return true;
	}
}
//This function to validate Date in format mmdd
function validateDate(objTxtBox,YearPart){
	if (objTxtBox.value.length != 4){
		if (objTxtBox.value.length == 3){
			objTxtBox.value = '0' + objTxtBox.value;
		}else{
			alert('invalid Format.');
			objTxtBox.focus();
			return false;			
		}		
	}	
	var MonthPart = objTxtBox.value.substring(0,2);
	var DayPart = objTxtBox.value.substring(2,4);
	if (!isDate(YearPart, MonthPart, DayPart)){
			alert('invalid Date Format.');
			objTxtBox.focus();
			return false;
	}
}	
//This function to validate Time in format hhmm
function validateTime(objTxtBox){
	if (objTxtBox.value.length != 4){
		if (objTxtBox.value.length == 3){
			objTxtBox.value = '0' + objTxtBox.value;
		}else{
			alert('invalid Time Format.');
			objTxtBox.focus();
			return false;			
		}		
	}	
	var HourPart = objTxtBox.value.substring(0,2);
	var MinPart = objTxtBox.value.substring(2,4);
	if ((MinPart > 59) || (MinPart < 0)){
			alert('invalid Time Format.');
			objTxtBox.focus();
			return false;
	}
	if ((HourPart > 23) || (HourPart < 0)){
			alert('invalid Time Format.');
			objTxtBox.focus();
			return false;
	}
}
<!-- To Compare Dates -->

function compareDates(date1,date2){
		
	//To Date
	
	var firstpos=date2.value;
	var firstpos=firstpos.indexOf("/");
    UserDay=date2.value.substring(0,firstpos);
	var lastpos=date2.value;
	var lastpos=lastpos.lastIndexOf("/");
    UserMonth=date2.value.substring(firstpos+1,lastpos);
	var mystring=date2.value;
    UserYear=date2.value.substring(lastpos+1,date2.value.length);
	
	//From Date
	var firstpos=date1.value;
	var firstpos=firstpos.indexOf("/");
    myday=date1.value.substring(0,firstpos);
	var lastpos=date1.value;
	var lastpos=lastpos.lastIndexOf("/");
    mymonth=date1.value.substring(firstpos+1,lastpos);
	var mystring=date1.value;
    myyear=date1.value.substring(lastpos+1,date1.value.length);
	//
	daydiff=UserDay-myday;
	restdays=31-myday;
	currentday=restdays+UserDay;
	
	if(myyear >UserYear)
	{
	    alert("Error. Date1 cannot be larger than Date2");
		date1.focus();
		return false;
	}
 
	else if((myyear==UserYear) && (mymonth >UserMonth))
		{
        alert("Error. Date1 cannot be larger than Date2");
		date1.focus();
		return false;
		}
	else if((myyear==UserYear) && (mymonth==UserMonth)  && (myday>UserDay))
	{
	   alert("Error. Date1 cannot be larger than Date2");
	   date1.focus();
	   return false;
	}
	
}

<!-- To Compare Dates without alerts... -->
// if (date2) = 0 then it will compare (date1) with the (sysDate).
// This functoin returns False if (Date1) is larger thatn (Date2) or (sys. date)
function compareDatesNoAlert(date1,date2, sysDate)
{		
	//To Date
	if ( date2 != 0 )
	{
	var firstpos=date2.value;
	var firstpos=firstpos.indexOf("/");
    UserDay=date2.value.substring(0,firstpos);
	var lastpos=date2.value;
	var lastpos=lastpos.lastIndexOf("/");
    UserMonth=date2.value.substring(firstpos+1,lastpos);
	var mystring=date2.value;
    UserYear=date2.value.substring(lastpos+1,date2.value.length);
	}
	else
	{
	var firstpos=sysDate.indexOf("/");
    UserDay=sysDate.substring(0,firstpos);
	var lastpos=sysDate;
	var lastpos=lastpos.lastIndexOf("/");
    UserMonth=sysDate.substring(firstpos+1,lastpos);
	var mystring=sysDate;
    UserYear=sysDate.substring(lastpos+1,sysDate.length);
	}
	//From Date
	var firstpos=date1.value;
	var firstpos=firstpos.indexOf("/");
    myday=date1.value.substring(0,firstpos);
	var lastpos=date1.value;
	var lastpos=lastpos.lastIndexOf("/");
    mymonth=date1.value.substring(firstpos+1,lastpos);
	var mystring=date1.value;
    myyear=date1.value.substring(lastpos+1,date1.value.length);
	//
	daydiff=UserDay-myday;
	restdays=31-myday;
	currentday=restdays+UserDay;
	
	if(myyear >UserYear)
	{
	    //alert("Invalid date.Date From cannot be larger than Date To.");
		date1.focus();
		return false;
	}
 
	else if((myyear==UserYear) && (mymonth >UserMonth))
		{
        //alert("Invalid date.Date From cannot be larger than Date To.");
		date1.focus();
		return false;
		}
	else if((myyear==UserYear) && (mymonth==UserMonth)  && (myday>UserDay))
	{
	   //alert("Invalid date.Date From cannot be larger than Date To.");
	   date1.focus();
	   return false;
	}
	
}


//**************************************************************************
// This Function Used to repalce substring in a string.
//**************************************************************************
function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

//***********************************************************
function txtToHTML(strTesto)
{
	strTesto = replaceSubstring(strTesto, "#", String.fromCharCode(8218));
    strTesto = replaceSubstring(strTesto, "+", String.fromCharCode(402));
	strTesto = replaceSubstring(strTesto, "%", String.fromCharCode(8217));	
	strTesto = replaceSubstring(strTesto, "&", String.fromCharCode(8230));	
	strTesto = replaceSubstring(strTesto, '"', String.fromCharCode(8224));
    return(strTesto);
}
//***********************************************************