

if(navigator.userAgent.match(/opera mini/i)) {
//alert("Opera mini");
}
//Validate
   	jQuery.validator.addMethod("eqDest", function(value, element, param) {
				 return this.optional(element) || value != param.val();
				}, "Fra og Til kan ikke være lik");

$('#bookPage').live('pageshow',function(event){ 
$('#bookContent').append("<img src='http://www.wideroe.no/count.asp?id=mobbook' alt='' width='0' height='0' />");

   $("input[name$='TRIP_TYPE']").change(function(){
 
  var radio_value = $(this).val();
 
  if(radio_value=='R') {
    $("#showBackDate").show();
	$("#showBackDateLabel").show();
	//$(".dateWrap").css({'display': 'block'})
  }
  else if(radio_value=='O') {
    $("#showBackDate").hide();
	$("#showBackDateLabel").hide();
	//$(".dateWrap").css({'display': 'block'})
   }
  });
 
  $("#showBackDate").hide();
  $("#showBackDateLabel").hide();
  //$(".dateWrap").css({'display': 'block'});


  
    $("#bookForm").validate({
    rules: {
        E_LOCATION_1: { eqDest: $('#B_LOCATION_1') }
    }

	});
 
   $("#FIELD_YTH_NUMBER").change(function () {  
       if ($("#FIELD_YTH_NUMBER").val() > 0 ) { 
	   //alert("biiip");
            $("#FIELD_ADT_NUMBER").val(0).slider("refresh"); 
       } else if ($("#FIELD_ADT_NUMBER").val() == 0 && $("#FIELD_YTH_NUMBER").val() == 0 && $("#FIELD_CHD_NUMBER").val() == 0 && $("#FIELD_INFANTS_NUMBER").val() == 0){ 
            $("#FIELD_ADT_NUMBER").val(1).slider("refresh"); 
       } 
	   else { 
            $("#FIELD_ADT_NUMBER").val(); 
       } 
    }); 
   $("#FIELD_ADT_NUMBER").change(function () {  
       if ($("#FIELD_ADT_NUMBER").val() > 0 ) { 
            $("#FIELD_YTH_NUMBER").val(0).slider("refresh"); 
       } else if ($("#FIELD_ADT_NUMBER").val() == 0 && $("#FIELD_YTH_NUMBER").val() == 0 && $("#FIELD_CHD_NUMBER").val() == 0 && $("#FIELD_INFANTS_NUMBER").val() == 0){ 
            $("#FIELD_ADT_NUMBER").val(1).slider("refresh"); 
       } 
	   else { 
            $("#FIELD_YTH_NUMBER").val(); 
       } 
    }); 
   $("#FIELD_CHD_NUMBER").change(function () {  
       if ($("#FIELD_CHD_NUMBER").val() > 0 && $("#FIELD_ADT_NUMBER").val() == 0) { 
            $("#FIELD_CHD_NUMBER").val(0).slider("refresh"); 
       } else if ($("#FIELD_ADT_NUMBER").val() == 0 && $("#FIELD_YTH_NUMBER").val() == 0 && $("#FIELD_CHD_NUMBER").val() == 0 && $("#FIELD_INFANTS_NUMBER").val() == 0){ 
            $("#FIELD_ADT_NUMBER").val(1).slider("refresh"); 
       } 
	   else { 
            $("#FIELD_CHD_NUMBER").val(); 
       } 
    }); 
   $("#FIELD_INFANTS_NUMBER").change(function () {  
       if ($("#FIELD_INFANTS_NUMBER").val() > 0 && $("#FIELD_ADT_NUMBER").val() == 0) { 
            $("#FIELD_INFANTS_NUMBER").val(0).slider("refresh"); 
       } else if ($("#FIELD_ADT_NUMBER").val() == 0 && $("#FIELD_YTH_NUMBER").val() == 0 && $("#FIELD_CHD_NUMBER").val() == 0 && $("#FIELD_INFANTS_NUMBER").val() == 0){ 
            $("#FIELD_ADT_NUMBER").val(1).slider("refresh"); 
       } 
	   else { 
            $("#FIELD_INFANTS_NUMBER").val(); 
       } 
    }); 
   $("#FIELD_ADT_NUMBER").change(function () {  
       if ($("#FIELD_ADT_NUMBER").val() == 0 && $("#FIELD_CHD_NUMBER").val() > 0) { 
            $("#FIELD_CHD_NUMBER").val(0).slider("refresh"); 
       } else { 
            $("#FIELD_CHD_NUMBER").val(); 
       } 
    }); 
   $("#FIELD_ADT_NUMBER").change(function () {  
       if ($("#FIELD_ADT_NUMBER").val() == 0 && $("#FIELD_INFANTS_NUMBER").val() > 0) { 
            $("#FIELD_INFANTS_NUMBER").val(0).slider("refresh"); 
       } else { 
            $("#FIELD_INFANTS_NUMBER").val(); 
       } 
    }); 
   $("#FIELD_INFANTS_NUMBER").change(function () {  
       if ($("#FIELD_INFANTS_NUMBER").val() > $("#FIELD_ADT_NUMBER").val()) { 
            $("#FIELD_INFANTS_NUMBER").val($("#FIELD_ADT_NUMBER").val()).slider("refresh"); 
       } else { 
            $("#FIELD_INFANTS_NUMBER").val(); 
       } 
    }); 
//alert("filtrer");
	//alert($('#FIELD_ADT_NUMBER').val());


  });
//END Validate
//Datepicker
	$(function() {
	
		$( "#depDate" ).datepicker({ dateFormat: 'DD dd.mm.yy', altField: '#date1', constrainInput: true, altFormat: 'yymmdd0000', firstDay: 1, minDate: 0, maxDate: '+1y', onClose: function(dateText, inst) 
    { 

            var minValue = $(this).val();
            minValue = $.datepicker.parseDate("DD dd.mm.yy", minValue);
            minValue.setDate(minValue.getDate());
            $("#depDate2").datepicker( "option", "minDate", minValue);
			$("#showBackDate").html($("#depDate2").val());

    },
		beforeShow: function(input, inst) 
    {
		$("#showToDate").css({color:'#006f51'});
		$.extend($.datepicker,{_checkOffset:function(inst,offset,isFixed){return offset}}); 
		
		$('html,body').animate({
			 scrollTop: $("#showToDate").offset().top -50
			 }, 20);
			 
	}

	});
		$("#depDate").datepicker('setDate', '+1D'); 
		$( "#depDate" ).datepicker($.datepicker.regional['no']);
	//alert($( "#depDate" ).val());
	
			$("#showToDate").click(function() {
			  $("#depDate").datepicker('show');
			});
	
			  var toDate = $("#depDate").val();
			  $("#showToDate").html(toDate);
			  
			$("#depDate").change(function() {
			  var toDate = $(this).val();
			  $("#showToDate").html(toDate);
		   });
		
		$( "#depDate2" ).datepicker({ dateFormat: 'DD dd.mm.yy', altField: '#date2', constrainInput: true, altFormat: 'yymmdd0000', firstDay: 1, minDate: '+2d', maxDate: '+1y-2d', onClose: function(dateText, inst) 
    { 
		$('html,body').animate({
			 scrollTop: $("#btnDiv").offset().top -0
			 }, 20);
    },
		beforeShow: function(input, inst) 
    {
		$("#showBackDate").css({color:'#006f51'});
		$.extend($.datepicker,{_checkOffset:function(inst,offset,isFixed){return offset}}); 
		
		$('html,body').animate({
			 scrollTop: $("#showBackDate").offset().top -50
			 }, 20);


    }

		});		

		$("#depDate2").datepicker('setDate', '+3D'); 
		$( "#depDate2" ).datepicker($.datepicker.regional['no']);

			$("#showBackDate").click(function() {
			  $("#depDate2").datepicker('show');
			});
		
			  var backDate = $("#depDate2").val();
			  $("#showBackDate").html(backDate);	
			  
			$("#depDate2").change(function() {
			  var backDate = $(this).val();
			  $("#showBackDate").html(backDate);
		   });
		   
	});
//END Datepicker
//Cross domain request
/*
 * Create a YQL query, strip all script tags, and insert into the parent element.
*/ 
function getLoader() {
	$.mobile.showPageLoadingMsg();
}
  $(document).ready(function(){ 
  
  
   //$('.screen').html(screen.width);
  
//alert(screen.width);
	$("#wrapBy").show();
	$("#wrapBy3").hide();
	$("#wrapBy4").hide();
	//alert($("#filter").val());
	var filter = $("#filter").val();
						  if (filter == 'wf')
					  {
					  var selectedAirport = "WF";
					  }
					  else if (filter == 'sk')
					  {
					  var selectedAirport = "SK";
					  }
					  else
					  {
					  var selectedAirport = "WFSK";
					  }
	var path = "http://www.wideroe.no/firapps/webservice.asmx/GetAirportList?language=no&filter="+selectedAirport;

	//alert(path);
	requestCrossDomain(path, function(results) {
		//$('#container').append("<option>"+results+"</option>");
		$("#by").selectmenu("refresh");
		
	});
					$("#filter").change(function()
											{
							$.mobile.showPageLoadingMsg();
							//$("#filter").selectmenu("refresh");				
						var filter = $("#filter").val();
							 if (filter == 'wf')
							  {
							  
							  var selectedAirport = "WF";
							  	$("#wrapBy").show();
								$("#wrapBy3").hide();
								$("#wrapBy4").hide();
							  }
							  else if (filter == 'sk')
							  {
							//$("#filter").selectmenu("refresh");
							 
							  var selectedAirport = "SK";
								$("#wrapBy").hide();
								$("#wrapBy3").show();
								$("#wrapBy4").hide();
							  }
							  else
							  {
							  //$("#filter").selectmenu("refresh");
							  
							  var selectedAirport = "WFSK";
							  	$("#wrapBy").hide();
								$("#wrapBy3").hide();
								$("#wrapBy4").show();
							  }
						
							var path = "http://www.wideroe.no/firapps/webservice.asmx/GetAirportList?language=no&filter="+selectedAirport;
						//alert(path);
						
							requestCrossDomain(path, function(results) {
									
								//$('#container').append("<option>"+results+"</option>");

							
							$("#by4").selectmenu("refresh");
							$("#by3").selectmenu("refresh");

								
							});
						$("#filter").selectmenu("refresh");	
						});	


});	
		
function requestCrossDomain( site, callback ) {
	if ( !site ) {
		alert('No site passed');
		return false;
	}
	var filter = $("#filter").val();
						  if (filter == 'wf')
					  {
					  var selectedAirport = "WF";
					  }
					  else if (filter == 'sk')
					  {
					  var selectedAirport = "SK";
					  }
					  else
					  {
					  var selectedAirport = "WFSK";
					  }
//alert("tesssst"+filter);
	yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';

	$.getJSON(yql, function(data) {
		if ( data.results[0] ) {
		
			data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
			//alert(data);
																					if (filter == 'wf')
																						{
																						   $('#by').append("<option value=''>Velg flyplass</option>");
																						 }
																					else if (filter == 'sk')
																						 {
																						   $('#by3').append("<option value=''>Velg flyplass</option>");
																						 }
																					else
																						 {
																						   $('#by4').append("<option value=''>Velg flyplass</option>");
																						 }
			$(data).find('Airports').each(function(){
														  
														  var AirportName = ($(this).find('no').text()).replace(/\s/g, '&nbsp;');
														  
														  $(this).find('airportcode').each(function(){
																						   
																						   
																						   var AirportCode = $(this).text();
																					if (filter == 'wf')
																						{
																						   $('#by').append("<option value='"+ AirportCode +"'>"+AirportName+"</option>");
																						 }
																					else if (filter == 'sk')
																						 {
																						   $('#by3').append("<option value='"+ AirportCode +"'>"+AirportName+"</option>");
																						 }
																					else
																						 {
																						   $('#by4').append("<option value='"+ AirportCode +"'>"+AirportName+"</option>");
																						 }
																						 });
														  });
														 
			//$('#container').append("<option>"+data+"</option>");

			if ( typeof callback === 'function' ) {
				callback(data);
				
				//$("#by").selectmenu('refresh',true);
			}
			$.mobile.hidePageLoadingMsg();
		}
		
		else throw new Error('Nothing returned from getJSON.');
	});	
}
//*********************

function getIrute(){
  $('#resultat').live('pageshow',function(event){ 
  //$.mobile.showPageLoadingMsg();
  });	
	var filter = document.getElementById("filter").value;
	//alert(filter);
	if (filter == 'wf')
	{
		var prestation = document.getElementById("by");	
		var station = document.getElementById("by").value;
			if (station == '') 
			{
			alert("Velg flyplass");
			window.location = "#flyirute";
			}
			else
			{
			window.location = "#resultat";
			}
	}	
	else if (filter == 'sk')
	{
		var prestation = document.getElementById("by3");
		var station = document.getElementById("by3").value;
			if (station == '') 
			{
			alert("Velg flyplass");
			window.location = "#flyirute";
			}
			else
			{
			window.location = "#resultat";
			}
	}
	else
	{
		var prestation = document.getElementById("by4");
		var station = document.getElementById("by4").value;
			if (station == '') 
			{
			alert("Velg flyplass");
			window.location = "#flyirute";
			}
			else
			{
			window.location = "#resultat";
			}
	}
	//var adtype = document.getElementById("type").value;
	//var stationname = document.getElementById("by").optionsinnerHTML;
	var stationname = prestation.options[prestation.selectedIndex].text;
	//var element = document.getElementById('destin');
	//element.innerHTML = prestation.options[prestation.selectedIndex].text;
	

	var irute = "/../firiphone2/result_stn.aspx?user=wapuser&language=no&station="+station+"&adtype=a&stationname="+stationname+"&filter="+filter+" #lblresultDesc";
	//alert(irute)
	//alert('Adtype is ' + irute1  + ' and station is ' + station + ' and stationname is '+ stationname +'.');	
	
	$('#include-from-outside').load(irute,
									function()
									{
										$.mobile.silentScroll(0);
										$.mobile.hidePageLoadingMsg();
									});
}

function getIrute2(){
  $('#resultat2').live('pageshow',function(event){ 
 //$.mobile.showPageLoadingMsg();
  });
	var filter = document.getElementById("filter").value;
	//alert(filter);
	if (filter == 'wf')
	{
		var prestation = document.getElementById("by");	
		var station = document.getElementById("by").value;
	}	
	else if (filter == 'sk')
	{
		var prestation = document.getElementById("by3");
		var station = document.getElementById("by3").value;
	}
	else
	{
		var prestation = document.getElementById("by4");
		var station = document.getElementById("by4").value;
	}
	//var adtype = document.getElementById("type").value;
	//var stationname = document.getElementById("by").optionsinnerHTML;
	var stationname = prestation.options[prestation.selectedIndex].text;
	//var element = document.getElementById('destin');
	//element.innerHTML = prestation.options[prestation.selectedIndex].text;
	

	var irute2 = "/../firiphone2/result_stn.aspx?user=wapuser&language=no&station="+station+"&adtype=d&stationname="+stationname+"&filter="+filter+" #lblresultDesc";
	//alert(irute2)
	//alert('Adtype is ' + irute1  + ' and station is ' + station + ' and stationname is '+ stationname +'.');	
	
	$('#include-from-outside2').load(irute2,
									function()
									{
										$.mobile.silentScroll(0);
										$.mobile.hidePageLoadingMsg();
									});
}

function getNews(){	
	$.mobile.showPageLoadingMsg();
	$.ajax({
						 type: "GET",
						 url: "/../rss/Default.aspx?cat=11&n=5",
						 beforeSend:function(){$.mobile.showPageLoadingMsg();},
						 dataType: "xml",
						 success: function(xml) {
						 var news = $('#news');
						 $(xml).find('item').each(function(){
												  var title = $(this).find('title').text();
												  var link = $(this).find('link').text();
												  var description = $(this).find('description').text();
												  $(this).find('pubDate').each(function(){
																			   var pubDate = $(this).text();
																			   news.append("<li class='ui-li ui-li-static ui-body-a'><div id='news_header'>"+ title +"</div><div id='news_desc'>"+ description +"</div><div id='news_date'>"+ pubDate +"</div></li>");
																			   });
												  });
												}
			});
		$.mobile.hidePageLoadingMsg();	
}
function getInfo(){	
	$.mobile.showPageLoadingMsg();
	$.ajax({
						type: "GET",
						url: "/../rss/rssinfo.aspx?cat=780&l=no",
					    dataType: "xml",
						success: function(xml) {
						var info = $('#info');
						$(xml).find('item').each(function(){
													var title = $(this).find('title').text();
													var link = $(this).find('link').text();
													var description = $(this).find('description').text();
												    $(this).find('pubDate').each(function(){
																				var pubDate = $(this).text();
																				info.append("<li class='ui-li ui-li-static ui-body-a'><div id='news_header'>"+ title +"</div><div id='news_desc'>"+ description +"</div></li>");
																				});
													});
												 }
			});
		$.mobile.hidePageLoadingMsg();	
}
  $('#aircraft').live('pageshow',function(event){ 
  $.mobile.showPageLoadingMsg();
  });	
function aircraft(flynr, flytype, site, callback ){
		
		var site = site+"airplanes/show/?theme=wf&l=no&type="+flytype+"&nr="+flynr+"&from=mobWeb";
      //alert(site);
        if ( !site ) {  
            alert('No site was passed.');  
            return false;  
        }  
      
        var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';  
      //alert(yql);
        $.getJSON( yql, function(data) {  
      
        if ( data.results[0] ) {  
            data = data.results[0];  
      //alert(data);
	  $('#showAircraft').html(data);
            if ( typeof callback === 'function') {  
                callback(data);  
            } 
	$.mobile.hidePageLoadingMsg();			
        }  
        else throw new Error('Nothing returned from getJSON.');  


	//var fly = "/../airplanes/show/?theme=wf&l=no&type="+flytype+"&nr="+flynr+"";
	//alert(fly);	
	//$('#showAircraft').load(fly);
	});	

}
  $('#reason').live('pageshow',function(event){ 
  $.mobile.showPageLoadingMsg();
  });	
function reasons(reasonID, site, callback ){
	if(reasonID.length > 2) 
		{
			var ReasonHeading = "Årsak til kanselleringen";
		}
	else
		{
			var ReasonHeading = "Årsak til forsinkelsen";
		}
		var site = site+"Fir/c/reasonelement.aspx?l=no&c="+reasonID+"&e=text";
      //alert(site);
        if ( !site ) {  
            alert('No site was passed.');  
            return false;  
        }  
      
        var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';  
      //alert(yql);
        $.getJSON( yql, function(data) {  
      
        if ( data.results[0] ) {  
            data = data.results[0];  
      //alert(data);
	  $('#reasonHead').empty().append(ReasonHeading);
	  $('#showReason').html(data);
            if ( typeof callback === 'function') {  
                callback(data);  
            }  
			$.mobile.hidePageLoadingMsg();
        }  
        else throw new Error('Nothing returned from getJSON.');  


	//var fly = "/../airplanes/show/?theme=wf&l=no&type="+flytype+"&nr="+flynr+"";
	//alert(fly);	
	//$('#showAircraft').load(fly);
	});	
	
}

//END Cross domain request
//Travellers
function alertBook()
	{

	//alert($("#FIELD_INFANTS_NUMBER").val());
	// Set passengers
	var index = 0;	
	for (var i = 1; i <= 9; i++) {    // Emty TRAVELLER_TYPE
		var fieldName = 'TRAVELLER_TYPE_' + i;
		FPC.elements[fieldName].value = '';
	}	
	for (var i = 1; i <= 5; i++) {    // Emty HAS_INFANT
		var fieldName = 'HAS_INFANT_' + i;
		//FPC.elements[fieldName].value = 'FALSE';
		FPC.elements[fieldName].value = '';

	}	
	
	//var adType
	//if (displayedForm.Vtype){if (displayedForm.Vtype[0].checked) {adType="ADT"} else {adType="YTH"}}
	//	else {
	//	adType="ADT"
	//	}

	for (var i = 1; i <= FPC.FIELD_ADT_NUMBER.value; i++) {
	index += 1;
	var fieldName = 'TRAVELLER_TYPE_' + index;
	FPC.elements[fieldName].value = 'ADT';
	//alert(FPC.elements[fieldName].value);
	}	
	
	for (var i = 1; i <= FPC.FIELD_YTH_NUMBER.value; i++) {
	index += 1;
	var fieldName = 'TRAVELLER_TYPE_' + index;
	FPC.elements[fieldName].value = 'YTH';
	}	

	for (var i = 1; i <= FPC.FIELD_CHD_NUMBER.value; i++) {
	index += 1;
	var fieldName = 'TRAVELLER_TYPE_' + index;
	FPC.elements[fieldName].value = 'CHD';
	}
	
	for (var i = 1; i <= FPC.FIELD_INFANTS_NUMBER.value; i++) {
	var fieldName = 'HAS_INFANT_' + i;
	FPC.elements[fieldName].value = 'TRUE';
	}

}
$(window).unload(function(){ 
 $.mobile.hidePageLoadingMsg(); 
}); 

//END Travellers

