var App_Alias = new function() { var _options = {}; var _alias = null; var _source = null; var _buildAlias = function (value) { if (!value || value == '') { return '' } return value.replace(/^\s+/, '').replace(/\s+$/, '').replace(/&+/, '-and-') .replace(/[^\sA-Za-z0-9_\.-]/g, '').replace(/\s+/g, '-').replace(/-{2,}/g, ''); } var _onSourceBlur = function () { if (_source.value != '' && (_alias.value == '' || _alias.isFromSource)) { _alias.isFromSource = true; _alias.value = _buildAlias(_source.value); } } var _onAliasBlur = function () { var alias = _buildAlias(_source.value); if (alias == _alias.value) { _alias.isFromSource = true; } else { _alias.isFromSource = false; } } var _addEvent = function (obj, type, func) { if ('object' != typeof obj && !(obj = document.getElementById(obj))) { return; } if (obj.addEventListener) { obj.addEventListener(type, func, false); } else if (obj.attachEvent) { var f = function(e) {func.call(obj, e);} obj.attachEvent('on' + type, f); } } var _init = function (o) { if (!o || !o.alias || !o.source) { throw 'App_Alias: "alias" and "source" must be defined in App_Alias.init()'; } _options = o; _source = document.getElementById(_options.source); _alias = document.getElementById(_options.alias); if (!_alias || !_source) { return false; // throw 'App_Alias: Can not find element with id "'+_options.alias+'" or "'+_options.source+'"'; } _addEvent(_source, 'blur', _onSourceBlur); _addEvent(_alias, 'blur', _onAliasBlur); } function _construct() { this.init = _init; } return new _construct(); }