jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

$(document).ready(function() {
					
	$("#startform").submit(function() {
		return validate();
	});
	
	function validate() {
		
		var valid = true;
		
		var first_name = $("#first_name").val();
		var last_name = $("#last_name").val();
		var area_code = $("#area_code").val();
		var prefix = $("#prefix").val();
		var number = $("#number").val();
		var email = $("#email").val();

		
		if(first_name.length == 0) {
			$("#first_name").css("background-color", "red");
			valid = false;
		} else {
			$("#first_name").css("background-color", "");
		}
		
		
		if(last_name.length == 0) {
			$("#last_name").css("background-color", "red");
			valid = false;
		} else {
			$("#last_name").css("background-color", "");
		}
		
		if(isNaN(area_code) || area_code.length != 3) {
			$("#area_code").css("background-color", "red");
			valid = false;
		} else {
			$("#area_code").css("background-color", "");
		}
		
		if(isNaN(prefix) || prefix.length != 3) {
			$("#prefix").css("background-color", "red");
			valid = false;
		} else {
			$("#prefix").css("background-color", "");
		}
		
		if(isNaN(number) || number.length != 4) {
			$("#number").css("background-color", "red");
			valid = false;
		} else {
			$("#number").css("background-color", "");
		}
		
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!filter.test(email)) {
			$("#email").css("background-color", "red");
			valid = false;
		} else {
			$("#email").css("background-color", "");	
		}

		
		if(valid) {
			
			var term = $("select[name='NewCategory']").val();
			var amount = $("select[name='FaceAmount']").val();
			var health = $("select[name='Health']").val();
			
			var smoker = $("input[name='Smoker']:checked").val();
			var state = $("select[name='State']").val();
			
			var bmonth = $("select[name='BirthMonth']").val();
			var bday = $("select[name='Birthday']").val();
			var byear = $("select[name='BirthYear']").val();
			
			var first_name = $("#first_name").val();
			var last_name = $("#last_name").val();
			var area_code = $("#area_code").val();
			var prefix = $("#prefix").val();
			var number = $("#number").val();
			var email = $("#email").val();
			var subdomain = $("#subdomain").val();
			
			
			$.cookie("term", term, { path: '/' });
			$.cookie("amount", amount, { path: '/' });
			$.cookie("health", health, { path: '/' });
			
			$.cookie("state", state, { path: '/' });
			$.cookie("smoker", smoker, { path: '/' });
			
			$.cookie("bmonth", bmonth, { path: '/' });
			$.cookie("byear", byear, { path: '/' });
			$.cookie("bday", bday, { path: '/' });
			$.cookie("first_name", first_name, { path: '/' });
			$.cookie("last_name", last_name, { path: '/' });
			$.cookie("area_code", area_code, { path: '/' });
			$.cookie("prefix", prefix, { path: '/' });
			$.cookie("number", number, { path: '/' });
			$.cookie("email", email, { path: '/' });
			$.cookie("subdomain", subdomain, { path: '/' });
			
			$.ajax({ url: '/tracker/ajax/processform',
				 async: false,
				 type: 'POST',
				 data: $("#startform").serialize(),
				 success: function (response) {
				 }
			 });
			
		} else {
			return false;
		}
		
	}

});
