//js file

function isDotExpression( InString , NeedsDot)

{
  var dots, index, tmpNeedDot;
  dots = 0;
  for (index = 0; index < InString.length; index++)
  {
    if ( InString.substring( index , index + 1) == "." )
    {
      if ( ( index == 0 ) || ( index == InString.length - 1 ) ) return (false);
      dots++;
      if ( dots > 1 ) tmpNeedDot = 1;
      else tmpNeedDot = 0;
      if ( !isDotExpression( InString.substring( 0 , index ) , tmpNeedDot ) ) return (false);
    }
   }
  if (( NeedsDot == 1 ) && ( dots < 1 )) return (false);
  if ( InString.length < dots * 2 + 1 ) return (false);
  return (true);
  }


 function IsEmail( InString )
  {
  var left, right;

  if ( InString.length == 0 ) return (false);
  for (Count = 0; Count < InString.length; Count++)
  {
    TempChar = InString.substring (Count, Count+1);
	if ("1234567890abcdefghijklmnoprstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ.@_-".indexOf (TempChar, 0) == -1) return (false);
  }
  if ( InString.indexOf('@') < 1 ) return (false); //if it begins with or doesn't have a '@'zzzzzzzzzzzzzzzzz
  if ( InString.lastIndexOf('@') != InString.indexOf('@') ) return (false); //if it has more than one '@'

  left  = InString.substring( 0 , InString.indexOf('@'));
  right = InString.substring( InString.indexOf('@') + 1, InString.length);
  if ( (!isDotExpression( left, 0 )) || (!isDotExpression( right , 1 )))
  return false;
  return true;
  }

   function alltrim(s)
    {
   	  slen = s.length
   	  cnt = 0
      	  for(i=0;i<slen;i++)
   	  {
   	  	if(s.charAt(i) == " ")
   	  	    cnt++
   	  	else
   	  		break
   	  }

   	  if (cnt == slen)
   	  	return ""

   	  s = s.substring(cnt)

	  slen = s.length
   	  cnt = 0
   	  for(i=slen-1;i>=0;i--)
   	  {
   	  	if(s.charAt(i) == " ")
   	  		cnt++
   	  	else
   	  		break
   	  }
	  s = s.substring(0,slen-cnt)

     return s
   }

 function Validate()
 {
      document.contactusForm.Name.value = alltrim(document.contactusForm.Name.value);
      document.contactusForm.Email.value = alltrim(document.contactusForm.Email.value);
      document.contactusForm.Itineraryno.value = alltrim(document.contactusForm.Itineraryno.value);

      for(var i=0;i<document.contactusForm.type_of_reservation.length;i++)
        {
            if(document.contactusForm.type_of_reservation.options[i].selected)
            {
             if(document.contactusForm.type_of_reservation.options[i].value=="")
              {
                  alert("Please select the category.");
                  return false;
              }
              else if (document.contactusForm.type_of_reservation.options[i].value=="Hostel" || document.contactusForm.type_of_reservation.options[i].value=="Tour")
              {
                  // validate the first name
    	       if  (alltrim(document.contactusForm.Name.value)=="" )
    	     {
 	      alert("Please enter your Name");
 	      document.contactusForm.Name.focus();
 	      return false;
 	     }



                // validate e-mail

                   if(alltrim(document.contactusForm.Email.value)!="")
                 {
 	                if(!IsEmail(document.contactusForm.Email.value))
                   	    {
 	                    strErrorMessage = "Please enter valid Email Address.\n";
                             alert(strErrorMessage);
                            document.contactusForm.Email.focus();
                             return false;
                            }
                }
          else
          {
           alert("Please enter your Email Address");
            document.contactusForm.Email.focus();
            return false;
         }


      if(alltrim(document.contactusForm.Comments.value)=="")
         {
   	alert("Please enter Comments");
   	document.contactusForm.Comments.focus();
   	return false;

       }

      }
      else
      {
         // validate the first name
 	if  (alltrim(document.contactusForm.Name.value)=="" )
 	 {
 	  alert("Please enter your Name");
 	  document.contactusForm.Name.focus();
 	  return false;
 	 }



      // validate e-mail

    if(alltrim(document.contactusForm.Email.value)!="")
   {
 	if(!IsEmail(document.contactusForm.Email.value))
 	 {
 	  strErrorMessage = "Please enter valid Email Address.\n";
          alert(strErrorMessage);
          document.contactusForm.Email.focus();
          return false;
     }
   }
   else
   {
       alert("Please enter your Email Address");
       document.contactusForm.Email.focus();
       return false;
   }



   if(alltrim(document.contactusForm.Itineraryno.value)=="")
   {
   	alert("Please enter your Itinerary number");
   	document.contactusForm.Itineraryno.focus();
   	return false;

   }

   if(alltrim(document.contactusForm.Comments.value)=="")
   {
   	alert("Please enter Comments");
   	document.contactusForm.Comments.focus();
   	return false;

   }

      }

     }
   }





 }

