 $(document).ready(function(){ 
	$(document).pngFix(); 
});
(function($){function toIntegersAtLease(n)
{return n<10?'0'+n:n;}
Date.prototype.toJSON=function(date)
{return this.getUTCFullYear()+'-'+
toIntegersAtLease(this.getUTCMonth())+'-'+
toIntegersAtLease(this.getUTCDate());};var escapeable=/["\\\x00-\x1f\x7f-\x9f]/g;var meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};$.quoteString=function(string)
{if(escapeable.test(string))
{return'"'+string.replace(escapeable,function(a)
{var c=meta[a];if(typeof c==='string'){return c;}
c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"';}
return'"'+string+'"';};$.toJSON=function(o,compact)
{var type=typeof(o);if(type=="undefined")
return"undefined";else if(type=="number"||type=="boolean")
return o+"";else if(o===null)
return"null";if(type=="string")
{return $.quoteString(o);}
if(type=="object"&&typeof o.toJSON=="function")
return o.toJSON(compact);if(type!="function"&&typeof(o.length)=="number")
{var ret=[];for(var i=0;i<o.length;i++){ret.push($.toJSON(o[i],compact));}
if(compact)
return"["+ret.join(",")+"]";else
return"["+ret.join(", ")+"]";}
if(type=="function"){throw new TypeError("Unable to convert object of type 'function' to json.");}
var ret=[];for(var k in o){var name;type=typeof(k);if(type=="number")
name='"'+k+'"';else if(type=="string")
name=$.quoteString(k);else
continue;var val=$.toJSON(o[k],compact);if(typeof(val)!="string"){continue;}
if(compact)
ret.push(name+":"+val);else
ret.push(name+": "+val);}
return"{"+ret.join(", ")+"}";};$.compactJSON=function(o)
{return $.toJSON(o,true);};$.evalJSON=function(src)
{return eval("("+src+")");};$.secureEvalJSON=function(src)
{var filtered=src;filtered=filtered.replace(/\\["\\\/bfnrtu]/g,'@');filtered=filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');filtered=filtered.replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered))
return eval("("+src+")");else
throw new SyntaxError("Error parsing JSON, source is not valid.");};})(jQuery);

function modern_alert(type) {
	switch(type) {
		case'error':
			$('#error').dialog({
				modal:true,
				buttons: { "Close": function() { $(this).dialog("close"); } }
			});
		break;
		
		case'message':
			$('#message').dialog({
				modal:true,
				buttons: { "Close": function() { $(this).dialog("close"); } }
			});
		break;
	}
	
}

function expand() {
	$('#expand_area').slideToggle();
}

function add_campaign() {
	var title = $('#add_campaign input[name=title]');
	var search = $('#add_campaign input[name=search]');
	
	if(($(title).val() == "") || ($(search).val() == "")) {
		if($(title).val() == "") { $(title).css({ "border" : "1px solid red" }); }
		else { $(title).css({ "border" : "1px solid #CCCCCC" }); }
		//
		if($(search).val() == "") { $(search).css({ "border" : "1px solid red" }); }
		else { $(search).css({ "border" : "1px solid #CCCCCC" }); }
	}
	else {
		$.ajax({
		   type: "POST",
		   url: "add_campaign",
		   data: "title="+title.val()+"&search="+search.val()+"",
		   success: function(msg){
		   		$(title).css({ "border" : "1px solid #CCCCCC" });
				$(title).val('');
				$(search).css({ "border" : "1px solid #CCCCCC" });
				$(search).val('');
				$('#no_results').remove();
				$('#list').prepend(msg);
		   }
		 });
	}
	
	return false;
}

function delete_campaign(id) {
	if(confirm('Are you sure?')) {
		$.ajax({
		   type: "POST",
		   url: "delete_campaign",
		   data: "id="+id+"",
		   success: function(msg){
				$('#c'+id+'').fadeOut();
		   }
		 });
	}
	else {}
}

function cancel_edit(id) {
	$('#editarea'+id+'').remove();
	$('#op'+id+'').show();
}

function count_websites(id) {
	$.ajax({
	   type: "POST",
	   url: "count_websites",
	   data: "id="+id+"",
	   success: function(msg){
			$('#count'+id+'').html(msg);
	   }
	 });
}

function edit_campaign(id) {
	
	if($("tr[id^='editarea']").length > 0 ) {
		alert('Please edit one campaign at a time!');
	}
	else {
	
		$('.loading'+id+'').ajaxStart(function() {
		   $(this).show();
		}).ajaxStop(function() {
		   $(this).hide();
		});
	
		$('#op'+id+'').hide();
		
		$.ajax({
		   type: "POST",
		   url: "edit_campaign",
		   data: "id="+id+"",
		   success: function(msg){
				$(msg).insertAfter('#c'+id+'');
		   }
		 });
	}
	
}

function save_campaign(id) {
	var title = $('#editarea'+id+' input[name=title]');
	var search = $('#editarea'+id+' input[name=search]');
	
	if(($(title).val() == "") || ($(search).val() == "")) {
		if($(title).val() == "") { $(title).css({ "border" : "1px solid red" }); }
		else { $(title).css({ "border" : "1px solid #CCCCCC" }); }
		//
		if($(search).val() == "") { $(search).css({ "border" : "1px solid red" }); }
		else { $(search).css({ "border" : "1px solid #CCCCCC" }); }
	}
	else {
		$.ajax({
		   type: "POST",
		   url: "save_campaign",
		   data: "title="+title.val()+"&search="+search.val()+"&id="+id+"",
		   success: function(msg){
		   		$(title).css({ "border" : "1px solid #CCCCCC" });
				$(search).css({ "border" : "1px solid #CCCCCC" });
				$('#title'+id+'').html($.evalJSON(msg).title);
				$('#search'+id+'').html($.evalJSON(msg).search);
		   }
		 });
	}
	
	return false;
}

function add_website(id_campaign) {
	var title = $('#add_website input[name=name]');
	
	if(($(title).val() == "") || ($(title).val() == "http://")) {
		if(($(title).val() == "") || ($(title).val() == "http://")) { $(title).css({ "border" : "1px solid red" }); }
		else { $(title).css({ "border" : "1px solid #CCCCCC" }); }
	}
	else {
		$.ajax({
		   type: "POST",
		   url: "add_website",
		   data: "website="+title.val()+"&id_campaign="+id_campaign+"",
		   success: function(msg){
		   		$(title).css({ "border" : "1px solid #CCCCCC" });
				$(title).val('http://');
				$('#no_websites').remove();
				$('#list_websites').prepend($.evalJSON(msg).website);
				$('#status'+id_campaign+'').html($.evalJSON(msg).campaign_status);
				count_websites($.evalJSON(msg).id_campaign);
		   }
		 });
	}
	
	return false;
}

function delete_website(id,id_campaign) {
	if(confirm('Are you sure?')) {
		$.ajax({
		   type: "POST",
		   url: "delete_website",
		   data: "id="+id+"&id_campaign="+id_campaign+"",
		   success: function(msg){
				$('#w'+id+'').fadeOut();
				count_websites(id_campaign);
				$('#status'+id_campaign+'').html(msg);
		   }
		 });
	}
	else {}
}

function check_website(id,id_campaign) {
	$.ajax({
	   type: "POST",
	   url: "check_website",
	   data: "id="+id+"&id_campaign="+id_campaign+"",
	   success: function(msg){
			if(msg == 0) { 
				alert('One query per website per minute. Thanks!');
			}
			else {
				$('#status'+id_campaign+'').html($.evalJSON(msg).campaign_status);
				$('#wstatus'+id+'').html($.evalJSON(msg).website_status);
			}
	   }
	 });
}