/*
 * Written 11/14/2007 by Chris Schroeder
 * for Johns Hopkins Bloomberg School of Public Health
 */

var requestInclude = null;//include container
var requestContainer = null;//include placement id

/* this is the AJAX request. 
 * We can move this to a common lib soon.
 */
function createRequest() {
	var request = null;
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}
	if (request == null) {
		alert ("Error creating request object");
	} else {
		return request;
	}
}

/*
 * We kick off each new app with this function
 * to declare all includes we intend to use.
 */
	function setCFVars(pages) {
		if(pages) {
			for (key in pages) {
				eval(key+"='"+pages[key]+"'");
			}
		}
	}
	
	function getURLVars(page) {
		thisPage = String(page).split('?');
		if(thisPage[1]) {
			urlVars = thisPage[1].split('&');
			for(i=0;i<=urlVars.length;i++) {
				if(urlVars[i]) {
					thisVar =String(urlVars[i]).split('=');
					my = [];
					my.key = thisVar[0];
					my.value = thisVar[1]
					eval(my.key + "='" + my.value + "'");
				}
			}
		}
	}
	
	function getBanner(requestInclude,query) {
		requestInclude = requestInclude+"?";
		if(query) {
			for (key in query) {
				if(typeof key != "number") {
					requestInclude += key + "=" + query[key] + "&";
				}
			}
		}		
		jsRequest = createRequest();
		jsRequest.open("GET", requestInclude, true);
		jsRequest.onreadystatechange = function() {
			if(jsRequest.readyState == 4) {
				document.write('<div class="titleBar" title="'+jsRequest.responseText.boxTitle+'">');
				document.write('<span class="titleText"><a href="'+jsRequest.responseText.content_url+'" title="'+jsRequest.responseText.banner_title+'">'+jsRequest.responseText.banner_title+'<'+'/a><'+'/span>');
				document.write('<'+'/div>');
				document.write('<div class="flashImage" title="'+jsRequest.responseText.boxTitle+'">');
				document.write('<a href="'+jsRequest.responseText.content_url+'" title="'+jsRequest.responseText.banner_title+'"><img src="'+jsRequest.responseText.fullpath+'" width="'+jsRequest.responseText.boxWidth+'" height="'+jsRequest.responseText.boxHeight+'" alt="'+jsRequest.responseText.altText+'"/><'+'/a>');
				document.write('<'+'/div>');
			}		
		}
		jsRequest.send(null);
	}
	

/*
 * This is the AJAX request for the 
 * individual projects
 */
	
function requestHandler(requestInclude, query){
	requestInclude = requestInclude + "?";
	if(query) {
		for (key in query) {
			requestInclude += key + "=" + query[key] + "&";
		}
	}		
	ajaxRequest = createRequest();
	ajaxRequest.open("GET", requestInclude, true);
	ajaxRequest.onreadystatechange = loadInfo;
	ajaxRequest.send(null);
}

/*
 * This is the function that loads the 
 * project data into the itemContainer
 */
	function loadInfo() {
		//clearInfo();we clear this here because of a Safari rendering bug
		if(ajaxRequest.readyState == 4) {
			document.getElementById(requestContainer).innerHTML = ajaxRequest.responseText;
		}
	}
	
	function cleanUp() {
		var finishedEditing = confirm("Your edits have been entered. Click OK to finish or Cancel to continue making changes.");
		if(finshedEditing) {
			window.location=document.location;
		}
	}
	
/*
 * Better versions of the functions above 
 */
	function rHandler(requestInclude,requestContainer,query){
		requestInclude = requestInclude + "?";
		if(query) {
			for (key in query) {
				requestInclude += key + "=" + query[key] + "&";
			}
		}		
		aRequest = createRequest();
		aRequest.open("GET", requestInclude, true);
		aRequest.onreadystatechange = function () {
			if(aRequest.readyState == 4) {
				loadResponse(requestContainer,aRequest);
			}
		}
		aRequest.send(null);
	}

	function loadResponse(requestContainer,aResponse) {
		//clearInfo();we clear this here because of a Safari rendering bug
		document.getElementById(requestContainer).innerHTML = aResponse.responseText;
	}

/* 
 * Removes content from the itemContainer
 */
	function clearInfo() {
		document.getElementById(requestContainer).innerHTML = "";
	}
	
	function makeHighlightedLink(txtarea){
		var sl = (txtarea.value).substring(txtarea.selectionStart,
		txtarea.selectionEnd);  
		strHref = prompt("Enter the URL you want to link to:", "")
			if (strHref == null) return;
			if (strHref.indexOf('http://') == -1) {
				strHref = "http://" + strHref;
			}
		
		txtarea.value = txtarea.value.replace(sl,"<a href='" + strHref + "' title='" + sl + "'>" + sl + "</a>")
			return;
	}

	function makeLink(inputName) {
		strSelection = inputName.value;
	
		strHref = prompt("Enter the URL you want to link to:", "")
		if (strHref == null) return;
		if (strHref.indexOf('http://') == -1) {
			strHref = "http://" + strHref;
		}
	
		inputName.value = "<a href='" + strHref + "' title='" + strSelection + "'>" + strSelection + "</a>"
		return;
	}
	
	function requestConfirmation() {
		var confirmed = confirm("Click OK to send this email.\rClick cancel if you'd like to make changes or adjustments.");
		if(confirmed) {
			return true;
		} else {
			return false;
		}
	}
	
	function buildBookmarks() {
		if(document.location) {
			document.write('<link rel="stylesheet" href="http://commprojects.jhsph.edu/_includes/generic.css" type="text/css">');
			document.write('<div class="bookMarks">');
			document.write('<a href="http://del.icio.us/post?title='+document.title+'&url='+document.location+'" title="del.icio.us" target="_blank"><img src="http://commprojects.jhsph.edu/_images/icon_delicious.gif" alt="del.icio.us" border="0"></a> ');
			document.write(' <a href="http://del.icio.us/post?title='+document.title+'&url='+document.location+'" title="del.icio.us" target="_blank" class="bookMark Normal">del.icio.us</a>');
			document.write(' | ');
			document.write('<a href="http://www.google.com/bookmarks/mark?op=edit&bkmk='+document.location+'&title='+document.title+'&zx=1SGlL5riQvQ" title="Google"><img src="http://commprojects.jhsph.edu/_images/google.gif" alt="Google" border="0"></a>');
			document.write(' <a href="http://www.google.com/bookmarks/mark?op=edit&bkmk='+document.location+'&title='+document.title+'&zx=1SGlL5riQvQ" title="Google" class="bookMark Normal">Google</a>');
			document.write(' | ');
			document.write('<a href="http://digg.com/submit?phase=2&url='+document.location+'&title='+document.title+'" title="digg.com" target="_blank"><img src="http://commprojects.jhsph.edu/_images/digg.gif" alt="digg.com" border="0"></a>');
			document.write(' <a href="http://digg.com/submit?phase=2&url='+document.location+'&title='+document.title+'" title="digg.com" target="_blank" class="bookMark Normal">digg.com</a>');
			document.write(' | ');
			//document.write('<a href="http://www.technorati.com/search/'+	document.location+'" title="technorati" target="_blank"><img src="http://commprojects.jhsph.edu/_images/technorati.gif" alt="technorati" border="0"></a>');
			//document.write(' <a href="http://www.technorati.com/search/'+	document.location+'" title="technorati" target="_blank" class="bookMark Normal">technorati</a>');
			//document.write(' | ');
			document.write('<a href="http://www.facebook.com/share.php?u='+	document.location+'" title="facebook" target="_blank"><img src="http://commprojects.jhsph.edu/_images/facebook_icon.gif" alt="facebook" border="0"></a>');
			document.write(' <a href="http://www.facebook.com/share.php?u='+	document.location+'" title="facebook" target="_blank" class="bookMark Normal">facebook</a>');
			document.write('</div>');
		}
	}

	function changeVal(fField, otherVal, changedVal, changedText) {
		if(otherVal.value != "") {
			fField.options[fField.selectedIndex].value = changedVal;
			fField.options[fField.selectedIndex].text = changedText;
		}
	} 
	
/* Keyword/Link replacement functions */
function replaceKeywords(requestInclude,tn) {
	bRequest = createRequest();
	bRequest.open("GET", requestInclude, true);
	bRequest.onreadystatechange = function () {
		if(bRequest.readyState == 4) {
			if(bRequest.responseText) {
				var j = eval("("+bRequest.responseText+")");
				splitResponse(j,tn);
			}
		}
	}
	bRequest.send(null);
}

function splitResponse(response,tagName) {
	for (key in response) {
		var t = createLink(response[key],key);
		setTopicLinks(t,key,tagName)
	}
}

function createLink(urlLink,keyword) {
	var m = '<a href="'+urlLink+'" title="'+keyword+'">'+keyword+'<'+'/a> ';
	return m;
}

function setTopicLinks(relatedLink,keywordText,tagName) {
	var  x  =  document.getElementsByTagName(tagName);  
	for  (i=0;i  <  x.length;i++){
		var l = x[i].innerHTML;
		var s = keywordText+" ";
		if(x[i].className == 'linkSection') {
			x[i].innerHTML  =  l.replace(s,relatedLink);
		}
	}
}

/* adds and subtracts values from a form field */
function addValue(field,increment) {
	field.value = parseInt(field.value) + parseInt(increment);
}
	
function removeValue(field,increment) {
	field.value = parseInt(field.value) - parseInt(increment);
}
