jQuery(document).ready(function(){
	jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
	    phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	});
	
	$.extend($.modal.defaults, {
		escClose: true,
		opacity: 80,
		overlayClose: true,
		overlayCss: {backgroundColor:"#000"},
		minHeight: 450	
	});
	
	setValidate();
	$("#btnSubmit").click(function(){
		$("#frmContact").submit();		
	});

});

/*
function showContactModal(){
	
	$.get("/contact/widget", function(data){
		$(data).modal({
			containerId: 'simplemodal-container-large',
			closeHTML: "<a href='#' title='Close' class='simplemodal-close'><img src=\"/img/modal-close.png\" /></a>",				
			onOpen: function (dialog) {
				$(".simplemodal-close").hide();
				dialog.overlay.fadeIn('fast', function () {
					dialog.data.hide();
					dialog.container.fadeIn('fast', function () {
						dialog.data.fadeIn('fast', function(){
							$(".simplemodal-close").fadeIn('slow');
						});
					});
				});
			},
			onShow: function (dialog){
				setValidate();
				$("#btnSubmit").click(function(){
					$("#frmContact").submit();		
				});
			},
			onClose: function (dialog) {
				dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(200, function () {
						dialog.overlay.fadeOut(200, function () {
							$.modal.close();
						});
					});
				});
			}
		});
	});		
}
*/

function setValidate(){
	
	$("#frmContact").validate({
   		messages: {
			message:{
				required: "Please enter a message"
			},
			name:{
				required: "Please specify your name"
			},
			email: {
				required: "Please enter a valid email address",
			  	email: "Valid email example: name@domain.com"
			},
			phone:{
				required: "Please supply your phone number",
				phoneUS: "Valid phone example: 212-555-1234"
			}
		},
		rules: {
			name: {
				required: true
			},
			email: {
				required: true,
				email: true
			},
			message: {
				required: true
			},
			phone:{
				required: true,
				phoneUS: true
			}
		},
		submitHandler: function(form){
			submitContactForm(); 
		},		
		//errorLabelContainer: '#errors ul',
		highlight: function(element, errorClass) {
	     	$(element).parent("span").addClass("invalid");
	     	$(element).parent("td").addClass("invalid");
	     	$(element).parent("td").prev("td").children("p").addClass("invalid");
	  	},
		unhighlight: function(element, errorClass) {
	     	$(element).parent("span").removeClass("invalid");
	     	$(element).parent("td").removeClass("invalid");
	     	$(element).parent("td").prev("td").children("p").removeClass("invalid");
	  	},
		wrapper: "p",
		/*
		invalidHandler: function(form,validator){			
			$("#messageForm").fadeOut("fast", function(){
				$("#errors").fadeIn("errors");
			});
		}
		*/
	});

}

function tryAgain(){
	$("#errors").fadeOut("fast", function(){
		$("#messageForm").fadeIn("errors");
	});
}

function submitContactForm(){

	//all good if here... hide the form
	$("#btnSubmit").hide();
	$("#imgSpinner").toggle();

	var post_data = new Object();	
	var interest = new Array();
	
	post_data.name = $("#name").attr("value");
	post_data.email = $("#email").attr("value");
	post_data.company = $("#company").attr("value");
	post_data.phone = $("#phone").attr("value");
	post_data.message = $("#message").attr("value");
		
	if($("#electronics-recycling").attr('checked')){
		interest.push($("#electronics-recycling").attr('value'));
	}
	if($("#data-destruction").attr('checked')){
		interest.push($("#data-destruction").attr('value'));
	}
	if($("#asset").attr('checked')){
		interest.push($("#asset").attr('value'));
	}
	if($("#inks").attr('checked')){
		interest.push($("#inks").attr('value'));
	}
	if($("#replacement-parts").attr('checked')){
		interest.push($("#replacement-parts").attr('value'));
	}
	if($("#print-services").attr('checked')){
		interest.push($("#print-services").attr('value'));
	}
		
	post_data.interest = interest.join(",");
	
	$.post("/contacts/add", post_data, contactFormResults, "json");
}

function contactFormResults(result){

	if(result.error){
		
		alert(result.error);
		$("#imgSpinner").toggle();
		$("#btnSubmit").show();
		
	}else if(result.success){
		
		$("#imgSpinner").toggle();

		$("#widget-instructions").hide();
		$("#messageForm").hide();
		$("#messageSuccess").fadeIn("slow");				
				
	}
}
