var newWin = null;
var partnerNewWin = null;
var printCommentNewWin = null;
var defltOffset = 130;
var divider=8;
var offset = 10;
var dictLeftOffset = 220;
//setTimeout('setReadBarTop();', 200);
window.onload=onWindowLoad;

if(!window.event && window.captureEvents)
{
	window.captureEvents(Event.MOUSEOVER|Event.MOUSEOUT|Event.CLICK|Event.DBLCLICK|Event.KEYDOWN);
	window.onmouseover = getEventHandler;
	window.onmouseout = getEventHandler;
	window.onclick = getEventHandler;
	window.ondblclick = getEventHandler;
	window.onkeydown =  getEventHandler;
	window.event = new Object;
}

function getEventHandler(e)
{
	window.event.clientX = e.pageX;
	window.event.clientY = e.pageY;
	window.event.x = e.layerX;
	window.event.y = e.layerY;
	window.event.screenX = e.screenX;
	window.event.screenY = e.screenY;
	window.event.keyCode = e.which;
	return ((getEventHandler(e)==false)?false:true);
}

function showDict(clickedEl, showElId)
{
	var dictDiv = document.getElementById(showElId);
	dictDiv.className='dictionaryDivShow';
	dictDiv.style.left = window.event.clientX - dictLeftOffset;
	if(document.all)
	{
	    dictDiv.style.top = window.event.clientY + document.body.scrollTop;
	}
	else
	{
	    dictDiv.style.top = window.event.clientY + document.documentElement.scrollTop;
	}
}

function hideDict(hideElId)
{
	var dictDiv = document.getElementById(hideElId);
	dictDiv.className='dictionaryDivHide';
}

function onWindowLoad()
{
    setReadBarTop();
    changeSearchButtonName();
    if (!window.XMLHttpRequest)
    {
        window.XMLHttpRequest = function(){
        return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }    
}

function setReadBarTop()
{
   var sidec = document.getElementById('tblSidePanel');
   var dY = document.body.scrollTop  - parseFloat(sidec.style.top);
//   if(sidec!=null)
//   {
//        sidec.style.top = (document.body.scrollTop >= defltOffset)?offset + document.body.scrollTop:defltOffset;
//        setTimeout('setReadBarTop();', 80);
//   }
    if(sidec!=null)
    {
        if(document.body.scrollTop >  parseFloat(sidec.style.top) || parseFloat(sidec.style.top)>defltOffset)
        {
            if(Math.abs(dY)<divider){divider=divider-1>0?divider-1:1;}else{divider=8;}
            sidec.style.top = parseFloat(sidec.style.top) + dY/divider;
        }
        if(parseInt(sidec.style.top)<=defltOffset){sidec.style.top=defltOffset;}
        setTimeout('setReadBarTop();', 40);
    }
}

function openPartnerWindow(url)
{
	closeWindow(partnerNewWin);
	partnerNewWin = window.open(url, "ABOMPartnerWin","toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=0,width=700,height=500,left=200,top=200");
}
function openWindow(url, width, height)
{
	closeWindow(newWin);
	newWin = window.open(url, "ABOMNewWin","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width="+width+",height="+height+",left=200,top=200");
}
function openCommentPrintWindow(cmntTdId)
{
    var html = '<html><head><title>Print Commentary</title></head><body>';
    html+='<link href="Styles.css" rel="stylesheet" type="text/css" />';
    html+='<script type="text/javascript" language="javascript" src="Main.js"></script>'
    commentHtml=document.getElementById(cmntTdId).innerHTML;
    var pos1 = commentHtml.indexOf('<DIV');
    if(pos1>0)
    {
        var pos2 = commentHtml.indexOf('>',pos1);
        var pos3 = commentHtml.indexOf('</DIV');
        html+=commentHtml.substring(pos2+1,pos3)
        html+='</body></html>';
        
        closeWindow(printCommentNewWin);
        printCommentNewWin = window.open("empty.htm", "ABOMCommentPrintWin","toolbar=1,location=1,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=700,height=600,left=200,top=200");
        printCommentNewWin.document.write(html);
    }

}
function closeWindow(winName)
{
	if (winName != null && typeof winName == "object")
	{
		winName.close();
	}
}
function getQueryStringValue(key)
{
    var queryStringVariableArray;
    var urlQArray = document.URL.split("?");
    var queryString = (urlQArray.length>1)?urlQArray[1]:urlQArray[0];
    queryString = queryString.split("#")[0];
    var queryStringVariableArray = queryString.split("&");
    for (var icount = 0; icount < queryStringVariableArray.length;icount++)
    {
        var nameValue;
        nameValue = queryStringVariableArray[icount].split("=");
        if (nameValue[0] == key)
        {
              return nameValue[1];
        }
    }
    return null;
}

function getBookMark()
{
    var bookMark=null;
    var urlArray = document.URL.split("#");
    if(urlArray.length>1)
    {
        bookMark= urlArray[1];
    }
    return bookMark;
}

function changeFontSize(eleId,dfltSize,upDown) 
{
    var ele =document.getElementById(eleId);
    var size;
    if(ele.style.fontSize) 
    {
        size = parseInt(ele.style.fontSize.replace("px",""));
    } 
    else 
    {
        size = parseInt(dfltSize);
    }
    if(upDown=='up')
    {
        size += 1;
    }
    else
    {
        size -= 1;
    }
    ele.style.fontSize = size+"px"
}
function getCheckedCheckBoxValues(checkBoxObj) 
{
    var retVal = "";
	if(checkBoxObj)
	{
	    var length = checkBoxObj.length;
	    if(length == undefined && checkBoxObj.checked)
	    {
		    retVal= checkBoxObj.value;
	    }
	    else
	    {
	        for(var i = 0; i < length; i++) 
	        {
		        if(checkBoxObj[i].checked) 
		        {
                    if(retVal!=""){retVal+=",";}
                    retVal+=checkBoxObj[i].value;
		        }
	        }
	    }
	}
	return retVal;
}

function getCheckedRadioButtonValue(radioBtnObj) 
{
    var retVal = "";
	if(radioBtnObj)
	{
	    var length = radioBtnObj.length;
	    if(length == undefined && radioBtnObj.checked)
	    {
	        retVal = radioBtnObj.value;
	    }
	    else
	    {
	        for(var i = 0; i < length; i++) 
	        {
		        if(radioBtnObj[i].checked) 
		        {
			        retVal = radioBtnObj[i].value;
			        break;
		        }
	        }
	    }
	}
	return retVal;
}

function setCheckedRadioButtonValue(radioBtnObj, newValue) 
{
	if(!radioBtnObj){return;}
	var length = radioBtnObj.length;
	if(length == undefined) 
	{
		radioBtnObj.checked = (radioBtnObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < length; i++)
	{
		radioBtnObj[i].checked = false;
		if(radioBtnObj[i].value == newValue.toString()) 
		{
			radioBtnObj[i].checked = true;
		}
	}
}

function findPosition(element) 
{
    if( typeof( element.offsetParent ) != 'undefined' )
    {
        for( var posX = 0, posY = 0; element; element = element.offsetParent ) 
        {
            posX += element.offsetLeft;
            posY += element.offsetTop;
        }
        return [ posX, posY ];
     } 
     else 
     {
        return [ element.x, element.y ];
     }
}
function trim(str)
{
    return str.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1');
}

