var QTEXT = 0;
var QTEXTAREA = 1;
var QRADIO = 2;
var QCHECKBOX = 3;

var AREG = 0;
var AOTHER = 1;


function loadSurvey(surveyId) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', "loadSurvey.aspx?id=" + surveyId, true);
//    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
			var surveyText = self.xmlHttpReq.responseText;
			if (surveyText == "expired") {
				document.getElementById('surveyDiv').innerHTML = "Thank you for your assistance in our research, however, this survey has already expired.";
			}
			else if (surveyText == "") {
				document.getElementById('surveyDiv').innerHTML = "Thank you for your assistance in our research, however, the survey you are looking for could not be found.";
			}
			else
				createSurveyFromXML(surveyText);
        }
    }
    self.xmlHttpReq.send(null);
}

function createSurveyFromXML(xmlString) {
	var matches;
	var r;
//	r = /<answer([\S\s]*?)>([\S\s]*?)<\/answer>/gi;

	r = makeR("title");
	matches = r.exec(xmlString);
	surveyObj.title = matches[2];
	delete r;
	
	r = makeR("startdate");
	matches = r.exec(xmlString);
	surveyObj.startDate = matches[2];
	delete r;
	
	r = makeR("enddate");
	matches = r.exec(xmlString);
	surveyObj.endDate = matches[2];
	delete r;
	
	r = makeR("welcome");
	matches = r.exec(xmlString);
	surveyObj.welcomemessage = matches[2];
	delete r;
	
	r = makeR("thankyou");
	matches = r.exec(xmlString);
	surveyObj.thankyoumessage = matches[2];
	delete r;
	
	r = makeR("qperpage");
	matches = r.exec(xmlString);
	surveyObj.qperpage = matches[2];
	delete r;
	
	r = makeR("question");
	while (matches = r.exec(xmlString)) {
		var que = new question();
		
		var attR = /(\S+?)="(\S+?)"/g;
		var attrs;
		while (attrs = attR.exec(matches[1])) {
			if (attrs[1] == "type")
				que.qtype = attrs[2];
			if (attrs[1] == "id")
				que.id = attrs[2];
			if (attrs[1] == "size")
				que.size = attrs[2];
		}
		
		var exR = /required/g;
		if (exR.test(matches[1]))
			que.required = true;
		
		var r2 = makeR("q");
		var matches2;
		matches2 = r2.exec(matches[2]);
		que.q = matches2[2];
		r2 = makeR("mask");
		matches2 = r2.exec(matches[2]);
		if (matches2) {
			que.mask = matches2[2];
		}
		r2 = makeR("errormessage");
		matches2 = r2.exec(matches[2]);
		if (matches2)
			que.errormessage = matches2[2];
		r2 = makeR("condition");
		matches2 = r2.exec(matches[2]);
		if (matches2)
			que.condition = matches2[2];
		r2 = makeR("answer");
		while (matches2 = r2.exec(matches[2])) {
			var ans = new answer();
			
			var exR = /exclusive/;
			if (exR.test(matches2[1])) {
				ans.exclusive = true;
			}

			var r3 = makeR("a");
			var matches3;
			matches3 = r3.exec(matches2[2]);
			ans.a = matches3[2];
			r3 = makeR("value");
			matches3 = r3.exec(matches2[2]);
			ans.value = matches3[2];
			if (ans.value == "")
				ans.atype = AOTHER;
			r3 = makeR("skipto");
			matches3 = r3.exec(matches2[2]);
			if (matches3)
				ans.skipTo = matches3[2];
			que.addAnswer(ans);
		}
		surveyObj.addQuestion(que);
	}
	delete r;
	
	surveyObj.writeSurvey();
}

function makeR(word) {
	return new RegExp("<"+word+"([\\S\\s]*?)>([\\S\\s]*?)<\\/"+word+">", "gi");
}
/*
function myElement(name) {
	this.tagName = name;
	this.text = "";
	this.firstAttribute = null;
	this.firstChild = null;
	this.nextSibling = null;
	
	this.parseXML = parseXML;
	this.appendChild = appendChild;
	this.interpretElement = interpretElement;
}
*/
/*
function parseXML(xmlString) {
	with (this) {
	while (xmlString.length > 0) {
		var nextTagStart = xmlString.indexOf("<");
		if (nextTagStart == 0) {
			var tagNode;
			var innerStart = xmlString.indexOf(">") + 1;
			var fullTag = xmlString.substring(0, innerStart);
			var tn;
			if (fullTag.indexOf(" ") >= 0)
				tn = fullTag.substring(1, fullTag.indexOf(" "));
			else
				tn = fullTag.substring(1, fullTag.indexOf(">"));
			tagNode = new myElement(tn);
			
			appendChild(tagNode);
			var innerEnd = xmlString.indexOf("</"+tn, innerStart);
			var outerEnd = xmlString.indexOf(">", innerEnd) + 1;
			tagNode.parseXML(xmlString.substring(innerStart, innerEnd));
			xmlString = xmlString.substring(outerEnd);
		}
		else {
			var textNode;
			textNode = new myElement("");
			
			var endOfElement = xmlString.indexOf("<");
			if (endOfElement < 0) {
				textNode.text = xmlString;
				appendChild(textNode);
				return;
			}
			
			textNode.text = xmlString.substring(0, endOfElement);
			appendChild(textNode);
			xmlString = xmlString.substring(endOfElement);
		}
	}
	}
}
*/
function appendChild(node) {
	with (this) {
		if (firstChild == null)
			firstChild = node;
		else {
			var kid = firstChild;
			while (kid.nextSibling != null)
				kid = kid.nextSibling;
			kid.nextSibling = node;
		}
	}
}
/*
function interpretElement(myObj) {
	with (this) {
		if (tagName != "") {
			alert(tagName);
			switch(tagName) {
				case "title":		if (myObj.title && firstChild)
										myObj.title = firstChild.text;
									break;
				case "welcome":		if (myObj.welcomemessage && firstChild)
										myObj.welcomemessage = firstChild.text;
									break;
				case "thankyou":	if (myObj.thankyoumessage && firstChild)
										myObj.thankyoumessage = firstChild.text;
									break;
				case "startdate":	if (myObj.startDate && firstChild)
										myObj.startDate = firstChild.text;
									break;
				case "enddate":		if (myObj.endDate && firstChild)
										myObj.endDate = firstChild.text;
									break;
				case "qperpage":	if (myObj.qperpage && firstChild)
										myObj.qperpage = firstChild.text;
									break;
				case "question":	var que = new question();
									myObj.addQuestion(que);
									myObj = que;
									break;
				case "q":			if (myObj.q && firstChild)
										myObj.q = firstChild.text;
									break;
				case "answer":		var ans = new answer();
									myObj.addAnswer(ans);
									myObj = ans;
									break;
				case "a":			if (myObj.a && firstChild)
										myObj.a = firstChild.text;
									break;
				case "value":		if (myObj.value && firstChild)
										myObj.value = firstChild.text;
									break;
				case "mask":		if (myObj.mask && firstChild)
										myObj.mask = firstChild.text;
									break;
			}
		}
		
		var node = firstChild;
		while (node != null) {
			node.interpretElement(myObj);
			node = node.nextSibling;
		}
	}
}
*/
function exclusiveCheck(id, part) {
	var contDiv = document.getElementById('qdiv'+id);
	var inputs = contDiv.getElementsByTagName("INPUT");
	for (i=0; i<inputs.length; i++) {
		if (inputs[i].name != "q"+id+"_"+part)
			inputs[i].checked = false;
	}
}

function exclusiveUnCheck(id, part) {
	var contDiv = document.getElementById('qdiv'+id);
	var inputs = contDiv.getElementsByTagName("INPUT");
	var que = surveyObj.getQuestionById(id);
	var ans = que.firstAnswer;
	for (i=0; i<inputs.length; i++) {
		if (ans == null)
			break;
		if (inputs[i].name != "q"+id+"_"+part && ans.exclusive)
			inputs[i].checked = false;
		ans = ans.nextAnswer;
	}
}

function getRadio(id, anum) {
	var contDiv = document.getElementById('qdiv'+id);
	var inputs = contDiv.getElementsByTagName("INPUT");
	if (anum-1 > 0 && anum-1 < inputs.length)
		return inputs[anum-1];
	return null;
}
