window.onload = function(){};
function registerOnload(newFunction){
	// alert(registerOnload.caller + " " + newFunction)
	var curWinOnload = window.onload;

	if(typeof window.onload != 'function'){
		window.onload = newFunction;
	}
	else{
		window.onload = function(){
			curWinOnload();
			newFunction();
		};
	}
}


var questions_result = new Array();

// calculate a score based on user selections and display the appropriate 
// text as a response. Displays an error if the user submits the form
// without providing any selections
function calcTotal()
{
	var score = 0;
	var q1Score = 0;
	var q2Score = 0;
	var q3Score = 0;
	var q4Score = 0;
	var msgCount = 0;
	var riskLabel = "";
	var riskTxt = "";
	
	var track_result_txt = "";
	var risk_short = "";
	var nr = 0;
	questions_result = new Array();
	
	var theForm = document.getElementById("calcForm");
	var questions = new Array("q1","q2","q3","q4");
	
	// question 1
	var checked = false;
	for (var i = 0; i < 7; i++)
	{
		if (theForm.q1[i].checked){
			q1Score = parseInt(theForm.q1[i].value) * .5;
			msgCount++;
			checked = true;
			var rad_id = theForm.q1[i].id;
			parseQuestionResult(1, rad_id, 'left');
			//var nr = i+1; 
			//addQuestionsResult("Q1", "A"+nr, 1);
		}
	}
	if (!checked) parseQuestionResult(1, "noA", "");
			
	// question 2
	var checked = false;
	for (var i = 0; i < 2; i++)
	{
		if (theForm.q2[i].checked){
			q2Score = parseInt(theForm.q2[i].value);
			msgCount++;
			checked = true;
			var rad_id = theForm.q2[i].id;
			parseQuestionResult(2, rad_id, 'left');
			//var nr = i+1; 
			//addQuestionsResult("Q2", "A"+nr, 2);
		}	
	}
	if (!checked) parseQuestionResult(2, "noA", "");
			
	// question 3
	var checked = false;
	for (var i = 0; i < 2; i++)
	{
		if (theForm.q3[i].checked){
			q3Score = parseInt(theForm.q3[i].value) * (-1.5);
			msgCount++;
			checked = true;
			var rad_id = theForm.q3[i].id;
			parseQuestionResult(3, rad_id, 'left');
			//var nr = i+1; 
			//addQuestionsResult("Q3", "A"+nr, 3);
		}	
	}
	if (!checked) parseQuestionResult(3, "noA", "");
	
	// question 4
	var checked = false;
	for (var i = 0; i < 2; i++)
	{
		if (theForm.q4[i].checked){
			q4Score = parseInt(theForm.q4[i].value) * 2;
			msgCount++;
			checked = true;
			var rad_id = theForm.q4[i].id;
			parseQuestionResult(4, rad_id, 'left');
			//var nr = i+1; 
			//addQuestionsResult("Q4", "A"+nr, 4);
		}	
	}
	if (!checked) parseQuestionResult(4, "noA", "");
	
	$("#result_short").val("");
	
	if (msgCount == 4){
	
		score = q1Score + q2Score + q3Score + q4Score;

		if (score < 1.5){
			riskLabel = "Low risk: ";
			riskTxt = "There’s a less than 30% probability of you having high blood pressure, high cholesterol, diabetes and heart disease";
			risk_short = "Low risk (less than 30%)";
		}	
		if (score >= 1.5 && score <= 2.5){
			riskLabel = "Medium risk: ";
			riskTxt = "There’s a 30-59% probability of you having high blood pressure, high cholesterol, diabetes and heart disease";
			risk_short = "Medium risk (30-59%)";
		}	
		if (score > 2.5){
			riskLabel = "High risk: ";
			riskTxt = "There’s more than a 60% probability of you having high blood pressure, high cholesterol, diabetes and heart disease ";
			risk_short = "High risk (more than 60%)";
		}
		
		
		$("#result_short").val(risk_short);
		document.getElementById("span1").className = '';
		
		//track_result_txt = riskLabel+" "+riskTxt; 
		track_result_txt = risk_short; 
		
	}
	else {
		riskLabel = "To calculate your risk, please answer all four of the above questions.";
		document.getElementById("span1").className = 'error';
		$("#result_short").val("");
		
		track_result_txt = "Questionary incomplete!";
	}
		
	if (document.getElementById("span1").hasChildNodes())
		document.getElementById("span1").removeChild(document.getElementById("span1").firstChild);
	
	if (document.getElementById("span2").hasChildNodes())
		document.getElementById("span2").removeChild(document.getElementById("span2").firstChild);
	
	document.getElementById("span1").appendChild(document.createTextNode(riskLabel));
	document.getElementById("span2").appendChild(document.createTextNode(riskTxt));
	
	document.getElementById("results").style.display = 'block';
	
	track_result(questions_result, track_result_txt, "underlyingConditions");

	return false;
}

// calculate a score based on user selections and display the appropriate 
// text as a response. Displays an error if the user submits the form
// without providing any selections
function calcTotal_suffer()
{

	var score = 0;
	var q1Score = 0;
	var q2Score = 0;
	var q3Score = 0;
	var q4Score = 0;
	var q5Score = 0;
	var msgCount = 0;
	var riskLabel = "";
	var riskTxt = "";
	
	var track_result_txt = "";
	var risk_short = "";
	var nr = 0;
	questions_result = new Array();
	
	var theForm = document.getElementById("calcForm");
	var questions = new Array("q1","q2","q3","q4","q5");
	
	// question 1
	var checked = false;
	for (var i = 0; i < 6; i++)
	{
		if (theForm.q1[i].checked){
			q1Score = parseInt(theForm.q1[i].value);
			msgCount++;
			checked = true;
			//var nr = i+1; 
			//addQuestionsResult("Q1", "A"+nr, 1);
			var rad_id = theForm.q1[i].id;
			parseQuestionResult(1, rad_id, 'right');
		}
	}
	if (!checked) parseQuestionResult(1, "noA", "");
			
	// question 2	
	var checked = false;
	for (var i = 0; i < 6; i++)
	{
		if (theForm.q2[i].checked){
			q2Score = parseInt(theForm.q2[i].value);
			msgCount++;
			checked = true;
			//var nr = i+1; 
			//addQuestionsResult("Q2", "A"+nr, 2);
			var rad_id = theForm.q2[i].id;
			parseQuestionResult(2, rad_id, 'right');
		}	
	}
	if (!checked) parseQuestionResult(2, "noA", "");
	
	// question 3
	var checked = false;
	for (var i = 0; i < 5; i++)
	{
		if (theForm.q3[i].checked){
			q3Score = parseInt(theForm.q3[i].value);
			msgCount++;
			checked = true;
			//var nr = i+1; 
			//addQuestionsResult("Q3", "A"+nr, 3);
			var rad_id = theForm.q3[i].id;
			parseQuestionResult(3, rad_id, 'right');
		}	
	}
	if (!checked) parseQuestionResult(3, "noA", "");
	
	// question 4
	var checked = false;
	for (var i = 0; i < 6; i++)
	{
		if (theForm.q4[i].checked){
			q4Score = parseInt(theForm.q4[i].value);
			msgCount++;
			checked = true;
			//var nr = i+1; 
			//addQuestionsResult("Q4", "A"+nr, 4);
			var rad_id = theForm.q4[i].id;
			parseQuestionResult(4, rad_id, 'right');
		}	
	}
	if (!checked) parseQuestionResult(4, "noA", "");
	
	// question 5
	var checked = false;
	for (var i = 0; i < 5; i++)
	{
		if (theForm.q5[i].checked){
			q5Score = parseInt(theForm.q5[i].value);
			msgCount++;
			checked = true;
			//var nr = i+1; 
			//addQuestionsResult("Q5", "A"+nr, 5);
			var rad_id = theForm.q5[i].id;
			parseQuestionResult(5, rad_id, 'right');
		}	
	}
	if (!checked) parseQuestionResult(5, "noA", "");
	
	$("#result_short").val("");
	
	if (msgCount == 5){
	
		score = q1Score + q2Score + q3Score + q4Score + q5Score;

		if (score >= 22 && score <= 25){
			riskLabel = "Have you got ED?";
			riskTxt = "Your answers indicate that you haven’t got erectile dysfunction. However, if you have any concerns, why not discuss them with your doctor? Your GP should be able to help you with any difficulties or worries you may have.";
			risk_short = "Your answers indicate that you haven’t got erectile dysfunction.";
		}
		if (score >= 17 && score <= 21){
			riskLabel = "Have you got ED?";
			riskTxt = "Your answers indicate that you have mild erectile dysfunction. Fortunately, there are lots of things you can do about it. The first step is to visit your doctor to get an official diagnosis and discuss treatment options.";
			risk_short = "Your answers indicate that you have mild erectile dysfunction.";
		}	
		if (score >= 12 && score <= 16){
			riskLabel = "Have you got ED?";
			riskTxt = "Your answers indicate that you have mild to moderate erectile dysfunction. Fortunately, there are lots of things you can do about it. The first step is to visit your doctor to get an official diagnosis and discuss treatment options.";
			risk_short = "Your answers indicate that you have mild to moderate erectile dysfunction.";
		}			
		if (score >= 8 && score <= 11){
			riskLabel = "Have you got ED?";
			riskTxt = "Your answers indicate that you have moderate erectile dysfunction. Fortunately, there are lots of things you can do about it. The first step is to visit your doctor to get an official diagnosis and discuss treatment options.";
			risk_short = "Your answers indicate that you have moderate erectile dysfunction.";
		}	
		if (score >= 1 && score <= 7){
			riskLabel = "Have you got ED?";
			riskTxt = "Your answers indicate that you have severe erectile dysfunction. Fortunately, there are lots of things you can do about it. The first step is to visit your doctor to get an official diagnosis and discuss treatment options.";
			risk_short = "Your answers indicate that you have severe erectile dysfunction.";
			
		}			
		document.getElementById("span1").className = '';
		
		$("#result_short").val(risk_short);
		
		track_result_txt = risk_short; 
		
	}
	else {
		riskLabel = "To calculate your risk, please answer all five of the above questions.";
		document.getElementById("span1").className = 'error';
		
		$("#result_short").val("");
		
		track_result_txt = "Questionary incomplete!";
	}
		
	if (document.getElementById("span1").hasChildNodes())
		document.getElementById("span1").removeChild(document.getElementById("span1").firstChild);
	
	if (document.getElementById("span2").hasChildNodes())
		document.getElementById("span2").removeChild(document.getElementById("span2").firstChild);
	
	document.getElementById("span1").appendChild(document.createTextNode(riskLabel));
	document.getElementById("span2").appendChild(document.createTextNode(riskTxt));
	
	document.getElementById("results").style.display = 'block';
	
	
	track_result(questions_result, track_result_txt, "doIhave");

	return false;
}

function parseQuestionResult(nr, id, pos) {
	var q = $("#qtxt_"+nr).html();
	
	if (id != "noA") {
		if (pos == "right") var a = $("#"+id).next().html();
		if (pos == "left") var a = $("#"+id).prev().html();
	} else {
		var a = "no answer selected";
	}	
	addQuestionsResult (q, a, nr); 
}


function addQuestionsResult (q, a, nr) {
	if (q.length > 60) q = q.substr(0, 55) + "... ?"; 

	var i = questions_result.length;
	questions_result[i] = new Object();
	questions_result[i]["question"] = q;
	questions_result[i]["answer"] = a;
	questions_result[i]["nr"] = nr;
}

function track_result(questions, result, form_id) {	
	var q_count = questions.length;
	var nr = 0;
	var q_string = ""; //"Q1: "+ full question + "/" + meaningful answer 1 + ";" +  "Q2: " + full question + "/" + meaningful answer 2 + ";"   + "Q3" ...

	for (i=0;i<q_count;i++) {
		nr = questions[i]["nr"];
		q = questions[i]["question"];
		q = q.replace(/\//g, " - ");
		q = q.replace(/;/g, ",");
		
		a = questions[i]["answer"];
		a = a.replace(/\//g, " - ");
		a = a.replace(/;/g, ",");
		
		if (form_id == "underlyingConditions" && nr == 1) {
			if (a == "1") a = a + " (Excellent)";
			if (a == "7") a = a + " (Poor)";
		}
		
		if (i < q_count-1) q_string += "Q"+nr+": "+ q + "/" + a + ";"; 
		else q_string += "Q"+nr+": "+ q + "/" + a ;
	}
	
	
	clearWtIds();
	addWtId (form_id);
	addWtId (form_id+"Result");

//debugTracking ("WT."+form_id+","+q_string+", WT."+form_id+"Result,"+result);	

	dcsMultiTrack("WT."+form_id,encodeURIComponent(q_string), "WT."+form_id+"Result",encodeURIComponent(result)); 

}

var episodes = new Object();
episodes["episode1"] = "Episode1: Was it the tight trousers?";
episodes["episode2"] = "Episode2: Just think of something sexy";
episodes["episode3"] = "Episode3: Dont you find me attractive anymore?";
episodes["episode4"] = "Episode4: It never made me go blind";
episodes["episode5"] = "Episode5: The guys could be hiding something";
episodes["episode6"] = "Episode6: Why is picking up the phone so hard";
episodes["episode7"] = "Episode7: What a difference a doctor makes";
episodes["episode8"] = "Episode8: From droop to Don Juan";


function trackFlashToWebtrends (module, action, value1, value2) {
	var debug = false;
	var paramName = "";
	var paramValue = "";
	var paramName2 = "";
	var paramValue2 = "";
	var paramName3 = "";
	var paramValue3 = "";
	
	if (debug) debugTracking(module+", "+action+", "+value1+", "+value2);
	
	switch (module) {
	  case "Home":
			paramValue = value1;
			if (typeof(episodes[value1]) != 'undefined') paramValue = episodes[value1];
			if (action == "start") {
				paramName = "WT.cusAnimation";
				paramName2 = "WT.cusAnimationStatus";
				paramValue2 = "started";
			}
			if (action == "stop") {
				paramName = "WT.cusAnimation";
				paramName2 = "WT.cusAnimationStatus";
				paramValue2 = "completed";
			}
		break;
		
	  case "HowCommonIsEd":
			if (action == "save") {
				paramName = "WT.causesofErectile";
				paramValue = value1;
			}
		break;
		
	  case "BodyMap":
			if (action == "show" && value1 != "") {
				// "WT.bodyMap" value e.g. "HighBloodPressure"
                // param2  "WT.bodyMiniFlash" value e.g. "2"
				paramName = "WT.bodyMap";
				paramValue = value1;
				paramName2 = "WT.bodyMiniFlash";
				paramValue2 = value2;
			}
		break;
		
	  case "EdPrevented":
			if (action == "save") {
				paramName = "WT.ErectileCured";
				paramValue = value1;
			}
		break;
		
	  case "AskDrEdwards":
			paramName = "WT.cusVideoCategory";
			paramValue = $(document).getUrlParam("WT.cusCategory");
			
			var vid = $(document).getUrlParam("WT.cusQuestion");
			if (action == "start") {
				paramName2 = "WT.cusVideo";
				paramValue2 = vid;
				paramName3 = "WT.cusVideoStatus";
				paramValue3 = "started";
			}
			if (action == "stop") {
				paramName2 = "WT.cusVideo";
				paramValue2 = vid;
				paramName3 = "WT.cusVideoStatus";
				paramValue3 = "completed";
			}
		break;
		
	  default:
		if (debug) debugTracking("Please check module");
		return false;
		break;
	}
	
	clearWtIds();
	
	if (paramName == "" && paramValue == "") {
		if (debug) debugTracking ("Please check values");
		return false;
	} else {
		if (paramName != "" && paramName2 == "" && paramName3 == "") {
			addWtId (paramName);
			dcsMultiTrack(paramName, encodeURIComponent(paramValue));
			if (debug) debugTracking(paramName+", "+paramValue);
		}
		
		if (paramName != "" && paramName2 != "" && paramName3 == "") {
			addWtId (paramName);
			addWtId (paramName2);
			dcsMultiTrack(paramName, encodeURIComponent(paramValue), paramName2, encodeURIComponent(paramValue2));
			if (debug) debugTracking(paramName+", "+paramValue+", "+paramName2+", "+paramValue2);
		}
		
		if (paramName != "" && paramName2 != "" && paramName3 != "") {
			addWtId (paramName);
			addWtId (paramName2);
			addWtId (paramName3);
			dcsMultiTrack(paramName, encodeURIComponent(paramValue), paramName2, encodeURIComponent(paramValue2), paramName3, encodeURIComponent(paramValue3));
			if (debug) debugTracking(paramName+", "+paramValue+", "+paramName2+", "+paramValue2+", "+paramName3+", "+paramValue3);
		}
	}

}

function trackWebtrendsPrint(param,value) {
	trackWebtrends(param,value,"WT.cusPrintOutResult", $("#result_short").val() );
}

function trackWebtrends(param,value,param2,value2) {
	var debug = false;
	
	clearWtIds();
	
	if (debug) debugTracking(param+", "+value+", "+param2+", "+value2);
	
	if (typeof(param2) == 'undefined') {
		addWtId (param);
		dcsMultiTrack(param, encodeURIComponent(value));
	} else {
		addWtId (param);
		addWtId (param2);
		dcsMultiTrack(param, encodeURIComponent(value), param2, encodeURIComponent(value2));
	}	
}

function debugTracking(msg) {
	$("#tracking").css("diplay","block");
	var c = $("#tracking").html() + "<br />" + msg;
	$("#tracking").html(c);
}



var wt_Ids = new Array();

/*
*	Webtrends: save the individual used keys
*/
function addWtId (key) {
	var pos = key.indexOf("WT.");
	if (pos == 0) key = key.substr(3, (key.length-3)); 
	
	wt_Ids[wt_Ids.length] = key;
}

/*
*	Webtrends: clear the individual used keys
*/
function clearWtIds() {
	for (i=0;i<wt_Ids.length;i++) {
		delete _tag['WT'][wt_Ids[i]];
	}
	wt_Ids = new Array();
}




