
function trim(str) 
{
   while (str.substring(0,1) == ' ') str = str.substring(1);
   while (str.substring(str.length-1,str.length) == ' ') str = str.substring(0,str.length-1);
   return(str);
}

function isWhitespace (s)

{   
   var whitespace = " \t\n\r"; 
   var i;
    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) != -1) 
	{
		alert(s + " contain whitespace or linefeed.");
 		return true;
        }//if
    }//for
    // All characters are whitespace.
    return false;
}

function isEmpty(s)
{   
     if((s == null) || (s.length == 0))
	alert("Empty string.");
     return ((s == null) || (s.length == 0))
}

function echeck(str)
{
   var s = str;
   var i = 1;
   var sLength = s.length;
   var defaultEmptyOK = false;	
   var validstr = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@._-";

   if (isEmpty(s)) 
   if (echeck.arguments.length == 1) return defaultEmptyOK;
   else return (echeck.arguments[1] == true);

   
    // is s whitespace?
    if (isWhitespace(s)) return false;


   // look for @
   while ((i < sLength) && (s.charAt(i) != "@"))
   { 
      i++;
   }

   if ((i >= sLength) || (s.charAt(i) != "@"))
   {
      alert("Please enter a valid email.");
      return false;
   }
   else 
      i += 2;

   for(i = 0; i < sLength; i++) {
       if(validstr.indexOf(s.charAt(i)) == -1)  {    
          alert("Please enter a valid email.");
          return false
       }
   } 

   var lat = s.indexOf('@');
   if (lat > 0){
      if(s.substring(lat+1).indexOf('@')>0) {
	alert("Invalid E-mail ID double occurance of @");
	return false
	}	
   }

   // look for .
   while ((i < sLength) && (s.charAt(i) != "."))
   { 
      i++;
   }

   var ldot = s.substring(lat+1).indexOf('.');
   if (ldot <= 0) {
	alert("Invalid E-mail ID missing '.'");
   	return false;
   }	

   i = s.lastIndexOf('.');

   // there must be at least one character after the .
   if ((i >= sLength - 1) || (s.charAt(i) != "."))
   {
      alert("Invalid E-mail ID.\n there must be at least one character after the . ");      
      return false;
   }
   else 
      return true;
}

function replaceAll(strReplace,StrOld, StrNew)
{
   while(strReplace.indexOf(StrOld)>=0)
      strReplace = strReplace.replace(StrOld, StrNew) ;

   return(strReplace);
}


// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isLetter (c)
{   
    if(!(((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z"))))	
        alert( c + " character is not a letter ");
    return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}



// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   
    if(!(((c >= "0") && (c <= "9"))))	  
        alert(c +" is not a digit");
return ((c >= "0") && (c <= "9"))
}



// Returns true if character c is a letter or digit.

function isLetterOrDigit (c)
{   
    return (isLetter(c) || isDigit(c))
}

//------------------------------------------------------------------- //
//isInteger(value) Returns true if value contains all digits
//------------------------------------------------------------------- 
function isInteger(val)
{
    if (isBlank(val)){
	   return false;
     } 
    for(var i=0;i<val.length;i++){
	   if(!isDigit(val.charAt(i))){
		return false;
	   } 
     } 
   return true; 
} 

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;
}
