$(document).ready(function(){ 

	function saveSelectedCourse( id ) {
		var course_id = id ;
		var status = $("#enrol_" + course_id).attr("checked") ;
		$.ajax({
   			type: "POST",
   			url: "./form.enrol_checkbox.php",
   			data: "course_id=" + course_id + "&status=" + status/*,
 			success: function(msg){
     			alert( "Data Saved: " + msg );
   			}*/
 		});
	}
	
	$(":checkbox").click(function () {
		var element_id = $(this).attr("id") ;
		var arr_element = element_id.split("_") ;
		var course_id = arr_element[1] ;
		saveSelectedCourse(course_id) ;
	});
	
	// counts how many courses are ticked
	function countSelectedCourses() {
		var num_courses = $("input:checked").length;
		$("#count_courses").html( (num_courses <= 0 ? "Select your courses and fees." : num_courses + " course" + (num_courses > 1 ? "s" : "") + " selected. <!--(<a id=\"clear_boxes\" style=\"text-decoration: underline\">clear</a>) <a id=\"enrolment_link\"><b>Proceed to the registration form &raquo;</b></a>--> <input type=\"submit\" value=\"Proceed to the registration form &raquo;\" />"));	
	}
	countSelectedCourses();
	$(":checkbox").click(countSelectedCourses);
  
	// auto-tick when fee drop-down changes
	$("select").change(function () {
		var element_id = $(this).attr("id") ;
		var arr_element = element_id.split("_") ;
		
		var course_id = arr_element[1] ;
 		$("#enrol_" + course_id).attr("checked","checked") ;
 		
 		saveSelectedCourse(course_id);
 		countSelectedCourses();
  	});
	
 	// toggle previous courses table
	$("#toggle_previous").click(function () {
		$("#previous_courses").toggle();
	});
	
	// on'submit' register interest form
	$(".label_err").hide() ;
	$("#submit_register_interest").click(function() {
		$(".label_err").hide() ;
		var errors = 0 ;
		var err_msg = "" ;
		
		// full name required
		if ( "" == $("input#fname").val() ) {
			$("#fname_label_err").show() ;
			errors++ ;
		}
		
		if ( "" == $("input#lname").val() ) {	
			$("#lname_label_err").show() ;
			errors++ ;
		}
		
		// email address required
		if ( "" == $("input#email").val() ) {
			$("#email_label_err").show() ;
			errors++ ;
		}
		else if ( $("input#email").val() != $("input#cemail").val() ) {
			$("#cemail_label_err").show() ;
			errors++ ;
		}
		
		if ( 0 == errors ) {
			var data_string = $("form#register_interest").serialize() ;
			//alert ( data_string ) ;
			$.ajax({
   				type: "POST",
   				url: "./form.register_interest.php",
   				data: data_string,
 				success: function(msg){
 					if ( "done" != msg ) {
 						alert( msg ) ;
 					}
 					else {
 						$("#submit_register_interest").replaceWith("<div id=\"form_submitted\">Thank you. A confirmation e-mail will be sent to you shortly.<br />We look forward to seeing you there!</div>") ;
 					}
   				}
 			});
		}
	});
	
	/*
	function clearCheckboxes() {
		alert( 'hello' ) ;
		$('input:checkbox').removeAttr('checked');
		countSelectedCourses();
	}
	$("#clear_boxes").click(clearCheckboxes);
	*/
});
	
// doesn't work!
/*
function goToByScroll(id){
	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}
*/

/*	
function toggleCourse( course_id, action )
{
    var description = document.getElementById( 'course_' + course_id ) ;
    description.className = action ;

    var title = document.getElementById( 'course_title_' + course_id ) ;
    title.className = action + "_title" ;

    var row = document.getElementById( 'course_row_' + course_id ) ;
    row.className = action + "_row" ;

    var row_close = document.getElementById( 'course_row_close_' + course_id ) ;
    row_close.className = action + "_row" ;

    var row_spacer = document.getElementById( 'course_row_spacer_' + course_id ) ;
    row_spacer.className = action + "_row" ;

    var info_link  = document.getElementById( 'more_info_' + course_id ) ;
    var close_link = document.getElementById( 'close_info_' + course_id ) ;

    if ( "show" == action )
    {
        info_link.className = "hide" ;
        close_link.className = "show" ;
    }
    else if ( "hide" == action )
    {
        info_link.className = "show" ;
        close_link.className = "hide" ;
    }
}*/

function validate()
{
    var flag = 0 ;

    for( i=0; i<document.course_registration.enrol.length; i++ )
    {
        if ( true == document.course_registration.enrol[i].checked )
        {
            flag++ ;
        }
    }

    if ( 0 == flag )
    {
        alert( "You must select at least one course to enrol on." ) ;
        return false ;
    }
    else if ( flag > 0 )
    {
        return true ;
    }
    else
    {
        return false ;
    }
}

function recalculate()
{
    var new_total = 0.00 ;

    for( i=0; i<document.send_registration.elements.length; i++ )
    {
        if( "select-one" == document.send_registration.elements[i].type && "" != document.send_registration.elements[i].value && !isNaN( document.send_registration.elements[i].value ) )
        {
            new_total += parseFloat( document.send_registration.elements[i].value ) ;
        }
    }

    document.getElementById( 'total_fee' ).innerHTML = new_total.toFixed( 2 ) ;
}

function other()
{
    var ref = document.send_registration.ref.value ;
    var other = document.getElementById( 'other_ref' ) ;

    if ( "Other" == ref )
    {
        other.className = "show" ;
        document.send_registration.ref_other.value = "" ;
    }
    else
    {
        other.className = "hide" ;
        document.send_registration.ref_other.value = ref ;
    }
}

function trim( text )
{
    if ( undefined == text )
    {
        return "" ;
    }
    else
    {
        return text.replace(/^s*|\s*$/g,'' ) ;
    }
}

function isEmpty( str )
{
    var strRE = /^[\s ]*$/gi ;
    return strRE.test( str ) ;
}

function validate_reg()
{
    var err = 0 ;

    for ( i=0; i<document.send_registration.elements.length; i++ )
    {
        var el    = document.send_registration.elements[i] ;
        var elVal = el.value ;

        if ( "checkbox" != el.type )
        {
            el.className = "field filled" ;
            if ( "" == elVal || isEmpty( elVal ) )
            {
                el.className = "field missing" ;
                err++ ;
            }
        }
    }

    if ( 0 != err )
    {
        alert( "Please fill in all the fields." ) ;
        return false ;
    }
    else
    {
        for ( j=0; j<document.send_registration.elements.length; j++ )
        {
            var elc    = document.send_registration.elements[j] ;
            var elcVal = elc.value ;

            if ( "relevant-medical-details" == elc.name && !elc.checked )
            {
                alert( "You must tick the box to confirm you have provided all the relevant medical information." ) ;
                return false ;
            }
            else if ( "waiver-general" == elc.name && !elc.checked )
            {
                alert( "You must tick the box to confirm you have read, understood and agree to the ID general activity waiver." ) ;
                return false ;
            }
        }
        return true ;
    }
}
