if(!window.WWWROOT) window.WWWROOT = "/images/"
if(!window.RESOURCES) window.RESOURCES = "/mskcc_resources/"
if(!window.baseDomain) window.baseDomain = ''

if(!window.checkProtocol)
	document.write('<SCRIPT SRC="/template_resources/_javascript/protocols/http.js"></SCRIPT>')

var defaultAddress = 'publicaffairs@mskcc.org'

var formWindow = 'width=460,height=460,resizable=1,status=1,scrollbars=1,menubar=0,location=0,toolbar=0'
var imageWindow = 'width=620,height=460,resizable=1,status=1,scrollbars=1,menubar=0,location=0,toolbar=0'
var resizableImageWindowURL = "/mskcc/html/53258.cfm"

var _pagingBackGroundColor = "";
// var maxWordBuffer = 100;

/****************** SET DOCUMENT TITLE ******************/
function setDocumentTitle(title){
	if(isNN4) return 
	document.title = 'Sloan-Kettering - ' + title
}

/****************** OPEN IN PARENT WINDOW ******************/
function openInParentWindow(href){
	if(top.opener){
		top.opener.top.location = href
		top.close
	} else {
		top.location = href
	}	
	return false
}

/*************** OPEN PMID ***************/
function openPMID(ID){
	location = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=PubMed&cmd=Retrieve&list_uids=" + ID + "&dopt=Citation";
}

/*************** OPEN PUBMED BY AUTHOR ***************/
function openPubMedByAuthor(name){
	name = name.replace(/, ?(III|JR)/i, '')
	location = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=search&db=pubmed&term=(((\"neoplasms\"[MH]+OR+cancer[TW])+OR+sloan-kettering+AND+" + escape(name) + "[AU])";
}
	
/*************** OPEN PUBMED ***************/
function openPubMed(text){
	if(text.indexOf("http://") == 0)
		location = text;
	else
		openPubMedByAuthor(text)
}

/*************** OPEN USPTO ***************/
function openUSPTO(id){
	//	http://patft.uspto.gov/netacgi/nph-Parser?TERM1=6,923,014&Sect1=PTO1&Sect2=HITOFF&d=PALL&p=1&u=%2Fnetahtml%2Fsrchnum.htm&r=0&f=S&l=50
	location = "http://patft.uspto.gov/netacgi/nph-Parser?TERM1=" + id + "&Sect1=PTO1&Sect2=HITOFF&d=PALL&p=1&u=%2Fnetahtml%2Fsrchnum.htm&r=0&f=S&l=50";
}

/*************** OPEN WIPO ***************/
function openWIPO(id){
	// http://v3.espacenet.com/textdoc?DB=EPODOC&IDX=WO0164635
	location = "http://v3.espacenet.com/textdoc?DB=EPODOC&IDX=" + id;
}

/*************** OPEN GIVING ***************/
function openGiving(){
	location = "http://www.mskcc.org/giving/giftintro.html";
}

/****************** GOTO MENU ******************/
function gotoMenu(element){
	var value = element.options[element.selectedIndex].value

	// fix glossary urls
	if(location.pathname == RESOURCES + "glossary/index.asp"){
		var reID = /(location=')(\d+)/i
		if(reID.test(value))
			value = value.replace(reID, "$1" + WWWROOT + "html/$2")
	}

	eval(value, "unsafe"); 
	return false
}

/****************** SEARCH HELP ******************/
function searchHelp(){
	openWindow(WWWROOT + 'html/5666.cfm','searchHelp', imageWindow);
}

/****************** OPEN FLASH APPLICATION ******************/
function openFlashApplication(){
	if(arguments.length == 4){
		var type = arguments[0]
		var title = arguments[1]
		var width = arguments[2] 
		var height = arguments[3]
	} else {
		var title = ''
		var type = arguments[0]
		var width = arguments[1] 
		var height = arguments[2]
	}

	var win = "width=" + (width+10) + ",height=" + (height + 10) + ",resizable"
	var query = 'type=' + type + "&width=" + width + "&height=" + height + ((title)?"&title=" + escape(title):"")
	openWindow('/mskcc/shared/forms/Nomograms/flash/detect.cfm?' + query, type + 'Application' + type, win)
}

/****************** OPEN NOMOGRAMS ******************/
function openNomograms(type){
	type = (!type || type=='prostate')?'Prostate':type
	var title = type.charAt(0).toUpperCase() + type.substring(1) + ' Nomogram'

	if(type.toLowerCase() == 'prostate')
		openFlashApplication('Prostate', title, 586, 505)
	else
		openFlashApplication(type, title, 586, 399)
}

/****************** OPEN DICTIONARY ******************/
function openDictionary(id){
	if(location.pathname == RESOURCES + "glossary/index.asp"){
		openWindow('/mskcc/html/12501.asp?ID=' + id, 'Dictionary' + id, imageWindow)
		return false
	} else {
		return true
	}
}

/****************** GLOSSIFY DOCUMENT ******************/
function glossifyDocument(){
	if(location.pathname == RESOURCES + "glossary/index.asp"){
		history.go(-1)
	} else {
		var url = (window.location.pathname + window.location.search + '').replace(/\/(print|glossary)\//i, "/html/");
		location = RESOURCES + "glossary/index.asp?URL=" + escape(url) + window.location.hash;
	}
	return false
}

/****************** GET STYLESHEET ******************/
function getStyleSheet(){
	var text = '<STYLE>'
	text += '.lightBackgroundColor{background-color:' + window.LightColor + '}\n';
	text += '.mediumBackgroundColor{background-color:' + window.MediumColor + '}\n';
	text += '.darkBackgroundColor{background-color:' + window.DarkColor + '}\n';
	text += '</STYLE>'
	return text
}

/****************** OPEN FORM WINDOW ******************/
function openFormWindow(ID){
	if(arguments.length == 2){ 
		var query = arguments[1]
	} else {
		var query = ((arguments[1])?"EmailTo=" + escape(arguments[1]) + "&":"") + 
			((arguments[2])?"EmailFrom=" + escape(arguments[2]) + "&":"") + 
			((arguments[3])?"title=" + escape(arguments[3]) + "&":"") + 
			((arguments[4])?"subtitle=" + escape(arguments[4]) + "&":"")
	}
	if(!isNaN(ID))
		var location = '/mskcc/html/' + ID + '.cfm'
	else
		var location = ID

	// intranet templ fix
	if(WWWROOT == "/intranet/" && (ID==12431 || ID==12433) )
		location = "http://www.mskcc.org" + location
	else
		location = baseDomain + location

	openWindow(location + '?' + query, 'Form' + ID, formWindow)
}

/****************** SEND MAIL ******************/
function sendMail(address, subject){
	if(!address)
		address = defaultAddress
	
	var formID = (address.toLowerCase() == "publicaffairs@mskcc.org")?57702:12431
	var query = 'EmailTo=' + ((address)?address:defaultAddress) + ((subject)?'&subject=' + subject:'')
	openFormWindow(formID, query)
}

/****************** SEND TO A FRIEND ******************/
function sendToAFriend(title, url, breadcrumb, description){
	if(title == "undefined") title = window.ShortTitle
	if(!description) description = ''

	var query = 'PageTitle=' + escape(title) + '&PageURL=' + escape(url) + '&PageBreadCrumb=' + escape(breadcrumb) + '&PageDescription=' + escape(description)
	openFormWindow(12433, query)
}

/****************** ORDER ******************/
function order(SubTitle, to){
	var query = 'EmailTo=' + escape(to) +  '&SubTitle=' + escape((SubTitle)?SubTitle:'')
	openFormWindow(12474, query)
}

/****************** REGISTER ******************/
function register(SubTitle, to){
	var query = 'EmailTo=' + escape(to) +  '&SubTitle=' + escape((SubTitle)?SubTitle:'')
	openFormWindow(12481, query)
}

/****************** SKI SUMMER INTERNSHIP ******************/
function skiSummerInternship(to){
	var query = ((to)?'EmailTo=' + escape(to):'')
	openFormWindow(12483, query)
}

/****************** VIEW IMAGE ******************/
function viewImage(src, title, caption){
	var href = '/mskcc/html/66470.cfm?ImageSrc=' + escape(src) + 
		((title)?'&ImageTitle=' + escape(title):'') + 
		((caption)?'&ImageCaption=' + escape(caption):'')

	openWindow(href, 'view_image', imageWindow)
}

/****************** PROMPT SUBSCRIBE ******************/
function promptSubscribe(){
	var emailAddress = prompt("Subscribe to Lately@MSKCC\nPlease enter your email address.", '')
	if(emailAddress)
		subscribe(emailAddress)
}

/****************** SUBSCRIBE LATELY@MSKCC ******************/
function subscribe(Email){
	if(Email == "Enter E-mail Address"){Email = "";}
	location = '/mskcc/html/19370.cfm?Email=' + escape(Email);
}
subscribeToLately = subscribe

/****************** UNSUBSCRIBE LATELY@MSKCC ******************/
function unsubscribe(Email){
	if(Email)
		openWindow('/mskcc/html/12487.cfm?Email=' + escape(Email), 'UnSubscribe', formWindow)
	else
		openWindow('/mskcc/html/12487.cfm', 'UnSubscribe', formWindow)
}
unsubscribeToLately = unsubscribe

/****************** SUBSCRIBE TO NEWS@MSKCC ******************/
function subscribeToNews(to){
	openWindow('/mskcc/html/12557.cfm?EmailTo=' + escape(to), 'SubscribeNews', formWindow)
}

/****************** UNSUBSCRIBE TO NEWS@MSKCC ******************/
function unsubscribeToNews(to){
	openWindow('/mskcc/html/12559.cfm?EmailTo=' + escape(to), 'UnSubscribeNews', formWindow)
}

/****************** CHECK SEARCH CRITEIA ******************/
function checkSearchCriteria(form){
	if(!form.Criteria) return true
	if(form.Criteria.value == "")return true
	if(window._Criteria == form.Criteria.value){
		alert("Your search for '" + form.Criteria.value + "' is in process.  Please wait.")
		return false
	}
	window._Criteria = form.Criteria.value
	return true
}

/****************** OPEN EVENT DETAILS ******************/
function openLbcEvent(EventID){
	location = '/mskcc/html/59513.cfm?EventView=details&CategoryID=3&EventID=' + EventID
}

/****************** DETECT COOKIES ******************/
function detectCookies(ID){
	if(!isCookiesEnabled()){
		location.replace('/mskcc/html/' + (ID || 71961) + '.cfm');
	}

	// is cookies enabled
	function isCookiesEnabled(){
		document.cookie = "_t_=1";
		return (document.cookie.indexOf("_t_")!=-1)?true:false;
	}
}
