
//set up an object that just contains responses for the check test

var chkTitles = new function() {
	this.i2 = "you make decisions based on your experience and you know your own mind.";
	this.i1 = "you like to make decisions that are based on your experience but may consider the views of others.";
	this.e1 = "you will listen to others and consider their views before making a decision.";
	this.e2 = "you carefully consider the views of others and use them in your decision making.";
	this.d2 = "you are more engaged by facts and information rather than ideas.";
	this.d1 = "you tend to be engaged by facts although occasionally an idea catches your imagination.";
	this.b1 = "you tend to be excited by ideas, although occasionally you are engaged by facts.";
	this.b2 = "you are excited by ideas and possibilities rather than facts.";
	this.t2 = "you solve problems in a rational, matter of fact, logical way.";
	this.t1 = "you solve problems in a matter of fact way, and may consider the feelings of others.";
	this.f1 = "you solve problems by taking into account the feelings of others and may use a matter of fact approach.";
	this.f2 = "you solve problems in an empathetic way connecting with the feelings of those involved.";
	this.p2 = "you like life to be planned and structured and prefer to avoid unexpected surprises.";
	this.p1 = "you prefer life to be planned and orderly but are ok with events that happen 'out of the blue'.";
	this.m1 = "you quite enjoy life to be spontaneous but like to have a degree of order and structure.";
	this.m2 = "you take life 'as it comes' and prefer the spontaneity rather than it being predictable and planned.";
}

var chkResults = new function() {
 this.e = "No - I prefer to use the views of others";
 this.i = "No - I prefer to use my own experience";
 this.d = "No - I prefer facts";
 this.b = "No - I prefer ideas";
 this.t = "No - I prefer to use more facts and logic";
 this.f = "No - I prefer to use more feelings and empathy";
 this.p = "No - I prefer life to be more planned";
 this.m = "No - I prefer life to be more spontaneous";
}

var showTest = new function() {
	this.opts;
	this.ajax = new Ajax();
	this.tests = new Array();
	this.title;
	this.res;
	this.curNum = -1;
	this.ie;
	this.db;
	this.tf;
	this.pm;
	this.curTest;
	
	this.addTest = function(typ, test, phrases, value) {
	 var num = showTest.tests.length;
	 showTest.tests[num] = new testInvite(typ, test, phrases, value);
 	 return num;
	}

    this.addOption = function(id, value, num, img, family, size, weight) {
		showTest.tests[num].options[showTest.tests[num].options.length] = new opt(id, value, img, family, size, weight);
	}
	
	this.setUp = function() {
	 showTest.curNum++;
	 if (showTest.curNum >= showTest.tests.length) {
		showTest.getRep();
	 } else {
	 
		var dv = document.getElementById(_loadContainerId);
		while (dv.hasChildNodes()) {
			dv.removeChild(dv.firstChild);
		}
		showTest.curTest = showTest.tests[showTest.curNum].test;
       showTest.dispOpts(showTest.curNum);
     }
	}
	
	this.dispOpts = function(num) {
	 showTest.res = new Array();
	 var tst = showTest.tests[num];
	 var typ = tst.typ;
	 var dv=document.getElementById(_loadContainerId);
	 var p=document.createElement("p");
	 p.setAttribute("id", "personaliteyetitle");
	 p.innerHTML = showTest.tests[showTest.curNum].phrases[0];
	 dv.appendChild(p);
	 var tab,tbod,tr, cent;
	 cent=document.createElement("center");
	 tab=document.createElement("table");
	 tab.style.maxWidth="92%";
	 tab.setAttribute("id", "personaliteyetable");
	 tbod=document.createElement("tbody");
	 tr=document.createElement("tr");
	 tab.appendChild(tbod);
	 tbod.appendChild(tr); 
	 if (typ=="image") showTest.imgOpts(tst, tr);
	 else if (typ=="word") showTest.wordOpts(tst, tr);
	 else if (typ=="radio") showTest.radOpts(tst, tr);
	 else if (typ=="check") showTest.checkOpts(tst, tr);
	 cent.appendChild(tab);
	 dv.appendChild(cent);
	 dv=tab=tbod=tr=p=cent=null;
	}
	
	this.imgOpts = function(tst, tr) {
	 var td, img;
	  for (var i=0; i<tst.options.length; i++) {
	   td=document.createElement("td");
		img = document.createElement("img");
		img.setAttribute("id", tst.options[i].id);
		img.setAttribute("src", tst.options[i].value);
		img.onclick=showTest.results;
		img.style.cursor="pointer";
		td.appendChild(img);
		tr.appendChild(td);
	  }
	   img=td=tr=null;
	}
	
	this.wordOpts = function(tst, tr) {
		var td, txt, vals, br;
		for (var i=0; i<tst.options.length; i++) {
			td=document.createElement("td");
			td.setAttribute("id", tst.options[i].id);
			vals = tst.options[i].value.split(" ");
			for (var x=0; x<vals.length; x++) {
			 if (x>0) {
				br=document.createElement("br");
				td.appendChild(br);
			  }
			 txt = document.createTextNode(vals[x]);
			 td.appendChild(txt);
			}
			td.style.backgroundImage = "url('/apps/coachingsuccess/" + tst.options[i].img + "')";
			td.style.backgroundRepeat = "no-repeat";
			td.onclick=showTest.results;
			td.style.backgroundColor="#FFFFFF";
			td.style.width="100px";
			td.style.height="100px";
			td.style.border="0px solid #000000";
			td.style.fontFamily=tst.options[i].fontfamily;
			td.style.fontSize=tst.options[i].fontsize;
			td.style.fontWeight=tst.options[i].fontweight;
			td.style.textDecoration="none";
			td.style.textAlign="center";
			td.style.cursor="pointer";
			tr.appendChild(td);
		}
		td=txt=tr=br=null;
	}
	
	
	this.radOpts = function(tst, tr) {
		var htm, td;
		td=document.createElement("td");
		td.style.textAlign="left";
		td.style.fontFamily="arial";
		htm = (tst.test=="gender") ? "Are you:<br>" : "<img src='" + tst.value + "' alt='Relationship Graphic'><br>";
		for (var i=0; i<tst.options.length; i++) {
			htm += "<br><input type='radio' id='" + tst.options[i].id + "' name='radiotest'> " + tst.options[i].value + " ";
		}
		td.innerHTML = htm;
		img = document.createElement("img");
		img.style.marginTop = "20px";
		img.style.marginLeft = "50px";
		img.setAttribute("src", "/apps/coachingsuccess/select.png");
		img.setAttribute("alt", "click here to select an option");
		img.style.cursor="pointer";
		img.onclick=showTest.results;
		img.style.display="block";
		td.appendChild(img);
		tr.appendChild(td);
		img=htm=td=null;		
	}
	
	this.checkOpts = function(tst, tr) {
		var htm, td;
		td=document.createElement("td");
		var vl;
		var curtst = tst.test;
		if (curtst=="iecheck") vl = this.ie.toLowerCase();
		else if (curtst=="dbcheck") vl=this.db.toLowerCase();
		else if (curtst=="tfcheck") vl=this.tf.toLowerCase();
		else if (curtst=="pmcheck") vl=this.pm.toLowerCase();
		htm = "<font style='font-family:arial; font-size:14px'>Your responses suggest that " + chkTitles[vl] + "</font>";
		htm += "<div style='position:relative; margin-left:auto; margin-right:auto; width:60%; font-family:Arial; font-size:14px; border:0px solid #000000'><input type='radio' id='agree/ok' name='radiotest' checked='true' value='1'> Yes, that describes me</p>";
		var cvl = new Array();
		var cvf = new Array();
		if (vl=="i2") {
		 cvl[0] = chkResults.e;
		 cvf[0] = "i1";
		} else if (vl=="t2") {
		  cvl[0] = chkResults.f;
		  cvf[0] = "t1";
		} else if (vl=="p2") {
		  cvl[0] = chkResults.m;
		  cvf[0] = "p1";
		} else if (vl=="d2") {
		  cvl[0] = chkResults.b;
		  cvf[0] ="d1";
		} else if (vl=="e2") {
		  cvl[0] = chkResults.i;
		  cvf[0] = "e1";
		} else if (vl=="f2") {
		  cvl[0] = chkResults.t;
		  cvf[0] = "f1";
		} else if (vl=="b2") {
		  cvl[0] = chkResults.b;
		  cvf[0] = "b1";
		} else if (vl=="m2") {
		  cvl[0] = chkResults.p;
		  cvf[0] = "m1";
		} else if (vl=="t1" || vl=="f1") {
			cvl[0] = chkResults.t;
			cvf[0] = (vl=="t1") ? "t2" : "t1";
			cvl[1] = chkResults.f;
			cvf[1] = (vl=="t1") ? "f1" : "f2";
		} else if (vl == "i1" || vl == "e1") {
			cvl[0] = chkResults.i;
			cvf[0] = (vl=="i1") ? "i2" : "i1";
			cvl[1] = chkResults.e;
			cvf[0] = (vl=="i1") ? "e1" : "e2";
		} else if (vl=="p1" || vl == "m1") {
			cvl[0] = chkResults.p;
			cvf[0] = (vl=="p1") ? "p2" : "p1";
			cvl[1] = chkResults.m;
			cvf[1] = (vl=="p1") ? "m1" : "m2";
		} else if (vl=="d1" || vl == "b1") {
			cvl[0] = chkResults.d;
			cvf[0] = (vl=="d1") ? "d2" : "d1";
			cvl[1] = chkResults.b;
			cvf[1] = (vl=="d1") ? "b1" : "b2";
		}
		for (var i=0; i<cvl.length; i++) {
			htm += "<p><font style='font-family:arial; font-size:14px'><input type='radio' id='" + curtst + "/" + cvf[i] + "' name='radiotest'> " + cvl[i] + "</font></p>";
		}
		htm += "</div>"
		td.innerHTML = htm;
		img = document.createElement("img");
		img.setAttribute("src", "/apps/coachingsuccess/select.png");
		img.setAttribute("alt", "click here to select an option");
		img.style.cursor="pointer";
		img.onclick=showTest.results;
		img.style.display="block";
		img.style.marginLeft="auto";
		img.style.marginRight="auto";
		td.appendChild(img);
		tr.appendChild(td);
		img=htm=td=null;		
	}
	
	this.results = function() {
		
		this.parentNode.removeChild(this);
		var typ = showTest.tests[showTest.curNum].typ;
		if (typ=="radio" || typ=="check") {
		 var tgs = document.getElementById("personaliteyetable").getElementsByTagName("input");
		 for (var i=0; i<tgs.length; i++) {
			if (tgs[i].type=="radio") {
				if (tgs[i].checked) {
				 var id = tgs[i].id.split("/");
					showTest.res[0] = id[1];
					break;
				}
			}
		}
		tgs=null;
		  showTest.endTest();
		} else {
		 var id = this.id.split("/");
		 showTest.res[showTest.res.length] = id[1];
		  if (typ=="image") {
		    var dv = document.getElementById("personaliteyetable").getElementsByTagName("img");
		  } else {
			var dv=document.getElementById("personaliteyetable").getElementsByTagName("td");
		  }
		  if (dv.length < 2) {
		    if (typ=="word") {
		      id = dv[0].id.split("/");
				showTest.res[showTest.res.length] = id[1];
			}
			showTest.endTest();
		  } else {
			showTest.changeTitle(dv.length);
		  }
		  dv=null;
		}
	}
	
	this.changeTitle = function(num) {
	  var phrasenum = 4 - num;
	  if (showTest.tests[showTest.curNum].phrases.length == 1) {
		document.getElementById("personaliteyetitle").innerHTML = showTest.tests[showTest.curNum].phrases[0];
	  } else {
		document.getElementById("personaliteyetitle").innerHTML = showTest.tests[showTest.curNum].phrases[phrasenum];
	  }
	}	
	
	this.endTest = function() {
		//build up data 
		var htm = "test=" + showTest.curTest;
		for (var i=0; i<showTest.res.length; i++) {
			htm += "&res" + i + "=" + showTest.res[i];  
		}
		showTest.ajax.doGet("/apps/coachingsuccess/testfunction.php?" + htm, showTest.ret);
	}
	
	this.ret = function(str) {
	 var test = showTest.tests[showTest.curNum].test;
	  if (test == "ieword") showTest.ie = str;
	  else if (test == "dbword") showTest.db=str;
	  else if (test=="tfword") showTest.tf=str;
	  else if (test=="pmword") showTest.pm=str;
		showTest.setUp();
	}
	
	this.getRep = function() {
	  window.location.href="/apps/coachingsuccess/pdfreport.php?id=1";
	}
}


function testInvite(typ, test, phrases, value) {
	this.typ = typ; //image or word
	this.options = new Array();
	this.phrases = phrases;	
	this.value=value;
	this.test=test;
}

function opt(id, value, img, fontfamily, fontsize, fontweight) {
	this.id = id;
	this.value = value;
	this.img=img;
	this.fontfamily = fontfamily;
	this.fontsize=fontsize;
	this.fontweight = fontweight;
}

//now the rest of the page is designed to set the test order, this easily enables us to add in more test pages later on
var fullTest = new function() {
	this.ajax = new Ajax();
	this.ajax.method="POST";
	this.tests = new Array();
	
	this.dataCheck= function() {
	 var err=false;
      var errMsg = "";
     var obj,errobj, form;
     var formObj = document.getElementById(_loadContainerId).getElementsByTagName("input");
     for (var i =0; i<formObj.length; i++) {
	  obj = formObj[i];
	  errobj=document.getElementById(formObj[i].id + "error");
	  if (errobj!=null) errobj.style.visibility="hidden";
		if (formObj[i].type=="text") {
			if (obj.value=="") {
			  err=true;
		     if (errobj != null) {
				errobj.style.visibility="visible";
				errMsg += errobj.firstChild.innerHTML + "\n";
			  }
			} 
		} else if (formObj[i].type=="checkbox") {
			if (!obj.checked) {
				err=true;
				if (errobj != null) {
				  errobj.style.visibility="visible";
				  errMsg += errobj.firstChild.innerHTML + "\n";
				}
			}
		} else if (formObj[i].type=="radio") {
			
		}
      }
      tgs=null;
		if (errMsg != "") alert(errMsg);
	if (!err) return true;
	else return false;
	}
	this.getData = function(e) {
	  if (!e) var e = window.event;
	  e.cancelBubble=true;
	  if (e.stopPropogation) e.stopPropogation();
	
		//first build all the data from the box
		_loadContainerId = this.parentNode.parentNode.id;
		if (!fullTest.dataCheck()) return false;
		var app = document.getElementById(_loadContainerId);
		var tgs = app.getElementsByTagName("input");
		var htm = "start=on";
		for (var i=0; i<tgs.length; i++) {
		 if (tgs[i].type=="submit") continue;
		 if (tgs[i].type=="checkbox") {
			if (tgs[i].checked) htm += "&" + tgs[i].name + "=true";
		 } else {
				htm += "&" + tgs[i].name + "=" + tgs[i].value;
		 }
		}
		tgs = app.getElementsByTagName("select");
		for (var i=0; i<tgs.length; i++) {
				htm += "&" + tgs[i].name + "=" + tgs[i].value;
		}
		//now encode any data and send to the server
		fullTest.ajax.postData = htm;
	  fullTest.ajax.doGet("/apps/coachingsuccess/testfunction.php", fullTest.disp);	
	  fullTest.ajax.postData=null;
	  return false;
	}
	
	this.disp = function(str) {
		//now set up all the basic test information here
		if (str=="1") {
			showTest.setUp();
		} else {
			alert("You must complete all required fields");
		}
	}
	
	this.onsubmit = function() {
		return false;
	}
}


//where the type is a word test, in the add option function
//add the following arguments - fontFamily, fontSize, fontWeight
function addTests() {
 var num = showTest.addTest("image", "db", new Array("Please choose a picture", "Please choose another picture", "Please choose a final picture"));
	showTest.addOption("db/d2", "/apps/coachingsuccess/d2.gif", num);
	showTest.addOption("db/d1", "/apps/coachingsuccess/d1.gif", num);
	showTest.addOption("db/b1", "/apps/coachingsuccess/b1.gif", num);
	showTest.addOption("db/b2", "/apps/coachingsuccess/b2.gif", num);
	
	num = showTest.addTest("image", "dbword", new Array("Please choose a word that best describes you", "Please choose a word that best describes you", "Please choose a word that best describes you"));
	showTest.addOption("db/d2", "/apps/coachingsuccess/analytical.png", num);
	showTest.addOption("db/d1", "/apps/coachingsuccess/realistic.png", num);
	showTest.addOption("db/b1", "/apps/coachingsuccess/curious.png", num);
	showTest.addOption("db/b2", "/apps/coachingsuccess/insightful.png", num);
	
	num = showTest.addTest("image", "ie", new Array("Please choose a sunlounger", "Please choose another sunlounger", "Please choose a final sunlounger"));
	showTest.addOption("ie/i2", "/apps/coachingsuccess/i2.gif", num);
	showTest.addOption("ie/i1", "/apps/coachingsuccess/i1.gif", num);
	showTest.addOption("ie/e1", "/apps/coachingsuccess/e1.gif", num);
	showTest.addOption("ie/e2", "/apps/coachingsuccess/e2.gif", num);

num = showTest.addTest("image", "ieword", new Array("Please choose a word that best describes you", "Please choose a word that best describes you", "Please choose a word that best describes you"));
	showTest.addOption("ie/i2", "/apps/coachingsuccess/independent.png", num);
	showTest.addOption("ie/i1", "/apps/coachingsuccess/diplomatic.png", num);
	showTest.addOption("ie/e1", "/apps/coachingsuccess/friendly.png", num);
	showTest.addOption("ie/e2", "/apps/coachingsuccess/sociable.png", num);

	num = showTest.addTest("image", "tf", new Array("Please choose a texture", "Please choose another texture", "Please choose a final texture"));
	showTest.addOption("tf/t2", "/apps/coachingsuccess/t2.gif", num);
	showTest.addOption("tf/t1", "/apps/coachingsuccess/t1.gif", num);
	showTest.addOption("tf/f1", "/apps/coachingsuccess/f1.gif", num);
	showTest.addOption("tf/f2", "/apps/coachingsuccess/f2.gif", num);
	
	num = showTest.addTest("image", "tfword", new Array("Please choose a word that best describes you", "Please choose a word that best describes you", "Please choose a word that best describes you"));
	showTest.addOption("tf/t2", "/apps/coachingsuccess/decisive.png", num);
	showTest.addOption("tf/t1", "/apps/coachingsuccess/resourceful.png", num);
	showTest.addOption("tf/f1", "/apps/coachingsuccess/sensitive.png", num);
	showTest.addOption("tf/f2", "/apps/coachingsuccess/caring.png", num);
	
	num = showTest.addTest("image", "pm", new Array("Please choose a graphic", "Please choose another graphic", "Please choose a final graphic"));
	showTest.addOption("pm/p2", "/apps/coachingsuccess/p2.gif", num);
	showTest.addOption("pm/p1", "/apps/coachingsuccess/p1.gif", num);
	showTest.addOption("pm/m1", "/apps/coachingsuccess/m1.gif", num);
	showTest.addOption("pm/m2", "/apps/coachingsuccess/m2.gif", num);
	
	num = showTest.addTest("image", "pmword", new Array("Please choose a word that best describes you", "Please choose a word that best describes you", "Please choose a word that best describes you"));
	showTest.addOption("pm/p2", "/apps/coachingsuccess/dependable.png", num);
	showTest.addOption("pm/p1", "/apps/coachingsuccess/reliable.png", num);
	showTest.addOption("pm/m1", "/apps/coachingsuccess/easygoing.png", num);
	showTest.addOption("pm/m2", "/apps/coachingsuccess/imaginative.png", num);
	
	
	num = showTest.addTest("radio", "loyalty", new Array("Please select a relationship"), "/apps/coachingsuccess/relationshipgraphic.gif");
	showTest.addOption("loyalty/l1", "They are all the same", num);
	showTest.addOption("loyalty/l2", "Two are the same and one is different", num);
	showTest.addOption("loyalty/l3", "They are the same and different", num);
	showTest.addOption("loyalty/l4", "One is different two are the same", num);
	showTest.addOption("loyalty/l5", "They are all different", num);
	
	num = showTest.addTest("word", "km11", new Array("What is most important to you in a relationship?"));
	showTest.addOption("km11/km1", "The connection", num, "tbg5.gif", "verdana", "13px", "bold");
	showTest.addOption("km11/km2", "Feeling Good", num, "tbg5.gif", "verdana", "13px", "bold");
	showTest.addOption("km11/km3", "Security", num, "tbg5.gif", "verdana", "13px", "bold");
	showTest.addOption("km11/km4", "Variety", num, "tbg5.gif", "verdana", "13px", "bold");
	showTest.addOption("km11/km5", "Personal Growth", num, "tbg5.gif", "verdana", "13px", "bold");
	showTest.addOption("km11/km6", "Giving", num, "tbg5.gif", "verdana", "13px", "bold");
	
	num = showTest.addTest("word", "km12", new Array("What is most important to you in a relationship?"));
	showTest.addOption("km12/km1", "Companionship", num, "tbg6.gif", "trebuchet", "14px", "bold");
	showTest.addOption("km12/km2", "Happiness", num, "tbg6.gif", "trebuchet", "14px", "bold");
	showTest.addOption("km12/km3", "Commitment", num, "tbg6.gif", "trebuchet", "14px", "bold");
	showTest.addOption("km12/km4", "Excitement", num, "tbg6.gif", "trebuchet", "14px", "bold");
	showTest.addOption("km12/km5", "Fulfilment", num, "tbg6.gif", "trebuchet", "14px", "bold");
	showTest.addOption("km12/km6", "Sharing", num, "tbg6.gif", "trebuchet", "14px", "bold");
	
	num = showTest.addTest("word", "km21", new Array("What is most important to you at work?"));
	showTest.addOption("km21/km1", "Connecting with others", num, "tbg7.gif", "arial", "13px", "bold");
	showTest.addOption("km21/km2", "Status", num, "tbg7.gif", "arial", "13px", "bold");
	showTest.addOption("km21/km3", "Security", num, "tbg7.gif", "arial", "13px", "bold");
	showTest.addOption("km21/km4", "Variety", num, "tbg7.gif", "arial", "13px", "bold");
	showTest.addOption("km21/km5", "Personal Growth", num, "tbg7.gif", "arial", "13px", "bold");
	showTest.addOption("km21/km6", "Giving", num, "tbg7.gif", "arial", "13px", "bold");
	
	num = showTest.addTest("word", "km22", new Array("What is most important to you at work?"));
	showTest.addOption("km22/km1", "Teamwork", num, "tbg8.gif", "georgia", "13px", "bold");
	showTest.addOption("km22/km2", "Reward", num, "tbg8.gif", "georgia", "13px", "bold");
	showTest.addOption("km22/km3", "Stability", num, "tbg8.gif", "georgia", "13px", "bold");
	showTest.addOption("km22/km4", "Challenges", num, "tbg8.gif", "georgia", "13px", "bold");
	showTest.addOption("km22/km5", "Development", num, "tbg8.gif", "georgia", "13px", "bold");
	showTest.addOption("km22/km6", "Making a difference", num, "tbg8.gif", "georgia", "13px", "bold");

	num = showTest.addTest("check", "dbcheck", new Array(""));
	showTest.addOption("dbcheck/d2", "d2", num);
	showTest.addOption("dbcheck/d1", "d1", num);
	showTest.addOption("dbcheck/b1", "b1", num);
	showTest.addOption("dbcheck/b2", "b2", num);
	
	num = showTest.addTest("check", "iecheck", new Array(""));
	showTest.addOption("iecheck/i2", "i2", num);
	showTest.addOption("iecheck/i1", "i1", num);
	showTest.addOption("iecheck/e1", "e1", num);
	showTest.addOption("iecheck/e2", "e2", num);
	
	num = showTest.addTest("check", "tfcheck", new Array(""));
	showTest.addOption("tfcheck/t2", "t2", num);
	showTest.addOption("tfcheck/t1", "t1", num);
	showTest.addOption("tfcheck/f1", "f1", num);
	showTest.addOption("tfcheck/f2", "f2", num);
	
	num = showTest.addTest("check", "pmcheck", new Array(""));
	showTest.addOption("pmcheck/p2", "p2", num);
	showTest.addOption("pmcheck/p1", "p1", num);
	showTest.addOption("pmcheck/m1", "m1", num);
	showTest.addOption("pmcheck/m2", "m2", num);
	
	num = showTest.addTest("radio", "gender", new Array(""));
	showTest.addOption("gender/m", "male", num);
	showTest.addOption("gender/f", "female", num);
}

_loadContainerId="";
window.onload = function() {
  //first get the container
    addTests();
}