var jQueryScriptOutputted = false;

initJQuery();


if(ops_scriptLoaded){
	alert("survey.js file is already included in the page. Please remove the other references to survey.js file.");
}

// global variable. gets value true when this script is loaded.
var ops_scriptLoaded = true;
var ops_alreadySubmited = false;


function initJQuery() {
    
    //if the jQuery object isn't available
    if (typeof(jQuery) == 'undefined') {
    
    
        if (! jQueryScriptOutputted) {
            //only output the script once..
            jQueryScriptOutputted = true;
	    addScript("include/jquery/js/jquery-1.4.2.min.js");            
        }
        setTimeout("initJQuery()", 50);
    } else {
                        
		// JQuery ready function, executed at HTML-ready
		$(function() {
			// for each question
			$(".questionInput").each(function() {
				///////////////////////////////////////////////
				// Find matrix questions, and rotate cell 
				// groups (those that are marked for rotation)
				///////////////////////////////////////////////
				
				// create a clone of the matrix question
				var $matrix = $(".matrix", this);
				if ($matrix.size() == 0) {
					return;
				}
				var $clonedMatrix = $matrix.clone(true);
				
				// go through the groups in the matrix question until no groups left
				var g = 0;
				while (true) {
					// get row lists of the original and cloned matrices
					var $groupTrList = $("tr:has(.group" + g + ")", this);
					
					if ($groupTrList.size() == 0) {
						break;
					}
					// if rotation is off for the group - go to the next one
					if ($groupTrList.eq(0).children().filter(".rotationOn").size() == 0) {
						g++;
						continue;
					}
					
					// get list of the group tr's for the clone
					var $clonedGroupTrList = $("tr:has(.group" + g +")", $clonedMatrix);
			
					// create an array with random indices
					var rowSize = $groupTrList.size();
					var indices = new Array();
					var randomIndices = new Array();
					for (i=0;i<rowSize;i++) { indices[indices.length] = i; }
					randomIndices = indices.sort(function() {return Math.round(Math.random())-0.5});
			
					// for each row in the group
					$groupTrList.each(function(i) {
						// get cells in the current group
						var $groupTdList = $(this).children().filter(".group" + g + ".rotationOn");
						if ($groupTdList.size() == 0) {
							return;
						}
						$groupTdList = $groupTdList.add($groupTdList.eq(0).prev());
						
						// Get the td elements for the row, and filter on rotation-class
						var $clonedGroupTdList = $clonedGroupTrList.eq(randomIndices[i]).children().filter(".group" + g + ".rotationOn"); 
						
						// add the label column to the cloned list
						$clonedGroupTdList = $clonedGroupTdList.add($clonedGroupTdList.eq(0).prev());
			
						// copy the cells from the clone to their new position
						$groupTdList.each(function(j) {
							$(this).replaceWith($clonedGroupTdList.eq(j));
						});
					});
					g++;
				}
			});
		});
    }
}

function addScript(URL){
    var scriptArray = document.getElementsByTagName('script');
    for (i = 0; i < scriptArray.length;i++){
        var URLIndex = scriptArray[i].src.indexOf(URL);
        if (0 <= URLIndex){
            return;
        }
    }
    var headID = document.getElementsByTagName("head")[0];
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = URL;
    headID.appendChild(newScript);
}


/**
 * Check if current page has already been submitted.
 * Do not allow respondent to click on the same buttons multiple times, this result in double submissions
 * and might result in PK violation problem if database connection is slow.
 *
 * (used for surveys only, not polls!)
 */
function allowSubmit(){
	if (ops_alreadySubmited){
		alert(document.surveyForm.doubleSubmitMessage.value);
		return false;
	}
	ops_alreadySubmited = true;
	return true;
}


/**
 * Submit survey form (used for surveys only, not polls!)
 */
function submitForm(){
	if(allowSubmit()){
		document.surveyForm.submit();
	}
}


// Open popup with poll results
function openPollResultPopup(resultUrl){
	return window.open(resultUrl, 'pollResultPopup', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,resizable=1,width=350,height=340,top=400,left=400');
}


//Text required, numeric required
function textRequiredChk(field) {
	with (field){
		if (value == null || value == "") {
			return false;
		}
		return true;
	}
}

//Text min length
function textMinChk(field, min) {
	//If the field is empty - return
	if(!textRequiredChk(field)){
		return true;
	}
	if ( field.value.length < min ){
		return false;
	}
	return true;
}

//Text max length
function textMaxChk(field, max) {
	//If the field is empty - return
	if(!textRequiredChk(field)){
		return true;
	}
	if ( max !='' && field.value.length > max ){
		return false;
	}
	return true;
}

//Text email
function textEmailChk(field) {
	//If the field is empty - return
	if(!textRequiredChk(field)){
		return true;
	}
	if ( field.value.length < 5 || field.value.indexOf("@") < 1 || field.value.indexOf(".") < 1) {
		return false;
	}
	return true;
}


//Text date
function textDateChk(field, pattern) {
	if(!textRequiredChk(field)){
		return true;
	}
	with (field) {
		var ok = buildDate(field.value, pattern);
		if (!ok) {
			return false;
		}
	}
	return true;
}


//Text regular expression ( + contains)
//For more infor about regExp: http://www.webreference.com/js/column5/index.html
function textRegExpChk(field, pattern) {
	//If the field is empty - return
	if(!textRequiredChk(field)){
		return true;
	}
	var regexp = new RegExp(pattern);
	var ok = regexp.test(field.value);
	if(!ok){
		return false;
	}
	return true;
}


// Rating required
function radioRequiredChk(fieldName) {
	var optionSelected = -1;
	for (i = 0; i < fieldName.length; i++) {
		if (fieldName[i].checked) {
			optionSelected = fieldName[i].value;
		}
	}
	if (optionSelected < 0) {
		return false;
	}
	return true;
}

//Dropdown required
function dropdownRequiredChk(entered) {
	with (entered) {
		if (value == "") {
			return false;
		}
		return true;
	}
}

//Dropdown min choice
function dropdownMinChk(entered, minSelected) {
	var selectedCounter = 0;
	var length = entered.length;
	for(i = 0; i < length; i++) {
		opt = entered.options[i];
		if(i == 0 && (opt.value == "") ){
		}else if (opt.selected){
			selectedCounter++;
		}
	}
	if (selectedCounter < minSelected){
		return false;
	}
	return true;
}

//Dropdown max choice
function dropdownMaxChk(entered, maxSelected) {
	var selectedCounter = 0;
	var length = entered.length;
	for(i = 0; i < length; i++) {
		opt = entered.options[i];
		if(i == 0 && (opt.value == "") ){
		}else if (opt.selected){
			selectedCounter++;
		}
	}
	if (selectedCounter > maxSelected){
		return false;
	}
	return true;
}

//Numeric required
function numericChk(entered, datatype) {
	//If the field is empty - return
	if(!textRequiredChk(entered)){
		return true;
	}
	with (entered) {
		var checkvalue = parseFloat(value);
		if (datatype.charAt(0)=="i"){
			checkvalue = parseInt(value);
			if (isNaN(checkvalue)){
				return false;
			}
		}
		if(value != checkvalue){
			return false;
		}
	}
	return true;
}

//Numeric minimum value
function numericMinChk(entered, datatype, minValue) {
	//If the field is empty - return
	if(!textRequiredChk(entered)){
		return true;
	}
	with (entered) {
		checkvalue = parseFloat(value);
		min = parseFloat(minValue);
		if (datatype.charAt(0)=="i"){
			checkvalue = parseInt(value);
			if (isNaN(checkvalue)){
				return true;
			}
		}
		if(value != checkvalue){
			return true;
		}
		if (checkvalue < min) {
			return false;
		}
	}
	return true;
}

//Numeric maximum value
function numericMaxChk(entered, datatype, maxValue) {
	//If the field is empty - return
	if(!textRequiredChk(entered)){
		return true;
	}
	with (entered) {
		checkvalue = parseFloat(value);
		max = parseFloat(maxValue);
		if (datatype.charAt(0)=="i"){
			checkvalue = parseInt(value);
			if (isNaN(checkvalue)){
				return true;
			}
		}
		if(value != checkvalue){
			return true;
		}
		if (checkvalue > max) {
			return false;
		}
	}
	return true;
}

//Multiple choice required
function multChoiceRequiredChk(fieldName) {
	var optionSelected = -1;
	for (i = 0; i < fieldName.length; i++) {
		if (fieldName[i].checked){
			optionSelected = fieldName[i].value;
		}
	}
	if (optionSelected == -1) {
		return false;
	}
	return true;
}

//Multiple choice minimum selections
function multChoiceMinChk(form, questionNo, optionCount, minSelected) {
	var selectedCounter = 0;
	for (i = 0; i < form.length; i++) {
		if (form.elements[i].type == "checkbox") {
			for (j = 0; j <= optionCount; j++) {
				if (form.elements[i].name == ("q" + questionNo + "multiple_" + j)) {
					if (form.elements[i].checked == true)
						selectedCounter++
				}
			}
		}
	}
	if (selectedCounter < minSelected){
		return false;
	}
	return true;
}

//Multiple choice maximum selections
function multChoiceMaxChk(form, questionNo, optionCount, maxSelected) {
	var selectedCounter = 0;
	for (i = 0; i < form.length; i++) {
		if (form.elements[i].type == "checkbox") {
			for (j = 0; j <= optionCount; j++) {
				if (form.elements[i].name == ("q" + questionNo + "multiple_" + j)) {
					if (form.elements[i].checked == true)
						selectedCounter++;
				}
			}
		}
	}
	if (selectedCounter > maxSelected){
		return false;
	}
	return true;
}


function matrixGroupRequiredChk(form, fieldName, fra, to, reqSel){
	var log = "";
	var searchPos = 0;
	var name_;
	var selectedCounter = 0;
	for (p = fra; p <= to; p++) {
		name_ = fieldName + "_" + p;
		for (i = searchPos; i < form.length; i++) {
			if (form.elements[i].name == name_){
				searchPos = i;
				if(form.elements[i].checked == true){
					//log = log + "selected " +
					selectedCounter++;
				}
			}
		}
		if(selectedCounter == reqSel){
			return true;
		}
	}
	return false;
}

function debug(msg) {
	$("#op_debug").append(msg).show();
}

function freeTextKeyPressed(freeTextElement) {
	$(freeTextElement).change();
}

function checkFreetextLength(freeTextElement, maxLength) {
	var len = $(freeTextElement).val().length;
	if(maxLength > 0 && maxLength < len) {
		$(freeTextElement).val($(freeTextElement).val().substring(0,maxLength));
		return false;
	}
	return true;
}


function matrixRating(form, fieldName, fromRow, toRow, colCount){
	var name_;
	var checkedCount = 0;
	var i = 0;
	for (var p = fromRow; p <= toRow; p++) {
		checkedCount = 0;
		name_ = fieldName + "_" + p;
		i = getIndex(form, i, name_);
		if(i >= 0){
			for (var col = 0; col < colCount; col++) {
				if(form.elements[i].checked == true){
					checkedCount++;
				}
				i++;
			}
			if(checkedCount == 0){
				return false;
			}
		}
	}
	return true;
}

function checkRankingGroup(fieldElement, clickedNow){
//	alert("clickedNow: " + clickedNow + ", fieldElement.checked: " + fieldElement.checked);
	if (!fieldElement.checked && clickedNow) {
		checkRankingGroups();
		return;
	}
	
	var g = 0;
	var group = null;
	// find the group name where the selected/deselected cell is located
	while (true) {
		// checks if there is a group with such index
		var $groupTrList = $("tr:has(.group" + g + ")", $(fieldElement).parents(".matrix"));
		if ($groupTrList.size() == 0) {
			break;
		}
		if ($(fieldElement).parent().hasClass("group" + g)) {
			// the is the group we need
			group = "group" + g;
		}
		g++;
	}

	if (group != null) {
		// select or deselect the row
		if ($(fieldElement).parent().parent().find("td:." + group).find("input:checked").size() > 0) {
			$(fieldElement).parent().parent().find("td:." + group).find("input:not(:checked)").attr("disabled","disabled");
		} else {
			$(fieldElement).parent().parent().find("td:." + group).find("input").removeAttr("disabled");
		}
		
		// select or deselect the column
		var column = parseInt($(fieldElement).attr("name").split("_")[1]) + 1;
		if ($(fieldElement).parent().parent().parent().find("td:." + group).filter(':nth-child(' + column + ')').find("input:checked").size() > 0) {
			$(fieldElement).parent().parent().parent().find("td:." + group).filter(':nth-child(' + column + ')').find("input:not(:checked)").attr("disabled","disabled");
		} else {
			$(fieldElement).parent().parent().parent().find("td:." + group).filter(':nth-child(' + column + ')').find("input").removeAttr("disabled");
		}
	}
}
	
function findFirstRowCell(form, startName, colPos, from){
	var cellName = startName + "_" + colPos;
	var i = getIndex(form, from, cellName);
	return i;
}

function matrixGroupMinChk(form, fieldName, fromCol, toCol, fromRow, toRow, minSelected) {
	var name_;
	var checkedCount = 0;
	var i = 0;
	for(p = fromRow; p <= toRow; p++) {
		name_ = fieldName + fromCol + "_" + p;
		i = getIndex(form, i, name_);
		if(i >= 0){
			for(col = fromCol; col <= toCol; col++) {
				var element = form.elements[i];
				if(element.type == "checkbox" && element.checked == true){
					checkedCount++;
				}
				i++;
			}
		}
	}
	if(checkedCount < minSelected){
		return false;
	}
	return true;
}

function matrixGroupMaxChk(form, fieldName, fromCol, toCol, fromRow, toRow, maxSelected) {
	var name_;
	var checkedCount = 0;
	var i = 0;
	for(p = fromRow; p <= toRow; p++) {
		name_ = fieldName + fromCol + "_" + p;
		i = getIndex(form, i, name_);
		if(i >= 0){
			for(col = fromCol; col <= toCol; col++) {
				var element = form.elements[i];
				if(element.type == "checkbox" && element.checked == true){
					checkedCount++;
				}
				i++;
			}
		}
	}
	if(checkedCount > maxSelected){
		return false;
	}
	return true;
}


//************************************** Date validation *********************************
// http://javascript.internet.com/forms/validation-universal-date.html (with some changes)
/* Here's the list of tokens we support:
	 m (or M) : month number, one or two digits.
	 mm (or MM) : month number, strictly two digits (i.e. April is 04).
	 d (or D) : day number, one or two digits.
	 dd (or DD) : day number, strictly two digits.
	 y (or Y) : year, two or four digits.
	 yy (or YY) : year, strictly two digits.
	 yyyy (or YYYY) : year, strictly four digits.
	 mon : abbreviated month name (April is apr, Apr, APR, etc.)
	 Mon : abbreviated month name, mixed-case (i.e. April is Apr only).
	 MON : abbreviated month name, all upper-case (i.e. April is APR only).
	 mon_strict : abbreviated month name, all lower-case (i.e. April is apr
		 only).
	 month : full month name (April is april, April, APRIL, etc.)
	 Month : full month name, mixed-case (i.e. April only).
	 MONTH: full month name, all upper-case (i.e. APRIL only).
	 month_strict : full month name, all lower-case (i.e. april only).
	 h (or H) : hour, one or two digits.
	 hh (or HH) : hour, strictly two digits.
	 min (or MIN): minutes, one or two digits.
	 mins (or MINS) : minutes, strictly two digits.
	 s (or S) : seconds, one or two digits.
	 ss (or SS) : seconds, strictly two digits.
	 ampm (or AMPM) : am/pm setting.  Valid values to match this token are
		 am, pm, AM, PM, a.m., p.m., A.M., P.M.
*/
// Be careful with this pattern.  Longer tokens should be placed before shorter
// tokens to disambiguate them.  For example, parsing "mon_strict" should
// result in one token "mon_strict" and not two tokens "mon" and a literal
// "_strict".

var delimPat = /(-|\.|\/|:| )/i;
var tokPat = new RegExp("^month_strict|month|Month|MONTH|yyyy|YYYY|mins|MINS|mon_strict|ampm|AMPM|mon|Mon|MON|min|MIN|dd|DD|mm|MM|yy|YY|hh|HH|ss|SS|m|M|d|D|y|Y|h|H|s|S");

// lowerMonArr is used to map months to their numeric values.

var lowerMonArr={jan:1, feb:2, mar:3, apr:4, may:5, jun:6, jul:7, aug:8, sep:9, oct:10, nov:11, dec:12};

// monPatArr contains regular expressions used for matching abbreviated months
// in a date string.

var monPatArr=new Array();
monPatArr['mon_strict']=new RegExp(/jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec/);
monPatArr['Mon']=new RegExp(/Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/);
monPatArr['MON']=new RegExp(/JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC/);
monPatArr['mon']=new RegExp("jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec",'i');

// monthPatArr contains regular expressions used for matching full months
// in a date string.

var monthPatArr=new Array();
monthPatArr['month']=new RegExp(/^january|february|march|april|may|june|july|august|september|october|november|december/i);
monthPatArr['Month']=new RegExp(/^January|February|March|April|May|June|July|August|September|October|November|December/);
monthPatArr['MONTH']=new RegExp(/^JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER/);
monthPatArr['month_strict']=new RegExp(/^january|february|march|april|may|june|july|august|september|october|november|december/);


// cutoffYear is the cut-off for assigning "19" or "20" as century.  Any
// two-digit year >= cutoffYear will get a century of "19", and everything
// else gets a century of "20".

var cutoffYear=50;

/* buildDate does all the real work.It takes a date string and format string,
 tries to match the two up, and returns a Date object (with the supplied date
 string value).If a date string doesn't contain all the fields that a Date
 object contains (for example, a date string with just the month), all
 unprovided fields are defaulted to those characteristics of the current
 date. Time fields that aren't provided default to 0.Thus, a date string
 like "3/30/2000" in "%mm/%dd/%yyyy" format results in a Date object for that
 date at midnight.formatStr is a free-form string that indicates special
 tokens via the % character.Here are some examples that will return a Date
 object:

 buildDate('3/30/2000','%mm/%dd/%y') // March 30, 2000
 buildDate('March 30, 2000','%Mon %d, %y') // Same as above.
 buildDate('Here is the date: 30-3-00','Here is the date: %dd-%m-%yy')

 If the format string does not match the string provided, an error message
 (i.e. String object) is returned.Thus, to see if buildDate succeeded, the
 caller can use the "typeof" command on the return value.For example,
 here's the dateCheck function, which returns true if a given date is
 valid,and false otherwise (and reports an error in the false case):

 function dateCheck(dateStr, formatStr) {
	var myObj = buildDate(dateStr,formatStr);
	if (typeof myObj=="object") {
		// We got a Date object, so good.
		return true;
	} else {
		// We got an error string.
		alert(myObj);
		return false;
	}
 }

*/
function buildDate(dateStr,formatStr) {
	// parse the format string first.
	var tokArr=parseFormatString(formatStr);
	var strInd=0;
	var tokInd=0;
	var intMonth;
	var intDay;
	var intYear;
	var intHour;
	var intMin;
	var intSec;
	var ampm="";
	var strOffset;

	// Create a date object with the current date so that if the user only
	// gives a month or day string, we can still return a valid date.

	var curdate=new Date();
	intMonth=curdate.getMonth()+1;
	intDay=curdate.getDate();
	intYear=curdate.getFullYear();

	// Default time to midnight, so that if given just date info, we return
	// a Date object for that date at midnight.

	intHour=0;
	intMin=0;
	intSec=0;

	// Walk across dateStr, matching the parsed formatStr until we find a
	// mismatch or succeed.

	while (strInd < dateStr.length && tokInd < tokArr.length) {
	// Start with the easy case of matching a literal.
		if (tokArr[tokInd].type=="literal") {
			if (dateStr.indexOf(tokArr[tokInd].token, strInd) == strInd) {
				// The current position in the string does match the format pattern.
				strInd+=tokArr[tokInd++].token.length;
				continue;
			} else {
				// ACK! There was a mismatch; return error.
				return false;
			}
		}

		// If we get here, we're matching to a symbolic token.
		switch (tokArr[tokInd].token) {
			case 'm':
			case 'M':
			case 'd':
			case 'D':
			case 'h':
			case 'H':
			case 'min':
			case 'MIN':
			case 's':
			case 'S':
				// Extract one or two characters from the date-time string and if
				// it's a number, save it as the month, day, hour, or minute, as
				// appropriate.

				curChar=dateStr.charAt(strInd);
				nextChar=dateStr.charAt(strInd+1);
				matchArr=dateStr.substr(strInd).match(/^\d{1,2}/);
				if (matchArr==null) {
					// First character isn't a number; there's a mismatch between
					// the pattern and date string, so return error.

					switch (tokArr[tokInd].token.toLowerCase()) {
						case 'd': var unit="day";
							break;
						case 'm': var unit="month"; break;
						case 'h': var unit="hour"; break;
						case 'min': var unit="minute"; break;
						case 's': var unit="second"; break;
					}
					return false;
				}
				strOffset=matchArr[0].length;
				switch (tokArr[tokInd].token.toLowerCase()) {
					case 'd': intDay=parseInt(matchArr[0],10); break;
					case 'm': intMonth=parseInt(matchArr[0],10); break;
					case 'h': intHour=parseInt(matchArr[0],10); break;
					case 'min': intMin=parseInt(matchArr[0],10); break;
					case 's': intSec=parseInt(matchArr[0],10); break;
				}
				break;
			case 'mm':
			case 'MM':
			case 'dd':
			case 'DD':
			case 'hh':
			case 'HH':
			case 'mins':
			case 'MINS':
			case 'ss':
			case 'SS':
				// Extract two characters from the date string and if it's a
				// number, save it as the month, day, or hour, as appropriate.
				strOffset=2;
				matchArr=dateStr.substr(strInd).match(/^\d{2}/);
				if (matchArr==null) {
					// The two characters aren't a number; there's a mismatch
					// between the pattern and date string, so return an error
					// message.
					return false;
				}
				switch (tokArr[tokInd].token.toLowerCase()) {
					case 'dd': intDay=parseInt(matchArr[0],10);break;
					case 'mm': intMonth=parseInt(matchArr[0],10);break;
					case 'hh': intHour=parseInt(matchArr[0],10); break;
					case 'mins': intMin=parseInt(matchArr[0],10); break;
					case 'ss': intSec=parseInt(matchArr[0],10); break;
				}
				break;
			case 'y':
			case 'Y':
				// Extract two or four characters from the date string and if it's
				// a number, save it as the year.Convert two-digit years to four
				// digit years by assigning a century of '19' if the year is >=
				// cutoffYear, and '20' otherwise.
				if (dateStr.substr(strInd,4).search(/\d{4}/) != -1) {
					// Four digit year.
					intYear=parseInt(dateStr.substr(strInd,4),10);
					strOffset=4;
				} else {
					if (dateStr.substr(strInd,2).search(/\d{2}/) != -1) {
						// Two digit year.
						intYear=parseInt(dateStr.substr(strInd,2),10);
						if (intYear>=cutoffYear) {
							intYear+=1900;
						}
						else {
							intYear+=2000;
						}
						strOffset=2;
					} else {
						// Bad year; return error.
						return false;
					}
				}
				break;
			case 'yy':
			case 'YY':
				// Extract two characters from the date string and if it's a
				// number, save it as the year.Convert two-digit years to four
				// digit years by assigning a century of '19' if the year is >=
				// cutoffYear, and '20' otherwise.
				if (dateStr.substr(strInd,2).search(/\d{2}/) != -1) {
					// Two digit year.
					intYear=parseInt(dateStr.substr(strInd,2),10);
					if (intYear>=cutoffYear) {
						intYear+=1900;
					}
					else {
						intYear+=2000;
					}
					strOffset=2;
				} else {
					// Bad year; return error
					return false;
				}
				break;
			case 'yyyy':
			case 'YYYY':
				// Extract four characters from the date string and if it's a
				// number, save it as the year.
				if (dateStr.substr(strInd,4).search(/\d{4}/) != -1) {
					// Four digit year.
					intYear=parseInt(dateStr.substr(strInd,4),10);
					strOffset=4;
				} else {
					// Bad year; return error.
					return false;
				}
				break;
			case 'mon':
			case 'Mon':
			case 'MON':
			case 'mon_strict':
				// Extract three characters from dateStr and parse them as
				// lower-case, mixed-case, or upper-case abbreviated months,
				// as appropriate.
				monPat=monPatArr[tokArr[tokInd].token];
				if (dateStr.substr(strInd,3).search(monPat) != -1) {
					intMonth=lowerMonArr[dateStr.substr(strInd,3).toLowerCase()];
				} else {
					// Bad month, return error.
					return false;
				}
				strOffset=3;
				break;
			case 'month':
			case 'Month':
			case 'MONTH':
			case 'month_strict':
				// Extract a full month name at strInd from dateStr if possible.
				monPat=monthPatArr[tokArr[tokInd].token];
				matchArray=dateStr.substr(strInd).match(monPat);
				if (matchArray==null) {
					// Bad month, return error.
					return false;
				}
				// It's a good month.
				intMonth=lowerMonArr[matchArray[0].substr(0,3).toLowerCase()];
				strOffset=matchArray[0].length;
				break;
			case 'ampm':
			case 'AMPM':
				matchArr=dateStr.substr(strInd).match(/^(am|pm|AM|PM|a\.m\.|p\.m\.|A\.M\.|P\.M\.)/);
				if (matchArr==null) {
					// There's no am/pm in the string.Return error msg.
					return false;
				}
				// Store am/pm value for later (as just am or pm, to make things
				// easier later).
				if (matchArr[0].substr(0,1).toLowerCase() == "a") {
					// This is am.
					ampm = "am";
				}
				else {
					ampm = "pm";
				}
				strOffset = matchArr[0].length;
				break;
			default:
				break;
		}
		strInd += strOffset;
		tokInd++;
	}
	if (tokInd != tokArr.length || strInd != dateStr.length) {
		/* We got through the whole date string or format string, but there's
		more data in the other, so there's a mismatch. */
		return false;
	}
	// Make sure all components are in the right ranges.
	if (intMonth < 1 || intMonth > 12) {
		return false;
	}
	if (intDay < 1 || intDay > 31) {
		return false;
	}
	// Make sure user doesn't put 31 for a month that only has 30 days

	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && intDay == 31) {
		return false;
	}
	// Check for February date validity (including leap years)
	if (intMonth == 2) {
		// figure out if "year" is a leap year; don't forget that
		// century years are only leap years if divisible by 400
		var isleap=(intYear%4==0 && (intYear%100!=0 || intYear%400==0));
		if (intDay > 29 || (intDay == 29 && !isleap)) {
			return false;
		}
	}
	// Check that if am/pm is not provided, hours are between 0 and 23.

	if (ampm == "") {
		if (intHour < 0 || intHour > 23) {
			return false;
		}
	} else {
		// non-military time, so make sure it's between 1 and 12.
		if (intHour < 1|| intHour > 12) {
			return false;
		}
	}
	// If user specified amor pm, convert intHour to military.

	if (ampm=="am" && intHour==12) {
		intHour=0;
	}
	if (ampm=="pm" && intHour < 12) {
		intHour += 12;
	}
	if (intMin < 0 || intMin > 59) {
		return false;
	}
	if (intSec < 0 || intSec > 59) {
		return false;
	}
	return true;
}

// FormatToken is a datatype we use for storing extracted tokens from the
// format string.

function FormatToken (token, type) {
	this.token=token;
	this.type=type;
}

function parseFormatString (formatStr) {
	var tokArr=new Array;
	var tokInd=0;
	var strInd=0;
	var foundTok=0;
	while (strInd < formatStr.length) {
		var charAtpos = formatStr.charAt(strInd);
		if( charAtpos.match(delimPat) ){
			tokArr[tokInd++] = new FormatToken(charAtpos, "literal");
			strInd += 1;
		}else if ((matchArray=formatStr.substr(strInd).match(tokPat)) != null) {
				strInd += matchArray[0].length;
				tokArr[tokInd++] = new FormatToken(matchArray[0], "symbolic");
		}
	}
	return tokArr;
}

function getIndex(what, from, which) {
	for (var i = from; i < what.elements.length; i++){
		if (what.elements[i] != null && what.elements[i].name == which){
			return i;
		}
	}
	return -1;
}

function reloadSurveyWindow(){
	document.location.reload();
}

// detect Netscape 4 function
function isNetscape4(){
	var browserName = navigator.appName.toLowerCase();
	if (browserName.indexOf('netscape') > (-1) && parseInt(navigator.appVersion) < 5 ){
		return true;
	} else {
		return false;
	}
}

function uploadFile(systemurl, formE, formName, freeTextField){
	if(formE.s == null || formE.r == null){
		return false;
	}
	var name = "upload_w";
	var uploadUrl = systemurl + "/rupload?sId=" + formE.s.value + "&rId=" + formE.r.value + "&en=" + formE.enc.value;
	uploadUrl = uploadUrl + "&ticket=" + formE.ticket.value + "&fn=" + formName +"&tan=" + freeTextField + "&lang=" + formE.lang.value;
	var features ="toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=1,resizable=1,width="+500+",height="+400;
	//alert("uploadUrl = \n\n" + uploadUrl);
	window.open(uploadUrl, name, features);
}



/**
 *    JSON (Added OPS_ infront to make sure there is no conflicts with JSON used in the main page)
 */


// Constructor -- pass a REST request URL to the constructor
//
function OPS_JSONscriptRequest(fullUrl) {
		// REST request path
		this.fullUrl = fullUrl;
		// Keep IE from caching requests
		this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
		// Get the DOM location to put the script tag
		this.headLoc = document.getElementsByTagName("head").item(0);
		// Generate a unique script tag id
		this.scriptId = 'JscriptId' + OPS_JSONscriptRequest.scriptCounter++;

		if(!this.headLoc || this.headLoc == null){
			alert("Unable to create server request - HEAD element is not found in the document.");
		}
}

// Static script ID counter
OPS_JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
OPS_JSONscriptRequest.prototype.buildScriptTag = function () {

		// Create the script tag
		this.scriptObj = document.createElement("script");

		// Add script object attributes
		this.scriptObj.setAttribute("type", "text/javascript");
		this.scriptObj.setAttribute("charset", "utf-8");
		this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE + '&scriptId=' + this.scriptId);
		this.scriptObj.setAttribute("id", this.scriptId);
}

// removeScriptTag method
OPS_JSONscriptRequest.prototype.removeScriptTag = function () {
		// Destroy the script tag
		this.headLoc.removeChild(this.scriptObj);
}

// addScriptTag method
OPS_JSONscriptRequest.prototype.addScriptTag = function () {
		// Create the script tag
		this.headLoc.appendChild(this.scriptObj);
}

// remove the caller script element
function OPS_JSONremoveCallerScript(scriptId){
	try {
		var callerScript = document.getElementById(scriptId);
		var headElement = document.getElementsByTagName("head").item(0);
		headElement.removeChild(callerScript);
	}catch(ignore){
	}
}

var MATRIX_CELL_NUMERIC_INT	= 6;
var MATRIX_CELL_NUMERIC_DEC	= 7;

var operators = {
   '+'   : {operator: '+', precedence: 1, calculate: function(l,r) { return l + r; } },
   '-'   : {operator: '-', precedence: 1, calculate: function(l,r) { return l - r; } },
   '*'   : {operator: '*', precedence: 2, calculate: function(l,r) { return l * r; } },
   '/'   : {operator: '/', precedence: 2, calculate: function(l,r) { return l / r; } }
};

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function parseVal(r) {
    var startOffset = r.offset;
    var value = 0;
    // floating point number
    var digits = "0123456789";
    while(digits.indexOf(r.string.substr(r.offset, 1)) >= 0 && r.offset < r.string.length) r.offset++;
    if(r.string.substr(r.offset, 1) == ".") {
        r.offset++;
        while(digits.indexOf(r.string.substr(r.offset, 1)) >= 0 && r.offset < r.string.length) r.offset++;
    }
    if(r.offset > startOffset) {  // get the number
        return parseFloat(r.string.substr(startOffset, r.offset-startOffset));
    } else if(r.string.substr(r.offset, 1) == "+") {  // unary plus
        r.offset++;
        return parseVal(r);
    } else if(r.string.substr(r.offset, 1) == "-") {  // unary minus
        r.offset++;
        return (-1)*(parseVal(r));
    } else if(r.string.substr(r.offset, 1) == "(") {  // expression in parenthesis
        r.offset++;   // eat "("
        value = parseExpr(r);
        if(r.string.substr(r.offset, 1) == ")") {
            r.offset++;
            return value;
        }
        r.error = "Parsing error: ')' expected";
        throw 'parseError';
    } else {
        if(r.string.length == r.offset) {
            r.error = 'Parsing error at end of string: value expected';
            throw 'valueMissing';
        } else  {
            r.error = "Parsing error: unrecognized value";
            throw 'valueNotParsed';
        }
    }
}

function parseExpr(r) {
    var stack = [{precedence: 0}];
    var op;
    var value = parseVal(r);  // first value on the left
    for(;;){
    	var nextOp = ("+-*/".indexOf(r.string.substr(r.offset,1)) >= 0) ? operators[r.string.substr(r.offset++, 1)] : null;
        op = nextOp || {precedence: 0}; 
        while(op.precedence <= stack[stack.length-1].precedence) {  
            // precedence of op is lower than the one in stack, calculate the left first
            var tos = stack.pop();
            if(!tos.calculate) return value;  // end reached
            // do the calculation ("reduce"), producing a new value
            value = tos.calculate(tos.value, value);
        }
        // store on stack and continue parsing ("shift")
        stack.push({operator: op.operator, precedence: op.precedence, calculate: op.calculate, value: value});
        value = parseVal(r);  // value on the right
    }
}

function parseMatrixFormula(str) {
    var r = {string: str, offset: 0};
    var value = parseExpr(r);
    if(value == Number.POSITIVE_INFINITY || value == Number.POSITIVE_INFINITY || value == Number.NaN) {
    	// show ####
    	throw 'number out of range';
    }
    if(r.offset < r.string.length){
      r.error = 'Syntax error: junk found at offset ' + r.offset;
        throw 'trailingJunk';
    }
    return value;
}


/**
 * Expands a range for summation. For example:
 * 
 *   A2:B6(default: cells within same question as formula-cell), or Q2!A2:Q2!B6 (if question 2 and formula not on same page)  
 * could be expanded to:
 *   Q2!A2 + Q2!A4 + Q2!A5 + Q2!B5
 * 
 * In this example, the cells expanded are the only cells in the matrix range which are numeric fields.
 * 
 * @param range a string like "A2:B6", or "Q2!A2:Q2!B6"
 * @param question the (default) question with the formula where range is a part of
 * @param questionNo question number of the (default) question
 * @return
 */
function expandMatrixSummationRange(range, question, questionNo) {
	var q = question;
	var sumAreaArray = range.split(":");
	var indexExclamation = range.indexOf("!");
	var prefix = "Q" + questionNo + "!"; // default prefix
	if(indexExclamation > 1) {
		var qNo = sumAreaArray[0].substring(1, indexExclamation);
		prefix = range.substring(0, indexExclamation + 1); // a string like "Q2!"
		q = matrixSummationInfo[qNo-1];
		// get the cell refs
		sumAreaArray[0] = sumAreaArray[0].substring(indexExclamation + 1);
		sumAreaArray[1] = sumAreaArray[1].substring(indexExclamation + 1);
	}
	// check validity
	if (!q || !q.matrix) return {summationString: "", summationCount: 0};
	
	var col1 = toNumericFromAlphabeticPermutation(sumAreaArray[0].substring(0, sumAreaArray[0].search(/[0-9]/)));
	var row1 = parseInt(sumAreaArray[0].substring(sumAreaArray[0].search(/[0-9]/)))-1;
	
	var col2 = toNumericFromAlphabeticPermutation(sumAreaArray[1].substring(0, sumAreaArray[1].search(/[0-9]/)));
	var row2 = parseInt(sumAreaArray[1].substring(sumAreaArray[1].search(/[0-9]/)))-1;
	
	var colStart = Math.min(col1,col2);
	var colEnd = Math.max(col1,col2);
	var rowStart = Math.min(row1,row2);
	var rowEnd = Math.max(row1,row2);
	// check boundaries
	colEnd = (q.matrix.columns.length <= colEnd)? q.matrix.columns.length-1 : colEnd;
	rowEnd = (q.matrix.columns[0].length <= rowEnd)? q.matrix.columns[0].length-1 : rowEnd;
	
	var expandString = "";
	var count = 0;
	for ( var col = colStart; col <= colEnd; col++) {
		for ( var row = rowStart; row <= rowEnd; row++) {
			if(q.matrix.columns[col][row]) {
				var cellType = q.matrix.columns[col][row].cellType;
				if(cellType == MATRIX_CELL_NUMERIC_INT || cellType == MATRIX_CELL_NUMERIC_DEC) {
					expandString += prefix + toAlphabeticPermutation(col, true) + (row+1) + "+";
					count++;
				}
			}
		}
	}
	if(expandString != "") expandString = expandString.substr(0, expandString.length-1); // remove the last "+"
	return {summationString: expandString, summationCount: count};
}

/**
 * Expands math-functions (sum, avg) in a matrix. Searches the formula for these functions, then 
 * takes the ranges, and expands the expression. Ie:
 *   sum(A2:B6) (default: cells within same question as formula-cell), or sum(Q2!A2:Q2!B6)(if question 2 and formula not on same page) 
 * could be expanded to:
 *   Q2!A2 + Q2!A4 + Q2!A5 + Q2!B5
 *  
 * Average will be expanded like this:
 * 
 *   avg(A2:B6) 
 * could be expanded to:
 *   (Q2!A2 + Q2!A4 + Q2!A5 + Q2!B5)/4
 * 
 * In this example, the cells expanded are the only cells in the matrix range which are numeric fields.
 * 
 * @param formula a mathematical formula that possibly includes mathematical functions
 * @param question The (default) matrix question
 * @param questionNo question number of the (default) question
 * @return
 */
function expandMatrixMathFunction(formula, question, questionNo) {
	if (!question || !question.matrix) {
		return;
	}
	var summationObj;
	var formulaStr = formula.toUpperCase().replace(/\s/g, ""); // remove spaces
	
	// expand SUM function
	var reSum = new RegExp("sum\\(((q|Q)[0-9]+!)?[a-zA-Z]+[0-9]+:((q|Q)[0-9]+!)?[a-zA-Z]+[0-9]+\\)", "gi");
	var arraySum = formulaStr.match(reSum);
	if (arraySum) {
		var arraySumCopy = $.extend([], arraySum, true);
		for ( var i = 0; i < arraySum.length; i++) {
			arraySum[i] = arraySum[i].substring(4);
			arraySum[i] = arraySum[i].substring(0, arraySum[i].length - 1);
			summationObj = expandMatrixSummationRange(arraySum[i], question, questionNo);
			if(summationObj.summationCount > 0) {
				formulaStr = formulaStr.replace(arraySumCopy[i], "(" + summationObj.summationString + ")" );
			} else {
				formulaStr = formulaStr.replace(arraySumCopy[i], "0" );
			}
		}
	}
	
	// expand AVG function
	var reAvg = new RegExp("avg\\(((q|Q)[0-9]+!)?[a-zA-Z]+[0-9]+:((q|Q)[0-9]+!)?[a-zA-Z]+[0-9]+\\)", "gi");
	var arrayAvg = formulaStr.match(reAvg);
	if (arrayAvg) {
		var arrayAvgCopy = $.extend([], arrayAvg, true);
		for ( var j = 0; j < arrayAvg.length; j++) {
			arrayAvg[j] = arrayAvg[j].substring(4);
			arrayAvg[j] = arrayAvg[j].substring(0, arrayAvg[j].length - 1);
			summationObj = expandMatrixSummationRange(arrayAvg[j], question, questionNo);
			if(summationObj.summationCount > 0) {
				formulaStr = formulaStr.replace(arrayAvgCopy[j], "(" + summationObj.summationString + ")/" + summationObj.summationCount);
			} else {
				formulaStr = formulaStr.replace(arrayAvgCopy[j], "0" );
				//throw 'invalidFormula: average count is 0';
			}
		}
	}
	
	// expand default strings, e.x. "A3" to "Q1!A3"
	var re = new RegExp("((q|Q)[0-9]+!)?[a-zA-Z]+[0-9]+", "gi");
	var arrayExpanded = formulaStr.match(re);
	if (arrayExpanded) {
		for ( var k = 0; k < arrayExpanded.length; k++) {
			if(arrayExpanded[k].indexOf("!") == -1) {
				// look-around should be applied here, so that we replace only "A3", not "Q2!A3" or "A36";
				// since javascript does not support look-behind, a walk-around is used in replace function
				var regExp = new RegExp("(!)?" + arrayExpanded[k] + "(?![0-9]+)", "gi");
				formulaStr = formulaStr.replace(regExp, function($0, $1){
					return $1 ? $0 : "Q"+questionNo+"!"+arrayExpanded[k];
				});
			}
		}
	}
	
	return formulaStr;
}

/**
 * Creates an alphanumeric permutation from an integer(0-based), i.e. 0 is "A", 1 is "B", 26 is "AA", etc.
 */
function toAlphabeticPermutation(num, useCapitalLetters) {
	var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
	if(useCapitalLetters) {
		alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
	}
	var modNum = num % alphabet.length;
	var permutation = "";
	permutation = permutation + alphabet[modNum];

	var divNum = Math.round((num / alphabet.length) - 0.5);
	if(divNum > 0) {
		if(divNum > alphabet.length) {
			permutation = toAlphabeticPermutation(divNum - 1, useCapitalLetters) + permutation;
		} else {
			permutation = alphabet[divNum - 1] + permutation;
		}
	}
	return permutation;
}

/**
 * Finds the position of a character in the latin alphabet.
 * 
 * @param character
 * @return The position in the alphabet.
 */
function alphabetPosition(character) {
	var alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
	for ( var i = 1; i <= alphabet.length; i++) {
		if(character == alphabet[i-1]) {
			return i;
		}
	}
}

/**
 * Creates a number (0-based) from an alphanumeric permutation, i.e. "B" is 1, "AB" is 27, etc
 */
function toNumericFromAlphabeticPermutation(str) {
	var strCopy = str.toUpperCase();
	var result = 0;
	for ( var i = 0; i < strCopy.length-1; i++) {
		result = 26 * (result + alphabetPosition(strCopy.charAt(i)));
	}
	result += alphabetPosition(strCopy.charAt(strCopy.length-1));
	return result - 1;
}

function addMatrixSummationHandlers() {
	if(typeof(matrixSummationInfo) == 'undefined' || !matrixSummationInfo) return;
	
	// loop through all matrix questions
	for(var qIndex = 0; qIndex < matrixSummationInfo.length; qIndex++) {
		if(!matrixSummationInfo[qIndex] || !matrixSummationInfo[qIndex].matrix) continue;
		var cells = matrixSummationInfo[qIndex].matrix.columns;
		// loop through all cells
		for(var col = 0; col < cells.length; col++) {
			for(var row = 0; row < cells[0].length; row++) {
				var cell = cells[col][row];
				// if a cell is numeric with formula
				if(cell && cell.formula) {
					// expand the formula and find cell names within it
					var formula = expandMatrixMathFunction(cell.formula, matrixSummationInfo[qIndex], qIndex+1);
					// match all formula cells (in the form like "Q2!A6")
					var cellRefArray = formula.match(/(q|Q)[0-9]+![a-zA-Z]+[0-9]+/gi);
					// loop through formula cells
					for(var k = 0; cellRefArray && k < cellRefArray.length; k++) {
						// fetch index of reference question and index of reference column/row
						var qRefIndex = getFormulaCells(cellRefArray[k]).qIndex; 
						var colRefIndex = getFormulaCells(cellRefArray[k]).colIndex;
						var rowRefIndex = getFormulaCells(cellRefArray[k]).rowIndex;
						// the reference question in formula must exist and be matrix
						if(!matrixSummationInfo[qRefIndex] || !matrixSummationInfo[qRefIndex].matrix) continue;
						
						var cellsRef = matrixSummationInfo[qRefIndex].matrix.columns;
						if(colRefIndex < cellsRef.length && rowRefIndex < cellsRef[0].length) {
							var formulaCell = cellsRef[colRefIndex][rowRefIndex];
							if(formulaCell && (formulaCell.cellType == 6 || formulaCell.cellType == 7)) {
								// register "keyup" event on formulaCell
								var qNo = qRefIndex + 1;
								var eventData =  { formulaCellSelector: "#q"+(qIndex+1)+"cell"+col+"_"+row, "formula": formula, "qNo": qNo, isInteger: (cell.cellType == 6), colIndex: colRefIndex, rowIndex: rowRefIndex};
								$("#q"+qNo+"cell"+colRefIndex+"_"+rowRefIndex).bind("keyup", eventData, numericFieldKeyupHandler);
							}
						}
					}
				}
			}
		}
	}

	// initialize values on current page
	calculateInitialMatrixValues();
}

function calculateInitialMatrixValues() {
	// loop through matrix summation array, and find all formulas on current page
	var fromSection = $("#fromS").val();
	var toSection = $("#toS").val();
	for ( var s = fromSection; s <= toSection; s++) {
		var qNoOnPage = $("#section" + s).val().split("-");
		for ( var i = 0; i < qNoOnPage.length; i++) {
			var q = matrixSummationInfo[qNoOnPage[i]-1];
			if(q && q.matrix.columns) {
				var cells = q.matrix.columns;
				// loop through all cells
				for(var col = 0; col < cells.length; col++) {
					for(var row = 0; row < cells[0].length; row++) {
						var cell = cells[col][row];
						// if it is a numeric cell with formula
						if(cell && cell.formula) {
							// calculate formula, and set field value
							var formula = expandMatrixMathFunction(cell.formula, q, qNoOnPage[i]);
							var $cellSelector = $("#q" + qNoOnPage[i] + "cell" + col + "_" + row);
							var isInteger = (cell.cellType == 6)? true : false;
							var result = calculateCellFormula(formula.replace(/\s/g, ""), isInteger);
							// update response info and formula cell field value 
							matrixSummationResponseInfo[qNoOnPage[i]-1].matrix.columns[col][row] = result;
							$cellSelector.val(result).trigger("keyup");
						}
					}
				}
			}
		}
	}
}

/**
 * Handler for numeric fields in a matrix. Updates fields containing a formula referencing
 * the field the handler is attached to; Updates matrixSummationResponseInfo.
 */
function numericFieldKeyupHandler(event) {
	
	//check if value is valid: should contain at most one "," or "."
	var fieldValue = $(event.target).val();
	var cnt = fieldValue.replace(/,/g, ".").split(".").length;
	if(cnt > 2) {
		$(event.data.formulaCellSelector).val("####");
		return;
	}
	
	// update value in matrixSummationResponseInfo
	var col = event.data.colIndex;
	var row = event.data.rowIndex;
	matrixSummationResponseInfo[event.data.qNo-1].matrix.columns[col][row] = fieldValue;
	
	// update result of formula
	
	var formulaString = event.data.formula.replace(/\s/g, ""); // remove spaces
	var result = calculateCellFormula(formulaString, event.data.isInteger);
	// update response info and formula cell field value 
	var selector = event.data.formulaCellSelector;
	var questionNo = selector.split("cell")[0].substring(2);
	var colIndex = selector.split("cell")[1].split("_")[0];
	var rowIndex = selector.split("cell")[1].split("_")[1];
	matrixSummationResponseInfo[questionNo-1].matrix.columns[colIndex][rowIndex] = result;
	$(selector).val(result).trigger("keyup");
}

function calculateCellFormula(formulaString, isInteger) {
	var formulaStringCopy = formulaString;
	// find all cell references
	var refCellArray = formulaStringCopy.match(/(q|Q)[0-9]+![a-zA-Z]+[0-9]+/gi);
	// loop through all cell references and fetch values
	for(var m = 0; m < refCellArray.length; m++) {
		var refQIndex = getFormulaCells(refCellArray[m]).qIndex;
		var refColIndex = getFormulaCells(refCellArray[m]).colIndex;
		var refRowIndex = getFormulaCells(refCellArray[m]).rowIndex;
		// replace cell references in formula with values in the input fields
		var value = matrixSummationResponseInfo[refQIndex].matrix.columns[refColIndex][refRowIndex];
		if(value && value != "") {
			formulaStringCopy = formulaStringCopy.replace(refCellArray[m], value);
		} else {
			if(formulaStringCopy.indexOf("/") < 0){
				formulaStringCopy = formulaStringCopy.replace(refCellArray[m], "0");
			} else {
				return "####";
			}
		}
	}
	// calculate the formula, now with numbers only
	var result;
	try {
		result = parseMatrixFormula(formulaStringCopy.replace(/,/g, "."));
		result = isInteger ? Math.round(result) : roundNumber(result, 2);
	} catch (e) {
		return "####";
	}
	
	// set the result into field referenced by "formulaCellSelector"
	return result;
}

// return an object in the following format:
// {qIndex: 2, colIndex: 3, rowIndex: 5}
function getFormulaCells(cellRef) {
	var indexExclamation = cellRef.indexOf("!");
	var questionIndex = cellRef.substring(1, indexExclamation) - 1;
	var cellRef = cellRef.substring(indexExclamation + 1);
	var col = toNumericFromAlphabeticPermutation(cellRef.substring(0, cellRef.search(/[0-9]/)));
	var row = cellRef.substring(cellRef.search(/[0-9]/)) - 1;
	return {qIndex: questionIndex, colIndex: col, rowIndex: row};
}

