
function validateDate(day, month, year)
{
    var d = new Date();
    d.setFullYear( year, month-1, day );

    return (d.getDate() == day);
}


function Redirect(url)
{
	document.location.href=url;
}

function trim(s) 
{
// Remove leading spaces and carriage returns
while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
{ s = s.substring(1,s.length); }

// Remove trailing spaces and carriage returns
while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
{ s = s.substring(0,s.length-1); }

return s;
} 

function isnumeric(sText)
{
	if (trim(sText) == '') return false;
	
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

//*************************************************** 
// *** Date Validation Script *********************** 
//*************************************************** 
// *** assembled by Heaven's Martini **************** 
// *** borrowed ideas from all over (www.irt.org **** 
//*************************************************** 

// ******************************************************** 
// *** movebox takes the parameters of the active field *** 
// *** the next field you want to auto forward to and the** 
// *** length of the activefield when auto forward ******** 
function movebox(activefield,nextfield,maxlen) 
{ 
  var activefieldlen = activefield.value.length; 
   
   if (activefieldlen == maxlen) 
    nextfield.focus(); 
} 
// **** End if Move Box *************************** 

// ************************************************ 
//  *** Upper Range integer function ************** 

function upRange(mynum,upr) 
{ 
  var un = mynum.value; 
  if (un > upr) 
   return false; 
  else   
   return true; 
} 

function LeapYear(year) { 
    if ((year/4)   != Math.floor(year/4))   return false; 
    if ((year/100) != Math.floor(year/100)) return true; 
    if ((year/400) != Math.floor(year/400)) return false; 
    return true; 
} 

// ************************************************* 


function checkDay(dobj,monthobj) 
{ 
   var mobj = monthobj.value; 

  if (isNaN(dobj.value)) 
    { 
     alert("The Day field can only contain numbers\n You inputted: " + mobj.value); 
     mobj.value = "0"; 
    } 

   if (mobj.length < 1) 
     monthobj.focus(); 
   else 
    { 
      // ** if first day num is > 3, add a 0 to it ** 
     if (dobj.value.length == 1) 
      if (dobj.value > 3) 
       dobj.value = '0' + dobj.value; 
       
     if (dobj.value.length == 2) 
      { 
       if (mobj == 1 || mobj == 3 || mobj == 5 || mobj == 7 || mobj == 8 || mobj == 10 || mobj == 12) 
        var daymax = 31; 
       else if (mobj == 2) 
        var daymax = 29 
       else 
        var daymax = 30 

       if (!upRange(dobj,daymax)) 
        { 
         alert("There is no Day Greater than " + daymax); 
         dobj.value = daymax; 
         dobj.focus(); 
        } 
   
      } // ** if length of field is 2, then do cool things ** 
    } // ** Month has correct month. 
} 
//***************************************************** 
//***************** END OF DAY ************************ 

//***************************************************** 
//***************** Start of MONTH ************************ 
//***************************************************** 
function checkMonth(mobj) 
{ 
  if (isNaN(mobj.value)) 
    { 
     alert("The Month field can only contain numbers\n You inputted: " + mobj.value); 
     mobj.value = "0"; 
    } 

   { 
    // ** if first num is > 1, add a 0 to it ** 
     if (mobj.value.length == 1) 
       if (mobj.value > 1) 
        mobj.value = '0' + mobj.value; 

     if (mobj.value.length == 2) 
      { 
       if (!upRange(mobj,12)) 
        { 
         alert("There is no Month Greater than 12"); 
         mobj.value = 12; 
         mobj.focus(); 
        } 
   
      } // ** if length of field is 2, then do cool things ** 
    } // *** numbers only *** 
} 
//***************************************************** 
//***************** END OF Month ************************ 

//***************************************************** 
//***************** Start of YEAR ************************ 
//***************************************************** 

function checkYear(yobj,mobj,dobj) 
{ 
  if (isNaN(yobj.value)) 
    { 
     alert("The Year field can only contain numbers\n You inputted: " + mobj.value); 
     mobj.value = "0"; 
    } 
  else 
   { 
    if (yobj.value.length == 2) 
     { 
       // ****** Quick Year ****** 
      if(yobj.value-0 < 10) 
       yobj.value = "20" + yobj.value; 
      if (yobj.value > 50 && yobj.value-0 < 100) 
       yobj.value = "19" + yobj.value; 
     } // ** if 2 numbers in validation **   

    if (yobj.value.length == 4) 
     { 
       // ******* Ridiculous Year Validation ****** 
      if (yobj.value > 2100) 
       { 
        alert("The Year field can only contain \n realistic years not " + yobj.value); 
       } 
      if (yobj.value < 1900) 
       { 
        alert("The Year field can only contain \n realistic years not " + yobj.value); 
        yobj.value = "19"; 
       } 
     
      if (!upRange(mobj,12)) 
       { 
        alert("There is no Month Greater than 12"); 
        mobj.value = 12; 
        mobj.focus(); 
       } 

      if (!LeapYear(yobj.value) && (mobj.value == 2) && (dobj.value == 29)) 
       { 
        dobj.value = 28;   
       } 
      if (!upRange(dobj,31)) 
       { 
        alert("There is no Day Greater than 31"); 
        dobj.value = 31; 
        dobj.focus(); 
       } 
     } // *** if 4 digits in Year ***   
  } // *** numbers only *** 
} 
//***************************************************** 
//***************** END OF YEAR ************************ 
//***************************************************** 
//******************** End of Date Validation*********** 

function ToSQLString(str2)
{
	strReply = '';
	iAscii = 0;
	strChar = '';
	
	for(x=0; x<str2.length; x++)
	{
		strChar = str2.charAt(x);
		iAscii = str2.charCodeAt(x);
		
		if ((iAscii >= 0) && (iAscii <= 31))
			strReply = strReply + ' ';
		
		else if (iAscii == 127)
			strReply = strReply + ' ';
			
		else if (iAscii == 34) //double quote
			strReply = strReply + String.fromCharCode(127);
		
		else if (iAscii == 39) //single quote
			strReply = strReply + String.fromCharCode(255);
			
		else if (iAscii == 46) //slash
			strReply = strReply + String.fromCharCode(95);
			
		else if (iAscii == 92) //backslash
			strReply = strReply + String.fromCharCode(95);
			
		else if (iAscii == 59) //semi colon
			strReply = strReply + String.fromCharCode(95);
			
		else
			strReply = strReply + strChar;
	}
	
	return strReply;
}

function FromSQLString(str2)
{
	strReply = '';
	iAscii = 0;
	
	for(x=0; x<str2.length; x++)
	{
		strChar = str2.charAt(x);
		iAscii = str2.charCodeAt(x);
		
		if (iAscii == 127)
			strReply = strReply + '"';
		
		else if (iAscii == 255)
			strReply = strReply + '\'';
		
		else 
			strReply = strReply + strChar;
	}
	
	return strReply;
}

function IsAlphabetOnly(str2)
{
	iAscii = 0
	
	for(x=0; x<str2.length; x++)
	{
		iAscii = str2.charCodeAt(x);
		
		if (!((iAscii >= 65 && iAscii <= 90) || (iAscii >= 97 && iAscii <= 122)))
			return false;
	}
	
	return true;
}

function IsAlphaNumericOnly(str2)
{
	iAscii = 0
	
	for(x=0; x<str2.length; x++)
	{
		iAscii = str2.charCodeAt(x);
		
		if (!((iAscii >= 48 && iAscii <= 57) || (iAscii >= 65 && iAscii <= 90) || (iAscii >= 97 && iAscii <= 122)))
			return false;
	}
	
	return true;
}

function clock() 
{
	if (!document.layers && !document.all) return;
	var digital = new Date();
	
	var months=new Array(13);
	months[1]="January";
	months[2]="February";
	months[3]="March";
	months[4]="April";
	months[5]="May";
	months[6]="June";
	months[7]="July";
	months[8]="August";
	months[9]="September";
	months[10]="October";
	months[11]="November";
	months[12]="December";

	var day 	= digital.getDate();
	var month	= digital.getMonth();
	var year	= digital.getYear();
	
	var hours = digital.getHours();
	var minutes = digital.getMinutes();
	var seconds = digital.getSeconds();
	
	if (minutes <= 9) minutes = "0" + minutes;
	if (seconds <= 9) seconds = "0" + seconds; 
	dispTime = day + " " + months[month+1] + " " + year + " &nbsp; &nbsp;" + hours + ":" + minutes + ":" + seconds 
	
	if (document.layers) {
		document.layers.pendule.document.write(dispTime);
		document.layers.pendule.document.close();
	}
	else
		if (document.all)
			pendule.innerHTML = dispTime;

	setTimeout("clock()", 1000);
}

function emptycheck(t)
{
	if (trim(t.value) == '')
	{
		t.focus();
		return true;
	}
	
	return false;
}

function isempty(t)
{
    return (trim(t) == '')
}

function numericonlycheck(t)
{
	if (!isnumeric(t.value))
	{
		t.focus();
		t.select();
		return false;
	}

	return true;
}

function alphabetonlycheck(t)
{
	if (!IsAlphabetOnly(t.value))
	{
		t.focus();
		t.select();
		return false;		
	}

	return true;
}

function alphanumericonlycheck(t)
{
	if (!IsAlphaNumericOnly(t.value))
	{
		t.focus();
		t.select();
		return false;
	}

	return true;
}

function textboxfocusselect(t)
{
	t.focus();
	t.select();
}

function validateIntegerTextbox(me, e)
{
    var val;
    if(typeof e.which != 'undefined') {
        val = e.which;
    }
    else {
        val = e.keyCode;
    }

        //alert(e.which);
        switch (val) {
            case 0:  //navigation keys in Mozilla
            case 8:  //backspace
            case 9:
            case 13: //enter
            
            case 48: //0
            case 49: //1
            case 50: //2
            case 51: //3
            case 52: //4
            case 53: //5
            case 54: //6
            case 55: //7
            case 56: //8
            case 57: //9
            

                  return true;
            default:
                  return false;
        }
}


