$().ready(function() {

	function findValueCallback(event, data, formatted) {
		$("").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	}
	
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}
	
	$("#suggest1").autocomplete(charities).result(function(event, item) {
  location.href = "index.php?action=charity&charity="+item;
});

	$("#suggest2").autocomplete(charities).result(function(event, item) {
  location.href = "index.php?action=charity&charity="+item;
});

	$("#suggest3").autocomplete(charities).result(function(event, item) {
  charity_link(item);
});

	$("#suggest4").autocomplete(charities).result(function(event, item) {
  charity_search(item);
});

	$("#suggest5").autocomplete(charities).result(function(event, item) {
  charity_search(item);
});


});

function charity_link(item) {
citem = ""+item+"";
match = new RegExp(" ", "ig");
clink = citem.replace(match, "%20");
document.getElementById('link').value = "http://www.search2give.co.uk/support?"+clink;
}

function charity_search(item) {
citem = ""+item+"";
match = new RegExp(" ", "ig");
clink = citem.replace(match, " ");
clink2 = citem.replace(match, "%20");
chlink = "http://www.search2give.co.uk/support?"+clink2;
form_action = "http://www.search2give.co.uk/";
//document.getElementById('link').value = "<form action=\"http://www.search2give.co.uk/\" method='GET'><input type="hidden" name="action" value="charity" /><input type="hidden" name="charity" value="+item+" /><input type='text' name='query' /></form>";
box1 = '<div style="font-size:14px;  margin-bottom:10px;">Search the web to support <br /> your favourite charity:</div><form method="GET" action="http://search2give.co.uk/results.php" target="blank" style="margin-bottom:5px;"><input type="hidden" name="loc" value="1" /><input type="hidden" name="type" value="web" /><input type="hidden" name="action" value="charity" /><input type="hidden" name="charity" value="'+item+'" /><input style="width:180px; border:1px solid #7f9db9; font-family:arial;" type="text" name="query" value="" /><input name="btnG" value="Search" class="form" type="submit"></style></form><span style=" font-size:11px;  width:130px;">Powered by:</span><br /><a href="'+chlink+'"><img src="http://www.search2give.co.uk/images/logolink1.gif" width="179" height="43" alt="Search2Give" border="0"></a>';
box2 = '<span style="font-size:13px; width:130px;">Search the web:</span><form method="GET" action="http://search2give.co.uk/results.php" target="blank" style="margin-bottom:5px; margin-top:0px"><input type="hidden" name="loc" value="1" /><input type="hidden" name="type" value="web" /><input type="hidden" name="action" value="charity" /><input type="hidden" name="charity" value="'+item+'" /><input style="width:125px; border:1px solid #7f9db9; font-family:arial;" type="text" name="query" value="" /><input name="btnG" value="Go" class="form" type="submit"></form><span style=" font-size:11px; width:130px;">Powered by:</span><br /><a href="'+chlink+'"><img src="http://www.search2give.co.uk/images/logolink2.gif" width="150" height="33" alt="Search2Give" border="0"></a>';
box3 = '<form method="GET" action="http://search2give.co.uk/results.php" target="blank" style="margin-bottom:5px;"><input type="hidden" name="loc" value="1" /><input type="hidden" name="type" value="web" /><input type="hidden" name="action" value="charity" /><input type="hidden" name="charity" value="'+item+'" /><input class="s2gbox" type="text" name="query" value="" /><style>.s2gbox{border:1px solid #7f9db9; width:150px; font-family:arial; background:url(http://search2give.co.uk/images/searchboxlogo.gif) no-repeat #ffffff;}.s2gbox:focus {background: #ffffff}</style><input name="btnG" value="Search" class="form" type="submit"></form>';
box4 = '<table style="border:1px solid #d2e2e7"><tr><td rowspan="2"><a href="'+chlink+'"><img src="http://www.search2give.co.uk/images/icon5.jpg" width="100" height="100" alt="Search2Give" border="0"></a></td><td style="width:265px; height:63px; padding-left:10px; font-size:16px; line-height:130%;">Support your favourite charity by searching the web!</td></tr><tr><td valign="top" style="padding-left:10px; height:37px;"><form method="GET" action="http://search2give.co.uk/results.php" target="blank"><input type="hidden" name="loc" value="1" /><input type="hidden" name="type" value="web" /><input type="hidden" name="action" value="charity" /><input type="hidden" name="charity" value="'+item+'" /><input style="width:180px; border:1px solid #7f9db9; font-family:arial;" type="text" name="query" value="" /><input name="btnG" value="Search" class="form" type="submit"></form></td></tr></table>';
box5 = '<table style="border:1px solid #d2e2e7" cellspacing="5"><tr><td rowspan="2" style="padding-left:5px;"><a href="'+chlink+'"><img src="http://search2give.co.uk/images/icon6.gif" border="0"></a></td><td style="padding-left:15px; font-size:16px;" valign="bottom">Search the web to support your favourite charity:</td></tr><tr><td valign="top" style="padding-left:15px;"><form method="GET" action="http://search2give.co.uk/results.php" target="blank"><input type="hidden" name="loc" value="1" /><input type="hidden" name="type" value="web" /><input type="hidden" name="action" value="charity" /><input type="hidden" name="charity" value="'+item+'" /><input style="border:1px solid #7f9db9; width:400px; height:20px; font-size:17px; font-family:arial;" type="text" name="query" value="" /><input style="height:27px; margin-right:5px;" name="btnG" value="Search" class="form" type="submit"></form></td></tr></table>';
document.getElementById('box1').value = box1;
document.getElementById('box2').value = box2;
document.getElementById('box3').value = box3;
document.getElementById('box4').value = box4;
document.getElementById('box5').value = box5;

document.getElementById('ctext').innerHTML = "Searchers using your search box will now support <b>"+item+"</b>. Pick your favourite search box and get the codes <b>below:</b>";

}