//////////////
// Settings //
//////////////

var AJAXdomain = window.location.protocol + '//' + window.location.hostname + '/';
var AJAXSSLDOMAIN = window.location.protocol + '//' + location.hostname + '/';

var thisElement = '';
var errClr = "textError";
var error = '';

function checkdealers() {
	if ($('#zip').val() != '') {

		if ( !($('#zip').val() > 0 && $('#zip').val() < 100000) ) {
			$('#errorBox').html('<p>That zip code is just wrong.</p>');
		} else {

			if ($('#distance').val() == '') {
				$('#errorBox').html('<p>We need a radius for your area.</p>');
			} else {

				$('#errorBox').html('');
				$('#dealer').submit();

			}

		}

	} else {

		if ($('#city').val() == '' && $('#state').val() == '' && $('#country').val() == '') {
			$('#errorBox').html('<p>A country, state, city or zip code is needed.</p>');
		} else {

			$('#errorBox').html('');
			$('#dealer').submit();

		}

	}
}

function contactPost() {
	error = '';

	if ($('#questionType').val() == '') {
		error += '<p class="error">Select a department.</p>';
	}
	if ($('#name').val() == '') {
		error += '<p class="error">Enter your name.</p>';
	}
	if ($('#email').val() == '') {
		error += '<p class="error">Enter an email address.</p>';
	}
	if ( !verifyEmailFormat($('#email').val())) {
		error += '<p class="error">Your email address is not straight.</p>';
	}


	if (error == '') {
		$('#errorContainer').html("");

		var myval = 0;
		if ($('#f_mailinglist:checked').length == 1)
		{
			myval = 1;
		}

		var url = AJAXdomain + 'contact/ajaxcontact/';
		$('#resultContainer').load(url, {
			questionType: $('#questionType').val(),
			name: $('#name').val(),
			email: $('#email').val(),
			phone: $('#phone').val(),
			company: $('#company').val(),
			comments: $('#comments').val(),
			mailinglist: myval
		});
	} else {
		$('#errorContainer').html(error);
	}
}


// Load A Page W/ Ajax
function getPage(url, pars, div, thisMethod) {
	if (!thisMethod) thisMethod = 'get';
	var myAjax = new Ajax.Updater( div , url, { method: thisMethod, parameters: pars });
}


// Error Check Secondary Scripts
function checkField(field, value, errorMessage) {
	if (field == value) {
		error += errorMessage;
		highlightErrorField(field);
	} else deHighlightErrorField(field);

}

//function highlightErrorField(field) {
	//$('#field').attr({class: 'errClr'});
//}
//function deHighlightErrorField(field) {
	//$('#field').attr({class: 'text'});
//}

// Scotty, the old cold used in this file does not work in Safari. The new code above does. Everything seems to work fine with the code below activated.
function highlightErrorField(field) {
	$(field).className = errClr;	
}
function deHighlightErrorField(field) {
	$(field).className = 'text';	
}

function isValidEmail(str) {
  verifyEmailFormat(str);
}

function verifyEmailFormat(str) {
	if (str == '') return true;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		return true;
	else
		return false;
}
