
function availableHeight() {
	return f_filterResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

var view = "normal";
var shell = null;
var main = null;
var body_overflow = null;
function togglePrintView() {
	if(view == "normal") {
		var top = document.getElementById('top');
		main = document.getElementById('page-body3');
		shell = document.getElementById('shell');
		top.replaceChild(main, shell);
		main.style.width = "100%";
		body_overflow = document.body.style.overflow;
		document.body.style.overflow = "visible";
		view = "print";

	} else if(view == "print") {
		var top = document.getElementById('top');
		top.replaceChild(shell, main);
		var bot = document.getElementById('page-body2');
		bot.appendChild(main);
		document.body.style.overflow = body_overflow;
		view = "normal";
		correctHeight();

	}
}

function correctHeight() {
	var height = availableHeight() - 128;
	var width = availableWidth() - 166;
	if(view == "normal") {
		document.getElementById('page-table').style.height = height;
		document.getElementById('page-row').height = height;
		document.getElementById('page-body').style.height = height;
		document.getElementById('page-sidebar').height = height;
		document.getElementById('page-body2').style.height = height;
		document.getElementById('page-body2').style.width = width;
	}
}
function toggleDisplay(nodes) {
	for(var i = 0; i < nodes.length; i++) {
		var element = nodes[i];
		if(element.className == "collapsee") {
			if(element.style.display == "none")
				element.style.display = "block";
			else
				element.style.display = "none";
		}
	}
	return false;
}

function toggleClassDisplay(clazz) {
	var rulesPropName = "cssRules";
  var pattern = '.' + clazz;
	var matcher = new RegExp(pattern);
	for(var sheet_i = 0; sheet_i < document.styleSheets.length; sheet_i++) {
		var rules = document.styleSheets[sheet_i][rulesPropName];
		if(!rules) {
			rulesPropName = "rules";
			rules = document.styleSheets[sheet_i][rulesPropName];
		}
		if(rules) {
			for(var rule_i = 0; rule_i < rules.length; rule_i++) {
				var rule = rules[rule_i];
				if(matcher.test(rule.selectorText)) {
					var display = rule.style.display;
					if(display == "none") 
						rule.style.display = "block";
					else
						rule.style.display = "none";
				}
			}
		}
	}
	return false;
}

var formAction = null;

function addCoreIdQueryString(event) {
	var element = event.target || event.srcElement;
	var path = element.value;
	var coreId = coreIdFromPath(path);
	var form = element;
	while(form && form.nodeName.toUpperCase() != "FORM") {
		form = form.parentNode;
	}
	if(!formAction) {
		formAction = form.action;
	}
	form.action = formAction + "?coreId=" + coreId;	
	window.status = form.action;
	return true;
}

function coreIdFromPath(path) {
	return path.replace( /.*[\/\\]/, "" ).replace( /\.(xml|zip|ZIP|XML)$/, "");
}
function slideMenuMouseOver(menuId) {
  var div = document.getElementById(menuId);
  if(div.id == "bottom"){
  setTransition(div, "33px", 'height 1ms ease-in'); 
  } else{
    div.style.display = "block";
  }
 }
 
 function slideMenuMouseOut(menuId) {
  var div = document.getElementById(menuId);
  if(div.id == "bottom"){
    setTransition(div, "4px", 'height 1s ease-in');  
  } else{
    div.style.display = "none";
  }
 }
 
 function showBottomMenu(menuId) {
  var div = document.getElementById(menuId);
  setTransition(div, "33px", 'height 1ms ease-in');
  setTimeout("slideMenuMouseOut('bottom')",5000);
 }

 function setTransition(div, height, transition) {
    div.style.height = height;
    div.style.webkitTransition = transition;
	div.style.MozTransition = transition;
 }
  function showContentImage() {
	if(document.getElementById('toc-div') && document.getElementById('toc-div').innerHTML!="<a href=\"#\" title=\"\"></a>" ) {
	   document.getElementById('menu2div').style.display = "block";
	}
 }

 function hideMenu(menuId){
  var div = document.getElementById(menuId);
  div.style.display = "none";
 }

 function submitform(action) {
	 document.forms['theForm'].action = action;
	 document.forms['theForm'].submit();
}

function adjustMenuHeight() {
if (document.getElementById('side2') != null) {
	var n = document.getElementById('side2').getElementsByTagName('div');
	if(n.length > 20) {
		 document.getElementById('side2').style.height = "90%";
		 document.getElementById('side2').style.overflow = "auto";
	 }
	}
} 

function validate(id, type) {
    if(type == "date") {
	var textbox = document.getElementById(id);
	textbox.onblur = function() {
	var value = this.value;
	var error = false; 	
	var span = document.getElementById(id + "_span");
	if(value) {
    var regex = '^(((((19|[2-9][0-9])[0-9][0-9])(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[13456789]|1[012])(0[1-9]|[12][0-9]|30))|(02(0[1-9]|1[0-9]|2[0-8]))))|(((1[6-9]|[2-9][0-9])(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))0229))((([01][0-9]|2[0-3]([0-5][0-9]([0-5][0-9])?)?)|(24(00(00)?)?))(\.[0-9][0-9][0-9])?)?)$';
	if(!value.match(regex)) {
	  span.innerHTML = "<img id='error_image' title='Date/Time is invalid. Valid examples are 19701027 or 19701027235454' src='/prpllr/icon_error.gif'/>"; 
	  error = true;
	}
	}
 	if(!error) {
	 span.innerHTML = "";
	} 
  }
 }
}

function validateAndSubmit(prevNext) {
   	if(document.getElementById('error_image') && prevNext.id.indexOf("validate") !=-1) {
      alert("Please correct the form errors first.");
    } else {
	  document.getElementById("startrowno").value = document.getElementById(prevNext.id).value;
	  document.forms[1].submit();
    }
}

function onEnterKeyPress(e,id) {
    if (document.getElementById(id)) {
		if (e.keyCode == 13) {
		   document.getElementById(id).click();
		}
	}
}


