// on ready
$(document).ready(function() {
	// init!
	LandingPage.init();
});

var LandingPage = {
	
	// load any info from cookie if possible
	checkCookie: function() {
		var cookie_validate = false;
	    $("#entry-form :input").each(function() {
	        var object = this.name;
	        value = Malikot.readCookie(object);
	        if (value && "string" == typeof value) {
	            $("input[name='" + object + "']").val(value);
                cookie_validate = true;
            }
        });
		if (cookie_validate) LandingPage.final_validate(false);
	},
	
	// debug
	debug: function(message) {
		if (typeof(console) !== 'undefined' && console != null) console.log(message);
	},
	
	// init
	init: function() {
		// check cookie
		LandingPage.checkCookie();
		// add validation classes, add event triggers
		$('input[name=firstname], input[name=lastname]').addClass('validate_text').blur(function() { LandingPage.validate($(this), 'text'); });
		$('input[name=email]').addClass('validate_email').blur(function() { LandingPage.validate($(this), 'email'); });
		$('input[name^=phone]').addClass('validate_phone').blur(function() { $(this).addClass('touched'); LandingPage.validate($(this), 'phone'); });
		$('input[name=zip_code]').addClass('validate_zip').blur(function() { LandingPage.validate($(this), 'zip'); });
		// form on submit
		$('#entry-form').bind('submit', LandingPage.submit);
	},
	
	// log to google analytics
	log: function(category, action, label) {
		if (window.pageTracker) {
			domain = window.location.href;
			pageTracker._trackEvent(category, action, label);
		}
	},
	
	// submit
	submit: function() {
		// final validation
		field_count = 0;
		valid_count = 0;
		// check em
		$(this).find('.validate_email').each(function() {
			field_count++;
			var valid = LandingPage.validate($(this), 'email');
			if (valid) valid_count++;
		});
		$(this).find('.validate_phone').each(function() {
			field_count++;
			var valid = LandingPage.validate($(this), 'phone', true);
			if (valid) valid_count++;
		});
		$(this).find('.validate_text').each(function() {
			field_count++;
			var valid = LandingPage.validate($(this), 'text');
			if (valid) valid_count++;
		});
		$(this).find('.validate_zip').each(function() {
			field_count++;
			var valid = LandingPage.validate($(this), 'zip');
			if (valid) valid_count++;
		});
		// check experience (little different flow)
		var experience = $('input[name=experience]:checked').length;
		// if experience checked, remove warning
		if (experience) $('#experience-warning').remove();
		// if no experience checked
		else if (!$('#experience-warning').length) $('.experience-questions').prepend('<strong id="experience-warning" style="color: red;">Please Select An Option:<br></strong>');
		// if not all valid
		if (valid_count != field_count || !experience) {
			// notify
			alert('Please correct the form errors.');
			// and stop
			return false;
		}
		// and let the form do its thang
		return true;
	},
	
	// validate
	validate: function($field, type, no_ignore) {
		// validation type
		switch(type) {
			// email
			case 'email':
				// the value
				var value = $field.attr('value');
				// match it
				var valid = value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
				// done and done
				break;
			// phone
			case 'phone':
				// combine-a-nator!
				var field_count = 0;
				var touched_count = 0;
				var value = '';
				$field.parent().find('.validate_phone').each(function() {
					value += $(this).attr('value');
					field_count++;
					if ($(this).hasClass('touched')) touched_count++;
				});
				// if they've touched each of the fields or no ignore's allowed (for final validation)
				if (field_count == touched_count || no_ignore) var valid = value.match(/[0-9]{10}/);
				// else ignore it
				else var valid = 'ignore';
				// done and done
				break;
			// text
			case 'text':
				// the value
				var value = $field.attr('value');
				// trimmed has length?
				var valid = value && value.length;
				// done and done
				break;
			// zip code
			case 'zip':
				// the value
				var value = $field.attr('value');
				// match it
				var valid = value.match(/[0-9]{5}(\-[0-9]{4})?/);
				// done and done
				break;
		}
		// if ignore
		if (valid == 'ignore') {
			// debug
			LandingPage.debug('Ignoring ' + $field.attr('name') + ':' + value);
			// and stop here
			return valid;
		}
		// debug
		LandingPage.debug($field.attr('name') + ':' + value + ' ' + (valid ? 'is' : 'is not') + ' valid ' + type);
		// the validation status
		var $status = $field.parents('td').eq(0).siblings('.col-help');
		// if valid
		if (valid) $status.text('Okay!').removeClass('fail').addClass('valid');
		// else invalid
		else $status.text('Error!').removeClass('valid').addClass('fail');
		// return valid state
		return valid;
	}
	
};

var Malikot = {
	setCookie: function(key, value) {
		document.cookie = key+"="+value+"; expires=Fri, 5 Feb 2010 20:47:11 UTC; path=/";
	},
	readCookie: function(key) {
		var nameEQ = key + "=";
		var 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;
	}
}
