var whitespace = " \t\n\r";

function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{
	var i;

    if (isEmpty(s))
		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)
			return false;
    }

    // All characters are whitespace.
    return true;
}

function isEmail (s)
{   if (isEmpty(s)) 
		return false;

     /*  if (isEmail.arguments.length == 1)
			return defaultEmptyOK;
       else
			return (isEmail.arguments[1] == true);*/
   
    if (isWhitespace(s))
		return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    {
		i++;
    }

    if ((i >= sLength) || (s.charAt(i) != "@"))
		return false;
    else
		i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    {
		i++;
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != "."))
		return false;
    else
		return true;
}

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 to check the validity of expirty month not less than
//current month when the expiry year is the same of current year.
//it receives three values - expiry year (as number in four digits eg. 1999)
//expiry month as (first three letters of month) and the field name.
function CardExpiry (year, month, theField)
 {
          var curdate = new Date();
          var curmm = curdate.getMonth();
          var curyy = curdate.getYear();
          if (curyy <= 99) {
             curyy = curyy + 1900;
          }
          if (month == "Jan")
             { var curmonth = 0 }
          if (month == "Feb")
             { var curmonth = 1 }
          if (month == "Mar")
             { var curmonth = 2 }
          if (month == "Apr")
             { var curmonth = 3 }
          if (month == "May")
             { var curmonth = 4 }
          if (month == "Jun")
             { var curmonth = 5 }
          if (month == "Jul" )
             { var curmonth = 6 }
          if (month == "Aug")
             { var curmonth = 7 }
          if (month == "Sep")
             { var curmonth = 8 }
          if (month == "Oct")
             { var curmonth = 9 }
          if (month == "Nov")
             { var curmonth = 10 }
          if (month == "Dec")
             { var curmonth = 11 }
         if ((curyy == year) && (curmonth < curmm)) {
            alert ("Invalid Credit Card Expiry Date");
            theField.focus();
           return false;
         }
return true;
 }


function futureDate(year1, theField)
 {
   var curdate = new Date();
   var curyear = curdate.getYear();
   if (curyear <= 99) {
      curyear = ((curyear + 1900) - 10)
   }
   if (year1 > curyear) {
      alert ("Date of Birth should be atleast 10 years less than current year");
      theField.focus();
      return false;
   }

  return true;
}



// called onClick of form
function validateForm(theForm)
{
if ( ! validateTextField (theForm.name, "Name", true,
			true, 1, 50,
			"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. ", "letters, spaces and fullstop"))
		return false;

if ( ! validateTextField (theForm.address1, "Address", true,true,1,50,
       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz., 0123456789-!@#$%^&*(){}[]:;<>/", "digits, letters, spaces, fullstop , comma and hyphen "))
        return false;

if ( ! validateTextField (theForm.address2, "Address1", true,false,1,50,
       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz., 0123456789-!@#$%^&*(){}[]:;<>", "digits, letters, spaces, fullstop , comma and hyphen "))
        return false;

if ( ! validateTextField (theForm.city, "City", true,
     true, 1, 50,
     "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- .,", "digits, letters, spaces, fullstop and hyphen "))
	return false;

 if ( ! validateTextField (theForm.zip, "PinCode", true, true, 4, 10, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- ", "digits, letters, spaces and hypen"))
     return false;

    
if ( ! validateTextField (theForm.state, "State", true, true, 1, 25, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- .", "digits, letters, spaces, fullstop and hyphen "))
        return false;

if ( ! validateTextField (theForm.EMail, "Email Address", true, true, 1, 50, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-@._", "digits, letters, spaces, fullstop and hyphen "))
        return false;

if (! isEmail (theForm.EMail.value))
       {
        alert("Please enter a valid EMail address in the \"E-Mail\" field.");
        theForm.EMail.focus();
        return (false);
       }
if (theForm.phone.value != "" )
   {

   if ( ! validateTextField (theForm.phone, "Phone", true, true, 1, 15, "0123456789-() ", "digits, left parenthesis, right parenthesis, spaces and hyphen"))
     return false;
   }

if (theForm.fax.value != "" )
   {

   if ( ! validateTextField (theForm.fax, "Fax", true, true, 1, 15, "0123456789-() ", "digits, left parenthesis, right parenthesis, spaces and hyphen"))
     return false;
   }

if (theForm.catid.value != 4)
{
if ( ! validateTextField (theForm.adult, "Adult", true, true, 1, 2, "0123456789", "digits"))
    {
     return false;
     }
}

if (theForm.catid.value == 4)
{
if ( ! validateTextField (theForm.adult, "Adult", true, true, 1, 2, "0123456789", "digits"))
    {
     return false;
     }
}

if (theForm.catid.value == 4)
{
    //if ((theForm.adult.value < 18) || (theForm.adult.value > 35))
    if ((theForm.adult.value < Number(theForm.minbookticket.value)) || (theForm.adult.value > Number(theForm.maxbookticket.value)))
       {
        alert("Total No. of tickets should be Min of " + Number(theForm.minbookticket.value) + " and Max of " + Number(theForm.maxbookticket.value));
	    theForm.adult.focus();
        return false;
       }
}


if ( ! validateTextField (theForm.child, "Child", true, true, 1, 2, "0123", "digits of 1 or 2 or 3"))
     return false;
  
if ( theForm.child.value != "" ) 
if ( Number(theForm.child.value) < 0 ) 
   {
    alert("No. of childs should be more than zero");
    theForm.child.focus();
    return false;
   }
   

  if (theForm.catid.value != 4)
  {
  if ( Number(theForm.adult.value) + Number(theForm.child.value) <  Number(theForm.minbookticket.value) )
   {
    alert("Minimum number of tickets should be " + Number(theForm.minbookticket.value));
    theForm.adult.focus();
    return false;
   }
   }

  if (theForm.catid.value != 4)
  {
  if ( Number(theForm.adult.value) + Number(theForm.child.value) >  Number(theForm.maxbookticket.value) )
   {
    alert("Total no. of tickets you can book is " + Number(theForm.maxbookticket.value));
    theForm.adult.focus();
    return false;
   }
   }
 

if ( Number(theForm.adult.value) + Number(theForm.child.value) < 1 )
   {
    alert(" No. of tickets for either adult or child should be given");
    theForm.adult.focus();
    return false;
   }
  
  return true;
}


function validTourDate (givenDate,validtourdays)
{
 var curdate, curyear , curmonth , curday;
 var curdate3month,datesame;
 var todaydate,cnt,firstofmonth,todaydate1;
 var tourdate,noofdaysofmonth,tourdays;

curdate = new Date();
curyear = Number(String(curdate.getYear()).substr(1))+(curdate.getYear()<100?1900:2000);
curmonth = curdate.getMonth();
curday = curdate.getDate();
datesame = 0;
if (givenDate.value.length==0)
    {
     alert("The format of Tour date is dd/mm/yyyy or dd-mm-yyyy");
     return(false);
    }

if ( ! validateTextField (givenDate, "Tour Date", true, true, 8, 10, "0123456789 /-", "digits, blank space and slash"))
     return false;

dateElements = givenDate.value.split('/');


if (dateElements.length!=3)
 {
   dateElements = givenDate.value.split('-');
   if (dateElements.length!=3)
      {
       alert("The format of Tour date is dd/mm/yyyy or dd-mm-yyyy");
       return(false);
      }
 }

if (curmonth == "Jan")
   noofdaysofmonth = 31 
if (curmonth == "Feb")
    if ( curyear % 4 == 0)
       noofdaysofmonth = 29;
    else
       noofdaysofmonth = 28;
if (curmonth == "Mar")
   noofdaysofmonth = 31 
if (curmonth == "Apr")
   noofdaysofmonth = 30 
if (curmonth == "May")
   noofdaysofmonth = 31 
if (curmonth == "Jun")
   noofdaysofmonth = 30 
if (curmonth == "Jul" )
   noofdaysofmonth = 31 
if (curmonth == "Aug")
   noofdaysofmonth = 31 
if (curmonth == "Sep")
   noofdaysofmonth = 30 
if (curmonth == "Oct")
   noofdaysofmonth = 31 
if (curmonth == "Nov")
   noofdaysofmonth = 30 
if (curmonth == "Dec")
   noofdaysofmonth = 31 
  
 if ( (Number(dateElements[0])<1) || (Number(dateElements[0])>noofdaysofmonth))
    {
     alert("Day value entered  is not valid");
     return(false);
    }

 if ( (Number(dateElements[1]-1)<0) || (Number(dateElements[1]-1)>12))
    {
     alert("Month value entered  is not valid");
     return(false);
    }
 if ( ( dateElements[2] < ( curyear - 1 ) ) || ( dateElements[2]> ( curyear + 1 ) ) )
    {
     alert("Year value entered  is not valid");
     return(false);
    }

 tourdate = new Date(dateElements[2],dateElements[1]-1,dateElements[0]);
 todaydate = new Date(curyear,curmonth ,curday);


 curdate3month = new Date(curyear,curmonth + 3,curday);
 if ( tourdate > curdate3month )
    {
     alert("Tour date is not valid \n\n You can't reserve beyond 3 month interval");
     return(false);
    }
if ( ( tourdate - todaydate ) < 259200000 )
    {
     alert("Tour date is not valid  \n\n You can't reserve below 3 days interval");

     return(false);
    }

tourdays = validtourdays.value.split('-');
if ( tourdays[0] == 3 )
   {
    
    curdate = tourdays[2].split(','); //3rd 4th
    noofdaysofmonth = tourdays[1].split(',');//friday
    datematch:
    for (cnt=curmonth; cnt<curmonth+3;cnt++)
        {
          firstofmonth = new Date(curyear,cnt,1);
          firstofmonth = firstofmonth.getDay()
    for (curdate3month = 0;curdate3month < noofdaysofmonth.length;curdate3month++)
        {
         todaydate = 1 +( noofdaysofmonth[curdate3month]  - firstofmonth ) 
         for (curday = 0;curday < curdate.length; curday++) 
            {
             
             todaydate1 = todaydate + 7 * ( curdate[curday] - 1 );    
             if ( todaydate < 32 )
                {
                  tourdays = new Date(curyear,cnt,todaydate1);
                  if ( tourdate - tourdays == 0 )
                     {
                      datesame = 1;
                      break datematch;
                     }
                 }
                 
             }
          }
          }
  if ( datesame != 1 )
     {
       alert("Tour is not available on this date");
       return false;
     }

   }

 return (true);

}



