
// code adapted from w3schools.com 
function isAjaxSupported()
{
	try {    // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {    // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return (false);
			}
		}
	}
	return (xmlHttp);
}


// Uses AJAX to transparently save the mapped survey changes - no page refresh needed...
function updateMapping(sid, qid, tsid, tqid)
{
	var xmlHTTP = isAjaxSupported();

	if (!(xmlHTTP)) { return (false); }
	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4) {
			document.getElementById("questionchooser").innerHTML = xmlHttp.responseText;
		}
		}
	var url = "processTrendSurveyUpdate.cfm?surveyid=" + sid
		+ "&questionid=" + qid + "&trendsurveyid=" + tsid
		+ "&trendquestionid=" + tqid;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


function addDemoMapping(sid, did, rid, tsdri)
{
	var xmlHTTP = isAjaxSupported();
	var divsdri = "div" + sid + "|" + did + "|" + rid;

	if (!(xmlHTTP)) { return (false); }
	xmlHttp.onreadystatechange=function(divsdri)
		{
		if(xmlHttp.readyState==4) {
			//document.getElementById("questionchooser").innerHTML = xmlHttp.responseText;
		}
		}
	var url = "processTrendDemoMapping.cfm?action=add&" 
		+ "surveyid=" + sid	+ "&demoid=" + did + "&realid=" + rid + "&trendsdr=" + tsdri;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


function removeDemoMapping(sid, did, rid, tsid, tdid, trid)
{
	var xmlHTTP = isAjaxSupported();
	var divsdri = "div" + sid + "|" + did + "|" + rid;
	var tsdri = tsid + "|" + tdid + "|" + trid;

	if (!(xmlHTTP)) { return (false); }
	xmlHttp.onreadystatechange=function(divsdri)
		{
		if(xmlHttp.readyState==4) {
			//document.getElementById("questionchooser").innerHTML = xmlHttp.responseText;
		}
		}
	var url = "processTrendDemoMapping.cfm?action=remove&" 
		+ "surveyid=" + sid	+ "&demoid=" + did + "&realid=" + rid + "&trendsdr=" + tsdri;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


function seedWordDatabase(sid)
{
	var xmlHTTP = isAjaxSupported();
	if (!(xmlHTTP)) { return (false); }

	document.getElementById("divpanel").innerHTML = 
		"<p>Please wait while we seed the database with words...</p>";
	document.getElementById("divword").innerHTML =
		"<p>Click on a word on the left to see more details...</p>";
	
	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4) {
			document.getElementById("divpanel").innerHTML = xmlHttp.responseText;
		}
		}
	
	var url = "processSeedWords.cfm?aActiveSurveyID=" + sid;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


function viewWordDictionary(sid, letter, searchstr, sortstr)
{
	var xmlHTTP = isAjaxSupported();
	if (!(xmlHTTP)) { return (false); }

	document.getElementById("divpanel").innerHTML = 
		"<p>Please wait while we filter and sort the word dictionary...</p>";
	
	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4) {
			document.getElementById("divpanel").innerHTML = xmlHttp.responseText;
		}
		}
	
	var url = "processViewDictionary.cfm?aActiveSurveyID=" + sid + "&letter=" + letter
		+ "&searchstr=" + searchstr + "&sortstr=" + sortstr;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


var occurlist = "|";

function viewWord(sid, word)
{
	var xmlHTTP = isAjaxSupported();
	if (!(xmlHTTP)) { return (false); }

	occurlist = "|";

	document.getElementById("divword").innerHTML = 
		"<p>Please wait while we compute the details about the chosen word...</p>";
	
	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4) {
			document.getElementById("divword").innerHTML = xmlHttp.responseText;
		}
		}
	
	var url = "processViewWord.cfm?aActiveSurveyID=" + sid + "&word=" + word;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


function toggleDisplay(elid)
{
	var dispval = document.getElementById(elid).style.display;
	if (dispval == "none") dispval = "block";
	else dispval = "none";
	document.getElementById(elid).style.display = dispval;
}


function selectOccurrence(surveyid, questionid, responseid, posi)
{
	var selectcolor = "#9999ff";
	var highlightcolor = "#ffff99";
	var spanid = surveyid + ":" + questionid + ":" + responseid + ":" + posi;
	var currentcolor = document.getElementById(spanid).style.backgroundColor;
	if (currentcolor == selectcolor) {
		// Then unselect the occurrence
		document.getElementById(spanid).style.backgroundColor = highlightcolor;
		var pos = occurlist.lastIndexOf(spanid);
		if (pos >= 0) {
			replacestring = spanid + "|";
			occurlist = occurlist.replace(replacestring, "");
		}
	} else {
		// Then select the occurrence
		document.getElementById(spanid).style.backgroundColor = selectcolor;
		occurlist = occurlist + spanid + "|";
	}
}


function replaceWord(wordold, wordnew, replacetype)
{
	var xmlHTTP = isAjaxSupported();
	if (!(xmlHTTP)) { return (false); }

	document.getElementById("divword").innerHTML = 
		"<p>Please wait while we replace the word '" + wordold 
		+ "' with the new word '" + wordnew + "'...</p>";
	

	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4) {
			document.getElementById("divword").innerHTML = xmlHttp.responseText;
		}
		}

	var url = "processChangeWord.cfm?wordold=" + wordold
				+ "&wordnew=" + wordnew + "&occurrence=";
	if (replacetype == "ALL") {
		url = url + replacetype;
	} else {
		url = url + occurlist;
	}
	occurlist = "|";
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


function preloadReplaceAll(surveyid)
{
	var xmlHTTP = isAjaxSupported();
	if (!(xmlHTTP)) { return (false); }

	document.getElementById("divword").innerHTML = 
		"<p><b>Please wait while we preload the replace all database...</b></p>";

	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4) {
			document.getElementById("divword").innerHTML = xmlHttp.responseText;
		}
		}

	var url = "processPreloadReplaceAll.cfm?surveyid=" + surveyid;
	occurlist = "|";
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


function generateCleanedResponses(surveyid)
{
	var xmlHTTP = isAjaxSupported();
	if (!(xmlHTTP)) { return (false); }

	document.getElementById("divword").innerHTML = 
		"<p><b>Please wait while we generate all the cleaned responses...</b></p>";

	xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4) {
			document.getElementById("divword").innerHTML = xmlHttp.responseText;
		}
		}

	var url = "processGenerateCleanedResponses.cfm?surveyid=" + surveyid;
	occurlist = "|";
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}
