var Timer=0, Win=null, debug=0
	, s_editor=Array(), s_editor_length = 0
	, wysi_navi_popup=null, wysi_current=null, wysi_current_set=0
	, currentTextArea=null
	, menue_current = -1
	, submit_form="seitenform"
	, writable_init=0
	, pop=null
	, section_old=null
	, js_version=2.11
	, extraHeight = 0;

function OpenSelect(theURL, w) {
    if (typeof(w) != 'number') { w=600; };
    Win=window.open(theURL, "Auswahl", "scrollbars=yes,toolbar=no,menu=no,width=" + w + ",height=600");

    if (typeof(Win) != "object")
    { alert("This application needs to open a window\nPlease check your 'popup-blocker'-settings...\n... and retry your last click!"); }
    else
    { Win.focus(); Timer=window.setInterval("CheckPopup()", 200); }
}

function swDisplay(toShow, toHidePSV) {
    window.document.getElementById(toShow).style.display='inline';
    var toHides = toHidePSV.split('.');
    for(i=0; i<toHides.length; i++)
        window.document.getElementById(toHides[i]).style.display='none';
}

function form_submit(form_name)
{
    if (typeof(form_name) != "string" || form_name == "") { 
        form_name = submit_form; 
    }

    try { 
        document.getElementsByName(form_name)[0].submit(); 
    }
    catch(e) { 
        popup_warn("error in form_submit:", 1); 
    }
}

function CheckPopup() {
    //~ var pop_is_opened;
    //~ try {
        //~ pop_is_opened = ((Win.closed==true));
    //~ } catch(e) {
        //~ try { window.clearInterval(Timer); } catch(e) { }
        //~ pop_is_opened=false;
    //~ }

    //~ if (pop_is_opened) {
        //~ try { window.clearInterval(Timer); } catch(e) { }

        //~ try {
            //~ document.getElementsByName("Action")[0].value = "Reload";
            //~ document.getElementsByName(submit_form)[0].target = '';
        //~ } catch(e) {
            //~ popup_warn("error in CheckPopup:\n", 1);
        //~ }

        //~ Win=null;
        //~ form_submit();
    //~ }
}

function action_clear() { try { self.document.getElementsByName("Action")[0].value = ""; } catch(e) {} }

function set_val(cn,val,window_name) {
    var win;
    try {
        if (typeof(window_name)!="string" || window_name=="") { win = self; }
        else { win = window_get(window_name); }

        win.document.getElementsByName(cn)[0].value = val;
    } catch(e) {
        popup_warn("error in set_val:\ncn:" + cn + "\nval:" + val, 1);
    }
}

function submit_val(cn, val, target) {
    var back_target="",back_cn="";
    try {
        if (typeof(target)=="string" && target!="") {
            back_target = document.getElementsByName(submit_form)[0].target;
            back_cn = document.getElementsByName(cn)[0].value;
            document.getElementsByName(cn)[0].value = val;
            document.getElementsByName(submit_form)[0].target = target;
        } else {
            document.getElementsByName(cn)[0].value = val;
        }
    } catch(e) {
        popup_warn("error in submit_val:\ncn:" + cn + "\nval:" + val, 1);
    }

    form_submit();

    try {
        if (typeof(target)=="string" && target!="") {
            document.getElementsByName(submit_form)[0].target = back_target;
            document.getElementsByName(cn)[0].value = back_cn;
        }
    } catch(e) {
        popup_warn("error in submit_val..2:\ncn:" + cn + "\nval:" + val, 1);
    }
}

function save_state(obj)
{
    if (typeof(state_run) != "number" || state_run=="") { return; }

//    try { document.getElementsByName("anchor")[0].value = obj.name; } catch(e) { }

    form_submit();
}

function ConfirmDelete(Question)
{
    Check = window.confirm(Question);

    if (Check == true)
    {
        try { document.getElementsByName("Action")[0].value = "Delete"; }
        catch(e) { popup_warn("error in ConfirmDelete:\n", 1); }

        form_submit();
    }
}

function p2div()
{
    code = wp_GetCode();
    code = code.replace(/<p/gi, "<div");
    wp_SetCode(code.replace(/<\/p>/gi, "</div>"));
}

function PrintSite()
{ //self.document.location.href=self.document.location.href + "&PrintTemplate=1";
    window.print()
}

function submitsf(val)
{
    try { document.getElementsByName("State")[0].value = val; }
    catch(e) { popup_warn("error in submitsf:\n", 1); }

    form_submit();
}

function OpenWindow(theURL)
{ window.open(theURL, '', 'scrollbars=yes,resizable=yes,toolbar=no,menu=no,width=640,height=600'); }

function OpenWindow2(theURL, name, w, h) {
    var popup;

    if (typeof(w) != 'number') { w=640; }; if (typeof(h) != 'number') { h=600; }
    popup = window.open(theURL, name, 'scrollbars=yes,resizable=yes,toolbar=no,menu=no,width=' + w + ',height=' + h);
    popup.focus();
}

function OpenWindowFull(theURL)
{ window.open(theURL, '', 'scrollbars=yes,toolbar=no,menu=no,resizable=yes,width=680,height=600'); }

function MM_openBrWindow(theURL,winName,features)
{ window.open(theURL,winName,features); }

function openEditor(name, start)
{ //alert(document.getElementsByName(name)[0] + " " + start);
  currentTextArea = document.getElementsByName(name)[0];

  var edit=window.open(start, 'editorWindow', 'width=720, height=450');
  edit.focus();
};

function popup_warn(str, d)
{ if ((debug >= d) || (d==0)) { alert("global warning_js:" + debug + ", d: " + d + "\n" + str); } }

function popup_debug(str)
{
    var w;
    w=window.open('','debug','scrollbars=yes,resizable=yes,toolbar=no,menu=no,width=680,height=600');
    w.document.write('<pre><tt>' + str + '</pre></tt>');
    w.focus();
}

function iframe_post(frame_name, post_url, post_target)
{
    var w, wd, obj;

    try { w = window_get(frame_name); wd = w.document; }
    catch(e)
    { popup_warn("iframe_post:\n" + frame_name + "- not found\nI reload now!!", 2); document.location.reload(); return; }

    try { obj = wd.getElementsByName(submit_form)[0]; }
    catch(e)
    { popup_warn("iframe_post:\n" + submit_form + "- not found\nI reload now!!", 2); document.location.reload(); return; }

    if (typeof(obj) != "object")
    { popup_warn("iframe_post:\n" + submit_form + "- is no obj!\nfind failed\nI reload now!!\n" + w, 2); document.location.reload(); return; }

    if (w.writable_init==1 && (wd.getElementsByName('writable')[0].value != 1))
    {
        if (typeof(L_save_will_fail__buffer_input__reload_site) != "string" || (L_save_will_fail__buffer_input__reload_site==""))
        { alert("Posting values canceld.\nThey would not be saved!\n1. Please save your input values\n2. Reload the Page\n3. renter your input\n"); }
        else
        { alert(L_save_will_fail__buffer_input__reload_site); }

        return;
    }

    try
    {
        obj.method = "POST";
        obj.target = post_target;
        obj.action = post_url;

//        wd.getElementsByName("set_m")[0].value = menue_current;

        obj.submit();
    }
    catch(e)
    { popup_warn("iframe_post:\nsubmit failed!\nI reload now!!", 2); document.location.reload(); }

    w.iframe_restore_section(frame_name);
}

function writable() {
    try {
        var obj=self.document.getElementsByName('writable');
        if(typeof(obj) == 'object'
        && obj.length>0) {
            obj[0].value = 1;
            writable_init = 1;
        }
    } catch(e) {
        writable_init = 0;
    }
}

function writable_cancel()
{ try { self.document.getElementsByName('writable')[0].value = 0 } catch(e) { } }

function writable_cancel_parent()
{ parent.writable_init=1; try { parent.document.getElementsByName('writable')[0].value = 0 } catch(e) { } }

function writable_catch() {
	var obj;
    if (writable_init==1)
    {
        obj = self.document.getElementsByName('writable')[0];
        if (typeof(obj) != "object" || obj.value != 1)
        {
            if (typeof(L_writable_not) != "string" || (L_writable_not==""))
            { alert("Warning!!\nYou may not make canges!\nYour changes will not be stored!\nIf you think, this might be an error, reload the site.\n"); }
            else
            { alert(L_writable_not); }
        }
    }
}

function window_get(window_name)
{
    var obj;
    try { 
        obj = eval("window." + window_name); 
    } catch(e) { 
        //
    }

    if (typeof obj === "undefined" || typeof(obj) != "object" || !obj.document || !obj.location){
        obj = window; 
    }
    return obj;
}

function iframe_set_section(frame_name, new_section) {
    try {
        if (section_old == null)
        { window_get(frame_name).section_old = window_get(frame_name).document.getElementsByName('ia')[0].value; }

        window_get(frame_name).document.getElementsByName('ia')[0].value=new_section;
    } catch(e) {
        popup_warn(frame_name + "\n 'ia' - the section-var\nnot found\nbaaaad!!\nnew s: " + new_section, 2);
        document.location.reload();
    }
}

function setlang(lang) {
    try {
        var obj = self.document.getElementsByName('ls')[0];
        if (typeof(obj) != 'object' || typeof(obj.options) != 'object') return;
    } catch(err) {
        return;
    }

    var found = false;
    for (var i=0; i<obj.options.length; i++) {
        if (obj.options[i].value == lang) {
            obj.options[i].selected = true;
            found = true;
            obj.selectedIndex = i;
        } else {
            obj.options[i].selected = false;
        }
    }

    if (!found) {
        try {
        	obj.options[0].selected = true;
        	obj.selectedIndex = 0;
        } catch(err) {
        }
    }
}

function iframe_restore_section(frame_name)
{
    try { window_get(frame_name).document.getElementsByName('ia')[0].value = window_get(frame_name).section_old; }
    catch(e) { }
}

function on_set_meneu(menue_prefix, on, nr, on_color, off_color)
{
    var i;

    for (i=0; i < nr; i++)
    {
        if(window.document.getElementById(menue_prefix + i))
        {
            if (i == on) { window.document.getElementById(menue_prefix + i).style.background = '#' + on_color; }
            else { window.document.getElementById(menue_prefix + i).style.background = '#' + off_color; }
        }
    }

    menue_current = on;
}

function call_wysi(window_name, field_name, nav_start_type)
{
    if (debug && (typeof(wysi_url)!="string" || wysi_url==""))
    { popup_warn("in call_wysi:\nno wysi_url - url defined!", 1); return; }

    if ((typeof(nav_start_type)=="number" || nav_start_type!=0))
    { nav_start_type='&nav_start_type=' + nav_start_type; }
    else
    { nav_start_type=''; }

    s_editor[window_name]=field_name;
    s_editor_length++;

    window_get(window_name).document.location.href=wysi_url + '?width='
        + document.getElementsByName(window_name)[0].style.width + '&height='
        + document.getElementsByName(window_name)[0].style.height
        + nav_start_type;
}

function wysi_save() {
    var ret = true;

    try {
        for (name in s_editor) {
            window.frames[name].document.getElementById('htmlCode').do_save();
        }
    } catch(e) {
        ret = false;
	}

    if (parent.wysi_navi_popup != null && parent.wysi_navi_popup.document != null)
    { parent.wysi_navi_popup.close(); }

    return ret;
}

function wysi_show_design()
{
    try
    { for (name in s_editor) { window.frames[name].document.getElementById('htmlCode').update_view(); } }
    catch(e)
    { }
}

function state_set(window_name, val)
{
    try
    { window_get(window_name).document.getElementsByName('State')[0].value = "." + val; }
    catch (e)
    { popup_warn("Storing new state failed:\n'State'-var not found\nmaybe middle frame not valid!", 2); }

    try
    {
        window_get(window_name).document.getElementsByName('comment')[0].value = self.document.getElementsByName('comment')[0].value
    }
    catch (e)
    { popup_warn("Storing new state - comment failed:\n'comment'-var not found\n", 2); }
}

function wysi_save_child(window_name, input_name)
{
    try
    {
        if (window_get(window_name).document.getElementById(input_name).wysi_save())
        { window_get(window_name).document.getElementById(input_name + '_changed').value = 1; }
    }
    catch(e)
    { popup_warn("wysi_save_child:\n'" + input_name + "' - not found\nWysi could not save", 4); }
}

function wysi_show_design_child(window_name, input_name)
{
    try
    { window_get(window_name).document.getElementById(input_name).wysi_show_design(); }
    catch(e)
    { popup_warn("wysi_show_design_child:\nWysi could not call design", 4); }
}

function preview_call_finish(window_name, input_name) {
    wysi_show_design_child(window_name, input_name);
    if (typeof(pop) == "object") {
        pop.focus();
    }

    try {
        iframe_restore_section(window_name);
    } catch(e) {
    }
}

function save_child(window_name, i_n, chk_sel)
{
    try
    {
        if (typeof(chk_sel) == "number" && chk_sel != 0)
        {
            if (document.getElementsByName(i_n)[0].checked)
            { window_get(window_name).document.getElementsByName(i_n)[0].value = 1; }
            else
            { window_get(window_name).document.getElementsByName(i_n)[0].value = 0; }
        }
        else
        { window_get(window_name).document.getElementsByName(i_n)[0].value = document.getElementsByName(i_n)[0].value; }
    }
    catch(e)
    { popup_warn("comment_save_child:\n'" + i_n + "' - not found\ncomment could not be saved", 4); }
}

function wysi_pops_obj_sel(my_url, w)
{
    parent.wysi_current = window.name;

    var open_it = false;

    try
    { open_it = ((parent.wysi_navi_popup == null) || (parent.wysi_navi_popup.document == null)); }
    catch (e)
    { open_it = true; }

    if (typeof(w) != 'number' || w < 600) { w=640; } else { w+=10; }

    if  (open_it)
    {
        try
        { parent.wysi_navi_popup = parent.window.open(my_url, 'obj_sel', 'width=' + w + ',height=600,scrollbars=yes,resizable=yes'); }
        catch (e)
        { alert("could not open a new window, plz. check your firewall/security settings"); }
    }

    parent.wysi_navi_popup.focus();
}

function wysi_call(url, obj_src, frame_name)
{
	var sh=0, obj;

	try
	{
		if (typeof(obj_src.frames[frame_name]) == "object") { obj=obj_src.frames[frame_name]; }
		else 												{ obj=obj_src; }
	}
	catch(e) { obj=obj_src; }

	try
	{
        if (obj.document && obj.document.body.scrollHeight)
        { sh = obj.document.body.scrollHeight; }
        else if (obj.contentDocument && obj.contentDocument.body.offsetHeight)
		{ sh = obj.contentDocument.body.offsetHeight; }
	}
	catch(e)
	{ }

	try
	{
		if (sh > 0) { obj.document.location.href = url + '&height=' + sh; }
		else		{ obj.document.location.href = url; }
	}
	catch(e)
	{ popup_warn("wysi_call:\ncould not change location!", 1); }
}

function wysi_opener_ins_at_cursor(name) {
    try {
        opener.htmlCode.insertAtSelection(document.getElementsByName(name)[0].value);
    } catch (e) {
        popup_warn("wysi_opener_ins_at_cursor:\ncould not ins!", 1);
    }

    self.focus();
}

function wysi_ins_imgnav(val, txt) {
    var win = self.opener;

    try {
        v = wysi_insLinkOptions(val);
        win.document.getElementsByName('wysi_img_link')[0].value = v;
        win.var_wysi_img_link = v;
    } catch(e) {
        popup_warn("wysi_ins_imgnav failed", 2);
        self.close();
        return;
    }

    try {
        win.document.getElementById('wysi_img_link_target_name').innerHTML = txt;
    } catch(e) {
        popup_warn("wysi_ins_imgnav set-text failed", 2);
        self.close();
    }

    self.close();
}

function wysi_insLinkOptions(val) {
    var linkName = "", linkType = "";
    try {
        linkName = self.document.getElementsByName("link_name")[0].value;
    } catch(e) {
        linkName = "";
    }

    try {
        linkType = self.document.getElementsByName("link_type")[0].value;
    } catch(e) {
        linkType = "";
    }

    try {
        if (linkType.length > 0
        || linkName.length > 0) {
            val = val.substring(0, (val.length - 2));

            if (val.split('|').length == 2)
                val += '||';
            else
                val += '|';

            var popup = '';
            if (linkType.length > 0) {
                if (linkType == 'popup') {
                    val += '|';
                    popup = '|1';
                } else
                    val += linkType + '|';
            } else
                val += '|';

            if (linkName.length > 0)
                val += linkName;
            val += popup + ']]';
        }
    } catch(e) {
    }

    return val;
}

function ckInsVal(val) {
    try {
        if (typeof(self.opener.window.CKEDITOR) != "object") {
	        alert ("Bitte rufen Sie den Navigator erneut auf!\nDas Zielfenster für's Einfügen kann nicht gefunden werden.\n");
	        self.close();
            return;
        }
    } catch(e) {
        alert ("Bitte rufen Sie den Navigator erneut auf!\nDas Zielfenster für's Einfügen kann nicht gefunden werden.\n");
        self.close();
    }

    try {
        self.opener.window.CKEDITOR.instances.Content.insertHtml(wysi_insLinkOptions(val));
//        self.opener.window.theCkEd.htmlCode.moveFocus();
    } catch(e) {
        alert ("Bitte rufen Sie den Navigator erneut auf!\nDas Zielfenster für's Einfügen kann nicht gefunden werden.\n");
        self.close();
    }
//    self.close();
}
function wysi_ins_value(val) {
    try {
        if (self.opener.s_editor_length > 1 && !self.opener.wysi_current_set) {
            self.opener.wysi_choose_window();
            return;
        }
    } catch(e) {
        alert ("Bitte rufen Sie den Navigator erneut auf!\nDas Zielfenster für's Einfügen kann nicht gefunden werden.\n");
        self.close();
    }

    try {
        self.opener.frames[self.opener.wysi_current].htmlCode.insertAtSelection(wysi_insLinkOptions(val));
        self.opener.frames[self.opener.wysi_current].htmlCode.moveFocus();
        self.opener.wysi_current_set = 0;
    } catch(e) {
        alert ("2.Bitte rufen Sie den Navigator erneut auf!\nDas Zielfenster für's Einfügen kann nicht gefunden werden.\n");
        self.close();
    }
    self.close();
}
function wysi_choose_window()
{
    popup_warn("Es gibt zwei Editorfenster.\nGehen Sie sicher, dass Sei bei richtigen Editor, den\n Objektnavigator gestartet haben. Positionieren Sie dort den Cursor an die Richtige Stelle!\n Beim nächsten Übernehmen, wird der gewünschte Inhalt übernommen!", 1);
    wysi_current_set = 1;
}

function parent_set(elem_name)
{
    try
    {
        parent.document.getElementsByName(elem_name)[0].value =
            self.document.getElementsByName(elem_name)[0].value;
    }
    catch(e)
    { popup_warn("parent_set:\n", 1); }
}

function parent_set_value(elem_name, value)
{
    try { parent.document.getElementsByName(elem_name)[0].value = value; }
    catch(e) { popup_warn("parent_set_value:\nelem_name:" + elem_name + "\nvalue:" + value, 1); }
}

function parent_submit()
{
    try { parent.document.getElementsByName(submit_form)[0].Submit(); }
    catch(e) { popup_warn("parent_submit:\n", 1); }
}
function wysi_opener_ins_at_cursor_value(req_id)
{
    try
    {
        if (self.opener.s_editor_length > 1 && !self.opener.wysi_current_set)
        { self.opener.wysi_choose_window(); return; }

        self.opener.frames[self.opener.wysi_current].htmlCode.insertAtSelection(document.getElementById(req_id).value);
        self.opener.frames[self.opener.wysi_current].htmlCode.moveFocus();
        self.opener.wysi_current_set = 0;
    }
    catch(e)
    {
        alert ("Bitte rufen Sie den Navigator erneut auf!\nDas Zielfenster für's Einfügen kann nicht gefunden werden.\n");
        self.close();
    }
}
function wysi_opener_ins_at_cursor_all(req_id)
{
    if (self.opener == null || self.opener.document == null)
    {
        alert ("Bitte rufen Sie den Navigator erneut auf!\nDas Zielfenster für's Einfügen kann nicht gefunden werden.\n");
        self.close();
    }


    if (self.opener.s_editor_length > 1 && !self.opener.wysi_current_set)
    {
        try { self.opener.wysi_choose_window(); }
        catch(e) { popup_warn("wysi_opener_ins_at_cursor_all:\nchoose_warn - error!", 1); }

        return;
    }

    try
    {
        self.opener.frames[self.opener.wysi_current].htmlCode.insertAtSelection(document.getElementById(req_id).innerHTML);
        self.opener.frames[self.opener.wysi_current].htmlCode.moveFocus();
        self.opener.wysi_current_set = 0;
    }
   catch(e)
    { popup_warn("wysi_opener_ins_at_cursor_all:\nreq_id:" + req_id, 1); }
}

function update_selectionHolder() {
	try { if (typeof(list_outer_sel) != 'number') return; } catch(e) { return; }//constraint

	var x=list_outer_sel, i
		, objs_name='Id[]'
		, obj_arr=document.getElementsByName(objs_name);
	try {
		for (i = 0; i < obj_arr.length; i++) { if (obj_arr[i].checked) x++; };
		document.getElementById('selection_holder').innerHTML=x;
	} catch(e) {
	}
};

function selections(cmd, objs_name, toSel) {
    var obj_arr, i;

    if (typeof(objs_name) == "string" && objs_name != "") {
        obj_arr = document.getElementsByName(objs_name);
    } else {
        obj_arr = document.getElementsByName("selection[]");
    }
//alert(obj_arr);// + " " + obj_arr.length);
    try {
        for (i = 0; i < obj_arr.length; i++) {
            if (cmd == "s_sel") { obj_arr[i].checked = 1; };
            if (cmd == "s_rev") { obj_arr[i].checked = !(obj_arr[i].checked); };
            if (cmd == "s_del") { obj_arr[i].checked = 0; };
            if (cmd == "s_id")  { obj_arr[i].checked = (obj_arr[i].value==toSel); };
        }
    }
    catch(e)
    { popup_warn("selections:\ncmd:" + cmd + "\nobjs_name:" + objs_name, 1); }
}

function my_get_elment(id, obj)
{
 	if (typeof(obj) != "object") { obj = self; }
    if (obj.document.getElementById) { return obj.document.getElementById(id); }
    if (obj.document.all) { return obj.document.all.id; }
    if (obj.document.layers) { return obj.document.layers.id }

    popup_warn("no object found: " + id, 2);

    return null;
}

function handleHighlight4MyKind(nameObj,off,on,innerStuff) {
    if(typeof(nameObj)!="string" || nameObj=='') return false;
    if(typeof(innerStuff)!="string") innerStuff='';

    var idx=-1;
    var objArr = document.getElementsByName(nameObj);
    for(var i=0;i<objArr.length;i++){
        objArr[i].innerHTML = objArr[i].innerHTML.replace(/<\/?[^>]+>/gi, '');
        objArr[i].style.color='#' + off;
        if (objArr[i].innerHTML==innerStuff) idx=i;
    }
    if (idx>-1)objArr[idx].style.color='#'+on;

    return true;
}

