$(document).ready(function() { 
	var emailAddress = "";
	var emailName = "";
});
function setUser(myEmail, myName){
	emailAddress = myEmail;
	emailName = myName;
	$("#name").val("");
	$("#email").val("");
	$("#company").val("");
	$("#message").val("");
	$("#validateTips").val("");
	$("#phone").val("");
	$("#address_1").val("");
	$("#address_2").val("");
	$("#city").val("");
	$("#state").val("");
	$("#zip").val("");
	$("#validateTips").html("All form fields are required.");
	
	$('#contact-form').dialog('open', 'option', 'hide');
}

$.fn.focus_first = function() {
  var elem = $('input:visible', this).get(0);
  var select = $('select:visible', this).get(0);
  if (select && elem) {
    if (select.offsetTop < elem.offsetTop) {
      elem = select;
    }
  }
  var textarea = $('textarea:visible', this).get(0);
  if (textarea && elem) {
    if (textarea.offsetTop < elem.offsetTop) {
      elem = textarea;
    }
  }
  
  if (elem) {
    elem.focus();
  }
  return this;
}


$(function(){
	var name = $("#name");
	var email = $("#email");
	var company = $("#company");
	var message = $("#message");
	var tips = $("#validateTips");
	var phone = $("#phone");
	var address_1 = $("#address_1");
	var address_2 = $("#address_2");
	var city = $("#city");
	var state = $("#state");
	var zip = $("#zip");
	var allFields = $([]).add(name).add(email).add(company).add(message).add(phone).add(address_1).add(address_2).add(city).add(state).add(zip);
	
	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}

	function checkLength(o,n,min,max) {

		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			return true;
		}

	}
	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}

	}

	$("#contact-form").dialog({	
		bgiframe: true,
		autoOpen: false,
		height: 450,
		width:400,
		modal: true,
		zIndex: 99999,
		buttons: {
			'Send Email': function() {
				var bValid = true;
				
				
				allFields.removeClass('ui-state-error');
				bValid = bValid && checkLength(name,"name",3,16);
				bValid = bValid && checkLength(email,"email",3,80);
				bValid = bValid && checkLength(company,"company",2,80);
				
				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");
				
				if(bValid){
					$.post(site_url + "sales/email", {name: name.val(), email: email.val(), company: company.val(), message: message.val(), phone: phone.val(), address_1: address_1.val(), address_2: address_2.val(), city: city.val(), state: state.val(), zip: zip.val(), emailAddress: emailAddress, emailName: emailName},
					  function(data){
					  	//$('#contact-form').html("<h1>Thank You for your email</h1>");
					  	//$('#validateTips').html("<h1>Thank You for your email</h1>");
					  	$('#contact-form').dialog('option', 'title', "Thank You for your email");
					  	$("#name").val("");
						$("#email").val("");
						$("#company").val("");
						$("#message").val("");
						$("#validateTips").val("");
						$("#phone").val("");
						$("#address_1").val("");
						$("#address_2").val("");
						$("#city").val("");
						$("#state").val("");
						$("#zip").val("");
					  	myTimer = $.timer(2000,function(){
					      $('#contact-form').dialog('close');
					    });

					    //$('#contact-form').dialog('close');
					    //$('#thank-you').html("<h1>Thank You for your email</h1>");
					  });
				}				
			},
			Cancel: function() {
				$('#contact-form').dialog('close');
			}
		},
		close: function() {
			//allFields.val('').removeClass('ui-state-error');
		},
		open: function(){
			$('#contact-form').dialog('option', 'title', 'Send Email to ' + emailName);
		}
	});
	
	$('#contact-user').click(function() {
		$('#contact-form').dialog('open');
	})
	.hover(
	function(){ 
		$(this).addClass("ui-state-hover"); 
	},
	function(){ 
		$(this).removeClass("ui-state-hover"); 
	}
	).mousedown(function(){
	$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
		$(this).removeClass("ui-state-active");
	});
});