// override the maxDate global variable which is used by the calendar (this is intially set in date.js)
// dealfinders can only be obtained for up to six months ahead
maxDate = new Date(today.getFullYear(),today.getMonth()+7,0);
    
// load all dfa behaviours after the dom is loaded
$(function(){jQuery.dfa.init();});

// ---------- All of the deal finder alerts functions are contained in the jQuery.dfa object/namespace ----------
// handle validation for each step of the dealfinder sign up page
jQuery.dfa = {
    
    // properties
   ajax_url:   '/travel-toolbox/dealfinder-alerts-ajax-handler/ajax.php',
    
    alerts_url: '/travel-toolbox/mydeal/',
    manage_alerts_url: '/travel-toolbox/manage-alerts/',
    user: null,
    current_step: 1,
    
    numElements: 0,
    codeArray: new Array(),
    nameArray: new Array(),
    depart1Array: new Array(),
    depart2Array: new Array(),
    daysStayingArray: new Array(),
    
    // methods
    init: function(){
     
        var current_step = this.current_step;
        var ajax_url = this.ajax_url;
        var alerts_url = this.alerts_url;
               
        if(document.domain == "cms.webjet.com.au" || document.domain  == "webjet-au")
        {
    		ajax_url = '/modules/mydealfinder-ajax-handler/ajax.php';
    		jQuery.dfa.ajax_url = ajax_url;
    	}
    
        
        // ---------- CREATE ALERT SCREEN ----------
        
        // Load alert if we are editing an existing one
        if(this.getUrlParam('edit'))
        {
            $.get(ajax_url,{action:'getAlert',id:this.getUrlParam('edit')},function(xml){
            

		   if($('destination_type', xml).text() == "transtasman")
			$('input:radio[name=destination_type]')[1].checked = true;
		   else if($('destination_type', xml).text() == "domestic")
		   	$('input:radio[name=destination_type]')[2].checked = true;
		   else
			$('input:radio[name=destination_type]')[0].checked = true;
			
		   if($('class', xml).text() == "economy")
		   {
		   	$('input:radio[name=class_type]')[0].checked = true;
		   	$('#destination_radio_domestic').show();
		   }
		   else
		   {
		   	$('input:radio[name=class_type]')[1].checked = true;
		   	$('#destination_radio_domestic').hide();
		   }

		   if($('airline_pref', xml).text() == "meal")
			$('input:radio[name=airline_pref]')[1].checked = true;
		   else
			$('input:radio[name=airline_pref]')[0].checked = true;
			
		$('#main_body').show();
		$('#main_selection').show();
		jQuery.dfa.showLengthOfStay();
            
            
             //  var start_date = $('start_date', xml).text().split('-');
             // document.getElementById('DateOut').value = start_date[2]+'/'+start_date[1]+'/'+start_date[0];
             //  var end_date = $('end_date', xml).text().split('-');
             //  document.getElementById('DateBack').value = end_date[2]+'/'+end_date[1]+'/'+end_date[0];
              
             //  var lengthStay = $('lengthStay', xml).text();
             //  if(lengthStay != "" && lengthStay > 0)
             //  		document.getElementById('lengthStay').value = lengthStay;
             
             $('#myDealfinderStep2 h1').text('Change My Deal Finder Alert');
             $('#dfa_change_id').val($('id', xml).text());
             
             	var region = $('input:radio[name=destination_type]:checked').val();
            	var classTo = $('input:radio[name=class_type]:checked').val();
               

                if($('destination_type', xml).text() == 'domestic')
		{
			$("#from").removeOption(/./);
			var myOptions = {
				"ADL" : "Adelaide",
				"BNE" : "Brisbane",
				"HBA" : "Hobart",
				"MEL" : "Melbourne - All Airports",
				"PER" : "Perth",
				"SYD" : "Sydney"
				
			};
			$("#from").addOption(myOptions, false);
		}
		else if(classTo == "business" && region == "international")
		{
			$("#from").removeOption(/./);
			var myOptions = {
				"BNE" : "Brisbane",
				"MEL" : "Melbourne",
				"PER" : "Perth",
				"SYD" : "Sydney"
			};
			$("#from").addOption(myOptions, false);        		
		}
		else if(classTo == "business" && region == "transtasman")
		{
			$("#from").removeOption(/./);
			var myOptions = {
				"BNE" : "Brisbane",
				"MEL" : "Melbourne",
				"SYD" : "Sydney"
			};
			$("#from").addOption(myOptions, false);       
        	}
		
		$('#from').val($('departure_code', xml).text()).change();
               
               
               
               // put it in a timeout so the change behavior above has done to do its work
               setTimeout(function(){
                   $('destinations row', xml).each(function(){
                      
                      	//alert("wer");
                      
                      	//$.dfa.addCity($('destination_code',this).text(),$('city_name',this).text());
                      
                      
			jQuery.dfa.codeArray[jQuery.dfa.numElements] = $('destination_code',this).text(); 
			jQuery.dfa.nameArray[jQuery.dfa.numElements] = $('city_name',this).text();
			var start_date = $('start_date', this).text().split('-');
			jQuery.dfa.depart1Array[jQuery.dfa.numElements] = start_date[2]+'/'+start_date[1]+'/'+start_date[0];
			var end_date = $('end_date', this).text().split('-');
			jQuery.dfa.depart2Array[jQuery.dfa.numElements] = end_date[2]+'/'+end_date[1]+'/'+end_date[0];
			
			//alert(jQuery.dfa.depart2Array[jQuery.dfa.numElements]);
			
			jQuery.dfa.daysStayingArray[jQuery.dfa.numElements] = $('lengthStay',this).text();			
			jQuery.dfa.numElements++;
                   });
			
                   
                   jQuery.dfa.redraw();
                   //jQuery.dfa.processLi();
               },500);
            });
            
        }
        
        $('#myDealfinderSignup input[name=next]').click(function(){
            if(current_step<3)
            {
                if(!$.dfa.validate(current_step)) return false;
                $('#myDealfinderStep' + current_step).fadeOut(500,function(){
                    if(current_step == 1) $.dfa.loadUser();
                    $('#myDealfinderSignup ul li:eq(' + (current_step-1) + ')').addClass('complete');
                    current_step++;
                    $('#myDealfinderSignup ul li').removeClass('current');
                    $('#myDealfinderSignup ul li:eq(' + (current_step-1) + ')').addClass('current');
                    $('#myDealfinderStep' + current_step).fadeIn(500,function(){
                        if($.browser.msie)this.style.removeAttribute('filter');
                    });
                });
                return false;
            }
        });
        
        $('#myDealfinderSignup input[name=back]').click(function(){
            if(current_step>1)
            {
                $('#myDealfinderStep' + current_step).fadeOut(500,function(){
                    current_step--;
                    $('#myDealfinderSignup ul li').removeClass('current');
                    $('#myDealfinderSignup ul li:eq(' + (current_step-1) + ')').addClass('current');
                    $('#myDealfinderStep' + current_step).fadeIn(500, function(){
                        if($.browser.msie)this.style.removeAttribute('filter');
                    });
                });
            }
            return false;
        });
        
        $('#myDealfinderSignup input[name=create-alert]').click(function(){
            if(!$.dfa.validate(1)){
                $('#myDealfinderSignup input[name=back]').click().click();
            }
            if(!$.dfa.validate(2)){
                $('#myDealfinderSignup input[name=back]').click();
            }
            $.dfa.submit();
            return false;
        });
        
        // this is used to detect when the CTRL or SHIFT key is held down
        var ctrlDown = false;
        var shiftDown = false;
        $(document).keydown(function(e){
            ctrlDown = e.which == 17 ? true : false;
            shiftDown = e.which == 16 ? true : false;
        });
        $(document).keyup(function(e){
            if(e.which == 17) ctrlDown = false;
            if(e.which == 16) shiftDown = false;
        });
        
        if($('input:radio[name=destination_type]:checked').length == 0)
        {
        	$('#main_selection').hide();	
        }
        
        $('.class_radio').click(function(){
        	
			setTimeout(function(){
			$('#main_body').show();
			$('#from').val('');
        		$('#selectedCities li, #to li').remove();
        		jQuery.dfa.showLengthOfStay();
        		}, 200);
        		
        		//Do Drop Downs!!!
        		
        		if($(this).val() == 'economy')
        		{
				$('#destination_radio_domestic').show();
        		}
        		else
        		{
				$('#destination_radio_domestic').hide();
        		}
        		
        		var region = $('input:radio[name=destination_type]:checked').val();
        		
        		if(region == 'domestic')
			{
				$("#from").removeOption(/./);
				var myOptions = {
					"ADL" : "Adelaide",
					"BNE" : "Brisbane",
					"HBA" : "Hobart",
					"MEL" : "Melbourne - All Airports",
					"PER" : "Perth",
					"SYD" : "Sydney"
				};
				$("#from").addOption(myOptions, false);
			}
			else if($(this).val() == "business" && region == "international")
			{
				$("#from").removeOption(/./);
				var myOptions = {
					"BNE" : "Brisbane",
					"MEL" : "Melbourne",
					"PER" : "Perth",
					"SYD" : "Sydney"
				};
				$("#from").addOption(myOptions, false);        		
			}
			else if($(this).val() == "business" && region == "transtasman")
			{
				$("#from").removeOption(/./);
				var myOptions = {
					"BNE" : "Brisbane",
					"MEL" : "Melbourne",
					"SYD" : "Sydney"
				};
				$("#from").addOption(myOptions, false);       
			}
			else
			{
				$("#from").removeOption(/./);
				$("#hiddenSelectFrom").copyOptions("#from", "all"); 
        		}
        		
        		
        		jQuery.dfa.resetVars();
        		jQuery.dfa.redraw();	
        	});
        
        $('.destination_radio').click(function(){
        	
        	setTimeout(function(){
			$('#main_selection').show();
			$('#from').val('');
        		$('#selectedCities li, #to li').remove();
        		jQuery.dfa.showLengthOfStay();
        		}, 200);
        		
        		//Do Drop Downs!!!
        		
        		var region = $('input:radio[name=destination_type]:checked').val();
            		var classTo = $('input:radio[name=class_type]:checked').val();
        		
        		if($(this).val() == 'domestic')
        		{
        			$("#from").removeOption(/./);
        			var myOptions = {
        				"ADL" : "Adelaide",
        				"BNE" : "Brisbane",
        				"HBA" : "Hobart",
					"MEL" : "Melbourne - All Airports",
					"PER" : "Perth",
					"SYD" : "Sydney"
				};
				$("#from").addOption(myOptions, false);
        		}
        		else if(classTo == "business" && region == "international")
        		{
        			$("#from").removeOption(/./);
        			var myOptions = {
					"BNE" : "Brisbane",
					"MEL" : "Melbourne",
					"PER" : "Perth",
					"SYD" : "Sydney"
				};
				$("#from").addOption(myOptions, false);        		
			}
        		else if(classTo == "business" && region == "transtasman")
        		{
        			$("#from").removeOption(/./);
				var myOptions = {
					"BNE" : "Brisbane",
					"MEL" : "Melbourne",
					"SYD" : "Sydney"
				};
				$("#from").addOption(myOptions, false);       
        		}
        		else
        		{
        			$("#from").removeOption(/./);
        			$("#hiddenSelectFrom").copyOptions("#from", "all"); 
        		}
        		
        		jQuery.dfa.resetVars();
        		jQuery.dfa.redraw();	
        });
        
        
        $('#from').change(function(){
            
            // change the heading in the selected destinations box
            if($('#from option:selected').text().length > 0)
                $('#selected-cities h4').text('From ' + $('#from option:selected').text() + ' to City(s):');
                
            // clear any destinations in the list as they will need to be set again for the newly selected city
            $('#selectedCities li, #to li').remove();
            // populate the enabled destinations for the selected city
            $.get(ajax_url,{action: 'getDestinationCitiesFromDepartureCode', code: $('#from').val(), destination_region: $('input:radio[name=destination_type]:checked').val(), class_type: $('input:radio[name=class_type]:checked').val()},function(xml){
            
            
                $('row',xml).each(function(){
                    if($('city_name',this).text().length > 0)
                    {
                        $('#to').append('<li val="' + $('code',this).text() + '">' + $('city_name',this).text() + '</li>');
                    }
                });
                $('#to li').each(function(){
                    jQuery.dfa.attachMultiBehaviors(this,ctrlDown,shiftDown);
                });
                 $('#selectedCities li').each(function(){
		    jQuery.dfa.attachMultiBehaviors(this,ctrlDown,shiftDown);
                });
                jQuery.dfa.processLi();
                 
            });
            $('#selectedCities li').click(function(){

		  $('#selectedCities li.selected').each(function(){
			 $(this).remove().appendTo('#to').removeClass('selected');
		 });
		});
        });
        
        // add cities to selected cities list
        $('#to').click(function(){
    

		   $('#to li.selected').each(function(){
		       //$(this).remove().appendTo('#selectedCities').removeClass('selected');
		       //jQuery.dfa.attachMultiBehaviors(this,ctrlDown,shiftDown);
		   });
       	});
        
        // remove cities from selected list
        $('#selectedCities').click(function(){
           $('#selectedCities li.selected').each(function(){
               $(this).remove().appendTo('#to').removeClass('selected');
               jQuery.dfa.attachMultiBehaviors(this,ctrlDown,shiftDown);
           });
          jQuery.dfa.showLengthOfStay();
        });
        
        $('input[name=frequency]').click(function(){
            if($(this).val() == 'weekly'){
                $('#weekday_select').show();
            } else {
                $('#weekday_select').hide();
            }
        });
        
        // check if logged in
        this.isLoggedIn();
        
        // ---------- MANAGE ALERT SCREEN ----------
        
        // show success message if required
        if(this.getUrlParam('success'))
        {
            $('#dfa_success_message').show();
        }
        
        // show success message if required
        if(this.getUrlParam('updated'))
        {
            $('#dfa_changed_message').show();
        }
        
        // unsubscribe links
        $('a.unsubscribeAlertLink').click(function(){
            var container = $(this).parents('div.alertSummary');
            $.post(ajax_url, {action: 'deleteAlert', id: $(this).parents('div.alertSummary').attr('id').replace('alert_','')}, function(xml){
                $('~div.alertSummary h2 span', container).each(function(){
                    var number = parseInt($.trim($(this).text().replace('.',''))) - 1;
                    $(this).text(number + '. ');
                });
                $(container).remove();
                $('#dfa_success_message').hide();
                $('#dfa_remove_message').show();
                $('#dfa_current_alerts_total').text('You currently have ' + $('div.alertSummary').length + ' alerts:');
            });
        });
                
         $('a.unsubscribeSMSLink').click(function(){
	        var container = $(this).parents('div.alertSummary');
           	 $.post(ajax_url, {action: 'deleteSMS', id: $(this).parents('div.alertSummary').attr('id').replace('alert_','')}, function(xml){
           	 	location = '/travel-toolbox/manage-alerts/';
           	 });
           });
        
         // unsubscribe links
	$('a.createSMSalertLink').click(function(){
	    location = '/travel-toolbox/manage-sms/?id=' + $(this).parents('div.alertSummary').attr('id').replace('alert_','');
        });
        
        // change links
        $('a.changeAlertLink').click(function(){
            document.location.href = alerts_url + '?edit=' + $(this).parents('div.alertSummary').attr('id').replace('alert_','');
            return false;
        });
    },
    
    showLengthOfStay: function()
    {
	
	if($('input:radio[name=destination_type]:checked').val() == "transtasman" || $('input:radio[name=destination_type]:checked').val() == "international")
		document.getElementById('airlineRow').style.visibility = 'visible';
	else
		document.getElementById('airlineRow').style.visibility = 'hidden';
    },
    
    processLi: function()
    {
    	$('#to li').click(function(){
		if(jQuery.dfa.numElements < 5)
		{
			//CHANGE ME!!!
			//alert($(this).attr('val'));
			//alert($(this).html());
			
			
			
			jQuery.dfa.saveDraw();
			
			jQuery.dfa.codeArray[jQuery.dfa.numElements] = $(this).attr('val');
			jQuery.dfa.nameArray[jQuery.dfa.numElements] = $(this).html();
			jQuery.dfa.depart1Array[jQuery.dfa.numElements] = "dd/mm/yyyy";
			jQuery.dfa.depart2Array[jQuery.dfa.numElements] = "dd/mm/yyyy";
			jQuery.dfa.daysStayingArray[jQuery.dfa.numElements] = "3";
			
			if(jQuery.dfa.numElements > 0)
			{
				jQuery.dfa.depart1Array[jQuery.dfa.numElements] = jQuery.dfa.depart1Array[0];
				jQuery.dfa.depart2Array[jQuery.dfa.numElements] = jQuery.dfa.depart2Array[0];
				jQuery.dfa.daysStayingArray[jQuery.dfa.numElements] = jQuery.dfa.daysStayingArray[0];
			}
			jQuery.dfa.numElements++;
			
			jQuery.dfa.redraw();
		}
	});
	jQuery.dfa.resetVars();
        jQuery.dfa.redraw();
    },
    
    redraw: function()
    {
    	var myhtml = '<input type="hidden" name="numElements" id="numElements" value="' + jQuery.dfa.numElements + '" /><table width="490px" class="mydftable"><tr><td colspan=2>My Deal Finder cities <span style="font-size: 9px">(max 5)</span></td><td><b>Depart Between:</b></td>';
    	if($('input:radio[name=destination_type]:checked').val() == "transtasman" || $('input:radio[name=destination_type]:checked').val() == "domestic")
	    	myhtml += '<td><b>Days Staying:</b></td>';
    	myhtml +='</tr>';
    	for(var i = 0; i < jQuery.dfa.numElements; i++)
    	{
    		myhtml += '<tr><td><input type="hidden" name="code_' + i + '" id="code_' + i + '" value="' + jQuery.dfa.codeArray[i] + '" />';
    		myhtml += (i + 1) + ". " +  jQuery.dfa.nameArray[i];
    		
    		myhtml += "</td><td>";
    		myhtml += "<a href='#' onClick='jQuery.dfa.removeCity(" + i + "); return false'><img src='/site/images/delete.png'></a>";
    		
    		myhtml += "</td><td>";
    		myhtml += '<input maxlength="10" id="depart_1_' + i + '" name="depart_1_' + i + '" size="10" value="' + jQuery.dfa.depart1Array[i].replace(/'/, "") + '" onchange="checkFutureDate(this);" onfocus="SC(this, \'depart_2_' + i + '\');" onclick="event.cancelBubble=true;SC(this, \'depart_2_' + i + '\');   return false" onblur="UpdDt(this, \'depart_2_' + i + '\');" /> and ';
    		myhtml += '<input maxlength="10" id="depart_2_' + i + '" name="depart_2_' + i + '" size="10" value="' + jQuery.dfa.depart2Array[i].replace(/'/, "") + '" onchange="checkFutureDate(this);" onfocus="SC(this, null);" onclick="event.cancelBubble=true;SC(this, null);  return false" onblur="UpdDt2(this, \'depart_1_' + i + '\');" />';
    		
    		myhtml += "</td>";
    		
    		if($('input:radio[name=destination_type]:checked').val() == "transtasman" || $('input:radio[name=destination_type]:checked').val() == "domestic")
    		{
			myhtml += "<td>";
			myhtml += "<select name='daysStaying_" + i + "' id='daysStaying_" + i + "'>";
			for(var j = 1; j < 30; j++)
			{
				myhtml += "<option value='" + j + "'";
				if(j == jQuery.dfa.daysStayingArray[i])
					myhtml += " selected";
				myhtml += ">" + j + "</option>";
			}
			myhtml += "</select></td>";
		}
    		
    		myhtml += "</tr>";
    	}
    	myhtml += "</table>";
    	$('#toCities').html(myhtml);
    	
    	//Do Tool Tips
    	
    	
    	for(var i = 0; i < jQuery.dfa.numElements; i++)
    	{
    		$("#depart_1_" + i + ", #depart_2_" + i + "").tooltip({
  			 position: "bottom", 
  			 tip: "#Cal2MFrame",
  			 offset: [10,20],
  			 effect: 'fade',
  			 def:      "mouseover focus,mouseout", 
  			 onBeforeHide: function(){return false}
  		});
    	}
    	
    },
    
    resetVars: function()
    {
    	jQuery.dfa.numElements = 0,
	jQuery.dfa.codeArray = new Array();
	jQuery.dfa.nameArray = new Array();
	jQuery.dfa.depart1Array = new Array();
	jQuery.dfa.depart2Array = new Array();
   	jQuery.dfa.daysStayingArray = new Array();
    },
    
    saveDraw: function()
    {
    	for(var i = 0; i < jQuery.dfa.numElements; i++)
    	{
    		jQuery.dfa.depart1Array[i] = $("#depart_1_" + i).val();
		jQuery.dfa.depart2Array[i] = $("#depart_2_" + i).val();
		if($('input:radio[name=destination_type]:checked').val() == "transtasman" || $('input:radio[name=destination_type]:checked').val() == "domestic")
			jQuery.dfa.daysStayingArray[i] = $("#daysStaying_" + i + "").val();
    	}
    },
    
    removeCity: function(vv)
    {
    	jQuery.dfa.saveDraw();
    	
    	jQuery.dfa.nameArray.splice(vv,1);
    	jQuery.dfa.codeArray.splice(vv,1);
    	jQuery.dfa.depart1Array.splice(vv,1);
    	jQuery.dfa.depart2Array.splice(vv,1);
    	jQuery.dfa.daysStayingArray.splice(vv,1);
    	jQuery.dfa.numElements--;
    	
    	jQuery.dfa.redraw();
    },
    
    attachMultiBehaviors: function(domElem,ctrlDown,shiftDown){
        $(domElem).toggle(
            function() {
                if(!ctrlDown && !shiftDown) $(this).parent().find('li').removeClass('selected');
               // $(this).addClass('selected');
            },
            function() {
                $(this).removeClass('selected');
            }
        )
        .mouseover(function(){
           // if(shiftDown) $(this).addClass('selected');
        });
    },
    
    addCity: function(val,text){
       var item = $('<li val="' + val + '">' + text + '</li>').appendTo('#selectedCities');
       this.attachMultiBehaviors(item);
       $('#to li').each(function(){
           if($(this).attr('val')==val){
               $(this).remove();
           }
       });
       
    },
    
    validate : function(step){
        
        switch(step){
            
            case 1:
              if($('#myDealfinderSignup input[name=firstname]').val()=='')
              {
                  alert('You must supply your first name');
                  $('#myDealfinderSignup input[name=firstname]').focus();
                  return false;
              }
              if($('#myDealfinderSignup input[name=email]').val()==''||$('#myDealfinderSignup input[name=email]').val().indexOf('@')==-1||$('#myDealfinderSignup input[name=email]').val().indexOf('.')==-1)
              {
                  alert('You must supply a valid email address');
                  $('#myDealfinderSignup input[name=email]').focus();
                  return false;
              }
              break;
              
            case 2:
              if($('#from').val()=='')
              {
                  alert('You must select a "from" location');
                  $('#from').focus();
                  return false;
              }
              if(jQuery.dfa.numElements == 0)
              {
                  alert('You must select at least one destination city');
                  return false;
              }
              else
              {
              	jQuery.dfa.saveDraw();
              	for(var i = 0; i < jQuery.dfa.numElements; i++)
              	{
              		//Check dates
              		if(jQuery.dfa.depart1Array[i] == "dd/mm/yyyy" || jQuery.dfa.depart1Array[i] == "" || jQuery.dfa.depart2Array[i] == "dd/mm/yyyy" || jQuery.dfa.depart2Array[i] == "")
              		{
              			alert('Please provide valid dates for all alerts');
              			if(jQuery.dfa.depart1Array[i] == "dd/mm/yyyy" || jQuery.dfa.depart1Array[i] == "")
	              			DE("depart_1_" + i).focus();
	              		else
	              			DE("depart_2_" + i).focus();
                  		return false;
              		}
              		else if(GetInputDate(DE("depart_1_" + i).value) >= GetInputDate(DE("depart_2_" + i).value))
              		{
              			alert('Please choose \'depart between\' dates that are least one day apart');
              			DE("depart_2_" + i).focus();
              			return false;
              		}
              	}
              }
              break;
        }
        
        return true;
    },
    
    loadUser: function(){
        $.get(this.ajax_url, {action: 'getUser', firstname: $('#firstname').val(), email: $('#email').val()}, function(xml){
           if($('result',xml).text())
           {
               this.user = $('row',xml);
               $('#dfa_manage_link').show();
           } 
        });
    },
  
    submit: function(){
        
        // build the values to send in the ajax request to create the alert
        var values = $('#frmMyDealfinder').serialize();
               
        //var selectedCities = new Array();
        //$('#selectedCities li').each(function(i){
        //    selectedCities[i] = $(this).attr('val');
        //});
        //values += '&selectedCities=' + selectedCities.join(',');
        values += '&action=createAlert';
        
        // send the ajax post and redirect to manage page if everything works properly
        var manage_alerts_url = this.manage_alerts_url;
        $.post(this.ajax_url, values, function(xml){
           $( 'status', xml ).text() == 'success' ? document.location.href = manage_alerts_url + '?success=true&destination_type=' + $('input:radio[name=destination_type]:checked').val() + '&travel_class=' + $('input:radio[name=class_type]:checked').val() : alert('An error occured. Please try again.');
        });
        
    },
    
    isLoggedIn: function(){
        $.get(this.ajax_url, {action: 'isLoggedIn'}, function(xml){
           if ($( 'status', xml ).text() == 'success')
           {
               this.user = $('result row', xml);
               $('#firstname').val($('firstname', this.user).text());
               $('#email').val($('email', this.user).text());
               $('#myDealfinderSignup input[name=next]:eq(0)').click();
               $('<span style="display:block; padding: 5px 0; font-weight:normal;">You are logged in as ' + $('email', this.user).text() + ' (<a href="#">logout</a>)</span>')
                 .css({
                     color: '#000',
                     fontSize: '12px'
                 })
                 .appendTo('#dfa_user_util')
                 .find('a')
                 .click(function(){
                     $.post(jQuery.dfa.ajax_url, {action: 'logout'}, function(xml){
                        document.location.href = jQuery.dfa.alerts_url;
                     });
                 })
                 .css('fontSize','12px');
               $('#dfa_user_util').show();
               return true;
           }
           else
           {
               return false;
           }
        });
    },
    
    getUrlParam: function(key){
        var sReturn = null;
        q_string = location.search.substring( 1 ).split( "&" );
        q_array = new Array();
        for( i=0; i < q_string.length; i++ ) 
        {
            pair = q_string[ i ].split( "=" );
            if ( key == pair[ 0 ].toString() )
                sReturn = unescape( pair[ 1 ] ).toString();
        } 	
        return sReturn;
    }

}