//////////////
// 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(tmp) {
	$('#search_type').val(tmp);
	$('.rem').removeClass('alert').html('');

	var err = "";
	var type = "";
	if(tmp == "is_dealer")
	{
		type = "deal_";
	}
	if(tmp == "is_413_dealer")
	{
		type = "413_";
	}
	if(tmp == "is_distributor")
	{
		type = "dist_";
	}

	if (tmp != "is_distributor")
	{
		//if ($('#'+type+'zip').val() != '')
		//{
			//if ( !($('#'+type+'zip').val() > 0 && $('#'+type+'zip').val() < 100000) )
			//{
			//	err = 'That zip code is just wrong.';
			//}
			//else if ($('#'+type+'distance').val() == '')
			//{
			//	err = 'We need a radius for your area.';
			//}
		//}
		//else if ($('#'+type+'city').val() == '' && $('#'+type+'state').val() == '' && $('#'+type+'country').val() == '')
		 if ($('#'+type+'city').val() == '' && $('#'+type+'state').val() == '' && $('#'+type+'country').val() == '')
		{
			err = 'A city, state or country is needed.';
		}
	}
	else if ($('#'+type+'country').val() == '')
	{
		err = "A country is needed.";
	}


	if (err != "")
	{
		$('#'+type+'err').html(err).addClass('alert');
		return false;
	}
	else
	{
		$('#dealer-search').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) {
	  	$('#'+div).load(url+"?"+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;
}
