function isNull(value) {
  return(value.length==0);
}

function isAlphaStr(value) {
   caps=value.toUpperCase();

   for(count=0; count < caps.length; ++count) {
      tc=caps.substring(count,count+1);
      if (tc<'A' || tc>'Z')
         return(false);
   }
   return(true);  
}

function isAlphaNum(value) {
   caps=value.toUpperCase();

   for(count=0; count < caps.length; ++count) {
      tc=caps.substring(count,count+1);
      if ( !((tc>='A' && tc<='Z') || (tc>='0' && tc<='9')) )
         return(false);
   }
   return(true);  
}


function isInteger(value) {
   count=(value.substring(0,1)=="-" || value.substring(0,1)=="+")?1:0;
   for(; count < value.length; ++count) {
      TempChar=value.substring(count,count+1);
      if(TempChar<'0' || TempChar>'9')
         return(false);
   }
   return(true);
}

function isFloat(value) {
   count=(value.substring(0,1)=="-" || value.substring(0,1)=="+")?1:0;
   for(dot_count=0; count < value.length; ++count) {
      TempChar=value.substring(count,count+1);
      if(TempChar==".") {
         if(count+1==value.length)
            return(false);
         ++dot_count;
         if(count==value.length || dot_count>1)
            return(false);
         continue;
      }
      if(TempChar<'0' || TempChar>'9') 
         return(false);
   }
   if(dot_count<1)
      return(false);
   return(true);
}

function isDate(value) {
  if(value.length!=10)
     return(false);
  if(value.charAt(2)!='/' || value.charAt(5)!='/')
     return(false);
  if(!(
       value.charAt(0)>='0' && value.charAt(0)<='9' &&
       value.charAt(1)>='0' && value.charAt(1)<='9' &&
       value.charAt(3)>='0' && value.charAt(3)<='9' &&
       value.charAt(4)>='0' && value.charAt(4)<='9' &&
       value.charAt(6)>='0' && value.charAt(6)<='9' &&
       value.charAt(7)>='0' && value.charAt(7)<='9' &&
       value.charAt(8)>='0' && value.charAt(8)<='9' &&
       value.charAt(9)>='0' && value.charAt(9)<='9'
      )
     )
      return(false);
   if(!(parseInt(value,10)>=1 && parseInt(value,10)<=12 &&
         parseInt(value.substr(3,2),10)>=1 && parseInt(value.substr(3,2),10)<=31 &&
         parseInt(value.substr(6,4),10)>=1976 && parseInt(value.substr(6,4),10)<=3000
       )
     )
      return(false);

   return(true);
}

function isMailAdd(value) {
  atpos=value.indexOf('@',0);
  dotpos=value.indexOf('.',atpos+2);
  if(!(atpos>0 && dotpos>0 && dotpos+1<value.length))
     return(false);
  else
     return(true);
}


fowl_words=new 
   Array("ASS","ASSHOLE","SHIT","FUCK",
         "FUCKED","PUSSY","TIDURT",
         "DICK","DICKHEAD");

function isFowlWord(value)
{
    str=value.toUpperCase();
    fwl = fowl_words.length;

    for (i=0; i<fwl; ++i) {
       //alert("find "+fowl_words[i]+" in "+str);
       if((s_pos=str.indexOf(fowl_words[i],0))>=0 && 
           !((str.charAt(s_pos+fowl_words[i].length) >= 'A' &&
              str.charAt(s_pos+fowl_words[i].length) <= 'Z') ||
             (s_pos!=0 && str.charAt(s_pos-1) >= 'A' &&
              str.charAt(s_pos-1) <= 'Z')
            )
          )
          return(true);
    }
}

//CUSTOM SCRIPTS 1
//Authored by: rcv
//Updated by: rcv
//Updated on: 6/8/00
//Remarks: Untested
function isSpaces(text)
{
 for(var i = 0 ; i < text.length ; i++)
  if (text.substring(i,i+1) != ' ') return false;
  return true;
}

function ltrim(text)
{
	while(text.charAt(0)==" ") text=text.substring(1,text.length);
	//text=rtrim(text)
	return text;
}

function rtrim(text)
{
	while(text.charAt(text.length-1)==" ") text=text.substring(0,(text.length-1));
	return text;
}
////////////////////////////////////////////////////

function trim(text)
{
	return ltrim(rtrim(text));
}

//END CUSTOM SCRIPTS 1

function lpad(str,len,ch)
{
   str1="";
   for (i=1; i<=len-str.length; ++i)
      str1+=ch;
   return(str1+str)
}

function rpad(str,len,ch)
{
   str1=str;
   for (i=1; i<=len-str.length; ++i)
      str1+=ch;
   return(str1)
}


/* Value types. 
   Use any of these as the type parameter for function ckValid */

t_alpha    = 1;
t_alphanum = 2
t_integer  = 3;
t_float    = 4;
t_date     = 5;
t_emailadd = 6;
t_fowlword = 7;

t_empty    = 256; /*Mask for the type param. if "object.value" may be empty. 
                    Example: if field may be optional */

function ckValid(object, name, type, maxlen, minlen) { 

   if (isNull(object.value) || isSpaces(object.value)) {
      if ((type&t_empty)==t_empty) 
         return(true);
      alert(name+" cannot be empty.");
      object.focus();
      return(false);
   }

   if ((t_empty&type)==t_empty)
      type&=~t_empty;   /* Turn t_empty OFF */

   if (maxlen>0 && object.value.length>maxlen) {
      alert(name+" exceeds "+maxlen+" characters.");
      object.focus();
      return(false);
   }

   if (minlen>0 && object.value.length<minlen) {
      alert(name+" is less than "+minlen+" characters.");
      object.focus();
      return(false);
   }


   if (type==null || type=="") {
      return(true);
   }
   else if (type==t_alpha) {
      if(!isAlphaStr(object.value)) {
        alert(name+" is not a string of alpha characters.");
        object.focus();
        return(false);
      }
      return(true);
   }
   else if (type==t_alphanum) {
      if(!isAlphaNum(object.value)) {
        alert(name+" is not a string of alphanumeric characters.");
        object.focus();
        return(false);
      }
      return(true);
   }
   else if (type==t_integer) { 
      if(!isInteger(object.value)) {
        alert(name+" is not an integer.");
        object.focus();
        return(false);
      }
      return(true);
   }
   else if (type==t_float) {
      if(!isFloat(object.value)) {
        alert(name+" is not a decimal number.");
        object.focus();
        return(false);
      }
      return(true);
   }
   else if (type==t_date) {
      if(!isDate(object.value)) {
        alert(name+": Invalid date format.");
        object.focus();
        return(false);
      }
      return(true);
   }
   else if (type==t_emailadd) {
      if(!isMailAdd(object.value)) {
        alert(name+" is not a valid Email Address.");
        object.focus();
        return(false);
      }
      return(true);
   }
   else if (type==t_fowlword) {
      if(isFowlWord(object.value)) {
        alert(name+" is a fowl word! ");
        object.focus();
        return(false);
      }
      return(true);
   }
   return(true);
}

function ckStrEq(obj1, obj2, name)
{
   if (obj1.value != obj2.value) {
     alert(name+" do not match");
     obj1.value="";
     obj2.value="";
     obj1.focus();
     return(false);
   }
   return(true);
}

function is_both_efg(obj1, obj2) 
{
return ( 
		 (  (isNull(obj1.value) || isSpaces(obj1.value))
			 && (isNull(obj2.value) || isSpaces(obj2.value))
		 ) ||
		 (  !(isNull(obj1.value) || isSpaces(obj1.value))
			 && !(isNull(obj2.value) || isSpaces(obj2.value))
		 )
	   );
}


/**
**  
** obj1, obj2   : the objects whose values should either be 
**                both empty or both filled.
** name1, name2 : the descriptive names of the objects.
** isarray      : a boolean to determine if the objects are arrays or not
**
***/
function is_both_ef(obj1, obj2, name1, name2, isarray)
{
 if (!isarray) {
	if (!is_both_efg(obj1,obj2)) {
	   alert("Either "+name1+" and "+name2+" are both empty\n or "
			  +name1+" and "+name2+" are both filled.");
	   return(false);
	}
	else
	   return(true);
 }
 else {
	for (i=0; i<obj1.length; ++i)
	   if (!is_both_efg(obj1[i],obj2[i])) {
		  alert("Either "+name1+" and "+name2+" are both empty\n or "
			  +name1+" and "+name2+" are both filled.");
		  return(false);
	   }
	return(true);
 }
}


/********************** Etc. Functions **********************/

/*Function trimFloat
Rounds a float to the specified no. of digits to the right of the 
decimal point   

flt  the float to be rounded/trimmed
prec the precision. 1 to round to the nearest tenths
					2 to round to the nearest hundredths, and so on...

if prec=0 trimFloat returns flt rounded to the nearest ones
if prec<0 trimFloat returns flt
*/

function trimFloat(flt,prec)
{
if (prec>=0)
return( Math.round(flt * Math.pow(10,prec))/Math.pow(10,prec) )
else
return(flt)
}