// global_javascripts.js

/* Opens centered Help window given the URL */
function openHelpWindow(url) {
	var helpWindow;
	var width = 660;
	var height = 500;
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top = parseInt((screen.availHeight/2) - (height/2));

	var windowFeatures = "width="+width+",height="+height+",resizable,left="+left+",top="+top+",screenX="+left+",screenY="+top+",scrollbars=yes";
	//alert(windowFeatures);
	if(!helpWindow || helpWindow.closed)
	{
		helpWindow = window.open(url,"Help", windowFeatures);
	} else {
		helpWindow.focus();
	}
}

//bksp, tab, del, end, home, left, right, f5
var navEditKeys = [8, 9, 46, 35, 36, 37, 39, 116];

function allowOnlyNumeric(evt) {
	evt = (evt) ? evt : window.event;
	var keyCode = (evt.which) ? evt.which : evt.keyCode;
	if (arguments.length > 1) {
		for (var i = 1; i < arguments.length; i++) {
			if (arguments[i] == keyCode || (typeof arguments[i] == "string" && arguments[i].charCodeAt(0) == keyCode)) {
				return true;
			}
		}
	}
	if (keyCode >= 48 && keyCode <= 57) {
		return true;
	}
	if (!("charCode" in evt) || evt.charCode > 0) {
		return false;
	}
	for (var i = 0; i < navEditKeys.length; i++) {
		if (navEditKeys[i] == keyCode) {
			return true;
		}
	}
	return false;
}

/* Functions to alternately show/hide rows based on ID */
function showRows(rowID)
{
	showRows(rowID,'block');
}

function showRows(rowID, displayStyle) {
     obj = document.getElementsByTagName('TR');
      for (i=0; i<obj.length; i++)
     {
          if (obj[i].id == rowID)
          obj[i].style.display = displayStyle;
     }
}	

function hideRows(rowID)
{
     obj = document.getElementsByTagName('TR');
      for (i=0; i<obj.length; i++)
     {
          if (obj[i].id == rowID)
          obj[i].style.display = 'none';
     }
}

	function show( id ) {
		document.getElementById(id).style.display = '';
	}

	function hide( id ) {
		document.getElementById(id).style.display = 'none';
	}


var pbLastPosY = 0;
var pbBarWidth = 0;
function movePBDiv() {
	var y = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
	if (y!=pbLastPosY) {
		document.getElementById('sbmPBDiv').style.top=y+'px';
		pbLastPosY=y;
	}
	if (TimeScroll)	window.setTimeout('movePBDiv()',100);
}
var TimeScroll = true;
function showPBDiv() {
	pbBarWidth = 1;
	var pbDiv = document.getElementById('sbmPBDiv');
	if (pbDiv) {
		window.onscroll=function(){window.onscroll=movePBDiv;TimeScroll=false;};
		updatePBDivWidth();
		window.setInterval("updatePBDivWidth()", 40);
		movePBDiv();
		pbDiv.style.visibility='visible';
	}
}
function hidePBDiv() {
	var pbDiv = document.getElementById('sbmPBDiv');
	if (pbDiv) {
		pbDiv.style.visibility='hidden';
	}
}

function updatePBDivWidth() {
	document.getElementById('sbmPBBarImg').style.width=pbBarWidth+'px';
	pbBarWidth += 3;
	if (pbBarWidth > 120) {
		pbBarWidth = 3;
	}
}
function fieldAutoTab(thisFieldObj, event, nextField) {
	var charCode = (event.which) ? event.which : event.keyCode;
	if (charCode != 9 && charCode != 16 ) { //9=tab 16=shift
		var thisFieldObjBase = thisFieldObj.name.substring(0,thisFieldObj.name.lastIndexOf('.')+1);
		var nextFieldObj = thisFieldObj.form.elements[thisFieldObjBase + nextField];
		if (thisFieldObj.value.length == thisFieldObj.maxLength) {
			nextFieldObj.focus();
		}
	}
}

function setQuoteCookie(quoteNumber) {
	var now = new Date();
	var expires = new Date(now.getTime()+(4*7*24*60*60*1000)); //4 wks
	Set_Cookie("quote",quoteNumber,expires);
}

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}  

function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return "";
    if (start == -1) return "";
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

var previouspage = "";
function monitor(thispage) {
  	if (thispage == previouspage) {
      return false;
    }
    previouspage = thispage;
    showPBDiv();
    return true;
}

function resetMonitor() {
	previouspage = "";
	hidePBDiv();
	TimeScroll = false;
}

function navigate(destination) {
alert( "here" );
	dest = $("navDestination");
	alert( "dest: " + dest );
	if (dest) {
		dest.value = destination;
		dest.form.action.value = "appNavigation";
		dest.form.submit();
	} else {
		window.location = destination + ".gen";
	}
}