// -- Trims blank spacees to the left and right
function Trim(expression) 
{  
   var Value=""  
   var Accept_Space=false 
   var Counter=0           
   var Counter2=0          
   var characters=false     
   
   for(Counter=0; Counter<expression.length; Counter++) 
   {  if(expression.charAt(Counter)==" ") 
      {  if (Accept_Space) 
         {  // checks for any more characters different than space to the end of the string 
          
            for(Counter2=Counter+1; Counter<expression.length; Counter2++) 
            {  if(expression.charAt(Counter2)!=" ") 
               {  characters=true 
                  Counter2=expression.length 
               } 
            } 
          
            if (characters) 
            {  Value=Value + expression.charAt(Counter) 
               Tem_Letras=false 
            } 
         } 
      } 
      else 
      {  Value=Value + expression.charAt(Counter) 
         Accept_Space=true 
      } 
   } 
   
   return(Value) 
}



//-- VALIDATES PRESSED KEY ACCORDING TO TYPE OF FIELD
 
function ValKey (restriction, argvalue) 
{
   var keycode = 0;
 
   // "C"  -> allows only letters
   // "N"  -> allows only numbers
   // "D"  -> allows only numbers with decimal 
   // "DN" -> allows numbers (positive or negative) with decimal
   // "DT" -> allows numbers and inserts date slash 
   // "X"  -> allows all characters except double quotes and single quotes
 
	if (window.event) 
   		{  
			keycode = window.event.keyCode;  
   
   			if ((restriction == "X") && (keycode == 39 || keycode == 180 || keycode == 38 || keycode == 34))
      			{  
	  				window.event.keyCode = 0;
   				}
   			else 
				{  
	  				if ((keycode > 47) && (keycode < 58))   
	      				{  
		  					if (restriction == "C")
		  						{  
			 						window.event.keyCode = 0;
	        					}
	      				}
      				else
						{  	
							if ((restriction == "DT") && (keycode != 47))
         						{  
								window.event.keyCode = 0;
      							}
     						else
      							{  	
									if ((restriction == "N"))
            							{  
											window.event.keyCode = 0;
           								}
         							else
         								{  		
											if ((restriction == "D") && (keycode != 44))
                 								{  
													window.event.keyCode = 0;
            									} 
            								else
            									{  	
													if ((restriction == "DN") && (keycode != 44) && (keycode != 45))
                    									{  
															window.event.keyCode = 0;
               											}
            									}
   
   		      								if (keycode == 44)
         										{  
													if (InStr(argvalue, ",") >= 0 && restriction != "X")
                    									{  
															window.event.keyCode = 0;
               											}
         										}  
         									else
         										{  
													if (keycode == 45)
            											{  
															if (InStr(argvalue, "-") >= 0)
                       											{  
																	window.event.keyCode = 0;
                  												} 
            											}  
         										}   
      									}   
   								}   
						}
				}
		}
}


//validates email field
function fValEmail(inEmail){
	if (inEmail.substring(inEmail.length - 1,inEmail.length) == "."){
		return false;
	} else {
		return ((inEmail.indexOf("@") > 0) && (inEmail.indexOf(".") > 0));
	}
}



// restrict special characters on KeyPress (IE)

function val_spcl_chars(xstr) {
	
	var iChars = "!%^*()+=[]\\\';,./{}|\":<>?";
   	var wret = true;
   	var wstr = eval(xstr);
	
	for (var i = 0; i < wstr.value.length; i++) {
      	if (iChars.indexOf(wstr.value.charAt(i)) != -1) {
         	//alert('Please don\'t use any special characters.'); 
         	alert('For both the User Name and Password use letters or numbers only. Do not use any other type of characters or add spaces.');
         	
		wstr.value = wstr.value.substring(0, wstr.value.length -1);
         	wret = false;
        }
   	}
	
  	return wret;

}
	
// restrict email field special characters on KeyPress (IE)
function val_email_spcl_chars(xstr) {
	
	var iChars = "!%^*()+=[]\\\';,/{}|\":<>?";
   	var wret = true;
   	var wstr = eval(xstr);
	
	for (var i = 0; i < wstr.value.length; i++) {
      	if (iChars.indexOf(wstr.value.charAt(i)) != -1) {
         	alert('Please don\'t use any special characters.'); 
			wstr.value = wstr.value.substring(0, wstr.value.length -1);
         	wret = false;
        }
   	}
	
  	return wret;

}



// restrict special characters onChange (NS IE)
function val_spcl_chars2(xstr) {
	
	var iChars = "!%^*()+=[]\\\';,./{}|\":<>?";
   	var wret = true;
   	var wstr = eval(xstr);
	
	for (var i = 0; i < wstr.value.length; i++) {
      	if (iChars.indexOf(wstr.value.charAt(i)) != -1) {
         	//alert('Please don\'t use any special characters.');
         	alert('For both the User Name and Password use letters or numbers only. Do not use any other type of characters or add spaces.');
			wstr.value = "";
			wstr.focus();
         	wret = false;
        }
   	}
	
  	return wret;

} 

function val_spcl_chars3(xstr) {
	
	var iChars = "!%^*()+=[]\\\';,./{}|\":<>?";
   	var wret = true;
   	var wstr = eval(xstr);
	
	for (var i = 0; i < wstr.value.length; i++) {
      	if (iChars.indexOf(wstr.value.charAt(i)) != -1) {
         	//alert('Please don\'t use any special characters.');
         	alert('Use letters or numbers only. Do not use any other type of characters or add spaces.');
			wstr.value = "";
			wstr.focus();
         	wret = false;
        }
   	}
	
  	return wret;

} 


// restrict email field special characters onChange (NS IE)
function val_email_spcl_chars2(xstr) {
	
	var iChars = "!%^*()+=[]\\\';,/{}|\":<>?";
   	var wret = true;
   	var wstr = eval(xstr);
	
	for (var i = 0; i < wstr.value.length; i++) {
      	if (iChars.indexOf(wstr.value.charAt(i)) != -1) {
         	alert('Please don\'t use any special characters.'); 
			wstr.value = "";
			wstr.focus();
         	wret = false;
        }
   	}
	
  	return wret;

} 


// -- Searches for one character within a string returning the first position found 
// Zero if nothing found

function InStr(chain,searched)
{
	var pos = -1;
	var n;
	var size;

	size = chain.length ;

   	for (n=0; n < size; n++)
		{  
			if (chain.substring(n, searched.length + n) == searched)
      			{  
					pos = n;
         			n = size + 1
	  			}
   		}
 
   	return pos;

} 





//-- automaticly inserts slash in a date field

function Insert_Slash(argvalue)
{
  
	var I = 0;
	var numslash = 0;
	var keycode = 0;
 
	if (argvalue.size >= 10)
		{  
			qtslash = 2;
   		}
	else
   		{  
			qtslash = 1;
   		}
   
	keycode = window.event.keyCode; 
   
	if (keycode != 8 && keycode != 46)
		{  
			I = InStr(argvalue.value, "/");
   
      		if (I >= 0)
      			{  
					numslash = numslash + 1;
      			}
   
      		I = InStr(argvalue.value.substring(I + 1, 10), "/");
   
      		if (I >= 0)
      			{  
					numslash = numslash + 1;
      			}
   
   			if (numslash < qtslash)
      			{  
					if ((argvalue.value.length == 2 || argvalue.value.length == 5) && (numslash < 2))
         				{  
							argvalue.value = argvalue.value + "/";
      					}
   				}  
   		}   
}

//-- Automaticly jumps to next field --
 
function Move(TheObj, size, TheNext)  
{
	var keycode = 0;
	if (TheObj.value.length == size)
		{  
			TheNext.focus();  
		}
	else
		{  
			if (window.event) 
				{  keycode = window.event.keyCode;
   					if (keycode == 13)
      					{  
							TheNext.focus();
         				}
      			}
   		}
}


//-- Validates Dates --
 
function CritData(argvalue)
{
   var wday;
   var wmonth;
   var wyear;
   var rret;
   var rstr;
 
   rret = true;
 
   if (argvalue.length == 10)
   {
      for (n = 0; n < argvalue.length; n++)
      {
         if ((argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9") && (argvalue.substring(n, n+1) != "/"))
         {
            rret = false;
         }
      }
 
      if (rret)
      {
         wmonth = argvalue.substring(0, 2);
         wday = argvalue.substring(3, 5);
         wyear = argvalue.substring(6, 10);
 
         if (argvalue.substring(2, 3) != "/" || argvalue.substring(5, 6) != "/")
         {
            rret = false;
         }
         else
         {
            if (wmonth < "01" || wmonth > "12")
            {
               rret = false;
            }
            else
            {
               if ((wmonth == "01") || (wmonth == "03") || (wmonth == "05") || (wmonth == "07") || (wmonth == "08") || (wmonth == "10") || (wmonth == "12"))
               {
                  if (wday < "01" || wday > "31")
                  {
                     rret = false;
                  }
               }
               else
               {
                  if ((wmonth == "04") || (wmonth == "06") || (wmonth == "09") || (wmonth == "11"))
                  {
                     if (wday < "01" || wday > "30")
                     {

                        rret = false;
                     }
                  }
                  else
                  {
                     if (wmonth == "02")
                     {
                        if ((wyear % 4) == 0)
                        {
                           if (wday < "01" || wday > "29")
                           {
                              rret = false;
                           }
                        }
                        else
                        {
                           if (wday < "01" || wday > "28")
                           {
                              rret = false;
                           }
                        }
                     }
                     else
                     {
                        rret = false;
                     }
                  }
               }
            }
         }
      }
   }
   else
   {
      rret = false;
   }
 
   return rret;
}


//-- CONVERTS DATE TO STRING FOR COMPARISON --
 
function datastr(wdate)
{
   var retdate;
 
   retdate = "";
   retdate = wdate.substring(6,10) + wdate.substring(3,5) + wdate.substring(0,2);
 
   return retdate;
}

