/* Call this script like this:
feisKeeper File
<script src="/includes/javaScript/main.js"></script>

*/


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

function userLogin()  {
  document.form1.loginAction.value = "userLogin";
  document.form1.submit();
}

function userLogout()  {
  document.form1.loginAction.value = "userLogout";
  document.form1.submit();
}


// Function to open a new window
function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
	win = window.open(mypage,myname,settings)
}


// Function to make text blink.  
// In order for the text to blink, you have to tage it like this <blink>text that blinks</blink>
// Then set the <body> tag for the page to this:  <body onload="setInterval('blinkIt()',500)">
function blinkIt()  {
  if(!document.all)  {
    return;
  }  else  {
    for(i = 0; i < document.all.tags('blink').length; i++)  {
      s = document.all.tags('blink')[i];
      s.style.visibility = (s.style.visibility == 'visible') ? 'hidden' : 'visible';
    }
  }
}


// Function to reload a page with new parameters, called from jump-type drop down boxes
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


// This function takes a drop down called "dropDownName" and loops through its options looking for "selectedValue".  When it finds it, it sets the selectedIndex
function setDropDownSelected(dropDownName, selectedValue)  {
  //alert('setting ' + rightName + ' to ' + newValue);
  var y = document.getElementById(dropDownName);
  var i;
  for (i = 0; i < y.options.length; i++) {
  	//alert('checking option: ' + y.options[i].value);
    if(y.options[i].value == selectedValue) {
      y.selectedIndex = i;
    }
  }
}


// Function to trap the enter key and submit a form
function submitOnEnter(e, formName)  {
  var intKey = (window.Event) ? e.which : e.keyCode;
  if(intKey == 13)  { //enter key
    userLogin();
  }
  return;
}


// Function to show Divs when its tab is clicked on
function showDiv(e)  {
  var x=document.getElementById(e);
  if (x)x.className='topPartOfPageNoBorderDiv';
  var x=document.getElementById(e + 'TabSelected');
  if (x)x.className='tabSelected';
  var x=document.getElementById(e + 'TabNotSelected');
  if (x)x.className='hide';
}


// Function to hide Divs when another Div's tab is clicked on
function hideDiv(e)  {
  var x=document.getElementById(e);
  if (x)x.className="hide";
  var x=document.getElementById(e + 'TabSelected');
  if (x)x.className='hide';
  var x=document.getElementById(e + 'TabNotSelected');
  if (x)x.className='tabNotSelected';
}


// Function to show page when its tab is clicked on
function showPage(e)  {
  var x=document.getElementById('page' + e);
  if (x)x.className='insetSection';
  var x=document.getElementById(e + 'PageSelected');
  if (x)x.className='pageSelected';
  var x=document.getElementById(e + 'PageNotSelected');
  if (x)x.className='hide';
}


// Function to hide page when another page's tab is clicked on
function hidePage(e)  {
  var x=document.getElementById('page' + e);
  if (x)x.className="hide";
  var x=document.getElementById(e + 'PageSelected');
  if (x)x.className='hide';
  var x=document.getElementById(e + 'PageNotSelected');
  if (x)x.className='pageNotSelected';
}

// This function adds a drop shadow behind a div
function divShadow(elementID)  {

  // Get ready to create shadows, we need dimensions on the div
  var divHeight  = document.getElementById(elementID).offsetHeight;
  var divWidth   = document.getElementById(elementID).offsetWidth;
  var divBorder  = document.getElementById(elementID).style.border.substr(0, 1) ? document.getElementById(elementID).style.border.substr(0, 1) : 0;
  var divZIndex  = document.getElementById(elementID).style.zIndex;

  // Create the shadowBottomLeftDiv
  var shadowTop  = (divHeight * 1) - 12 - (divBorder * 1);
  var shadowLeft = (-19);
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id', 'shadowBottomLeftDiv');
  newdiv.style.position = "absolute";
  newdiv.style.width = "30px";
  newdiv.style.height = "30px";
  newdiv.style.left = shadowLeft + "px";
  newdiv.style.top = shadowTop + "px";
  newdiv.style.backgroundImage = 'url(/img/shadow/bottomLeft.png)';
  newdiv.style.layerBackgroundImage = 'url(/img/shadow/bottomLeft.png)';
  newdiv.style.zIndex = divZIndex;
  newdiv.style.border = "0px";
  document.getElementById(elementID).appendChild(newdiv);

  // Create the shadowTopLeftDiv
  var shadowTop  = (-18);
  var shadowLeft = (-18);
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id', 'shadowTopLeftDiv');
  newdiv.style.position = "absolute";
  newdiv.style.width = "30px";
  newdiv.style.height = "30px";
  newdiv.style.left = shadowLeft + "px";
  newdiv.style.top = shadowTop + "px";
  newdiv.style.backgroundImage = 'url(/img/shadow/topLeft.png)';
  newdiv.style.layerBackgroundImage = 'url(/img/shadow/topLeft.png)';
  newdiv.style.zIndex = divZIndex;
  newdiv.style.border = "0px";
  document.getElementById(elementID).appendChild(newdiv);

  // Create the shadowTopRightDiv
  var shadowTop  = (-18);
  var shadowLeft = (divWidth * 1) - 12 - (divBorder * 1);
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id', 'shadowTopRightDiv');
  newdiv.style.position = "absolute";
  newdiv.style.width = "30px";
  newdiv.style.height = "30px";
  newdiv.style.left = shadowLeft + "px";
  newdiv.style.top = shadowTop + "px";
  newdiv.style.backgroundImage = 'url(/img/shadow/topRight.png)';
  newdiv.style.layerBackgroundImage = 'url(/img/shadow/topRight.png)';
  newdiv.style.zIndex = divZIndex;
  newdiv.style.border = "0px";
  document.getElementById(elementID).appendChild(newdiv);

  // Create the shadowBottomRightDiv
  var shadowTop  = (divHeight * 1) - 12 - (divBorder * 1);
  var shadowLeft = (divWidth * 1) - 12 - (divBorder * 1);
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id', 'shadowBottomRightDiv');
  newdiv.style.position = "absolute";
  newdiv.style.width = "30px";
  newdiv.style.height = "30px";
  newdiv.style.left = shadowLeft + "px";
  newdiv.style.top = shadowTop + "px";
  newdiv.style.backgroundImage = 'url(/img/shadow/bottomRight.png)';
  newdiv.style.layerBackgroundImage = 'url(/img/shadow/bottomRight.png)';
  newdiv.style.zIndex = divZIndex;
  newdiv.style.border = "0px";
  document.getElementById(elementID).appendChild(newdiv);

  // Create the shadowTopDiv
  var shadowTop  = (-18);
  var shadowLeft = (12);
  var shadowWidth = (divWidth * 1) - 24 - (divBorder * 1);
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id', 'shadowTopDiv');
  newdiv.style.position = "absolute";
  newdiv.style.width = shadowWidth + "px";
  newdiv.style.height = "19px";
  newdiv.style.left = shadowLeft + "px";
  newdiv.style.top = shadowTop + "px";
  newdiv.style.backgroundImage = 'url(/img/shadow/top.png)';
  newdiv.style.layerBackgroundImage = 'url(/img/shadow/top.png)';
  newdiv.style.zIndex = divZIndex;
  newdiv.style.border = "0px";
  document.getElementById(elementID).appendChild(newdiv);

  // Create the shadowLeftDiv
  var shadowTop  = (12);
  var shadowLeft = (-18);
  var shadowHeight = (divHeight * 1) - 24 - (divBorder * 1);
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id', 'shadowLeftDiv');
  newdiv.style.position = "absolute";
  newdiv.style.width = "19px";
  newdiv.style.height = shadowHeight + "px";
  newdiv.style.left = shadowLeft + "px";
  newdiv.style.top = shadowTop + "px";
  newdiv.style.backgroundImage = 'url(/img/shadow/left.png)';
  newdiv.style.layerBackgroundImage = 'url(/img/shadow/left.png)';
  newdiv.style.zIndex = divZIndex;
  newdiv.style.border = "0px";
  document.getElementById(elementID).appendChild(newdiv);

  // Create the shadowBottomDiv
  var shadowTop  = (divHeight * 1) - 1 - (divBorder * 1);
  var shadowLeft = (11);
  var shadowWidth = (divWidth * 1) - 23 - (divBorder * 1);
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id', 'shadowBottomDiv');
  newdiv.style.position = "absolute";
  newdiv.style.width = shadowWidth + "px";
  newdiv.style.height = "19px";
  newdiv.style.left = shadowLeft + "px";
  newdiv.style.top = shadowTop + "px";
  newdiv.style.backgroundImage = 'url(/img/shadow/bottom.png)';
  newdiv.style.layerBackgroundImage = 'url(/img/shadow/bottom.png)';
  newdiv.style.zIndex = divZIndex;
  newdiv.style.border = "0px";
  document.getElementById(elementID).appendChild(newdiv);

  // Create the shadowRightDiv
  var shadowTop  = (12);
  var shadowLeft = (divWidth * 1) - 1 - (divBorder * 1);
  var shadowHeight = (divHeight * 1) - 24 - (divBorder * 1);
  var newdiv = document.createElement('div');
  newdiv.setAttribute('id', 'shadowRightDiv');
  newdiv.style.position = "absolute";
  newdiv.style.width = "19px";
  newdiv.style.height = shadowHeight + "px";
  newdiv.style.left = shadowLeft + "px";
  newdiv.style.top = shadowTop + "px";
  newdiv.style.backgroundImage = 'url(/img/shadow/right.png)';
  newdiv.style.layerBackgroundImage = 'url(/img/shadow/right.png)';
  newdiv.style.zIndex = divZIndex;
  newdiv.style.border = "0px";
  document.getElementById(elementID).appendChild(newdiv);
  
}



// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
// Credit: http://www.somacon.com/p143.php
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
// Credit: http://www.somacon.com/p143.php
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


// ********************
// ALL OF THIS COMES FROM:
// http://www.brainjar.com/dhtml/tablesort/default.asp


// This code is necessary for browsers that don't reflect the DOM
// constants (like IE).
if (document.ELEMENT_NODE == null) {
  document.ELEMENT_NODE = 1;
  document.TEXT_NODE = 3;
}

function getTextValue(el) {

  var i;
  var s;

  // Find and concatenate the values of all text nodes contained
  // within the element.
  s = "";
  for (i = 0; i < el.childNodes.length; i++)
    if (el.childNodes[i].nodeType == document.TEXT_NODE)
      s += el.childNodes[i].nodeValue;
    else if (el.childNodes[i].nodeType == document.ELEMENT_NODE &&
             el.childNodes[i].tagName == "BR")
      s += " ";
    else
      // Use recursion to get text within sub-elements.
      s += getTextValue(el.childNodes[i]);

  return normalizeString(s);
}



// This function simply replaces multiple white space characters with a single space and removes any leading or trailing white space.
// Regular expressions for normalizing white space.
var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
var whtSpMult = new RegExp("\\s\\s+", "g");
function normalizeString(s) {

  s = s.replace(whtSpMult, " ");  // Collapse any multiple whites space.
  s = s.replace(whtSpEnds, "");   // Remove leading or trailing white
                                  // space.
  return s;
}

// This function compare two string values based on whether or not they can be interpreted as numeric values.
function compareValues(v1, v2) {

  var f1, f2;

  // If the values are numeric, convert them to floats.

  f1 = parseFloat(v1);
  f2 = parseFloat(v2);
  if (!isNaN(f1) && !isNaN(f2)) {
    v1 = f1;
    v2 = f2;
  }

  // Compare the two values.
  if (v1 == v2)
    return 0;
  if (v1 > v2)
    return 1
  return -1;
}

function sortTable(dataId, col, col2)  {

  // Get the table section to sort.
  var tblEl = document.getElementById(dataId);

  // Set up an array of reverse sort flags, if not done already.
  if (tblEl.reverseSort == null)  {
    tblEl.reverseSort = new Array();
	tblEl.reverseSort[4] = !tblEl.reverseSort[4];
  }

  // If this column was the last one sorted, reverse its sort direction.
  if (col == tblEl.lastColumn)
    tblEl.reverseSort[col] = !tblEl.reverseSort[col];

  // Remember this column as the last one sorted.
  tblEl.lastColumn = col;

  // Set the table display style to "none" - necessary for Netscape 6 
  // browsers.
  var oldDsply = tblEl.style.display;
  tblEl.style.display = "none";

  // Sort the rows based on the content of the specified column
  // using a selection sort.

  var tmpEl;
  var i, j;
  var minVal, minIdx;
  var testVal;
  var cmp;

  // First remove all heading rows
  for (i = 0; i < tblEl.rows.length - 1; i++) {
	while(tblEl.rows[i].getAttribute("name") == "headingRow")  {
	  //alert(tblEl.rows[i].getAttribute("name"));
	  tblEl.removeChild(tblEl.rows[i]);
	  //alert(tblEl.rows[i].getAttribute("name"));
	}
  }
  
  for (i = 0; i < tblEl.rows.length - 1; i++) {
	
    // Assume the current row has the minimum value.
    minIdx = i;
    minVal = getTextValue(tblEl.rows[i].cells[col]);

    // Search the rows that follow the current one for a smaller value.
    for (j = i + 1; j < tblEl.rows.length; j++) {
      testVal = getTextValue(tblEl.rows[j].cells[col]);
      cmp = compareValues(minVal, testVal);
      // Need to sort on secondary column?
      if (cmp == 0)
        compareValues(getTextValue(tblEl.rows[minIdx].cells[col2]),
                      getTextValue(tblEl.rows[j].cells[col2]));
      // Reverse order?
      if (tblEl.reverseSort[col])
        cmp = -cmp;
      // If this row has a smaller value than the current minimum,
      // remember its position and update the current minimum value.
      if (cmp > 0) {
        minIdx = j;
        minVal = testVal;
      }
    }

    // By now, we have the row with the smallest value. Remove it from
    // the table and insert it before the current row.
    if (minIdx > i) {
      tmpEl = tblEl.removeChild(tblEl.rows[minIdx]);
      tblEl.insertBefore(tmpEl, tblEl.rows[i]);
    }
  }

  makePretty(tblEl);
  
  // If we're sorting by the resident occupancy percentage column, and we're sorting highest to lowest, add heading rows for the following categories:
  // 90%+, 85%-89%, 80%-84%, 79%-
  // NOTE, this should move to its own function
   if(col == 4 && dataId == 'censusTableData' && tblEl.reverseSort[col])  {
    //alert('add headings!');
	
	// Set some variables
	var heading = new Array();
	heading[0] = 'Resident Occupancy 90 % +';
	heading[1] = 'Resident Occupancy 85 % - 89 %';
	heading[2] = 'Resident Occupancy 80 % - 84 %';
	heading[3] = 'Resident Occupancy 79 %-';
	heading[4] = 'Lease Up';
	
	var headingValue = new Array();
	headingValue[0] = '4000%';	// No one will EVER have a 4000% census, but we need the heading at the very top row
	headingValue[1] = '90%';
	headingValue[2] = '85%';
	headingValue[3] = '80%';
	headingValue[4] = '50%';
	
	var currentHeading = 0;
	
	// Loop through all rows
	for (i = 0; i < tblEl.rows.length - 1; i++) {
      // Check this row's value
	  if((compareValues(headingValue[currentHeading], getTextValue(tblEl.rows[i].cells[4]))) == 1)  {
	    // Insert the heading row
	    //alert('heading at row ' + i );
		headingRow = document.createElement("tr");
		headingRow.setAttribute("className", "detail2");	// For IE
		headingRow.setAttribute("class", "detail2");		// For everything else
		headingRow.setAttribute("name", "headingRow");
		headingRow.setAttribute("height", "18px");
		headingRow.setAttribute("bgColor", "#2B2567");
		headingCell = document.createElement("td");
		headingCell.setAttribute("colSpan", "14");
		headingCell.setAttribute("style", "color:#FFFFFF");	// For FF
		headingCell.style.cssText = "color:#FFFFFF";		// For IE
		
		headingStrong = document.createElement("strong");
		headingText = document.createTextNode(" " + heading[currentHeading]);
		headingRow.appendChild(headingCell);
		headingCell.appendChild(headingStrong);
		headingStrong.appendChild(headingText);
		tblEl.insertBefore(headingRow, tblEl.rows[i]);
		
		// Increment currentHeading
		currentHeading++;
	  }
	}
  } 
  
  // Restore the table's display style.
  tblEl.style.display = oldDsply;

  return false;
}

// Style class names.
var rowClsNm = "alternateRow";
var colClsNm = "sortedColumn";

// Regular expressions for setting class names.
var rowTest = new RegExp(rowClsNm, "gi");
var colTest = new RegExp(colClsNm, "gi");

function makePretty(tblEl) {

  var i, j;
  var rowEl, cellEl;
  
  var lightBg = '#FFFFFF';
  var darkBg = '#DDDDDD';

  // Set style classes on each row to alternate their appearance.
  for (i = 0; i < tblEl.rows.length; i++) {
   rowEl = tblEl.rows[i];
   //rowEl.className = rowEl.className.replace(rowTest, "");
    if (i % 2 != 0)  {
	  rowEl.bgColor = darkBg;
      //rowEl.className += " " + rowClsNm;
	} else  {
	  rowEl.bgColor = lightBg;
	}
    //rowEl.className = normalizeString(rowEl.className);
    // Set style classes on each column (other than the name column) to
    // highlight the one that was sorted.
/*
    for (j = 2; j < tblEl.rows[i].cells.length; j++) {
      cellEl = rowEl.cells[j];
      cellEl.className = cellEl.className.replace(colTest, "");
      if (j == col)
        cellEl.className += " " + colClsNm;
      cellEl.className = normalizeString(cellEl.className);
    }
*/
  }
  return;
}

// ********************
MM_reloadPage(true);