var startTop = 0;
var startLeft = 0;
var expDays = 30;
var expDate = new Date();
expDate.setTime(expDate.getTime() + (expDays*24*60*60*1000));

//var baseSite = 'http://www.visual-paradigm.com/';
//var baseSite = 'http://testcom.visual-paradigm.org/';
//var baseSite = 'http://172.19.33.249/vp/';
//var baseSite = 'http://'+window.location.host+'/vp/';
var baseSite = 'http://'+window.location.host+'/';

var destination;
var popwin;

function notify() {
	alert("notify");

}

function popDownload(htmlFile, resourceType, resourceDetail, isHidden) {
	preferredSite = getPreferredSite(resourceType, resourceDetail);
	if (preferredSite == null) {
		detectFeasibleSite(htmlFile, resourceType, resourceDetail, 0, 0, false, false, false, false, true, isHidden);
	} else {
		redirectDownload(getHTMLPath(preferredSite, htmlFile, resourceType));
	}
}

function openPopupS(htmlFile,htmlWidth,htmlHeight, showScroll, centerWin, resizeable, showWin) {
	executePopup(htmlFile,htmlWidth,htmlHeight, showScroll, centerWin, resizeable, showWin);
}

function openPopupE(htmlFile, resourceType, resourceDetail, htmlWidth, htmlHeight, showScroll, centerWin, resizeable, showWin) {
	window.id=11;
	window.name="abc";
	destination = htmlFile;
	
	if (resourceType == "viewlet" && (resourceDetail == "vpumlFeatures" || resourceDetail == "sdeFeatures" || resourceDetail == "bpvaFeatures" || resourceDetail == "dbvaFeatures")) {
			htmlHeight = 672;
	}
	
//	preferredSite = getPreferredSite(resourceType, resourceDetail);
//	if (preferredSite == null) {
		detectFeasibleSite(htmlFile, resourceType, resourceDetail, htmlWidth, htmlHeight, showScroll, centerWin, resizeable, showWin, false);
////		executePopup(preferredSite + htmlFile, htmlWidth, htmlHeight, showScroll, centerWin);
//	} else {
//		popwin = executePopup(getHTMLPath(preferredSite, htmlFile, resourceType), htmlWidth, htmlHeight, showScroll, centerWin, resizeable, showWin);
//	}
	
}

function detectFeasibleSite(htmlFile, resourceType, resourceDetail, htmlWidth, htmlHeight, showScroll, centerWin, resizeable, showWin, isDownload, isHidden) {
//alert(isHidden);
//		alert(baseSite + "edocs.php?url="+escape(htmlFile+"&resourceType="+resourceType+"&resourceDetail="+resourceDetail));
//		popwin = executePopup(baseSite + "edocs.php?url="+escape(htmlFile+"&resourceType="+resourceType+"&resourceDetail="+resourceDetail), htmlWidth, htmlHeight, showScroll, centerWin, resizeable, showWin);
	if (isDownload) {
		if (isHidden==true) {
			detecterURL = baseSite+"/balance/detecter.jsp?url="+htmlFile+"&resourceType="+resourceType+"&resourceDetail="+resourceDetail+"&isDownload=true";
			document.location.href=detecterURL;
		} else {
			executePopup(baseSite+"/balance/detecter.jsp?url="+htmlFile+"&resourceType="+resourceType+"&resourceDetail="+resourceDetail+"&isDownload=true", 0, 0, true, false, true, true);
		}
	} else {
		popwin = executePopup(baseSite + "edocs.jsp?url="+htmlFile+"&resourceType="+resourceType+"&resourceDetail="+resourceDetail, htmlWidth, htmlHeight, showScroll, centerWin, resizeable, showWin);
	}
}

function getHTMLPath(site, htmlFile, resourceType) {
		if (resourceType == "viewlet") {
//			return htmlFile + "&basepath="+site+"vpuml/";
			return htmlFile + "&basepath="+site;
		} else {
//			return preferredSite + "vpuml/" +htmlFile;
			return preferredSite + htmlFile;
		}
}

function getHTMLPath2(site, htmlFile, resourceType) {
		if (resourceType == "viewlet") {
			return htmlFile + "&basepath="+site+"vpuml/";
		} else {
			return preferredSite + "vpuml/" +htmlFile;
		}
}

function notifyAndReloadContent(htmlFile, location) {
	savePreferredSite(location);
	document.location.href=location+htmlFile;
}

function getPreferredSite(resourceType, resourceDetail) {
	var arg = resourceType + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
			return getActualVal(getCookieVal (j), resourceDetail);
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0)
			break;   
	}  
	return null;
}

function getCookieVal (offset, resourceDetail) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function getActualVal(cookieVal, resourceDetail) {
	var eqIndex = -1;
	while (true) {
		sepIndex = cookieVal.indexOf("@@");
		if (sepIndex > 0) {
			entryVal = unescape(cookieVal.substring(0, sepIndex));
		} else {
			entryVal = unescape(cookieVal);
		}
		eqIndex = entryVal.indexOf("=");
		if (eqIndex > 0) {
			var name = entryVal.substring(0, eqIndex);
			if (name == resourceDetail) {
				return entryVal.substring(eqIndex+1);
			}
		}		
		cookieVal = cookieVal.substring(sepIndex+2);
		if (sepIndex < 0) {
			break;
		}
	}	
}


function getCookieVal2 (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function savePreferredSite(site) {
//	document.cookie = "preferredSite=" + escape(site)+"; expires="+expDate.toGMTString();
	document.cookie = "preferredSite=" + escape(site);
}

function executePopDownload(htmlFile) {
	document.location.href=htmlFile;
}

function executePopup(htmlFile,htmlWidth,htmlHeight, showScroll, centerWin, resizeable, showWin) {
//	alert(htmlFile);
	if (centerWin) {
	  calculatePopupStartLocation(htmlWidth, htmlHeight);
	}
  str = 'resizable=yes,toolbar=0,menubar=0,';
  str = str + 'scrollbars=no,status=0,location=0,directory=0,width=350,height=200';

  version=Math.round(parseFloat(navigator.appVersion) * 1000);

  if(navigator.appName.indexOf("Konqueror")!=-1) { // konqueror
    htmlWidth+=18;
    htmlHeight+=96;
  }

  if(navigator.appName.indexOf("Netscape")!=-1) {
    if(version>=5000) {
      if(navigator.appVersion.indexOf("Mac")!=-1) { // Netscape6+ on mac
        htmlHeight+=5;
      }
    }
  }

  if(navigator.appName.indexOf("Microsoft")!=-1) {
    if(navigator.appVersion.indexOf("Mac")!=-1) { // IE on Mac
       htmlWidth  -= 11;
       htmlHeight -= 11;
    }
  }
//showWin = true;
	if (showScroll) {
		if (showWin) {
      win = window.open(htmlFile,'VPScreenshot','location=1,menubar=1,toolbar=1,status=1,resizable=yes,scrollbars=yes'+ ',width='+htmlWidth+',height='+htmlHeight+',top='+startTop+', left='+startLeft);	
		} else {
	    win = window.open(htmlFile,'VPScreenshot','resizable=yes,scrollbars=yes'+ ',width='+htmlWidth+',height='+htmlHeight+',top='+startTop+', left='+startLeft);	
		}
		win.focus();
	} else {
		if (showWin) {
      win = window.open(htmlFile,'VPScreenshot','location=1,menubar=1,toolbar=1,status=1,resizable=yes,scrollbars=no'+ ',width='+htmlWidth+',height='+htmlHeight+',top='+startTop+', left='+startLeft);	
		} else {
      win = window.open(htmlFile,'VPScreenshot','resizable=yes,scrollbars=no'+ ',width='+htmlWidth+',height='+htmlHeight+',top='+startTop+', left='+startLeft);	
		}
		win.focus();
	}	
	return win;
//  window.open(htmlFile + ', resizable=yes, scrollbars=yes, width='+htmlWidth+', height='+htmlHeight+',top='+startTop+', left='+startLeft);
}

function calculatePopupStartLocation(htmlWidth, htmlHeight) {
  if(navigator.appName.indexOf("Netscape")==-1) {
		startTop = (window.screen.height/2)  - (htmlHeight/2) - 24;
		startLeft = (window.screen.width/2) - (htmlWidth/2) - 6;
		startTop = Math.ceil(startTop);
		startLeft = Math.ceil(startLeft);
//		alert ("window.screen.width: " + window.screen.width + "\nhtmlWidth: " + htmlWidth +"\nstartLeft: " + startLeft);
  }
}

function popupAnchor(aLink, aTarget, aWidth, aHeight) {
	var lWindowProperties = "width="+aWidth+",height="+aHeight+",toolbar=0,location=0,menubar=0,resizable=0";
	return window.open(aLink, aTarget, lWindowProperties);
}
