﻿
$(document).ready(function(){
    //capture submit button click event
    $('#btnSubmit').click(function(){
        ProcessSignup();
    });
    //capture the Enter keypress event
    $(document).bind('keypress', function(event) {
        var code=event.charCode || event.keyCode;
        if(code && code == 13) {// if enter is pressed
	        ProcessSignup();
	    }
    });
    
    function ProcessSignup(){
        var firstName = $('#firstName').val();
        var lastName = $('#lastName').val();
        var companyName = $('#companyName').val();
        var emailAddress = $('#emailAddress').val();
        if(validate(firstName, lastName, emailAddress))
        {
            $('#btnSubmit').replaceWith("<img style='padding-left:15px;' src='/images/loading.gif' />");
            var params = {
                firstname: firstName, 
                lastname: lastName, 
                companyName: companyName, 
                emailAddress: emailAddress};
            $.post('/AJAX/Signup.aspx', params, function(){
                $('#signupForm').fadeOut("slow", function(){
                    $('#confirmation').fadeIn("slow");
                });   
            });
            pageTracker._trackPageview('Signup_Conversion');
        }
    }
    
    function validate(firstName, lastName, emailAddress){
        var errorMessage = '';
        if( ! firstName) errorMessage += 'Please enter your first name\n';
        if( ! lastName) errorMessage += 'Please enter your last name\n';
        if( ! emailAddress){
            errorMessage += 'Please enter your email address';
        }
        else{
            if( ! emailAddress.match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)){
               errorMessage += 'Please check the formatting of your email address';
            } 
        }
        if(errorMessage) {
            alert(errorMessage);
            return false;
        }
        
        return true;
    }

    // rollovers
    $('#middle a img').mouseover(function(){
        var imgsrc = $(this).attr('src');
        var matches = imgsrc.match(/_over/);
        if(!matches){
            var imgsrcON = imgsrc.replace(/.png$/ig, '_over.png');
            $(this).attr('src', imgsrcON);
        }
    });
    
    $('#middle a img').mouseout(function(){
        var imgsrc = $(this).attr('src');
        var matches = imgsrc.match(/_over/);
        if(matches){
            var imgsrcOff = imgsrc.replace(/_over.png$/ig, '.png');
            $(this).attr('src', imgsrcOff);
        }
    });
    
    //preload
    $('#middle a img').each(function(){
        var rollSrc = $(this).attr('src');
        var rollON = rollSrc.replace(/.png$/ig, '_over.png');
        $('<img>').attr('src', rollON);
    });
    
    //tips archive
     $('div.tip_title').bind('click', function(){           
            var $this = $(this);
            var current_height = $this.parent('div').height();
            //close
            $('div.tip_container').each(function(){
                var $this = $(this);
                var current_height = $this.height();
                if(current_height > 26){
                    $this.animate({height: '26px'}, 'normal');
                    $this.children('div.tip_title').css('background-image', 'url(/images/btn_arrow_close.gif)')
                    .removeClass('tip_title_active');
                }
            });
            //open
            if(current_height < 27){
                var open_height_px = ($this.next().height() + 26 + 10) + 'px';
                $this.css('background-image', 'url(/images/btn_arrow_open.gif)')
                .parent('div').animate({height: open_height_px}, 'normal')
                .end().addClass('tip_title_active').removeClass('tip_title_over');//remove over state for Safari
            }          
    }).hover(function(){
        $(this).addClass('tip_title_over');
    }, function(){
        $(this).removeClass('tip_title_over');
    });
});



