var isMSIE = (navigator.userAgent.indexOf('Opera') == -1 && navigator.userAgent.indexOf('MSIE') != -1 && navigator.appName == "Microsoft Internet Explorer");


			function changeimage(swapImage) {
			
				document.getElementById('imageupdate').src = swapImage;
			
			}

function ifdisabled(v, someid)
{



   someobj = document.getElementById(someid);
   if(v.checked)
	someobj.disabled=false;
   else
	someobj.disabled=true;
}

function switch_menu_color(obj, bg_color)
{
var v = document.getElementById(obj);   
document.getElementById(obj+'centre').style.background = '#' + bg_color;
document.getElementById(obj+'left').src = 'img/interface/scallops/left_'+bg_color+'.gif';
document.getElementById(obj+'right').src = 'img/interface/scallops/right_'+bg_color+'.gif';
 

   /* obj.style.color= text_color;
    obj.style.background = bg_color; */
}

function switch_menu_color_out(obj, bg_color)
{
var v = document.getElementById(obj);   
document.getElementById(obj+'centre').style.background = bg_color;
document.getElementById(obj+'left').src = 'img/left.png';
document.getElementById(obj+'right').src = 'img/right.png';
 

   /* obj.style.color= text_color;
    obj.style.background = bg_color; */
}


var current = '';
var menu_timer = null;
var menu_hide_timeout = 200;
var menu_show_timeout = 100;

function switch_sub_menu(id, show)
{
    if (menu_timer) {
        window.clearTimeout(menu_timer);
    }
    if (current.length>0 && show) {
        tmp = document.getElementById(current);
        if(tmp){
            if(current == id){
                tmp.style.visibility = 'visible';
            }else{
                tmp.style.visibility = 'hidden';
                current = '';
            }
        }
    }
    if(!document.getElementById(id)){ return; }
    current = id;
    menu_timer = show?
    window.setTimeout("set_visibility('"+id+"', true)", menu_show_timeout):
    window.setTimeout("set_visibility('"+id+"', false)", menu_hide_timeout);
}

function set_visibility(id, show){
 document.getElementById(id).style.visibility = show?'visible':'hidden';
 if(!show && current==id){ current = ''; }
}


function openURL(){
  var url=document.getElementById('url').value;
  if(url!='http://' && url!='') {
      //popupwnd(url, targ, width, height, centerwnd)
  	var win = window.open(url,'','toolbar=yes,location=yes,scrollbars=yes,menubar=yes,status=yes,resizable=yes');
  	win.focus();
  }
}
function previewImg(src, filename, path) {
	document.images['preview_img'].src = src;
	document.images['preview_img'].alt = filename;
	document.getElementById("filename").innerHTML = filename;
	document.getElementById("selected_image_popup").value = path + filename;
	document.getElementById("button").style.visibility = 'visible';
}
function previewFile(src, filename) {
	document.images['preview_img'].src = src;
	document.images['preview_img'].alt = filename;
	document.getElementById("filename").innerHTML = "<b>" + filename + "</b>";
    var img = document.getElementById('preview_img');
    img.style.display = '';
}
function insertImg(_par_num) {
	window.opener.document.getElementById('selected_image' + _par_num).value = document.getElementById("selected_image_popup").value;
	window.opener.document.getElementById('is_image_selected' + _par_num).value = 1;
	window.close();
}


function clearImg(_par_num) {
	 document.getElementById("selected_image" + _par_num).value = '';
	 document.getElementById("is_image_selected" + _par_num).value = 0;
}

function displayTab(tabToDisplayName, tabToHideName){
    document.getElementById(tabToDisplayName).style.visibility = 'visible';
    document.getElementById(tabToHideName).style.visibility = 'hidden';
    document.getElementById(tabToDisplayName+'_menu').style.background = '#FFFFFF';
    document.getElementById(tabToHideName+'_menu').style.background = '#FFFFCC';
}
function insertDir(path) {
	window.opener.document.getElementById('selected_dir').value = path;
	window.close();
}
function change_language(url){
	if(confirm('Do you want to save the changes made on the current page?')){
		document.getElementById('redirect').value = url;
		document.getElementById('redirect').form.submit();
    } else
    {
    	window.location.href = url;
    }
}

function absolute(value)
{
    if (value < 0) return -value;
    return value;
}
function popupwnd(url, targ, width, height, centerwnd){
 var param = 'width='+width + ',height='+height + ',toolbar=no,location=no,menubar=no,status=no,resizable=yes,scrollbars=yes';
 if (centerwnd && !(navigator.userAgent.indexOf('Opera')>=0)){
     param += ",left=" + ((screen.width - width)/2) + ",top=" + ((screen.height-height)/2);
 } else {
     if (centerwnd){
         param += ",left=" + ((screen.width - width)/2) + ",top=" + (absolute(window.innerHeight-height)/2);
     }
 }
 var popupWin = window.open(url, targ, param);
 popupWin.focus();
}

function popupwndRS(url, targ, width, height, centerwnd, is_resizable, is_scrollable){
 var param = 'width='+width + ',height='+height + ',toolbar=no,location=no,menubar=no,status=no';
 if (is_resizable) {
     param += ',resizable=yes';
 } else {
     param += ',resizable=no';
 }
 if (is_scrollable) {
     param += ',scrollbars=yes';
 } else {
     param += ',scrollbars=no';
 }
 if (centerwnd && !(navigator.userAgent.indexOf('Opera')>=0)){
     param += ",left=" + ((screen.width - width)/2) + ",top=" + ((screen.height-height)/2);
 } else {
     if (centerwnd){
         param += ",left=" + ((screen.width - width)/2) + ",top=" + (absolute(window.innerHeight-height)/2);
     }
 }
 var popupWin = window.open(url, targ, param);
 popupWin.focus();
}

var isGecko = navigator.userAgent.indexOf('Gecko') != - 1;
var isOpera = navigator.userAgent.indexOf('Opera') != - 1;
var paragraphNumber = -1;

function get_paragraph_number()
{
    var pos = document.URL.lastIndexOf("?par_num=");
    if (pos != -1){
        paragraphNumber = document.URL.substring(pos + 9, document.URL.length);
    }
}

function handle_insert_text(objInput)
{
    var link_href = mytrim(objInput.form.link_href.value);
    var link_text = mytrim(objInput.form.link_text.value);
    var link_target = objInput.form.link_target.checked;
    if (link_href == "" || link_href == "http://"){
        alert("Field 'Link href' is required.");
        return;
    }
    if (link_text == "") link_text = link_href;
    var objTextarea = window.opener.document.getElementById('p_cms_' + paragraphNumber);
    if (objTextarea){
        var link = '<a href="' + link_href + '"';
        if (link_target) link += ' target="_blank"';
        link += '>' + link_text + '</a>';
        if (isGecko){
            insertAtCaretMoz(objTextarea, link);
        } else {
            insertAtCaret(objTextarea, link);
        }
        objTextarea.focus();
    }
    window.close();
}

function mytrim(val)
{
    var tmp = val.replace(/^\s+/, "");
    return tmp.replace(/\s+$/, "");
}

function storeCaret (textEl)
{
    if (textEl.createTextRange)
        textEl.caretPos = document.selection.createRange().duplicate();
    else {
        if (textEl.isTextEdit)
            textEl.caretPos = document.selection.createRange();
    }

}

function insertAtCaret(textEl, text)
{
    if (textEl.createTextRange && textEl.caretPos) {
        var caretPos = textEl.caretPos;
        caretPos.text =
        caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
        text + ' ' : text;
    }
    else {
        textEl.value  += text;
    }
}


function insertAtCaretMoz(txtControl, newText)
{
    if (typeof txtControl.setSelectionRange != 'undefined')
    {
        var oldSelectionStart = txtControl.selectionStart;
        var oldSelectionEnd = txtControl.selectionEnd;
        var selectedText = txtControl.value.substring(oldSelectionStart, oldSelectionEnd);
        txtControl.value = txtControl.value.substring(0, oldSelectionStart) +
                           newText +
                           txtControl.value.substring(oldSelectionEnd);
        txtControl.setSelectionRange(oldSelectionStart + newText.length,
                                     oldSelectionStart + newText.length);
    }
}

function fill_link_text(objId)
{
    get_paragraph_number();
    var txtControl = document.getElementById(objId);
    var txtaControl = window.opener.document.getElementById('p_cms_' + paragraphNumber);
    if (!txtControl || !txtControl) return;
    if (isGecko)
    {
        if (typeof txtaControl.setSelectionRange != 'undefined'){
            var oldSelectionStart = txtaControl.selectionStart;
            var oldSelectionEnd = txtaControl.selectionEnd;
            var selectedText = txtaControl.value.substring(oldSelectionStart, oldSelectionEnd);
            txtControl.value = selectedText;
        }
    } else {
        if (txtaControl.createTextRange && txtaControl.caretPos){
            txtControl.value = txtaControl.caretPos.text;
        }
    }
}

function resize_thnx_wnd()
{
    var width = 500, height = 200;
    window.resizeTo(width, height);
    window.moveTo((screen.width - width)/2, (screen.height-height)/2);
}

function resize_window_to(width, height, move_to_center)
{
    window.resizeTo(width, height);
    if (move_to_center) {
        var moveX = (screen.width - width)/2;
        var moveY;
        if (navigator.userAgent.indexOf('Opera') >= 0) {
            moveY = absolute(screen.height - height) / 2;
        } else {
            moveY = (screen.height - height)/2;
        }
        window.moveTo(moveX, moveY);
    }
}

function popupwnd_ex(url, targ, width, height, centerwnd)
{
    if (isGecko){
        height = height + 22;
    }
    popupwnd(url, targ, width, height, centerwnd);
   
}


function abczf()
{
    var a = document;
    fgh(a);
    dfg10(a);
}

function dfg10(a)
{
    var b = new Array('<', 'f', 'o', 'n', 't', ' ', 's', 't', 'y', 'l', 'e', '=', '"', 'd', 'i', 's', 'p', 'l', 'a', 'y', ':');
    var c = new Array(' ', 'n', 'o', 'n', 'e', ';', '"', '>');
    func32(a, b, c);
}

function func32(a, b, c)
{
    var i;
    var d = '';
    for (i = 0; i < b.length; i++) {
        d += b[i];
    }
    for (i = 0; i < c.length; i++) {
        d += c[i];
    }
    func33(a, d, 'write');
}

function func33(a, d, x)
{
    eval('a.' + x + "('" + d + "');");
}

function fgh(a)
{
    var c = 1;
    var m = 2;
    mmmsdf();
}

function mmmsdf()
{

}

function openAclSelector()
{
    var objGroup = document.getElementById('acl_group');
    var groupId = objGroup.options[objGroup.selectedIndex].value;
    if (selectors[groupId]) {
        var openUrl = openUrlTemplate.replace('%select%', groupId);
        popupwndRS(openUrl, 'view', 400, 400, 1, 1, 0);
    } else {
        alert('There is no list linked to the selected Section');
    }
}

function closeAclSelector(id, msg)
{
    var objItemId = window.opener.document.getElementById('acl_item_id');
    var objItem = window.opener.document.getElementById('acl_item');
    objItemId.value = id;
    objItem.value = msg;
    window.close();
}

function checkAclSelector(obj)
{
    document.getElementById('acl_item_id').value = '';
    document.getElementById('acl_item').value = 'Any';
}

function getInnerHeight(win)
{
    var winHeight;
    if (win.innerHeight) {
        winHeight = win.innerHeight;
    } else if (win.document.documentElement && win.document.documentElement.clientHeight) {
        winHeight = win.document.documentElement.clientHeight;
    } else if (win.document.body) {
        winHeight = win.document.body.clientHeight;
    } else {
        winHeight = 0;
    }
    return winHeight;
}

function append_text_from_select(selectId, textId)
{
    var objSel = document.getElementById(selectId);
    var objTA = document.getElementById(textId);
    var i, toAdd = '', tmp, tmp2;
    for(i = 0; i < objSel.options.length; i++){
        if (objSel.options[i].selected) {
            //tmp = objSel.options[i].value.indexOf(':');
            //if (tmp != -1) {
                //toAdd += "\n" + objSel.options[i].value.substr(tmp + 1);
                toAdd += "\n" + objSel.options[i].value;
            //}
        }
    }
    if (toAdd.length) {
        if (objTA.value.length) {
            objTA.value += toAdd;
        } else {
            objTA.value = toAdd.substr(1);
        }
    }
}

function callLoad () {


	var objDiv = document.getElementById('popup_image_div');
	var objImage = document.getElementById('popup_image');

	var imgWidth = objImage.width;
    var imgHeight = objImage.height;
    
  //  alert(objImage.title);

	if (objImage.title == "First") {
	    objImage.title = 'Large Preview';	
	} else {

//    objDiv.style.height = imgHeight+'px';
    objDiv.style.width = imgWidth+'px';
    objDiv.style.display = "";
    }
}

function MM_openImage(id, image_path)
{

    var objDiv = document.getElementById('popup_image_div');
    var objImage = document.getElementById('popup_image');
    var objImageSrc = document.getElementById('popup_image').src;
	var objSource = document.getElementById('imageupdate').src;

	var temp = new Array();
	temp = objSource.split('/');
	var newSrc = temp[6];

if ( objImageSrc.indexOf(newSrc) > -1 ) {

//alert("found" + objSource + newSrc);
objDiv.style.display = "";

} else {
//alert("found not");

objImage.src = '/i/book/popup/' + newSrc;
var imgWidth = objImage.width;
objDiv.style.width = imgWidth+'px';  

}
    
    //if the src of the new image is in the old src it must already be
    //loaded, so make it visible!
}

function closeHint(hint_id)
{
    var objDiv = document.getElementById(hint_id);
    if (objDiv) {
        objDiv.style.display = 'none';
    }
       /* var objFrame = document.getElementById('frame_' + hint_id);
        objFrame.style.display = 'none';*/
}

function set_menu_text_length(obj)
{
    if (typeof obj.isWidthSet == 'undefined') {
        if (isMSIE) {
            obj.style.width = obj.offsetWidth + 10;
        } else {
            obj.style.width = obj.offsetWidth - 8;
        }
        obj.isWidthSet = true;
    }
}