jQuery.fn.enable = function() {
  return this.each(function() {
    this.disabled = false;
  });
};

function Global() {
  this.ckeditorChange = 0;
  this.fieldHighlightColor = '#FFFF00';
  this.fieldHighlightColorClear = '#FFFFFF';

  this.submit = function(id) {
    $('#'+id).submit();    
  }

  this.validateFields = function(fields) {
    var errorArray = new Array();
    for (var i=0; i < fields.length; i++) {
      var check = true;
      try { check = this.validateSingleField(fields[i]); } catch (e) { check = true; }
      if (!check) {
        errorArray[errorArray.length] = fields[i];
      }
    }
    return errorArray;
  }
  
  this.validateSingleField = function(id) {
    if (this.isSelect(id)) {
      var selected = $('select#'+id).val();
      if (selected <= -1) {
        return false;
      }
    } else if (this.isRadio(id)) {
      var radioChecked = false;
      $('input[name='+id+']').each(function(){
      	if (this.checked) {
      	  radioChecked = true;
      	}
      });
      return radioChecked;
    } else {
      if (undefined != $('#'+id).attr('isNumber') && $('#'+id).attr('isNumber') == 'true') {
        return this.validateNumber(id);
      } else if ($('#'+id).val().length == 0) {
        return false;
      }
    }
    return true;
  }
  
  this.validateNumber = function(id) {
    var number;
    try {
        number = new Number($('#'+id).val());
    } catch (e) {
        // alert(e); 
    }
    if (isNaN(number)) {    // is this a human/number
        $('#'+id).val('');
        return false;
    } else {    // are we a good number
        if (number <= 0) {
            $('#'+id).val('');
            return false;
        }
    }
    return true;
  }
  
  this.isRadio = function(id) {
    if ($('#'+id).attr('type') == 'radio') {
      return true;
    }
    return false;
  }
  
  this.isCheckbox = function(id) {
    if ($('#'+id).attr('type') == 'checkbox') {
      return true;
    }
    return false;
  }
  
  this.isSelect = function(id) {
    var value = $('select#'+id).val();
    if (undefined != value) {
      return true;
    }
    return false;
  }
  
  this.resetErrors = function(fields, errorMessageID) {
    $('#'+errorMessageID).html('');
    for (var i=0; i < fields.length; i++) {
      if ($('#'+fields[i]).attr('type') == 'radio') {
      	try { $('#'+fields[i]+'Bg').css('background-color', this.fieldHighlightColorClear); } catch (e) { }
      } else {
        try { $('#'+fields[i]).css('background-color', this.fieldHighlightColorClear); } catch (e) { }
      }
    }
  }
  
  this.processErrors = function(errorArray, errorMessage, errorMessageID) {
    $('#'+errorMessageID).html(errorMessage);
    for (var i=0; i < errorArray.length; i++) {
      if ($('#'+errorArray[i]).attr('type') == 'radio') {
      	$('#'+errorArray[i]+'Bg').css('background-color', this.fieldHighlightColor);
      	continue;
      }
      $('#'+errorArray[i]).css('background-color', this.fieldHighlightColor);
    }    
  }
  
  this.getLoader = function(message, target) {
    $.get("/loader.jsp?loaderMessage="+message, function(data) {
      $('#'+target).html($.trim(data));
    });
  }  
  
  this.getLoaderImageOnly = function(target) {
    $.get('/loaderImageOnly.jsp', function(data) {
      $('#'+target).html($.trim(data));
    });
  }

  this.changeClass = function(obj, className) {
    document.getElementById(obj).className = className;
  }
  
  this.formatTableRows = function(tableId, classOdd, classEven) {  // Use this function on tables that have the bg color in the rows <tr>
    if (undefined == classOdd || null == classOdd) { classOdd = 'dataTableRow2a'; } // set optional classOdd value
	if (undefined == classEven || null == classEven) { classEven = 'dataTableRow1a'; }  // set optional classEven value
	var tableObj = $('#'+tableId+' tr').each(function(i) {
  	  if (i > 0) {
	    if (this.className == classOdd || this.className == classEven) { // only effect rows with the odd or even CSS classes
	      if (i % 2 == 0) { // even
	        this.className = classEven;
	        $('#'+this.id).mouseover(function() { $G.changeClass(this.id,'dataTableRowOver'); });
	        $('#'+this.id).mouseout(function() { $G.changeClass(this.id,classEven); });
	      } else { // odd
	        this.className = classOdd;
	        $('#'+this.id).mouseover(function() { $G.changeClass(this.id,'dataTableRowOver'); });
	        $('#'+this.id).mouseout(function() { $G.changeClass(this.id,classOdd); });
	      }
	    }
   	  }
    });
  }
  
  this.execFunctionArray = function() {
  	if (undefined!= funcArray && null != funcArray && funcArray.length > 0) {
  		for (var i=0; i < funcArray.length; i++) {
  			// alert(funcArray[i]);
  			var t = setTimeout(funcArray[i], 0);
  		}
  		funcArray = new Array();
  	}
  }
  
  this.isIE6_7 = function() {
    return this.isIE([6, 7]);
  }
  
  this.isIE = function(version) {
    if ($.browser.msie) {
      if (undefined == version) {
        return true;
      } else {
        for (var i=0; i < version.length; i++) {
          if ($.browser.version.charAt(0) == version[i]) {
            return true;
          }
        }
      }
    }
    return false;
  }

  this.isIE6 = function() {
    if ($.browser.msie && $.browser.version.charAt(0) == '6') {
      return true;
    }
    return false;
  }
  
}

var $G = new Global();
