<?
if($_GET['action']=="funddelete" && $_GET['delete']) {
	//first lookup all the sponsorships inside the fund
	$id=intval($_GET['delete']);
	$q=mysql_query("SELECT * FROM fundraising WHERE id='$id' AND year='".$config['FAIRYEAR']."'");
	$f=mysql_fetch_object($q);
	//hold yer horses, no deleting system funds!
	if($f) {
		if($f->system=="no") {
			mysql_query("DELETE FROM sponsorships WHERE fundraising_type='".mysql_real_escape_string($f->type)."' AND year='".$config['FAIRYEAR']."'");
			mysql_query("DELETE FROM fundraising WHERE id='$id'");
			if(mysql_affected_rows())
				message_push(happy(i18n("Successfully removed fund %1",array($f->name))));
		}
		else {
			message_push(error(i18n("Cannot remove system fund")));
		}
	}
}
if($_POST['action']=="fundedit" || $_POST['action']=="fundadd") {
	$fundraising_id=intval($_POST['fundraising_id']);
	if($fundraising_id) {
		$q=mysql_query("SELECT * FROM fundraising WHERE id='$fundraising_id'");
		$f=mysql_fetch_object($q);
		$system=$f->system;
	}
	$name=mysql_real_escape_string($_POST['name']);
	$type=mysql_real_escape_string($_POST['type']);
	$description=mysql_real_escape_string($_POST['description']);
	$goal=intval($_POST['goal']);
}

if($_POST['action']=="fundedit") {
	if( ($system=="yes" && $goal) || ($system=="no" && $goal && $type && $name) ) {
		if($system=="yes") {
			mysql_query("UPDATE fundraising SET goal='$goal', description='$description' WHERE id='$fundraising_id'");
		}
		else {
			mysql_query("UPDATE fundraising SET goal='$goal', description='$description', type='$type', name='$name' WHERE id='$fundraising_id'");
		}
		if(mysql_error())
			message_push(error(mysql_error()));
		else
			message_push(happy(i18n("Saved fund changes")));
	}
	else {
		message_push(error(i18n("Required fields were missing, please try again")));
	}
	
}
if($_POST['action']=="fundadd") {
	if( $goal && $type && $name) {
		mysql_query("INSERT INTO fundraising (type,name,description,system,goal,year) VALUES ('$type','$name','$description','no','$goal','{$config['FAIRYEAR']}')");
		message_push(happy(i18n("Added new fund")));
	}
	else
		message_push(error(i18n("Required fields were missing, please try again")));
	if(mysql_error())
		message_push(error(mysql_error()));
}

?>
