var LandingPage = {
	valid : false,
	phone_0_focus : false,
	phone_1_focus : false,
	phone_2_focus : false,
	check_cookie : function()
	{
	    
	    $("#entry-form :input").each(function(){
	        
	        var o = this.name;
	        v = Malikot.readCookie(o)
	        cookie_validate = false;
	        if(v && "string" == typeof v) {
	            $("input[name='"+o+"']").val(v);
                cookie_validate = true;
            }
            
            if(cookie_validate) 
                LandingPage.final_validate(false) 
        });
	},
	phone_monitor : function() {
	    $('#id_phone_0').focus(function(){ LandingPage.phone_0_focus = true; });   
	    //$('#id_phone_1').focus({ alert('phone_1 hit') }); 
	    //$('#id_phone_2').focus({ alert('phone_2 hit') }); 
	},
	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;
			case 'phone_0':
			    LandingPage.phone_0_focus = true;
			    if(Validate.isLength(value, 3) && Validate.isDigit(value)) {
					valid = true;
				} else {
				    if(window.pageTracker) {
						domain = window.location.href;
			            pageTracker._trackEvent("Landing Page", "Phone Area Failed Validation", "Phone: "+value);
			        }
				}
			break;
			case 'phone_1':
			    LandingPage.phone_1_focus = true;
			    if(Validate.isLength(value, 3) && Validate.isDigit(value)) {
					valid = true;
				} else {
				    if(window.pageTracker) {
						domain = window.location.href;
			            pageTracker._trackEvent("Landing Page", "Phone Prefix Failed Validation", "Phone: "+value);
			        }
				}
			break;
			case 'phone_2':
			    LandingPage.phone_2_focus = true;
			    if(Validate.isLength(value, 4) && Validate.isDigit(value)) {
					valid = true;
				} else {
				    if(window.pageTracker) {
						domain = window.location.href;
			            pageTracker._trackEvent("Landing Page", "Phone Suffix Failed Validation", "Phone: "+value);
			        }
				}
			break;
			case 'program':
			    //valid = true;
		    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" && object != "program") {
			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);
		        }
			}
		} 
		
		return valid;

	},
	final_validate : function(set_cookie)
	{
		form_valid = true;
		
		$("#entry-form :input").each(function(){
		    if(this && this.name != 'experience' && this.name != 'program') {
		
			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;
	}
}