﻿var nn4 = (navigator.appName.indexOf("Netscape") > -1 && navigator.appVersion.indexOf("4") > -1) ? true:false
var ie = (document.all) ? true:false
var nn6 = (document.getElementById) ? true:false

//------------------------------------------------------------------------------
if (document.getElementById) {
	window.alert = function(txt) {
		createCustomAlert(txt);
	}
}

//------------------------------------------------------------------------------
function confirmDelete(objectName) {
  var result = window.confirm("Are you sure you want to delete " + objectName + "?");
  return result;
}

//------------------------------------------------------------------------------
function basicConfirm(message) {
  if (message.lastIndexOf('?') < message.length-1) {
    message = message+"?";
  }
  var result = window.confirm(message);
  return result;
}

//------------------------------------------------------------------------------
function getLayer(id){
  var layer = null;
  if (document.getElementById) {
  	layer = document.getElementById(id); // real browsers
  }
  else if (document.all) {
    layer = document.all[id]; //IE4
  }
  else if (document.layers) {
    layer = document.layers[id]; // NN4
  }
  else {
    alert("Unsupported Browser!! (no method to get elements by id)");
  }
  return layer;
}

//------------------------------------------------------------------------------
function showLayer(layer_id) {
  var layer = getLayer(layer_id);
  if (layer.display) {
    layer.display = ''; // blank so that the correct defaults are chosen by various browsers (and works on rows of tables in addition to divs)
    layer.visibility = 'visible';
  } else {
    layer.style.display = ''; // blank so that the correct defaults are chosen by various browsers (and works on rows of tables in addition to divs)
    layer.style.visibility = 'visible';
  }
}

//------------------------------------------------------------------------------
function isHidden(layer_id) {
  var layer = getLayer(layer_id);
  if (layer.display) {
    return layer.visibility == 'hidden';
  } else {
    return layer.style.visibility == 'hidden';
  }
}

//------------------------------------------------------------------------------
function isShown(layer_id) {
  var layer = getLayer(layer_id);
  if (layer.display) {
    return layer.visibility != 'hidden';
  } else {
    return layer.style.visibility != 'hidden';
  }
}

//------------------------------------------------------------------------------
function hideLayer(layer_id) {
  var layer = getLayer(layer_id);
  if (layer.display) {
    layer.display = 'none';
    layer.visibility = 'hidden';
  } else {
    layer.style.display = 'none';
    layer.style.visibility = 'hidden';
  }
}

//------------------------------------------------------------------------------
function toggleLayer(id) {
	var layer = getLayer(id);
	var isHidden;

	if (layer.display) {
    	isHidden = (layer.display == 'none' || layer.visibility == 'hidden');
	}
	else {
		isHidden = (layer.style.display == 'none' || layer.style.visibility == 'hidden');
	}
	
	if (isHidden) {
		showLayer(id);
	}
	else {
		hideLayer(id);
	}
}

//------------------------------------------------------------------------------
function checkOrUncheckAll(form, elementname, checked) {
  for (i=0; i < form.elements.length; i++ ) {
    if (form.elements[i].name == elementname) {
      if (form.elements[i].disabled == false) {
        form.elements[i].checked=checked;
      }
    }
  }
}

//------------------------------------------------------------------------------
function newHelpWindow(uri, title) {
	var serverUrl = "";
	var stringArray = location.href.split('/');
	if (stringArray.length >= 3) {
		serverUrl += stringArray[0] + "//";
		serverUrl += stringArray[2];
	}
	window.open(serverUrl + uri, title);
}

//------------------------------------------------------------------------------
function openPopup(uri, title, width, height) {
	//alert('openPopup() start');
	if (title == null) {
		title = "_blank";
	}
	var popW = 600, popH = 360;

	if (width != null) {
		popW = width;
	}

	if (height != null) {
		popH = height;
	}
	
	var w = 700, h = 600;
	if (screen != null) {
		w = screen.availWidth;
   		h = screen.availHeight;
	}
	else if (document.all) {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	else if (document.layers) {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	var leftPos = (w-popW)/2;
	var topPos = (h-popH)/2;

	var popupProps = "";
	popupProps += "width="+popW+",height="+popH;
	popupProps += ",top="+topPos+",left="+leftPos;
	//popupProps += ",scrollbars=no";
	popupProps += ",status=1";
	//alert('popupProps:'+popupProps);

    var popup = window.open(uri, title,  popupProps);
    popup.opener = window;
    
	//alert('openPopup() done');
}

//------------------------------------------------------------------------------
function getWindowElementById(wndw, aID) {
   	var element = null;

	if (document.getElementById) // Real Browsers
   		element = wndw.document.getElementById(aID)
	else if (document.all) // IE4
  		element = wndw.document.all[aID];
	else if (document.layers) // NN4
  		element = wndw.document.layers[aID]

	return element;
}

//------------------------------------------------------------------------------
function getElem(id){
  var layer = null;
  if (document.getElementById) {
  	layer = document.getElementById(id); // Real Browsers
  }
  else if (document.all) {
    layer = document.all[id]; // IE4
  }
  else if (document.layers){
    layer = document.layers[id]; // NN4
  }
  else {
    alert("Unsupported Browser!! (no method to get elements by id)");
  }
  return layer;
}

//------------------------------------------------------------------------------
function getDOMElementText(elem) {
    if (elem.childNodes != null && elem.childNodes.length > 0) {
        return elem.childNodes[0].nodeValue;
    } else {
        return "";
    }
}

//------------------------------------------------------------------------------
function getDOMFirstChildElementText(elem, child) {
    var childElems = elem.getElementsByTagName(child);
    if (childElems == null) {
        return "";
    } else {
        var childElem = childElems[0];
        if (childElem != null) {
            return getDOMElementText(childElem);
        } else {
            return "";
        }
    }
}

//------------------------------------------------------------------------------
function getDOMFirstChildElement(elem, child) {
    var childElems = elem.getElementsByTagName(child);
    if (childElems == null) {
        return null;
    } else {
        return childElems[0];
    }
}

//------------------------------------------------------------------------------
function getDOMChildElements(elem, child) {
    return elem.getElementsByTagName(child);
}

//------------------------------------------------------------------------------
function windowHeight() {
    // yes these really are all window hieghts, despite the misleading names
    if( typeof( window.innerHeight ) == 'number' ) {
        //Non-IE
        return window.innerHeight;
    } else if( document.documentElement && document.documentElement.clientHeight ) {
        //IE 6+ in 'standards compliant mode'
        return document.documentElement.clientHeight;
    } else if( document.body && document.body.clientHeight ) {
        //IE 4 compatible
        return document.body.clientHeight;
    }
    return 0;
}
    
//------------------------------------------------------------------------------
function windowWidth() {
    // yes these really are all window hieghts, despite the misleading names
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        return window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth ) {
        //IE 6+ in 'standards compliant mode'
        return document.documentElement.clientWidth;
    } else if( document.body && document.body.clientWidth ) {
        //IE 4 compatible
        return document.body.clientWidth;
    }
    return 0;
}

//------------------------------------------------------------------------------
function createCustomAlert(txt) {

	// create the modalContainer div as a child of the BODY element
	mObj = document.getElementsByTagName("body")[0].appendChild(document.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(document.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(document.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(document.createElement("h1"));
	h1.appendChild(document.createTextNode("Alert"));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(document.createElement("p"));
	msg.appendChild(document.createTextNode(txt));

	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(document.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(document.createTextNode("OK"));
	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { removeCustomAlert();return false; }

    resizePopup(300, 100, "alertBox");
}

//------------------------------------------------------------------------------
function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(getLayer("modalContainer"));
}

//------------------------------------------------------------------------------
function showPopup(url, width, height) {
   var screenElem = document.getElementsByTagName("body")[0].appendChild(document.createElement("div"))
   screenElem.id = "fixed-screen";
   
   var homeFlashElement = document.getElementById("flashAlt");
   var footer = document.getElementById("footer");
   
   if(homeFlashElement != null && footer!=null)
   {
   footer.visible = false;
        homeFlashElement.style.display="none";
        }
    
   var iframe = document.createElement("iframe");
   iframe.src = url;
   var popupElem = screenElem.appendChild(iframe);
   popupElem.id = "floatingframe";
   var maskElem =  screenElem.appendChild(document.createElement("div"));
   maskElem.id = "maskingdiv";

   resizePopup(width, height);

   // if we switch to object
   // popupElem.data=url;
}

//------------------------------------------------------------------------------
function hidePopup() {
    var body = document.getElementsByTagName("body")[0];
    var layer = getLayer( "fixed-screen" );
    body.removeChild( layer );
     var homeFlashElement = document.getElementById("flashAlt");
   
   if(homeFlashElement != null)
        homeFlashElement.style.display="block";
}

//------------------------------------------------------------------------------
function hidePopupAndRedirect(redirectUrl,formID) {

    formID.target="_blank";
    formID.action = redirectUrl;
    
    formID.submit();

   var body = document.getElementsByTagName("body")[0];
    var layer = getLayer( "fixed-screen" );
    body.removeChild( layer );
         var homeFlashElement = document.getElementById("flashAlt");

   if(homeFlashElement != null)
        homeFlashElement.style.display="block";

}

function ChangeFormMethod(theForm)
{       
    theForm.method = "get";
    
}

//------------------------------------------------------------------------------
function replaceURL() {
	var currUrl = document.URL;
	window.location.replace(currUrl);
}

//------------------------------------------------------------------------------
function centerPopup(popupId) {
    if (popupId == null) {
        popupId = "floatingframe";
    }

    var popupElem = getLayer(popupId);

    var windowH = windowHeight();
    var windowW = windowWidth();

    // center of screen
    var height = popupElem.style.height;
    if (height.indexOf("px") > 0) {
        height = height.substring(0, height.length - 2);
    }
    var width = popupElem.style.width;
    if (width.indexOf("px") > 0) {
        width = width.substring(0, width.length - 2);
    }

    var ttop = (windowH - height)/2;
    popupElem.style.top = ttop+"px";

    var lleft = (windowW - width)/2;
    popupElem.style.left = lleft+"px";
}

//------------------------------------------------------------------------------
function resizePopup(width, height, popupId) {
    if (popupId == null) {
        popupId = "floatingframe";
    }    
       
    if (width == null || height == null) {
        return;
    }
      
    var popupElem = getLayer(popupId);
    var windowH = windowHeight();
    var windowW = windowWidth();

    // enforce a 50px border
    height = Math.min(windowH-100, height);
    width = Math.min(windowW-100, width);

    popupElem.style.height = (height+10)+"px";
    popupElem.style.width = (width+10)+"px";

    // center of screen
    centerPopup(popupId);
}

//------------------------------------------------------------------------------
function resizeIframe() {
    var ff = parent.getLayer("floatingframe");
    ff.style.height = (document.body.offsetHeight + 40) + "px";
    parent.centerPopup();
}

//------------------------------------------------------------------------------
function setProperSizeForIframe(iframe) {
    var ff = parent.getLayer("floatingframe");
	var parentSize =  window.parent.windowHeight();
    var frame = document.getElementById(iframe);
    var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	if (frame.offsetHeight > windowHeight) {
		ff.style.height = (parentSize - 40) + "px";
		parent.centerPopup();
	}
}

//------------------------------------------------------------------------------
function hideLayers()
{
    document.getElementById('homeCopy').style.zIndex="-1";
	//document.getElementById('photoLogin').style.zIndex="-1";
	//document.getElementById('nav').style.zIndex="-1";
}
			
function showLayers()
{
	document.getElementById('homeCopy').style.zIndex="2";
	//document.getElementById('photoLogin').style.zIndex="2";
	//document.getElementById('nav').style.zIndex="2";
}
			