var clsSearchBox = new SearchBoxClass();//create an instance of js class for client page

/*
Author:	Bert O'Neill
Date:	22-Feb-08
Desc:	This class contains all the properties & functionality needed by the widget to performs it's tasks.
*/
function SearchBoxClass() {    
    //properties
    this._INVALID_ENTRY = 'La información introducida no es correcta, por favor corrígala y realice una nueva búsqueda'//Incorrect information was entered, please correct and search again -> spanish
    this._VUELOSBARATOS_URL = "http://www.vuelosbaratos.es/";        
    this._backgroundColourValid = 'white';
    this._backgroundColourInValid = '#FFFF99';//light yellow (microsoft hotmail for e.g.)
    this._regExpDates  = /^(?:(?:31(\/)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/;//validate date pattern (inc. leap year)
    this._regExpCities = /^[a-zA-ZÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜäëïöü¡¿\s.]+$/;//validate the cities\airports entered
    this._todaysDate = new Date();
    
    //references to fnxs.    
    this.GetItemById = GetItemById;
    this.IsPageToBeProcess = IsPageToBeProcess;
    this.FormatDate = FormatDate;
    this.ParseDateStringToArray = ParseDateStringToArray;
    this.ValidateEntries = ValidateEntries;
    this.PreProcessPage = PreProcessPage;
    this.PadWithZero = PadWithZero;
    this.RemoveLeadingZeros = RemoveLeadingZeros;
    
    //functions
    function RemoveLeadingZeros(val){return val.replace(/^0+/, ''); }
    function IsPageToBeProcess() {if (event.keyCode==13){ValidateEntries();}}//enter key pressed        
    function GetItemById(id) {return (document.all ? document.all[id] : document.getElementById(id));}    
    function FormatDate(objDate) {var month = parseInt(objDate.getMonth())+1;return objDate.getFullYear() + '-' + month  + '-' + objDate.getDate();}
    function ParseDateStringToArray(dt){
	    var aryDateParts = dt.split('/');
	    var dateObj = new Object();
	    dateObj.day= (aryDateParts.length > 0 ? parseInt(RemoveLeadingZeros(aryDateParts[0])) : clsSearchBox._todaysDate.getDate());
	    dateObj.month = (aryDateParts.length > 1 ? parseInt(RemoveLeadingZeros(aryDateParts[1]))-1 : (clsSearchBox._todaysDate.getMonth()-1));
	    dateObj.year  = (aryDateParts.length > 2 ? parseInt(RemoveLeadingZeros(aryDateParts[2])) : clsSearchBox._todaysDate.getFullYear());
	    return dateObj;
    }
    function PreProcessPage(){    
        //set default dates       
        var _todaysDate = new Date(); 
        
        var dteFrom = new Date();
        var dteTo = new Date();
        dteFrom.setDate(_todaysDate.getDate() + 7);
        dteTo.setDate(_todaysDate.getDate() + 14);
        
        var yearFrom = dteFrom.getFullYear( );
        var monthFrom = clsSearchBox.PadWithZero(dteFrom.getMonth( ) + 1);
        var dayFrom = clsSearchBox.PadWithZero(dteFrom.getDate());    
        
        var yearTo = dteTo.getFullYear( );
        var monthTo = clsSearchBox.PadWithZero(dteTo.getMonth( ) + 1);
        var dayTo = clsSearchBox.PadWithZero(dteTo.getDate( ));
        
        var dteFrom = clsSearchBox.GetItemById('txtFromDate');
        var dteTo = clsSearchBox.GetItemById('txtToDate');

        dteTo.value = dayTo + "/" + monthTo + "/" + yearTo; 
        dteFrom.value = dayFrom + "/" + monthFrom + "/" + yearFrom; 
    }
    
    function PadWithZero(Number) //pads the digit with a 0 if its less than 10
	{
		if(Number < 10)Number = 0 + "" + Number;
		return Number;
	}

    function ValidateEntries(){
        var dteFrom = GetItemById('txtFromDate');
        var dteTo = GetItemById('txtToDate');
        var oneWayTrip = new Boolean(GetItemById('oneWay').checked);
        var aryFromDate = ParseDateStringToArray(dteFrom.value);    
        var dteFromObj = new Date (aryFromDate.year, aryFromDate.month, aryFromDate.day)    
        var aryToDate = ParseDateStringToArray(dteTo.value);    
        var dteToObj = new Date (aryToDate.year, aryToDate.month, aryToDate.day)           
        var todaysDate = new Date();		
        
        //validate 'from' date not in past (before today)
		if (Date.parse(todaysDate) > Date.parse(dteFromObj)) 
		{			
			alert(clsSearchBox._INVALID_ENTRY);
			return;
		}
        
        //validate 'from' date
        if (!dteFrom.value.match(this._regExpDates))
        {
            alert(clsSearchBox._INVALID_ENTRY);
            dteFrom.focus();
            return;//exit validation fnx.
        }
        
        //validate 'to' date
        if (!dteTo.value.match(clsSearchBox._regExpDates))    
        {
            alert(clsSearchBox._INVALID_ENTRY);
            dteTo.focus();
            return;//exit validation fnx.
        }
                
        //validate dates
        if (dteToObj < dteFromObj || dteFromObj < clsSearchBox._todaysDate.getDate()) 
        { 
            alert(this._INVALID_ENTRY); 
            dteFrom.focus();
            return;    
        }
       
        var txtDepart = GetItemById('txtFrom');
        if (!txtDepart.value.match(clsSearchBox._regExpCities))
        {
            alert(clsSearchBox._INVALID_ENTRY); 
            txtDepart.focus();
            return;
        }
            
        var txtDest = GetItemById('txtTo');
        if (!txtDest.value.match(clsSearchBox._regExpCities))
        {
            alert(clsSearchBox._INVALID_ENTRY); 
            txtDest.focus();
            //txtDest.style.background = clsSearchBox._backgroundColourInValid;
            return;
        }
        
		//validate 'to' and 'from 'city\airport not the same
        if (txtDest.value.toUpperCase() == txtDepart.value.toUpperCase())    
        {
            alert(clsSearchBox._INVALID_ENTRY);
            txtDest.focus();
            return;//exit validation fnx.
        }

        
        var siteUrl = clsSearchBox._VUELOSBARATOS_URL + 'OfertasVuelos/flysearch.aspx?depcity=' + txtDepart.value + '&depIATA=&destIATA=&destcity=' + txtDest.value + '&depart=' + FormatDate(dteFromObj) + '&return=' + FormatDate(dteToObj) + '&adults=1&infants=0&children=0&currency=ES&flway=' + oneWayTrip + '&flight=true&train=false&bus=false&ferry=false';    
        window.open(siteUrl,"_blank");
    }
} 

