$("html").addClass("js");
var selected_tab = 1;
var selected_interest = null;

$(function() {
	document.title = 'HONBLUE OnSite';

	// enable fields that may be hidden on form submission
	$("#c_industry_val, #c_state_val, #h_company, [id^=q_equipment_], [id^=q_printing_], #submit_btn").attr("disabled", "");

	// prefill company text
	if ($.trim($("#h_company").val()) != '')
		$(".yourcompany").text( $.trim($("#h_company").val()) );

	// prefill state
	if ($.trim($("#c_state_val").val()) != '')
		$("#c_state").val( $.trim($("#c_state_val").val()) );

	// prefill industry
	if ($.trim($("#c_industry_val").val()) != '')
		$("#c_industry").val( $.trim($("#c_industry_val").val()) );

	// advantages tabs
	$(".advantage_btn").bind("click", function() {
		var id = this.id.split('_')[1];
		if (id == selected_tab) return;
		selected_tab = id;
		$(".advantages_text").slideUp();
		$("#advantage_"+id+"_text").slideDown();
		$(".advantage_btn").addClass("inactive");
		$(this).removeClass("inactive");
	});

	// forms options hover
	$("#main_question .answer, #main_question .close").hover(
			function() { $(this).addClass("over"); },
			function() { if (!$(this).hasClass("selected")) $(this).removeClass("over"); }
	);

	// selecting a form option
	$("#main_question .answer").bind("click", function() {
		$("#main_question .answer").removeClass("over selected");
		$(this).addClass("over selected");
		selected_interest = (this.id == 'q_yes') ? 'Yes' : 'No';
		$("#q_interested").val(selected_interest);
		$("#text_1, #text_2, #section_info, #section_questions, #submit_box").hide();
		if (selected_interest == 'Yes') {
			$("#text_1, #section_info, #submit_box").show();
			$("#section_questions").slideDown();
		}
		else {
			$("#text_2, #section_info, #submit_box").show();
			$("#section_questions").slideUp();
		}
		$("#q_close").show();
	});

	// clicking close
	$("#main_question .close").bind("click", function() {
		$("#section_questions").slideUp(function() {
			$("#text_2, #section_info, #submit_box").hide();
			$("#main_question .answer").removeClass("over selected");
		});
		selected_interest = null;
		$("#q_close").hide();
	});

	// field focus
	$("input[type=text], select")
		.focus(function() {
			$(this).addClass("focus");
		})
		.blur(function() {
			$(this).removeClass("focus");
		});

	// form submit
	$("#form").bind("submit", function() {
		$(this).validate(options);
		var valid = $(this).valid();
		if (valid) {
			var arr1 = new Array(), arr2 = new Array();
			// join equipment answers
			$("[id^=q_equipment_]:checked").each(function(i) {
				arr1[i] = this.value;
			});
			$("#q_equipment").val( arr1.join(', ') );
			// join printing answers
			$("[id^=q_printing_]:checked").each(function(i) {
				arr2[i] = this.value;
			});
			$("#q_printing").val( arr2.join(', ') );
			// disable fields we don't want to show up
			$("#c_industry_val, #c_state_val, #h_company, [id^=q_equipment_], [id^=q_printing_], #submit_btn").attr("disabled", "disabled");
		}
		else {
			return false;
		}
	});

	// thank you page
	if ($("#ty_interest").length && $.trim($("#ty_interest").val().toLowerCase()) == 'yes') {
		$("#ty_text_1").show();
		$("#ty_text_2").hide();
	}
	if ($.trim($("#ty_firstname").val()) != '') $("#ty_firstname_text_1, #ty_firstname_text_2").text( ', '+$.trim($("#ty_firstname").val()) );
	if ($.trim($("#ty_salesrep").val()) != '') $("#ty_salesrep_text").text( $.trim($("#ty_salesrep").val()) );
});

$.validator.messages.required = '';
var options = {
	errorContainer: ".warning",
	rules: {
		'c_firstname': { required: true },
		'c_lastname': {	required: true },
		'c_title': { required: true },
		'c_company': { required: true },
		'c_email': { required: true, email: true },
		'c_phone': { required: true },
		'c_address1': { required: true },
		'c_city': { required: true },
		'c_state': { required: true },
		'c_zip': { required: true },
		'c_industry': { required: true }
	}
};

function readCookie(name) {
	var nameEQ = name + '=',
		ca = document.cookie.split(';');
	for (var i=0; i<ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length, c.length);
		}
	}
	return null;
};
