$(function(){
	var getCityLists = $('#getCityLists');						
	var getCity = $('input[name="getCity"]');					
	var getCity2 = $('input[name="getCity2"]');
	var getCity3 = $('input[name="getCity3"]');
	var getCity4 = $('input[name="getCity4"]');
	var getCity5 = $('input[name="getCity5"]');
	var getCity6 = $('input[name="getCity6"]');

	
	var cityshutx = $('#cityshutx');	
	var getCityListsSH = false;									
	
	
	getCity.click(function(){
		freeivaYDK.cites.show(this, getCityLists);
		$(this).focus();
	});
	
	getCity2.click(function(){
		freeivaYDK.cites.show(this, getCityLists);
		$(this).focus();
	});
	
	getCity3.click(function(){
		freeivaYDK.cites.show(this, getCityLists);
		$(this).focus();
	});
	
	getCity4.click(function(){
		freeivaYDK.cites.show(this, getCityLists);
		$(this).focus();
	});
	
	getCity5.click(function(){
		freeivaYDK.cites.show(this, getCityLists);
		$(this).focus();
	});
	
	getCity6.click(function(){
		freeivaYDK.cites.show(this, getCityLists);
		$(this).focus();
	});
	

	cityshutx.click(function(){
		freeivaYDK.cites.hide(getCityLists);

	});
	
	
	//select city
	$('.city-list li span').hover(function(){$(this).addClass('current_city');}, function(){$(this).removeClass('current_city');}).click(function(){
		var type = freeivaYDK.cites._cs == 'getCity'?'get' :'ret';
		$('input[name="' + freeivaYDK.cites._cs + '"]').val($(this).text());
		
		freeivaYDK.cites.hide(getCityLists);
	});
	
	 getCityLists.mouseover(function(){getCityListsSH = true;}).mouseout(function(){getCityListsSH = false;}); 
	

	var freeivaYDK = {
		version : 1.2,
		cites : {
			show : function(source, target){

				var type = source.name;
				this.position(type, source);
				var selected = false;
				if(!this._cs){
					target.css(this.xyOpts[type]).show();
					this._cs = source.name;
					selected = true;
				}else{
					if(this._cs == source.name){
						target.toggle();
					}else{
						target.css(this.xyOpts[type]).show();
						selected = true;
					}
					this._cs = undefined;
				}
				if(selected) this.select($(source).val(), ['.city-list li span'], 'selected_city');
			},
			hide : function(target){
				target.hide();
				this._cs = undefined;
			},
			select : function(value, target, className){
				$(target[0] + '.' + className + ',' + target[1] + '.' + className).removeClass(className);
				$(target[0] + '[rel="' + value + '"]' + ', ' + target[1] + '[rel="' + value + '"]').addClass(className);
				$(target[0] + '.' + 'current_city' + ',' + target[1] + '.' + 'current_city').removeClass('current_city');
			},
			reset : function(fbID){
				this.changeCat($('#' + fbID)[0]);
			},
			xyOpts : {},
			position: function(type, source){
				var xy = $(source).position();
				if(!this.xyOpts[type]){
					this.xyOpts[type] = {
						left : xy.left  + 'px',
						top : xy.top + 20 + 'px',
						position: 'absolute'
					}
				}
			}//,
		}//,
	}

$(document).mousedown(function(event){
		event = window.event || event;
		var target = event.target || event.srcElement;
		if(!getCityListsSH && target != getCity[0]){
			freeivaYDK.cites.hide(getCityLists);
		}
		

	});

});
	


function CompareFlyDate(date1, date2)
{
	if ( date1=="" || date2=="") return -1;
	var dt1 = RebuildDate(date1);
	var dt2 = RebuildDate(date2);
	
	var dt1_array = dt1.split('-');
	var dt2_array = dt2.split('-');
  
    dt1 = dt1_array[2] + '-' + dt1_array[0] + '-' + dt1_array[1];
    dt2 = dt2_array[2] + '-' + dt2_array[0] + '-' + dt2_array[1];
    
	if ( dt1 > dt2 ) return (1);
	else if ( dt1 == dt2 ) return (0);
	else return (-1);
}

function RebuildDate(val)
{
  var date_array="";
  var get_input_date_str = val;
  var seperate;

  if(get_input_date_str == "")
  {
		return true;
  }
  
  if (get_input_date_str.indexOf("-") != -1)
  {
      seperate = "-";
  }
  else if (get_input_date_str.indexOf("-") != -1)
  {
      seperate = "-";
  }
  else if (get_input_date_str.indexOf(".") != -1)
  {
      seperate = ".";
  }
  date_array = get_input_date_str.split(seperate);

  if(date_array.length != 3)
  {
		return ""; 
  }
  if(date_array[2].length < 4) //mmddyyyy
  {
		return ""; 
  }
  
  date_array[0] = date_array[0] - 1;
  var get_input_date = new Date(date_array[2], date_array[0], date_array[1]);
  var dt = DateToStr(get_input_date);
  return dt;
 }

 function DateToStr(get_input_date)
{
  var get_input_year = get_input_date.getFullYear();
  var get_input_month = get_input_date.getMonth() + 1;
  var get_input_day = get_input_date.getDate();

  if(get_input_year<1000)
  {
		get_input_year=1900 + get_input_year;
  }
  if(get_input_month<10)
  {
		get_input_month="0" + get_input_month;
  }
  if(get_input_day<10)
  {
		get_input_day="0" + get_input_day;
  }
  var dt = get_input_month + "-" + get_input_day  + "-" + get_input_year;
  return dt;
}
function ADCity(ADType){
	//alert($(ADnum).html());
	if(ADType=="add"){
				$("#city_t").css("display","block");
				$("#add_city_link").css("display","none");
		}else if(ADType=="dele"){
				$("#city_t").css("display","none");
				$("#add_city_link").css("display","");
		}
		else{
				$("#city_t").css("display","none");
				$("#add_city_link").css("display","");
		}
	
	}

function Check_City_Time(Cnum){
						Cnum=="1"?Cnum="":Cnum=Cnum;
						
						var start_date = $("#start_date"+Cnum);
						var start_time = $("#start_time"+Cnum);
						var tip_time = $("#tip_time"+Cnum);
						var end_date = $("#end_date"+Cnum);
						var end_time = $("#end_time"+Cnum);
						var tip_timee = $("#tip_timee"+Cnum);
						
						
						
						if(start_date.attr("value")==""){
									tip_time.html(" * Select Start Date");
									tip_time.show();
									start_date.focus();
									return false;
									}else{
										tip_time.hide();	
										}
						if (start_time.attr("value")=="0") {
										tip_time.html(" * Select Start Time");
										tip_time.show();
										start_time.focus();
										return false;
										}else{
										tip_time.hide();	
									}	
						
						if(end_date.attr("value")==""){
									tip_timee.html(" * Select Start Date");
									tip_timee.show();
									end_date.focus();
									return false;
									}else{
										tip_timee.hide();	
										}
						if (end_time.attr("value")=="0") {
										tip_timee.html(" * Select Start Time");
										tip_timee.show();
										end_time.focus();
										return false;
										}else{
										tip_timee.hide();	
									}
						if(start_date.attr("value")!=""&& end_date.attr("value")!=""){
								
							if (CompareFlyDate(start_date.attr("value"),end_date.attr("value"))>0) { 
										tip_timee.show();
										tip_timee.css("display","block");
										tip_timee.html(" * The departure date of city "+ Cnum +" cannot be later than the limit date!");
										//alert('The departure date of city 1 cannot be later than the limit date!');
										end_date.focus();
										return false;
									}
						}

	}

function check_index_info(){
			
				 if($("#getCity").attr("value")==""){
					 $("#tip_city_name").html("Enter Destination");
			
					 $("#getCity").focus();
					 return false;
					 }else{
						 $("#tip_city_name").hide();
						 }

			
			if(Check_City_Time(1)==false) return false;
			
				
				
				if ($("#duration").attr("value")=="0") {
						$("#tip_duration").html(" * Enter Duration");
						$("#tip_duration").show();
						$("#duration").focus();
						return false;
						}else{
							$("#tip_duration").hide();	
							}
							
				if ( $("#b1").attr("checked")==false && $("#b2").attr("checked")==false && $("#b3").attr("checked")==false && $("#b4").attr("checked")==false && $("#b5").attr("checked")==false && $("#b6").attr("checked")==false && $("#b7").attr("checked")==false && $("#b8").attr("checked")==false && $("#b9").attr("checked")==false && $("#b10").attr("checked")==false && $("#b11").attr("checked")==false  ) {
					
					$("#tip_requests").html(" * Select Service Category");
					$("#tip_requests").show();
					$("#b1").focus() 
					return false;
				}else{
					$("#tip_requests").hide();	
					}

	}
function cs_form_info(){
			if($("#getCity").attr("type")!="hidden"){
					 if($("#getCity").attr("value")==""){
							 $("#tip_city_name").html("Enter Destination");
			
							 $("#getCity").focus();
							 return false;
							 }else{
								 $("#tip_city_name").hide();
							}
			
				if(Check_City_Time(1)==false) return false;
				
			
				//multiplecity start
				
				if($("#multiplecity").attr("checked")==true){
					
					if($("#getCity2").attr("value")==""){
							 $("#tip_city_name2").html("Enter Destination City 2");
			
							 $("#getCity2").focus();
							 return false;
							 }else{
								 $("#tip_city_name2").hide();
							}
						
						if(Check_City_Time(2)==false) return false;
						
						//city 3
						if($("#getCity3").attr("value")!=""){
							if(Check_City_Time(3)==false) return false;
							}else{
								$("#tip_time3").hide();
								$("#tip_timee3").hide();
								}

					
						//city 4
						if($("#getCity4").attr("value")!=""){
							$("#city_t").css("display","block");
							if(Check_City_Time(4)==false) return false;
							}else{
								$("#city_t").css("display","none");
								$("#tip_time4").hide();
								$("#tip_timee4").hide();
								}

						
						//city 5
						if($("#getCity5").attr("value")!=""){
							$("#city_t").css("display","block");
							if(Check_City_Time(5)==false) return false;
							}else{
								$("#tip_time5").hide();
								$("#tip_timee5").hide();
								}
		
						
						//city 6
						if($("#getCity6").attr("value")!=""){
							$("#city_t").css("display","block");
							if(Check_City_Time(6)==false) return false;
							}else{
								$("#tip_time6").hide();
								$("#tip_timee6").hide();
								}
				
					
					}
				// multiplecity end
				
				if ($("#duration").attr("value")=="0") {
								$("#tip_duration").html(" * Enter Duration");
								$("#tip_duration").show();
								$("#duration").focus();
								return false;
								}else{
									$("#tip_duration").hide();	
									}
									
						if ( $("#b1").attr("checked")==false && $("#b2").attr("checked")==false && $("#b3").attr("checked")==false && $("#b4").attr("checked")==false && $("#b5").attr("checked")==false && $("#b6").attr("checked")==false && $("#b7").attr("checked")==false && $("#b8").attr("checked")==false && $("#b9").attr("checked")==false && $("#b10").attr("checked")==false && $("#b11").attr("checked")==false ) {
							
							$("#tip_requests").html(" * Select Service Category");
							$("#tip_requests").show();
							$("#b1").focus() 
							return false;
						}else{
							$("#tip_requests").hide();	
							}
			
					}
	
				if ($("#group_size").attr("value")=="0") {
						$("#tip_group_size").html(" * Select your group size!");
						$("#tip_group_size").show();
						$("#group_size").focus();
						return false;
						}else{
							$("#tip_group_size").hide();	
							}
		
		
				if ($("#bidding").attr("checked")=="" && $("#bidding2").attr("checked")=="") {
						$("#tip_bidding").html(" * Please choose Additional Comment!");
						$("#tip_bidding").show();
						$("#bidding").focus();
						return false;
						}else{
							$("#tip_bidding").hide();	
							}
							
				if ($("#cs_check").attr("checked")=="") {
						$("#tip_cs_check").html(" * Please choose to agree to the Term Conditions!");
						$("#tip_cs_check").show();
						$("#cs_check").focus();
						return false;
						}else{
							$("#tip_cs_check").hide();	
							}
					
	}