
function showbutton(){
	var validfieldsrequired = 5;
	var countvalidfields = 0;

	// this function simply counts how many fields have the "valid" class assigned
	// if there are enough then it will make the 'submitbutton' div visible

	$(".valid").each(function (){
	
		countvalidfields++;

		if (countvalidfields >= validfieldsrequired){
			$("#submitbutton").show();

			//alert('test');
			return true;
		}
	});

	if (countvalidfields >= validfieldsrequired){
		return true;
	}
	return false;
}

function nntimekey(){
	var timekey = $('#timekeyval').val();
	//alert(timekey);

	var geturi='newtimekey.php';
	var tkerror;

	$.ajax({
                type: "GET",
                url: geturi ,
                dataType: "xml",
                data: ({timekey : timekey}),
                success: function(xml) {
			$(xml).each(function(){
				var tkerror = $(this).find('error').text();
				var expiredate = $(this).find('expiredate').text();
				
				if (tkerror){
					// populate error div
					$('#timekeyval').addClass('error');
					$('#tkerror').html('<br>' + tkerror );
					return false;
					
				}else if(expiredate){
				
					$('#timekeyval').addClass('valid');
					//if ( $('#accountstatus').html()	 == 'trial'){
						$('#accountstatus').html('Upgraded');
					//}
					$('#expiredate').html('Expires ' + expiredate);
					$('#tkerror').html('<br>TimeKey Added Successfully'   );
					return false;
				}
			});
		}	

	});
	return false;
}



$(document).ready(function () {
  $("#submitbutton").hide();
  var validateUsername = $('#username');
  $('#username').keyup(function () {
    var t = this; 
    if (this.value != this.lastValue) {
      if (this.timer) clearTimeout(this.timer);
      //validateUsername.removeClass('error').html('<img src="images/ajax-loader.gif" height="16" width="16" /> checking availability...');
      $('#validateusername').html('<img src="images/ajax.gif" height="16" width="16">');      

      this.timer = setTimeout(function () {
        $.ajax({
          url: 'validate.php',
          data: 'action=check_username&username=' + t.value,
          dataType: 'xml',
          type: 'get',
          success: function (j) {
	    $(j).find('result').each(function (){

			var ok = $(this).find('ok').text();
			var msg = $(this).find('msg').text();
				
			if (ok){
				$('#username').removeClass('error').addClass('valid');
				showbutton();
			}else{
				$('#username').removeClass('valid').addClass('error');
			}
            		$('#validateusername').html(msg);
		});
          }
        });
      }, 200);
      
      this.lastValue = this.value;
    }
  });

 $('#pwd2').keyup(function() {
	if (($('#password').val() != $('#pwd2').val()) ){
		$('#password').removeClass('valid').addClass('error');
		$('#pwd2').removeClass('valid').addClass('error');
		$('#validateusername').html('Passwords do not match');

	}else if($('#pwd2').val().length < 3){
		$('#password').removeClass('valid').addClass('error');
                $('#pwd2').removeClass('valid').addClass('error');
		$('#validateusername').html('Password is too short');
	}else{
		$('#password').removeClass('error').addClass('valid');
		$('#pwd2').removeClass('error').addClass('valid');
		$('#validateusername').html('Password is OK');

		showbutton();
	}
 });

 //$('#email').keyup(function() {
 $('#email').change(function() {
	var t = this;
	this.timer = setTimeout(function () {
        $.ajax({
          url: 'validate.php',
          data: 'action=check_email&email=' + t.value,
          dataType: 'xml',
          type: 'get',
          success: function (j) {
            $(j).find('result').each(function (){

                        var ok = $(this).find('ok').text();
                        var msg = $(this).find('msg').text();

                        if (ok){
                                $('#email').removeClass('error').addClass('valid');
				showbutton();
                        }else{
                                $('#email').removeClass('valid').addClass('error');
                        }
                        $('#validateusername').html(msg);
                });
          }
        });
      }, 200);
	}
 );

 $('#curpwd').change(function() {
        var t = this;
        this.timer = setTimeout(function () {
        $.ajax({
          url: 'validate.php',
          data: 'action=check_existing_password&curpwd=' + t.value,
          dataType: 'xml',
          type: 'get',
          success: function (j) {
            $(j).find('result').each(function (){

                        var ok = $(this).find('ok').text();
                        var msg = $(this).find('msg').text();

                        if (ok){
                                $('#curpwd').removeClass('error').addClass('valid');
                                showbutton();
                        }else{
                                $('#curpwd').removeClass('valid').addClass('error');
                        }
                        $('#validateusername').html(msg);
                });
          }
        });
      }, 200);
        }
 );



 $('#tcaptcha').keyup(function(){
	var t = this;
	this.timer = setTimeout(function () {
        $.ajax({
          url: 'validate.php',
          data: 'action=check_answer&answer=' + t.value,
          dataType: 'xml',
          type: 'get',
          success: function (j) {
            $(j).find('result').each(function (){

                        var ok = $(this).find('ok').text();
                        var msg = $(this).find('msg').text();

                        if (ok){
                                $('#tcaptcha').removeClass('error').addClass('valid');
				showbutton();
                        }else{
                                $('#tcaptcha').removeClass('valid').addClass('error');
                        }
                        $('#validateusername').html(msg);
                });
          }
        });
      }, 200);

	
 });

 $('#registerform').submit(function(){
	return showbutton();
 });
 
});


