// *********************************************************************************************************
// *********************************************************************************************************
// Version 1.3
// Updated 3/3/2008
// This script supports following extra attributes
//		lqsingleselect
//			Example: <INPUT TYPE="CheckBox" NAME="Q624" lqsingleselect VALUE="31">
//			Description: makes check box act as radio button. When this check box is selected - all other 
//			check boxes are cleared for the question. When some other check box is selected - this checkbox
//			is cleared
//		lqunselect, lqclear
//			Example: <INPUT TYPE="CheckBox" NAME="Q624" unselect VALUE="31">
//			Description: makes check box initially unselected when page is loaded.
//		lqanswerconnect:
//			Example: 
//				<INPUT TYPE="CheckBox" NAME="Q536" VALUE="13">
//				<TEXTAREA NAME="Q536:O" lqanswerconnect="13"></TEXTAREA>
//			Description: when text is entered into textbox - the answer alternative is selected
// *********************************************************************************************************

addOnloadEvent(initQuestions);
window.onerror = stoperror ;

var lqForm;

// *********************************************************************************************************
// LqForm class
// *********************************************************************************************************
function LqForm(oElement) {
	var oThis = this;
	this.htmlElement = oElement;
	this.oldsubmit = null;
	this.questions = new Array();
	
	if (typeof HTMLFormElement == 'undefined' && !typeof jQuery != 'undefined') {
		this.oldsubmit = this.htmlElement.submit;
		this.htmlElement.submit = function () {
			if(oThis.submitQuestions()) oThis.oldsubmit();
		};
	}

	var oInputs = this.htmlElement.getElementsByTagName("INPUT");
	if (oInputs) {
		if (oInputs.length!=null) {
			for (var i=0; i<oInputs.length; i++) this.addInput(oInputs[i]);
		}
		else this.addInput(oInputs);
	}
	oInputs = this.htmlElement.getElementsByTagName("TEXTAREA");
	if (oInputs) {
		if (oInputs.length!=null) {
			for (var i=0; i<oInputs.length; i++) this.addInput(oInputs[i]);
		}
		else this.addInput(oInputs);
	}
	oInputs = this.htmlElement.getElementsByTagName("SELECT");
	if (oInputs) {
		if (oInputs.length!=null) {
			for (var i=0; i<oInputs.length; i++) this.addInput(oInputs[i]);
		}
		else this.addInput(oInputs);
	}

	var oLinks = this.htmlElement.getElementsByTagName("A");
	if (oLinks) {
		if (oLinks.length)
			for (var i=0; i<oLinks.length; i++) this.addSetValueLink(oLinks[i]);
		else this.addSetValueLink(oLinks);
	}
	
	try {
		pageInit();
	}
	catch(e) {
		var noinit = true;
	}
}
LqForm.prototype.addInput = function (oInput) {
	if (!((oInput.type=="checkbox" || oInput.type=="radio" || oInput.type=="text" || oInput.type=="textarea" || oInput.type=="select-one") && oInput.name)) return false;
	var r = oInput.name.match(/^Q(\d+(\.\d+)?)(:.+)?$/);
	if (r == null || r.length <= 0) return false;
	
	var qi;
	var QID = "Q" + r[1];
	for (qi=0; qi<this.questions.length && this.questions[qi].id!=QID; qi++);
	if (qi==this.questions.length) this.questions[qi] = new LqQuestion(QID);
	switch (oInput.type) {
		case "select-one":
			this.questions[qi].addSelect(oInput);
			break;
		default:
			this.questions[qi].addAnswer(oInput);
	}
	return true;
}
LqForm.prototype.addSetValueLink = function (e) {
	if (e.name) {
		var r = e.name.match(/^setvalueQ(\d+(\.\d+)?):(\d+)$/);
		if (r && r.length > 3) {
			var QID = "Q" + r[1];
			var vi = Number(r[3]);
			if (vi>0)
				for (var qi=0; qi<this.questions.length; qi++)
					if (this.questions[qi].id==QID) {
						this.questions[qi].addSetValueLink(e, vi);
						break;
					}
		}
	}
}
LqForm.prototype.submitQuestions = function () {
	if (document.lqform.UNDO && document.lqform.UNDO.value != "") {
	  window.onbeforeunload = null;
    return true;
  }
	var ret = true;
	for (var qi=0; qi<this.questions.length; qi++) if (!this.questions[qi].onsubmit()) ret = false;
	if(ret) window.onbeforeunload = null;
	return ret;
}

// *********************************************************************************************************
// LqQuestion class
// *********************************************************************************************************
function LqQuestion(id) {
	this.id = id;
	this.answers = new Array();
	this.selects = new Array();
	this.unconnectedanswers = new Array();
}
LqQuestion.prototype.addAnswer = function (e) {
	this.answers[this.answers.length] = new LqAnswer(e, this, null);
}
LqQuestion.prototype.addSelect = function (e) {
	this.selects[this.selects.length] = new LqSelect(e, this);
}

LqQuestion.prototype.addSetValueLink = function (e, setvalue) {
	for (var ai=0; ai<this.answers.length; ai++)
		if ((this.answers[ai].htmlElement.type == "checkbox" || this.answers[ai].htmlElement.type == "radio") && this.answers[ai].htmlElement.value == setvalue) {
			this.answers[ai].addSetValueLink(e);
			break;
		}
}
LqQuestion.prototype.onsubmit = function () {
	for (var ai=0; ai<this.answers.length; ai++) 
		if (this.answers[ai].answerconnect && this.answers[ai].htmlElement.value != '') {
			this.answers[ai].answerconnect.setSelected(true);
			//this.answers[ai].answerconnect.htmlElement.checked = true;
			//this.answers[ai].answerconnect.onclick();
		}
	return true;
}

// *********************************************************************************************************
// LqSelect class
// *********************************************************************************************************
function LqSelect(oElement, Question) {
	this.htmlElement = oElement;
	this.question = Question;
	this.options = new Array();

	var oThis = this;
	this._onchange = function (e) {
		if (e == null) e = oThis.htmlElement.ownerDocument.parentWindow.event;
		return oThis.onchange(e);
	};
	addEvent( this.htmlElement, "change", this._onchange ) ;
	
	var oOptions = this.htmlElement.getElementsByTagName("OPTION");
	if (oOptions) {
		if (oOptions.length!=null) {
			for (var i=0; i<oOptions.length; i++) this.addOption(oOptions[i]);
		}
		else
			this.addOption(oOptions);
	}
	
}
LqSelect.prototype.addOption = function (e) {
	var a = new LqAnswer(e, this.question, this);
	this.question.answers[this.question.answers.length] = a;
	this.options[this.options.length] = a;
}
LqSelect.prototype.onchange = function (e) {
	for (var oi=0; oi<this.options.length; oi++)
		this.options[oi].updateSetValueLink();
	for (var oi=0; oi<this.options.length; oi++)
		if (this.options[oi].htmlElement.selected)
			this.options[oi].verifySingleSelect();
	return true;
}
LqSelect.prototype.value = function () {
	if (this.htmlElement.selectedIndex>=0)
		return this.htmlElement.options[this.htmlElement.selectedIndex].value;
	return "";
}
LqSelect.prototype.isSelected = function (answer) {
	if (answer.htmlElement.selected) return true;
	return false
}
LqSelect.prototype.setSelected = function (answer, value) {
	if (this.isSelected(answer) == value) return;
	answer.htmlElement.selected = value;
	this.onchange();
}

// *********************************************************************************************************
// LqAnswer class
// *********************************************************************************************************
function LqAnswer(oElement, Question, Select) {
	this.htmlElement = oElement;
	this.question = Question;
	this.select = Select;
	this.singleselect = null;
	this.answerconnect = null;
	this.answernoconnect = null;
	this.connectedto = new Array();
	this.setValueLink = null;
	this.numlen = NaN;
	this.numdec = NaN;
	
	var oThis = this;
	this._onclick = function (e) {
		if (e == null) e = oThis.htmlElement.ownerDocument.parentWindow.event;
		if (e!=null) {
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
		}
		return oThis.onclick(e);
	};
	this._onchange = function (e) {
		if (e == null) e = oThis.htmlElement.ownerDocument.parentWindow.event;
		return oThis.onchange(e);
	};
	this._onkeydown = function (e) {
		if (e == null) e = oThis.htmlElement.ownerDocument.parentWindow.event;
		return oThis.onkeydown(e);
	};
	
	if (this.select!=null) {
		this.singleselect = this.htmlElement.getAttribute("lqsingleselect");
		if (this.htmlElement.getAttribute("lqunselect") != null && this.htmlElement.getAttribute("lqunselect") !== "false" || this.htmlElement.getAttribute("lqclear") != null ) this.setSelected(false);
		
		for (var ai=0; ai<this.question.unconnectedanswers.length; ai++)
			if (this.htmlElement.value!="" && this.htmlElement.value==this.question.unconnectedanswers[ai].answernoconnect) {
				this.setConnectionTo(this.question.unconnectedanswers[ai]);				
        this.question.unconnectedanswers[ai].answerconnect = this;
				this.question.unconnectedanswers.splice(ai,1);
			}
	}
	else switch (this.htmlElement.type) {
		case "checkbox":
		case "radio":
			this.singleselect = this.htmlElement.getAttribute("lqsingleselect");
			if(this.singleselect || this.htmlElement.type == "radio") this.htmlElement.exclusive = true;
			if (this.htmlElement.getAttribute("lqunselect") != null && this.htmlElement.getAttribute("lqunselect") !== "false" || this.htmlElement.getAttribute("lqclear") != null ) this.setSelected(false);
			addEvent( this.htmlElement, "click", this._onclick ) ;
			
			for (var ai=0; ai<this.question.unconnectedanswers.length; ai++)
				if (this.htmlElement.value==this.question.unconnectedanswers[ai].answernoconnect) {
          this.question.unconnectedanswers[ai].answerconnect = this;
					this.question.unconnectedanswers.splice(ai,1);	
				}
			break;

		case "text":
		case "textarea":
			this.singleselect = this.htmlElement.getAttribute("lqsingleselect");
			try {
				this.answernoconnect = Number(this.htmlElement.getAttribute("lqanswerconnect"));
				if (this.answernoconnect<=0) this.answernoconnect = null;
			}
			catch (e) {
				this.answernoconnect = null;
			}
			if (this.answernoconnect) {
				for (var ai=0; ai<this.question.answers.length && !this.answerconnect; ai++) {
					if (this!=this.question.answers[ai] && (this.question.answers[ai].htmlElement.type=="checkbox" || this.question.answers[ai].htmlElement.type=="radio") && this.question.answers[ai].htmlElement.value==this.answernoconnect) {
						  this.answerconnect = this.question.answers[ai];
						  this.question.answers[ai].setConnectionTo(this);	
						}
				}
				if (!this.answerconnect) this.question.unconnectedanswers[this.question.unconnectedanswers.length] = this;
			}
			if (this.htmlElement.getAttribute("lqclear") != null ) this.htmlElement.value = "";
			this.htmlElement.value = this.htmlElement.value.replace( /\<br\\?\>/gi, "\n");
			addEvent( this.htmlElement, "change", this._onchange ) ;
			
			if (this.htmlElement.className) {
				var rex = new RegExp("numeric(\\d+)\\.?(\\d*)", "g");
				var format_array = rex.exec(this.htmlElement.className);
				if (format_array != null) {
					this.numlen = Number(format_array[1]);
					if (format_array.length > 2) this.numdec = Number(format_array[2]);
				}
				if (!isNaN(this.numlen)) {
					if (isNaN(this.numdec)) this.numdec = 0;
					addEvent( this.htmlElement, "keydown", this._onkeydown ) ;
				}
			}
			break;
	}
}

LqAnswer.prototype.onclick = function () {
	var el = this.htmlElement;
	this.updateSetValueLink();
	this.updateConnection();
	if(!el.checked) return true;
	this.verifySingleSelect();
	return true;
}
LqAnswer.prototype.onchange = function (e) {
	if (this.htmlElement.value == '') return true;
	if (!isNaN(this.numlen)) {
		var elValue = this.htmlElement.value.replace(/,/g, ".").replace(/\s+/g,'');
		var intLength = this.numlen > 0 ? "{0," + this.numlen + "}" : "+"; 
		var rexNum = new RegExp("^\\d"+intLength+"(\\.\\d*){0,1}$");
		if (!rexNum.test(elValue)) {
			this.htmlElement.value = "";
			return true;
		}
		var value = Number(elValue);
		if (isNaN(value)) {
			this.htmlElement.value = "";
			return true;
		}
		this.htmlElement.value = "" + Number(value.toFixed(this.numdec));
	}

	if (this.htmlElement.value == '') return true;
	if (this.answerconnect) {
		this.answerconnect.setSelected(true);
		//this.answerconnect.htmlElement.checked = true;
		//this.answerconnect.onclick();
	}
	else
		this.verifySingleSelect();
	return true;
}
LqAnswer.prototype.onkeydown = function (e) {
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	//code contains the pressed key in all browsers
	
	if (e.ctrlKey && code==86) {
		// Control + V to paste
		return true;
	}
	switch (code) {
		case 8:		//backspace
		case 9:		//tab
		case 20:	//caps lock
		case 27:	//escape
		case 33:	//page up
		case 34:	//page down
		case 35:	//end
		case 36:	//home
		case 37:	//left arrow
		case 38:	//up arrow
		case 39:	//right arrow
		case 40:	//down arrow
		case 45:	//insert
		case 46:	//delete
		case 112:	//F1
		case 113:	//F2
		case 114:	//F3
		case 115:	//F4
		case 116:	//F5
		case 117:	//F6
		case 118:	//F7
		case 119:	//F8
		case 120:	//F9
		case 121:	//F10
		case 122:	//F11
		case 123:	//F12
		case 144:	//Num Lock
		case 145:	// scroll lock
			break;
		default:
			if (!isNaN(this.numlen)) {
				var isDecPoint = (code==188 || code==190 || code==110);
				if (isDecPoint || (code>=48 && code<=57) || (code>=96 && code<=105)) {
					var value = this.htmlElement.value;
					var maxlen = this.numlen;
					//console.log(value);
					if (this.numdec>0) maxlen += this.numdec + 1;
					
					if (isDecPoint) {
						if (this.numdec<=0 || value.indexOf('.') >= 0 || value.indexOf(',') >= 0) return stopEvent(e);
					}
					else if (value.indexOf('.')<=0 && value.indexOf(',')<=0 && value.length>=this.numlen && this.numlen > 0) {
						return stopEvent(e);
					}
					else if (value.length>=maxlen && this.numlen > 0) {
						return stopEvent(e);
					}
				}
				else stopEvent(e);
			}
			break;
	}
}
LqAnswer.prototype.verifySingleSelect = function () {
	for (var i=0; i<this.question.answers.length; i++) {
		if (this.question.answers[i] != this) {
			if (this.question.answers[i].select!=null) {
				if (this.question.answers[i].isSelected() && 
					!(this.singleselect == null && this.question.answers[i].singleselect == null || 
					this.singleselect != "" && this.singleselect == this.question.answers[i].singleselect ))
						this.question.answers[i].setSelected(false);

				if (!this.question.answers[i].isSelected()) {
					for (var j=0; j<this.question.answers.length; j++)
						if (this.question.answers[j].answerconnect == this.question.answers[i]) {
							 this.question.answers[j].htmlElement.value = "";
							 this.question.answers[j].answerconnect.updateConnection();
						}
				}
			}
			else switch (this.question.answers[i].htmlElement.type) {
				case "radio":
				case "checkbox":
					if (this.question.answers[i].htmlElement.checked && 
						!(this.singleselect == null && this.question.answers[i].singleselect == null || 
						this.singleselect != "" && this.singleselect == this.question.answers[i].singleselect )) {
							   this.question.answers[i].htmlElement.checked = false;
							   this.question.answers[i].updateConnection();
							}

					if (!this.question.answers[i].htmlElement.checked) {
						for (var j=0; j<this.question.answers.length; j++)
							if (this.question.answers[j].answerconnect == this.question.answers[i])
								this.question.answers[j].htmlElement.value = "";
					}
					break;
				case "text":
					if (!this.question.answers[i].answerconnect && this.question.answers[i].htmlElement.value != "" && 
						!(this.singleselect == null && this.question.answers[i].singleselect == null || 
						this.singleselect != "" && this.singleselect == this.question.answers[i].singleselect ))
							this.question.answers[i].htmlElement.value = ""; 
					break;
			}
			this.question.answers[i].updateSetValueLink();
		}
				else if(this.connectedto.length > 0) {
            this.updateConnection();
        }
	}
}


LqAnswer.prototype.addSetValueLink = function (e) {
	this.setValueLink = new LqSetValueLink(e, this);
	this.setValueLink.setSelected(this.isSelected());
}
LqAnswer.prototype.isSelected = function () {
	if (this.select!=null) {
		return this.select.isSelected(this);
	}
	else switch (this.htmlElement.type) {
		case "radio":
		case "checkbox":
			return this.htmlElement.checked;
		case "text":
			return this.htmlElement.value != "";
	}
	return false;
}
LqAnswer.prototype.setSelected = function (value) {
	if (this.select!=null)
		this.select.setSelected(this, value);
	else switch (this.htmlElement.type) {
		case "radio":
		case "checkbox":
			if (this.htmlElement.checked == value) return;
			this.htmlElement.checked = value;
			this.onclick();
	}
}
LqAnswer.prototype.updateSetValueLink = function () {
	if (this.setValueLink != null) this.setValueLink.setSelected(this.isSelected());
}

LqAnswer.prototype.setConnectionTo = function (answer) { 
  this.connectedto.push(answer);
  this.updateConnection();	
}

LqAnswer.prototype.updateConnection = function () { 
	if(this.connectedto.length > 0) {
    if(this.isSelected()) {
      for(var x=0; x<this.connectedto.length; x++) {
        this.connectedto[x].htmlElement.disabled = false;
        Utilities.removeClassName(this.connectedto[x].htmlElement, "passiveConnection");
        Utilities.addClassName(this.connectedto[x].htmlElement, "activeConnection");
      }
    }
    else {
      for(x=0; x<this.connectedto.length; x++) {
        this.connectedto[x].htmlElement.disabled = true;
        Utilities.removeClassName(this.connectedto[x].htmlElement, "activeConnection");
        Utilities.addClassName(this.connectedto[x].htmlElement, "passiveConnection");
      }
    }
  }
}

// *********************************************************************************************************
// LqSetValueLink class
// *********************************************************************************************************
function LqSetValueLink(oElement, Answer) {
	this.htmlElement = oElement;
	this.answer = Answer;
	this.isSelected = false;
	this.htmlImage = null;
	this.imageSource = new Array();
	
	var oImages = this.htmlElement.getElementsByTagName("IMG");
	if (oImages) {
		if (oImages.length!=null) {
			if (oImages.length>0) this.htmlImage = oImages[0];
		}
		else
			this.htmlImage = oImages; 
	}
	if (this.htmlImage) {
		this.imageSource[0] = this.htmlImage.src;
		this.imageSource[1] = this.imageSource[0];
		this.imageSource[2] = this.imageSource[0];
		var r = this.imageSource[0].lastIndexOf("0");
		if (r >= 0) {
			this.imageSource[1] = this.imageSource[0].substr(0,r) + "1" + this.imageSource[0].substr(r+1);
			this.imageSource[2] = this.imageSource[0].substr(0,r) + "2" + this.imageSource[0].substr(r+1);
		}
	}
	
	var oThis = this;
	this._onmouseover = function (e) {
		if (e == null) e = oThis.htmlElement.ownerDocument.parentWindow.event;
		return oThis.mouseover(e);
	};
	this._onmouseout = function (e) {
		if (e == null) e = oThis.htmlElement.ownerDocument.parentWindow.event;
		return oThis.mouseout(e);
	};
	this._onclick = function (e) {
		if (e == null) e = oThis.htmlElement.ownerDocument.parentWindow.event;
		if (e!=null) {
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
		}
		return oThis.onclick(e);
	};
	if( this.htmlElement ) {
		addEvent( this.htmlElement, "mouseover", this._onmouseover ) ;
		addEvent( this.htmlElement, "mouseout", this._onmouseout ) ;
		addEvent( this.htmlElement, "click", this._onclick ) ;
	}
}
LqSetValueLink.prototype.setSelected = function (value) {
	this.isSelected = value;
	if (this.htmlImage) {
		if (this.isSelected)
			this.htmlImage.src = this.imageSource[1];
		else
			this.htmlImage.src = this.imageSource[0];
	}
}
LqSetValueLink.prototype.mouseover = function (e) {
	if (this.htmlImage) this.htmlImage.src = this.imageSource[1];
	return true;
}
LqSetValueLink.prototype.mouseout = function (e) {
	if (this.htmlImage) {
		if (this.isSelected)
			this.htmlImage.src = this.imageSource[1];
		else
			this.htmlImage.src = this.imageSource[0];
	}
	return true;
}
LqSetValueLink.prototype.onclick = function () {
  this.answer.setSelected(!this.isSelected);
	//this.answer.htmlElement.checked = !this.answer.htmlElement.checked;
	//this.answer.onclick();
	return false;
}

// *********************************************************************************************************
// ProgressBar 
// *********************************************************************************************************
function updateProgressBar() {
	var section = -1;
	var step = -1;
	var aValue=getElementsById(document, "SPAN", "LqQuestionSection");
	for (var v=1; v<3 && aValue.length>0; v++) {
		var svalue = document.all ? aValue[0].innerText : aValue[0].textContent;
		if (svalue.trim() == "") break;
		var nvalue=-1;
		try {nvalue = Number(svalue);} catch(e) {nvalue = -1;}
		if (nvalue<0) break;
		if (v==1) section=nvalue;
		else step=nvalue;
		aValue=getElementsById(document, "SPAN", "LqQuestionPosition");
	}
	var aProgressBar = getElementsById(document, new Array("TABLE", "DIV"), "ProgressBar");
	var stepTags = new Array("A", "INPUT");
	for (var p=0; p<aProgressBar.length; p++) {
		var currentStep=false;
		for (var s=1, oSection = getElementById(aProgressBar[p], new Array("TABLE", "DIV"), "progsection_" + s); oSection; oSection = getElementById(aProgressBar[p], new Array("TABLE", "DIV"), "progsection_" + (++s))) {
			for (var i=1, oStep=getElementById(oSection, stepTags, "progstep_" + i); oStep; oStep=getElementById(oSection, stepTags, "progstep_" + (++i))) {
				if (!(s==section && i==step)) {
          if(oStep.tagName != "INPUT") addEvent( oStep, "click", DSGoTo ) ;
          currentStep = false;
        }
				else currentStep = true;
				if (s<section || s==section && i<=step)
					for (var pn=oStep.parentNode; pn; pn=pn.parentNode) {
						if (pn.tagName && (pn.tagName=="TD" || pn.tagName=="SPAN" )) {
							if (pn.className != "" && pn.className.right(10) != "_highlight") pn.className = pn.className + "_highlight";
							if(currentStep && pn.className != "") pn.className = "currentStep " + pn.className;
							break;
						}
					}
			}
		}
	}
}
function DSGoTo(e) {
	if (e == null) e = document.parentWindow.event;
  var targ;
  if (!e) var e = window.event;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3) // defeat Safari bug
  targ = targ.parentNode;
	var qref = targ.href ? targ.href : "";
	var r = qref.lastIndexOf("#Q");
	if (r >= 0) {
		qref = qref.substr(r + 1);
		//alert( qref );
		
		var gotoElement = document.createElement("input");

		var attribute = document.createAttribute("type");
		attribute.nodeValue = "hidden";
		gotoElement.setAttributeNode(attribute);

		attribute = document.createAttribute("name");
		attribute.nodeValue = "GOTO" + qref;
		gotoElement.setAttributeNode(attribute);

		attribute = document.createAttribute("value");
		attribute.nodeValue = "GOTO";
		gotoElement.setAttributeNode(attribute);

		document.lqform.appendChild(gotoElement);
		//alert( gotoElement.name + "=" + gotoElement.value );
		
		document.lqform.submit();
	}
	//alert( "DSGoTo" );
	return false;
}


// *********************************************************************************************************
// General functions
// *********************************************************************************************************

function newsubmit(event) {
	var target = event ? event.target : this;
	// do anything you like here
	//alert('Submitting form to ' + target.action);
	// call real submit function
	if (lqForm && target == lqForm.htmlElement && !lqForm.submitQuestions()) return;
	this._submit();
}
if(typeof jQuery != 'undefined') {
	$(document).ready(function() {
		$("form").submit(function() {
			if (lqForm && this == lqForm.htmlElement && !lqForm.submitQuestions()) return false;
			else return true;
		});
	});
}
else if (typeof HTMLFormElement != 'undefined') {
	HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
	HTMLFormElement.prototype.submit = newsubmit;
}


function initQuestions() {
	lqForm = new LqForm(document.lqform);
	updateProgressBar();
}

function nextPage() {
	var pageIsValid = true;
	try {
		pageIsValid = pageValidate();
	}
	catch(e) {
		pageIsValid = true;
	}
	if (pageIsValid) {
		if(jQuery) $('form').submit();
		else document.lqform.submit();
	}
	return false;
}

function backPage() {
	var undoElement;
	try {
		undoElement = document.lqform.UNDO;
	}
	catch(e) {
		undoElement = null;
	}
	if (undoElement) {
		undoElement.value="UNDO";
	}
	else {
		var attribute;
		undoElement = document.createElement("input");

		attribute = document.createAttribute("type");
		attribute.nodeValue = "hidden";
		undoElement.setAttributeNode(attribute);


		attribute = document.createAttribute("name");
		attribute.nodeValue = "UNDO";
		undoElement.setAttributeNode(attribute);

		attribute = document.createAttribute("value");
		attribute.nodeValue = "UNDO";
		undoElement.setAttributeNode(attribute);

		document.lqform.appendChild(undoElement);
	}

	document.lqform.submit();
}

function setVariableValue(name, value) {
	var variableElement;
	try {
		var elements = document.getElementsByName( name );
		if (elements.length != null && elements.length > 0)
			variableElement = elements[0];
		else
			variableElement = null;
	}
	catch(e) {
		variableElement = null;
	}
	if (variableElement) {
		variableElement.value = value;
	}
	else {
		var attribute;
		variableElement = document.createElement("input");

		attribute = document.createAttribute("type");
		attribute.nodeValue = "hidden";
		variableElement.setAttributeNode(attribute);


		attribute = document.createAttribute("name");
		attribute.nodeValue = name;
		variableElement.setAttributeNode(attribute);

		attribute = document.createAttribute("value");
		attribute.nodeValue = value;
		variableElement.setAttributeNode(attribute);

		document.lqform.appendChild(variableElement);
	}
}


function stoperror() {
	return true ;
}

function addOnloadEvent(fnc){
	if ( typeof window.addEventListener != "undefined" )
		window.addEventListener( "load", fnc, false );
	else if ( typeof window.attachEvent != "undefined" ) {
		window.attachEvent( "onload", fnc );
	}
	else {
		if ( window.onload != null ) {
			var oldOnload = window.onload;
			window.onload = function ( e ) {
				oldOnload( e );
				window[fnc]();
			};
		}
		else 
			window.onload = fnc;
	}
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	}
}

function stopEvent(e) {
	if (e.preventDefault) {
		e.preventDefault();
		e.stopPropagation();
	} else {
		e.returnValue = false;
		e.cancelBubble = true;
	}
	return true;
}

/* Dispatch a click event into the document tree */
function dispatchClick(elm) { 
	//return;
	var evt = null; 
	if(document.createEvent) { 
		evt = document.createEvent('MouseEvents'); 
		if(elm && elm.dispatchEvent && evt && evt.initMouseEvent) { 
			evt.initMouseEvent( 
			'click', 
			true,     // Click events bubble 
			true,     // and they can be cancelled 
			document.defaultView,  // Use the default view 
			1,        // Just a single click 
			0,        // Don't bother with co-ordinates 
			0, 
			0, 
			0, 
			false,    // Don't apply any key modifiers 
			false, 
			false, 
			false, 
			0,        // 0 - left, 1 - middle, 2 - right 
			null);    // Click events don't have any targets other than 
			            // the recipient of the click 
			elm.dispatchEvent(evt); 
		} 
	} else if( document.createEventObject ) {
		elm.fireEvent('onclick');
	}
}


function getElementsById(parent, tags, ids) {
	var ret = new Array();
	if (typeof(tags) == "string") tags = new Array(tags)
	if (typeof(ids) == "string") ids = new Array(ids)
	
	if (parent.all) {
		for (var i=0; i<ids.length; i++) {
			var o = parent.all(ids[i]); //Changed from parent.all.item(ids[i]) which doesn't work in IE8
			if (o) {
				if (o.length) {
					for (var j=0; j<o.length; j++) ret[ret.length] = o(j);
				}
				else {
					ret[ret.length] = o;
				}
			}
		}
	}
	else {
		for (var t=0; t<tags.length; t++) {
			var o = parent.getElementsByTagName(tags[t]);
			if (o) {
				if (o.length) {
					for (var j=0; j<o.length; j++) {
						for (var i=0; i<ids.length; i++) {
							if (o[j].id && o[j].id == ids[i]) {
								ret[ret.length] = o[j];
								break;
							}
						}
					}
				}
				else {
					for (var i=0; i<ids.length; i++) {
						if (o.id && o.id == ids[i]) {
							ret[ret.length] = o;
							break;
						}
					}
				}
			}
		}
	}
	return ret;
}

function getElementById(parent, tags, ids) {
	if (typeof(tags) == "string") tags = new Array(tags)
	if (typeof(ids) == "string") ids = new Array(ids)
	
	if (parent.all) {
		for (var i=0; i<ids.length; i++) {
			var o = parent.all(ids[i]);
			if (o) {
				if (o.length!=null) {
					for (var j=0; j<o.length; j++)
						for (var t=0; t<tags.length; t++)
							if (o[j].tagName==tags[t]) return o[j];
				}
				else {
					for (var t=0; t<tags.length; t++)
						if (o.tagName==tags[t]) return o;
				}
			}
		}
	}
	else {
		for (var t=0; t<tags.length; t++) {
			var o = parent.getElementsByTagName(tags[t]);
			if (o) {
				if (o.length) {
					for (var j=0; j<o.length; j++) {
						for (var i=0; i<ids.length; i++) {
							if (o[j].id == ids[i]) return o[j];
						}
					}
				}
				else {
					for (var i=0; i<ids.length; i++) {
						if (o.id == ids[i]) return o;
					}
				}
			}
		}
	}
	return null;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.right = function(n){
    if (n <= 0) return "";
    if (n > this.length) return this.substr(0);
	return this.substr(this.length - n);
}

