function ltrim (s) // Krish
{
	for (i=0; i<s.length; i++)
		if ( s.charAt(i) != ' '  && s.charAt(i) != '\n'	&&
		     s.charAt(i) != '\t' && s.charAt(i) != '\b' &&
		     s.charAt(i) != '\r' && s.charAt(i) != '\f' )
			break;
	s = s.substring (i);
	return (s);
}

function validateTextField (theField, fieldLabel, shouldTrim, emptyCheck, minLength, maxLength, checkOK, OKString)
{
	fieldNameString = ' "' + fieldLabel + '" ';
	if (shouldTrim)
		theField.value = ltrim (theField.value);

	if (emptyCheck && theField.value == "")
	{
	alert("Please enter a value for the" + fieldNameString + "field.");
	theField.focus();
	return (false);
	}

	if (emptyCheck && minLength > 0 && theField.value.length < minLength)
	{
		alert("Please enter at least " + minLength + " characters in the" + fieldNameString + "field.");
		theField.focus();
		return (false);
	}

	if (maxLength > 0 && theField.value.length > maxLength)
	{
		alert("Please enter at most " + maxLength + " characters in the " + fieldNameString + "field.");
		theField.focus();
		return (false);
	}

	checkOK = ltrim(checkOK);
	if (checkOK != "")
	{
		var checkStr = theField.value;
		var allValid = true;
		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
			if (j == checkOK.length)
			{
				 allValid = false;
				 break;
			}
		}

		if (!allValid)
		{
	alert("Please enter only " + OKString + " in the" + fieldNameString + "field.");
			theField.focus();
			return (false);
		}
	}

	return true;
}

function validateDate1 (givenDate)
{
 var yearField, monthField, dayField;
 var validDate;

 dateElements = givenDate.value.split('/');

 validDate = dateElements [1] + "/" +  dateElements [2] + "/" +dateElemen
ts[0];
return (validDate);
}
function optionvalueupdate(theForm)
{
var i1,htlt
 var i1=theForm.txtR1.selectedIndex;
 var htlt=theForm.txtR1.options[i1].value;
alert(htlt);
}

// called onSubmit of form
function validatepen(theForm)
{
  var dateToday = new Date();
  var dateGiven = new Date();
  var str;
	  if ( ! validateTextField (theForm.txtname, "Name", true, true, 1, 50,  "abcdefghijklmnopqrstuvwxyz .ABCDEFGHIJKLMNOPQRSTUVWXYZ", "Alphaphets"))
	 return false;
         
if ( ! validateTextField (theForm.txtdesg, "Designation", true, true, 1, 50,"abcdefghijklmnopqrstuvwxyz .ABCDEFGHIJKLMNOPQRSTUVWXYZ", "Alphaphets"))
         return false;
 
         if ( ! validateTextField (theForm.txtdob, "Date of Birth", true,true, 10, 10,  "0123456789/", " Digits"))
         return false;
     
       if ( ! validateDate (theForm.txtdob, ' Date of Birth'))
             return (false);



if ( ! validateTextField (theForm.txtdoj, "Date of Joining", true,true, 10, 10,  "0123456789/", " Digits"))
         return false;

       if ( ! validateDate (theForm.txtdoj, 'Date of Joining'))
             return (false);


    if(theForm.txttype.options[theForm.txttype.selectedIndex].value =='Vol')
if ( ! validateDate (theForm.txtdor, 'Date of  Retirement'))
             return (false);
if ( ! validateTextField (theForm.txteolcredit, " EOL Surrender",  true,false,1,3,"0123456789","digits"))
 return (false);
if(parseInt(theForm.txteolcredit.value) >300)
{
    alert('Enter the value in the range 1-300 in the EOL Credit Field')
    theForm.txteolcredit.focus(); 
    return (false);
}  

if ( ! validateTextField (theForm.txtbpay, " Basic Pay",  true,true,1,5,"0123456789","digits"))
 return (false);

if ( ! validateTextField (theForm.txtperpay, " Personal Pay",  true,false,1,5,"0123456789","digits"))
 return (false);

if ( ! validateTextField (theForm.txtspepay, " Special Pay",  true,false,1,5,"0123456789","digits"))
         return (false);

if ( ! validateTextField (theForm.txteoly, " EOL  without MC year ",  true,false,1,2,"0123456789","digits"))
         return false;

if ( ! validateTextField (theForm.txteolm, " EOL  without MC month ",  true,false,1,2,"0123456789","digits"))
         return false;

if (! validateTextField (theForm.txteold, " EOL  without MC day ",  true,false,1,2,"0123456789","digits"))
         return false;

if ( ! validateTextField (theForm.txtsusy, " Suspension Year ",  true,false,1,2,"0123456789","digits"))
         return false;

if ( ! validateTextField (theForm.txtsusm, " Suspension Month ",  true,false,1,2,"0123456789","digits"))
         return false;

if (! validateTextField (theForm.txtsusd, " Suspension Day ",  true,false,1,2,"0123456789","digits"))
         return false;

  
return (true);
}

function finalValidation()
{
	var i;
	if ( !validatepen(document.forms[0]) )
	  return false ;
	return document.forms[0].submit(); 
}

