function removeError(element){
	var rowName = "Questions" + element.name.replace('Question', '');
	var elementObj = new getObj( rowName );
	inform( elementObj, 0);
}


function getObj(name){
	if (document.getElementById){
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}else if (document.all){
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}else if (document.layers){
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function inform( element, action ){
	if( action) {
		if (element.obj.className.indexOf('seformerror') == -1) element.obj.className += ' seformerror';
	}else {
		element.obj.className = element.obj.className.substring(0, element.obj.className.lastIndexOf(' '));
	}
}

function verify( thisObject, doNotSubmit ){
	var questionCount = -1;
	var index = 0;
	var Control = new Array;

	while( index < thisObject.length ){
		switch ( thisObject[index].type ){
		case 'radio':
			questionCount++;
			var thisThing = eval("thisObject.Question" + questionCount);
			var theThing = thisThing;
			var thisThingLength = 0;

			try {
				if (thisThing.id == undefined) {
					theThing = eval("thisObject.Question" + questionCount + '[0]');
					thisThingLength = thisThing.length;
				}
			}catch(e) {
				try {
					if (theThing.id.indexOf('seformrequiredfield') > -1 || theThing.id == '') {}
					else {
						theThing = eval("thisObject.Question" + questionCount + '[0]');
						thisThingLength = thisThing.length;
					}
				}catch(e) {
					theThing = eval("thisObject.Question" + questionCount + '[0]');
					thisThingLength = thisThing.length;
				}
			}

			if(theThing.id.indexOf('seformrequiredfield') > -1) {
				for ( var i = 0; i < thisThingLength; i ++){
					if( thisThing[i].checked ) {
						Control[questionCount] = true;
						var element = new getObj( "Questions" + questionCount );
						inform( element, 0);
						break;
					}
				}

				if (thisThingLength == 0 && theThing.checked) {
					Control[questionCount] = true;
					var element = new getObj( "Questions" + questionCount );
					inform( element, 0);
				}

				if ( !Control[questionCount] ) {
					Control[questionCount] = false;
					var element = new getObj( "Questions" + questionCount );
					inform( element, 1);
				}
			}
			while( thisObject[index].name == ("Question" + questionCount))
			index++;
		break;
		case 'checkbox':
			questionCount++;

			var thisThing = eval("thisObject.Question" + questionCount);
			var theThing = thisThing;
			var thisThingLength = 0;

			try {
				if (thisThing.id == undefined) {
					theThing = eval("thisObject.Question" + questionCount + '[0]');
					thisThingLength = thisThing.length;
				}
			}catch(e) {
				try {
					if (theThing.id.indexOf('seformrequiredfield') > -1 || theThing.id == '') {}
					else {
						theThing = eval("thisObject.Question" + questionCount + '[0]');
						thisThingLength = thisThing.length;
					}
				}catch(e) {
					theThing = eval("thisObject.Question" + questionCount + '[0]');
					thisThingLength = thisThing.length;
				}
			}

			if(theThing.id.indexOf('seformrequiredfield') > -1) {
				var testControl = 0;

				for ( var i = 0; i < thisThingLength; i ++){
					if( thisThing[i].checked )
					testControl++;
				}

				if (thisThingLength == 0 && theThing.checked) testControl++;

				var jsAnswerInformation = seform_array[theThing.name];
				var jsAnswerArray = jsAnswerInformation.split(',');
				var jsControl = jsAnswerArray[0];
				var jsAnswerCount = jsAnswerArray[1];

				switch ( jsControl ){
					case 'a':
						if( testControl >= 1) {
							Control[questionCount] = true;
							var element = new getObj( "Questions" + questionCount );
							inform( element, 0);
						}
					break;
					case 'e':
						if( testControl == jsAnswerCount ) {
							Control[questionCount] = true;
							var element = new getObj( "Questions" + questionCount );
							inform( element, 0);
						}
					break;
					case 'l':
						if( testControl >= jsAnswerCount ) {
							Control[questionCount] = true;
							var element = new getObj( "Questions" + questionCount );
							inform( element, 0);
						}
					break;
					case 'm':
						if( ( testControl <= jsAnswerCount ) && ( testControl > 0 ) ) {
							Control[questionCount] = true;
							var element = new getObj( "Questions" + questionCount );
							inform( element, 0);
						}
					break;
				}

				if ( !Control[questionCount] ) {
					Control[questionCount] = false;
					var element = new getObj( "Questions" + questionCount );
					inform( element, 1);
				}
			}
			while( thisObject[index].name == ("Question" + questionCount))
			index++;
		break;
		case 'text':
			questionCount++;
			if(questionCount > 0) {
				var thisThing = eval("thisObject.Question" + questionCount);
				var element = new getObj( "Questions" + questionCount );
				//initially set validation to true
				Control[questionCount] = true;
				inform( element, 0);
				
				//validate each field, and if errors found set to false and trigger inform alert
				if(thisThing.id.indexOf('seformrequiredfield') > -1 && !thisThing.value.length) {
					Control[questionCount] = false;
					inform( element, 1);
				}
				else if (thisThing.id.indexOf('fieldemail') > -1 && !sejs_checkEmail(thisThing.value, false)) {
					Control[questionCount] = false;
					inform( element, 1);
				}
			}
			index++;
		break;
		case 'textarea':
			questionCount++;
			if(eval("thisObject.Question" + questionCount + ".id.indexOf('seformrequiredfield')") > -1) {
				var thisThing = eval("thisObject.Question" + questionCount);
				if( thisThing.value != "" ){
					Control[questionCount] = true;
					var element = new getObj( "Questions" + questionCount );
					inform( element, 0);
				} else {
					Control[questionCount] = false;
					var element = new getObj( "Questions" + questionCount );
					inform( element, 1);
				}
			}
			index++;
		break;
		case 'select-one':
			questionCount++;
			if(eval("thisObject.Question" + questionCount + ".id.indexOf('seformrequiredfield')") > -1) {
				var thisThing = eval("thisObject.Question" + questionCount);

				if( thisThing.options.selectedIndex != 0 ) {
					Control[questionCount] = true;
					var element = new getObj( "Questions" + questionCount );
					inform( element, 0);
				}

				if( !Control[questionCount] ) {
					Control[questionCount] = false;
					var element = new getObj( "Questions" + questionCount );
					inform( element, 1);
				}
			}
			index++;
		break;
		case 'select-multiple':
			questionCount++;
			if(eval("thisObject.Question" + questionCount + ".id.indexOf('seformrequiredfield')") > -1) {
				var thisThing = eval("thisObject.Question" + questionCount);
				var testControl = 0;

				for( var i = 0; i < thisThing.options.length; i++ ){
					if( thisThing.options[i].selected )
					testControl++;
				}

				var theThing = thisThing;
				var jsAnswerInformation = seform_array[theThing.name];
				var jsAnswerArray = jsAnswerInformation.split(',');
				var jsControl = jsAnswerArray[0];
				var jsAnswerCount = jsAnswerArray[1];

				switch ( jsControl ){
					case 'a':
						if( testControl >= 1) {
							Control[questionCount] = true;
							var element = new getObj( "Questions" + questionCount );
							inform( element, 0);
						}
					break;
					case 'e':
						if( testControl == jsAnswerCount ) {
							Control[questionCount] = true;
							var element = new getObj( "Questions" + questionCount );
							inform( element, 0);
						}
					break;
					case 'l':
						if( testControl >= jsAnswerCount ) {
							Control[questionCount] = true;
							var element = new getObj( "Questions" + questionCount );
							inform( element, 0);
						}
					break;
					case 'm':
						if( ( testControl <= jsAnswerCount ) && ( testControl > 0 ) ) {
							Control[questionCount] = true;
							var element = new getObj( "Questions" + questionCount );
							inform( element, 0);
						}
					break;
				}

				if ( !Control[questionCount] ) {
					Control[questionCount] = false;
					var element = new getObj( "Questions" + questionCount );
					inform( element, 1);
				}
			}
			index++;
		break;
		default:
			index++;
		}
	}

	var i = 0;
	for( ; i < Control.length; i ++ ){
		if ( Control[i] == false ) {
			alert("Please complete all highlighted questions accurately.");
			return false;
		}
	}
	if (doNotSubmit) {
		alert('This form cannot be submitted because the form page it belongs to has not been imported into the site.');
		return false;
	}
	return true;
}

function sejs_checkEmail(object_value, required)
{
	//trim whitespace before we validate
    object_value = object_value.replace(/^\s+/,'').replace(/\s+$/,'');

    if( required )
    {
        if( object_value.length == 0 )
        {
            return false;
        }
    }else{
        if( object_value.length == 0 )
        {
            return true;
        }
    }

	return sejs_checkregex(object_value, /^[a-zA-Z_0-9-'\+~]+(\.[a-zA-Z_0-9-'\+~]+)*@([a-zA-Z_0-9-]+\.)+[a-zA-Z]{2,7}$/, required);
}

function sejs_checkregex(object_value, regexPattern, required)
{
    if( required )
    {
        if( object_value.length == 0 )
        {
            return false;
        }
    }else{
        if( object_value.length == 0 )
        {
            return true;
        }
    }

	return regexPattern.test(object_value);
}
