// JavaScript Document

function disableEnterKey(e)
{
	var key;
	if(window.event)
		key = window.event.keyCode;     //IE
	else
		key = e.which;     //firefox
	if(key == 13)
		return false;
	else
		return true;
}
$(document).ready(function(){
	//capture the return key
	$("#topFormIP").submit(function(){
		var qry = $(":input[name=qry]").val(); 
		var ip = $(":input[name=ip]").val();
		
		if(qry=="ip" && checkIP(ip)==true){
			window.location='http://www.ipillion.com/ip/'+ip;
			return false;
			
		}
		else if(qry=="site"){
			ip=ip.split("http://").join("");
			ip=ip.split("www.").join("");
			
			window.location='http://www.ipillion.com/site/'+ip;
			return false;
			
		}
		else{
			jAlert("Wrong IP Address: "+ip,"Error");
			return false;
			
		}
	});
	$("#submitAnalyseDomain").click(function(){
		$.fancybox.showActivity();
		var domain = $(":input[name=domain]").val();
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "mod=ajax&cmd=checkDomain&domain="+domain,
			dataType: "json",
			success: function(msg){
				$.fancybox.hideActivity();
				if(msg['success']==0){
					jAlert(msg['msg'],"Error");
				}
				else if(msg['success']==1){
					window.location='http://www.ipillion.com/site/'+msg['domain'];
				}
			}
		});
	});
	
	
	
	$(".fetchFormComplaint").click(function(){
		var values = $("#ipInfo").serialize();
		$.fancybox.showActivity();
		//$("#ipCommentsForm").hide('slow');
		$("#ipCommentsForm").html("");
		$("#ipCommentsForm").show();
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "mod=ajax&cmd=fetchFormComplaint&"+values,
			success: function(msg){
				$("#ipCommentsForm").html(msg);
				//$("#comment"+id+"").fadeIn('slow', function() {
				// Animation complete.
	  			//});
				$.fancybox.hideActivity();
			}
		});										 
	});
	
	
	$(".fetchFormAsk").click(function(){
		$.fancybox.showActivity();
		//$("#ipCommentsForm").hide('slow');
		$("#ipCommentsForm").html("");
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "mod=ajax&cmd=returnFormAsk",
			success: function(msg){
				$("#ipCommentsForm").html(msg);
				//$("#comment"+id+"").fadeIn('slow', function() {
				// Animation complete.
	  			//});
				$.fancybox.hideActivity();
			}
		});										 
	});
	$(".fetchFormCompareIP").click(function(){
		var values = $("#ipInfo").serialize();
		var count = $('.ipCompareBlock').size();
		count = parseFloat(count)+1;
		
		$.fancybox.showActivity();
		
		
		
		//$("#ipCommentsForm").hide('slow');
		$("#ipCommentsForm").html("");
		$("#ipCommentsForm").show();
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "mod=ajax&cmd=fetchFormCompareIP&"+values+"&count="+count,
			success: function(msg){
				$('.ipBlockContainerMain').append("<div class=\"ipCompareBlock\" id=\"ipCompareBlock"+count+"\"></div>");
				$('#ipCompareBlock'+count+'').html(msg);
				
				
				//$("#comment"+id+"").fadeIn('slow', function() {
				// Animation complete.
	  			//});
				$.fancybox.hideActivity();
			}
		});										 
	});
	
	
	
	$("#logoutLink").click(function(event){
		$.fancybox.showActivity();
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "mod=ajax&cmd=doLogout",
			dataType: "json",
			success: function(msg){
				if(msg['success']==0){
					$.fancybox.hideActivity();
					jAlert("There has been a problem signing you out.", 'Error');           			
				}
				else if(msg['success']==1){
					location.reload();
				}
			}
		});										 
		
	});
	
	
	$("#username").focus(function(){
		$(this).val('');
	});
	$("#password").focus(function(){
		$(this).val('');
	});
	
	$("#submitLogin").click(function(){
		$.fancybox.showActivity();
		var username=$("#username").val();
		var password=$("#password").val();
		$.ajax({
   			type: "POST",
   			url: "index.php",
   			data: "mod=ajax&cmd=validateLogin&username="+username+"&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();
				}
			}
		});
	});
	
});
function ipCompareBlockClose(id){
	$('#ipCompareBlock'+id+'').hide();	
}
function replyForm(id){
	$.fancybox.showActivity();
	$.ajax({
   		type: "POST",
   		url: "index.php",
   		data: "mod=ajax&cmd=fetchFormReply&id="+id,
		success: function(msg){
			$("#comment"+id+"").html(msg);
			$("#comment"+id+"").show();
			$.fancybox.hideActivity();
		}
 	});	
}
function likePost(id){
	$.fancybox.showActivity();
	$.ajax({
   		type: "POST",
   		url: "index.php",
   		data: "mod=ajax&cmd=likePost&id="+id,
		success: function(msg){
			$("#linkLike"+id).replaceWith("<img src=\"images/button_liked.gif\">  ");
		
			$.fancybox.hideActivity();
		}
 	});
}
function checkIP(ip){
	var tomatch=/^(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( tomatch.test(ip)==true){
		return true;
	}
	else{
		return false;
	}
}
function compareIpRetrieve(id){
	var ip = $("#ipCompareForm"+id+" input[name=ip]").val();
	$.fancybox.showActivity();
	if(checkIP(ip)==false){
		jAlert("Wrong IP Address: "+ip,"Error");
		$.fancybox.hideActivity();
	}
	else{
		$.ajax({
			type: "POST",
			url: "index.php",
			data: "mod=ajax&cmd=fetchIpDetails&ip="+ip+"&id="+id,
			dataType: "json",
			success: function(msg){
				$("#ipDetails"+id+"").html(msg['html']);
				
				if (GBrowserIsCompatible()) {
					var map = new GMap(document.getElementById("ipMap"+id));
					map.addControl(new GSmallMapControl());
					map.addControl(new GScaleControl());
					map.setCenter(new GLatLng(msg['latitude'],msg['longitude']), 7);
					var label='<div style="width:240px">'+ip+' is in '+msg['city']+', '+msg['region_name']+', '+msg['country_name']+'</div>'
					var point = new GLatLng(msg['latitude'],msg['longitude']);
					var marker = new GMarker(point)
					GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(label);});
					map.enableScrollWheelZoom();
					map.addOverlay(marker);
					marker.openInfoWindowHtml(label);
					$.fancybox.hideActivity();
				}
				
				
			}						
		});
	}
}

