/*
	/includes/register.js
	jquery functions for registration script
	
	created July 4, 2009 (PC)
	
*/


	$(document).ready(function () {
	
		// variables
		var userOK = false;
		var pwOK = false;
		var pw2OK = false;
	
		// Disable Form fields
		$("#email").attr('disabled', true);
		$("#uGrp").attr('disabled', true);
		$("#continue1").attr('disabled', true);
		//$("#finishBttn").attr('disabled', true);
        
        var validateEmail = $('#validateEmail');
        
        $('#email').keyup(function () {
            
            var iGrp = $('#iGrp').val();
            // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
            
           	if(iGrp < 1) {
           		alert('You must specify your ILC Group.');
           		$('#iGrp').focus();
           		return;
           	}
           	
           	if($('#uGrp').val() < 1) {
           		alert('You must specify your User Group.');
           		$('#uGrp').focus();
           		return;
           	}
  
            // only run the check if the username has actually changed - also means we skip meta keys
            if (this.value != this.lastValue) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
                validateEmail.removeClass('error').html('<img src="/images/icons/ajax_waiting.gif" height="24" width="24" align="absmiddle" /> checking...');
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'ajax/register.php',
                        data: 'cmd=check_email&iGrp='+ iGrp + '&email=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_username (php code) in to the validation message
                            validateEmail.html(j.msg);
                   			if(j.ok == true) {
                   				$("#continue1").removeAttr('disabled'); // enable submit button
                   			}
                        }
                    });
                }, 400);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
            
        }); // end #email.keyup
        
        
        // Get User Groups for specified ILC Group
        var uGrp_msg = $('#uGrp_msg');
        
        $("#iGrp").change(function() {
        
        	if($("#iGrp").val() > 0) {
        	
        	  // show our holding text in the validation message space
              $('#uGrp_msg').html('<img src="/images/icons/ajax_waiting.gif" height="24" width="24" align="absmiddle" /> loading...');
                
        
			  $.getJSON("ajax/register.php", // 1st arg to getJSON is the URI of the script
			  { 
				  iGrp: $("#iGrp").val(), 
				  cmd: "get_uGrps"
			  },
			  function(j) {
				  $('#uGrp options').remove(); // remove options from user group drop down
				  var options = '<option value="">Choose User Group</option>';
				  for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				  }
			  
			  
				  // stick these new options in the existing select menu
				  $("#uGrp").html(options);
				  $('#uGrp_msg').html('');
				  $("#uGrp").removeAttr('disabled');
				  // now your select menu is rebuilt with dynamic info
				} 
			); // end getJSON 
		  
		  } else {
		  	$("#uGrp").html('<option value="">Choose ILC Group</option>');
			$("#uGrp").attr('disabled', true);
		  }
    		
    	}); // end iGrp change
    	
    	
    	 $("#uGrp").change(function() {
    	 	if($("#uGrp").val() > 0) {
    	 		$("#email").removeAttr('disabled');
    	 	} else {
    	 		$("#email").attr('disabled', true);
    	 	}
    	 	
    	 }); // end uGrp change
    	 
    	
    	// Validtate Username function
    	var validateUsername = $('#validateUsername');
        
        $('#username').keyup(function () {
        
        	// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
        
        
        	// only run the check if the username has actually changed - also means we skip meta keys
            if (this.value != this.lastValue) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
                validateUsername.removeClass('error').html('<img src="/images/icons/ajax_waiting.gif" height="24" width="24" align="absmiddle" /> checking...');
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'ajax/register.php',
                        data: 'cmd=check_username&iGrp='+ $('#iGrp').val() + '&username=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_username (php code) in to the validation message
                            validateUsername.html(j.msg);
                            
                            if(j.ok == true) {
                            	userOK = true;	
                   			} else {
                   				userOK = false;
                   			}
                   			
                   			enable_finish();
                   			
                        }
                    });
                }, 400);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
       
        
        }); // end username keyup
        
         var pw_msg = $('#pw_msg');
         $('#pw').keyup(function () {
        
        	// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
        
        
        	// only run the check if the username has actually changed - also means we skip meta keys
            if (this.value != this.lastValue) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
                pw_msg.removeClass('error').html('<img src="/images/icons/ajax_waiting.gif" height="24" width="24" align="absmiddle" /> checking...');
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'ajax/register.php',
                        data: 'cmd=check_password&password=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_username (php code) in to the validation message
                            pw_msg.html(j.msg);
                            
                            if(j.ok == true) {
                   				pwOK = true;
                   			} else {
                   				pwOK = false;
                   			}
                   			
                   			enable_finish();
                   			
                        }
                    });
                }, 400);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
       
        
        }); // end pw keyup
        
        
        var pw2_msg = $('#pw2_msg');
         $('#pw2').keyup(function () {
        
        	// cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
        
        
        	// only run the check if the username has actually changed - also means we skip meta keys
            if (this.value != this.lastValue) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
                pw2_msg.removeClass('error').html('<img src="/images/icons/ajax_waiting.gif" height="24" width="24" align="absmiddle" /> checking...');
                
                // check to see if passwords match in 2/5 of a second
                this.timer = setTimeout(function () {
                    if(t.value == $('#pw').val()) {
                    	 pw2_msg.html('<img src="/images/icons/Success.gif" height="16" width="16" align="absmiddle" />');	
                    	
                    	 pw2OK = true;

                    } else {
                    	pw2_msg.html('Your passwords do not match.');	
                    	pw2OK = false;
                    }
                    
                    enable_finish();
                    
                }, 400);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
       
        
        }); // end pw2 keyup
        
        
        function enable_finish() {
        	if(userOK == true && pwOK == true && pw2OK == true) {
				 $("#finishBttn").removeAttr('disabled'); // enable submit button
			 } else {
				 $("#finishBttn").attr('disabled', true);
			 }
        }
        
        $("#rform").validate();
        
    }); // end document ready