var LF = LF || {};

LF.contact = LF.contact || {};

LF.contact.resetContactForm = function () {
	$('#basic-form').hide();
	$('#frmContact').css('height', 175);
	$('label.comments').hide();
	$('textarea#comments').hide();
	$('#category_span').hide();
	$('label.category').hide();
	$('#category_span select').val('');
	$('#category_span span').text('Choose an option');
	$('p#region').hide();
	$('span.region').hide();
	$('label.region').hide();
	
	$('label.volume').hide();
	$('input#volume').hide();
	$('label.current_supplier').hide();
	$('input#current_supplier').hide();
	$('label.products').hide();
	$('span#products_span').hide();
	$('#products_span select').val('');
	$('#products_span span').text('Choose an option');
	$('label.business').hide();
	$('span#business_span').hide();
	$('#business_span select').val('');
	$('#business_span span').text('Choose an option');
	$('label.country').hide();
	$('span#country_span').hide();
	$('#country_span select').val('');
	$('#country_span span').text('Choose an option');
	
	$('label span').hide();
	$('input').removeClass('error');
	
	LF.util.openThenAdjustPopup($('#contact_modal'));
};

LF.contact.contactCategoryChange = function () {
	$('#frmContact').css('height', 175);
	$('#basic-form').hide();
	$('span.region').removeClass('checked').children('input').attr('checked', false);
	$('label.volume').hide();
	$('input#volume').hide();
	$('label.current_supplier').hide();
	$('input#current_supplier').hide();
	$('label.products').hide();
	$('span#products_span').hide();
	$('#products_span select').val('');
	$('#products_span span').text('Choose an option');
	$('label.business').hide();
	$('span#business_span').hide();
	$('#business_span select').val('');
	$('#business_span span').text('Choose an option');
	$('label.country').hide();
	$('span#country_span').hide();
	$('#country_span select').val('');
	$('#country_span span').text('Choose an option');
	$('p#region').css('display', 'block');
	$('span.region').css('display', 'block');
	$('label.region').css('display', 'block');
	
	LF.util.openThenAdjustPopup($('#contact_modal'));
};

LF.contact.regionChange = function () {
	$('label.products').hide();
	$('span#products_span').hide();
	$('#products_span select').val('');
	$('#products_span span').text('Choose an option');
	$('label.business').hide();
	$('span#business_span').hide();
	$('#business_span select').val('');
	$('#business_span span').text('Choose an option');
	$('label.country').hide();
	$('span#country_span').hide();
	$('#country_span select').val('');
	$('#country_span span').text('Choose an option');
	
	$('#basic-form').show();
	$('label.volume').show();
	$('input#volume').show();
	$('label.current_supplier').show();
	$('input#current_supplier').show();
	$('#frmContact').css('height', 300);
	LF.util.openThenAdjustPopup($('#contact_modal'));
};

LF.contact.setupContact = function () {
	$('#email_us_span select').val('');
	$('#email_us_span span').text('Choose an option');
	
	$('#email_us').change(function() {
		$('#form_response').hide();
		if ($(this).val() == 'Media' || $(this).val() == 'Careers' || $(this).val() == 'General' || $(this).val() == 'Current') {
			LF.contact.resetContactForm();
			$('#basic-form').show();
			$('#frmContact').css('height', 250);
			$('label.comments').show().removeClass('sales');
			$('textarea#comments').show();
			LF.util.openThenAdjustPopup($('#contact_modal'));
		} else if ($(this).val() == 'Sales') {
			LF.contact.resetContactForm();
			$('label.category').show();
			$('#category_span').show();
			$('label.comments').show().addClass('sales');
			$('textarea#comments').show();
		} else if ($(this).val() == '') {
			LF.contact.resetContactForm();
		}
	});
	
	$('#category_span select').val('');
	$('#category_span span').text('Choose an option');
	
	$('#category').change(function() {
		LF.contact.contactCategoryChange();	
		
		if ($(this).val() == 'Cheese') {
			$('#domestic_span').click(function() {
				LF.contact.regionChange();
				$('label.business').show();
				$('span#business_span').show();
				$('#frmContact').css('height', 375);
				LF.util.openThenAdjustPopup($('#contact_modal'));
			});
			$('#international_span').click(function() {
				LF.contact.regionChange();
				$('label.country').show();
				$('span#country_span').show();
				$('#frmContact').css('height', 375);
				LF.util.openThenAdjustPopup($('#contact_modal'));
			});
		} else if ($(this).val() == 'Dairy') {
			$('#domestic_span,#international_span').click(function() {
				LF.contact.regionChange();
				$('label.products').show();
				$('span#products_span').show();
				$('#frmContact').css('height', 375);
				LF.util.openThenAdjustPopup($('#contact_modal'));
			});
		}
	});
};

LF.contact.sendEmail = function () {
	$.ajax({
		type: 'Post',
		url: '/contact.php',
		data: { 'data': JSON.stringify( $('#frmContact').serializeJSON() ) },
		dataType: 'json',
		success: function (data) {
			log(data);
			LF.contact.resetContactForm();
			$('#email_us_span select').val('');
			$('#email_us_span span').text('Choose an option');
			$('#form_response').text(data.message).fadeIn('500');
		},
		error: function (err) {
			log(err);
		}
	});
};

LF.contact.setupContactUi = function () {
	$('#contact_submit').click(function () {
		isContactValid = true;
		if ($('#email_us').val() == 'Sales') {
			if ($('#category').val() == 'Cheese') {
				if ($('input[name=region]:checked', '#frmContact').val() == 'Domestic') {
					if ($('#business').val() == '' || $('#business').val() == null) {
						$('#business_span').prev('label').find('span').show();
						$('#business_span').addClass('error');
						isContactValid = false;
					} else {
						$('#business_span').prev('label').find('span').hide();
						$('#business_span').removeClass('error');
					}
				}
				if ($('input[name=region]:checked', '#frmContact').val() == 'International') {
					if ($('#country').val() == '' || $('#country').val() == null) {
						$('#country_span').prev('label').find('span').show();
						$('#country_span').addClass('error');
						isContactValid = false;
					} else {
						$('#country_span').prev('label').find('span').hide();
						$('#country_span').removeClass('error');
					}
				}
			}
			if ($('#category').val() == 'Dairy') {
				if ($('#products').val() == '' || $('#products').val() == null) {
					$('#products_span').prev('label').find('span').show();
					$('#products_span').addClass('error');
					isContactValid = false;
				} else {
					$('#products_span').prev('label').find('span').hide();
					$('#products_span').removeClass('error');
				}
			}
		}
		
		if ($('#user_name').val() == '' || $('#user_name').val() == null) {
			$('#user_name').prev('label').find('span').show();
			$('#user_name').addClass('error');
			isContactValid = false;
		} else {
			$('#user_name').prev('label').find('span').hide();
			$('#user_name').removeClass('error');
		}
		if ($('#company_name').val() == '' || $('#company_name').val() == null) {
			$('#company_name').prev('label').find('span').show();
			$('#company_name').addClass('error');
			isContactValid = false;
		} else {
			$('#company_name').prev('label').find('span').hide();
			$('#company_name').removeClass('error');
		}
		if ($('#email').val() == '' || $('#email').val() == null) {
			$('#email').prev('label').find('span').show();
			$('#email').addClass('error');
			isContactValid = false;
		} else {
			$('#email').prev('label').find('span').hide();
			$('#email').removeClass('error');
		}
		
		if (isContactValid) {
			LF.contact.sendEmail();
		}
		return false;									 
	});
};

LF.contact.init = function () {
	LF.contact.setupContact();
	LF.contact.setupContactUi();
};

LF.global.domReadyLoad(LF.contact.init);

