// Autosuggest configuration ------

// Calendar configuration ------
Date.format = 'dd/mm/yyyy';
$(function()
{
	$('.date-pick').datePicker({
		clickInput:false,
		verticalOffset:22,
		horizontalOffset:2,
		showYearNavigation:false,
		createButton:true
		})
	
	var month=new Array(12);
	month[0]="01";
	month[1]="02";
	month[2]="03";
	month[3]="04";
	month[4]="05";
	month[5]="06";
	month[6]="07";
	month[7]="08";
	month[8]="09";
	month[9]="10";
	month[10]="11";
	month[11]="12";
	
	
	// Date range config
	$('#start-date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			var hidden = selectedDates[0];					
			if (d) {
				d = new Date(d);
				hidden = new Date(hidden);
				$('#end-date').dpSetStartDate(d.addDays(0).asString());
				$('#DOTMonthYear').val(month[hidden.getMonth()] + "-" + hidden.getFullYear());
				$('#DOTDay').val(hidden.getDate());
				$("#departing_error").hide("fast");
			}
		}
	);
});


// Validation configuration ------
function openDepCal(){
		$("#start-date").click();
		return false;
	}

function openReCal(){
		$("#end-date").click();
		return false;
	}

function JourneyTypeSelectionChanged(){
  var jtype = $("#jt").val();
	if (jtype == "R"){
		$("#returnOptions").show("fast");
		//setTimeout('openReCal()',300);
		return false;
		}
    if (jtype == "S" || jtype == "O"){
		$("#returnOptions").hide("fast");
		$('#rm').val("");
		$('#rd').val("");
		$('#end-date').val("DD/MM/YYYY");
		//$('#end-date').dpSetStartDate();
		return false;
		}
}
	
	
function ValidateJP() {

	var vfrom = $("#as_from").val();
	var vto = $("#as_to").val();
	var vfromtext = $("#of").val();
	var vtotext = $("#to").val();
	var voutday = $("#od").val();
	var voutmonth = $("#om").val();
	var vreday = $("#rd").val();
	var vremonth = $("#rm").val();
	var vjourney = $("#jt").val();
	var popular = $("#popular").val();
	var ofdrop = $("#of_drop").val();
	var todrop = $("#to_drop").val();	
	var startdate = $("#start-date").val();
	var enddate = $("#end-date").val();	
	var lenStart = $("#start-date").length;
	var lenEnd = $("#end-date").length;	
	var ilen = 1;
	var defaultValue = "DD/MM/YYYY";
	var jtype = $("#jptype").val();
	
	// Validate autosuggestion feilds 
	if (vfrom == "No Records Found"){
		$("#from_error").show("fast");
	return false;
	}
	if (vto == "No Records Found"){
		$("#from_error").show("fast");
	return false;
	}
	
	if (vfrom == ""){
		$("#from_error").show("fast");
		$("#as_from").select();
		$("#as_from").focus();
		return false;
	}
		else{$("#from_error").hide("fast");}
	
	if (vto == ""){
		$("#to_error").show("fast");
		$("#as_to").select();
		$("#as_to").focus();
		return false;
	}
		else{$("#to_error").hide("fast");}
		
	if (vfrom == vto && vfromtext == vtotext){
		$("#errormessage").show("fast");
		return false;
		}
		else{$("#errormessage").hide("fast");}
	
	if (ofdrop == todrop && vfrom == "from" && vto == "to"){
		$("#errormessage").show("fast");
		return false;
		}
		else{$("#errormessage").hide("fast");}
	
	
	
	
	// Out date validation
	if (startdate == defaultValue){
	    $("#departing_error").show("fast");
	    return false;
	}	
	
	if (voutday == "" && voutmonth == "" && startdate == ""){
		$("#departing_error").show("fast");
		//$("#start-date").click();
		//setTimeout('openDepCal()',600);
		return false;
	}
		else{$("#departing_error").hide("fast");}
		
		
	// Return date validation
	if (enddate == defaultValue && vjourney == "R"){
	    $("#returning_error").show("fast");
	    return false;
	}
		
	if (vreday == "" && vremonth == "" && enddate == "" && vjourney == "R"){
		$("#returning_error").show("fast");
		//setTimeout('openReCal()',600);
		return false;
	}
		else{$("#returning_error").hide("fast");}	
		
	
	// set values for free text input
	if (vfromtext == ""){
		$("#txtfrom").val(vfrom);
		$("#txtto").val(vto);
		$("#jptype").val("own");
	}	
	if (vtotext == ""){
		$("#txtto").val(vto);
		$("#txtfrom").val(vfrom);
		$("#jptype").val("own");
	}
	
	
	if (lenStart >= ilen && voutday == "" && voutmonth == ""){
	    
        var userEntry = $("#start-date").val();
        var userEntry_array = userEntry.split(new RegExp("[/-]"));
        $("#od").val(userEntry_array[0]);

        if(userEntry_array[1] == 1){
        voutmonth = "01";
        }	
        if(userEntry_array[1] == 2){
        voutmonth = "02";
        }	
        if(userEntry_array[1] == 3){
        voutmonth = "03";
        }
        if(userEntry_array[1] == 4){
        $("#om").val("04");
        }
        if(userEntry_array[1] == 5){
        $("#om").val("05");
        }
        if(userEntry_array[1] == 6){
        $("#om").val("06");
        }
        if(userEntry_array[1] == 7){
        $("#om").val("07");
        }
        if(userEntry_array[1] == 8){
        $("#om").val("08");
        }
        if(userEntry_array[1] == 9){
        $("#om").val("09");
        }
        if(userEntry_array[1] == 10){
        $("#om").val("10");
        }
        if(userEntry_array[1] == 11){
        $("#om").val("11");
        }
        if(userEntry_array[1] == 12){
        $("#om").val("12");
        }
	    //return false;
	}	

	
	if (lenEnd >= ilen && vreday == "" && vremonth == "" && vjourney == "R"){
	    
        var userEntryRe = $("#end-date").val();
        var userEntry_arrayRe = userEntryRe.split(new RegExp("[/-]"));
        $("#rd").val(userEntry_arrayRe[0]);

        if(userEntry_array[1] == 1){
        voutmonth = "01";
        }	
        if(userEntry_array[1] == 2){
        voutmonth = "02";
        }	
        if(userEntry_array[1] == 3){
        voutmonth = "03";
        }
        if(userEntry_array[1] == 4){
        $("#om").val("04");
        }
        if(userEntry_array[1] == 5){
        $("#om").val("05");
        }
        if(userEntry_array[1] == 6){
        $("#om").val("06");
        }
        if(userEntry_array[1] == 7){
        $("#om").val("07");
        }
        if(userEntry_array[1] == 8){
        $("#om").val("08");
        }
        if(userEntry_array[1] == 9){
        $("#om").val("09");
        }
        if(userEntry_array[1] == 10){
        $("#om").val("10");
        }
        if(userEntry_array[1] == 11){
        $("#om").val("11");
        }
        if(userEntry_array[1] == 12){
        $("#om").val("12");
        }
	    //return true;
	}
	
	
	
			
	return true;
}


function SubmitJP()
{
	if(ValidateJP())
	{
		return true;
	}
	return false;
}