/*
------------------------------------------
	Flipbox written by CrappoMan
	simonpatterson@dsl.pipex.com
------------------------------------------
*/
function flipBox(who) {
	var tmp; 
	if (document.images['b_' + who].src.indexOf('_on') == -1) { 
		tmp = document.images['b_' + who].src.replace('_off', '_on');
		document.getElementById('box_' + who).style.display = 'none';
		document.images['b_' + who].src = tmp;
	} else { 
		tmp = document.images['b_' + who].src.replace('_on', '_off');
		document.getElementById('box_' + who).style.display = 'block';
		document.images['b_' + who].src = tmp;
	} 
}

var flipBox = function(A) {
    FlipEngine.Flip(A, true)
};
var FlipEngine = {
    init: function() {
        this.Event.Add(window, "load",
        function() {
            FlipEngine.onLoad()
        })
    },
    onLoad: function() {
        this.CheckBoxs()
    },
    CheckBoxs: function() {
        var G = document.getElementsByTagName("img");
        for (var C = 0,
        B; B = G[C]; C++) {
            var D = B.name;
            if (D.substr(0, 2) == "b_") {
                var F = D.substring(2, D.length);
                var E = this.Cookie.Read("fusion_boxs", F);
                if (E) {
                    var A = this.$("box_" + F).style.display;
                    if ((E == "none" && A != "none") || (E == "block" && A == "none")) {
                        this.Flip(F, false)
                    }
                }
            }
        }
    },
    UpdateCookie: function() {
        var G = document.getElementsByTagName("img"),
        E = [];
        for (var C = 0,
        B; B = G[C]; C++) {
            var D = B.name;
            if (D.substr(0, 2) == "b_") {
                var F = D.substring(2, D.length);
                var A = (this.$("box_" + F).style.display == "none" ? "none": "block");
                E.push({
                    name: F,
                    value: A
                })
            }
        }
        this.Cookie.Create("fusion_boxs", E, 365)
    },
    Flip: function(C, F) {
        var B = document.images["b_" + C];
        var E = this.$("box_" + C);
        if (B && E) {
            var A = B.src.indexOf("_on");
            var D = (A == -1 ? "none": "block");
            E.style.display = D;
            B.src = B.src.replace((A == -1 ? "_off": "_on"), (A == -1 ? "_on": "_off"));
            if (F == true) {
                this.UpdateCookie()
            }
        }
    },
    $: function(A) {
        return document.getElementById(A)
    },
    Cookie: {
        Create: function(D, B, H) {
            if (typeof B == "object") {
                var G = "";
                for (var E = 0,
                F; F = B[E]; E++) {
                    if (E != 0) {
                        G += "|"
                    }
                    G += F.name + "=(" + F.value + ")"
                }
            } else {
                var G = B
            }
            if (H) {
                var C = new Date();
                C.setTime(C.getTime() + (H * 24 * 60 * 60 * 1000));
                var A = "; expires=" + C.toGMTString()
            } else {
                var A = ""
            }
            document.cookie = D + "=" + G + A + "; path=/"
        },
        Read: function(B, E) {
            if (!B) {
                return
            }
            var C, D, A;
            var F = {
                Doc: function(G) {
                    if (document.cookie.length > 0) {
                        return this.Phrase(document.cookie.split(";"), G + "=")
                    }
                },
                Sub: function(G, H) {
                    return this.Phrase(G.split("|"), H + "=(")
                },
                Phrase: function(G, I) {
                    for (var H = 0,
                    J; J = G[H]; H++) {
                        while (J.charAt(0) == " ") {
                            J = J.substring(1, J.length)
                        }
                        if (J.indexOf(I) == 0) {
                            return J.substring(I.length, J.length)
                        }
                    }
                    return null
                }
            };
            D = F.Doc(B);
            if (D) {
                C = D;
                if (E) {
                    A = F.Sub(D, E);
                    if (A) {
                        C = A.substring(0, A.length - 1)
                    }
                }
            }
            return C || null
        }
    },
    Event: {
        Add: function(C, B, A) {
            if (C.attachEvent) {
                C["e" + B + A] = A;
                C[B + A] = function() {
                    C["e" + B + A](window.event)
                };
                C.attachEvent("on" + B, C[B + A])
            } else {
                C.addEventListener(B, A, false)
            }
        }
    }
};
FlipEngine.init()

function addText(elname, wrap1, wrap2) {
	if (document.selection) { // for IE 
		var str = document.selection.createRange().text;
		document.forms['inputform'].elements[elname].focus();
		var sel = document.selection.createRange();
		sel.text = wrap1 + str + wrap2;
		return;
	} else if ((typeof document.forms['inputform'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
		var txtarea = document.forms['inputform'].elements[elname];
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		var oldScrollTop = txtarea.scrollTop;
		//if (selEnd == 1 || selEnd == 2)
		//selEnd = selLength;
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + wrap1 + s2 + wrap2 + s3;
		txtarea.selectionStart = s1.length;
		txtarea.selectionEnd = s1.length + s2.length + wrap1.length + wrap2.length;
		txtarea.scrollTop = oldScrollTop;
		txtarea.focus();
		return;
	} else {
		insertText(elname, wrap1 + wrap2);
	}
}

function insertText(elname, what) {
	if (document.forms['inputform'].elements[elname].createTextRange) {
		document.forms['inputform'].elements[elname].focus();
		document.selection.createRange().duplicate().text = what;
	} else if ((typeof document.forms['inputform'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
		var tarea = document.forms['inputform'].elements[elname];
		var selEnd = tarea.selectionEnd;
		var txtLen = tarea.value.length;
		var txtbefore = tarea.value.substring(0,selEnd);
		var txtafter =  tarea.value.substring(selEnd, txtLen);
		var oldScrollTop = tarea.scrollTop;
		tarea.value = txtbefore + what + txtafter;
		tarea.selectionStart = txtbefore.length + what.length;
		tarea.selectionEnd = txtbefore.length + what.length;
		tarea.scrollTop = oldScrollTop;
		tarea.focus();
	} else {
		document.forms['inputform'].elements[elname].value += what;
		document.forms['inputform'].elements[elname].focus();
	}
}

function show_hide(msg_id) {
	msg_id.style.display = msg_id.style.display == 'none' ? 'block' : 'none';
}
