$(document).ready(function(){
   
   var subjectText = $("#subjecttext").val();
 
   if ($("#cursus").val() != ''){
      changeCourseDate($("#cursus").val());
      changeTargetEmail($("#cursus").val());
      $("#cursusnaam").val(($('#cursus :selected').text()));
      $("#subject").val(subjectText + ' ' + $('#cursus :selected').text());
   }

   // JQuery functions called when document elements are loaded
   $(".datepicker").datepicker({ 
      yearRange: '-120:+1',
      dateFormat: "dd-mm-yy", 
      showOn: "both", 
      buttonImage: "/TXF/sample/img/calendar.gif", 
      buttonImageOnly: true 
   });
   
  /* $(".button[value='Annuleren']") .remove();*/
     $(".button[value='Annuleren']") .attr("style","display:none;");
	 $(".button[value='Cancel']") .attr("style","display:none;");
     
     $("#cursus").change(function(){
         changeCourseDate($(this).val());
         changeTargetEmail($(this).val());
         
         $("#cursusnaam").val(($('#cursus :selected').text()));
         $("#subject").val(subjectText + ' ' + $('#cursus :selected').text());
      });
     
     $("#cursusdata").change(function(){
        
         $("#cursusdatum_selected").val($(this).val());  

      });
     
     $('#price').attr("disabled", true); 
        
		
	/* This is for page 3 of the multiple days course form */
	$("input[@name='Block/EenA']").click(function(){
        this.blur();
        this.focus();
     });
	 $("input[@name='Block2']").click(function(){
        this.blur();
        this.focus();
     });
	 $("input[@name='Block1/EenA']").click(function(){
        this.blur();
        this.focus();
     });
	 $("input[@name='Block3/EenA']").click(function(){
        this.blur();
        this.focus();
     });
	 $("input[@name='Block4/EenA']").click(function(){
        this.blur();
        this.focus();
     });
	 
	$("input[@name='Block/EenA']").change(function(){
        calcTotalPrice();
     });
	 $("input[@name='Block2']").change(function(){
        calcTotalPrice();
     });
	 $("input[@name='Block1/EenA']").change(function(){
        calcTotalPrice();
     });
	 $("input[@name='Block3/EenA']").change(function(){
        calcTotalPrice();
     });
	 $("input[@name='Block4/EenA']").change(function(){
        calcTotalPrice();
     });
    
});




function calcTotalPrice() {
	
	var totprice = 0;
	if (blk1 = $("input[@name='Block/EenA']:checked").val()) {
		totprice = totprice + getLocalCoursePrice(blk1);
	}
	if (blk2 = $("input[@name='Block2']:checked").val()) {
		totprice = totprice + getLocalCoursePrice(blk2);
	}
	if (blk=$("input[@name='Block1/EenA']:checked").val()) {
		totprice = totprice + getLocalCoursePrice(blk);
	}
	if (blk=$("input[@name='Block3/EenA']:checked").val()) {
		totprice = totprice + getLocalCoursePrice(blk);
	}
	if (blk=$("input[@name='Block4/EenA']:checked").val()) {
		totprice = totprice + getLocalCoursePrice(blk);
	}
	$("#TotalPrice").val(totprice + ' EUR');
	$("#TotalPricedisabled").val(totprice + ' EUR');
}

function getLocalCoursePrice(id) {
	var localprice = 0;
	var pricelist = new Array();
	var y;
	
	pricelist = $("#pricelist").val().split(";");
			
	for (y in pricelist) {
		var price;
		price = pricelist[y].split("=");

		if (price[0] == id) {
			localprice = Number(price[1]);
		}

		if (localprice != 0) {break};
	}
	
	return localprice;
}


function changeCourseDate(val){
      var options = '';
      
      $("select#cursusdata").attr("disabled","disabled");
      
      $.get("/rss/courses", { p_cursus_id: val},
             
      function(data){
          options += '<option value=""></option>';        
         $(data).find("listvalue").each(function() {
            if ($(this).attr("displayvalue") == $("#cursusdatum_selected").val()){
               options += '<option selected="selected" value="' + $(this).attr("value") + '">' + $(this).attr("displayvalue") + '</option>';
            }else{
               options += '<option value="' + $(this).attr("value") + '">' + $(this).attr("displayvalue") + '</option>';
            }
            
         });
         
         $("select#cursusdata").html(options);
         $("select#cursusdata").removeAttr("disabled");
      });
}

function changeTargetEmail(cursus_id){
      
      $.get("/rss/course_email", { p_cursus_id: cursus_id},
             
      function(data){
         
         $(data).find("contactEmail").each(function() {
            $('#contactemail').attr("value",$(this).text());
         });
         
         $(data).find("cost").each(function() {
            $('#price').attr("value",$(this).text());
         });
       
      });
      
}
