/* PFrameworkCS API Copyright (C) 2008 by Gregor Kališnik <gregor@unimatrix-one.org> Copyright (C) 2008 by Unimatrix-One (www.unimatrix-one.org) This javascript is part of the PFramework CMS system. Any unauthorised use outside of this system is strictly prohibited.*/var globalTimeout = undefined;var PInputField = Class.create({initialize: function (id){this.field = $(id);this.id = id;this.image = $(id + '_image');this.errorText = new Element('div',{'style' : 'display: none;', 'id': id + '_msg'}); this.image.up(0).insert(this.errorText);this.image.writeAttribute('gtip', '#' + id + '_msg');this.tip = new gTip(this.field.id + '_image', 'right');var tip = this.tip;var image = this.image;this.field.onmouseover = function(){if (image.visible()) tip.show(image);}; this.field.onmouseout = function(){if (image.visible()) $$('.gtip-container').invoke('hide');}; this.setNormal();}, focus: function(){this.field.focus();}, setActions: function(actions){if (actions == undefined) return;if (actions.onkeyup != undefined) this.field.onkeyup = actions.onkeyup;if (actions.onblur != undefined) this.field.onblur = actions.onblur;}, setError: function(text, show){if (this.errorOutput != undefined) this.errorOutput.pushMessage(this.field.id, text, this.wrongClassName);if (this.image != undefined){this.image.src = '/images/' + PCookieManager.value('PSettings')['skin'] + '/status_icons/wrong.gif'; this.image.show();this.setTooltip(pi18n('Wrong'), text, 'wrong', show);} this.field.className = 'wrong';}, setWarning: function(text){if (this.errorOutput != undefined) this.errorOutput.pushMessage(this.field.id, text, this.warningClassName);if (this.image != undefined){this.image.src = '/images/' + PCookieManager.value('PSettings')['skin'] + '/status_icons/warning.gif'; this.image.show();this.setTooltip(pi18n('Warning'), text);} this.field.className = 'warning';}, setLoading: function(text){if (this.errorOutput != undefined) this.errorOutput.pushMessage(this.field.id, text, this.warningClassName);if (this.image != undefined){this.image.src = '/images/' + PCookieManager.value('PSettings')['skin'] + '/status_icons/w8ing3.gif'; this.image.show();this.setTooltip(pi18n('Loading'), text);} this.field.className = 'warning';}, setCorrect: function(text){if (this.errorOutput != undefined) this.errorOutput.removeMessage(this.field.id);if (this.image != undefined){this.image.src = '/images/' + PCookieManager.value('PSettings')['skin'] + '/status_icons/correct.gif';; this.image.show();this.setTooltip(pi18n('Correct'), text, 'correct');} this.field.className = 'correct';}, setNormal: function(text, show){if (this.errorOutput != undefined) this.errorOutput.removeMessage(this.field.id);if (text == undefined || !text.length){this.image.hide();}else{ this.setTooltip(pi18n('Note'), text, undefined, show);this.image.src = '/images/' + PCookieManager.value('PSettings')['skin'] + '/status_icons/normal.png';; this.image.show();} this.field.className = 'normal';}, disable: function(){this.field.disable();}, clear: function(){this.field.clear();}, enable: function(){this.field.enable();}, getValue: function(){return this.field.getValue();}, present: function(){return this.field.present();}, isCorrect: function(){return this.field.hasClassName('correct');}, isNormal: function(){return this.field.hasClassName('normal');}, setTooltip: function(title, text, typeClass, show){if (globalTimeout != undefined) clearTimeout(globalTimeout);$$('.gtip-container').invoke('hide');this.errorText.update(text);this.image.writeAttribute('title', title);this.tip.setup(this.image, typeClass);if (show == undefined || show == true) this.tip.show(this.image);globalTimeout = setTimeout(function(){$$('.gtip-container').invoke('hide');globalTimeout = undefined;}, text != undefined && text.length != 0 ? Math.log(text.length + 3) * 1000 : 1500);}});var PInputVerifyField = Class.create(PInputField,{initialize: function($super, id){this.verifyField = $(id + '_verify');$super(id);if (this.verifyField == undefined) alert('BUG! No verify field defined for ' + id + ' !');this.verifyField.onmouseover = this.field.onmouseover;this.verifyField.onmouseout = this.field.onmouseout;}, setError: function($super, text, show){$super(text, show);this.verifyField.className = 'wrong';}, setWarning: function($super, text){$super(text);this.verifyField.className = 'warning';}, setLoading: function($super, text){$super(text);this.verifyField.className = 'warning';}, setCorrect: function($super, text){$super(text);this.verifyField.className = 'correct';}, setNormal: function($super, text, show){$super(text, show);this.verifyField.className = 'normal';}, clear: function($super){this.field.clear();this.verifyField.clear();this.setNormal();}, disable: function($super){$super();this.verifyField.clear();this.verifyField.disable();}, setActions: function($super, actions){$super(actions);if (actions.onkeyup != undefined) this.verifyField.onkeyup = actions.onkeyup;if (actions.onblur != undefined) this.verifyField.onblur = actions.onblur;var field = this.field;var verifyField = this.verifyField;this.field.onchange = function(){if (field.present()){verifyField.enable();}else{ verifyField.disable();field.focus();} verifyField.clear();if (actions.onchange != undefined) actions.onchange();}; this.field.onkeyup = function(){if (field.present()){verifyField.enable();}else{ verifyField.disable();field.focus();} verifyField.clear();if (actions.onkeyup != undefined) actions.onkeyup();};}, isValid: function(){return this.field.getValue() == this.verifyField.getValue();}, isVerifiable: function(){return this.field.present() && this.verifyField.present();}});var PInputPasswordField = Class.create(PInputVerifyField,{initialize: function($super, id, barWidth){$super(id);if ($(id + '_scorebar') != undefined){this.scoreBar = $(id + '_scorebar');this.maxWidth = barWidth;}}, checkStrength: function(){if (this.scoreBar == undefined) return;var score = chkPass(this.field.getValue(), this.field.id);var level;if (score <= 20) level = pi18n('Very weak');else if (score > 20 && score <= 40) level = pi18n('Weak');else if (score > 40 && score <= 60) level = pi18n('Medium');else if (score > 60 && score <= 80) level = pi18n('Strong');else if (score > 80) level = pi18n('Very strong');var width = (this.maxWidth / 100) * score;this.scoreBar.setStyle({width: width + 'px'}); this.scoreBar.update(level);}});var PRadioButtonGroup = Class.create({initialize: function(id){this.id = id;this.radioButtons = new Object();this.onchange = function(){};}, addRadioButton: function(radioButton){this.radioButtons[radioButton.id] = radioButton;radioButton.setupButton(this);}, selectButton: function(id){var array = Object.values(this.radioButtons);var count = array.length;for (var i = 0;i < count;i++){if (array[i].id == id) array[i].setChecked(true);else array[i].setChecked(false);} this.onchange(this.radioButtons[id].getValue());}, getValue: function(){var array = Object.values(this.radioButtons);var count = array.length;for (var i = 0;i < count;i++){if (array[i].isChecked() == true) return array[i].getValue();}}, setActions: function(actions){this.onchange = actions.onchange;}, disable: function(){}, enable: function(){}});var PRadioButton = Class.create({initialize: function(id){this.id = id;this.radioButton = $(id);this.radioGroup = undefined;}, getValue: function(){return this.radioButton.getValue();}, group: function(){return this.radioGroup;}, setupButton: function(radioGroup){this.radioGroup = radioGroup;var id = this.id;this.radioButton.onclick = function(){radioGroup.selectButton(id);};}, setChecked: function(checked){this.radioButton.checked = checked;}, isChecked: function(){return this.radioButton.checked;}, disable: function(){this.radioButton.disable();}, enable: function(){this.radioButton.enable();}});var PSelectInput = Class.create({initialize: function(id){this.id = id;this.select = $(id);}, setActions: function(actions){if (actions.onchange != undefined) this.select.onchange = actions.onchange;}, getValue: function(){return this.select.getValue();}});var PForm = Class.create({initialize: function(id, barWidth){this.elements = Array();this.form = $(id);var elements = this.form.select('input');var elementsCount = elements.length;for (var i = 0;i < elementsCount;i++){var element = elements[i];var temp;if (element.id.indexOf('_verify') != -1 || element.id.indexOf('_image') != -1) continue;if (element.readAttribute('type') == 'text' || element.readAttribute('type') == 'search'){if ($(element.id + '_image') == undefined){temp = element;}else if ($(element.id + '_verify') != undefined){ temp = new PInputVerifyField(element.id);}else{ temp = new PInputField(element.id);}}else if (element.readAttribute('type') == 'password'){ if ($(element.id + '_image') == undefined && $(element.id + '_verify') == undefined){temp = element;}else{ temp = new PInputPasswordField(element.id, barWidth);}}else if (element.readAttribute('type') == 'radio'){ var group = element.readAttribute('group');if (group == undefined){alert('BUG: Element ' + element.id + ' does not have a defined group!');return;} if (this[group + 'Group'] == undefined) this[group + 'Group'] = new PRadioButtonGroup(group);temp = new PRadioButton(element.id);this[group + 'Group'].addRadioButton(temp);}else{ temp = element;} this[element.id + 'Field'] = temp;this.elements.push(temp);} elements = this.form.select('select');elementsCount = elements.length;for (var i = 0;i < elementsCount;i++){this[elements[i].id + 'Field'] = new PSelectInput(elements[i].id);}}, setSubmitFunction: function(onsubmit, buttonClass){if ($(onsubmit).submit){var formName = onsubmit;onsubmit = function(e){if (Event.isLeftClick(e) || (!Event.isLeftClick(e) && e.keyCode == Event.KEY_RETURN)){$(formName).submit();}};}else{ var submitFunction = onsubmit;onsubmit = function(e){if (Event.isLeftClick(e) || (!Event.isLeftClick(e) && e.keyCode == Event.KEY_RETURN)){submitFunction(e);}};} try{this.form.select('.' + buttonClass)[0].observe('click', onsubmit);}catch (e){} var inputs = this.form.select('.submitField');for (var i = inputs.length - 1;i >= 0;i--){inputs[i].observe('keyup', onsubmit);}}, parseData: function(){var data = new Object();var elements = this.form.select('input');elements = elements.concat(this.form.select('select'));var elementsCount = elements.length;for (var i = 0;i < elementsCount;i++){var element = this[elements[i].id + 'Field'];if (element == undefined) continue;if (element.isValid != undefined){if (element.isValid()){data[element.id] = element.getValue();data[element.id + '_verify'] = $(element.id + '_verify').getValue();}else{ return false;}}else if (element.group != undefined){ group = element.group();data[group.id] = group.getValue();}else{ data[element.id] = element.getValue();}} return data;}, disable: function(){var count = this.elements.length;for (var i = 0;i < count;i++) this.elements[i].disable();}, enable: function(){var count = this.elements.length;for (var i = 0;i < count;i++) this.elements[i].enable();}});
