// submit.js - quick submit form validator
function qsValidateFields(){
	try {
		var x;
		
		// check ip address
		if ( tcGetVal('ipaddress')==''){
			alert("Your computer IP address is not publishing properly. Try another computer. We require and record IP address for legal purposes and to prevent system failure.");
			return(false);
		}
		
		// check the URL
		var x = Trim(tcGetVal('txtUrl'));
		tcSetVal('txtUrl', x);
		if (! x.match(/http:\/\/\S+\.\S+$/)) {
			alert("Please enter a valid URL starting with http://");
			return(false);
		}
		if ( seContainsMarkup(x) ){
			alert('Please enter a valid URL that does not contain markup tags. It must start with http://');
			return(false);
		}
		
		
		x = Trim(tcGetVal('txtFname'));
		tcSetVal('txtFname', x);
		if ( ! x ) {
			alert("Please fill in the First Name field with your first name.");
			return(false);
		}
		if ( seContainsMarkup(x) ){
			alert('Please remove any HTML markup tags (<>) from all input fields. Check the First Name field.');
			return(false);
		}
		
		x = Trim(tcGetVal('txtLname'));
		tcSetVal('txtLname', x);
		if ( ! x ){
			alert("Please fill in the Last Name field with your last name (surname).");
			return(false);
		}
		if ( seContainsMarkup(x) ){
			alert('Please remove any HTML markup tags (<>) from all input fields. Check the Last Name field.');
			return(false);
		}
		
		x = Trim(tcGetVal('txtEmail'));
		tcSetVal('txtEmail', x);
		if ( ! seValidateEmail(x) ){
			alert('Please fill in e-mail address with a valid e-mail address. Remove any (<>) characters.');
			return(false);
		}
		if ( seContainsMarkup(x) ){
			alert('Please remove any HTML markup tags (<>) from all input fields. Check the First Name E-mail Address field.');
			return(false);
		}
		
		// Now we check the options fields here for HTML markup
		x = Trim(tcGetVal('txtPhone'));
		tcSetVal('txtPhone', x)
		if ( seContainsMarkup(x) ){
			alert('Please remove any HTML markup tags (<>) from all input fields. Check the Phone Number field.');
			return(false);
		}
		
		x = Trim(tcGetVal('txtPhoneWhen'));
		tcSetVal('txtPhoneWhen', x)
		if ( seContainsMarkup(x) ){
			alert('Please remove any HTML markup tags (<>) from all input fields. Check the Best Time to Call field.');
			return(false);
		}
		
		x = Trim(tcGetVal('txtComments1'));
		tcSetVal('txtComments1', x)
		if ( seContainsMarkup(x) ){
			alert('Please remove any HTML markup tags (<>) from all input fields. Check the Comments field.');
			return(false);
		}
		
		x = Trim(tcGetVal('txtComments2'));
		tcSetVal('txtComments2', x)
		if ( seContainsMarkup(x) ){
			alert('Please remove any HTML markup tags (<>) from all input fields. Check the Where You Heard About Us field.');
			return(false);
		}
		
		// check terms and conditions
		if (! tcIsChecked('terms')){
			alert("Please check the accept terms and conditions checkbox to proceed.");
			return(false);
		}
		
		// check challenge code match
		var x = Trim(tcGetVal('turing1'));
		tcSetVal('turing1', x);
		if (! x ){
			alert("Please fill in the challenge code");
			return(false);
		}
		
		// check challenge code match
		if ( tcGetVal('turing0') != tcGetVal('turing1') ){
			alert("Please fill in the challenge code properly. They do not match!");
			return(false);
		}
		
		// check challenge code match
		if ( tcGetVal('magic') == '') {
			alert("Security error has occurred. Please try another browser or computer to initiate your submission.");
			return(false);
		}
		
		// final confirm - forces a human to answer!
		if ( confirm("Please confirm you would like to submit your website now! Thank you!") ){
			tcSetVal('action','capture');
			return(true);
		} else {
			tcSetVal('action','');
			return(false);
		}
		
	} catch(e) {
		alert('Error in script: ' + e.name + '/' + e.message);
		return(false);
	}
}

// setup timer to monitor field lenghts

// txtComments1 -> divComment1CharsLeft
// txtComments2 -> divComment2CharsLeft

function qsUpdateCharCounters(){
	try {
		// Count the comments field.
		var maxChars = 500;
		var field = 'txtComments1';
		var divRem = 'divComment1CharsLeft';
		var c = tcGetVal(field);
		var len = c.length;
		var remaining = maxChars - len;
		document.getElementById(divRem).innerHTML = remaining;
		if (remaining < 0) {
			c = c.substr(0, maxChars);
			document.getElementById(field).value = c;
			remaining = 0;
			document.getElementById(divRem).innerHTML = remaining;
			alert('You have reached your message length limit of ' + maxChars + ' characters and have 0 characters remaining.');
		}
		
		// Count the comments2 field.
		var maxChars = 200;
		var field = 'txtComments2';
		var divRem = 'divComment2CharsLeft';
		var c = tcGetVal(field);
		var len = c.length;
		var remaining = maxChars - len;
		document.getElementById(divRem).innerHTML = remaining;
		if (remaining < 0) {
			c = c.substr(0, maxChars);
			document.getElementById(field).value = c;
			remaining = 0;
			document.getElementById(divRem).innerHTML = remaining;
			alert('You have reached your message length limit of ' + maxChars + ' characters and have 0 characters remaining.');
		}
		
		setTimeout('qsUpdateCharCounters()', 333); 
		
	} catch(e) {
		alert('Error in script 	qsUpdateCharCounters(): ' + e.message + '/' + e.name);
		return(false);
	}
}

// run myInitPage onload
function myInitPage(){
	try {
		var step = 1;
		qsUpdateCharCounters();
		
 	} catch(e) {
		alert("Error occurred during myInitPage() at step: " + step);
	}	
}

tcAddOnLoadEvent(myInitPage);

// alert('loaded at ' + new Date());
