<?php

function profile($profile, $edit=false){
	if(!empty($profile)){
		global $DB;
		global $user;
		echo "<div class=\"subscribe\">";
		if(!$edit && $user["username"] != $profile["username"]){
			$subscribed = is_subscribed($profile["username"]);
			$name =  (empty($profile["name"])?$profile["username"]:$profile["name"]);
			echo "<a onclick=\"subscribe('{$profile["username"]}')\"><img id=\"sub_ico\" src=\"images/icons/" . ($subscribed ? "unsubscribe" : "subscribe") . ".png\"></a><br><div id=\"sub_txt\"><small>" . ($subscribed ? "Unsubscribe from {$name}'s events." : "Receive e-mail notifications for {$name}'s events.");
			echo "</small></div>";
			if($user["permissions"]>$profile["permissions"] && $user["permissions"]>=1) {
				echo "<div id=\"block\" onclick=\"javascript:block('{$profile["username"]}')\"><img src=\"images/icons/block.gif\"> block</div>";
			}
		} else if ($user["username"] == $profile["username"] && !$edit) {
			echo "<a href=\"index.php?view=user&details=1\">Edit profile</a>";
		} else {
			echo "<a href=\"index.php?view=user&details=1&name={$profile["username"]}\">Preview</a>";
		}
		echo "</div>";
		
		if($edit) echo "<form action=\"index.php?view=user\" method=\"post\">\n";
		echo "<table>\n";
		echo "<tr><td width=\"200px;\">";
		display_avatar($profile,$edit);

		if(!empty($_POST["profile-submit"]) || !empty($_GET["details"])) $expanded = true;
		else $expanded = false;
		
		echo "</td><td>" . (empty($profile["name"]) ? "<b>{$profile["username"]}</b>" : "<b>{$profile["name"]}</b> {$profile["username"]}") . "<br><br><a href=\"javascript:void(0)\" onclick=\"javascript:expand('expandable')\"><img src=\"images/icons/user.png\"> user details </a><a id=\"expand_link\" href=\"javascript:void(0)\" onclick=\"javascript:expand('expandable')\">" . ($expanded ? "[ - ]" : "[ + ]") . "</a></td></tr>\n";
		echo "</table>\n";
		// expandable starts
		echo "<div id=\"expandable\" style=\"display:" . ($expanded ? "block" : "none") . "\">";
		echo "<table>\n";
		echo "<tr><td width=\"200px;\"></td><td></td></tr>";
		display_text_row($profile,"name",$edit);
		if($edit) display_text_row($profile,"email");
		if($edit||(!empty($profile["birthday_day"])&&!empty($profile["birthday_month"])&&!empty($profile["birthday_year"]))||!empty($profile["gender"])){
		echo <<<JS
			<script src="js/mootools-core-1.4.2-full-compat-yc.js" type="text/javascript"></script>
			<script src="js/mootools-more-1.4.0.1_datepicker_custom_yc.js" type="text/javascript"></script>    <script src="arian-mootools-datepicker-0768c57/Source/Locale.en-US.DatePicker.js" type="text/javascript"></script>
			<script src="js/arian-mootools-datepicker-0768c57/Source/Picker.js" type="text/javascript"></script>
			<script src="js/arian-mootools-datepicker-0768c57/Source/Picker.Attach.js" type="text/javascript"></script>
			<script src="js/arian-mootools-datepicker-0768c57/Source/Picker.Date.js" type="text/javascript"></script>
			<link href="js/arian-mootools-datepicker-0768c57/Source/datepicker_dashboard/datepicker_dashboard.css" rel="stylesheet">
			<script>
			var d=new Date();
			var dMin = new Date(1900,0,1)
			var dMax = new Date((d.getFullYear()-6),11,31)
			window.addEvent('domready', function(){
				new Picker.Date('birthday_full', {
					startView: 'years',
					positionOffset: {x: 5, y: 0},
					pickerClass: 'datepicker_dashboard',
					useFadeInOut: !Browser.ie,
					timePicker: false,
					minDate: dMin,
					maxDate: dMax,
					onSelect: function(date){document.id('birthday_year').value = date.get('year');
					document.id('birthday_month').value = (date.get('month')+1);
					document.id('birthday_day').value = date.get('date');
					document.id('birthday_full').value = (date.get('month')+1)+'/'+(date.get('date'))+'/'+(date.get('year'));} 
				});
			});
			</script>
JS;
			echo "<tr><th style=\"border-top:1px solid #FFFFFF;\" colspan=\"2\">Personal</th></tr>\n";
			if($edit||isset($profile["gender"])){
				echo "<tr><td>Gender:</td><td>\n";
				if($edit){
					echo "<select name=\"gender\" style=\"width: 90px; float: left; \" >\n";
					echo "<option value=\"-1\">Secret</option>\n";
					echo "<option value=\"0\"";
					if ($profile["gender"]!=NULL && $profile["gender"]==0) echo " selected=\"selected\"";
					echo ">Male</option>\n";
					echo "<option value=\"1\"";
					if ($profile["gender"]==1) echo " selected=\"selected\"";
					echo ">Female</option>\n";
					echo "</select>";
				} else {
					echo ($profile["gender"]==1 ? "Female" : "Male");
				}
				echo "</td></tr>\n";
			}


			if($edit||(!empty($profile["birthday_day"])&&!empty($profile["birthday_month"])&&!empty($profile["birthday_year"]))){
				
				echo "<tr><td>Birthday:</td><td>\n";
				if ($edit){
					echo "<input id=\"birthday_day\" name=\"birthday_day\" type='hidden' value=\"{$profile["birthday_day"]}\">\n";
					echo "<input id=\"birthday_month\" name=\"birthday_month\" type='hidden' value=\"{$profile["birthday_month"]}\">\n";
					echo "<input id=\"birthday_year\" name=\"birthday_year\" type='hidden' value=\"{$profile["birthday_year"]}\">\n";
					echo "<input id=\"birthday_full\" type='text' value=\"{$profile["birthday_month"]}/{$profile["birthday_day"]}/{$profile["birthday_year"]}\"><br>";
					} 
				else echo $profile["birthday_month"] . "/" . $profile["birthday_day"] . "/" . $profile["birthday_year"];
				echo "</td></tr>\n";
			}
		}
		if(!empty($profile["phone"]) || !empty($profile["msn"]) || !empty($profile["skype"]) || $edit) {
			echo "<tr><th style=\"border-top:1px solid #FFFFFF;\" colspan=\"2\">Contacts</th></tr>\n";
			display_text_row($profile,"phone",$edit);
			display_text_row($profile,"msn",$edit);
			display_text_row($profile,"skype",$edit);
		}
		echo "<tr><th style=\"border-top:1px solid #FFFFFF;\" colspan=\"2\">Address</th></tr>\n";
		display_text_row($profile,"address",$edit);
		display_text_row($profile,"city",false);
		display_text_row($profile,"state",false);
		display_text_row($profile,"zip",$edit);
		if($edit) echo "<tr><td>Receive subscriptions</td><td><input type=\"checkbox\" name=\"subs\" value=\"Subs\" " . ($profile["subs"] ? "checked=\"checked\"" : "") . "/> </td><tr>\n";
		
		if($edit) 
			echo "<tr><td></td><td><input type=\"submit\" name=\"profile-submit\" value=\"Save Changes\"> <a href=\"index.php?view=user&name={$profile["username"]}\">Preview</a></td></tr>\n";
		echo "</table>";
		echo "</div>";
		// expandable ends
		
		if($edit)
			echo "</form>\n";
		
	//	if(!$edit){	// show events by user
		
			if($_GET["events"] != "all") echo "<hr><center>Upcoming events (<a href=\"index.php?view=user&name={$profile["username"]}&events=all\">show all</a>):</center>";
			else echo "<hr><center>All events by user (<a href=\"index.php?view=user&name={$profile["username"]}\">show upcoming</a>):</center>";
			$sql = "SELECT SQL_CALC_FOUND_ROWS e.*,u.name FROM events e JOIN users u ON e.username=u.username ";
			$sql .= "WHERE e.username='{$profile["username"]}'";
			if($_GET["events"]!="all") $sql .= " AND time>" . (time()-24*3600) . " AND time<" . (time()+14*24*3600);
			$limit = get_page_limit();
			$sql .= " ORDER BY time ASC {$limit};";
			
			$result = $DB->qry($sql);
			$total = $DB->qry("SELECT FOUND_ROWS();",3);
			search_results($result,$total,make_get_url());
	//	}
		
	} else {
		echo "Invalid user";
	}
}

function edit_profile($username){
	if(empty($username)) return;
	$username = str_replace("'","''",$username);
	
	global $DB;
	if(isset($_POST["name"])) {
		$name = str_replace("'","''",ucfirst(strtolower($_POST["name"])));
		$DB->qry("UPDATE users SET name='{$name}' WHERE username='{$username}';");
	}
	if(isset($_POST["birthday_day"]) && isset($_POST["birthday_month"]) && isset($_POST["birthday_year"])) {
		if(!empty($_POST["birthday_day"])&&!empty($_POST["birthday_month"])&&!empty($_POST["birthday_year"])){
			$day = intval($_POST["birthday_day"]);
			$month = intval($_POST["birthday_month"]);
			$year = intval($_POST["birthday_year"]);
		} else {
			$day = 0;
			$month = 0;
			$year = 0;
		}
		$DB->qry("UPDATE users SET birthday_day={$day}, birthday_month={$month}, birthday_year={$year} WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if(isset($_POST["address"])) {
		$address = str_replace("'","''",$_POST["address"]);
		$DB->qry("UPDATE users SET address='{$address}' WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if(isset($_POST["gender"])) {
		$gender = intval($_POST["gender"]);
		if($gender==-1) $gender="NULL";
		$DB->qry("UPDATE users SET gender={$gender} WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if(isset($_POST["phone"])) {
		$phone = str_replace("'","''",$_POST["phone"]);
		$DB->qry("UPDATE users SET phone='{$phone}' WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if(isset($_POST["msn"])) {
		$msn = str_replace("'","''",$_POST["msn"]);
		$DB->qry("UPDATE users SET msn='{$msn}' WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if(isset($_POST["skype"])) {
		$skype = str_replace("'","''",$_POST["skype"]);
		$DB->qry("UPDATE users SET skype='{$skype}' WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if(isset($_POST["subs"])) {
		$DB->qry("UPDATE users SET subs=1 WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	} else {
		$DB->qry("UPDATE users SET subs=0 WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	return true;
}

function reload_user(){
	global $DB, $user;
	if(isset($user)){
		$user = $DB->qry("SELECT u.*,z.city,z.state_prefix AS state,z.lat,z.lon FROM users u LEFT JOIN zip_code z ON u.zip=z.zip_code WHERE username='{$user["username"]}';",4);
		if($DB->numrows()==0) {
			$user = null;
		}
	}
}


function display_text_row($array,$key,$edit=false){
	if(!empty($array[$key])||$edit){
		echo "<tr><td>" . ucfirst(strtolower($key)) . ": </td>";
		if($edit) echo "<td><input type=\"text\" name=\"{$key}\" value=\"{$array[$key]}\"></td></tr>\n";
		else echo "<td>{$array[$key]}</td></tr>\n";
	}	
}

?>