var Searchbar411Validator = function(){this.initialize.apply(this, arguments)};
Searchbar411Validator.prototype = {
  lang : 'fr',
  form_name : 'canoe411_fr',
  searchbar_query : '',
  text : [],
  
  initialize : function(lang, form_name, query) {
    this.lang = lang;
    this.form_name = form_name? form_name : 'canoe411_fr';
    this.searchbar_query = query? query : '';
    this._initText();
  },
  
  bind : function() {
    if ( document.forms[this.form_name] ) {
      var self = this;
  
      with ( document.forms[this.form_name] ) {
        onsubmit = function(){return self._validInfo.call(self);};
        elements['name'].value =  this.searchbar_query == ''? this.text['business'] : this.searchbar_query;
        elements['location'].value =  this.searchbar_query == ''? this.text['location'] : this.searchbar_query;

        elements['name'].onclick = function(){this.value=''; self._emptyInput.call(self);};
        elements['name'].onblur = function(){if(this.value == '') self._fillInput.call(self);};
        elements['name'].onfocus = function(){self._emptyInput.call(self);};
        elements['name'].onkeydown = function(){if(this.value == self.text['business']) {this.value = ''; this.style.color='#000';}};
        elements['location'].onblur = function(){if(this.value == '') self._fillInputLocation.call(self);};
        elements['location'].onfocus = function(){self._emptyInputLocation.call(self);};
      }
    }
  },
      
  _validInfo : function() {
    if ( document.getElementById('enterprises_tab').className == 'current' ) {
      
      //Entreprise      
      if(document.forms[this.form_name].elements['name'].value == "" || document.forms[this.form_name].elements['name'].value == this.text['business']){
        alert(this.text['please enter business']);
        document.forms[this.form_name].elements['name'].focus();
        return false;
      }
      
      if(document.forms[this.form_name].elements['location'].value == "" || document.forms[this.form_name].elements['location'].value == this.text['location']){
        alert(this.text['please enter city']);
        document.forms[this.form_name].elements['location'].focus();
        return false;        
      }
    }
    return true;
  },


  _emptyInput : function() {
    if(document.forms[this.form_name].elements['name'].value != "Business"){
      document.forms[this.form_name].elements['name'].value="";
      document.forms[this.form_name].elements['name'].style.color="black";
    }
  },

  _emptyInputLocation : function() {
    document.forms[this.form_name].elements['location'].value="";
   	document.forms[this.form_name].elements['location'].style.color="black";
  },

  _fillInput : function() {
    if(document.forms[this.form_name].elements['name'].value == ""){
      document.forms[this.form_name].elements['name'].value= this.text['business'];      
      document.forms[this.form_name].elements['name'].style.color="#A5A5A5";
    }
  },
  
  _fillInputLocation : function() {
    if(document.forms[this.form_name].elements['location'].value == ""){
      document.forms[this.form_name].elements['location'].value= this.text['location'];
          document.forms[this.form_name].elements['location'].style.color="#A5A5A5";
    }
  },
  
  _businessText : function() {
    document.forms[this.form_name].elements['name'].value= this.text['business'];
    document.forms[this.form_name].elements['location'].value = this.text['location'];
   	document.forms[this.form_name].elements['name'].style.color="#A5A5A5";
    document.forms[this.form_name].elements['location'].style.color="#A5A5A5";
  },
  
  _initText : function() {
    if ( this.lang == 'en' ) {
      this.text['business'] = 'Business';
      this.text['location'] = "City";
      this.text['please enter city'] = "Enter a city.";
      this.text['please enter business'] = "Enter a business.";
    }
    else {
      this.text['business'] = 'Entreprise';
      this.text['location'] = "Ville"; 
      this.text['please enter city'] = "Veuillez saisir une ville.";
      this.text['please enter business'] = "Veuillez saisir une entreprise.";
    }
  }  
};
