﻿//snap 10 JavaScript functions shared by web:html and msf based formats including webHost
var objectsById=new Object();
var missingObjects=new Object();
var dependents=new Object();
var askedCache=new Object();
var cacheSelectOptions=new Object();
var snapVars = new Object();
//var g_noReplyText = "%%SNAP_NR_MESSAGE";
//var g_NoReply = "%%SNAP_USERSTRINGNoReply%%_END_";
//var g_And = " %%SNAP_USERSTRINGSnapAnd%%_END_ ";
function snapVariable(vName, qName, gridFirstVname, inputType, responseType, numCodes, pageNum, askedCalc, validate, mustAns, exclusive, codeLabels, dependents)
{	this.vName = vName ? vName : null;	//String
	this.qName = qName ? qName : null;	//String
	this.gridFirstVname = gridFirstVname ? gridFirstVname : null;	//String
	this.inputType = inputType ? inputType : null;	//String: "checkbox", "radio", "select", "text", "hidden" //"textarea" being stated as "text" for now
	this.responseType = responseType ? responseType : null; //String: "Single", "Multiple", "Quantity", "Date", "Time", "Literal", "None"
	if (this.gridFirstVname != null)
	{	//grid-first or grid-next, but not grid header.
		var gridName = gridFirstVname + "_GRID";
		var gHeader = snapVars[gridName];
		if (gHeader == null)
		{	gHeader = new snapVariable(gridName);
			gHeader.gridMembers = new Array();
			gHeader.gridFirstVname = gridFirstVname;
			gHeader.pageNum = pageNum;
		}
		gHeader.gridMembers[gHeader.gridMembers.length] = vName;
		if (askedCalc && !gHeader.askedCalc)
		{	gHeader.askedCalc = getGridRouting;
		}
	}
	this.codeLabels = codeLabels ? codeLabels : null;	//Array
	this.askedCalc = askedCalc ? askedCalc : null;	//Function
	this.validate = validate ? validate : null;	//Function
	this.mustAns = mustAns ? mustAns : false;	//bool
	this.exclusive = exclusive ? exclusive : null;	//Array
	this.numCodes = numCodes ? numCodes : 0;	//Number
	this.pageNum = pageNum ? pageNum : 0;	//Number
	this.dependents = dependents ? dependents : null;	//Array
	snapVars[vName] = this;
	if (!snapVariable.prototype.IsGrid)
	{	snapVariable.prototype.IsGrid = function(){return (null != this.gridFirstVname);}
	}
}
function snapVarMask(vName, codeMask, autoAnswer)
{	var sVar = snapVars[vName];
	if (sVar)
	{	sVar.codeMask = codeMask ? codeMask : null;
		sVar.autoAnswer = autoAnswer ? autoAnswer : null;
		if (sVar.gridFirstVname != null)
		{	var gridName = sVar.gridFirstVname + "_GRID";
			var gHeader = snapVars[gridName];
			if (gHeader && !gHeader.askedCalc)
			{	gHeader.askedCalc = getGridRouting;
			}
		}
	}
}
function getGridRouting()
{	var askedVal = false;
	for (var i in this.gridMembers)
	{	if (!askedVal)
		{	askedVal = asked(this.gridMembers[i]);
		}
	}
	return askedVal;
}
function FindQuestion(qName)
{	qName = qName.toLowerCase();
	var retVal = null;
	for (var vName in snapVars)
	{	var sVar = snapVars[vName];
		if (sVar && sVar.qName && sVar.qName.toLowerCase()==qName)
		{retVal = sVar;break;
		}
	}
	if (!retVal)
		retVal = snapVars[qName.toUpperCase()];
	return retVal;
}
function snapHookEvents(question)
{	var items = snapObjectsByName(question);
	var i;
	var item;
	for (i=0; i<items.length; i++)
	{	item=items[i];
		if (item.type)
		{	if (item.type=='checkbox' || item.type=='radio')
				snapAddEvent(item, 'click', snapChangeMade);
			else if (item.type=='select-one' || item.type=='select-multiple')
				snapAddEvent(item, 'change', snapChangeMade);
			else if (item.type=='text' || item.type=='textarea')
			{	snapAddEvent(item, 'blur', snapChangeMade);
				snapAddEvent(item, 'keypress', snapTimedChange);
			}
		}
	}
}
function snapAddEvent(obj, evType, fn)
{	var result=false;
	if (obj)
	{	if (obj.attachEvent)
		{	result=obj.attachEvent("on"+evType, fn);
		} else if (obj.addEventListener)
		{	obj.addEventListener(evType, fn, true);
			result=true;
		}
	}
	return result;
}
function snapGlobalDoc()
{	return document.forms["SnapForm"].elements;
}
function snapObject(id)
{	var ret = objectsById[id];
	if (ret == null)
	{	if (missingObjects[id] == null)
		{	ret = document.getElementById(id);
			if (ret == null)
			{	missingObjects[id] = true;
			}
			objectsById[id] = ret;
		}
	}
	return ret;
}
function snapObjectsByName(name)
{	return document.getElementsByName(name);
}
function snapObjectsByTagName(tagName)
{	return document.getElementsByTagName(tagName);
}
function killEvent(eventOb)
{	if (eventOb)
	{	if (null != eventOb.cancelBubble) eventOb.cancelBubble = true;
		if (eventOb.stopPropagation) eventOb.stopPropagation();
		if (null != eventOb.returnValue) eventOb.returnValue = false;
		if (eventOb.preventDefault) eventOb.preventDefault();
	}
}
function snapEventOrigin(arg)
{	if (arg.srcElement)
		return arg.srcElement;
	else if (arg.target)
		return arg.target;
	return null;
}
function TextToHtml(txtValue)
{	var htmlValue = txtValue.replace(/&/g, "&amp;");
	htmlValue = htmlValue.replace(/[<]/g, "&lt;");
	htmlValue = htmlValue.replace(/>/g, "&gt;");
	htmlValue = htmlValue.replace(/\r\n/g,"<br>");
	htmlValue = htmlValue.replace(/\n/g, "<br>");
	htmlValue = htmlValue.replace(/\r/g,"<br>");
	htmlValue = htmlValue.replace(/\t/g, "&nbsp;");
	return htmlValue;
}
function snapFocusObject(object)
{	var done = false;
	if (object)
	{	try
		{	object.focus();
			done = true;
		}
		catch (e)//hidden control
		{	done = false;
		}
	}
	return done;
}
function snapShowQuestion(vName)
{	var control = snapObject(vName + "_1");
	snapFocusObject(control);
	var tab = snapObject(vName);
	if (tab)
	{	var offset = findObjY(tab);
		if (window.scrollTo)
		{	window.scrollTo(0,offset);
		}else if (window.scroll)
		{	window.scroll(0,offset);
		}
	}
}
function findObjY(obj)
{	var curtop = 0;
	if (obj.offsetParent)
	{	while (obj.offsetParent)
		{	curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}else if (obj.y)
		curtop = obj.y;
	return curtop;
}
//recursivley Invalidate Routing cache for dependents of 'question'
function snapUndoRoutingFor(vName)
{	if (vName && dependents[vName])
	{	var undo=dependents[vName];
		for (var i=0; i<undo.length; i++)
		{	var thisVar = undo[i];
			var sVar = snapVars[thisVar];
			if (sVar && sVar.IsGrid())
			{	var gridOwner = sVar.gridFirstVname;
				gridOwner += "_GRID";
				if (askedCache[gridOwner]!=null)
					delete askedCache[gridOwner];
			}
			if (askedCache[thisVar]!=null)
			{	delete askedCache[thisVar];
				snapUndoRoutingFor(thisVar);
			}else if (sVar && !sVar.askedCalc)
			{	snapUndoRoutingFor(thisVar);
			}
		}
	}
}
function resetRouting()
{	askedCache = new Object();
}
function snapEvalAllRouting()
{	var somethingShown=false;
	if (snapVars)
	{	for (var vName in snapVars)
		{	if(snapEvalAskedFor(vName))
			{	somethingShown=true;
			}
		}
	}
	return somethingShown;
}
////////////////////////////////////////////////////////////////////////////////
function snapInclude(obj, show, val)
{	var changed = false;
	if (obj)
	{	if (val == null)
		{	val = '';
		}
		if (!show)
		{	val = 'none';
		}
		if (obj.style.display!=val)
		{	obj.style.display = val;
			changed = true;
		}
	}
	return changed;
}
function snapHide(obj, show)
{	if (obj)
	{	obj.style.visibility=show?'visible':'hidden';
	}
}
function snapIsIncluded(obj)
{	var result=false;
	if (obj)
		result=(obj.style.display!='none');
	return result;
}
function findArrayValue(array, value)
{	var i;
	for(i=0;i<array.length;i++)
	{	if(array[i]==value)
			return i;
	}
	return null;
}
function snapSwapObs(aCode, newCode)
{	if(aCode && newCode)
	{	var aParent=aCode.parentNode;
		var newParent=newCode.parentNode;
		if (aParent &&newParent)
		{	aParent.removeChild(aCode);
			newParent.removeChild(newCode);
			aParent.appendChild(newCode);
			newParent.appendChild(aCode);
		}
	}
}
////////////////////////////////////////////////////////////////////////////////
// Variable No Reply
function doAutoAns(vName)
{	var sVar = snapVars[vName];
	if (sVar && sVar.codeMask)
	{	var numCodes = sVar.numCodes;
		var selObj = null;
		if (snapIsDropdown(vName))
			selObj = snapObject(vName+'_1');
		var changed = false;
		for(var i=1;i<=numCodes;i++)
		{	var showThisCode=sVar.codeMask(i);
			if (selObj && selObj.options)
			{	var found = false;
				for (var x = 0; x < selObj.options.length && !found; x++)
				{	if (selObj.options[x].value == i)
					{	found = true;
						if (selObj.options[x].selected != null)
						{	if (showThisCode != selObj.options[x].selected)
							{	changed = true;
								selObj.options[x].selected = showThisCode;
							}
						}
					}
				}
			}else
			{	var codeOb = snapObject(vName+'_'+i);
				if (codeOb && codeOb.checked != null)
				{	if (showThisCode != codeOb.checked)
					{	changed = true;
						codeOb.checked = showThisCode;
						if (sVar.checkImage != null)
							SyncImage(vName, i);
					}
				}
			}
		}
		if (changed)
			snapOnChange(vName);
	}
}
function snapCodeBox(vName, i)
{	var box = null;
	var sVar = snapVars[vName];
	if (sVar && (sVar.checkImage!=null))
	{	box = sVar.imgCodeBox[i];
	}
	if (box==null)
	{	box = snapObject(vName+'_'+i);
	}
	return box;
}
function snapGridLine(vName)
{
	var gridLine=null;
	var elem=snapObject(vName+'_1');
	while (elem && (!gridLine || gridLine.id == null || gridLine.id != vName))
	{
		while(elem!=null && (elem.tagName==null || elem.tagName.toLowerCase()!='tr'))
			elem=elem.parentNode;
		if (elem != null)
		{
			if (!gridLine || (elem.id != null && elem.id == vName))
				gridLine = elem;
			elem=elem.parentNode;
		}
	}
	return gridLine;
}
////////////////////////////////////////////////////////////////////////////////
// Variable Not Asked
function snapEvalAskedFor(vName, postponedItems)
{	var somethingShown=true;
	var sVar = snapVars[vName];
	var changed = false;
	if(sVar && (sVar.askedCalc || sVar.codeMask))
	{	var ask=asked(vName);
		somethingShown=ask;
		if(sVar.IsGrid())
		{
					var gridLine = snapGridLine(vName);
			if(gridLine!=null)
			{	snapInclude(gridLine, ask);
			}
			var gridOwner=sVar.gridFirstVname;
			if(gridOwner != vName)
			{	snapInclude(snapObject(vName+'_SPACER'), ask);
			}
			var gridName = gridOwner +"_GRID";
			if(snapVars[gridName] && snapVars[gridName].askedCalc)
			{	var gridask=asked(gridName);
				snapInclude(snapObject(gridName), gridask);
				snapInclude(snapObject(gridOwner+'_SPACER'), gridask);
				somethingShown=gridask;
			}
		} else
		{	if (snapInclude(snapObject(vName), ask))
				changed = true;
			snapInclude(snapObject(vName+'_SPACER'), ask);
		}
		if (!ask && sVar.doAutoAns)
		{	doAutoAns(vName);
		}
	}
	if(somethingShown && sVar && sVar.codeMask)
	{	var isCombo = snapIsDropdown(vName);
		var numCodes = sVar.numCodes;
		var i;
		var showThisCode;
		somethingShown=false;
		for(i=1;i<=numCodes;i++)
		{	showThisCode=sVar.codeMask(i);
			if (isCombo)
			{	if (snapIncludeCombo(vName, i, showThisCode, postponedItems))
					changed = true;
			}else
			{	var codeObject = snapObject(vName+'c'+i);
				var codeBox = snapCodeBox(vName, i);
				if(codeObject)
					snapInclude(codeObject, showThisCode);
				else
					snapInclude(codeBox, showThisCode);
				snapHide(snapObject(vName+'g'+i), showThisCode);
				snapHide(snapObject(vName+'v'+i), showThisCode);
				if (!showThisCode)
				{	var checkBox = snapObject(vName+'_'+i);
					if (checkBox && (checkBox.checked))
					{	checkBox.checked = false;
						if (checkBox != codeBox)
							SyncImage(vName, i);
						changed = true;
					}
				}
			}
			if(showThisCode)
				somethingShown=true;
		}
	}
	if (changed)
		snapOnChange(vName);
	return somethingShown;
}
function snapIncludeCombo(vName, code, showThisCode, postponedItems)
{	var changed = false;
	var selOb = snapObject(vName + '_1');
	if (selOb && selOb.options)
	{	var found = false;
		if (showThisCode)
		{	var removedList = cacheSelectOptions[vName];
			if (removedList)
			{	for (var i =0; i < removedList.length && !found; i++)
				{	if (removedList[i].value != null && removedList[i].value == code)
					{	found = true;
						if (postponedItems)
						{	postponedItems[vName] = true;
						}else
						{	var option = removedList[i];
							removedList.splice(i, 1);
							var pos = null;
							for (var j =0; j < selOb.options.length && (null == pos); j++)
							{	if (selOb.options[j].value > option.value || (selOb.options[j].value ==0 && selOb.options[j].text==""))
								{	pos = selOb.options[j];
								}
							}
							selOb.insertBefore(option, pos );
						}
					}
				}
			}
		}else
		{	for (var i =0; i < selOb.options.length && !found; i++)
			{	if (selOb.options[i].value == code)
				{	found = true;
					if (selOb.options[i].selected)
					{	changed = true;
						try
						{b.options[i].selected = false;
						}catch(e)
						{//ie 6 bug?
						}
					}
					if (postponedItems)
					{	postponedItems[vName] = true;
					}else
					{	var option = selOb.options[i];
						selOb.removeChild(option);
						if (!cacheSelectOptions[vName])
							cacheSelectOptions[vName] = new Array();
						var arr = cacheSelectOptions[vName];
						arr[arr.length] = option;
					}
				}
			}
		}
	}
	return changed;
}
////////////////////////////////////////////////////////////////////////////////
// Variable exclusive
function snapCheckExclusiveOf(sVar, object)
{	if ((sVar.checkImage != null) && object.type && (object.type == 'image'))
	{	object = resolveCheckImg(sVar, object);
	}
	if (object.checked)
	{	var changed = false;
		var c;
		var excl=sVar.exclusive;
		var code=parseInt(object.id.substr(object.id.indexOf('_')+1), 10);
		var isExclusive=false;
		for (c=0; c<excl.length && !isExclusive; c++)
		{	if (excl[c]==code)
				isExclusive=true;
		}
		if (isExclusive)
		{	var numCodes=sVar.numCodes;
			for (c=1; c<=numCodes; c++)
			{	var codeBox = snapObject(sVar.vName+'_'+c);
				if (codeBox && (object!=codeBox) && (codeBox.checked!=null))
				{	codeBox.checked=false;
					if (sVar.checkImage != null)
						SyncImage(sVar.vName, c);
					changed = true;
				}
			}
		} else
		{	for (c=0; c<excl.length; c++)
			{	code=snapObject(object.name+'_'+excl[c]);
				if (code && code.checked)
				{	code.checked=false;
					if (sVar.checkImage != null)
						SyncImage(sVar.vName, excl[c]);
					changed = true;
				}
			}
		}
		if (changed)
		{	snapOnChange(object.name);
		}
	}
}
////////////////////////////////////////////////////////////////////////////////
function snapNumCodes(vName)
{	var result=0;
	var sVar = snapVars[vName];
	if (sVar)
	{	result = sVar.numCodes;
	}
	return result;
}
function snapIsOpen(vName)
{	var result = false;
	var sVar = snapVars[vName];
	if (sVar)
	{	result = sVar.isOpen;
		if (result==null &&sVar.inputType)
		{	result = (sVar.inputType == "text" || sVar.inputType == "textarea" || sVar.inputType == "hidden");
			sVar.isOpen = result;
		}
	}
	return result;
}
function snapIsClosed(vName)
{	var result = false;
	var sVar = snapVars[vName];
	if (sVar)
	{	result = sVar.isClosed;
		if (result==null && sVar.responseType)
		{	result = (sVar.responseType == "M" || sVar.responseType == "S");
			sVar.isClosed = result;
		}
	}
	return result;
}
function snapIsDropdown(vName)
{	var result = false;
	var sVar = snapVars[vName];
	if (sVar)
	{	result = sVar.isDropDown;
		if (result==null && sVar.inputType)
		{	result = (sVar.inputType == "select");
			sVar.isDropDown = result;
		}
	}
	return result;
}
////////////////////////////////////////////////////////////////////////////////
// Variable response
function snapVarReply(vName)
{	var result=g_NoReply;
	//if (asked(vName))
	{	if(snapIsOpen(vName))
			result=snapOpenReply(vName);
		else if(snapIsClosed(vName))
			result=snapClosedReply(vName);
	}
	return result;
}
function snapVarValue(vName)
{	var result="";
	if (snapIsDropdown(vName))
	{	var code=snapObject(vName+'_1');
		if(code.options)
		{	for (var i = 0; i < code.options.length; i++)
			{	var thisOption=code.options[i];
				if(thisOption!=null && thisOption.selected && thisOption.value!=null && thisOption.value.length>0)
				{	if(result.length>0)
						result+=';';
					result+=thisOption.value;
				}
			}
		}
	} else 
	{	var c=snapNumCodes(vName);
		while(c > 0)
		{	code=snapObject(vName+'_'+c);
			if (code)
			{	if((code.checked==null || code.checked) && code.value!=null && code.value.length>0)
				{	if(result.length>0)
						result+=';';
					result+=code.value;
				}
			}
			c--;
		}
	}
	return result;
}
function snapClosedAns(vName)
{	var result=null;
	if(asked(vName))
	{	result=snapVarValue(vName);
	}
	if(result==null)
		result="";
	return result;
}
function snapClosedReply(vName)
{	var result=g_NoReply;
	var response=snapVarValue(vName);
	if(response.length>0)
	{	var responses=response.split(";");
		var i;
		result="";
		for(i=0;i<responses.length;i++)
		{	if(i>0)
			{	if(i<responses.length-1)
					result+=", ";
				else
					result+=g_And;
			}
			result += snapCodeLabel(vName, responses[i]);
		}
	}
	return result;
}
function snapCodeLabel(vName, code)
{	var result=code;
	var sVar = snapVars[vName];
	if (sVar && sVar.codeLabels)
	{	if (0 < code && code <= sVar.codeLabels.length)
		{	result = sVar.codeLabels[code - 1];
		}
	}
	return result;
}
function snapOpenValue(vName)
{
	var reply="";
	var glovar=snapObject(vName+'_1');
	if(glovar != null && glovar.value!=null)
		reply=""+glovar.value;
	if(reply==g_noReplyText)
		reply="";
	reply=reply.replace(/^\s*/,"").replace(/\s*$/,"");
	return reply;
}
function snapOpenAns(vName)
{	var reply=""
	if(asked(vName))
	{
		reply=snapOpenValue(vName);
	}
	return reply;
}
function snapOpenReply(vName)
{	var reply=snapOpenValue(vName);
	if(reply==null || reply.length==0 || reply==g_noReplyText)
		reply=g_NoReply;
	return reply;
}
function snapSetOpenReply(vName, value)
{	var glovar=snapObject(vName+'_1');
	if(glovar != null && glovar.value!=null)
		glovar.value=value;
}
function snapChangeMade(arg)
{	var origin = snapEventOrigin(arg);
	if (origin)
	{	snapCheckValueOf(origin);
		snapOnChange(origin.name);
	}
}
var g_timer = 0;
function snapTimedChange(arg)
{  if (g_timer)
   {  clearTimeout(g_timer);
      g_timer = null;
   }
   var origin = snapEventOrigin(arg);
	if (origin)
	{	var name = origin.name;
      g_timer = setTimeout(function(){g_timer=null;snapOnChange(name);}, 1000); 
   }
}
function snapOnChange(vName)
{	if (vName)
	{	var sVar = snapVars[vName];
		if (sVar && sVar.OnChangeFn)
			sVar.OnChangeFn();
		snapUndoRoutingFor(vName);
		if (typeof snapEvalTotals != 'undefined'){ snapEvalTotals(vName);}
		if (typeof snapSubstituteText != 'undefined'){ snapSubstituteText(vName);}
		if (typeof snapEvalPageRouting != 'undefined'){ snapEvalPageRouting();}
		else if (typeof snapEvalAllRouting != 'undefined'){ snapEvalAllRouting();}
	}
}
// Check syntax of open-ended variables / check exclusive code settings for others
function snapCheckValueOf(object)
{	if (object.name && object.id)
	{	var sVar = snapVars[object.name];
		if (sVar && sVar.exclusive)
		{	if (sVar.inputType == 'checkbox')
			{	snapCheckExclusiveOf(sVar, object);
			}else if (sVar.inputType == 'select')
			{	snapSelectExclusiveOf(sVar, object);
			}
		}
	}
}
function snapSelectExclusiveOf(sVar,object)
{	if (nota_dd)
	{	nota_dd(object, sVar.exclusive);
	}
}
var nota_prevList = new Array();
var nota_firstCode=0,nota_lastSingleClicked=0,nota_numSelected=0,nota_lastCode=0;
function nota_dd(selectOb, codes)
{	var newList=new Array();
	var currentList=new Array();
	var lastClickedCode=0,endCode=0,i=0,j=0;
	var isLastSingle=false,found=false,unSelected=false;
	newList = nota_getNewList(selectOb);
	currentList = nota_getCurrentList(selectOb);
	lastClickedCode = nota_getLastClickedCode(currentList,newList);
	if (nota_numSelected>1 && !isNaN(nota_firstCode) && nota_firstCode!=null && !isNaN(nota_lastCode) && nota_lastCode!=null)
	{	isLastSingle=nota_isInNumArray(codes, selectOb.options[nota_lastCode].value, 0);
		var i=nota_firstCode;
		endCode=(nota_firstCode<nota_lastCode) ? nota_lastCode+1 : lastCode-1;
		while(i!=endCode)	//loop all codes in range nota_firstCode -> nota_lastCode
		{	if ((isLastSingle && i!=lastClickedCode) || (nota_isInNumArray(codes, selectOb.options[i].value, 0) && selectOb.options[i].selected==true))
			{	selectOb.options[i].selected=false;
				unSelected=true;
			}
			nota_firstCode<nota_lastCode ? i++ : i--;
		}
	}
	if (unSelected==true)
		selectOb.selectedIndex=lastClickedCode;
	nota_prevList = nota_getCurrentList(selectOb);
	nota_lastSingleClicked = newList[0];
}
function nota_getCurrentList(selectOb)
{	//Return all codes currently checked
	var i=0,j=0,lastJ=0;
	var cList=new Array();
	nota_firstCode=-1;
	nota_lastCode=-1;
	for(i=0;i<selectOb.length;i++)
	{	if (selectOb[i].selected==true)
		{	if (nota_firstCode<0)nota_firstCode=i;
			if (j>lastJ)
			{	nota_lastCode=i;
				lastJ=j
			}
			cList[j]=i;
			j++;
		}
	}
	return cList;
}
function nota_getLastClickedCode(currList,newList)
{	//If user has clicked up the list return the first code from newList
	//nota_lastSingleClicked is needed for Ctrl_0(M), Ctrl_5(M), Shift_1 over a single
	if (newList.length==1 || newList[0]<nota_prevList[0] || newList[0]<nota_lastSingleClicked)
		return newList[0];
	else	//If user has clicked down the list return the last code from newList
		return newList[newList.length-1];
}
function nota_isInNumArray(arr, i, startIndex)
{	var j=0;
	var found=false;
	if (!(arr==null))
	{	for (j=startIndex; j<arr.length; j++)
		{	if (arr[j]==i) found=true;
		}
	}
	return found;
}
function nota_getNewList(selectOb)
{	//returns allChecked - previousList
	var i,j,k=0;
	nota_numSelected=0;
	var found=false;
	var newList = new Array();
	var collection = selectOb;
	for (i=0; i<collection.length; i++)
	{	if (collection[i].selected==true)
		{	nota_numSelected++;
			if (!nota_isInNumArray(nota_prevList, i, 0))
			{	newList[k]=i;
				k++;
			}
		}
	}
	return newList;
}
////////////////////////////////////////////////////////////////////////////////
function getAsked(vName)
{	var isAsked = true;
	if(!vName || vName=="")
	{	isAsked=false;
	} else
	{	var sVar = snapVars[vName];
		if (!sVar)
		{	isAsked=true;//?
		}else if(sVar.askedCalc)
		{	isAsked=sVar.askedCalc();
		}
		if (isAsked && sVar && sVar.codeMask)
		{	var numCodes=sVar.numCodes;
			var autoReplies=0;
			if (sVar.autoAnswer)
			{	sVar.doAutoAns = false;
				autoReplies = sVar.autoAnswer;
			}
			var showing = 0;
			for(var i=1;i<=numCodes && (showing <= autoReplies);i++)
			{	var showThisCode=sVar.codeMask(i);
				if (showThisCode)
					showing++;
			}
			if (showing <= autoReplies)
			{	isAsked = false;
				if (autoReplies > 0)
					sVar.doAutoAns = true;
			}
		}
	}
	return isAsked;
}
function asked(vName)
{	if (null == askedCache[vName])
	{	askedCache[vName]=getAsked(vName);
		if (null == askedCache[vName])
		{	askedCache[vName]= true;
		}
	}
	return askedCache[vName];
}
function noreply(vName)
{	var isNR = false;
	if (asked(vName))
	{	if (ans(vName) == "")
		{	isNR = true;
		}
	}
	return isNR;
}
function ans(vName)
{	var answer="";
	if(snapIsOpen(vName))
		answer=snapOpenAns(vName);
	else if(snapIsClosed(vName))
		answer=snapClosedAns(vName);
	return answer;
}
function ansAsNum(vName)
{	return parseFloat(ans(vName));
}
function cVR(vName,value)
{	var result=false;
	if (asked(vName))
	{	var glovar=snapObject(vName);
		if(glovar != null)
		{	var testValue=snapVarValue(vName);
			if((";"+testValue+";").indexOf(";"+value+";")>=0)
			{	result=true;
			}
		}
	}
	return result;
}
function cVV(name1,name2)
{	var result=false;
	if (asked(name1) && asked(name2) && !noreply(name1) && !noreply(name2))
	{	var r1=";"+ans(name1)+";";
		var r2=";"+ans(name2)+";";
		while(r2.length>2&&!result)
		{	var i=r2.substring(1).indexOf(";")+1;
			if(r1.indexOf(r2.substring(0,i+1))>=0)
				result=true;
			else
				r2=r2.substring(i);
		}
	}
	return result;
}
function cVM(vName)//(, ...)
{	var result=false;
	var numArgs = arguments.length;
	if ((1 < numArgs) && asked(vName))
	{	if (snapIsClosed(vName))
		{	if (snapIsDropdown(vName))
			{	var control = snapObject(vName + '_1');
				if (control && (control.options != null))
				{	if (control.type.toLowerCase()=='select-one')
					{	if (control.options[control.selectedIndex] != null)
						{	var val = control.options[control.selectedIndex].value;
							if (val > 0)
							{	for (var i = 1; (i < numArgs) && !result; i++)
								{	result = (val == arguments[i]);
								}
							}
						}
					}else
					{	for (var opt = 0; !result && (opt < control.options.length); opt++)
						{	if (control.options[opt].selected && (control.options[opt].value>0))
							{	var val = control.options[opt].value;
								for (var i = 1; (i < numArgs) && !result; i++)
								{	result = (val == arguments[i]);
								}
							}
						}
					}
				}
			}else
			{	for (var i = 1; (i < numArgs) && !result; i++)
				{	var requiredCode = snapObject(vName + '_' + arguments[i]);
					if ((null != requiredCode) && (null != requiredCode.checked))
					{	result = (requiredCode.checked == true);
					}
				}
			}
		}else
		{	var answer = ans(vName);
			if (answer!="")
			{	answer = ";"+answer+";";
				for (var i = 1; (i < numArgs) && !result; i++)
				{	if (0 <= answer.indexOf(";"+arguments[i]+";"))
					{	result = true;
					}
				}
			}
		}
	}
	return result;
}
function cInRange(value, minValue, maxValue)
{	var result=(value!=null && (maxValue!=NaN) && (minValue!=NaN) && (value>=minValue && value<=maxValue)||(value>=maxValue && value<=minValue));
	return result;
}
function cVarRange(vName,realName,min,max,UiCode)
{	var inRange=false;
	var locVar=snapObject(vName+ "_1");
	if (locVar != null)
	{	var numstr=snapOpenValue(vName);
		if (0 < numstr.length)
		{	var number = parseFloat(numstr);
			if (!isNaN(number) && (number>=min) && (number<=max))
			{	inRange = true;
			}
		}
	}
	return inRange;
}
////////////////////////////////////////////////////////////////////////////////
function codeReply(vName,nameCodes)
{	var reply="";
	var glovar=snapObject(vName);
	if(glovar != null)
	{	var first=true;
		var mValue=glovar.value+";";
		while(mValue.length>1)
		{	var cPos=mValue.indexOf(";");
			if(!first)
			{	if(cPos==mValue.length-1)
					reply+=g_And;
				else
					reply+=", ";
			}
			reply+=nameCodes[parseInt(mValue.substring(0,cPos), 10)-1];
			first=false;
			mValue=mValue.substring(cPos+1);
		}
	}
	if(reply.length==0)
		reply=g_NoReply;
	return reply;
}
//masking
function VarToNum(vName)
{	var result = 0;
	if (snapIsOpen(vName))
	{	var control = snapObject(vName + '_1');
		if (control)
		{	result = parseInt(snapOpenValue(vName), 10);
		}
	}else if (snapIsDropdown(vName))
	{	var control = snapObject(vName + '_1');
		if (control && (control.options != null))
		{	var ok = true;
			for (var x = 0; (x < control.options.length) && ok; x++)
			{	if (control.options[x].selected)
				{	if (0 == result)
					{	result = control.options[x].value;
					}else
					{	result = 0;
						ok = false;
					}
				}
			}
		}
	}else if (snapIsClosed(vName))
	{	var numCodes = snapNumCodes(vName);
		var ok = true;
		for (var x = 1; (x <= numCodes) && ok; x++)
		{	var control = snapObject(vName + '_' + x);
			if (control && control.checked)
			{	if (0 == result)
				{	result = control.value;
				}else
				{	result = 0;
					ok = false;
				}
			}
		}
	}
	if (0 == result)
	{	result = NaN;
	}
	return result;
}
////////////////////////////////////////////////////////////////////////////////
// Date and Time
function FormatDate(Date)
{	var dateString = "";
	if (snapIsUk())
	{	dateString += Date.getDate()+"/";
		dateString += (1 + Date.getMonth());
	} else
	{	dateString += (1 + Date.getMonth())+"/";
		dateString += Date.getDate();
	}
	dateString += "/" + Date.getYear();
	return dateString;
}
function FormatTime(Time)
{	var timeString = "";
	var tempStr = Time.getHours().toString();
	if (tempStr.length == 1)
		tempStr = "0" + tempStr;
	timeString += tempStr + ":";
	tempStr = Time.getMinutes().toString();
	if (tempStr.length == 1)
		tempStr = "0" + tempStr;
	timeString += tempStr + ":";
	tempStr = Time.getSeconds().toString();
	if (tempStr.length == 1)
		tempStr = "0" + tempStr;
	timeString += tempStr;
	return timeString;
}
function GetToday()
{	var Now = new Date();
	var Today = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), 0, 0, 0, 0);
	return Today;
}
function getMonthName(i)
{	var month = "";
	switch (i)
	{
	case 0: month="January";break;
	case 1: month="February";break;
	case 2: month="March";break;
	case 3: month="April";break;
	case 4: month="May";break;
	case 5: month="June";break;
	case 6: month="July";break;
	case 7: month="August";break;
	case 8: month="September";break;
	case 9: month="October";break;
	case 10: month="November";break;
	case 11: month="December";break;
	}
	return month;
}
function GetNow()
{	var Now = new Date();
	Now.setFullYear(1970, 0, 1);
	return Now;
}
////////////////////////////////////////////////////////////////////////////////
function getLabel(labelArray, orderArray, code)
{	return labelArray[orderArray[code]];
}
function getValue(orderArray, code)
{	var val = orderArray[code] + 1;
	var str = "" + val;
	return str;
}
