var submitcount=0;

function checkSubmit() {
	if (submitcount == 0) {
		submitcount++;
		document.Surv.submit();
	}
}

function wordCounter(field, countfield, maxlimit) {
	wordcounter=0;
	for (x=0;x<field.value.length;x++) {
		if (field.value.charAt(x) == " " && field.value.charAt(x-1) != " ") {
			wordcounter++;
		}  // Counts the spaces while ignoring double spaces, usually one in between each word.
		if (wordcounter > 20) {
			field.value = field.value.substring(0, x);
		}
		else {
			countfield.value = maxlimit - wordcounter;
		}
	}
}

var firstErrorField = '';

function checkForm(form) {
	var alertText = '';
	firstErrorField = '';
	
	if (!form.name.value) {
		alertText += "Please enter your name.\n";
		checkFirstErrorField('name');
	}	
	if (!form.phone.value) {
		alertText += "Please enter a phone number where we can contact you.\n";
		checkFirstErrorField('phone');
	}
	if (!form.email.value) {
		alertText += "Please enter your email address.\n";
		checkFirstErrorField('email');
	}
	else {
		if (!(emailCheck(form.email.value))) {
			alertText += "Please enter a valid email address.\n";
			checkFirstErrorField('email');
		}
	}
	if (!form.title.value) {
		alertText += "Please enter a title for your listing.\n";
		checkFirstErrorField('title');
	}
	if (!form.desc.value) {
		alertText += "Please enter a description and price for your item(s).\n";
		checkFirstErrorField('desc');
	}
	
	if (!form.captchaCode.value) {
		alertText += "To help prevent spam posts, please type the characters you see in the graphic.";
		checkFirstErrorField('captchaCode');
	}
	else if (form.captchaCode.value != captcha) {
		alertText += "The security code you typed in is incorrect.";
		checkFirstErrorField('captchaCode');
	}
		
	if (alertText != '') {
		alert(alertText);
		form[firstErrorField].focus();
		form[firstErrorField].select();
		return false;
	}
	else {
		form.submit();
	}
}

