var serverDate=jQuery('#serverDate').get(0).title;
jQuery(document).ready(function(){
	jQuery("#owDepartDateTime").datepicker({
		defaultDate: Date.parseExact(serverDate, 'd/M/yyyy'),
		dateFormat:'dd/mm/yy',
		minDate: Date.parseExact(serverDate, 'd/M/yyyy'),
		numberOfMonths: 2,
		buttonImage: '/images/calender.jpg',
        buttonImageOnly:true,
        buttonText: "Calendar",
        showOn: 'both',
		showOptions: {direction: 'down'}
	});
	
    jQuery("#owsupplierSelAll").click(function(){
        jQuery('table[id="owsupplierList"] input').each(function(){
        	jQuery(this).attr('checked', true);        
        });        
    });
    
    jQuery("#owsupplierUnSelAll").click(function(){    	
        jQuery('table[id="owsupplierList"] input').each(function(){
        	jQuery(this).attr('checked', false);        
        });        
    });
});


var date="NULL";
                
                // function for validating form feild
                
function checkalloneway() {	 

	if (jQuery('#js.token')!= null)
		jQuery('#js.token').value = new Date().getSeconds();
	
	// orign-destination validation
	document.getElementById('placeerror1').style.display = "none";
	document.getElementById('originnull').style.display = "none";
	document.getElementById('destinationnull').style.display = "none";
	jQuery('#PreferredAirlinesErr1').get(0).style.display = "none";
	jQuery('#selectorgdeserror1').get(0).style.display = "none";
	
	if(jQuery("#destinationerror").css("display","inline")) {
		jQuery("#destinationerror").css("display","none");
	}
	
	setOriginDestinationId();
	
	var origin1=document.getElementById('owOrigin').value;
	var destination1=document.getElementById('owDestination').value;
	if(!validateDestination(origin1,destination1,"owDestination")) {
		clear("owDestination");
	}
	destination1=document.getElementById('owDestination').value;
	if((origin1 == 'NA' && destination1== 'NA') || (origin1 == '' && destination1== '')) {
		jQuery('#selectorgdeserror1').get(0).style.display = "inline";
		return false;
	} else if( origin1 == destination1 ) {
		document.getElementById('placeerror1').style.display = "inline";
		return false;
	}  
	
	if(!travelType) {
		if(fetchCityCode(origin1)==''){
			document.getElementById('originnull').style.display = "inline";
			return false;
		}else if(fetchCityCode(destination1)=='') {
			document.getElementById('destinationnull').style.display = "inline";
			return false;
		}
	}
	
	if(origin1 == 'NA') {
		document.getElementById('originnull').style.display = "inline";
		return false;
	}

	if(destination1 == 'NA') {
		document.getElementById('destinationnull').style.display = "inline";
		return false;
	} else {
       	document.getElementById('destinationnull').style.display = "none";
    }
	
	//     For checking depart date relative to todays date
	document.getElementById('dateerror').style.display = "none";
	var departDate=document.getElementById('owDepartDateTime').value;
	if(departDate=='') {
		document.getElementById('dateerror1').style.display = "inline";
		return false;
	}
	
	//This is for old code now it's handle date picker
	var departDateparse = Date.parseExact(departDate,'d/M/yyyy');
	var todaydate1=Date.parseExact(serverDate,'d/M/yyyy');
	var compareresult1=todaydate1.compareTo(departDateparse);
	if(compareresult1==1){
		document.getElementById('dateerror1').style.display = "inline";
		return false;
	} 
	
	
	//  For checkinf no of passenger
	document.getElementById('passengererror1').style.display = "none";
	document.getElementById('infaintserror1').style.display = "none";
	var adults=document.getElementById('adult1').value;
	var minors=document.getElementById('minors1').value;
	var infaints=document.getElementById('infaints1').value;
	var count=1;

	var adultsow=Number(adults);
	var minorsow=Number(minors);
	var infaintsow=Number(infaints) 
	var total=adultsow+minorsow;
	
	if(total==0 || total>9){
		document.getElementById('passengererror1').style.display = "inline";
		count=0;
	}
	
	if(infaintsow > adultsow){
		document.getElementById('infaintserror1').style.display = "inline";
		count=0;
	}
	
	if(count!=1){
		return false;
	}	 
	
	var isAirline = false; 
	
	jQuery('select[id^="owairline"]').each(function(value){
		if(jQuery("select#"+this.id+" option:selected").val()=="00"){
			var selectedAirline=jQuery("select#"+this.id+" option:selected").text();
			if(selectedAirline.search("Select Airline") < 0){
				isAirline = true;
				return false;
			}
		}
	});
	
	if(isAirline){
		document.getElementById('PreferredAirlinesErr1').style.display = "inline";
		return false;
	}
	
	return true;
}

  function searchdays(link){
	  jQuery('#searchdaysloader').get(0).style.display ="inline";
	  jQuery.ajax({ type: "GET",
	    url: link,
	    success: function(response){		  
		  jQuery('#searchdaysloader').get(0).style.display ="none";		 
		  jQuery('#searchbydays').html(response);		      
	      return response;
	    },
	    error: function(e){ 
	    	jQuery('#searchdaysloader').get(0).style.display ="none";
	    	jQuery('#searchbydays').html("An error occurred, with response: " +response);	       
	       return response;
	    }
	    
	  });
	  
	  
  }
 
  
  function onewayNoRestrictionhideCheckBox(){
		if(jQuery("#ckbnorestriction").get(0).checked){
			jQuery("#ckbdirectflights").get(0).checked = false;
			jQuery("#ckbdirectflights").get(0).disabled= true;
			jQuery("#ckbnorefund").get(0).checked = false;
			jQuery("#ckbnorefund").get(0).disabled= true;
			jQuery("#ckbnoticket").get(0).checked = false;
			jQuery("#ckbnoticket").get(0).disabled= true;
			
			}
			else{
				jQuery("#ckbdirectflights").get(0).disabled= false;
				jQuery("#ckbnorefund").get(0).disabled= false;
				jQuery("#ckbnoticket").get(0).disabled= false;
			}	
		}
  
  function changeChildAndInfant(adultid,childid,infantid){
		var adult= Number(jQuery("#"+adultid).val()),childoptions="",infantoptions="";
		
		var childnum=(9-adult);
		for(var i=0 ; i<=childnum;i++){
			childoptions += '<option value="'+i+'">'+i+'</option>';
		}
		
		for(var i=0 ; i<=adult;i++){	
		infantoptions += '<option value="'+i+'">'+i+'</option>';
		}
		
		jQuery("select#"+childid).html(childoptions);
		jQuery("select#"+infantid).html(infantoptions);	
		
	}  
  

