var dialog = null;
var isDialogLoaded = false;
var isOpeningWindowUnloaded = false;

var browserName = navigator.appName.substring(0, 8);
var browserVersion = parseInt(navigator.appVersion);
var isIEBrowser = (browserName == "Microsof");
var isNSBrowser = (browserName == "Netscape");
var isIE4Browser = (isIEBrowser && (browserVersion >= 4));
var isNS6Browser = (isNSBrowser && (browserVersion >= 5));

var height = 600
var width = 700

function focusDialog() {
   if (dialog != null) {
      dialog.focus();
   }
}

function dialogOnBlur() {
   if (isDialogLoaded) {
      window.focus();
   }
}

function dialogOnLoad() {
   isDialogLoaded = true;
   
   if (isNSBrowser) {    
      window.onblur = dialogOnBlur();
      if (isDialogLoaded && isOpeningWindowOpen()) {
         getOpeningWindow().onunload = openingWindowOnUnload;
      }
   }
}

function dialogOnUnload() { 
   if (isDialogLoaded && isOpeningWindowOpen()) {
      getOpeningWindow().dialog = null;
   }
  
   isDialogLoaded = false;
   window.close();
}

function getOpeningWindow() {
   if (isIE4Browser) {
      return window.dialogArguments;
   } else {
      return window.opener;
   }
}

function isOpeningWindowOpen() {
   var openingWindow = getOpeningWindow();
   return (openingWindow != null) && !openingWindow.closed && !isOpeningWindowUnloaded;
}

function openingWindowOnUnload() {
   isOpeningWindowUnloaded = true;
   dialogOnUnload();
}

function openIEModalDialog(url, width, height, scroll) {
	var features = "dialogHeight:" + (height+30) + "px;dialogWidth:" + width +
		"px;center:yes;help=no;resizable=yes;status=no";
	features += (";scroll=" + (scroll ? "yes" : "no") + ";");
  	return window.showModalDialog(url, window, features);
}
 
function openDefaultModalDialog(url, name, width, height, scroll) {
   if ((dialog != null) && dialog.closed) {
      dialog = null;
   }

   if (dialog == null) {
		var features = "dependent=yes,location=no,menubar=no,resizable=yes,status=no," +
        "titlebar=no,toolbar=no,height=" + height + ",width=" + width;
		features += (",scrollbars=" + (scroll ? "yes" : "no"));
		features += ", top=0, left=0";
      dialog = window.open(url, name, features);  
   
      if (isNSBrowser) {
 //        window.onfocus = focusDialog; 
      } 
   }
	return dialog; 
}

function openModalDialog(url, name, ieWidth, ieHeight, nsWidth, nsHeight, scroll) {
	// using two sets of dimensions allows us to better tune for specific browsers
   if (isIE4Browser) {
      return openIEModalDialog(url, ieWidth, ieHeight, scroll);
   } else {
      return openDefaultModalDialog(url, name, nsWidth, nsHeight, scroll);
   }
}


// dialog-specific:
function openPrevEmpPopup() { return openModalDialog('/static/prevEmpPopUp.asp', 'prevEmppopup', 505, 460, 505, 500); }
function openOtherIncPopup() { return openModalDialog('/static/otherIncPopUp.asp', 'otherinc', 440, 200, 300, 225); }
function openNysCountyPopup() { return openModalDialog('/static/nysCountyPopUp.asp', 'nysCounty', 270, 120, 270, 120); }
function openTaxInsurPopup() { return openModalDialog('/static/TaxInsurPopUp.asp', 'taxes', 410, 185, 410, 185); }
function openSecMortgagePopup() { return openModalDialog('/static/secMortgagePopUp.asp', 'secMort', 400, 240, 400, 240); }
function openPropPopup() { return openModalDialog('/static/propPopUp.asp', 'prop', 495, 500, 495, 500, true); }
function openLoanLengthPopup() { return openModalDialog("/pallet/example.html", "fixedRate", 500, 500, 500, 500); }
function openInitialDrawPopup() { return openModalDialog("/static/initialDrawPopUp.asp", "fixedRate", 380, 120, 380, 120); }
function openAlimonyPopup() { return openModalDialog('/static/alimonyPopUp.asp', 'alimonyPopUp', 300, 175, 300, 180); }
function openMonthlyFeesPopup() { return openModalDialog('/static/monthlyFeesPopUp.asp', 'monthlyFeesPopUp', 380, 120, 380, 120); }
function openConsolidatePopup() { return openModalDialog('/static/consolidatePopUp.asp', 'consolidatepopup', 564, 160, 590, 160); }
function openEqualOpPopup() { return openModalDialog('/static/equalOpPopUp.asp', 'equalOpPopup', 350, 200, 350, 200); }
function openTravelerMilesPopup(option) { return openModalDialog('/static/travelerMilesPopUp.asp?option='+option, 'travelerMilesPopup', 700, 400, 700, 400, true); }
function openTravelerMiles2Popup() { return openModalDialog('/static/travelerMiles2PopUp.asp', 'travelerMilesPopup2', 500, 320, 500, 320); }

function openAdvLoginPopup(url, decode)
{ 
	var logurl;
	logurl = '/advisor/login/advlogin.asp?url=' + url + '&decode=' + decode;
	return openDefaultModalDialog(logurl, 'advlogin', 700, 600, true); 
<!--	return openModalDialog(logurl, 'Advlogin', width, height,500, 320); -->
}

function openRegistrationPopup()
{
	var start;
	start = '/subscription/serverside/start.asp';
	window.open(start,"","toolbar=1,location=0,directories=0,status=0,scrollbars=yes,copyhistory=0,width=800,height=500,top=100,left=100");
}
function openAdvisorPopup(url)
{
	return openDefaultModalDialog(url, 'popup', 700, 600, true);
}

function popUpTool(URL)
{
	var vHeight = 495;
	var vWidth = 315;	
	
	var strRam;
	strRam = Math.random();
	var url = URL + "?tm=" + strRam;



	if (isNS6Browser)
		openNS6Dialog(url, 'workstation', vWidth, vHeight, true);
	else
		openDefaultModalDialog(url, 'workstation', vWidth, vHeight, true);
	
}

function openNS6Dialog(url, name, width, height, scroll) {
  	var features = "dependent=yes,location=no,menubar=no,resizable=yes,status=no," +
        "titlebar=no,toolbar=no,height=" + height + ",width=" + width;
		features += (",scrollbars=" + (scroll ? "yes" : "no"));
		features += ", top=0, left=0";
      	return window.open(url, name, features);  
}
