function fnWordCount(obj,div,count,event) {
	var val_split = obj.value.split(" ");
	if (event.keyCode == 32){
		if (val_split.length >= count){
			return false;
		//}else if(obj.value.charCodeAt(obj.value.length-1)==32){
		//	return false;
		}else if(obj.value.length == 0){
			return false;
		}
	}
	return true;
}

function fnWordCountCheck(obj,div,count){
	var val_split = obj.value.split(" ");
	if (val_split.length > count){
		var x;
		var space_count = 0;
		var new_string = '';
		for (x=0;x<obj.value.length;x++){
			if (obj.value.charCodeAt(x)==32){
				space_count++;
				if (space_count == count){
					obj.value = new_string;
					document.getElementById(div).innerHTML = count;
					return true;
				}
			}
			new_string += obj.value.charAt(x);
		}
	}
	document.getElementById(div).innerHTML = val_split.length;
	return true;
}

function isUnsignedInteger(s) {
	return (s.toString().search(/^[0-9]+$/) == 0);
}
function add_up_x(survey_part_id, name) {
	var qaps = parseInt(document.getElementById(survey_part_id + '_qaps_count').value);
	var x_total = 0;

	for(x=0;x<qaps;x++) {
		var tmp_total = parseInt(document.getElementById(survey_part_id + '_x_' + x).value);
		var tmp_total_2 = document.getElementById(survey_part_id + '_x_' + x).value;

		if(isNaN(tmp_total)) {
			tmp_total = 0;
		}

		if(isUnsignedInteger(tmp_total_2)||tmp_total_2=='') {
			x_total += tmp_total;
		} else {
			alert('Please supply a whole number response for these fields');
			return false;
		}
	}

	document.getElementById('add_to_x_total_' + survey_part_id).innerHTML = x_total;
	document.getElementById('add_to_x_total_2_' + survey_part_id).value = x_total;
	return true;
}

function fnDoSave(){
	if (document.but.next.value != 'Please Wait'){
		if (confirm('By selecting this option the responses you have entered up to this point will be saved\nand you will be e-mailed a link that allows you to continue the survey at a later date.\n\nAre you sure you wish to continue?') == true){
			document.but.next.disabled = true;
			document.but.next.value = 'Please Wait';
			if (document.but.prev){
				document.but.prev.disabled = true;
			}
			document.form.action = 'dosave.php?s=<? echo encrypt($s); ?>';
			document.form.submit();
			return true;
		}
	}
}

function isvalidpath(path) {
	if(path.indexOf(":\\") == 1 && IsNumeric(path.substr(0,1)) == false) {
		// position is correct and first char is not numeric, (windows local drive)
		return true;
	} else if(path.indexOf("\\\\") == 0) {
		// position is correct, \\ found (windows share)
		return true;
	} else if(path.indexOf("/") == 0) {
		// position is correct, / found (linux / mac volume)
		return true;
	}

	return false;
}
function IsEmail(str) {
    // checks for a vaild email
	// returns False for invalid addresses
	// returns True for valid addresses

	// chk length

	var atpos = str.indexOf("@");
	if (str.length < 6){
    	//a@b.c should be the shortest an address could be
		return false;
	}

	// At least one @ and not at front or back
	if (atpos==-1 || atpos==0 || atpos==(str.length-1)){
	   return false;
	}

	// At least one . and not at front or back
	if (str.indexOf(".")==-1 || str.indexOf(".")==0 || str.indexOf(".")==(str.length-1)){
	    return false;
	}

	// Only one @
	if (str.indexOf("@",(atpos+1))!=-1){
	    return false;
	}

	var before = "1234567890qwertyuiopasdfghjklzxcvbnm+.@-_'QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < atpos ;i++){
		if(before.indexOf(str.charAt(i))<0){
			return false;
		}
	}

	var after = "1234567890qwertyuiopasdfghjklzxcvbnm.-QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=atpos+1; i < str.length ;i++){
		if(after.indexOf(str.charAt(i))<0){
			return false;
		}
	}

	var last_dot_pos=str.length-str.lastIndexOf(".")-1;
	if (last_dot_pos>4 || last_dot_pos<2){
		return false;
	}
	return true;
}
function IsNumeric(str){
	var strValidChars = "0123456789";
	var strChar;
	var found_decimal_place=false;

	if (str.length == 0) return false;

	for (i = 0; i < str.length; i++){
		strChar = str.charAt(i);
		if (strChar=='.'){
			if (found_decimal_place==false){
				found_decimal_place=true;
			}else{
				return false;
			}
		}else if (strChar=='-'){
			if (i!=0) return false;
		}else if (strValidChars.indexOf(strChar) == -1){
			return false;
		}
	}
	return true;
}
function checkExclusive(field){
	var name_array;
	var name_array2;
	var type;

	if (eval('document.form.' + field + '.className == "exclusive"')){
		type = 'Uncheck All';
	}else{
		type = 'Uncheck Exclusive';
	}

	name_array = field.split('_');
	for (x=0;x<document.form.length;x++){
		if (document.form[x].name == field + '_other'){
			if (eval('document.form.' + field + '.checked == true')){
				document.form[x].disabled = false;
			}else{
				document.form[x].value = '';
				document.form[x].disabled = true;
			}
		}else{
			name_array2 = document.form[x].name.split('_');
			if (name_array2[4] != 'other' && name_array[2] == name_array2[2] && document.form[x].name!=field){
				if (type == 'Uncheck All'){
					document.form[x].checked = false
					
					if (!eval('!document.form.' + document.form[x].name + '_other')){
						eval('document.form.' + document.form[x].name + '_other.disabled = true');
						eval('document.form.' + document.form[x].name + '_other.value = ""');
					}
				}else{
					if (document.form[x].className == 'exclusive'){
						document.form[x].checked = false
						if (!eval('!document.form.' + document.form[x].name + '_other')){
							eval('document.form.' + document.form[x].name + '_other.disabled = true');
							eval('document.form.' + document.form[x].name + '_other.value = ""');
						}
					}
				}
			}
		}
	}
}
function enableTextbox(field,selected){
	eval('var rb_length = document.form.' + field + '.length');
	for (x=0;x<rb_length;x++){
		if (eval('document.form.' + field + '_' + x + '_other')){
			if (x==selected){
				eval('document.form.' + field + '_' + x + '_other.disabled = false');
			}else{
				eval('document.form.' + field + '_' + x + '_other.value = \'\'');
				eval('document.form.' + field + '_' + x + '_other.disabled = true');
			}
		}
	}
}