/*  File: newsletter.js     */
/*  By:   Jeremy Tredway    */
/*  Ver:  2008-03-02        */

/********************************
  the following routines require 
    jquery.js
*********************************/

function checkName(val) {
	var regex = /^([ \u00c0-\u01ffa-zA-Z'])+$/;
	if (regex.test(val)) { return true; }
	else { return false; }
}

function checkEmail(val) {
	var regex = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*/;
	if (regex.test(val)) { return true; }
	else { return false; }
}

function checkDigit(val) {
	var regex = /^[0-9]{1,6}$/;
	if (regex.test(val)) { return true; }
	else { return false; }
}

function checkAddress(val) {
	var regex = /^[a-zA-Z0-9ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ\.\,\-\/\']+[a-zA-Z0-9ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ\.\,\-\/\' ]+$/;
	if (regex.test(val)) { return true; }
	else { return false; }
}

function checkPhone(val) {
	var regex = /((\(\d{3}\) ?)|(\d{3}[- \.]))?\d{3}[- \.]\d{4}(\s((x\.?|ext\.?|xt\.?)\s*\d+)?){0,1}$/;
	if (regex.test(val)) { return true; }
	else { return false; }
}

function checkZip(val) {
	var regex = /^\d{5}(-\d{4})?$/;
	if (regex.test(val)) { return true; }
	else { return false; }
}

function checkXlinkdig(val) {
	var regex = /^[0-7#]+$/;
	if (regex.test(val)) { return true; }
	else { return false; }
}

// onload routines
$(function(){

	// form validation
	$('.std_form #submit').click(function() {
		var submit = true;
		var this_form = '#' + $(this).parents('form').attr('id');
		// reset errors
		$(this_form).find('.error').hide();
		// check for errors
		$(this_form).find('.required').each(function() {
			if ($(this).val() == '') {
				submit = false;
				$(this).next('.error').fadeIn(400);
			}
			if ($(this).attr('name') == 'first_name') {
				check_firstN = checkName($(this).val());
				if (!check_firstN) {
					submit = false;
					$(this).next('.error').fadeIn(400);
				}
			}
			if ($(this).attr('name') == 'first_last') {
				check_lastN = checkName($(this).val());
				if (!check_lastN) {
					submit = false;
					$(this).next('.error').fadeIn(400);
				}
			}
			if (/email/.test( $(this).attr('name') ) ) {
				check_email = checkEmail($(this).val());
				if (!check_email) {
					submit = false;
					$(this).next('.error').fadeIn(400);
				}
			}
			if ($(this).attr('name') == 'phone') {
				check_phone = checkPhone($(this).val());
				if (!check_phone) {
					submit = false;
					$(this).next('.error').fadeIn(400);
				}
			}
			if ($(this).attr('name') == 'street') {
				check_address = checkAddress($(this).val());
				if (!check_address) {
					submit = false;
					$(this).next('.error').fadeIn(400);
				}
			}
			if ($(this).attr('name') == 'city') {
				check_city = checkName($(this).val());
				if (!check_city) {
					submit = false;
					$(this).next('.error').fadeIn(400);
				}
			}
			if ($(this).attr('name') == 'zip') {
				check_zip = checkZip($(this).val());
				if (!check_zip) {
					submit = false;
					$(this).next('.error').fadeIn(400);
				}
			}
			if ($(this).attr('id') == 'agent_name') {
				check_agent = ( ('#agent').val=='no' || checkName( $(this).val() ) );
				if (!check_agent) {
					submit = false;
					$(this).next('.error').fadeIn(400);
				}
			}
			if ($(this).attr('id') == 'cust_name') {
				check_agent = ( ('#cust').val=='no' || checkXlinkdig( $(this).val() ) );
				if (!check_agent) {
					submit = false;
					$(this).next('.error').fadeIn(400);
				}
			}
		});
		return submit;
	});

	// clear error messages upon blur
	$('.std_form .required').blur(function () {
		if ($(this).val() != '') {
			$(this).next('.error').fadeOut(400);
		}
	});

});

