// GENERAL FUNCTIONS

var cururl = ''// Current URL

function preloadSajax() {
      preload_image_object = new Image();
      image_url = new Array();
      image_url[0] = "images/sjwaiteml.png";
      image_url[1] = "images/sjeml.png";
      image_url[2] = "images/sjfail.png";
      image_url[3] = "images/ajax-loader.gif";
       var i = 0;
       for(i=0; i<=3; i++)
         preload_image_object.src = image_url[i];
}



// GENERAL EVENTS

$("#sjstatus").ajaxError(function(event, request, settings){
   showError(event, request, settings);
 });


function showWait() {
  $("#sjstatus").hide(300);
  $("#sjstatus").css("display: none;");
  $("#sjstatus").html("<div id='sjwait'><div id='w1'></div></div><div id='sjwaitmsg'>Please wait while data is being processed. It may take some time depending on the speed of the network and current server loads.</div>");
  $("#sjstatus").css("visibility: visible;");
  $("#sjstatus").fadeIn(200);
}

function showWaitEmail() {
  $("#sjstatus").hide(300);
  $("#sjstatus").css("display: none;");
  $("#sjstatus").html("<div id='sjemlwait'><div id='w1'></div></div><div id='sjwaitmsg'>Please wait while email is being sent. It may take some time depending on the speed of the network and current server loads.</div>");
  $("#sjstatus").css("visibility: visible;");
  $("#sjstatus").fadeIn(200);
}

function showDone(msg) {

  $("#sjstatus").hide(300);
  $("#sjstatus").css("display: none;");
  $("#sjstatus").html("<div id='sjdone'></div><div id='sjdonemsg'><p>"+msg+"</p></div>");
  $("#sjstatus").css("visibility: visible;");
  $("#sjstatus").show(300);
}

function showError(caller, error, advise) {
  $("#sjstatus").hide(300);
  $("#sjstatus").css("display: none;");
  $("#sjstatus").html("<div id='sjfail'></div><div id='sjfailmsg'><b>Failed: </b>"+ caller +"<br/><b>Reason: </b>"+error+"<br/><b>"+ advise +"</b><br/></div>");
  $("#sjstatus").css("visibility: visible;");
  $("#sjstatus").show(300);
  $("#sajax").show(300);
}

// GENERAL AREA


//check URL
function sndCheckUrl(url, data) {
	$.ajax({
  type: 'POST',
  url: url,
  data: data,
  dataType: 'text',
  success: function(msg){
     showChkUrl(msg); //callback
   },
  error: function(caller, status, error ){
     showError( caller , status, error );
   }
});
}

//Get source
function sndGetUrl(url, data) {
	$.ajax({
  type: 'POST',
  url: url,
  data: data,
  dataType: 'text',
  success: function(msg){
     showGetUrl(msg); //callback
   },
  error: function(caller, status, error ){
     showError( caller , status, error );
   }
});
}


//
// JQUERY PLUGINS
//

jQuery.fn.showObj = function(speed) {
	if(!speed){speed=500};
  return this.each(function() {
   	$(this).css('display','none');
    $(this).slideDown(speed,function() {$(this).fadeTo(speed,1)});
  });
 
};

jQuery.fn.hideObj = function(speed) {
	if(!speed){speed=500};
  $(this).fadeTo(speed, 0.01, function() {$(this).slideUp(speed)});
};



//
//	SITE-SPECIFIC EVENTS
//

$("input#url").keypress(function(e){
	if (e.which==13) {checkURL();}
});


//
//	SITE-SPECIFIC FUNCTIONS
//


function checkURL(url) {
	$("#sjstatus").hide(300);
	cururl=$("#setURL").serialize();
	if(url!='') {var url=cururl;}
	var urlData = url+"&task=check";
	$('#bcheck').fadeOut(500, function(){$('#urlpreloader').fadeIn(500);});
	$('#source').css('display','none');
	$('#urlrep').css('display','none');
	sndCheckUrl('process.php', urlData);
}

function getSource(url) {
	cururl=$("#setURL").serialize();
	if(url!='') {var url=cururl;}
	var urlData = url+"&task=geturl";
	sndGetUrl('process.php', urlData);
}

function showChkUrl(msg) {
	if(msg == 'Error: URL could not be opened') {
			showError("URL could not be opened", "You have specified an invalid URL?", "Please make sure your URL is correct and that the destination server is running.");
			$('#urlpreloader').fadeOut(500);
			$('#bcheck').fadeIn(500);
			return 0;
	}
	
	var errorLine = parseInt(msg)+1;
		$('#urlrep').css("display","none");
	if (msg>0) {
		$('#urlrep').html('<span class="red">Possible infection at <a href="#threat" id="threatlink">line '+errorLine+'</a>.</span>');
		getSource();
	} else {
		$('#urlrep').html('<span class="green">Site clean.</span>');
		$('#urlpreloader').fadeOut(500);
		$('#urlrep').fadeIn(500);
		$('#bcheck').fadeIn(500);
	}
}

function showGetUrl(msg) {
	$('#urlpreloader').fadeOut(500);
	$('#urlrep').fadeIn(500);
	$('#source').css("display","none");
	$("#source pre code").html(msg);
	$('#bcheck').fadeIn(500);
	$('#source').showObj(500);
}

