function SurveyRegexValidator(source, args)
{
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;
	var ctrl = document.getElementById(cid);
	if (ctrl == null)
		return;
	var val = ctrl.value;
	if (val.length == 0)
	{
		args.IsValid = true;
		return;
	}
	
	var regstr = source.getAttribute('regex');
	if (regstr == null)
		return;
	var regex = new RegExp(regstr, "i");
	args.IsValid = regex.test(val);
}

function SurveyRequireOpen(source, args)
{
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;
	var ctrl = document.getElementById(cid);
	if (ctrl == null)
		return;
	args.IsValid = (ctrl.value.length > 0);
}

function SurveyRequireMultipleChoice(source, args)
{
	source.errormessage = source.getAttribute('originalerr');
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;
	
	var ctrls = document.getElementsByName(cid);
	// Check for select box
	if (ctrls.length == 1 && ctrls[0].type == 'select-one')
	{
		args.IsValid = (ctrls[0].selectedIndex > 0) || (ctrls[0].options.length == 1);
		return;
	}
	
	if (ctrls.length == 0)
	{
		args.IsValid = true;
		return;
	}
	
	for (var i=0; i<ctrls.length; i++)
	{
		var ctrl = ctrls[i];
		if (ctrl.checked)
		{
			args.IsValid = true;
			return;
		}
	}
}

function SurveyRequireMultipleResponse(source, args)
{
	source.errormessage = source.getAttribute('originalerr');
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;

	var ctrls = document.getElementsByName(cid);
	if (ctrls.length == 0)
	{
		args.IsValid = true;
		return;
	}

	// Check for select box
	var count = 0;
	if (ctrls.length == 1 && ctrls[0].type == 'select-multiple')
	{
		var len = ctrls[0].options.length;
		if (len == 0)
		{
			args.IsValid = true;
			return;
		}
		for (var i=0; i<len; i++)
			if (ctrls[0].options[i].selected)
				count++;
	} 
	// Check for transfer box
	else if (ctrls.length == 1 && ctrls[0].type == 'hidden')
	{
		if (ctrls[0].value.length == 0)
			count = 0;
		else
			count = ctrls[0].value.split(',').length;
	}
	// Checkboxes	
	else
	{
		for (var i=0; i<ctrls.length; i++)
		{
			var ctrl = ctrls[i];
			if (ctrl.checked)
				count++;
		}
	}
	args.IsValid = count > 0;
}

function SurveyRequireOtherText(source, args)
{
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;
	var cids = cid.split(',');
	if (cids.length != 2)
		return;
	var ctrls = document.getElementsByName(cids[0]);
	var ctrl = null;
	for (var i=0; i<ctrls.length; i++)
	{
		if (ctrls[i].value == -1)
		{
			ctrl = ctrls[i];
			break;
		}
	}
	var textctrl = document.getElementById(cids[1]);
	if (ctrl == null || textctrl == null)
		return;
	args.IsValid = !ctrl.checked || textctrl.value.length > 0;
}

function SurveyMaximumSelection(source, args)
{
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;

	var maxselected = source.getAttribute('maxselected');
	if (maxselected == null || maxselected == 0)
	{
		args.IsValid = true;
		return;
	}

	var ctrls = document.getElementsByName(cid);
	if (ctrls.length == 0)
	{
		args.IsValid = true;
		return;
	}
	
	var count = 0;
	if (ctrls.length == 1 && ctrls[0].type == 'select-multiple')
	{
		var opts = ctrls[0].options;
		for (var i=0; i<opts.length; i++)
			if (opts[i].selected)
				count++;
	}
	else if (ctrls.length == 1 && ctrls[0].type == 'hidden')
	{
		var val = ctrls[0].value
		if (val.length > 0)
			count = val.split(',').length
	}
	else 
	{
		for (var i=0; i<ctrls.length; i++)
		{
			var ctrl = ctrls[i];
			if (ctrl.checked)
				count++;
		}
	}
	args.IsValid = count <= maxselected;
}

function SurveyRequireMatrix(source, args)
{
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;
	var sep = source.getAttribute('separator');
	if (sep == null)
		return;
	/*var answers = source.getAttribute('answers');
	if (answers == null)
		return;*/
	var ctrls = document.getElementsByName(cid);
	var hash = new Array();
	var count = 0;
	//store all checkbox values, grouped per question
	for (var i=0; i<ctrls.length; i++)
	{
		var vals = ctrls[i].value.split(sep);
		if (!hash[vals[0]]) //if it hasn't a checked checkbox yet, try this one
		{
			hash[vals[0]] = ctrls[i].checked;
		}
	}
	//loop again, now checking if every question has a checked checkbox
	for (var i=0; i<ctrls.length; i++)
	{
		var vals = ctrls[i].value.split(sep);
		if (!hash[vals[0]])
		{
			args.IsValid = false;
			return;
		}
	}
	args.IsValid = true;
}

function SurveyRequireScale(source, args)
{
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;
	var cids = cid.split(',');
	var notapp = document.getElementById(cids[cids.length-1]);
	if (notapp != null && notapp.checked)
	{
		args.IsValid = true;
		return;
	}
	var count = 0;
	for (var i=0; i<cids.length-1; i++)
	{
		var ctrls = document.getElementsByName(cids[i]);
		for (var j=0; j<ctrls.length; j++)
		{
			if (ctrls[j].checked)
			{
				count++;
				break;
			}
		}
	}
	args.IsValid = (count == cids.length - 1);
}

function SurveyRequirePriority(source, args)
{
	source.errormessage = source.getAttribute('originalerr');
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;
	if (cid.length == 0)
	{
		args.IsValid = true;
		return;
	}
	var cids = cid.split(',');
	var count = 0;
	var check = {};
	for (var i=0; i<cids.length; i++)
	{
		var ctrls = document.getElementsByName(cids[i]);
		for (var j=0; j<ctrls.length; j++)
		{
			if (ctrls[j].checked)
			{
				if (check[j] != null)
				{
					source.errormessage = source.getAttribute('errsameanswer');
					args.IsValid = false;
					return;
				}
				check[j] = 1;
				count++;
				break;
			}
		}
	}
	args.IsValid = (count == cids.length);
}

function SurveyRequireAttachment(source, args)
{
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;
	var ctrl = document.getElementById(cid);
	if (ctrl == null)
		return;
	args.IsValid = (ctrl.value.length != 0);
}

function SurveyAttachmentCheck(source, args)
{
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;
	var ctrl = document.getElementById(cid);
	if (ctrl == null)
		return;
	var exts = source.getAttribute('extensions');
	var name = ctrl.value
	if (exts == null || name.length == 0)
	{
		args.IsValid = true;
		return;
	}
	
}

function SurveyRequireExpandableGrid(source, args)
{
	args.IsValid = false;
	var cid = source.getAttribute('controlid');
	if (cid == null)
		return;
	var ctrl = document.getElementById(cid);
	if (ctrl == null)
		return;
	args.IsValid = (parseInt(ctrl.value) > 0);
}
