var DLG_SHOW_POS_MOUSE = 0;
var DLG_SHOW_POS_CENTER = 1;
var DLG_SHOW_POS_V_MOUSE_H_CENTER = 2;

//Dialog box variables
var isdrag = false;
var isresize = false;
var x, y;
var dragObj;
var resizeObj;
var csX1;
var csY1;
var csX2;
var csY2;

function WritePlayer(container, id, file) 
{
	document.getElementById(container).innerHTML = 
	    "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' width='17' height='17' id='" +
	        id + "' data='/resources/musicplayer.swf?song_url=" + file + "' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='/resources/musicplayer.swf?song_url=" + file + 
	        "' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='/resources/musicplayer.swf?song_url=" + 
	        file + "' quality='high' bgcolor='#ffffff' width='17' height='17' name='" + id + 
	        "' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object>";
}


function WriteFlashObj(container, id, flashFile, width, height)
{
	document.getElementById(container).innerHTML = 
	    "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' width='" + width + "' height='" + height + "' id='" +
	        id + "' data='" + flashFile + "' align='middle'><param name='WMode' value='Opaque'/><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='" + flashFile + 
	        "' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='" + 
	        flashFile + "' quality='high' bgcolor='#ffffff' width='" + width + "' height='" + height + "' name='" + id + 
	        "' align='middle' allowScriptAccess='sameDomain' WMode='Opaque' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object>";
}


function SetCookie(name, value, isSession) 
{
	var expDate = new Date();
	expDate.setTime(expDate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 
	
	var newCookie = name + "=" + value + "; path=/; "
	if (isSession == false)
		newCookie += "expires=" + expDate.toGMTString();
	document.cookie = newCookie;
}




function GetCookie(name) 
{
	var allCookies = String(document.cookie);	
	var cookieStart = allCookies.indexOf(name);
	if(cookieStart != -1) 
	{
		cookieStart = cookieStart + name.length + 1;
		var cookieEnd = allCookies.indexOf(";", cookieStart);
		if(cookieEnd == -1) 
		{
		    cookieEnd = allCookies.length;
		}
		return allCookies.substring(cookieStart, cookieEnd);
	} 
	else 
	{
		return false;
	}
}




function printarticle(lngId)
{
	strWin = window.open ("/printarticle.php?id=" + lngId , "_blank", "toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,width=800,height=500");
}

//sfHover = function() {
//	var navEl = document.getElementById("nav");
//	if (navEl != null)
//	{
//		var sfEls = navEl.getElementsByTagName("LI");
//		for (var i=0; i<sfEls.length; i++) {
//			sfEls[i].onmouseover=function() {
//				this.className+=" sfhover";
//			}
//			sfEls[i].onmouseout=function() {
//				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
//			}
//		}
//	}
//}
//if (window.attachEvent) window.attachEvent("onload", sfHover);


function SwitchImg(imgName, over)
{
	fld = document.getElementById (imgName + "img");
	eval ("fld.src = img" + imgName + (over ? "over" : "off") +".src;");
	//fld.src = "/_structure/images/system/" + imgName + (over ? "over" : "off") + ".jpg";
}





function GetWindowSize(getWidth) {
  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;
  }
  //window.alert( 'Width = ' + myWidth + ', Height = ' + myHeight);
  if (getWidth) 
    return myWidth
  else
    return myHeight
  
}




function FindPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
    {
        while(1) 
        {
            curleft += obj.offsetLeft;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    }
    else if(obj.x)
    {
        curleft += obj.x;
    }
    return curleft;
}




function FindPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
    {
        while(1)
        {
            curtop += obj.offsetTop;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    }
    else if(obj.y)
    {
        curtop += obj.y;
    }
    return curtop;
}



function GetScrollTop() 
{
    var scrlTop = 0;
    if (!document.all)
    {
        //Non ie
        scrlTop = window.pageYOffset;
    }
    else
    {
        if (document.documentElement && !document.documentElement.scrollTop)
        {
            scrlTop = 0;
        }
        else if (document.documentElement && document.documentElement.scrollTop)
        {
            scrlTop = document.documentElement.scrollTop;
        }
        else if (document.body && document.body.scrollTop)
        {
            scrlTop = document.body.scrollTop;
        }
    }

    return(scrlTop);
}

function FindHeight(obj)
{
    return (obj.offsetHeight);
}



function FindWidth(obj)
{
    return (obj.offsetWidth);
}


function InsertAtCursor(fld, val) 
{
	if (document.selection) 
	{
		//ie support
		fld.focus();
		sel = document.selection.createRange();
		sel.text = val;
	}
	else if (fld.selectionStart || fld.selectionStart == "0") 
	{
		//moz/netscape support
		var startPos = fld.selectionStart;
		var endPos = fld.selectionEnd;
		fld.value = fld.value.substring(0, startPos) + val + fld.value.substring(endPos, fld.value.length);
	} 
	else 
	{
		fld.value += val;
	}
}

function EncloseText(fld, tag) 
{
    var startText = '[' + tag + ']';
    var endText = '[/' + tag + ']';

	if (typeof(fld.caretPos) != "undefined" && fld.createTextRange)
	{
	    //IE
		var caretPos = fld.caretPos;
		var caret_pos_len = caretPos.text.length;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? startText + caretPos.text + endText + ' ' : startText + caretPos.text + endText;

		if (caret_pos_len == 0)
		{
			caretPos.moveStart("character", - endText.length);
			caretPos.moveEnd("character", - endText.length);
			caretPos.select();
		}
		else
		{
			fld.focus(caretPos);
		}
	}
	else if (typeof(fld.selectionStart) != "undefined")
	{
	    //Moz
		var begin = fld.value.substr(0, fld.selectionStart);
		var selection = fld.value.substr(fld.selectionStart, fld.selectionEnd - fld.selectionStart);
		var end = fld.value.substr(fld.selectionEnd);
		var newCursorPos = fld.selectionStart;
		var scrollPos = fld.scrollTop;

		fld.value = begin + startText + selection + endText + end;

		if (fld.setSelectionRange)
		{
			if (selection.length == 0)
				fld.setSelectionRange(newCursorPos + startText.length, newCursorPos + startText.length);
			else
				fld.setSelectionRange(newCursorPos, newCursorPos + startText.length + selection.length + endText.length);
			fld.focus();
		}
		fld.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else
	{
		fld.value += startText + endText;
		fld.focus(fld.value.length - 1);
	}
}

// Remember the current position.
function StoreCaret(fld)
{
	// Only bother if it will be useful.
	if (typeof(fld.createTextRange) != "undefined")
	{
		fld.caretPos = document.selection.createRange().duplicate();
	}
}

function HideMsg(type)
{
    var msg = document.getElementById(type);
    msg.style.display = "none";
}

function AddToFavourites(dispName, url)
{
    /* Firefox support*/
    if (window.sidebar) 
    {
        window.sidebar.addPanel(dispName, url, "");
    }
    /* opera support */
    else if(window.opera && window.print)
    { 
        var anchor = document.createElement('a');
        anchor.setAttribute('href',url);
        anchor.setAttribute('title',dispName);
        anchor.setAttribute('rel','sidebar');
        anchor.click();
    }
    /* Internet Explorer */
    else if(document.all)
    {
    	window.external.AddFavorite(url, dispName);
    }
    /* Unsupported */
    else
    {
        alert ("Unfortunately your browser does not support this feature.");
    }
    
}


function mtf(e)
{
	var p = '\u006d\u0061\u0069\u006c\u0074\u006f\u003a';
	void(top.location = p + e);
	return false;
}

function dn()
{
}

function ShowImg(obj)
{
	obj.style.display = 'block';
//	var parent = obj.parentNode;
//	if (parent != null)
//	{
//	    parent.style.visibility = 'visible';
//	    parent.parentNode.style.visibility = 'visible';
//	}
}


function DetectImageSize(picName,picTitle)
{
      var picURL = picName.src;
      //location.href = picURL;
      //return;
      //alert (picURL);
      var imgurl = "/dispimage.php?t=" + picTitle + "&w=" + picName.width + "&h=" + picName.height + "&i=" + picURL;
      var newWindow = window.open(imgurl, 'newWin', 'resize=yes,toolbar=no,width='+picName.width+',height='+(picName.height+130));
      //newWindow.document.write('<html><head><title>'+picTitle+'<\/title><\/head><body background="'+picURL+'"><\/body><\/html>');
      //newWindow.resizeBy(picName.width-newWindow.document.body.clientWidth,picName.height-newWindow.document.body.clientHeight);
      newWindow.focus();
} 

function InitSearch(fld, clr)
{
    if (fld.value.toLowerCase().substr(0, 6) == "search")
    {
        fld.value = "";
        fld.style.color = clr;
    }
    
}

function InitLogon(fld, clr)
{
    var curVal = fld.value;
    
    switch (curVal)
    {
        case "email address":
            fld.value = "";
            fld.style.color = clr;
            break;

        case "password":
            fld.style.display = "none";
            var pwd = document.getElementById("txtPassword");
            pwd.style.display = "inline";
            pwd.style.color = clr;
            pwd.focus();
            break;
    }
    
}

function StoreFieldData(fld)
{
    var val = encodeURIComponent(fld.value);
    SetCookie("fld_" + fld.id, val, true);
}



function ShowDialog(e, dlgId, text, showPos, width, height) 
{
    var posx = 0;
    var posy = 0;
    if (width == null) 
    {
        width = 250;
    }
    if (height == null) 
    {
        height = 250;
    }
    switch (showPos) 
    {
        case DLG_SHOW_POS_MOUSE:
            if (!e) var e = window.event;
            if (e.pageX || e.pageY) 
            {
                posx = e.pageX;
                posy = e.pageY;
            }
            else if (e.clientX || e.clientY) 
            {
                posx = e.clientX;
                posy = e.clientY;
            }
            break;
            
        case DLG_SHOW_POS_CENTER:
            //Get screen dims
            var winWidth = GetWindowSize(true);
            var winHeight = GetWindowSize(false);
            posx = (winWidth / 2);
            posy = GetScrollTop() + (winHeight / 2) - 85; //100-15
            break;
            
        case DLG_SHOW_POS_V_MOUSE_H_CENTER:
            var winWidth = GetWindowSize(true);
            if (!e) var e = window.event;
            if (e.pageX || e.pageY) {
                posy = e.pageY;
            }
            else if (e.clientX || e.clientY) 
            {
                posy = e.clientY;
            }
            posx = (winWidth / 2);
            break;
    }

    var dlgObj = document.getElementById(dlgId);
    var dlgInner = document.getElementById(dlgId + "Inner");
    dlgObj.style.display = 'block';
    if (text != '')
    {
        dlgInner.innerHTML = text;
    }
    dlgObj.style.left = (posx - (width / 2)) + "px";
    dlgObj.style.top = (posy + 15) + "px";
    dlgObj.style.width = width + "px";
    dlgObj.style.height = height + "px";
    dlgInner.style.width = (width - 10) + "px";
    dlgInner.style.height = (height - 50) + "px";



}



function HideDialog(fld, id)
{
    var dlgObj = (fld != null) ? fld.parentNode.parentNode : document.getElementById(id);
    dlgObj.style.display = 'none';

}

function CancelDrag() 
{
    isdrag = false;
}

function CancelResize() {
    isresize = false;
}

function MoveMouse(e) 
{
    var ie = document.all;
    var nn6 = document.getElementById && !document.all;
    if (isdrag) 
    {
        var left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
        var top = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
        //alert("left = " + left + ", top = " + top);
        //if (left > csX1 && left < csX2) 
        //{
            dragObj.style.left = left + "px";
        //}

       //if (top > csY1 && top < csY2) 
       // {
            dragObj.style.top = top + "px";
       // }
        return false;
    }
}

function ResizeMouse(e) 
{
    var ie = document.all;
    var nn6 = document.getElementById && !document.all;
    if (isresize) 
    {
        var width = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
        var height = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
        //alert("left = " + left + ", top = " + top);
        //if (left > csX1 && left < csX2) 
        //{
        var dlgInner = document.getElementById(resizeObj.id + "Inner");
        resizeObj.style.width = width + "px";
        dlgInner.style.width = (width - 10) + "px";
        //}

        //if (top > csY1 && top < csY2) 
        // {
        resizeObj.style.height = height + "px";
        dlgInner.style.height = (height - 50) + "px";
        // }
        return false;
    }
}


function SelectMouse(e) 
{
    var ie = document.all;
    var nn6 = document.getElementById && !document.all;
    var bg = document.getElementById("screen");
    csX1 = 0; //FindPosX(bg);
    csY1 = 0; //FindPosY(bg);
    csX2 = csX1 + FindWidth(bg) - 50;
    csY2 = csY1 + FindHeight(bg) - 117;

    //alert(csX2 + ", " + csY2);
    var fobj = nn6 ? e.target : event.srcElement;
    //alert(fobj.id);
    var topelement = nn6 ? "HTML" : "BODY";
    while (fobj.tagName != topelement && fobj.className != "dialogBox") 
    {
        fobj = nn6 ? fobj.parentNode : fobj.parentElement;
    }
    if (fobj.className == "dialogBox") 
    {
        isdrag = true;
        dragObj = fobj;
        tx = parseInt(dragObj.style.left + 0, 10);
        ty = parseInt(dragObj.style.top + 0, 10);
        x = nn6 ? e.clientX : event.clientX;
        y = nn6 ? e.clientY : event.clientY;
        //alert("here");
        document.onmousemove = MoveMouse;
        return false;
    }
}


function SelectResize(e) 
{
    var ie = document.all;
    var nn6 = document.getElementById && !document.all;

    //alert(csX2 + ", " + csY2);
    var fobj = nn6 ? e.target : event.srcElement;
    
    var topelement = nn6 ? "HTML" : "BODY";
    while (fobj.tagName != topelement && fobj.className != "dialogBox") 
    {
        fobj = nn6 ? fobj.parentNode : fobj.parentElement;
    }
    //alert(fobj.id);
    if (fobj.className == "dialogBox") 
    {
        isresize = true;
        resizeObj = fobj;
        tx = parseInt(resizeObj.style.width + 0, 10);
        ty = parseInt(resizeObj.style.height + 0, 10);
        x = nn6 ? e.clientX : event.clientX;
        y = nn6 ? e.clientY : event.clientY;
        //alert("here");
        document.onmousemove = ResizeMouse;
        return false;
    }
}
