var win;

var lastDIV;
var menuleft = 0;
var menutop = 0;
var menuright = 0;
var menubottom = 0;

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

/*function hideAll() {
	if (lastMenu != null) {
		lastMenu.display = "none";
		lastMenu.left = 0;
	}
}*/



function removeAllButNumeric(num) {
	var temp = num.toString();
	var output = "";
	
	for (var i=0; i<temp.length; i++) {
		if (temp.charAt(i) == "." || temp.charAt(i) == 1 || temp.charAt(i) == 2 || temp.charAt(i) == "3" || temp.charAt(i) == "4" || temp.charAt(i) == "5" || temp.charAt(i) == "6" || temp.charAt(i) == "7" || temp.charAt(i) == "8" || temp.charAt(i) == "9" || temp.charAt(i) == "0") {
			output += temp.charAt(i);
		}
	}
	
	return output;
}

function updateMenus(e) {
	var x;
	var y
	
	if( browser.isIE ) {
		if (!e) var e = window.event;
	

		x = e.clientX;
		y = e.clientY;
	
		if (x < menuleft || x > menuright || y < menutop || y > menubottom) {
			hideAllDivs();
		}
	}
	//status = "x = " + x + ", y = " + y;
}

function showDiv(index, caller) {
	lastDIV = index;
	
	var temp
	var obj
	
	if( browser.isIE ) {
		if( document.all("divSubMenu").length > 1 ) {
			if( document.all("divSubMenu")[index] != null ) {
				obj = document.all("divSubMenu")[index];
			}
		} else {
			if( document.all("divSubMenu") != null ) {
				obj = document.all("divSubMenu");
			}
		}
		hideAllDivs();
		hideKBTypes();
	
		menuleft = obj.style.pixelLeft - 6;
		menutop = menutopstart - 22;
		menuright = menuleft + 240;
		menubottom = menutopstart + obj.style.pixelHeight + 16; //(caller.childrencount * menuheight);

		obj.style.visibility = "visible";
	} else {
	
		if( document.getElementsByName("divSubMenu") != null ) {
			var objs = document.getElementsByName("divSubMenu")
			
			temp = objs[index].style.visibility;
			obj = objs[index];
	
			hideAllDivs();
			
			menuleft = obj.style.pixelLeft - 6;
			menutop = menutopstart - 22;
			menuright = menuleft + 240;
			menubottom = menutopstart + obj.style.pixelHeight + 16; //(caller.childrencount * menuheight);

			obj.style.visibility = "visible";
		}
	}
	
}

function hideAllDivs() {
	
	if( browser.isIE ) {
		if (typeof(document.all["divSubMenu"]) == "object") {
			showKBTypes();
			lastDIV = -1;
			if( document.all["divSubMenu"].length > 1 ) {
				for (var i=0; i<document.all["divSubMenu"].length; i++) {
					document.all["divSubMenu"][i].style.visibility = "hidden";
				}
			} else {
				document.all("divSubMenu").style.visibility = "hidden";
			}
		}
	} else {
	
		//Only loop through 15 times to get rid of menus that aren't clicked.
		showKBTypes();

	
		var objs = document.getElementsByName("divSubMenu");

		for (var i=0; i < objs.length; i++)
		{
		
			objs[i].style.visibility = "hidden";
		}

	}
	
}

function setDate(itm, newDate) {
	document.getElementById(itm).value = newDate;
}

function chooseDate(itm) {
	if (itm.value != "") {
		win = window.open('/data/shared/calendar/calendar.asp?box=' + itm.id + '&selectedDate=' + itm.value, 'newWin', 'addressbar=true, width=220, height=220');
	} else {
		win = window.open('/data/shared/calendar/calendar.asp?box=' + itm.id, 'newWin', 'width=220, height=220');
	}
	win.focus();
}

function hideKBTypes() {
	if( browser.isIE ) {
		for (var i=0; i<document.all["kbType"].length; i++) {
			document.all["kbType"][i].style.visibility = "hidden";
		}
	}
}

function showKBTypes() {
	if( browser.isIE ) {
		for (var i=0; i<document.all["kbType"].length; i++) {
			document.all["kbType"][i].style.visibility = "visible";
		}
	}
}

function loadPage(path, delay) {
	resetPage();
	//setTimeout("document.location.href = '" + path + "';", 100);
	document.location.href = path;
}

function displayHourglass() {
	hideAllDivs();
	hideKBTypes();
	//document.getElementById("hourglass").style.display = 'block';
}

function resetPage() {
	hideAllDivs();
	hideKBTypes();
	displayHourglass();
	if (document.getElementById("currentpage")) {
		document.getElementById("currentpage").value = 1;
	}
}

function printDocument() {
	if (window.print) {
		window.print();
	} else {
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";
	}
}

// CONTEXT MENU
var menu = null;

function prepare() {
	menu = document.getElementById("divContextMenu");
}

function showMenu() {
	if( browser.isIE ) {
		var rightedge = document.body.clientWidth - event.clientX;
		var bottomedge = document.body.clientHeight - event.clientY;
	
		// if there isn't enough horizontal space, move the popup
		if (rightedge < menu.offsetWidth) {
			menu.style.left = document.body.scrollLeft + event.clientX-menu.offsetWidth;
		} else {
			menu.style.left = document.body.scrollLeft + event.clientX;
		}
		// if there isn't enough vertical space, move the popup
		if (bottomedge < menu.offsetHeight) {
			menu.style.top = document.body.scrollTop + event.clientY-menu.offsetHeight;
		} else {
			menu.style.top = document.body.scrollTop + event.clientY;
		}
	
		menu.style.display = "block";
	}
	return false;
}

function hideMenu() {
	//showKBTypes();
	menu.style.display = "none";
}

function confirmDeleteProspectType()
{
var agree=confirm("Are you sure you want to delete this prospect type?");
if (agree)
	return true ;
else
	return false ;
}

function deleteAlert(message)
{
	alert(message);
	return false;
}

function confirmDelete(message)
{
	var agree=confirm("Are you sure you want to delete " + message + "?");
	if (agree)
		return true ;
	else
		return false ;
}

function confirmAction(q) {
	var agree = confirm(q);
	if(agree)
		return true;
	else
		return false;
}

function salesPersonUpdateCheck(number)
{
	var temp = document.myForm[number].SalesPersonID.value;

	if((temp == "") || (temp == null))
	{
		alert("Invalid ID.");
		document.myForm[number].SalesPersonID.focus();
		return false;
	}
	
	var i;
	for(i = 0; i < temp.length; i++)
	{
		var digit = temp.charAt(i);
		if(digit < "0" || digit > "9")
		{
			alert("Invalid, ID is a number.");
			document.myForm[number].SalesPersonID.focus();
			return false;
		}
	}

	if((document.myForm[number].SalesPersonName.value == "") || (document.myForm[number].SalesPersonName.value == null))
	{
		alert("Invalid Name.");
		document.myForm[number].SalesPersonName.focus();
		return false;
	}
		
	return true;
}

/*
 * menuBug()
 *
 * Fixes Internet Explorer so that select menus disappear when menus are rolled-over
 *
 *
 */
function menuBug( val ) {
	if( browser.isIE ) {
		var objs = document.getElementsByTagName('Select');
		
		//alert( objs.length);
		
		for( var i = 0; i < objs.length; i++ ){
			// display = 'none' or ''
			//objs[i].style.display = val;
			// visibility = 'hidden' or 'visible'
			objs[i].style.visibility = val;
		}
	}
}

//document.oncontextmenu = showMenu;
//document.onclick = hideMenu;

//checkDate by default only allows 1 year into the past
//future_date is a flag to allow future dates 1 year into the future
//birth_date is a flag to allow dates back 100 yrs
function checkDate(form_date, form_name, future_date, birth_date)
{
  future_date = (future_date == null) ? 0 : future_date; //default to 0
  birth_date = (birth_date == null) ? 0 : birth_date; //default to 0
  var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  var date_arr = form_date.split("/");
	if(date_arr.length != 3)
	{
		alert("Invalid date in " + form_name);
		return false;
	}
  var month = date_arr[0];
  var day = date_arr[1];
  var year = date_arr[2];
  
  if(year.length == 2) // if used 2 yr format add 20 to the front
  	year = "20".concat(year);
  
  if(year%4 == 0) 
  	monthLength[1] = 29; //Update for leap year
  
	if(year.length != 4) 
	{
  	alert("Invalid year in " + form_name);
  	return false;
  }
		
  if(day > monthLength[month-1])
  {
  	alert("Invalid day in " + form_name);
  	return false;
  }
  
  if(month > 12 || month < 1)
  {
  	alert("Invalid month in " + form_name);
  	return false;
  }
  
  var today = new Date();
  
  if(future_date == 0)
  {
  	if(year > today.getFullYear())
  	{
  		alert(form_name + " cannot be greater than todays date.");
   		return false;
  	}
  	if(year == today.getFullYear() && month > today.getMonth() + 1)
  	{
  		alert(form_name + " cannot be greater than todays date.");
   		return false;
  	}
  	if(year == today.getFullYear() && month == today.getMonth() + 1 && day > today.getDate())
  	{
  		alert(form_name + " cannot be greater than todays date.");
   		return false;
  	}
  }
  else // only alow 1 year into the future
  {	
  	if(year > today.getFullYear() + 1)
  	{
  		alert(form_name + " cannot be more than a year from todays date.");
   		return false;
  	}
  	if(year > today.getFullYear() && month > today.getMonth() + 1)
  	{
  		alert(form_name + " cannot be more than a year from todays date.");
   		return false;
  	}
  	if(year > today.getFullYear() && month == today.getMonth() + 1 && day > today.getDate())
  	{
  		alert(form_name + " cannot be more than a year from todays date.");
   		return false;
  	}
  }
  
  if(birth_date == 0)// only alow 1 year into past
  {	
		if(future_date == 0)
  	{
    	if(year < today.getFullYear() - 1)
    	{
    		alert(form_name + " cannot be more than a year ago from todays date.");
     		return false;
    	}
    	if(year < today.getFullYear() && month < today.getMonth() + 1)
    	{
    		alert(form_name + " cannot be more than a year ago from todays date.");
     		return false;
    	}
    	if(year < today.getFullYear() && month == today.getMonth() + 1 && day < today.getDate())
    	{
    		alert(form_name + " cannot be more than a year ago from todays date.");
     		return false;
    	}
		}
		else
		{
    	if(year < today.getFullYear())
    	{
    		alert(form_name + " cannot be before todays date.");
     		return false;
    	}
    	if(year == today.getFullYear() && month < today.getMonth() + 1)
    	{
    		alert(form_name + " cannot be before todays date.");
     		return false;
    	}
    	if(year == today.getFullYear() && month == today.getMonth() + 1 && day < today.getDate())
    	{
    		alert(form_name + " cannot be before todays date.");
     		return false;
    	}
		}
  }
  else // alow 100 years back
  {	
  	if(year < today.getFullYear() - 101)
  	{
  		alert(form_name + " cannot be more than a 100 years ago from todays date.");
   		return false;
  	}
  	if(year < today.getFullYear() - 100 && month < today.getMonth() + 1)
  	{
  		alert(form_name + " cannot be more than a 100 years ago from todays date.");
   		return false;
  	}
  	if(year < today.getFullYear() - 100 && month == today.getMonth() + 1 && day < today.getDate())
  	{
  		alert(form_name + " cannot be more than a 100 years ago from todays date.");
   		return false;
  	}
  }
  	//now = now.getTime(); //NN3
  return true;
}
  
