/* Javascripts that provide common scripts for KinoKrug forms */

function selectRadio(radioId) {
   $(radioId).checked = "true";
}

function toggleCheckbox(checkboxId) {
   var checkbox = $(checkboxId);
   checkbox.checked = !checkbox.checked;
}

function disableEnterKey(e) {
   var key;
   if(window.event) key = window.event.keyCode; //IE
   else key = e.which; //firefox
   return (key != 13);
}


/* Provides supporting js for input fields, that have title as faded text right in the field when nothing is entered */
var LabeledFieldHandler = Class.create({

   initialize : function(inputFieldId,label) {
      this.label = label;
      this.field = $(inputFieldId);
      this.emptyClass = "labeled_empty";
      this.setClass = "labeled_set";
      // binding event listeners and setting label if initially empty
      this.field.onfocus = this.onLabeledFieldFocus.bind(this);
      this.field.onblur = this.onLabeledFieldBlur.bind(this);
      this.onLabeledFieldLoad();
   },

   onLabeledFieldLoad : function () {
      if(this.field.value.strip() == "") {
         this.field.className = this.emptyClass;
         this.field.value = this.label;
      }
   },

   onLabeledFieldFocus : function () {
      if(this.field.className == this.emptyClass) {
         Field.clear(this.field);
         this.field.className = this.setClass;
      }
   },

   onLabeledFieldBlur : function () {
      if(this.field.value.strip() == "") {
         this.field.className = this.emptyClass;
         this.field.value = this.label;
      }
   },

   onLabeledFieldSubmit : function () {
      if(this.field.className == this.emptyClass) {
         Field.clear(this.field);
         this.field.className = this.setClass;
      }
   },

   updateLabel : function(label) {
      this.label = label;

      if(this.field.className == this.emptyClass) {
         this.field.value = this.label;
      }
   },

   getActualFieldValue : function() {
      if(this.field.className == this.emptyClass) {
         return '';
      } else {
         return this.field.value;
      }
   }

});

