﻿//Javascript Modal window based on script by http://slayeroffice.com/
//Custom Alert Demonstration
//Last build: 19/10/2008
//Copyright by Mahdi Yousefi,2008 All rights reserved, Tini.IR
//Use free => "just if do not remove my name: Mahdi Yousefi"
var modal = function(name, ALERT_TITLE, Ok_Text, Cancel_Text, contentId, widthStyle, maxBtn) {
    this.title = ALERT_TITLE;
    this.button = Ok_Text;
    this.buttonClose = Cancel_Text;
    this.id = contentId;
    this.html = "&nbsp;";
    this.htmlMode = false;
    this.width = widthStyle;
    this.name = name;
    this.maxBtn = maxBtn;
};
modal.back = null;
modal.alert = null;
modal.header = null;
modal.content = null;
modal.OK = null;
modal.isMax = false;
modal.Cancel = null;
modal.getModal = function() {
    var d = document;
    modal.back = d.getElementById("modalContainer");
    modal.alert = d.getElementById("alertBox");
};
modal.prototype.getHtml = function() {
    if (!this.htmlMode) {
        this.htmlMode = true;
        this.html = document.getElementById(this.id).innerHTML;
        document.getElementById(this.id).innerHTML = "";
    };
    return this.html;
};
modal.prototype.setHtml = function(ct) {
    this.htmlMode = true;
    this.htmlMode = ct;
};
modal.max = function() {
    if (modal.isMax) {
        modal.alert.style.marginTop = modal.top;
        modal.alert.style.left = modal.left;
        modal.alert.style.width = modal.width + "px";
        modal.alert.style.height = modal.height + "px";
        modal.isMax = false;
    } else {
        modal.top = modal.alert.style.marginTop;
        modal.left = modal.alert.style.left;
        modal.width = modal.alert.offsetWidth;
        modal.height = modal.alert.offsetHeight;
        modal.alert.style.marginTop = "0px";
        modal.alert.style.left = "0px";
        modal.alert.style.width = modal.clientWidth() + "px";
        modal.alert.style.height = (modal.clientHeight() - 5) + "px";
        modal.isMax = true;
    };
};
modal.prototype.setPrameter = function(showIt, content) {
    modal.getModal();
    var d = document;
    if (showIt) {
        modal.back.style.display = "block";
    };
    modal.back.style.height = document.documentElement.scrollHeight + "px";
    modal.OK.innerHTML = this.button;
    modal.OK.href = 'javascript:' + this.name + '.remove(true)';
    modal.OK.style.display = (this.button) ? "block" : "none";
    modal.Cancel.innerHTML = this.buttonClose;
    modal.Cancel.href = 'javascript:' + this.name + '.remove(false)';
    modal.Cancel.style.display = (this.buttonClose) ? "block" : "none";
    modal.header.innerHTML = '<a class="modal-close" href="' + modal.Cancel.href + '">&nbsp;</a>';
    if (this.maxBtn)
        modal.header.innerHTML += '<a class="modal-max" href="javascript:modal.max()">&nbsp;</a>&nbsp;';
    modal.header.innerHTML += this.title;
    modal.content.innerHTML = content;
    if (this.width)
        modal.alert.style.width = this.width + "px";
    modal.alert.style.height = "auto";
    var tp = ((modal.clientHeight() - modal.alert.offsetHeight) / 2);
    if (tp < 0)
        tp = 50;
    if (modal.isIE6()) {
        tp += modal.scrollTop();
    };
    modal.alert.style.marginTop = tp + "px";
    modal.alert.style.left = (d.documentElement.scrollWidth - modal.alert.offsetWidth) / 2 + "px";
};
modal.prototype.create = function() {
    var d = document;
    var ct = this.getHtml();
    if (d.getElementById("modalContainer")) {
        this.setPrameter(true, ct);
        return;
    };
    modal.back = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
    modal.back.id = "modalContainer";
    modal.alert = modal.back.appendChild(d.createElement("div"));
    modal.alert.id = "alertBox";
    modal.header = modal.alert.appendChild(d.createElement("h1"));
    modal.header.id = "alertBoxTitle";
    modal.content = modal.alert.appendChild(d.createElement("div"));
    modal.content.id = "alertBoxHolder";
    modal.OK = modal.alert.appendChild(d.createElement("a"));
    modal.OK.id = "closeBtn";
    modal.Cancel = modal.alert.appendChild(d.createElement("a"));
    modal.Cancel.id = "endBtn";
    var divClear = modal.alert.appendChild(d.createElement("div"));
    divClear.className = "modalClear";
    this.setPrameter(true, ct);
};
modal.isIE6 = function() {
    var ie6 = false;
    if (window.navigator.appName && window.navigator.userAgent) {
        ie6 = (window.navigator.appName.indexOf('Internet Explorer') > 0);
        ie6 = (ie6 && (window.navigator.userAgent.split(/;/)[1].indexOf('MSIE 6') > 0));
    };
    return ie6;
};
modal.prototype.show = function() {
    this.create();
    if (this.onshow) {
        this.onshow();
    };
};
modal.prototype.remove = function(e) {
    if (modal.isMax) {
        modal.max();
    };
    var closeIt = true;
    if (this.onclose)
        closeIt = this.onclose(e);
    if (closeIt) {
        modal.back.style.display = "none";
    };
};
modal.clientHeight = function() {
    return modal.filterResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
};
modal.clientWidth = function() {
    return modal.filterResults(
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
};
modal.scrollTop = function() {
    return modal.filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
};
modal.filterResults = function(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
};