// JavaScript Document
var pinWin = null;
function doFocus(field) {field.focus();}
function trim(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}
function rTrim(sInString) {
  return sInString.replace( /\s+$/g, "" );// strip trailing
}
function lTrim(sInString) {
  return sInString.replace( /^\s+/g, "" );// strip leading
}
function Open_Blank_Window(opsid, win) {
    ops_window=window.open(opsid, win);
    ops_window.focus()
}
function Open_Popup(opsid, win, wdth, hght, tool) {
    if (typeof wdth == "undefined") { wdth = "320"; }
    if (typeof hght == "undefined") { hght = "240"; }
    if (typeof tool == "undefined") { tool = "no"; }
    ops_window=window.open(opsid, win,"width="+wdth+",height="+hght+",toolbar="+tool+",menubar=no,resizable=yes,scrollbars=yes,location=no,directories=no,status=yes");
    ops_window.focus()
}
function strpad2(val){ 
    return (val.toString().length==1?"0"+val:val); 
}
function getThisById(what) {
    if (!document.getElementById) {return}
    elt = eval ("document.getElementById('" + what + "')");
//    window.focus(); 
    return elt;
}
function clearInput(input) {
    getThisById(input).value = "";
}
function setChecked(input) {
    getThisById(input).checked = true;
}
function doPageOnLoad() {
    focusFld = getThisById('focusfield');
    if (focusFld) {
        fldfocus = getThisById(focusFld.value);
        if (fldfocus) fldfocus.focus();
    }
    loadNavmen = getThisById('loadnavmen');
    if (loadNavmen) {
        menu = getThisById('H_'+loadNavmen.value);
        if (menu) {menu.id = "selected";}
        menu = getThisById(loadNavmen.value);
        if (menu) {menu.id = "selected";}
    }
    loadNavmen = getThisById('loadnavmenM');
    if (loadNavmen) {
        menu = getThisById('H_'+loadNavmen.value);
        if (menu) {menu.id = "selected";}
        menu = getThisById(loadNavmen.value);
        if (menu) {menu.id = "selected";}
    }
    loadTab = getThisById('loadtab');
    if (loadTab) {
        menu = getThisById(loadTab.value);
        if (menu) selmenu(menu);
    }
    loadTab = getThisById('loadsubtab');
    if (loadTab) {
        menu = getThisById(loadTab.value);
        if (menu) selsubtab(menu);
    }
    hdStyle = getThisById('hdrstyle');
    if (hdStyle) {hdStyle.selectedIndex = hdStyle.value; changeHeaderStyle(hdStyle);}
}
function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth,myHeight];
}
function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}
function setOpenerURL(url) {
    opener.location.href = url;
}
function confirmDelete(btn) {
    coords = findPos(btn);
    deletedv = getThisById("deletediv");
    deletediv.style.left = coords[0]-parseInt(deletediv.style.height)-70;
    deletediv.style.top = coords[1]-parseInt(deletediv.style.height);
    size = getWindowSize();
    if (parseInt(deletediv.style.left) + parseInt(deletediv.style.width) > size[0]) deletediv.style.left = (size[0] - parseInt(deletediv.style.width))+"px";
    deletedv.style.display = 'block';
    deletedv.style.zIndex = 9999;
    okBtn = getThisById("okButton");
    okBtn.name = btn.name;
    okBtn.style.width = btn.style.width;
    okBtn.value = btn.value;
    okBtn.focus();
}
function doReneg() {
    deletedv = getThisById("deletediv");
    deletedv.style.display = 'none';
}

//  TAB menus
var prevMenu = null;
function selmenu(menu) {
    if (prevMenu != null) {
        prevMenu.className = "noselect";
        theDiv = getThisById(prevMenu.id + 'Tab');
        theDiv.className = "backTab";
    }
    loadTab = getThisById('loadtab');
    if (loadTab) loadTab.value = menu.id;
    menu.className = "select";
    prevMenu = menu;
    theDiv = getThisById(menu.id + 'Tab');
    theDiv.className = "frontTab";
    theDiv.focus();
}
var prevSubTab = null;
function selsubtab(menu) {
    if (prevSubTab != null) {
        prevSubTab.className = "noselect";
        theDiv = getThisById(prevSubTab.id + 'Tab');
        theDiv.className = "backTab";
    }
    loadTab = getThisById('loadsubtab');
    if (loadTab) loadTab.value = menu.id;
    menu.className = "select";
    prevSubTab = menu;
    theDiv = getThisById(menu.id + 'Tab');
    theDiv.className = "frontTab";
    theDiv.focus();
}
//  Move listbox items
function moveListboxItem(pListbox,pMoveUp) {
    listbox = getThisById(pListbox);
    if (listbox) {
        if (listbox.selectedIndex < 0) {
            alert("Sorry, but you need to select a page first!");
            return;
        }
        indx = -1;
        if (pMoveUp) {
            if (listbox.selectedIndex > 0) indx = listbox.selectedIndex - 1;
        } else {
            if (listbox.selectedIndex < listbox.length-1) indx = listbox.selectedIndex + 1
        }
        if (indx < 0) {
            if (pMoveUp) moveDir = 'Up'; else moveDir = 'Down';
            alert("Sorry, but you can't Move this page " + moveDir + " any further!");
        } else {
            pgeid = '';
            if (location.href.indexOf("?") < 0) {
                page = getThisById('webpageid');
                pgeid = '?webpageid=' + page.value;
            }
            url = location.href + pgeid + "&shuffle=" + listbox.options[listbox.selectedIndex].value + "&moveup=" + pMoveUp;
            location.href = url;
        }
    }
}
/*  */
var pupBtn = null;
function showPup(btn,pFocus) {
    pupBtn = btn;
    coords = findPos(btn);
    stylediv = getThisById(btn.id + "div");
    stylediv.style.left = coords[0] - parseInt(stylediv.style.width) / 2;
    stylediv.style.top = coords[1] - 10;
    size = getWindowSize();
    if (parseInt(stylediv.style.left) + parseInt(stylediv.style.width) > size[0]) stylediv.style.left = (size[0] - parseInt(stylediv.style.width))+"px";
    stylediv.style.display = 'block';
    //  Save selection in the Content textarea
    cont = getThisById('contentarea');
    saveSelectedText(cont);
    //  reset fields in popup for reuse
    if (btn.id == 'link') {
        tmp = getThisById('linkpage'); if (tmp) tmp.selectedIndex = 0;
        tmp = getThisById('linkurl'); if (tmp) tmp.value = '';
        setChecked('linksame');
    }
    //  set focus to filed if specified
    if (pFocus) {foc = getThisById(pFocus); if (foc) foc.focus();}
}
function hidePup() {
    stylediv = getThisById(pupBtn.id + "div");
    stylediv.style.display = 'none';
}
/* Adding HTML Tags to Content text */
var selectionStart = null;
var selectionBefore = null;
var selectionContent = null;
var selectionAfter = null;
var selectionEnd = null;
var selectionBookmark = null;
function saveSelectedText(obj) {
    if(typeof obj.selectionStart == 'number')
    {
        // Mozilla, Opera, and other browsers
        selectionStart = obj.selectionStart;
        selectionEnd   = obj.selectionEnd;
        selectionBefore = obj.value.substring(0, selectionStart);
        selectionContent = obj.value.substring(selectionStart, selectionEnd);
        selectionAfter = obj.value.substring(selectionEnd, obj.value.length);
    }
    else if(document.selection)
    {
        selectionBookmark = null;
        // Internet Explorer
        obj.focus();
        var range = document.selection.createRange();
        if(range.parentElement() != obj) return false;

        if(typeof range.text == 'string') {
            selectionBookmark = range.getBookmark();
        }
    }
    else
        obj.value += text;
};

function applyTag(tag) {
    cont = getThisById('contentarea');
    saveSelectedText(cont);
    if (cont) wrapText(cont, '<'+tag+'>', '</'+tag+'>');
};
function applyAlignTag(align) {
    cont = getThisById('contentarea');
    saveSelectedText(cont);
    if (cont) wrapText(cont, '<span class="align'+align+'">', '</span>');
};
function wrapText(obj, beginTag, endTag) {
    if(typeof obj.selectionStart == 'number')
    {
        // Mozilla, Opera, and other browsers
        obj.value = selectionBefore + beginTag + selectionContent + endTag + selectionAfter;
        obj.focus();
        obj.selectionStart = selectionStart;
        obj.selectionEnd = selectionEnd + beginTag.length + endTag.length;
        obj.focus();
    }
    else if(document.selection)
    {
        // Internet Explorer
        // make sure it's the textarea's selection
        obj.focus();
        var range = document.selection.createRange();
        range.moveToBookmark(selectionBookmark);
        range.select();
//        if(range.parentElement() != obj) return false;

        if(typeof range.text == 'string') {
            tagLength = range.text.length;
            range.text = beginTag + range.text + endTag;
            range = document.selection.createRange();
            range.moveToBookmark(selectionBookmark);
            range.moveEnd('character', beginTag.length + tagLength + endTag.length);
            range.select();
        }
    }
    else
        obj.value += text;
};
function applyLinkTag(lnk) {
    linkwin = getThisById('linksame');
    if (!linkwin.checked) 
        linkwin = getThisById('linknew');
    link = getThisById(lnk.name);
    if (link) {
        if (lnk.name == 'linkpage') {
            lnkurl = link.options[link.selectedIndex].value + '.php';
        }
        if (lnk.name == 'linkurl') {
            protocol = getThisById('linkhttp');
            lnkurl = protocol.value + link.value;
        }
    }
    cont = getThisById('contentarea');
    theTarget = ''; if (linkwin.value == 'new') theTarget = ' target="_blank"';
    if (link && cont) wrapText(cont, '<a href="'+lnkurl+'"'+theTarget+'>', '</a>');
};
function applyImageTag(img) {
    imgStyle = getThisById('imgleft');
    if (!imgStyle.checked) 
        imgStyle = getThisById('imgright');
    if (!imgStyle.checked) 
        imgStyle = getThisById('imgcenter');
    theImg = getThisById('imgname');
    if (theImg) {theImage = theImg.options[theImg.selectedIndex].value;}
    cont = getThisById('contentarea');
    theFloat = ''; theEnd = '';
    if (imgStyle.value == 'left') {theFloat = ' style="float:left;"'; theEnd = '<span style="clear:both;"></span>'};
    if (imgStyle.value == 'right') {theFloat = ' style="float:right;"'; theEnd = '<span style="clear:both;"></span>';}
    imgTab = '<img src="images/'+theImage+'"'+theFloat+' border="0" />';
    if (theImage && cont) wrapText(cont, imgTab, theEnd);
};
function applyListTag(align) {
    cont = getThisById('contentarea');
    if (cont) {
        if (align == 'number')
            splitAndWrapText(cont, 1, '<ol>' + '\r\n' + '<li>', '</li>' + '\r\n' + '</ol>')
        else
            splitAndWrapText(cont, 1, '<ul class="'+align+'">' + '\r\n' + '<li>', '</li>' + '\r\n' + '</ul>')
    };
};
function splitAndWrapText(obj, splitType, beginTag, endTag) {
    if(typeof obj.selectionStart == 'number')
    {
        // Mozilla, Opera, and other browsers
        var start = obj.selectionStart;
        var end   = obj.selectionEnd;
        theText   = splitText(splitType, obj.value.substring(start, end));
        
        obj.value = obj.value.substring(0, start) + beginTag + theText + endTag + obj.value.substring(end, obj.value.length);
        obj.focus();
        obj.selectionStart = start;
        obj.selectionEnd = end + beginTag.length + endTag.length;
        obj.focus();
    }
    else if(document.selection)
    {
        // Internet Explorer
        // make sure it's the textarea's selection
        obj.focus();
        var range = document.selection.createRange();
        if(range.parentElement() != obj) return false;
        theText = splitText(splitType, range.text);

        if(typeof range.text == 'string') {
            document.selection.createRange().text = beginTag + theText + endTag;
        }
    }
    else
        obj.value += text;
};
function splitText(splitType, textString) {
    theText = textString;
    if (splitType == 1) {
        theText = theText.replace(/\r\n/gm, "</li>" + '\r\n' + "<li>");
    }
    return theText;
}

