jQuery(function(){
	CascadeOptionalFilterign();
	var settings = {
            tl: { radius: 5 }, tr: { radius: 5 }, bl: { radius: 5 }, br: { radius: 5 }, antiAlias: true
        };
       

		     var NumOfChildren =0;
		     var NumOfChildren2 =0;
		     var airporterrorset = false;
		     var dateerrorset = false;	
		     var childrenerrorset = false;
		     var fltairporterrorset = false;
		     var fltdeparterrorset = false;
		     var fltreturnerrorset = false;
		     var fltcomparedateserrorset = false;
		     var fltchildrenerrorset = false;
			 var date = new Date();
			 var airportnamecorrect = false;
			 var htlchildrenerrorset = false;
			 date.setDate(date.getDate()+1);  					 
			
			 $("#holidaysubmit").click(function(){	
				 
				 if(ValidationDate($('#Holiday_DepartDate').val()) && ValidationAirport($("#Holiday_From").val()) && ValidationChildren())
				 {
					 if(dateerrorset){$("#Holiday_DateError").remove();dateerrorset = false;}
					 if(airporterrorset){$("#Holiday_FromAirportError").remove();airporterrorset = false;}
					 return true;
				 }
				 else
				 {
					 if(!ValidationDate($('#Holiday_DepartDate').val()))
					 {
						 if(!dateerrorset){$("#Holiday_DepartDate").after('<label id = "Holiday_DateError" generated="true" class="error">Please enter a correct date in dd/mm/yyyy format.</label>');dateerrorset = true;}
					 }
					 else
					 {
						 if(dateerrorset){$("#Holiday_DateError").remove();dateerrorset = false;}
					 }
					 
					 if(!ValidationAirport($("#Holiday_From").val()))
					 {
						 if(!airporterrorset){$("#Holiday_From").after('<label id = "Holiday_FromAirportError" generated="true" class="error">Please Select a valid airport.</label>');airporterrorset = true;}
					 }
					 else
					 {
						 if(airporterrorset){$("#Holiday_FromAirportError").remove();airporterrorset = false;}
					 }
					
					if(!ValidationChildren())
					{
						 if(!childrenerrorset){$("#HolidayChildrenText").after('<label id = "Holiday_ChildAgesError" generated="true" class="error">Please Select Proper ages for the children.</label>');childrenerrorset = true;}
					 }
					 else
					 {
						 if(childrenerrorset){$("#Holiday_ChildAgesError").remove();childrenerrorset = false;}
					 }
					 return false;
				 }
			 });
			 /*END HOLIDAY PACKAGE VALIDATION*/
	
			 /*FLIGHT ONLY VALIDATION*/
	           $("#flightonlysubmit").click(function(){	
	        	   
				 if(ValidationDate($('#returndate').val()) && ValidationDate($('#departdate').val()) && ValidationAirport($("#from_port").val()) && ValidationCompareDates($('#departdate').val(),$('#returndate').val()) && ValidationChildren())
				 {
					 if(fltdeparterrorset){$("#flt_DepartError").remove();fltdeparterrorset = false;}
					 if(fltreturnerrorset){$("#flt_ReturnError").remove();fltreturnerrorset = false;}
					 if(fltairporterrorset){$("#flt_AirportError").remove();fltairporterrorset = false;}
					 if(fltcomparedateserrorset){$("#flt_CompareError").remove();fltcomparedateserrorset = false;}
					 return true;
				 }
				 else
				 {
					 if(!ValidationDate($('#departdate').val()))
					 {
						 if(!fltdeparterrorset){$("#departdate").after('<label id = "flt_DepartError" generated="true" class="error">Please enter a correct date in dd/mm/yyyy format.</label>');fltdeparterrorset = true;}
					 }
					 else
					 {
						 if(fltdeparterrorset){$("#flt_DepartError").remove();fltdeparterrorset = false;}
					 }
					 if(!ValidationDate($('#returndate').val()))
					 {
						 if(!fltreturnerrorset){$("#returndate").after('<label id = "flt_ReturnError" generated="true" class="error">Please enter a correct date in dd/mm/yyyy format.</label>');fltreturnerrorset = true;}
					 }
					 else
					 {
						 if(fltreturnerrorset){$("#flt_ReturnError").remove();fltreturnerrorset = false;}
					 }
					 if(!ValidationCompareDates($('#departdate').val(),$('#returndate').val()))
					 {//COMPARE ERROR
						 if(!fltcomparedateserrorset){$("#returndate").after('<label id = "flt_CompareError" generated="true" class="error">Return Date must be later than Depart Date.</label>');fltcomparedateserrorset = true;}
					 }
					 else
					 {
						 if(fltcomparedateserrorset){$("#flt_CompareError").remove();fltcomparedateserrorset = false;}
					 }
					 
					 if(!ValidationAirport($("#from_port").val()))
					 {
						 if(!fltairporterrorset){$("#from_port").after('<label id = "flt_AirportError" generated="true" class="error">Please Select a valid airport.</label>');fltairporterrorset = true;}
					 }
					 else
					 {
						 if(fltairporterrorset){$("#flt_AirportError").remove();fltairporterrorset = false;}
					 }
					 if(!ValidationChildren())
						{
							 if(!fltchildrenerrorset){$("#ChildrenAgesText").after('<label id = "flt_ChildAgesError" generated="true" class="error">Please Select Proper ages for the children.</label>');fltchildrenerrorset = true;}
						 }
						 else
						 {
							 if(fltchildrenerrorset){$("#flt_ChildAgesError").remove();fltchildrenerrorset = false;}
						 }
												 
					 return false;
				 }
			 });			 
			 /*END FLIGHT ONLY VALIDATION*/
	           /*Hotel Only Validation*/
	           $("#hotelonlysubmit").click(function(){	
					 
					 if(ValidationDate($('#checkindate').val()) && ValidationChildren())
					 {
						 if(dateerrorset){$("#hotelonly_DateError").remove();dateerrorset = false;}						 
						 return true;
					 }
					 else
					 {
						 if(!ValidationDate($('#checkindate').val()))
						 {
							 if(!dateerrorset){$("#checkindate").after('<label id = "hotelonly_DateError" generated="true" class="error">Please enter a correct date in dd/mm/yyyy format.</label>');dateerrorset = true;}
						 }
						 else
						 {
							 if(dateerrorset){$("#hotelonly_DateError").remove();dateerrorset = false;}
						 }	
						 
						 if(!ValidationChildren())
							{
								 if(!htlchildrenerrorset){$("#RoomChildrenAgesText").after('<label id = "htl_ChildAgesError" generated="true" class="error">Please Select Proper ages for the children.</label>');htlchildrenerrorset = true;}
							 }
							 else
							 {
								 if(htlchildrenerrorset){$("#htl_ChildAgesError").remove();htlchildrenerrorset = false;}
							 }
							 
						 return false;
					 }
				 });
				 /*END Hotel Only Validation*/
			 
			 		$('#departdate').change(function(){
			 			var departdate = $(this).val();
			 			var departyear = departdate.substring(6,departdate.length);
			 			var departmonth = departdate.substring(3,5);
			 			var departday = departdate.substring(0,2);
			 			var datestring = departmonth + '/'+departday+'/'+departyear;
			 			
			 			var returndate = addDays(new Date(datestring),7);
			 			returndate = addZeros(returndate.getDate())+'/'+addZeros(returndate.getMonth()+1)+'/'+returndate.getFullYear();
			 			$('#returndate').val(returndate);
			 		});	 
			 $('#departdate').datePicker().val(date.asString()).trigger('change');
			$(function() {
					$('#selectable').selectable();
					
				});
			$('#returndate').datePicker().val(date.asString()).trigger('change');
			$(function() {
					$('#selectable').selectable();
					
				});
			$('#checkindate').datePicker().val(date.asString()).trigger('change');
			$(function() {
					$('#selectable').selectable();
					
				});
			
			$('#Holiday_DepartDate').datePicker().val(date.asString()).trigger('change');
			$(function() {
				$('#selectable').selectable();
				
			});
								
										
			 $('#Children').change(function(){
				 $.each('1234567',function(val,text){
					 if((val+1) <= $('#Children').val())
					 {
						 $('#Child'+(val+1)+"Age").show();				 
					 }
					 else
					 {
						 $('#Child'+(val+1)+"Age").hide();
					 }
					 if(($('#Children').val()) >0)
					 {
						 $('#ChildrenAgesText').show();
					 }
					 else
					 {
						 $('#ChildrenAgesText').hide();
					 }
					 
				 });			 
			 });
			 
			 $('.lblHoliday').click(function(){					 
				 $(this).addClass('current');
				 $('.lblFlight').removeClass('current');
				 $('.lblHotel').removeClass('current');
				 $('#searchform_flightonly').hide();
				 $('#searchform_hotelonly').hide();
				 $('#searchform_holiday').show();				
			 });
			 $('.lblFlight').click(function(){					 
				 $(this).addClass('current');
				 $('.lblHoliday').removeClass('current');
				 $('.lblHotel').removeClass('current');
				 $('#searchform_flightonly').show();
				 $('#searchform_hotelonly').hide();
				 $('#searchform_holiday').hide();			
			 });
			 $('.lblHotel').click(function(){					 
				 $(this).addClass('current');
				 $('.lblHoliday').removeClass('current');
				 $('.lblFlight').removeClass('current');
				 $('#searchform_flightonly').hide();
				 $('#searchform_hotelonly').show();
				 $('#searchform_holiday').hide();			
			 });
			 $('#RoomsRequired').change(function(){
				  HideRooms($(this).val());		
				  CheckHotelOnlyChildren();
			 });
			 
			 $.each('1234567',function(val,text){
				 $('#hotelonly_Room'+(val+1)+'Children').change(function(){ //If One Changes then
					 NumOfChildren=0; 
					 //Go Trough all the rooms children selection
					 $.each('1234567',function(val1,text1){
						 if((val1+1)<= $('#RoomsRequired').val())
						 {
						 NumOfChildren += parseInt($('#hotelonly_Room'+(val1+1)+'Children').val());
						 }
					 });
				
					ShowChildren();				
				 });
					 
				 });
			 function CheckHotelOnlyChildren()
			 {			 NumOfChildren =0;
				 //Go Trough all the rooms children selection
				 $.each('1234567',function(val1,text1){
					 if((val1+1)<= $('#RoomsRequired').val())
					 {
					 NumOfChildren += parseInt($('#hotelonly_Room'+(val1+1)+'Children').val());
					 }
				 });
		
				ShowChildren();	
		
			 }
			 function CheckHotelOnlyChildren2()
			 {			 NumOfChildren2 =0;
				 //Go Trough all the rooms children selection
				 $.each('1234567',function(val1,text1){
					 if((val1+1)<= $('#HolidayRoomsRequired').val())
					 {
					 NumOfChildren2 += parseInt($('#holiday_Room'+(val1+1)+'Children').val());
					 }
				 });
		
				ShowChildren2();	
		
			 }
			 $('#HolidayRoomsRequired').change(function(){
				 HideRooms2($('#HolidayRoomsRequired').val());	
				 CheckHotelOnlyChildren2();
			 });
			 $.each('1234567',function(val,text){
				 $('#holiday_Room'+(val+1)+'Children').change(function(){ //If One Changes then
					 NumOfChildren2=0; 
					 //Go Trough all the rooms children selection
					 $.each('1234567',function(val1,text1){
						 if((val1+1)<= $('#HolidayRoomsRequired').val())
						 {
						 NumOfChildren2 += parseInt($('#holiday_Room'+(val1+1)+'Children').val());
						 }
					 });
					ShowChildren2();				
				 });
					 
				 });
			 
			
			 /*RESET SOME FORM ITEMS*/
			 $('#Children').val('0');
			 $('#Flexibility').val('These dates only');
			 $('#from_port').val(-1);
			 $('#RoomsRequired').val(1);
			 $('#Duration').val(7);
			 $('#holiday_Room1Adults').val(2);
			 $('#Holiday_Flexibility').val('+/- 1 day');			 
			 $('#ChildrenAgesText').hide();
			 $('#RoomChildrenAgesText').hide();
			 $('#searchform_flightonly').hide();
			 $('#searchform_hotelonly').show();
			 $('#searchform_holiday').hide();		
			 $('#HolidayChildrenText').hide();	
			 $('#Adults').val('2');
			 $('#hotelonly_Room1Adults').val('2');
			 HideRooms(1);
			 HideRooms2(1);
			// $('#Room1').hide();
			 /*RESET SOME FORM ITEMS*/	
			 
			 /*FUNCTIONS*/
			 function ShowChildren()
			 {				 
				 $.each('1234567',function(val,text){
					 if((val+1) <= NumOfChildren)
					 {
						 $('#RoomsChild'+(val+1)+"Age").show();				 
					 }
					 else
					 {
						 $('#RoomsChild'+(val+1)+"Age").hide();
					 }
					 if(NumOfChildren >0)
					 {
						 $('#RoomChildrenAgesText').show();
					 }
					 else
					 {
						 $('#RoomChildrenAgesText').hide();
					 }
					 
				 });		
			 }
			 function ShowChildren2()
			 {				 
				 $.each('1234567',function(val,text){
					 
					 if(NumOfChildren2 >0)
					 {
						 $('#HolidayChildrenText').show();
					 }
					 else
					 {
						 $('#HolidayChildrenText').hide();
					 }
					 if((val+1) <= NumOfChildren2)
					 {
						 
						 $('#HolidayRoomsChild'+(val+1)+'Age').show();				 
					 }
					 else
					 {
						 $('#HolidayRoomsChild'+(val+1)+'Age').hide();
					 }
					 
				 });		
			 }
			 function HideRooms(NumOfRooms)
			 {
				 $.each('1234567',function(val,text){
					 if(NumOfRooms >0)
					 {
						 $('#Roomoccupantstext1').show();
						 $('#Roomoccupantstext2').show();
					 }
					 else
					 {
						 $('#Roomoccupantstext1').hide();
						 $('#Roomoccupantstext2').hide();
					 }
					 if((val+1) <=NumOfRooms)
					 {
						 $('#Room'+(val+1)).show();
					 }
					 else
					 {
						 $('#Room'+(val+1)).hide(); 
					 }
				 });
			 }
			 function HideRooms2(NumOfRooms)
			 {
				 $.each('1234567',function(val,text){
					 if(NumOfRooms >0)
					 {
						 $('#HolidayRoomChildrenAgesText').show();					
					 }
					 else
					 {
						 $('#HolidayRoomChildrenAgesText').hide();						 
					 }
					 if((val+1) <=NumOfRooms)
					 {
						 $('#HolidayRoom'+(val+1)).show();
					 }
					 else
					 {
						 $('#HolidayRoom'+(val+1)).hide(); 
					 }	 
			 });
			 }			
						

			 
		 }); // end document ready

function validateDate( strValue ) {
	/************************************************
	DESCRIPTION: Validates that a string contains only
	    valid dates with 2 digit month, 2 digit day,
	    4 digit year. Date separator can be ., -, or /.
	    Uses combination of regular expressions and
	    string parsing to validate date.
	    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

	PARAMETERS:
	   strValue - String to be tested for validity

	RETURNS:
	   True if valid, otherwise false.

	REMARKS:
	   Avoids some of the limitations of the Date.parse()
	   method such as the date separator character.
	*************************************************/
	  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	 
	  //check to see if in correct format
	  if(!objRegExp.test(strValue))
	    return false; //doesn't match pattern, bad date
	  else{
	    var strSeparator = strValue.substring(2,3) 
	    var arrayDate = strValue.split(strSeparator); 
	    //create a lookup for months not equal to Feb.
	    var arrayLookup = { '01' : 31,'03' : 31, 
	                        '04' : 30,'05' : 31,
	                        '06' : 30,'07' : 31,
	                        '08' : 31,'09' : 30,
	                        '10' : 31,'11' : 30,'12' : 31}
	    var intDay = parseInt(arrayDate[0],10); 

	    //check if month value and day value agree
	    if(arrayLookup[arrayDate[1]] != null) {
	      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)
	        return true; //found in lookup table, good date
	    }
	    
	    //check for February (bugfix 20050322)
	    //bugfix  for parseInt kevin
	    //bugfix  biss year  O.Jp Voutat
	    var intMonth = parseInt(arrayDate[1],10);
	    if (intMonth == 2) { 
	       var intYear = parseInt(arrayDate[2]);
	       if (intDay > 0 && intDay < 29) {
	           return true;
	       }
	       else if (intDay == 29) {
	         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
	             (intYear % 400 == 0)) {
	              // year div by 4 and ((not div by 100) or div by 400) ->ok
	             return true;
	         }   
	       }
	    }
	  }  
	  return false; //any other values, bad date
	}


function ValidationDate(myDate)
{
	var datecorrect = false;
	 if(myDate.length >0){
		 
		 datecorrect = validateDate(myDate);
		 
	 }			 
		 return datecorrect;
}

function ValidationCompareDates(myDate1,myDate2)
{
	var valid = false;
	var DepDate = myDate1.split("/");
	var RetDate = myDate2.split("/");
	var date1 = new Date(DepDate[2], DepDate[1]-1, DepDate[0]);
	var date2 = new Date(RetDate[2], RetDate[1]-1, RetDate[0]);
	var date1Comp = date1.getTime(); // milliseconds
	var date2Comp = date2.getTime();
	if (date1Comp < date2Comp)
	{

		valid = true;
	}
	return valid;
}

function ValidationAirport(myAirport)
{
	var airportcorrect = false;
	 if(myAirport != -1){
		 
		 airportcorrect = true;
		 
	 }			 
		 return airportcorrect;
}
function ValidationChildren()
{
	var childcorrect;
	var Counter =0;
	$('.ChildSelect').each(function(){     
	if($(this).val() == -1){
		
		if($(this).is(':visible'))
		{
		childcorrect = false;
		}
		else
		{
			if(childcorrect!=false)
			{
			childcorrect = true;
			}
		}
	 }
	else
	{
		if(Counter == 0)
		{
			if(childcorrect!=false)
			{
			childcorrect = true;
			}
		}
	}
	Counter++;
	});
		 return childcorrect;
}



function addDays(myDate,days)
{
	return new Date(myDate.getTime() + days*24*60*60*1000);
}

function addZeros(num)
{
	num = num+'';

if(num.length <2)
{
	num = '0'+num;
}
return num;
	
}


function CascadeOptionalFilterign()
{
$('#op_HotelOnly').click(function(){
	
	$('#OptionalFilteringli_HotelOnly').toggle();
});	

$('#op_Packages').click(function(){
	
	$('#OptionalFilteringli_Packages').toggle();
});	
}

