$(function(){
	showBadge("as");
	$(document).asMask().enterToTab();
	/*
	jQuery.validator.setDefaults({
	               onkeyup:false,
	               waitingValidation: true,
	               onfocusout:false,
	           });
	           */
	
	$(".openform").click(function(){
		loadPopup();
		centerPopup();
	});
	$("#closeFormBtn").click(function(){
		disablePopup();
	});
	$("#closeBtn").click(function(){
		disablePopup();
	});
	$("#txCP").focusout(function(){
			var cpVal = String($(this).val());
			
			var cp4 = cpVal.substr(0,4);
			var cp3 = cpVal.substr(5,3);
			if(cp3=="" && cp4!="" && cp4.length==4){
				cp3="000";
				$(this).val(cp4+"-"+cp3);
			}
			
	     	if($(this).val().length == 8){
		     	$("#small_preloader").show();
		    	$.get("includes/getCP.php",{txCP : $(this).val()},function(data){
			    		var localidade = $("#txLocalidade");
			    		localidade.removeAttr("readonly");
			    		localidade.parent().removeClass("transparent");
			    		localidade.parent().prev().removeClass("transparent");
			    		localidade.parent().next().removeClass("transparent");
			    		localidade.val(data.city);
			    		$("#small_preloader").hide();
		    	},"json");
	    	}
	    });
	try {
		$("#txIdade").msDropDown({
		    mainCSS:'dd2',
		    showIcon:false
		});
		$("#txParentesco").msDropDown({
		    mainCSS:'dd2',
		    showIcon:false
		});
	} catch(e) {
	    alert("Error: "+e.message);
	}
	
	/*
	
	*/
    $('#step1').validate({
        remoteValidationEmail: 'ASMailChecker.php',
        onWait: function(){
       	$('#continuarBtn').hide();
       	$("#loader").show();
       },
        submitHandler: function(form) {
	        $("#loader").hide();
        	//$('#continuarBtn').show();
            checkForm(form);
        },
        showMessageOn: $('.errorLabel'),
        inputHighlight:true,
        afterValidate:function(){
            if($('.error').length > 0){
				if((parseInt($('.error').length)-parseInt($(".errorLabel .error").length))==0){
				    $(".errorLabel").show();
				}
				$('#formFields .error:input[type=text]').each(function(){
				    if($(this).is(":text") && ($(this).val()!="" || $(this).attr("validate").toString().search("required")==0)){
				        $(this).parent().prev().attr("class","inputLeftError");
				        $(this).parent().attr("class","inputCenterError");
				        $(this).parent().next().attr("class","inputRightError");
				    }else if($(this).is(":text") && $(this).val()=="" && $(this).attr("validate").toString().search("required")!=0){
				        $(this).parent().prev().attr("class","inputLeft");
				        $(this).parent().attr("class","inputCenter");
				        $(this).parent().next().attr("class","inputRight");
				    }
				});
			}
			if($('.valid').length > 0){
			                if((parseInt($('.error').length)-parseInt($(".errorLabel .error").length))==0){
			                    $(".errorLabel").children("label").html("");
			                }
			            
			                $('#formFields .valid:input[type=text]').each(function(){
			                    $(this).parent().prev().attr("class","inputLeft");
			                    $(this).parent().attr("class","inputCenter");
			                    if($(this).attr("validate").toString().search("required")!=0 || $(this).val()!=""){
			                    	$(this).parent().next().attr("class","inputRightOK");
			                    }else{
			                    	$(this).parent().next().attr("class","inputRight");
			                    }
			                });
			
			            }
        }
    });

});

function checkForm(form){
    var formID ="#"+form.id;
    var successForm = formID+"_success";
    var formURL = form.id+".php";
    var formData = $(formID).serialize();
    $.post(formURL, formData, function(data){
        if(data.message=="success"){
        	$(formID).fadeOut(500,function(){
        		$("#etiqueta").fadeIn(500);
        		$(successForm).fadeIn(500);
				track("Sucesso",100);
        		$("#conv").html('<iframe src="includes/affpixel.php" frameborder="0" width="1" height="1"></iframe>');
        	});
        }else{
        	$(formID).fadeOut(500,function(){
        		$(successForm).html("Ocorreu um erro! Por favor tente mais tarde.");
        		$(successForm).fadeIn(500);
        	});  
        }
        return false;
    },"json");


}

// GLOBAL VARS

var popupStatus = 0;

//FUNCTIONS

function centerPopup(){
    var windowHeight = document.documentElement.clientHeight;
    if ($.browser.msie && $.browser.version.substr(0,1)<=7) {
        $("#shadow").css({
            "height": windowHeight,
            "left":"0%"
        });
    }else{
        $("#shadow").css({
            "height": "100%"
        });
    }
    
}

function loadPopup(){

    if(popupStatus==0){
        $("#shadow").css({
            "opacity": "0.9"
        });
        $("#shadow").fadeIn("slow");
        var percentage = parseInt((($(window).width()/2)-($("#formboxABS").width()/2))*100/$(window).width())+"%";
        $("#formboxABS").css("left",percentage);
        $("#formboxABS").fadeIn("slow",function(){
        	$('body,html').animate({
        		scrollTop:30
        	},300);
        });
        
        
        popupStatus = 1;
    }
}

function disablePopup(){
    if(popupStatus==1){
        $("#shadow").fadeOut("slow");
        $("#formboxABS").fadeOut("slow");
        popupStatus = 0;
    }
}

function popitup(url) {
	newwindow=window.open(url,'','height=300,width=400,scrollbars=1');
	if (window.focus) {newwindow.focus()}
	return false;
}

