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='/_structure/images/musicplayer.swf?song_url=" + file + "' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='/_structure/images/musicplayer.swf?song_url=" + file + 
	        "' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='/_structure/images/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 (tag == 'url')
    {
        var theurl = prompt("Please enter the full url to link to", "http://");
        if (theurl == null)
        {
            return (false);
        }
        else
        {
            startText = '[' + tag + ' lnk=\'' + theurl + '\']';
        }
    }

	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, s)
{
    var p = '\u006d\u0061\u0069\u006c\u0074\u006f\u003a';
    var loc = p + e;
    if (s != '')
    {
        loc += "?subject=" + s;
    }
    void (top.location = loc);
	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.toLowerCase().substr(0, 6) == "enter "))
    {
        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;
    }
}


function CheckEnterPressed(e, fld)
{
    if (window.event)
    {
	    e = window.event;
	    if (e.keyCode == 13)
	    {
	        e.cancelBubble = true;
	        document.getElementById(fld).click();
	        return (false);
	    }
    }
    else
    {
	    if (e.keyCode == 13)
	    {
	        //e.cancelBubble = true;
	        document.getElementById(fld).click();
	        return (false);
	    }
    }
    
    return (true);
}


function ClearListBox (fldid)
{
    var fld = document.getElementById(fldid);
    while (fld.options.length > 0)
    {
        fld.options[0] = null;
    }
}

function ToggleQuestion (id)
{
	var div;
	div = document.getElementById ("divQ" + id);
	div.style.display= (div.style.display == "none") ? "block" : "none";
}

function LoadImage(imgName)
 {
    var img = new Image();
    img.src = imgName;
    return (img);
}

function ShowImgPreview(event, picName, index) 
{

    var pvw = document.getElementById("imgPvwPanel_" + index);
    pvw.style.display = "block";
    var imgpvw = document.getElementById("imgPreview_" + index);
    imgpvw.src = picName.src;
    pvw.style.width = (picName.width + 20) + "px";
    pvw.style.height = (picName.height + 40) + "px";
    var winWidth = GetWindowSize(true);
    var winHeight = GetWindowSize(false);
    var scrollTop = GetScrollTop();
    var posx = (winWidth / 2) - (picName.width / 2);
    var posy = (winHeight / 2) - (picName.height / 2);
    pvw.style.top = scrollTop + posy + "px";
    pvw.style.left = posx + "px";
}

function HideImgPreview(index) 
{
    var pvw = document.getElementById("imgPvwPanel_" + index);
    pvw.style.display = "none";
}

var getElementsByClassName = function(className, tag, elm) {
    if (document.getElementsByClassName) {
        getElementsByClassName = function(className, tag, elm) {
            elm = elm || document;
            var elements = elm.getElementsByClassName(className),
                nodeName = (tag) ? new RegExp("\\b" + tag + "\\b", "i") : null,
               returnElements = [],
               current;
            for (var i = 0, il = elements.length; i < il; i += 1) {
                current = elements[i];
                if (!nodeName || nodeName.test(current.nodeName)) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    else if (document.evaluate) {
        getElementsByClassName = function(className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
                classesToCheck = "",
                xhtmlNamespace = "http://www.w3.org/1999/xhtml",
                namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace) ? xhtmlNamespace : null,
                returnElements = [],
                elements,
                node;
            for (var j = 0, jl = classes.length; j < jl; j += 1) {
                classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
            }
            try {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
            }
            catch (e) {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
            }
            while ((node = elements.iterateNext())) {
                returnElements.push(node);
            }
            return returnElements;
        };
    }
    else {
        getElementsByClassName = function(className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
                classesToCheck = [],
                elements = (tag === "*" && elm.all) ? elm.all : elm.getElementsByTagName(tag),
                current,
                returnElements = [],
                match;
            for (var k = 0, kl = classes.length; k < kl; k += 1) {
                classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
            }
            for (var l = 0, ll = elements.length; l < ll; l += 1) {
                current = elements[l];
                match = false;
                for (var m = 0, ml = classesToCheck.length; m < ml; m += 1) {
                    match = classesToCheck[m].test(current.className);
                    if (!match) {
                        break;
                    }
                }
                if (match) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    return getElementsByClassName(className, tag, elm);
};


function setActiveStyleSheet(title) 
{
    var i, a, main;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) 
    {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
        {
            a.disabled = true;
            if (a.getAttribute("title") == title) a.disabled = false;
        }
    }
}

function getActiveStyleSheet() 
{
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) 
    {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
    }
    return null;
}

function getPreferredStyleSheet() 
{
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) 
    {
        if (a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
    }
    return null;
}

function loadActiveStylesheet() 
{
    var cookie = GetCookie("style");
    var title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title);
}

function saveActiveStylesheet() 
{

    var title = getActiveStyleSheet();
    SetCookie("style", title, false);

}
