/*******************************
    Script for main template
********************************/

var subMenuStartTime = 0;
var subMenuTop = 0;
var subMenuBottom = 0;
var subMenuLeft = 0;
var subMenuRight = 0;

function closesubnav(event){
	if ( visibleMenu != "none" &&
         (event.clientY < subMenuTop || event.clientY > subMenuBottom
            || event.clientX < subMenuLeft || event.clientX > subMenuRight)){
        document.getElementById(visibleMenu).style.display='none';
		visibleMenu = "none";
    }
}

    function elementPosition(obj) {
        var curleft = 0, curtop = 0;
        var bodyScrollTop = 0;
        if (obj.offsetParent) {
            curleft = obj.offsetLeft;
            curtop = obj.offsetTop;

            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
                bodyScrollTop = obj.scrollTop;
            }
        }
        curtop -= bodyScrollTop;
        return { x: curleft, y: curtop };
    }

function showAdminButton(tabName, e){
    adminTab = document.getElementById(tabName);
//    adminTab.style.left = e.clientX-50;
//    adminTab.style.top = e.clientY+40;
    adminTab.style.display='block';
    setTimeout("adminTab.style.display='none'", 5 * 1000);
}

function showsubnav(tabName, subMenuName){
    if (visibleMenu != "none" && visibleMenu != subMenuName) {
        document.getElementById(visibleMenu).style.display='none';
    }
	if (subMenuName != "none") {
        if (tabName == null){
            alert("Tab with id='null' is not found");
        } else {
            var tabElement = document.getElementById(tabName);
            if (tabElement == null){
                alert("Tab with id='"+tabName+"' is not found");
            } else {
                var subtabElement = document.getElementById(subMenuName);
                var tabPos = elementPosition(tabElement);

                var width = getDocumentWidth();
                if (width < (tabPos.x +128)){
                    subtabElement.style.left = width - 128;
                } else {
                    subtabElement.style.left = tabPos.x-5;
                }

                subtabElement.style.display='block';
                var subtabPos = elementPosition(subtabElement);
                subMenuTop = tabPos.y;
                subMenuBottom = subtabPos.y + subtabElement.clientHeight+15;

                subMenuLeft = subtabPos.x - 15;
                subMenuRight = subtabPos.x + subtabElement.clientWidth+15;



                visibleMenu = subMenuName;
                subMenuStartTime = new Date().getTime();
                //setTimeout("closeSubNavMenu()", 5 * 1000);
            }
        }
    }
}
function closeSubNavMenuWithDelay (subMenuName) {
    setTimeout("closeSubNavMenu('"+subMenuName+"')", 3 * 1000);
}
function closeSubNavMenu () {
    if (new Date().getTime() - subMenuStartTime < 4.5 * 1000) return;
    if (visibleMenu != "none") { document.getElementById(visibleMenu).style.display='none'; }
    visibleMenu = "none";
}

function setdiv() {
	y = (getDocumentHeight() - headY);
	width = getDocumentWidth();
	if (y > 0) { divY = (y);
	} else { divY = 1; }

	document.getElementById('pageHeader').style.height = headY;
	document.getElementById('pageHeader').style.width = width;
	document.getElementById('hcontent').style.height = headY - 36;
    document.getElementById('pageBody').style.top = headY;
	document.getElementById('pageBody').style.height = divY;
	document.getElementById('pageBody').style.width = (width<1015?width:1015);
    if (document.getElementById('leftMenuBody') != null) {
        document.getElementById('leftMenuBody').style.height = divY;
        document.getElementById('leftMenuBody').style.width = 215;
        if (document.getElementById('rightBody') != null) {
            document.getElementById('rightBody').style.height = divY - 102 - 30;
            document.getElementById('rightBody').style.width = width - 215;
        }
    }
    if(document.getElementById('menuBar') != null){
        document.getElementById('menuBar').style.width = width - 225;
    }
    if(document.getElementById('logo') != null){
        document.getElementById('logo').style.width = (width>900?width:900);
    }
	if(document.getElementById('copyright') != null){
        document.getElementById('copyright').style.width = width - 225 - 60;
	}
    if (width < 900){
        document.body.style.overflowX = 'auto';
    } else {
        document.body.style.overflowX = 'hidden';
    }
    //hide the header if not used
    /*
    if (headY <= 100)
    {
        document.getElementById('hcontent').style.visibility = "hidden";
    } */

    if (document.getElementById('pageHeader') != null) {
        document.getElementById('pageHeader').style.visibility = 'visible';
    }
    if (document.getElementById('homeOurLocation') != null) {
        document.getElementById('homeOurLocation').style.display = 'block';
    }    
    if (document.getElementById('rightBody') != null) {
        document.getElementById('rightBody').style.visibility = 'visible';
    }
    if (document.getElementById('copyright') != null) {
        document.getElementById('copyright').style.visibility = 'visible';
    }
}
	
function getDocumentHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) { //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) { //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) { //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getDocumentWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) { //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) { //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth ) { //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
	if (myWidth > 1015){
	    myWidth = 1015;
	}
  return myWidth;
}

function hidemessages() {
    messageComponent.innerHTML = "<empty>";
    messageComponent.style.overflow = "hidden";
    messageComponent.style.height="0px";
    headY = headY - 6 + messageComponent.offsetHeight;
    setdiv();
}
function nofeature() {
    alert("Feature not yet implemented.");
}


//global variables to track form values
var referenceArray = [];
var testArray = [];
var dirtyFlag = false;

function checkDirtyPage(mode) {
    countArray = 0;
    testVal = "";
    if (mode != "store" && referenceArray.length < 1) { document.location = mode; return; } //this page is not being checked so leave
   
    //get all the form values
    allInputs = document.getElementsByTagName("INPUT");
    allSelects = document.getElementsByTagName("SELECT");

    //load the appropriate array
    for (x=0;x<allInputs.length;x++) {
        countArray = x;
        inputType = allInputs[x].type;
        if ((inputType == "text" || inputType == "textarea") && allInputs[x].dirty != "ignore") { testVal = allInputs[x].value; }
        if ((inputType == "radio" || inputType == "checkbox")  && allInputs[x].dirty != "ignore") { testVal = allInputs[x].checked; }
        if (mode == "store") { referenceArray[x] = testVal; }
        else { testArray[x] = testVal; }
    }
    for (x=0;x<allSelects.length;x++) {
        countArray++;
        selectType = allSelects[x].type;
        if ((selectType == "select-one" || selectType == "select-multiple") && allSelects[x].dirty != "ignore") { testVal = allSelects[x].selectedIndex; }
        if (mode == "store") { referenceArray[countArray] = testVal; }
        else { testArray[countArray] = testVal; }
    }
    //compare the arrays, and display a message if the form is dirty
    if (mode != "store") {
        for (x=0;x<testArray.length;x++) {
            if (referenceArray[x] != testArray[x] || dirtyFlag == true) { dirtyFlag = true; }
        }
        if (dirtyFlag) {
            if (confirm("You made changes to this form. Continue without saving?")) { 
                document.location = mode; 
            }
        }
        else { document.location = mode; }
    }
}


function fixPath(data) {
    var mycookie = document.cookie;
 //   if (document.cookie.match(/IV_JCT=%2F(.*)(;*)/)==null) {
   //     return data;
   // } else {
     //   return '/' + document.cookie.match(/IV_JCT=%2F(.*)(;*)/)[1] + data;
    //}
    
 	var mycookie = document.cookie;
	if (mycookie.match(/IV_JCT=(%2F)+([^;]*)/)!=null) {
        return '/' + mycookie.match(/IV_JCT=(%2F)+([^;]*)/)[2] + data;
    } else {
        return data;
    }

}


///////////////////// Double-click trapper ////////////////////////////////////////////////////////

// Customize...
var TrapClicksForHowManySeconds = 120;
var AlertBoxMessage = "One click is sufficient. The server should respond momentarily.\n\n\nClick the \"OK\" button.";

// Initialize...
var TrapTime = 0;
var DoubleClickTrapperCounter = 0;

function DoubleClickTrapperAction(NameOfSubmitButton,NewTextForSubmitButton) {
DoubleClickTrapperCounter++;
var trapTheClick = false;
if(DoubleClickTrapperCounter > 1 ) { trapTheClick = true; }
if(trapTheClick == true) {
	if(TrapTime == 0) {
		var tDate = new Date;
		TrapTime = tDate.valueOf();
		}
	else {
		var tDate = new Date;
		var localTrapTime = tDate.valueOf();
		if((localTrapTime - TrapTime) > (TrapClicksForHowManySeconds * 1000)) {
			TrapTime = 0;
			trapTheClick = false;
			DoubleClickTrapperCounter = 0;
			}
		}
	}
var valueToReturn = true;
if(trapTheClick == true) {
	valueToReturn = false;
	alert(AlertBoxMessage);
	}
else
{
	document.getElementById(NameOfSubmitButton).value = NewTextForSubmitButton;
}
return valueToReturn;
}

function openWindow(url) {
    window.open(url);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//-->