var cazLive = {
    updates: 0,
    updatestatus: $('.updatestatus'),
    updatesnumber: $('.updatenumber'),
    eventID: '',
    regbox: ''
};

function checkBlanks() {



		if ($("#registeredevents").children().size() == 0) {
		
		$("#registeredevents").append('<div class="section registeredeventszero">You have not yet registered for any events.</div>');
		
		}
		
		if ($("#liveevents").children().size() == 0) {
		
		$("#liveevents").append('<div class="section liveeventszero">There are currently no live events scheduled. Please check back soon.</div>');
		
		}
		
		if ($("#teleconferencesevents").children().size() == 0) {
		
		$("#teleconferencesevents").append('<div class="section teleconferenceseventszero">There are currently no teleconferences scheduled. Please check back soon.</div>');
		
		}

}

function updateTextBox(elementID, textValue) {

    $('#event' + elementID).attr("checked",textValue);

}

function checkDetailsStatus() {

    var count = 0;

    $('.contactfields input').each(function() {
        if (!this.value) {
            count++;
        }

        if (count === 0) {

            $(cazLive.updatestatus).text('complete');

        } else {

            $(cazLive.updatestatus).text('incomplete');
        }

    });
	

}

function updatesMade() {

    cazLive.updates++;

    $(cazLive.updatesnumber).text(cazLive.updates);

}

function messageBar() {

    if (cazLive.updates === 0) {
        $('#messagebar').css('display', 'block');
        $('#logo').addClass('message');
    }

}

		function submitForm() {
		
			if(cazLive.updates != 0) {
				Popup.showModal('confirmDetailsPopup');
			} else {
					document.location = "register.asp?view=sendemail&email=" + document.getElementById('email').value
			}
		}


function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

$(document).ready(function() {

//$('input[type="checkbox"]').hide();
$('input[type="radio"]').hide();
$('.button').show();

	
	checkBlanks();
    checkDetailsStatus();



// Add event
    $('.register').live('click', function() {

if ($(this).parent().parent().hasClass("roadshow")) {
 if ($("#registeredevents").children().hasClass("roadshow"))
			{
				
				alert('You can only register for one Q&A roadshow. Please remove the other event first.');
				
				return false;
			}
									   }

        cazLive.eventID = $(this).parent().parent().attr("id");

        $("#" + cazLive.eventID).fadeOut('fast', function() {
     $("#" + cazLive.eventID).appendTo("#registeredevents").fadeIn();
	 $('.registeredeventszero').remove();

  });
		
			
		
		
				$(this).toggleClass("remove");
		$(this).toggleClass("register").text("Remove >");
        updateTextBox(cazLive.eventID, true);
            messageBar();
        updatesMade();
        $('html,body').animate({
            scrollTop: 0
        }, 'slow');

		
		
    });


// Remove event
    $('.remove').live('click', function() {

        cazLive.eventID = $(this).parent().parent().attr("id");
        cazLive.regbox = $(this).parent().parent().attr("class");


        if (cazLive.regbox === 'section event' || cazLive.regbox === 'section roadshow' ) {
			
			$("#" + cazLive.eventID).fadeOut('fast', function() {
       $("#" + cazLive.eventID).appendTo("#liveevents").fadeIn();
	   
	   $('.liveeventszero').remove();

	
  });
			
        } else {
			
			$("#" + cazLive.eventID).fadeOut('fast', function() {
       $("#" + cazLive.eventID).appendTo("#teleconferencesevents").fadeIn();
	   
	   $('.teleconferenceseventszero').remove();
  });
			
        }
		
		$(this).toggleClass("remove");
		$(this).toggleClass("register").text("Register >");
        updateTextBox(cazLive.eventID, false);
        messageBar();
        updatesMade();
    });


// hide/show contact details
    $("#editdetails").click(function() {

        $("#yourdetails").slideToggle();
		
		
		

    });


// contact fields detail status 
    $('#yourdetails input').blur(function() {
        checkDetailsStatus();
    });
	


// Contact fields validation 

    $("input[type=submit]").click(function() {
										   
		var error = true;								   

        $('.errors').hide();
        $('input').removeClass("error");
		
        var name = $("input#firstname").val();
        if (name === "") {
            $(".errors").show().html("Please enter your first name");
            $("input#firstname").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        }

        var lastname = $("input#lastname").val();
        if (lastname === "") {
            $(".errors").show().html("Please enter your last name");
            $("input#lastname").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        }

        var email = $("input#email").val();
        if (email === "") {
            $(".errors").show().html("Please enter your email address");
            $("input#email").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        } else if (!isValidEmailAddress(email)) {
            $(".errors").show().html("Please enter a valid email address");
            $("input#email").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;

        }

        var company = $("input#company").val();
        if (company === "") {
            $(".errors").show().html("Please enter your company name");
            $("input#company").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        }

        var tel = $("input#tel").val();
        if (tel === "") {
            $(".errors").show().html("Please enter your telephone number");
            $("input#tel").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        }
		
		var webcast = $("input.webcast").val();
        if (webcast === "") {
            $(".errors").show().html("Please select the webcast(s) you would like to register for");
            $("input.webcast").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        }
		
		if (!error) {
			$("#yourdetails").slideDown();
		}
		
		return error;

    });
	
	$("input[type=image]").click(function() {
										   
		var error = true;								   

        $('.errors').hide();
        $('input').removeClass("error");
		$('input').removeClass("required");
		
		var event = $("input.event").val();
		if($("input.event").is(':checked') === false) {
            $(".errors").show().html("Please select the webcast(s) you would like to register for");
            $(".event").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        }
		
		var tel = $("input#tel").val();
        if (tel === "") {
            $(".errors").show().html("Please enter your telephone number");
            $("input#tel").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        }
		
		var company = $("input#company").val();
        if (company === "") {
            $(".errors").show().html("Please enter your company name");
            $("input#company").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        }
		
		var email = $("input#email").val();
        if (email === "") {
            $(".errors").show().html("Please enter your email address");
            $("input#email").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        } else if (!isValidEmailAddress(email)) {
            $(".errors").show().html("Please enter a valid email address");
            $("input#email").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;

        }
		
		var lastname = $("input#lastname").val();
        if (lastname === "") {
            $(".errors").show().html("Please enter your last name");
            $("input#lastname").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        }
		
        var name = $("input#firstname").val();
        if (name === "") {
            $(".errors").show().html("Please enter your first name");
            $("input#firstname").focus().addClass("error");
            $('html,body').animate({
                scrollTop: 0
            }, 'slow');
            error = false;
        }

        

        

        

        
		
		
		if (!error) {
			$("#yourdetails").slideDown();
		}
		
		return error;

    });

});



$(".formcheckbox input[type=checkbox]").hide();

function updateBtnRegister(checkbox) {

	$('#' + checkbox).attr("checked", true)
	$('.event').removeClass("required");
	$('#' + checkbox).siblings('.registerbtn').addClass('selected');

}

function updateBtnRemove(checkbox2) {

    $('#' + checkbox2).siblings('.registerbtn').removeClass('selected');
	$('#' + checkbox2).attr("checked", false)

}

$(document).ready(function() {

    //register button clicked
    $(".registerbtn").live("click", function() {
		var checkbox = $(this).siblings('input[type=checkbox]').attr('id');
			updateBtnRegister(checkbox);									   
    });
	
	$(".selected").live("click", function() {
		var checkbox2 = $(this).siblings('input[type=checkbox]').attr('id');
			updateBtnRemove(checkbox2);									   
    });

});
