function StayParams() 
{
    var checkIn, checkOut, destination, persons, rooms;
    
    this.getCheckIn = function() { return checkIn;};
    this.setCheckIn = function(checkInDate) { checkIn = checkInDate;};
    
    this.getCheckOut = function() { return checkOut;};
    this.setCheckOut = function(checkOutDate) { checkOut = checkOutDate;};
    
    this.getDestination = function() { return destination;};
    this.setDestination = function(Dest) { destination = Dest;};
    
    this.getPersons = function() { return persons;};
    this.setPersons = function(PersNum) { persons = PersNum;};
    
    this.getRooms = function() { return rooms;};
    this.setRooms = function(RoomsNum) { rooms = RoomsNum;};
    
    this.isEmptyDate = function(aDate) {
        return aDate == '';
    };
    
}

var NO_ACTION = 0, SEARCH_WA = 1, SEARCH_WOA= 2, ERASE_COOKIES = 3;

function SearchWidget(stayParams)
{
    this.stayParams = stayParams;
    var errors = new Array();
    var action = NO_ACTION;
    var fullTtoday = new Date();
    
    var today = YAHOO.widget.DateMath.getDate(fullTtoday.getFullYear(), 
                                              fullTtoday.getMonth(), 
                                              fullTtoday.getDate());

    var config = new Object();
    
    this.init = function(oConfig) { 
       config = oConfig;
    };

    this.getStayParams = function() { return this.stayParams.getStayParams();};
    this.setStayParams = function(stayParamsObj) { this.stayParams.setStayParams(stayParamsObj);};

    this.getDestinationDefaultText = function() { return SearchWidget.destinationDefaultText;};
    this.setDestinationDefaultText = function(txt) { SearchWidget.destinationDefaultText = txt;};
    
    this.process = function() {
       stayParamsValidation();
       actionChoise(this);
    };

    this.hasErrors = function() { return errors.length!=0; };

    this.getErrors = function() {
        return errors; 
    };
    
    this.getAction = function() { return action;};    
       
    function exceptionRaised(aError) {
        for (var i=0; i<errors.length; i++) {
            if (errors[i] == aError) {
                return true;
            }
        }
        return false;
    }
    
    function setErrors(err)
    {
        errors.push(err);
    }

    function stayParamsValidation() {
       
       if (!Date.isValid(YAHOO.util.Date.format(stayParams.getCheckIn(),{format: config.currentDateFormat.toUpperCase()}),config.toolboxDateFormat)) {
           setErrors('INVALID_CHECKIN_DATE');
       }       
       if (!exceptionRaised('INVALID_CHECKIN_DATE')
           && YAHOO.widget.DateMath.before(YAHOO.widget.DateMath.add(today, YAHOO.widget.DateMath.DAY, config.MAX_CHECKIN_ALLOWED_OFFSET), stayParams.getCheckIn())) {
           setErrors('INVALID_CHECKIN_DATE');
       }
       if (!exceptionRaised('INVALID_CHECKIN_DATE')
           && YAHOO.widget.DateMath.before(stayParams.getCheckIn(), YAHOO.widget.DateMath.add(today, YAHOO.widget.DateMath.DAY, config.MIN_CHECKIN_ALLOWED_OFFSET))) {
           setErrors('CHECKIN_LESS_THAN_TODAY');
       }
       
       if (!Date.isValid(YAHOO.util.Date.format(stayParams.getCheckOut(),{format: config.currentDateFormat.toUpperCase()}),config.toolboxDateFormat)) {
           setErrors('INVALID_CHECKOUT_DATE');
       }
       if (!exceptionRaised('INVALID_CHECKOUT_DATE')
           && YAHOO.widget.DateMath.before(YAHOO.widget.DateMath.add(today, YAHOO.widget.DateMath.DAY, config.MAX_CHECKOUT_ALLOWED_OFFSET), stayParams.getCheckOut())) {
           setErrors('INVALID_CHECKOUT_DATE');
       }
       if (!exceptionRaised('INVALID_CHECKOUT_DATE')
           && !exceptionRaised('INVALID_CHECKIN_DATE')
           && !YAHOO.widget.DateMath.after(stayParams.getCheckOut(),stayParams.getCheckIn())) {
           setErrors('CHECKOUT_LESS_THAN_CHECKIN');
       }
       if (SearchWidget.hasDestination
           && (stayParams.getDestination() == ''
               || stayParams.getDestination() == SearchWidget.destinationDefaultText)) {
               setErrors('DESTINATION_EMPTY');
       }
       if (stayParams.getPersons()=='') {
           setErrors('PERSONS_EMPTY');
       }
       if (stayParams.getRooms()=='') {
           setErrors('ROOMS_EMPTY');
       }
       if (!exceptionRaised('PERSONS_EMPTY')
           && !exceptionRaised('ROOMS_EMPTY')
           && parseInt(stayParams.getRooms())>parseInt(stayParams.getPersons())){
           setErrors('PERSONS_LESS_THAN_ROOMS');
       }
    }
    
    function actionChoise(self) {
        if (stayParams.isEmptyDate(stayParams.getCheckIn())
            && stayParams.isEmptyDate(stayParams.getCheckOut())
            && !exceptionRaised('DESTINATION_EMPTY')) {
                setAction(SEARCH_WOA);
        }

        if (!self.hasErrors()) {
            setAction(SEARCH_WA);
        }

        if ((!SearchWidget.hasDestination || exceptionRaised('DESTINATION_EMPTY'))
            && stayParams.isEmptyDate(stayParams.getCheckIn())
            && stayParams.isEmptyDate(stayParams.getCheckOut())) {
            setAction(ERASE_COOKIES);
        }
    }
   
    function setAction(anAction){
        action = anAction;
    }
    
}

SearchWidget.prototype.hasDestination = true;
SearchWidget.prototype.destinationDefaultText = '';

function SearchFormDAO(aForm)
{
    this.config = new Object();
    var form = aForm;
    var allowedUrlParams;
    
    this.init = function(oConfig) {
      this.config = oConfig;
    };
    
    this.getDestination = function() {
        if (form.destination) {
            return YAHOO.lang.trim(form.destination.value);
        } else {
            return '';
        }
    };

    this.getCheckIn = function() {
        return Date.parseString(form.checkin.value,this.config.toolboxDateFormat);
    };
    
    this.getCheckOut = function() {
        return Date.parseString(form.checkout.value,this.config.toolboxDateFormat);
    };
    
    this.getPersons = function() {
      if (form.pval.selectedIndex !='') {
        return parseInt(form.pval.selectedIndex);
      }
      return form.pval.selectedIndex;
    };
    
    this.getRooms = function() {
      if (form.rval.selectedIndex !='') {
        return parseInt(form.rval.selectedIndex);
      }
      return form.rval.selectedIndex;
    };    

    this.setCheckIn = function(aDate) {
       if (aDate!=null) {
          form.checkin.value = YAHOO.util.Date.format(aDate, {format: this.config.currentDateFormat.toUpperCase()});
          form.checkin.dateValue = aDate;
       }
    };
    
    this.setCheckOut = function(aDate) {
       if (aDate!=null) {                 
          form.checkout.value = YAHOO.util.Date.format(aDate, {format: this.config.currentDateFormat.toUpperCase()});
          form.checkout.dateValue = aDate;
       }
    };
    
    this.setPersons = function(pNo) {
       form.pval.selectedIndex = parseInt(pNo);
    };
    
    this.setRooms = function(rNo) {
       form.rval.selectedIndex = parseInt(rNo);
    };    
    
    this.setAllowedUrlParams = function(allowedUrlParams) {
       this.allowedUrlParams = allowedUrlParams;
    };    
    
        
    this.getGeoId = function() {
        if (form.geoid) {
            return parseInt(form.geoid.value);
        } else {
            return '';
        }
    };
    
    this.getHotelId = function() {
        if (form.htid) {
            return parseInt(form.htid.value,10);
        } else {
            return '';
        }
    };    
    
    this.getLg = function() {
        if (form.lg) {
            return YAHOO.lang.trim(form.lg.value);
        } else {
            return '';
        }
    };
}



function NextAction()
{

}

NextAction.prototype.execute = function() {return false};

NextAction.prototype.init = function(aDAO, params) {return false};


// ------------ search perform action
PerformSearchAction.prototype = new NextAction;

PerformSearchAction.prototype.constructor = PerformSearchAction;

function PerformSearchAction() {}

PerformSearchAction.prototype.init = function(aDAO, actionToDo) {
    this.DAO = aDAO;
    this.actionToDo = actionToDo;
}

PerformSearchAction.prototype.execute = function() {
    if (this.actionToDo == ERASE_COOKIES) {
        EraseCookies();
    } else {
         var q = new Object();
        if (this.actionToDo == SEARCH_WA) {
            q.sd   = padZero(String(this.DAO.getCheckIn().getDate()));
            q.sm   = padZero(String(this.DAO.getCheckIn().getMonth()+1));
            q.sy   = this.DAO.getCheckIn().getFullYear();
            q.ed   = padZero(String(this.DAO.getCheckOut().getDate()));
            q.em   = padZero(String(this.DAO.getCheckOut().getMonth()+1));
            q.ey   = this.DAO.getCheckOut().getFullYear();
            q.pval = this.DAO.getPersons();
            q.rval = this.DAO.getRooms();
        }
        if (this.DAO.getDestination() != '') q.city = this.DAO.getDestination();
        if (this.DAO.getGeoId() != '') q.geoid = this.DAO.getGeoId();
        if (this.DAO.getLg() != '') q.lg = this.DAO.getLg();

        var queryString = buildQueryStr(q);
        window.location = "/search/index.php" + queryString;
    }
};



// ------------ template perform action
HotelPerformSearchAction.prototype = new NextAction;

HotelPerformSearchAction.prototype.constructor = HotelPerformSearchAction;

function HotelPerformSearchAction() {}

HotelPerformSearchAction.prototype.init = function(aDAO, actionToDo, oConfig) {
   
    this.DAO = aDAO;
    this.preSetDAO = new PreSetDAO(oConfig);
    this.actionToDo = actionToDo;
}

HotelPerformSearchAction.prototype.execute = function() {

   if (this.actionToDo == ERASE_COOKIES) {
      EraseCookies();
   } else {
      var q = new Object();
      q['availParams'] = new Object();
      q.availParams.sd   = padZero(String(this.DAO.getCheckIn().getDate()));
      q.availParams.sm   = padZero(String(this.DAO.getCheckIn().getMonth()+1));
      q.availParams.sy   = this.DAO.getCheckIn().getFullYear();
      q.availParams.ed   = padZero(String(this.DAO.getCheckOut().getDate()));
      q.availParams.em   = padZero(String(this.DAO.getCheckOut().getMonth()+1));
      q.availParams.ey   = this.DAO.getCheckOut().getFullYear();
      q.availParams.pval = this.DAO.getPersons();
      q.availParams.rval = this.DAO.getRooms();

      // set cookie
      this.preSetDAO.set_avail_cookies (q.availParams);
            
      q['baseParams'] = new Object();

      q.baseParams.htid = this.DAO.getHotelId();
      q.baseParams.lg = this.DAO.getLg();
      
      // other allowed parameters from url
      var allowedUrlParams = this.preSetDAO.getAllowedUrlParams();
      delete allowedUrlParams.sd;
      delete allowedUrlParams.sm;
      delete allowedUrlParams.sy;
      delete allowedUrlParams.ed;
      delete allowedUrlParams.em;
      delete allowedUrlParams.ey;
      delete allowedUrlParams.pval;
      delete allowedUrlParams.rval;     
      delete allowedUrlParams.lg;
      
      // read htid param from url if undefined in DAO
      if(typeof(q.baseParams.htid) == 'undefined' || q.baseParams.htid =='') {
         q.baseParams.htid = allowedUrlParams.htid;
      }
      
      delete allowedUrlParams.htid;
      
      q['extraParams'] = new Object();
      // add all other parameters to q
      for (var param in allowedUrlParams) {
         q.extraParams[param] = allowedUrlParams[param];
      }      
            
      // load template property and top properties 
      ctrl.getSearchAvailabilities(q);      
                
   }
};
      
NotifyErrorAction.prototype = new NextAction;

NotifyErrorAction.prototype.constructor = NotifyErrorAction;

function NotifyErrorAction() {}

NotifyErrorAction.prototype.errorsMsg = new Array();

NotifyErrorAction.prototype.errors = {INVALID_CHECKIN_DATE: 1, INVALID_CHECKOUT_DATE: 2, CHECKIN_LESS_THAN_TODAY: 3, CHECKOUT_LESS_THAN_CHECKIN: 4,
                                      DESTINATION_EMPTY: 5, PERSONS_EMPTY: 6, ROOMS_EMPTY: 7, PERSONS_LESS_THAN_ROOMS: 8}

NotifyErrorAction.prototype.init = function(aDAO, errorsToNotify) {
   this.DAO = aDAO;
   this.errorsToNotify = errorsToNotify;
}

NotifyErrorAction.prototype.execute = function() {
    var errorStr = '';
    for (var i=0; i<this.errorsToNotify.length; i++) {
        errorStr = errorStr + "\n" + this.errorsMsg[this.errorsToNotify[i]];
    }
    alert(errorStr);
};

function SearchWidgetCalendars(oConfig) {
   var config = oConfig;
   var calCheckIn;
   var calCheckOut;
      
   // Limits range
   var minCheckinAllowed   =  YAHOO.widget.DateMath.add(new Date(), YAHOO.widget.DateMath.DAY, config.MIN_CHECKIN_ALLOWED_OFFSET);
   var minCheckoutAllowed  =  YAHOO.widget.DateMath.add(new Date(), YAHOO.widget.DateMath.DAY, config.MIN_CHECKOUT_ALLOWED_OFFSET);
   var maxCheckinAllowed   =  YAHOO.widget.DateMath.add(new Date(), YAHOO.widget.DateMath.DAY, config.MAX_CHECKIN_ALLOWED_OFFSET);
   var maxCheckoutAllowed  =  YAHOO.widget.DateMath.add(new Date(), YAHOO.widget.DateMath.DAY, config.MAX_CHECKOUT_ALLOWED_OFFSET);      
   var range               = {from: minCheckinAllowed, to: maxCheckoutAllowed};

   this.initCals = function() {
      calCheckIn = init("checkin","checkout",true);
      calCheckout = init("checkout","checkin",false);
   };

   function checkDateRangeUI(type, args, cal) {
     var pagedate = cal.cfg.getProperty("pagedate");
   
     var leftArrow = YAHOO.util.Dom.getElementsByClassName(cal.Style.CSS_NAV_LEFT, null, cal.oDomContainer)[0];
     var rightArrow = YAHOO.util.Dom.getElementsByClassName(cal.Style.CSS_NAV_RIGHT, null, cal.oDomContainer)[0];
     
     if (pagedate.getMonth() == range.from.getMonth() && pagedate.getFullYear() == range.from.getFullYear()) {
         YAHOO.util.Dom.setStyle(leftArrow, "visibility", "hidden");
     } else {
         YAHOO.util.Dom.setStyle(leftArrow, "visibility", "visible");
     }
     if (pagedate.getMonth() == range.to.getMonth() && pagedate.getFullYear() == range.to.getFullYear()) {
         YAHOO.util.Dom.setStyle(rightArrow, "visibility", "hidden");
     } else {
         YAHOO.util.Dom.setStyle(rightArrow, "visibility", "visible");
     }
   };   
   
   function init(self,other,before) {
      var cal = new YAHOO.widget.Calendar("cal"+self,"cal"+self+"Container" );
      cal.renderEvent.subscribe(checkDateRangeUI, cal);      
      cal.cfg.setProperty('mindate', (before)? minCheckinAllowed : minCheckoutAllowed);      
      cal.cfg.setProperty('maxdate', (before)? maxCheckinAllowed : maxCheckoutAllowed);         
      cal.cfg.setProperty("MDY_YEAR_POSITION", config.dateOrder.y);
      cal.cfg.setProperty("MDY_MONTH_POSITION", config.dateOrder.m);
      cal.cfg.setProperty("MDY_DAY_POSITION", config.dateOrder.d);
      cal.cfg.setProperty("MY_LABEL_YEAR_POSITION", config.labelDateOrder.y);
      cal.cfg.setProperty("MY_LABEL_MONTH_POSITION", config.labelDateOrder.m);
      cal.cfg.setProperty("MY_LABEL_YEAR_SUFFIX", config.labelYearSuffix);      
      cal.cfg.setProperty("locale_weekdays", "medium");
      cal.cfg.setProperty("WEEKDAYS_MEDIUM", config.strThreeCharsDays);
      cal.cfg.setProperty("MONTHS_LONG", config.strMonth);      
      cal.elDate = YAHOO.util.Dom.get(self);
      cal.elOtherDate = YAHOO.util.Dom.get(other);
      cal.isBefore = before;
      cal.over = false;
      cal.selectEvent.subscribe(getDate, cal, true);
      cal.renderEvent.subscribe(setupListeners, cal, true);
      YAHOO.util.Event.addListener(cal.elDate, 'focus', showCal,cal);
      YAHOO.util.Event.addListener(cal.elDate, 'change', closeCalAndSyncDates,{currentFormat: config.toolboxDateFormat, cal: cal});
      YAHOO.util.Event.addListener(cal.elDate, 'keypress', closeCalendarByEnterOrTab,cal);
      YAHOO.util.Event.addListener(cal.elDate, 'blur', hideCal,cal);
      YAHOO.util.Event.addListener('img'+cal.elDate.name, 'click', function(){cal.elDate.focus()});
      cal.render();

      return cal;      
   };
      
    function setupListeners(type,args,cal) {
      YAHOO.util.Event.addListener('cal'+cal.elDate.name+'Container', 'mouseover', function (){cal.over = true;});
      YAHOO.util.Event.addListener('cal'+cal.elDate.name+'Container', 'mouseout', function (){cal.over = false;});
   };
   
   function getDate(type,args,cal) {
      var newDate = args[0][0];
      cal.elDate.value = newDate[1]+'/'+newDate[2]+'/'+newDate[0];
      closeCalAndSyncDates(type,{currentFormat: "M/d/yyyy", cal: cal});
   };
   
   function closeCalAndSyncDates(type,args) {
      var cal = args.cal;
      var calDateStr = cal.elDate.value;
      var currentFormat = args.currentFormat;
      
      cal.elDate.dateValue = Date.parseString(calDateStr,currentFormat);
      if (cal.elDate.dateValue!=null) {
         cal.elDate.value = YAHOO.util.Date.format(cal.elDate.dateValue, {format: config.currentDateFormat.toUpperCase()});
         syncDates(cal);
      }
      cal.over = false;
      cal.hide();
   };
   
   function showCal(e,cal) {
      var curDate = Date.parseString(cal.elDate.value,config.toolboxDateFormat);
      
      if (curDate != null) {
         cal.cfg.setProperty('selected', cal.elDate.value);
         cal.cfg.setProperty('pagedate', (curDate.getMonth()+1) + "/" + curDate.getFullYear());
         cal.render();         
      }
      cal.show();
      var xy = YAHOO.util.Dom.getXY(cal.elDate);
      xy[1] = xy[1] + 25;
      YAHOO.util.Dom.setXY('cal'+cal.elDate.name+'Container',xy);
            
   };
   
   function hideCal(e,cal) {
      if (!cal.over) {
         cal.hide(); 
      }
   };
   
   function syncDates(cal) {
      var curDate = cal.elDate.dateValue;
      var curOtherDate = cal.elOtherDate.dateValue;
      if(!curOtherDate || curOtherDate == null) {
         curOtherDate = YAHOO.widget.DateMath.add(curDate, YAHOO.widget.DateMath.DAY, (cal.isBefore)?1:-1);
      } else {
         if(cal.isBefore && !YAHOO.widget.DateMath.before(curDate, curOtherDate)) {
            curOtherDate = YAHOO.widget.DateMath.add(curDate, YAHOO.widget.DateMath.DAY, 1);            
         } else if(!cal.isBefore && !YAHOO.widget.DateMath.after(curDate, curOtherDate)) {
            curOtherDate = YAHOO.widget.DateMath.add(curDate, YAHOO.widget.DateMath.DAY, -1);
         }
      }
      cal.elOtherDate.dateValue = curOtherDate;
      cal.elOtherDate.value = YAHOO.util.Date.format(curOtherDate, {format: config.currentDateFormat.toUpperCase()});
   };
}

function PreSetDAO(oConfig)
{   
    this.urlParams = getQueryStringArgs(oConfig);
    
    var dateFormat = 'dd/M/yyyy';
    
   
    // accept date with 2 digits
    this.formatDayMonthIsoDate = function(num) {
       if ( !isNaN(num) && (num%1==0) && (num.length == 1) ) {
          return '0'+num;
       }
       return num;
    }
    
    
    this.getCheckIn = function() {
       if (this.urlParams.sd && this.urlParams.sm && this.urlParams.sy) {
          
          var smy = this.formatDayMonthIsoDate(this.urlParams.sm) + "/" + this.urlParams.sy;
          return Date.parseString((this.formatDayMonthIsoDate(this.urlParams.sd) + "/" + smy),dateFormat);
       }
       
       return Date.parseString(getCookie("savail[_sd]") + "/" + getCookie("savail[_smy]"),dateFormat);
    };
    
    this.getCheckOut = function() {
       if (this.urlParams.ed && this.urlParams.em && this.urlParams.ey) {
          var emy = this.formatDayMonthIsoDate(this.urlParams.em) + "/" + this.urlParams.ey;
          return Date.parseString((this.formatDayMonthIsoDate(this.urlParams.ed) + "/" + emy),dateFormat);
       }
       return Date.parseString(getCookie("savail[_ed]") + "/" + getCookie("savail[_emy]"),dateFormat.replace('mm','M'));
    };
    
    this.getPersons = function() {
       if (this.urlParams.pval) {
          return parseInt(this.urlParams.pval);
       }
       else if (getCookie("savail[_pv]")) {
          return parseInt(getCookie("savail[_pv]"));
       } else {
          return 2;
       }
    };
    
    this.getRooms = function() {
       if (this.urlParams.rval) {
          return parseInt(this.urlParams.rval);
       }
       else if (getCookie("savail[_rv]")) {
          return parseInt(getCookie("savail[_rv]"));
       } else {
          return 1;
       }
    };    
    
    this.getAllowedUrlParams = function() {
       return this.urlParams;
    };  
        
   

    this.setCheckIn = function(aDate) {
        
    };
    
    this.setCheckOut = function(aDate) {
        
    };
    
    this.setPersons = function(pNo) {

    };
    
    this.setRooms = function(rNo) {

    };

    
   function getCookie(cookie_name) {
      var search = cookie_name + "=";
      if (document.cookie.length > 0) { // if there are any cookies
         offset = document.cookie.indexOf(search);
         if (offset != -1) { // if cookie exists
            offset += search.length;
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset) ;
            // set index of end of cookie value
            if (end == -1){
               end = document.cookie.length;
            }
            return unescape(document.cookie.substring(offset, end));
         }
      }
   }
   
    this.set_avail_cookies = function(obj) {
      sd = obj.sd;
      smy = obj.sm + "/" + obj.sy;
      ed = obj.ed;
      emy = obj.em + "/" + obj.ey; 
      pv = obj.pval;
      rv = obj.rval;
      
      var str_sd = ""+sd;
      var str_ed = ""+ed;
      
      if (str_sd.length == 1)
         sd = '0'+sd;
      if (str_ed.length == 1)
         ed = '0'+ed;
      if (smy.length == 6)
         smy = '0'+smy;
      if (emy.length == 6)
         emy = '0'+emy;
         
         
      now = new Date();
      expire = new Date();
      expire.setHours(now.getHours()+1);
      expire.setMinutes(now.getMinutes());
      expire.setSeconds(now.getSeconds());
      
      setVenereCookie("savail[_sd]", sd, expire );
      setVenereCookie("savail[_smy]", smy, expire );
      setVenereCookie("savail[_ed]", ed, expire );
      setVenereCookie("savail[_emy]", emy, expire );
      setVenereCookie("savail[_pv]", pv, expire );
      setVenereCookie("savail[_rv]", rv, expire );
   }
      
   /**
   * return all allowed <parameter,value> couples from url
   */
   function getQueryStringArgs(oConfig) {
      var args = new Object();
      var query = window.location.search.substring(1);
      var pairs = query.split("&");
      for (var i=0; i<pairs.length; i++) {
         var pos = pairs[i].indexOf("=");
         if (pos == -1) {
            continue;
         }
         var argName = pairs[i].substring(0, pos);
         var value = pairs[i].substring(pos+1);
         if (typeof(oConfig.allowedUrlParameters[argName]) == "undefined") {
            continue;
         }
         value = decodeURIComponent(value);
         args[argName] = value;
      }
      
      return args;
   }   
}

function doSubmitSW() { 
    
    var isExecutedSuccessAction = false;
    
    var aDAO = searchWidgetConfig.formDao;
    var successAction = searchWidgetConfig.performAction;
    var errorAction = searchWidgetConfig.errorAction;
    aDAO.init(searchWidgetConfig);
    var sp = new StayParams;
    sp.setCheckIn(aDAO.getCheckIn());
    sp.setCheckOut(aDAO.getCheckOut());
    sp.setDestination(aDAO.getDestination());
    sp.setPersons(aDAO.getPersons());
    sp.setRooms(aDAO.getRooms());
    
    
    var sw = new SearchWidget(sp);
    sw.init(searchWidgetConfig);
    sw.process();
    if (sw.hasErrors()) {  
        errorAction.init(aDAO, sw.getErrors());
        toDo = errorAction;
        isExecutedSuccessAction = false;
    } else {    
        successAction.init(aDAO, sw.getAction(), searchWidgetConfig);
        toDo = successAction;
        isExecutedSuccessAction = true;
        
        // tracking
        if (typeof(window.track_hotel_search_widget) == 'function') {
        	   window.track_hotel_search_widget(sp);
        }
    }
    
    toDo.execute();
    
    return isExecutedSuccessAction;
}

function submitByEnter(e){ //e is event object passed from function invocation
   var characterCode = e.keyCode;

   if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
      doSubmitSW(); //submit the form
   }
}

function closeCalendarByEnterOrTab(e,cal){ //e is event object passed from function invocation
   var characterCode = e.keyCode;

   if(characterCode == 13 || characterCode==09){ //if generated character code is equal to ascii 13 (if enter key) or ascii 09 (if tab key)
      cal.over = false;
      cal.hide();
   }
}
