var LandingPage = {
	valid : false,
	check_cookie : function()
	{
	    $("#entry-form :input").each(function(){
	        var o = this.name;
	        v = Malikot.readCookie(o)
	        cookie_validate = false;
	        if(v) {
	            $("input[name='"+o+"']").val(v);
                cookie_validate = true;
            }
            
            if(cookie_validate) 
                LandingPage.final_validate(false)
                
        });
	},
	push_notification : function(valid, help)
	{
		$(help).hide();
		
		if(valid) {
			$(help).html("ok!");
			$(help).removeClass("fail");
			$(help).addClass("valid");
			
		} else {
			$(help).html("oops!");
			$(help).addClass("fail");
			$(help).removeClass("valid");
			$(help).fadeIn();
			
		}
		
		$(help).fadeIn();
	},
	validate : function(object, value){
		
		valid = false;
		
		switch(object)
		{
			case 'email':
				if(Validate.email(value)) {
					valid = true;
				} else {
				    if(window.pageTracker) {
						domain = window.location.href;
			            pageTracker._trackEvent("Landing Page", "Email Failed Validation", "Email: "+value);
			        }
				}
			break;
			case 'zip_code':
				if(Validate.isLength(value, 5) && Validate.isDigit(value)) {
					valid = true;
				} else {
				    if(window.pageTracker) {
						domain = window.location.href;
			            pageTracker._trackEvent("Landing Page", "Zip Failed Validation", "Zip: "+value);
			        }
				}
			break;
		}
		
		//# make sure has value, and not a special case validation.
		if(object != "zip_code" && object != "email" && object != "phone_0" && object != "phone_1" && object != "phone_2") {
			if(Validate.hasVal(value) && !Validate.isDigit(value) ) {
				valid = true;
			} else {
			    if(window.pageTracker) {
					domain = window.location.href;
    		            pageTracker._trackEvent("Landing Page", object+" Failed Validation", "This : "+object+" no value : "+value);
		        }
			}
		} else if( object == 'phone_0' || object == 'phone_1' || object == 'phone_2' ){
			phone_0 = $('#id_phone_0').val();
			phone_1 = $('#id_phone_1').val();
			phone_2 = $('#id_phone_2').val();
			full_phone = phone_0 + phone_1 + phone_2;
			if(full_phone.length == 10 && Validate.isDigit(full_phone)){
				valid = true;
			} else {
				valid = false;
			}
		}
		
		return valid;

	},
	final_validate : function(set_cookie)
	{
		form_valid = true;
		
		$("#entry-form :input").each(function(){
			var p = $(this).parent().get(0);
			var help = $(p).next().get(0);
			var v = $(this).val();
			var o = this.name;
			if(o != "experience" && o != "") {
				valid = LandingPage.validate(o, v);	
				if(set_cookie == true)
				    Malikot.setCookie(o, v);
				LandingPage.push_notification(valid, help);
			}
			//# if something is not valid - keep them on the page.	
			if(valid == false)
				form_valid = false;
		})

		return form_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;
	}
}