function setDates() {
  now = new Date();
  dep = new Date();
  ret = new Date(); 
  dep.setTime(now.getTime()+60000*60*24*7);
  ret.setTime(now.getTime()+60000*60*24*14);
  var m1 = dep.getMonth()+1;      
  var d1   = dep.getDate();  
  var m2 = ret.getMonth()+1;   
  var d2   = ret.getDate();
  var y1 = dep.getYear();
  var y2 = ret.getYear();
  m1 = m1 < 10 ? '0' + m1 : m1;
  m2 = m2 < 10 ? '0' + m2 : m2;
  d1 = d1 < 10 ? '0' + d1 : d1;
  d2 = d2 < 10 ? '0' + d2 : d2;
  y1 = y1 < 1000 ? y1 + 1900 : y1;
  y2 = y2 < 1000 ? y2 + 1900 : y2;  
  document.SearchFare.depart_date.value=m1+'/'+d1+'/'+y1;
  document.SearchFare.return_date.value=m2+'/'+d2+'/'+y2; 
  document.SearchFare.trip_type[0].checked = true;
}

function saveCurrentData() 
{   
	var agent_flag = 0;
	for(i=0; i<document.SearchFare.elements.length; i++)
	{
		if (document.SearchFare.elements[i].name == 'agent_id'){
			agent_flag = 1;
		}
		//document.write("The field name is: " + document.SearchFare.elements[i].name + " and it’s value is: " + document.SearchFare.elements[i].value + ".<br />");
	}
    var trip = document.SearchFare.trip_type[0].checked?1:0;
	if (document.SearchFare.trip_type[2].checked){
		trip = 2;
	}		
	var data = '';
	data = document.SearchFare.gateway_dep.value;
	data += ":" + document.SearchFare.gateway_ret.value;    	
	data += ":" + trip;
	data += ":" + document.SearchFare.depart_date.value;
	data += ":" + document.SearchFare.return_date.value;
	data += ":" + document.SearchFare.adult.value;
	data += ":" + document.SearchFare.child.value;
	data += ":" + document.SearchFare.infant.value;	
    data += ":" + document.SearchFare.cls.value;
	data += ":" + document.SearchFare.gateway_departure.value; 
	data += ":" + document.SearchFare.gateway_return.value; 
	data += ":" + document.SearchFare.dest_dep.value; 
	data += ":" + document.SearchFare.dest_ret.value; 
	data += ":" + document.SearchFare.dest_departure.value; 
	data += ":" + document.SearchFare.dest_return.value; 		
	data += ":" + document.SearchFare.client_ref.value;
    data += ":" + document.SearchFare.currency.value;
	if (agent_flag)
	{
		//alert(document.SearchFare.agent_id.value);
		data += ":" + document.SearchFare.agent_id.value;
	}
	document.cookie= "fn_query=" + escape(data) + ";path=/";
	//alert(data);
}


function set_date_from_cookie() 
{	
	var datacookie = document.cookie;
	var prefix = "fn_query=";
	var begin = datacookie.indexOf(prefix);	
	if (begin >= 0 )
	{        
	    var data = unescape(datacookie.substring(begin+prefix.length));        
        var fn_cookies= data.split(";");
		var arrdata= fn_cookies[0].split(":");        
		document.SearchFare.gateway_dep.value =arrdata[0];
		document.SearchFare.gateway_ret.value =arrdata[1];
		document.SearchFare.gateway_departure.value =arrdata[9];
		document.SearchFare.gateway_return.value =arrdata[10];
        document.SearchFare.trip_type[0].checked = true;               
		document.SearchFare.depart_date.value =arrdata[3];
        document.SearchFare.return_date.value =arrdata[4];
		document.SearchFare.adult.value =arrdata[5];
        document.SearchFare.child.value =arrdata[6];
		document.SearchFare.infant.value =arrdata[7];
        document.SearchFare.cls.value =arrdata[8];        
	}
	else
	{        
		setDates();
	}
}


function getCurrentDate() {
  now = new Date();  
  var m1 = now.getMonth()+1;      
  var d1 = now.getDate();    
  var y1 = now.getYear(); 
  m1 = m1 < 10 ? '0' + m1 : m1;  
  d1 = d1 < 10 ? '0' + d1 : d1;  
  y1 = y1 < 1000 ? y1 + 1900 : y1; 
  
  var date = m1+'/'+d1+'/'+y1; 
  return(date); 
}

function compareDate(date1,date2,type) {
 // var calRE = getFormat();

    var dStr1 = date1.substring(6,10) + "" + date1.substring(0,2) + "" + date1.substring(3,5);
    var dStr2 = date2.substring(6,10) + "" + date2.substring(0,2) + "" + date2.substring(3,5);
        
  if (dStr1 == dStr2)
  {   
    return 0;
  }
  else if (dStr1 < dStr2 && type == 1)
  {   
    return 1;
  }
  else if (dStr1 > dStr2 && type == 2)
  {   
    return 1;
  }
  else {
    return -1;
  }
}

function chkNumofDays(date){
     //Get 1 day in milliseconds
    var one_day=1000*60*60*24;
    var today=new Date();
    var sel_date=new Date(date.substring(6,10),date.substring(0,2)-1,date.substring(3,5));
    
    //Calculate difference btw the two dates, and convert to days
    var diff = Math.ceil((sel_date.getTime()-today.getTime())/(one_day));
    //alert(diff);
    return diff;
}

function setReturnDate(){       
    var date = document.SearchFare.depart_date.value;  
    var dep =new Date(date.substring(6,10),date.substring(0,2)-1,date.substring(3,5));
    dep.setTime(dep.getTime()+60000*60*24*7);
    var m1 = dep.getMonth()+1;      
    var d1 = dep.getDate();    
    var y1 = dep.getYear();
 
    m1 = m1 < 10 ? '0' + m1 : m1; 
    d1 = d1 < 10 ? '0' + d1 : d1; 
    y1 = y1 < 1000 ? y1 + 1900 : y1;
  
    document.SearchFare.return_date.value=m1+'/'+d1+'/'+y1;    
}

function checksyntax(date)
{
    var now = new Date();
    var y = now.getYear(); 
    y = y < 1000 ? y + 1900 : y;    
    if (date.substring(2,3) != '/')
    {
        date = '0' + date
    }
    if (date.substring(5,6) != '/')
    {
        date = date.substring(0,3) + '0' + date.substring(3,10)
    }
    
    if (date.length != 10)
    {
        date = date.substring(0,6) + y;
    }

    if (date.substring(2,3) != '/' && date.substring(5,6) != '/')
    {
        return 1;
    }    
    else
    {
    return date;
    }
}

function is_valid_airport(id) {

   var city = document.getElementById(id).value;   
   if ( city.length < 3 )  {
      alert('Please make sure you have selected the right ' + id + ' airport');
      return false;
   }

   var code = city.toUpperCase();

   if ( code.length < 3 )  {
      alert('Please make sure you have selected the right ' + id + ' airport');
      return false;
   }

   if ( city.length >= 3 ) {
      var code = city.replace(/.*\((...)\)/, "$1").toUpperCase();
   }
   
   if ( code.length == 3 ) {   	
   	if (id == "gateway_departure"){
      		document.SearchFare.gateway_dep.value = code;
	}
	if (id == "gateway_return"){
      		document.SearchFare.gateway_ret.value = code;
	}
	if (id == "dest_departure"){
      		document.SearchFare.dest_dep.value = code;
	}
	if (id == "dest_return"){
      		document.SearchFare.dest_ret.value = code;
	}
      return true;
   }

   alert('Please make sure you have selected the right airport');
   return false;
}


function ValidateDate() {	
    is_valid_airport('gateway_departure');
    is_valid_airport('gateway_return');	
    if (document.SearchFare.trip_type[2].checked){
    	is_valid_airport('dest_departure');
    	is_valid_airport('dest_return');
    }
	saveCurrentData();
    if (!(document.SearchFare.trip_type[0].checked || document.SearchFare.trip_type[1].checked ||
    document.SearchFare.trip_type[2].checked))
    {
        alert('Please select the Trip type');
        return false;
    }
    if (document.SearchFare.depart_date.value.length > 0) {
        var date = document.SearchFare.depart_date.value;
        if (date.substring(0,2) > 12){
            alert('The Depart Date Format is Incorrect.  Please enter the date in this format MM/DD/YYYY')
            return false;
        }
    }
    else {
	    alert('You must select a departing date');
	    return false;
    }

    if (checksyntax(document.SearchFare.depart_date.value) == 1)
    {
        alert('The Depart Date Format is Incorrect')
        return false;
    }
    else
    {
        document.SearchFare.depart_date.value = checksyntax(document.SearchFare.depart_date.value)
    }        

    if (compareDate(document.SearchFare.depart_date.value, getCurrentDate(),1) == 1)
    {
        alert('Your depart date is earlier than today. Please select another date.')
        return false;
    }
           
    if (document.SearchFare.trip_type[0].checked || document.SearchFare.trip_type[2].checked){        
        if (document.SearchFare.return_date.value.length > 0 ) {
            var date = document.SearchFare.return_date.value;
            if (date.substring(0,2) > 12){
                alert('The Return Date Format is Incorrect.  Please enter the date in this format MM/DD/YYYY')
                return false;
            }
        }
        else {        
            alert('You must select a returning date');
            return false;        
        }
        if (checksyntax(document.SearchFare.return_date.value) == 1)
        {
            alert('The Return Date Format is Incorrect')
            return false;
        }
        else
        {        
            document.SearchFare.return_date.value = checksyntax(document.SearchFare.return_date.value);        
        }
    
        if (compareDate(document.SearchFare.return_date.value, getCurrentDate(),1) == 1)
        {
            alert('Your return date is earlier than today. Please select another date.')
            return false;
        }
        
        if (compareDate(document.SearchFare.depart_date.value,document.SearchFare.return_date.value,2) == 1)
        {
            alert('Your return date is earlier than departure date. Please select another date.')
            return false;
        } 
      }
        
    if (document.SearchFare.gateway_departure.value == '')
    {
		var depart_alert = "Please enter the departure city or select a city from the drop down list.  ";
		depart_alert += "If you have entered a city and you see this message please make sure ";
		depart_alert += "you have entered the city only and no province, state or country.";
        alert(depart_alert);
        return false;
    }

    if (document.SearchFare.gateway_return.value == '')
    {
		var return_alert = "Please enter the destination city or select a city from the drop down list.  ";
		return_alert += "If you have entered a city and you see this message please make sure ";
		return_alert += "you have entered the city only and no province, state or country.";
        alert(return_alert);
        return false;
    } 
	if (document.SearchFare.trip_type[2].checked){
		 if (document.SearchFare.dest_departure.value == '')
		{
			var depart_alert = "Please enter the destination departure city or select a city from the drop down list.  ";
			depart_alert += "If you have entered a city and you see this message please make sure ";
			depart_alert += "you have entered the city only and no province, state or country.";
			alert(depart_alert);
			return false;
		}
	
		if (document.SearchFare.dest_return.value == '')
		{
			var return_alert = "Please enter the destination return city  or select a city from the drop down list.  ";
			return_alert += "If you have entered a city and you see this message please make sure ";
			return_alert += "you have entered the city only and no province, state or country.";
			alert(return_alert);
			return false;
		} 
	}
     if (document.SearchFare.infant.value)
    {
        if (document.SearchFare.infant.value > document.SearchFare.adult.value)
        {
            alert('Number of Infants must be less than equal to number of Adults');
            return false;
        }
    }   
	omnitureOnClickCode();
    return true;
}

function omnitureOnClickCode() {
	
	var s=s_gi('flightnetworkdev'); 					//use the proper report suite

	//all variables that will be set - including the events variable
	s.linkTrackVars='events,prop1,prop2,prop3,prop4,prop5,prop6,prop7,prop8,prop9,prop10,prop11,prop12,prop15,prop16,eVar1,eVar2,eVar3,eVar4,eVar5,eVar6,eVar7,eVar8,eVar9,eVar10,eVar11,eVar12,eVar20,prop22,prop25,prop26,prop27,prop50,eVar22,eVar25,eVar26,eVar27,eVar50';
	
	//the events that will be tracked in s.events				 		
	s.linkTrackEvents='event1';
	
		//now set all variables as normal
	s.events='event1';   //Set the search event						
	s.prop1=s.eVar1=document.SearchFare.gateway_dep.value;   //Origin City
	s.prop2=s.eVar2=document.SearchFare.gateway_ret.value;   //Destination City
	s.prop3=s.eVar3=document.SearchFare.gateway_dep.value + ' - ' +document.SearchFare.gateway_ret.value; //Origin-Destination City Pair
	s.prop4=s.eVar4=document.SearchFare.depart_date.value;  //Departure Date
	//alert("Prop 1: " + document.SearchFare.gateway_dep.value + 'Prop2: ' +  document.SearchFare.gateway_ret.value + ' '+document.SearchFare.depart_date.value + ' ' + document.SearchFare.return_date.value);
	//alert("Prop 1: " + s.prop1 + 'Prop2: ' + s.prop2 + 'Prop3: '+ s.prop3 + 'Prop4: ' + s.prop4);
	var tripduration = chkNumofDays(document.SearchFare.return_date.value) - chkNumofDays(document.SearchFare.depart_date.value);
       
	//s.prop6=s.eVar6=tripduration;   //Trip Duration
	s.prop7=s.eVar7=chkNumofDays(document.SearchFare.depart_date.value); //Days to Departure

        s.prop8 = s.eVar8 = 0;
        s.prop9 = s.eVar9 = 0;
        s.prop10 = s.eVar10 = 0;
        s.prop11 = s.eVar11 = 0;
	s.prop8=s.eVar8=document.SearchFare.adult.value;   //# of Adults
	s.prop9=s.eVar9=document.SearchFare.child.value;  //#of Children
	s.prop10=s.eVar10=document.SearchFare.infant.value; //# of Infants
	s.prop11=s.eVar11= s.prop8*1 + s.prop9*1;  //# of passengers - sum prop8 and prop9
	var triptype = 'Round Trip';						// default - round trip
	if(document.SearchFare.trip_type[1].checked) triptype = 'Oneway';	// one way
	if(document.SearchFare.trip_type[2].checked) triptype = 'MultiCity';	// open jaw
	//alert("TripType is " + triptype);
	s.prop12 = s.eVar12 = triptype; //Flight Type
	if(triptype == 'Oneway') {
		s.prop5=s.eVar5='0'; //Return Date
		s.prop6=s.eVar6='0';   //Trip Duration
	}
	else {
		s.prop5=s.eVar5=document.SearchFare.return_date.value; //Return Date
		s.prop6=s.eVar6=tripduration;   //Trip Duration
	}
	s.prop16 = s.pageName;	
        //alert("values " + chkNumofDays(document.SearchFare.depart_date.value) + ' ' +  document.SearchFare.adult.value + ' '+document.SearchFare.child.value + ' ' + document.SearchFare.cls.value);
        //alert("Prop7: " + s.prop7 + 'Prop8: ' + s.prop8 + 'prop9: '+ s.prop9 + 'prop10: ' + s.prop10 + 'prop11: ' + s.prop11 + 'prop12: ' + s.prop12);
	
	//Send the image request
	s.tl('www.flightnetwork.com', 'o', 'Flight Search');		//send the image request - see the manual for the parameter options

        	
	//Best proctice is to clear all variables once the s.tl call has been sent
	s.events='';					
	s.prop1=s.eVar1='';			
	s.prop2=s.eVar2='';			
	s.prop3=s.eVar3='';		
	s.prop4=s.eVar4='';	
	s.prop5=s.eVar5='';	
	s.prop6=s.eVar6='';			
	s.prop7=s.eVar7='';			
	s.prop8=s.eVar8='';			
	s.prop9=s.eVar9='';			
	s.prop10=s.eVar10='';			
	s.prop11=s.eVar11='';			
	s.prop12=s.eVar12='';
	
}
