function checkAges( field ,message )
{
  var valid = "0123456789"
  var ok = "yes";
  var temp;
  var status=true;

  if(field.value!="")
  {
      for (var i=0; i<field.value.length; i++)
      {
         temp = "" + field.value.substring(i, i+1);
         if (valid.indexOf(temp) == "-1") ok = "no";
      }
      if( ok == "no" )
      {
         alert("Invalid entry for the  "+message+"!. Only numbers are accepted!");
		 field.value = "";
         field.focus();         
         status = false;
       }
       
       else if ( ok == "yes" )
       {
		  if ( field.value < 0 || field.value > 19 )
		  {
		  	 ok = "no";
	         alert("Invalid age for the  "+message+"!. Enter an age between 1-19 !");
			 field.value = "0";
	         field.focus();         
	         status = false;
		  }			      		
       }
   }
   else
   {
       alert("Please enter the "+message+" ." );
       field.focus();
       status = false;
   }
   if (status == true)
   {
      setCheckoutDateByNights();
   }

   return status;
}


function validateRWP()
{
	var valid = false;
	var roomCount = Search.noOfRooms.value;
	
	if ( roomCount == 1 )
	{	
		if ( !validateR1() )
			return false;
	}
	
	else if (  roomCount == 2 )
	{
		if ( !validateR1() )
			return false;
			
		if ( ! validateR2() )
			return false;
	}
	else if (  roomCount == 3 )
	{
		if ( !validateR1() )
			return false;
			
		if ( ! validateR2() )
			return false;

		if ( ! validateR3() )
			return false;
	}
	else if (  roomCount == 4 )
	{
		if ( !validateR1() )
			return false;
			
		if ( ! validateR2() )
			return false;

		if ( !validateR3() )
			return false;
			
		if ( ! validateR4() )
			return false;
	}
	else if (  roomCount == 5 )
	{
		if ( !validateR1() )
			return false;
			
		if ( ! validateR2() )
			return false;
			
		if ( !validateR3() )
			return false;
			
		if ( ! validateR4() )
			return false;
			
		if ( ! validateR5() )
			return false;
	}
	
	return true;
}

function validateR1()
{
	var valid = true;
	var children = Search.rm1_children.value;		
	
	if ( children  == 0 )
	{
		valid = true
	}
	
	if ( children == 1 || children == 2 || children == 3 )
	{
		if ( Search.Child1AgeRoom1.value == null || Search.Child1AgeRoom1.value == "" || Search.Child1AgeRoom1.value == "0" )
		{
			alert ( "Invalid age for the first child" );
			Search.rm1_firstChild.focus();
			valid = false;
		}
	}
	if ( children == 2 || children == 3 )
	{
		if ( Search.Child2AgeRoom1.value == null || Search.Child2AgeRoom1.value == "" || Search.Child2AgeRoom1.value == "0" )
		{
			alert ( "Invalid age for the second child" );
			Search.rm1_secondChild.focus();
			valid = false;
		}
	}
	if ( children == 3 )
	{
		if ( Search.Child3AgeRoom1.value == null || Search.Child3AgeRoom1.value == "" || Search.Child3AgeRoom1.value == "0" )
		{
			alert ( "Invalid age for the third child" );
			Search.rm1_thirdChild.focus();
			valid = false;
		}
	}
	
	return valid;
}

function validateR2()
{
	var valid = true;
	var children = Search.rm2_children.value;		
	
	if ( children  == 0 )
	{
		valid = true
	}
	
	if ( children == 1 || children == 2 || children == 3 )
	{
	
		if ( Search.Child1AgeRoom2.value == null || Search.Child1AgeRoom2.value == "" || Search.Child1AgeRoom2.value == "0" )
		{
			alert ( "Invalid age for the first child" );
			Search.rm2_firstChild.focus();
			valid = false;
		}
	}
	if ( children == 2 || children == 3  )
	{
		if ( Search.Child2AgeRoom2.value == null || Search.Child2AgeRoom2.value == "" || Search.Child2AgeRoom2.value == "0" )
		{
			alert ( "Invalid age for the second child" );
			Search.rm2_secondChild.focus();
			valid = false;
		}
	}
	if ( children == 3 )
	{
		if ( Search.Child3AgeRoom2.value == null || Search.Child3AgeRoom2.value == "" || Search.Child3AgeRoom2.value == "0" )
		{
			alert ( "Invalid age for the third child" );
			Search.rm2_thirdChild.focus();
			valid = false;
		}
	}
	
	return valid;
}

function validateR3()
{
	var valid = true;
	var children = Search.rm3_children.value;		
	
	if ( children  == 0 )
	{
		valid = true
	}
	
	if ( children == 1 || children == 2 || children == 3  )
	{
		if ( Search.Child1AgeRoom3.value == null || Search.Child1AgeRoom3.value == "" || Search.Child1AgeRoom3.value == "0" )
		{
			alert ( "Invalid age for the first child" );
			Search.rm3_firstChild.focus();
			valid = false;
		}
	}
	if ( children == 2 || children == 3 )
	{
		if ( Search.Child2AgeRoom3.value == null || Search.Child2AgeRoom3.value == "" || Search.Child2AgeRoom3.value == "0" )
		{
			alert ( "Invalid age for the second child" );
			Search.rm3_secondChild.focus();
			valid = false;
		}
	}
	if ( children == 3 )
	{
		if ( Search.Child3AgeRoom3.value == null || Search.Child3AgeRoom3.value == "" || Search.Child3AgeRoom3.value == "0" )
		{
			alert ( "Invalid age for the third child" );
			Search.rm3_thirdChild.focus();
			valid = false;
		}
	}
	
	return valid;
}

function validateR4()
{
	var valid = true;
	var children = Search.rm4_children.value;		
	
	if ( children  == 0 )
	{
		valid = true
	}
	
	if ( children == 1 || children == 2 || children == 3 )
	{
		if ( Search.Child1AgeRoom4.value == null || Search.Child1AgeRoom4.value == "" || Search.Child1AgeRoom4.value == "0" )
		{
			alert ( "Invalid age for the first child" );
			Search.rm4_firstChild.focus();
			valid = false;
		}
	}
	if ( children == 2 || children == 3 )
	{
		if ( Search.Child2AgeRoom4.value == null || Search.Child2AgeRoom4.value == "" || Search.Child2AgeRoom4.value == "0" )
		{
			alert ( "Invalid age for the second child" );
			Search.rm4_secondChild.focus();
			valid = false;
		}
	}
	if ( children == 3 )
	{
		if ( Search.Child3AgeRoom4.value == null || Search.Child3AgeRoom4.value == "" || Search.Child3AgeRoom4.value == "0" )
		{
			alert ( "Invalid age for the third child" );
			Search.rm4_thirdChild.focus();
			valid = false;
		}
	}
	
	return valid;
}

function validateR5()
{
	var valid = true;
	var children = Search.rm5_children.value;		
	
	if ( children  == 0 )
	{
		valid = true
	}
	
	if ( children == 1 || children == 2 || children == 3  )
	{
		if ( Search.Child1AgeRoom5.value == null || Search.Child1AgeRoom5.value == "" || Search.Child1AgeRoom5.value == "0" )
		{
			alert ( "Invalid age for the first child" );
			Search.rm5_firstChild.focus();
			valid = false;
		}
	}
	if ( children == 2 || children == 3  )
	{
		if ( Search.Child2AgeRoom5.value == null || Search.Child2AgeRoom5.value == "" || Search.Child2AgeRoom5.value == "0" )
		{
			alert ( "Invalid age for the second child" );
			Search.rm5_secondChild.focus();
			valid = false;
		}
	}
	if ( children == 3 )
	{
		if ( Search.Child3AgeRoom5.value == null || Search.Child3AgeRoom5.value == "" || Search.Child3AgeRoom5.value == "0" )
		{
			alert ( "Invalid age for the third child" );
			Search.rm5_thirdChild.focus();
			valid = false;
		}
	}
	
	return valid;
}

function Today()
{
   var dd = new Date();
 //  return dd;
   //alert dd;
   return(dd.getDate() + "/" + (dd.getMonth()+1) + "/" +  dd.getFullYear());
   //return((dd.getMonth()+1) + "/" + dd.getDate() + "/" + dd.getFullYear());
}

function setInitialDates ()
{
	var dd = new Date ( ( new Date () ).getTime() + 7*24*3600*1000 ) ;
	
	var date = dd.getDate ();	
	var month = ( dd.getMonth () + 1 );
	var yr = dd.getFullYear();
	
	document.Search.checkInDate.value = ( date < 10 ? ( "0" + date ) : date ) + "/" + ( month < 10 ? ( "0" + month ) : month  ) + "/" + yr;	
	setCheckOutDate();
}

function showCheckInCalendar()
{
   var obj = document.getElementById('checkInDate');
   var xlocation = getOffsetLeft ( document.getElementById('CinCal') );
   var ylocation = getOffsetTop ( document.getElementById('CinCal') );
   ylocation += 125;
   xlocation += 22;
  
   if ( document.body.scrollLeft > 0 )
   		xlocation -= document.body.scrollLeft + 8;
   if ( document.body.scrollTop > 0 )
   		ylocation -= document.body.scrollTop + 0;
   /* x=300 , y=280 */
   var chInDate = document.Search.checkInDate.value;
   var controlStr = "";

   if ( chInDate != null && chInDate != "" )
   {
	   controlStr ="AppendOrReplace=Replace;AppendChar=';';CloseOnSelect=Yes;ReturnData=Date;Title= Calendar;InlineX=500;InlineY=270;CurrentDate="+chInDate+";SelectAfter=today;SelectBefore=\"\";AllowWeekends=Yes;Resizable=No;CallFunction=setCheckOutDate ();PopupX="+xlocation+";PopupY="+ylocation+";Nav=Yes;SmartNav=Yes;Fix=Yes;";   		   } 
   else
   {
	   controlStr ="AppendOrReplace=Replace;AppendChar=';';CloseOnSelect=Yes;ReturnData=Date;Title= Calendar;InlineX=500;InlineY=270;CurrentDate=Today();SelectAfter=today;SelectBefore=\"\";AllowWeekends=Yes;Resizable=No;CallFunction=setCheckOutDate ();PopupX="+xlocation+";PopupY="+ylocation+";Nav=Yes;SmartNav=Yes;Fix=Yes;";
   }  
   
   //'dd/mm/yyyy'
   show_calendar('Search.checkInDate',null,null, null,null, controlStr);
}

function showCheckOutCalendar ()
{
   var chInDate = document.Search.checkInDate.value;
   var chOutDate = document.Search.checkOutDate.value;
   
   var coutDate = new Date ( 
   						chInDate.split("/")[2] , //year
   						chInDate.split("/")[1]-1 ,//month
   						chInDate.split("/")[0]    //date
   						);
   var coutDate = new Date ( coutDate.getTime() + 1*24*3600 *1000);
   var coutStr = coutDate.getDate() + "/" + ( coutDate.getMonth () + 1 ) + "/" + coutDate.getFullYear();
   
   /*var cin = cinDate.getMonth() + "/" + cinDate.getDate() + "/" + cinDate.getFullYear ();*/
   
   var obj = document.getElementById('checkOutDate');

   var xlocation = getOffsetLeft ( document.getElementById('CoutCal') );
   var ylocation = getOffsetTop  ( document.getElementById('CoutCal') );
   ylocation += 125;
   xlocation += 22;
   if ( document.body.scrollLeft > 0 )
   		xlocation -= document.body.scrollLeft + 8;
   if ( document.body.scrollTop > 0 )
   		ylocation -= document.body.scrollTop + 0;      
   if (	chInDate == null || chInDate == "" )
   {
   		alert ( "Please select the check in date first" );
   		return;
   } 
   else if ( chOutDate == null || chOutDate == "" )
   {
        var controlStr ="AppendOrReplace=Replace;AppendChar=';';CloseOnSelect=Yes;ReturnData=Date;Title=Calendar;InlineX=500;InlineY=270;CurrentDate="+chInDate +";SelectAfter="+coutStr +";SelectBefore=\"\";AllowWeekends=Yes;Resizable=No;CallFunction= setNoOfNights();PopupX="+xlocation+";PopupY="+ylocation+";Nav=Yes;SmartNav=Yes;Fix=Yes;";
  	    //Calendar.js
	  //  show_calendar('Search.checkOutDate',null,null, null ,null, controlStr);
   }
   else
   {
        var controlStr ="AppendOrReplace=Replace;AppendChar=';';CloseOnSelect=Yes;ReturnData=Date;Title=Calendar;InlineX=500;InlineY=270;CurrentDate="+chOutDate +";SelectAfter="+coutStr +";SelectBefore=\"\";AllowWeekends=Yes;Resizable=No;CallFunction= setNoOfNights();PopupX="+xlocation+";PopupY="+ylocation+";Nav=Yes;SmartNav=Yes;Fix=Yes;";
  	    //Calendar.js
	   // show_calendar('Search.checkOutDate',null,null, null ,null, controlStr);
   }	
}


function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}

function checkDateFormatandUpdate(datefield)
{
	if (checkDateFormat(datefield))
	{
		updateNumNights();
	}
	else
	{
		return false
	}
}



function updateNumNights()
{
	var strcheckindate=document.Search.checkInDate.value;
	var strcheckoutdate=document.Search.checkOutDate.value;
		var checkindate=new Date()
	var checkoutdate=new Date()
	var strArr = strcheckindate.split('/');
	var dd = strArr[0];
	var mm = strArr[1];
	var yyyy = strArr[2];
	checkindate= new Date( mm + '/' + dd + '/' + yyyy);
	
	strArr =strcheckoutdate.split('/');
	dd = strArr[0];
	mm = strArr[1];
	yyyy = strArr[2];
	checkoutdate= new Date( mm + '/' + dd + '/' + yyyy);
	var diffindays=days_between(checkindate,checkoutdate);
	document.Search.selNoOfNights.value = diffindays;

}



function setCheckOutDate ()
{
   var dd;
   var mm;
   var yy;
   var cInDate = Search.checkInDate.value;   
	
   var noOfNights = document.Search.selNoOfNights.value;	  
   
   if ( noOfNights  == null || noOfNights == "" )
   {
   		noOfNights = "01";	 
   		document.Search.selNoOfNights.value = noOfNights;   		
   }
   
   var nights = parseInt( noOfNights );
   
   if ( cInDate == null || cInDate == "" )
   {
   		return;
   }
   
   //alert(pickupDate);
   mm = cInDate .split("/")[1];
   dd = cInDate .split("/")[0];
   yy = cInDate .split("/")[2];

   var objCheckOutDate = new Date( ( yy ) , ( mm -1 ), ( dd ) );   
   
   objCheckOutDate = new Date( objCheckOutDate .getTime() + nights *24*3600 *1000);
 
   var mm = ( objCheckOutDate.getMonth() + 1 ) < 10 ? "0" + ( objCheckOutDate.getMonth() + 1 ) : ( objCheckOutDate.getMonth() + 1 ) ;
   var dd = ( objCheckOutDate.getDate() )  < 10 ? "0" + ( objCheckOutDate.getDate() ) : ( objCheckOutDate.getDate() ) ;   
	
   document.Search.checkOutDate.value= dd + "/" + mm + "/" +  objCheckOutDate.getFullYear();
 //document.Search.checkOutDate.value="21/07" +  "/" +  objCheckOutDate.getFullYear();
 
 //var checkin=new Date(document.Search.checkInDate.value);
 //var checkout=new Date(document.Search.checkOutDate.value);
 //alert (Math.ceil((checkout.getTime()-checkin.getTime())/(1000*60*60*24)));
//document.Search.selNoOfNights.value=daydiff( checkout-checkin);
//setNoOfNights ();
}

function setNoOfNights ()
{
	var chInDate = Search.checkInDate.value;
	var chOutDate = Search.checkOutDate.value;
	
	var cin = new Date ( 
				chInDate.split("/")[2] ,  //yyyy
				chInDate.split("/")[1]-1 ,//mm 
				chInDate.split("/")[0] ); //dd
	var cout = new Date ( 
				chOutDate .split("/")[2] , 
				chOutDate .split("/")[1]-1 , 
				chOutDate .split("/")[0] );
	
	var diff = ( cout.getTime () - cin.getTime () ) / ( 1000*3600*24 );
	var diff_int = 	parseInt ( diff );

	if ( diff_int <= 0 )
	{
		alert ( "Invalid Check out date selected. Please select a date at least greater one date than Check in date." );
		showCheckOutCalendar ();
	}
	else 
	{
		document.Search.selNoOfNights.value = ( diff_int < 10 ) ? "0" + ( diff_int ) : ( diff_int ) ;
	}
}

function setCheckoutDateByNights ()
{
	setCheckOutDate();
}


function validateDateChIn() 
{
	var cin = document.Search.checkInDate.value;

	if ( cin == null || cin == "" )
	{		
		return false;		
	}
	else
	{
		return true;
	}
}

function validateDateChOut() 
{
	var cout = document.Search.checkInDate.value;	
	
	if ( cout == null || cout == "" )
	{		
		return false;		
	}
	else
	{
		return true;
	}
}

function getSelectedRadio(buttonGroup) 
{
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) 
   { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) 
      {
         if (buttonGroup[i].checked) 
         {
            return i
         }
      }
   } else 
   {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) 
{
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) 
   {
      return "";
   } else {
      if (buttonGroup[i]) 
      { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else 
      { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
}

var _tpNS = (document.all)?false:true;
function getOffsetLeft (el) {
  var ol = el.offsetLeft;
  while ((el = el.offsetParent) != null)
    ol += el.offsetLeft;
  
  return ol;
}
function getOffsetTop (el) {
  var ot = el.offsetTop;
  while((el = el.offsetParent) != null)
   ot += el.offsetTop;
   
  return ot;
}


function checkDateFormat(c_dateValue)
{ 
	var dtDate,dd,mm,yyyy;
    
    //Allow users to put in dates in format 21mar
  
  
   	if(c_dateValue.value.length==4)
   	{
   		c_dateValue.value= "0" + c_dateValue.value;
   	}
   
    if (c_dateValue.value.length==5)
	{
	   
	    var months = new Array()
	    months["JAN"] =1;
		months["FEB"] = 2;
		months["MAR"] = 3;
	    months["APR"] = 4;
	    months["MAY"] = 5;
	    months["JUN"] = 6;
	    months["JUL"] = 7;
	    months["AUG"] = 8;
	    months["SEP"] = 9;
	    months["OCT"] = 10;
	    months["NOV"] = 11;
	    months["DEC"] = 12	;
	    	    
	   	dd=c_dateValue.value.substring(0,2);
	    var month= c_dateValue.value.substring(2,5).toUpperCase();
	    mm=months[month]
	    var today = new Date();
		
		var today_dd, today_mm, today_yyyy;
		today_dd = today.getDate();
		today_mm = today.getMonth() +1;
		today_yyyy = today.getFullYear();
		dtDate = new Date(mm + '/' +  dd + '/' + today.getFullYear());
	     if (dtDate<today)
	    {
	    dtDate = new Date(mm  + '/' + dd + '/' + (today.getFullYear()+1));
	    }
	    yyyy=dtDate.getFullYear();
	    c_dateValue.value=(dd  + "/" + mm + "/" + yyyy);
    }
    else
    {
  	    var today = new Date();
		today_dd = today.getDate();
		today_mm = today.getMonth() +1;
		today_yyyy = today.getFullYear();
		var strArr = c_dateValue.value.split('/');
	    dd = strArr[0];
	    mm = strArr[1];
	    yyyy = strArr[2];
	    var c_dd, c_mm, c_yyyy;
		dtDate = new Date( mm + '/' + dd + '/' + yyyy);
	  }
    
    //mm = parseInt(c_mm.value);
    //dd = parseInt(c_dd.value);
    //yyyy = parseInt(c_yyyy.value);
   
 
	
	if (isNaN(dd))
     {
      c_dateValue.value="";   
      return false;

     }
     else if (dd<1 || dd>31)
     {
        c_dateValue.value="";       
        return false ;

     }
     else if (isNaN(mm))
     {
        c_dateValue.value="";       
	    return false ;
      }
      else if (mm<1 || mm>12)
      {
         c_dateValue.value="";       
         return false ;

      } else if ( mm == 2) 
	  {
		if ( yyyy % 4 > 0 && dd > 28 )
        {            
        	c_dateValue.value="";       
            return false ;
        }
        else if (dd > 29)
        {
        	c_dateValue.value="";       
            return false ;
        }
		else
		{
			if (compareDates( dd,mm,yyyy,today_dd,today_mm,today_yyyy ))
				{
					return true;
				}
				else
				{
					c_dateValue.value=""; 
					return false;
				}			
		}
      }
      else if (isNaN(yyyy))
      {
         c_dateValue.value=""; 
      
      }
      else if (yyyy<2000 || yyyy>3000)
      {
       
          c_dateValue.value="";       
          return false ;

      } else 
	  {         
           dtDate.setFullYear( yyyy, mm - 1, dd);		   
           if (( dtDate.getFullYear() == yyyy) && ( (dtDate.getMonth() + 1) == mm) && ( dtDate.getDate()== dd) ) 
		   {        
			 	if (compareDates( dd,mm,yyyy,today_dd,today_mm,today_yyyy ))
				{ 
					return true;
				}
				else
				{
				  	 c_dateValue.value=""; 
					 return false;
				}				
           } 
		   else 
		   {
		    c_dateValue.value="";       
            return false ;
           }

    }
}


function compareDates( dd,mm,yyyy,today_dd,today_mm,today_yyyy)
{
	 
	if (yyyy < today_yyyy)
	{		       
		return false;
	}
	else if(yyyy == today_yyyy)
	{
	  if ( mm < today_mm)
	 {		
		return false;		
	 }
	 else if( mm == today_mm )
	      {
		    if ( dd < today_dd)
	        {	
		     return false;
	        }
			else
			{
			 return true;
			}
		  
		  }
		  else
		  {
		   return true;
		  }
	}
	else{
	  return true;
	}

}
