
var Opacity = new Object();
Opacity.set = function(object, opacity) {
object.style.opacity = opacity;
var ieOpacity = Math.round(opacity*100);
object.style.filter = sprintf("progid:DXImageTransform.Microsoft.Alpha(opacity=%s)", ieOpacity);
}


function isPNGHackNeeded() {
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])
return (version > 5.5 && version < 7.0 && document.body.filters);
}

function correctPNG() {
if (!isPNGHackNeeded()) { return; };
for (var i=0; i<document.images.length; i++) {
var img = document.images[i]
var imgName = img.src.toUpperCase()
if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
if (correctPNGNode(img)) { i = i-1; };
}
}
}
function correctPNGNode(node) {
if (!isPNGHackNeeded()) { return false; };
switch (node.tagName) {
case "IMG":
return correctPNGNodeImg(node);
case "INPUT":
if (node.type != "image") { return false; };
return correctPNGNodeInput(node);
default:
return false;
};
if (img.tagName != "IMG") {
return false;
};
}
function correctPNGNodeImg(img) {
if (!isPNGHackNeeded()) { return false; };
var width = (img.width || parseInt(img.currentStyle.width));
var height = (img.height || parseInt(img.currentStyle.height)); 
if (isNaN(width) || isNaN(height)) {
return false;
};  


var newNode = document.createElement("span");
newNode.id = img.id;
newNode.className = img.className;
newNode.title = img.title || img.alt;
newNode.style.cssText = img.style.cssText;
newNode.style.display = "inline-block";
if (img.align == "left") { newNode.style.styleFloat = "left"; };
if (img.align == "right") { newNode.style.styleFloat = "right"; };
newNode.style.width = width.toString() + "px";
newNode.style.height = height.toString() + "px";
newNode.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale')";
newNode.style.cursor = "pointer";
newNode.style.fontSize = "1px";
img.parentNode.replaceChild(newNode, img);
return true;
}
function correctPNGNodeBg(div) {
if (!isPNGHackNeeded()) { return false; };
var fullSrc = div.currentStyle.backgroundImage;
var matches = fullSrc.match(/\(["'](.*)["']\)/);
if (!matches) {
return;
};
var src = matches[1];
div.style.backgroundImage = "none";  
if (div.currentStyle.position != "absolute" && div.currentStyle.position != "relative") {
div.style.position = "relative";
};
var overlay = document.createElement("div");
overlay.style.width = div.offsetWidth.toString() + "px";
overlay.style.height = div.offsetHeight.toString() + "px";
overlay.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + src + "\', sizingMethod='crop')";
var overlayContainer = document.createElement("div");
overlayContainer.style.position = "absolute";
overlayContainer.style.top = div.offsetTop.toString() + "px";
overlayContainer.style.left = div.offsetLeft.toString() + "px";
overlayContainer.style.fontSize = div.currentStyle.fontSize;
if (div.currentStyle.backgroundPositionX != "left") {
overlayContainer.style.paddingLeft = div.currentStyle.backgroundPositionX;
};
if (div.currentStyle.backgroundPositionY != "top") {
overlayContainer.style.paddingTop = div.currentStyle.backgroundPositionY;
};
var newZIndex = div.currentStyle.zIndex - 1;
if (newZIndex >= 0) {
overlayContainer.style.zIndex = newZIndex;
};
overlayContainer.appendChild(overlay);
div.offsetParent.appendChild(overlayContainer);
return true;
}
function correctPNGNodeInput(input) {
if (!isPNGHackNeeded()) { return false; };
if (input.offsetWidth == 0) {
return false;
};  
input.style.position = "relative";
var src = input.src;
var overlay = document.createElement("div");
overlay.style.position = "absolute";
overlay.style.top = (input.offsetTop + 1).toString() + "px";
overlay.style.left = (input.offsetLeft + 1).toString() + "px";
overlay.style.width = (input.offsetWidth - 6).toString() + "px";
overlay.style.height = (input.offsetHeight - 6).toString() + "px";
overlay.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + src + "\', sizingMethod='crop')";
overlay.style.zIndex = input.currentStyle.zIndex - 1;
input.offsetParent.appendChild(overlay);
Opacity.set(input, 0);
return true;
}



page.c.addOnLoadHandler(function(e) {
correctPNG();
});




page.m._layoutSearchKeywordCleared = false;
page.c.addOnLoadHandler(function() {
addEvent($("layout_search_keyword"), "focus", function() {
if (!page.m._layoutSearchKeywordCleared) {
$("layout_search_keyword").value = "";
};
});
if ($("layout_link_back")) { 
addEvent($("layout_link_back"), "click", function(e) {
history.back();
cancelEvent(e);
});
};
});







var Validators = {
POPUP_BOTTOM: 0,
POPUP_TOP: 1,
POPUP_LEFT: 2,
POPUP_RIGHT: 3,
errorPopup: null,
hideCallback: null,
offsetX: 3,
offsetY: 3,
timeout: 3000,
_errorPopupVisible: false
};
Validators.handleError = function(element, text) {
this.showErrorPopup(element, text);
}
Validators.showErrorPopup = function(element, text) {
if (Validators._errorPopupVisible) {
return;
};
try {
element.focus();
textBoxSelect(element, 0, element.value.length);
} catch (e) {


};
Validators.showErrorPopupNoSelect(element, text);
}
Validators.getErrorPopup = function() {
if (this.errorPopup == null) {
this.errorPopup = this.createErrorPopup();
};
return this.errorPopup;
}
Validators.getErrorPopupContent = function() {
if (this.errorPopup == null) {
this.errorPopup = this.createErrorPopup();
};
return this.errorPopup;
}
Validators.createErrorPopup = function() {
var errorPopup = document.createElement('div');
errorPopup.className = 'errorpopup';
document.body.appendChild(errorPopup);
addEvent(errorPopup, 'click', function(e) { Validators.hideErrorPopup(); });
return errorPopup;
}
Validators.hideErrorPopup = function() {
if (this.hideCallback != null) {
clearTimeout(this.hideCallback);
this.hideCallback = null;
};
this.doHideErrorPopup();
this._errorPopupVisible = false;
}
Validators.doHideErrorPopup = function() {
this.getErrorPopup().style.visibility = 'hidden';
}
Validators.showErrorPopupNoSelect = function(element, text) { 
if (this._errorPopupVisible) {
return;
};

this.hideErrorPopup();
try {
element.focus();
} catch (e) {

};
this.updateErrorPopup(text);
this.positionErrorPopup(element);
this.displayErrorPopup();
if (this.timeout) {
this.hideCallback = setTimeout(function() { Validators.hideCallback = null; Validators.hideErrorPopup(); }, this.timeout);
};
this._errorPopupVisible = true;
};
Validators.displayErrorPopup = function() {
var error_item = this.getErrorPopup();
error_item.style.visibility = 'visible';
error_item.style.position = 'absolute';
}
Validators.updateErrorPopup = function(text) {
var error_item = this.getErrorPopupContent();
error_item.innerHTML = text + "<div class='comment'>" + _("Click to Close the Window") + "</div>";
}
Validators.positionErrorPopup = function(element) {
var error_item = this.getErrorPopup();

var popupBase = element;
if (getElementComputedStyle(popupBase, "display") == "none") {
popupBase = $(element.id + "___Frame");
};

var popupPosition = this.POPUP_BOTTOM;
var top;
var left;
switch (popupPosition) {
case this.POPUP_TOP:
left = getLeft(popupBase) + this.offsetX;
top = (getTop(popupBase) - error_item.clientHeight - this.offsetY);
break;
case this.POPUP_BOTTOM:
left = getLeft(popupBase) + this.offsetX;
top = getBottom(popupBase) + this.offsetY;
break;
case this.POPUP_LEFT:
top  = getTop(popupBase);
left = getLeft(popupBase) - 150 - this.offsetX;
break;
case this.POPUP_RIGHT:
top  = getTop(popupBase);
left = getRight(popupBase) + this.offsetX;
break;
};
if (top < 0) { top = 0; };

if (left + 150 > document.body.clientWidth) {
left = document.body.clientWidth - 150;
};
if (left < 0) { left = 0; };
error_item.style.left = left.toString() + 'px';
error_item.style.top  = top.toString() + 'px';
}
String.prototype.subst = function (vars) {
var result = this;
for (var key in vars) {
var ref = "\\${" + key.toString() + "}";
var value = vars[key].toString();
result = result.replace(new RegExp(ref, "g"), value);
};
return result;
}



Validators.string_length = function(element, minlength, maxlength, messages) {


if (element.value == "") {
return true;
};
var vars = { max_length: maxlength == null ? "" : maxlength.toString(), 
min_length: minlength == null ? "" : minlength.toString() };
if (minlength != null && element.value.length < minlength) {
this.handleError(element, messages.too_short.subst(vars));
return false;
}
if (maxlength != null && element.value.length > maxlength) {
this.handleError(element, messages.too_long.subst(vars));
return false;
};
return true;
};
Bindings.prototype = new Object();
Bindings.prototype.add = Bindings_add;
Bindings.prototype.handle = Bindings_handle;
Bindings.prototype.handleGroup = Bindings_handleGroup;
Bindings.prototype.remove = Bindings_remove;
function Bindings() {
this._bindings = {};
this.GROUPS = ['element', 'group', 'all'];
for (var i=0; i<this.GROUPS.length; i++) {
this._bindings[this.GROUPS[i]] = { sequence: [], hash: {} };
};
}
function Bindings_add(fun, group) {
this.remove(fun, group);
this._bindings[group].sequence.push(fun);
this._bindings[group].hash[fun] = this._bindings[group].sequence.length - 1;
}
function Bindings_handle(event) {
var handled = false;
for (var i=0; i<this.GROUPS.length; i++) {
var data = this.handleGroup(this.GROUPS[i], event);
handled = handled || data.handled;
if (data.terminated) {
break;
};
};
if (handled) {

};
}
function Bindings_handleGroup(group, event) {
var binding = this._bindings[group];
if (!binding || binding.sequence.length == 0) { 
return { handled: false, terminated: false };
};
for (var i = binding.sequence.length - 1; i >= 0; i--) {
if (!binding.sequence[i](event)) {
return { handled: true, terminated: true };
};
};
return { handled: true, terminated: false };
}
function Bindings_remove(fun, group) {
var index = this._bindings[group].hash[fun];
if (index == null) {
return;
};
this._bindings[group].sequence.splice(index, 1);
this._bindings[group].hash[fun] = null;
}
var Event = new Object();
Event.addElement = function(eventType, element, handler) {
Event.initBindings(element, eventType);
element._eventBindings[eventType].add(handler, 'element');
};
Event.addGroup = function(eventType, element, handler) {
Event.initBindings(element, eventType);
element._eventBindings[eventType].add(handler, 'group');
};
Event.initBindings = function(element, eventType) {
if (!element._eventBindings) {
element._eventBindings = {};
};
if (!element._eventBindings[eventType]) { 
element._eventBindings[eventType] = new Bindings();
addEvent(element, eventType, function(e) { element._eventBindings[eventType].handle(e); });
};
};



function $F(id) {
if ($(id)) {
if ($(id).getAttribute("multiple")) {
return $FM(id);
};
return $(id).value;
};
if ($(id + "_yy") && 
$(id + "_mm") && 
$(id + "_dd")) {
return $FD(id);
};
}


function $FD(id) {
return sprintf("%s-%02s-%02s", $F(id + "_yy"), $F(id + "_mm"), $F(id + "_dd"));
}


function $FM(id) {
var result = [];
$A($(id).options).each(function(option) {
if (option.selected) {
result.push(option.value);
};
});
return result;
}

Array.prototype.forall = function(callback) {
return this.reduce(function(a,b) { return a && callback(b); }, true);
}



Array.prototype.findFirst = function(predicate) {
for (var i = 0; i < this.length; i++) {
var item = this[i];
if (predicate(item)) {
return item;
};
};
return null;
}





Class.extend(FormControl, Object);
function FormControl(id, initialValue, validators, filters) {
FormControl.baseConstructor.call(this);
this.id = id;
this.initialValue = initialValue;
this.validators = validators;
this.filters = filters;
}
FormControl.prototype.validate = function() {
var validationResult;
if ($(this.id)) {
var oldValue = this.getValue();
this.setValue(this.filter(oldValue));
validationResult = !this.validators.findFirst(function(validator) { return !validator(); });
this.setValue(oldValue);
} else {
validationResult = !this.validators.findFirst(function(validator) { return !validator(); });
};
return validationResult;
}
FormControl.prototype.filter = function(value) {
return this.filters.reduce(function(value, f) { return f.apply(value); }, value);
}
FormControl.prototype.getName = function() {
return $(this.id).name;
}
FormControl.prototype.getValue = function() {
return $(this.id).value;
}
FormControl.prototype.setValue = function(value) {
$(this.id).value = value;
}
Class.extend(FormControlRadio, FormControl);
function FormControlRadio(id, initialValue, ids, validators, filters) {
FormControlRadio.baseConstructor.call(this, id, initialValue, validators, filters);
this.ids = ids;
}
FormControlRadio.prototype.getName = function() {
return $(this.ids[0]).name;
}
FormControlRadio.prototype.getValue = function() {
var checked = this.ids.findFirst(function(id) { return $(id) && $(id).checked; });
if (!checked) return null;
return $(checked).value;
}











Class.extend(InputForm, Object);
function InputForm() {
this._id = null;
this._validatedHandlers = [];
this._controls = [];
this._target = null;
this._targetLoaded = false;
this._submitting = false;
}
InputForm.prototype.getId = function() {
return this._id;
}
InputForm.prototype.addControl = function(control) {
this._controls.push(control);
}
InputForm.prototype.addValidatedHandler = function(handler) {
this._validatedHandlers.push(handler);
}
InputForm.prototype.beforeValidated = function(e) {
if (window.FCKeditorAPI) {
this._controls.each(function(control) {
var editor = FCKeditorAPI.GetInstance(control.id);
if (editor) {
$(control.id).value = editor.GetXHTML(true);
};
});
}
}
InputForm.prototype.getTarget = function() {
return this._target;
}
InputForm.prototype.getSubmitting = function() {
return this._submitting;
}
InputForm.prototype.isModified = function() {
var notModified = this._controls.forall(function(control) { 
return control.initialValue == $F(control.id); 
});
return !notModified;
}
InputForm.prototype.saveState = function() {
this._controls.each(function(control) { 
control.initialValue = $F(control.id);
});
}
InputForm.prototype.validate = function() {
return this._controls.findFirst(function(control) { return !control.validate(); });
}
InputForm.prototype.setId = function(id) {
this._id = id;
}
InputForm.prototype.setSubmitting = function(value) {
this._submitting = value;
}
InputForm.prototype.setTarget = function(target) {
this._target = target;
}
InputForm.prototype.setupEvents = function() {
var form = this;
addEvent($(this.getId()), "submit", function(e) {
page._inputFeatures.each(function(feature) { feature.cleanup(); });
form.setSubmitting(true);
form.beforeValidated(e);

var badField = null;
var forceSend = false;
var status;

badField = form.validate();




if (badField || forceSend) {
form.setSubmitting(false);
page._inputFeatures.each(function(feature) { 
if (!badField || $(badField.id) !== feature._element) {
feature.restore();
};
});
cancelEvent(e);
} else {
form.onValidated(e);
};
});
}
InputForm.prototype.onValidated = function(e) {
this._validatedHandlers.each(function(handler) {
handler(e);
});
}
InputForm.prototype.onTargetLoad = function(e) {
if (!this._targetLoaded) {
this._targetLoaded = true;
return;
};
this.onTargetLoadCustom(e);
}
InputForm.prototype.onTargetLoadCustom = function() {
}
InputForm.prototype.toHash = function() {
var result = this._controls.reduce(function(result, control) { 
result[control.getName()] = control.getValue(); 
return result; 
}, {});
return result;
}



