$(function(){
	pageTracker._trackPageview("/beta/step_1");
	function emptyObject(obj) {
	    for (var i in obj) {
	        return false;
	    }
	    return true;
	}
	// set select of country for US users
	function chselect(country){
		$("#country").val(country);
		if (country == "US"){
			$("#state_tr").fadeIn();
		}else{
			$("#state option:first").attr("selected","yes");
			$("#state_tr").hide();
			//$("#state_tr").fadeOut();
		}
    }
	chselect("US");
	$("#country").change(function(){
		chselect($(this).val());
	});
	// set cookies oject
	var elobj = {
		name: $("#join-beta input[name=name]"),
		company: $("#join-beta input[name=company]"),
		title: $("#join-beta input[name=title]"),
		email: $("#join-beta input[name=email]"),
		website: $("#join-beta input[name=website]"),
		phone: $("#join-beta input[name=phone]"),
		where: $("#join-beta textarea[name=where]"),
		comments: $("#join-beta textarea[name=comments]"),
		address: $("#join-beta input[name=address]"),
		city: $("#join-beta input[name=city]"),
		state: $("#state"),
		zip: $("#join-beta input[name=zip]"),
		country: $("#country")
	};
	// set cookies
	var coockobj = {};
	for(var key in elobj){
		if($.cookie(key)){
			coockobj[key] = $.cookie(key);
			elobj[key].val($.cookie(key));
		}
		if ($.cookie(key) == "US"){
			var stcook = $.cookie("state");
			$("#state").val(stcook);
			chselect($.cookie(key));
		}
	}
	if($.cookie("country")){
		chselect($.cookie("country"));
	}
	// get user ip
//	if(emptyObject(coockobj)){
//		$.ajax({
//			type: "GET",
//			url: "getip.php",
//			success: function(data){
//				chselect(data);
//			}
//		});
//	}
	/**
	 * form validation
	 */
 	$("#join-beta").validate({
		rules: {
			name: "required",
			company: "required",
			email: {
				required: true,
				email: true
			},
			where: "required",
			phone:{
				phoneUS: true
			}
//			state:{
//				required:function(el){
//					var elval = $("#country option:selected").val() == "US";
//					return elval;
//				}
//			}
		},
		onfocusout: false,
		onkeyup: false
	});

	// form steps
	$("#beta_next").click(function(){
		var sname = $("#join-beta").validate().element("#name");
		var scompany = $("#join-beta").validate().element("#company");
		var semail = $("#join-beta").validate().element("#email");
		var swhere = $("#join-beta").validate().element("#where");
		var sphone = $("#join-beta").validate().element("#phone");
		if(typeof sphone == "undefined")
			sphone = "true";

		if (sname && scompany && semail && swhere && sphone){
			$("#reg_step1").hide();
			$("#reg_step2").fadeIn();
			$("#reqmess").hide();
			pageTracker._trackPageview("/beta/step_2");
		}
		return false;
	});
	$("#beta_back").click(function(){
		if ($("#reg_step1").css("display") == "none"){
			$("#reg_step2").hide();
			$("#reg_step1").fadeIn();
			$("#reqmess").show();
		}
		return false;
	});

	// Submit Beta form registration
	$("#join-beta").submit( function() {
		if ($("#join-beta").validate().form() == true) {
		// Form elements object
		var betelval = {
			name: $("#join-beta input[name=name]"),
			company: $("#join-beta input[name=company]"),
			title: $("#join-beta input[name=title]"),
			email: $("#join-beta input[name=email]"),
			website: $("#join-beta input[name=website]"),
			phone: $("#join-beta input[name=phone]"),
			where: $("#join-beta textarea[name=where]"),
			comments: $("#join-beta textarea[name=comments]"),
			address: $("#join-beta input[name=address]"),
			city: $("#join-beta input[name=city]"),
			state: $("#state option:selected"),
			zip: $("#join-beta input[name=zip]"),
			country: $("#country option:selected")
		};

		// create cookies
		for(var key in betelval){
			$.cookie(key,betelval[key].val(),{expires: 0, path: '/', domain: window.location.host});
		}
        var subscribe;
		if ($("input[name=subscribe]").is(":checked")) {
			subscribe = $("input[name=subscribe]").val();
		}else {
			subscribe = 'no';
		}

		// get lead type
		var lead_type;
		if(window.location.search != ''){
			var url = window.location.search;
			var tmp = (url.substr(1)).split('&');
			var strArr = tmp[0].split('=');
			lead_type = strArr[1];
		}else{
			lead_type = '';
		}
		
		// Nimble object
		var nimbdata =
			{
				name: betelval.name.val(),
				company: betelval.company.val(),
				title: betelval.title.val(),
				email: betelval.email.val(),
				website: betelval.website.val(),
				phone: betelval.phone.val(),
				where: betelval.where.val(),
				comments: betelval.comments.val(),
				address: betelval.address.val(),
				city: betelval.city.val(),
				state: betelval.state.text(),
				zip: betelval.zip.val(),
				country: betelval.country.text(),
				leadType: lead_type,
				subscribe: subscribe,
				partner: "beta"

			};

			$.ajax({
				type: "GET",
				url: "http://"+ window.location.host +"/lib/beta.php",
				data: "name="+betelval.name.val()+
					  "&company="+betelval.company.val()+
					  "&title="+betelval.title.val()+
					  "&email="+betelval.email.val()+
					  "&website="+betelval.website.val()+
					  "&phone="+betelval.phone.val()+
					  "&where="+betelval.where.val()+
					  "&comments="+betelval.comments.val()+
					  "&address="+betelval.address.val()+
					  "&city="+betelval.city.val()+
					  "&state="+betelval.state.text()+
					  "&zip="+betelval.zip.val()+
					  "&country="+betelval.country.text()+
					  "&subscribe="+subscribe,
				beforeSend: function(){
					$("#join-beta").fadeOut("normal", function() {
						$("#form-beta").html("<div class='ajax_req' id='load' style='text-align:center;'><p>Please wait...</p><p><img src='http://www.nimble.com/wp-content/themes/nimble/images/load.gif' alt=''></p></div>").fadeIn("normal");
					});
				},
				success: function(){
					$.ajax({
						type:"POST",
						url: "http://"+ window.location.host +"/lib/nimble_crm.php",
						data: nimbdata,
						error: function(){
							$("#form-beta").html("<div class='ajax_req' style='text-align:center;'><p style='padding: 10px 0px;'><strong>Server error</strong></p></div>").show();
						},
						success: function(data){
							pageTracker._trackPageview("/beta/submitted");
							$("#form-beta").html("<p><strong>"+betelval.name.val()+",</strong> <br/>"+data+"</p>").show();
							if(data != 'Server error!')
							$('#beta_info_box').hide();
						}
					});
				},
				error: function(){
					$("#form-beta").html("<div class='ajax_req' style='text-align:center;'><p style='padding: 10px 0px;'><strong>Server error</strong></p></div>").show();
				}
			});
		}else{
			return false;
		}
		return false;
	});
});
