$(document).ready(function(){
	$(".page").each(function(){
		$(this).children("fieldset:last").css({background: "none"})
	});
	
	jQuery.validator.addMethod("reqVisible", function(value, element) {
		
		if ( $(element).parents(".page:visible").length > 0 ){
			
			if( value != "" && value != null && typeof value != 'undefined' ){
				return true;
			}
			else{
				return false;
			}
		}
		else{
			return true;
		}
	}, "This element is required");
	
	jQuery.validator.addMethod("dbLength", function(value, element) {
		
		if ( $(element).parents(".page:visible").length > 0 ){
			var name = $(element).attr("name");
			var data = $.ajax({
				url: "/_ajax/ajax.lengthverify.asp",
				data: {
					data: value,
					table: 'tbl_directory',
					field: name
				},
				type: "POST",
				async: false
			}).responseText;
			
			return (data == "true");
		}
		else{
			return true;
		}
	}, "This element is too long.");
	
	jQuery.validator.addMethod("recaptchaVisible", function(value, element){
		
		if ( $(element).parents(".page:visible").length > 0 ){
			return ajaxCheckRecaptcha();
		}
		else{
			return true;
		}
		
	}, "Entered text does not match image. Please try again");
	
	$("#sppRegistrationForm").validate({
		errorClass: "invalid",
		errorElement: "em",
		focusInvalid: false,
		//errorLabelContainer: "#erroBox1",
		errorPlacement: function(error, element) {
			if(element.attr('id') == "recaptcha_ajax_valid") error.insertAfter(element);
		},
		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
			$(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
		},
		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
			$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
		},
		messages: {
			directory_phone_areacode: {
				reqVisible: ""
			},
			directory_phone_prefix: {
				reqVisible: ""
			},
			directory_phone_number: {
				reqVisible: ""
			}
		},
		rules: {
			directory_class: "reqVisible",
			directory_companyname: {reqVisible: true, dbLength: true},
			directory_businessoftype: {reqVisible: true, dbLength: true},
			directory_fname: {reqVisible: true, dbLength: true},
			directory_lname: {reqVisible: true, dbLength: true},
			directory_email: {reqVisible: true, dbLength: true},
			directory_title: {reqVisible: true, dbLength: true},
			directory_street: {reqVisible: true, dbLength: true},
			directory_city: {reqVisible: true, dbLength: true},
			directory_province: {reqVisible: true, dbLength: true},
			directory_postalcode: {reqVisible: true, dbLength: true},
			directory_phone_areacode: {reqVisible: true, number: true},
			directory_phone_prefix: {reqVisible: true, number: true},
			directory_phone_number: {reqVisible: true, number: true},
			directory_established: {reqVisible: true, dbLength: true},
			directory_employees: {reqVisible: true, number: true},
			directory_services: {reqVisible: true, dbLength: true},
			directory_practices: {reqVisible: true, dbLength: true},
			recaptcha_ajax_valid: "recaptchaVisible"
			
		}/*,
		submitHandler: function(form){
		   	$(form).ajaxSubmit();
			return false;
	   }*/
	});
	
	/* page 1 */
	
	$("input[name='directory_class']").click(function(){
		if( $("input[name='directory_class']:checked").val() > 1 ){
			$("#next1").css("background-image","url(/_images/spp.registration-form-next.png)")
		}
		else{
			$("#next1").css("background-image","url(/_images/spp.registration-form-submit.png)")
		}
	})
	
	$("#next1").click(function(){
		if( $("#sppRegistrationForm").valid()){
			$(".page1").hide();
			if( $("input[name=directory_class]:checked").val() == 1  ){
				$("#sppRegistrationForm").submit();
			}
			if( $("input[name=directory_class]:checked").val() > 1  ){
				$(".page2").show();
				$(window).scrollTop("370");
			}	
		}
		else{
			$("fieldset .invalid:first").focus();
		}
	});
	
	/* page 2 */
	
	$("#industry_add").click(function(){
		$("#industry_list option:selected").filter(":enabled").each(function(){		
			var l = $("#industry_selected option").length;
			if ( l < 3 ){
				var s = $(this).clone().appendTo("#industry_selected")
				.end()
				.attr("disabled",'disabled');
				$("#industry_selected").removeClass("invalid");
			}
		});
	});
	
	$("#industry_remove").click(function(){
		$("#industry_selected option:selected").each(function(){
			var id = $(this).attr("value");
			$(this).remove();
			$("#industry_list option[value="+id+"]").removeAttr("disabled");
		})
	});
	
	$("#next2").click(function(){	
		if( $("#sppRegistrationForm").valid()){
			if( $("#industry_selected option").length > 0 ){
				$(".page2").hide();
				$(".page3").show();
				$("#primaryContent").focus();
			}
			else{
				$("#industry_selected").addClass("invalid");
			}
		}
		else{
			$(".invalid:first").focus();
		}
	});
	$("#prev2").click(function(){	
		$(".page2").hide();
		$(".page1").show();
		$("#primaryContent").focus();
	});
	
	/* page 3 */
	$("#next3").click(function(){
		$("#industry_selected option").attr("selected","selected");
		$(".page3, #80_content_id, #next3").hide();
		$("#loadingForm").show();
		$("#sppRegistrationForm").submit();
	});
	$("#prev3").click(function(){	
		$(".page3").hide();
		$(".page2").show();
		$("#primaryContent").focus();
	});
	
});