// check ip address
function validate_ip(ip)
{
	var regex_ip=/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
	if(regex_ip.test(ip)==true){
		return true;

	}
	else{
		return false;	
	}
}
$(document).ready(function(){
	$("#loginbar").hide();
	
	$("#loginlink").click(function(event){
		$('#loginbar').fadeIn('slow', function() {
        // Animation complete
      });
	});
	$("#submit_login").click(function(){
		$.fancybox.showActivity();
		var email=$("#email").val();
		var password=$("#password").val();
		$.ajax({
   			type: "POST",
   			url: "index.php",
   			data: "mod=ajax&cmd=login&email="+email+"&password="+password,
   			dataType: "json",
			success: function(msg){
				//$("#ips").load('index.php #ips');
				if(msg['success']==0){
					$.fancybox.hideActivity();
					jAlert(msg['msg'], 'Error');           			
				}
				else if(msg['success']==1){
					location.reload();
				}
			}
		});
	});
	
	
	$("#loginclose").click(function(event){
		$("#loginbar").hide();
	});
	
	$('a').tooltip({
		delay: 0,
		track: true,
		showURL: false,
		showBody: "; "
	});

	$("#createstyle").click(function(event){
		$.fancybox.showActivity();
		$("#newstyle").load('index.php',{'mod':'ajax','cmd':'createstyle'}, function(){
	 		$.scrollTo('#newstyle', 800);
			$.fancybox.hideActivity();
		});
	
		
	
	
	
	});
	$("#addip").click(function(){
		$.fancybox.showActivity();
	
		var newip = $("#newip").val();
		var account_key = $("#account_key").val();
		
		if(validate_ip(newip)==true){
			$.ajax({
   				type: "POST",
   				url: "index.php",
   				data: "mod=ajax&cmd=addip&ip="+newip+"&account_key="+account_key,
   				dataType: "json",
				success: function(msg){
					$("#newip").val("");
					$("#ips").load('index.php #ips');	
					$.fancybox.hideActivity();
	
				}
 			});	
		
		}
		else if(validate_ip($("#newip").val())==false){
			//$.fancybox('Incorrect IP format.');
			jAlert('Looks like a bad IP?', 'Error');           			
		}
		$.fancybox.hideActivity();
	
	});
	
	
	
});

function deleteip(account_key,ip){
	$.fancybox.showActivity();
	$.ajax({
		type: "POST",
   		url: "index.php",
   		data: "mod=ajax&cmd=deleteip&ip="+ip+"&account_key="+account_key,
   		success: function(msg){
			$("#ips").load('index.php #ips');
			$.fancybox.hideActivity();	
		}
 	});
}
function deletestyle(account_key,id){
	$.fancybox.showActivity();
	$.ajax({
		type: "POST",
   		url: "index.php",
   		data: "mod=ajax&cmd=deletestyle&id="+id+"&account_key="+account_key,
   		success: function(msg){
			$('#styleslist').load('index.php #styleslist');
			$.fancybox.hideActivity();	
		}
 	});
}

