<?php

						/*************************************\
						
						    Handshakes Professional (3.3)
						            by DZOIC LTD
						       Copyright(c) 2005-2007
						        http://www.dzoic.com
						
						\*************************************/

						///////////////////////////////////////

						admin_validate_section("customize",3);
						$action = $GLOBALS["Get"]->val("action");

							switch($action){

								case '':
									profile_sections();
									break;
									
								case 'edit':
									edit_profile_section();
									break;
									
								case 'copy':
									copy_profile_section();
									break;
									
								case 'repos_input_item':
									reposition_input_item();
									break;
									
								case 'repos_output_item':
									reposition_output_item();
									break;
									
								case 'edit_input_item':
									edit_input_item();
									break;
									
								case 'save_input_item':
									save_input_item();
									break;
									
								case 'delete_item':
									delete_item();
									break;
									
								case 'edit_output_item':
									edit_output_item();
									break;
									
								case 'save_output_item':
									save_output_item();
									break;
									
								case 'create':
									create_profile_section();
									break;
									
								case 'delete':
									delete_profile_section();
									break;

							}//switch

						///////////////////////////////////////
						
function create_profile_section(){
	
	admin_validate();
	
	$names["section_name"] = stripslashes(html_entity_decode($GLOBALS["Get"]->val("section_name")));
	$names["account_page"] = stripslashes(html_entity_decode($GLOBALS["Get"]->val("account_page")));
	$names["profile_page"] = stripslashes(html_entity_decode($GLOBALS["Get"]->val("profile_page")));
	$theme = $GLOBALS["Get"]->val("theme");
	
	$name = ereg_replace("[^A-Za-z|0-9|_]","_",strtolower($names["section_name"]));
	$name = ereg_replace("[_]+","_",$name);	
	
	if(empty($name) || empty($names["account_page"]) || empty($names["profile_page"])){
		$GLOBALS["aerr_id"] = 10;
		profile_sections();
	}//if
	elseif($names["account_page"] == $names["profile_page"]){
		$GLOBALS["aerr_id"] = 30;
		profile_sections();
	}//elseif
	
	$new_profile_section = new_profile_section($names,$theme);
		 
	load_page("index.php?section=profile_sections&action=edit&theme=".$theme."&profile=".$new_profile_section);	
	
}//create_profile_section

function new_profile_section($names,$theme){
	
	extract($names);
	
	$name = ereg_replace("[^A-Za-z|0-9|_]","_",strtolower($section_name));
	$name = ereg_replace("[_]+","_",$name);	
	
	include(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php");	
	
	$exists = 0;
	foreach($profile_sections as $psection){
		if($psection["section"] == $name){
			$exists = 1;
			break;
		}//if
	}//foreach
	
	if($exists || !eregi("^[a-z]",$name)){
		mt_srand((double) microtime() * 100000);
		$name = "section_".hs_substr(md5(time().mt_rand(0,1000)),0,5);
	}//if
	
	//loading default data
	$default_key     = @min(array_keys($profile_sections));
	$default_form    = form_data($theme,$profile_sections[$default_key]["form"]);
	$default_output  = output_data($theme,$profile_sections[$default_key]["profile"]);
	$default_section = $profile_sections[$default_key]["section"];
	
	//alter DB
	$sql_query = "SHOW FIELDS FROM profile_access";
	$result = $GLOBALS["DB"]->result($sql_query);
	$token_fields = array();
	foreach($result as $res){
		$token_fields[] = $res["Field"];
	}//foreach
	if(!in_array($name,$token_fields)){
		$sql_query = "ALTER TABLE `profile_access` ADD `".$name."` ENUM('0','1','2','3','4') NOT NULL DEFAULT '0'";
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	
	//new form
	$form["start"] = "<form action=\"index.php\" method=\"post\">
	<input type=\"hidden\" name=\"handler\" value=\"account\">
	<input type=\"hidden\" name=\"action\" value=\"".$name."\">
	<input type=\"hidden\" name=\"next\" value=\"\">";	
	$form["end"] = "</form>";	
	$form["src"] = "db";
	$form["id"] = "mem_id";
	$form["err_page"] = "account";
	$form["err_section"] = $name;
	$form["hidden"] = array();
	$default_item = array_search($default_section,$default_form["names"]);
	$form["labels"]["0"] = $default_form["labels"][$default_item];
	$form["fields"]["0"] = $default_form["fields"][$default_item];
	$form["types"]["0"]  = $default_form["types"][$default_item];
	$form["names"]["0"]  = $name;
	$form["assoc"] = array();
	$form["assoc"]["profile_access"] = $name;
	$form["txt_buttons"] = $default_form["txt_buttons"];	
	$form["img_buttons"] = $default_form["img_buttons"];
	$form["but_action"]  = $default_form["but_action"];
	$form["but_onclick"] = $default_form["but_onclick"];
	$form["rules"] = array();
	$form["search_assoc"] = array();
	save_form_data($theme,"account_".$name,$form);	
	
	//new output
	$output["labels"] = array();
	$output["names"]  = array();
	$output["types"]  = array();
	$output["assoc"]  = array();
	$output["id"]     = "mem_id";
	save_output_data($theme,"profile_".$name,$output);
	
	//adding new profile section
	$new_key = @max(array_keys($profile_sections))+1;
	$profile_sections[$new_key]["name"] = $section_name;
	$profile_sections[$new_key]["section"] = $name;
	$profile_sections[$new_key]["form"] = "account_".$name;
	$profile_sections[$new_key]["profile"] = "profile_".$name;
	$write_str = "<?php\n\n";
	$write_str.= go_through_array("\$profile_sections",$profile_sections);
	$write_str.= "\n?>";
	$file_handler = fopen(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php","w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	$new_profile_section = $new_key;
	
	//updating account menu
	if(file_exists(DOC_ROOT."/themes/".$theme."/menus/account.inc.php")){
		$menu_data = menu_data($theme,"account");
		$new_key = @max(array_keys($menu_data["items"]))+1;
		$menu_data["items"][$new_key]["on"]    = "<b>".$section_name."</b>";		
		$menu_data["items"][$new_key]["off"]   = $section_name;
		$menu_data["items"][$new_key]["link"]  = "index.php?page=account&section=".$name;
		$menu_data["items"][$new_key]["page"]  = "account_".$name;
		$menu_data["items"][$new_key]["group"] = "";
		save_menu_data($theme,"account",$menu_data);
	}//if
	
	//updating profile menu
	if(file_exists(DOC_ROOT."/themes/".$theme."/menus/profile.inc.php")){
		$menu_data = menu_data($theme,"profile");
		$new_key = @max(array_keys($menu_data["items"]))+1;
		$menu_data["items"][$new_key]["on"]    = "<b>".$section_name."</b>";		
		$menu_data["items"][$new_key]["off"]   = $section_name;
		$menu_data["items"][$new_key]["link"]  = "index.php?section=".$name;
		$menu_data["items"][$new_key]["page"]  = "profile_".$name;
		$menu_data["items"][$new_key]["group"] = "";
		save_menu_data($theme,"profile",$menu_data);
	}//if
	
	//adding new pages: account page
	$page["forms"] = array("account_".$name);
	$sql_query = "SELECT association FROM associations WHERE assoc_type='auth'
				  AND theme='".$theme."' AND page='account_".$default_section."'";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$page["auth"][] = $res["association"];
	}//foreach
	
	$sql_query = "SELECT association FROM associations WHERE assoc_type='menu'
				  AND theme='".$theme."' AND page='account_".$default_section."'";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$page["menus"][] = $res["association"];
	}//foreach
	
	$sql_query = "SELECT association FROM associations WHERE assoc_type='task'
				  AND theme='".$theme."' AND page='account_".$default_section."'";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$page["tasks"][] = $res["association"];
	}//foreach
	
	$sql_query = "SELECT code,onload FROM javascripts WHERE page='account_".$default_section."' AND theme='".$theme."'";
	$js = $GLOBALS["DB"]->row($sql_query);
	
	$page["javascript"]["code"]   = $js["code"];
	$page["javascript"]["onload"] = $js["onload"];
	include(DOC_ROOT."/themes/".$theme."/page_groups.inc.php");
	while(list($key,$val) = each($page_groups)){
		$pages = explode(",",$val["pages"]);
		if(in_array("account_".$default_section,$pages))
			$page["groups"][] = $key;
	}//while
	
	$sql_query = "SELECT message FROM tips WHERE page='account_".$default_section."' AND theme='".$theme."'";
	$tip = $GLOBALS["DB"]->single($sql_query);	
	$page["tip"]   = $tip;
	$page["title"] = $account_page;
	$page["items_file"] = "account_".$name;
	
	add_new_page($theme,$page);	
	
	copy(DOC_ROOT."/themes/".$theme."/layout/account_".$default_section.".inc.php",
		 DOC_ROOT."/themes/".$theme."/layout/account_".$name.".inc.php");
		 
	$page = array();
		 
	//adding new pages: profile page
	$sql_query = "SELECT association FROM associations WHERE assoc_type='auth'
				  AND theme='".$theme."' AND page='profile_".$default_section."'";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$page["auth"][] = $res["association"];
	}//foreach
	
	$sql_query = "SELECT association FROM associations WHERE assoc_type='menu'
				  AND theme='".$theme."' AND page='profile_".$default_section."'";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$page["menus"][] = $res["association"];
	}//foreach
	
	$sql_query = "SELECT association FROM associations WHERE assoc_type='task'
				  AND theme='".$theme."' AND page='profile_".$default_section."'";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$page["tasks"][] = $res["association"];
	}//foreach
	
	$sql_query = "SELECT code,onload FROM javascripts WHERE page='profile_".$default_section."' AND theme='".$theme."'";
	$js = $GLOBALS["DB"]->row($sql_query);
	
	$page["javascript"][code]   = $js[code];
	$page["javascript"][onload] = $js[onload];
	include(DOC_ROOT."/themes/".$theme."/page_groups.inc.php");
	while(list($key,$val) = each($page_groups)){
		$pages = explode(",",$val["pages"]);
		if(in_array("profile_".$default_section,$pages))
			$page["groups"][] = $key;
	}//while
	
	$sql_query = "SELECT message FROM tips WHERE page='profile_".$default_section."' AND theme='".$theme."'";
	$tip = $GLOBALS["DB"]->single($sql_query);	
	$page["tip"]   = $tip;
	$page["title"] = $profile_page;
	$page["items_file"] = "profile_".$name;
	
	add_new_page($theme,$page);
	
	copy(DOC_ROOT."/themes/".$theme."/layout/profile_".$default_section.".inc.php",
		 DOC_ROOT."/themes/".$theme."/layout/profile_".$name.".inc.php");
		 
	return $new_profile_section;
	
}//new_profile_section

function delete_profile_section(){
	
	admin_validate();
	
	$theme   = $GLOBALS["Get"]->val("theme");
	$profile = $GLOBALS["Get"]->val("profile");
	
	include(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php");
	
	$section_name = $profile_sections[$profile]["section"];
	$form_name    = $profile_sections[$profile]["form"];
	$output_name  = $profile_sections[$profile]["profile"];
	
	//delete from menus
	if(file_exists(DOC_ROOT."/themes/".$theme."/menus/account.inc.php")){
		$menu_data = menu_data($theme,"account");
		while(list($key,$val) = each($menu_data["items"])){
			if($val["page"] == "account_".$section_name)
				unset($menu_data["items"][$key]);
		}//while
		save_menu_data($theme,"account",$menu_data);
	}//if
	
	//updating profile menu
	if(file_exists(DOC_ROOT."/themes/".$theme."/menus/profile.inc.php")){
		$menu_data = menu_data($theme,"profile");
		while(list($key,$val) = each($menu_data["items"])){
			if($val["page"] == "profile_".$section_name)
				unset($menu_data["items"][$key]);
		}//while
		save_menu_data($theme,"profile",$menu_data);
	}//if
	
	//deleting form
	$form_data = form_data($theme,$form_name);
	while(list($key,$val) = each($form_data["names"])){
		if($val != $section_name){
			$data["theme"] = $theme;
			$data["profile"] = $profile;
			$data["form_name"] = $form_name;
			$data["item"] = $key;
			delete_item($data);
		}//if
	}//while
	
	//delete from profile sections
	unset($profile_sections[$profile]);
	$write_str = "<?php\n\n";
	$write_str.= go_through_array("\$profile_sections",$profile_sections);
	$write_str.= "\n?>";
	$file_handler = fopen(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php","w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
		
	//remove files
	@unlink(DOC_ROOT."/themes/".$theme."/forms/account_".$section_name.".inc.php");
	@unlink(DOC_ROOT."/themes/".$theme."/profiles/profile_".$section_name.".inc.php");
	
	//delete pages
	delete_page($theme,"account_".$section_name);
	delete_page($theme,"profile_".$section_name);
	
	//alter DB
	/* deprecated! might brake other themes *\
	$sql_query = "ALTER TABLE `profile_access` DROP `".$section_name."`";
	$GLOBALS["DB"]->execute($sql_query);
	\* deprecated! might brake other themes */
	
	load_page("index.php?section=profile_sections&theme=".$theme);
	
}//delete_profile_section
						
function profile_sections(){
	
	admin_validate();
	
	$data["themes"] = admin_get_themes();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(!$theme)
		$theme = $GLOBALS["THEME"];
		
	$data["theme"] = $theme;
	
	$data["section"] = "customize";
	$data["subsection"] = 3;
	$data["title_id"] = "44";	
	include(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php");
	
	$data["profiles"] = array();
	
	while(list($key,$val) = each($profile_sections)){
		$profile["name"] = $val["name"];
		$profile["edit"] = "index.php?section=profile_sections&action=edit&theme=".$theme."&profile=".$key;
		$profile["copy"] = "index.php?section=profile_sections&action=copy&theme=".$theme."&profile=".$key;
		$profile["delete"] = "index.php?section=profile_sections&action=delete&theme=".$theme."&profile=".$key;
		$data["profiles"][] = $profile;
	}//while
	
	$vals = array("section_name","account_page","profile_page");
	$data["values"] = $GLOBALS["Get"]->arr($vals,1);
	
	$layout[0] = "profile_sections_list";
	
	admin_output($layout,$data);
	
}//profile_sections

function edit_profile_section(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$profile = $GLOBALS["Get"]->val("profile");
	
	include(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php");
	
	$form_name = $profile_sections[$profile]["form"];
	$output_name = $profile_sections[$profile]["profile"];
	
	$form_data = form_data($theme,$form_name);
	
	$data["input_items"] = array();
	$data["output_items"] = array();
	$data["form"] = $form_name;
	
	$exclude_default = array_search($profile_sections[$profile]["section"],$form_data["names"]);
	if(!extract_null_value($exclude_default))
		unset($form_data["names"][$exclude_default]);
		
	$keys = array_keys($form_data["names"]);
	
	$start = $keys[0];
	$i = count($keys)-1;
	$end = $keys[$i];
	while(list($key,$val) = each($form_data["names"])){		
		$field = form_extract_field($form_data["fields"][$key],$theme);
		$item["label"]     = $form_data["labels"][$key];		
		$item["field"]     = $field["type"];
		$item["edit"]      = "index.php?section=profile_sections&action=edit_input_item&profile=".$profile."&form=".$form_name."&theme=".$theme."&item=".$key;
		$item["delete"]    = "index.php?section=profile_sections&action=delete_item&profile=".$profile."&form=".$form_name."&theme=".$theme."&item=".$key;
		if($key != $start)
			$item["move_up"] = "index.php?section=profile_sections&action=repos_input_item&profile=".$profile."&theme=".$theme."&form=".$form_name."&item=".$key."&direction=up";
		else
			$item["move_up"] = "";
		if($key != $end)
			$item["move_down"] = "index.php?section=profile_sections&action=repos_input_item&profile=".$profile."&theme=".$theme."&form=".$form_name."&item=".$key."&direction=down";
		else
			$item["move_down"] = "";
		$data["input_items"][] = $item;
	}//while
	
	$output_data = output_data($theme,$output_name);
	$keys = array_keys($output_data["names"]);
	
	$start = $keys[0];
	$i = count($keys)-1;
	$end = $keys[$i];
	while(list($key,$val) = each($output_data["names"])){
		$field = output_extract_field($output_data["types"][$key],$theme);
		$item["label"]     = $output_data["labels"][$key];		
		$item["field"]     = $field["type"];
		$item["edit"]      = "index.php?section=profile_sections&action=edit_output_item&profile=".$profile."&output=".$output_name."&theme=".$theme."&item=".$key;
		if($key != $start)
			$item["move_up"] = "index.php?section=profile_sections&action=repos_output_item&theme=".$theme."&output=".$output_name."&item=".$key."&direction=up";
		else
			$item["move_up"] = "";
		if($key != $end)
			$item["move_down"] = "index.php?section=profile_sections&action=repos_output_item&theme=".$theme."&output=".$output_name."&item=".$key."&direction=down";
		else
			$item["move_down"] = "";
		$data["output_items"][] = $item;
	}//while
	
	$data["theme"]   = $theme;
	$data["profile"] = $profile;
	$data["section_name"] = $profile_sections[$profile]["name"];
	
	$data["section"] = "customize";
	$data["subsection"] = 3;
	$data["title_id"] = "44";	
	$data["link"] = "index.php?section=profile_sections&theme=".$theme;
	
	$layout[0] = "edit_profile_section";
	
	admin_output($layout,$data);
	
}//edit_profile_section

function copy_profile_section(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$profile = $GLOBALS["Get"]->val("profile");
	
	$copy = $GLOBALS["Get"]->type_val("copy","integer");
	
	if(!$copy){
		
		unset($profile_sections);
		include(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php");
	
		$data["theme"]   = $theme;
		$data["profile"] = $profile;
		$data["section_name"] = $profile_sections[$profile]["name"];
		
		$themes = admin_get_themes();
		$sections = array();
		foreach($themes as $key => $val){
			$sections[$key] = $val.": Create New Section";
			unset($profile_sections);
			include(DOC_ROOT."/themes/".$key."/profile_sections.inc.php");
			foreach($profile_sections as $k => $ps){
				$newkey = $key."||".$k;
				$sections[$newkey] = $val.": ".$ps["name"];
			}//foreach
		}//foreach
		
		$data["sections"] = $sections;
		
		$data["values"]["replace"] = "replace";
	
		$data["section"] = "customize";
		$data["subsection"] = 3;
		$data["title_id"] = "44";	
		$data["link"] = "index.php?section=profile_sections&theme=".$theme;
	
		$layout[0] = "copy_profile_section";
	
		admin_output($layout,$data);
		
	}//if
	else{
		
		$copy_to = $GLOBALS["Get"]->val("copy_to");
		$replace = $GLOBALS["Get"]->val("replace");
		
		$dist = explode("||",$copy_to);
		
		$dtheme = $dist[0];
		$dsection = $dist[1];
		
		include(DOC_ROOT."/themes/".$theme."/titles.inc.php");
		
		unset($profile_sections);
		include(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php");
		$sprofile_sections = $profile_sections;
		
		if($dsection){
			
			unset($profile_sections);
			include(DOC_ROOT."/themes/".$dtheme."/profile_sections.inc.php");
			$dprofile_sections = $profile_sections;
			
			if(!isset($dprofile_sections[$dsection]))
				$dsection = 0;
			else{
				
				if($replace == "replace"){
					
					copy(DOC_ROOT."/themes/".$theme."/forms/".$sprofile_sections[$profile]["form"].".inc.php",
						 DOC_ROOT."/themes/".$dtheme."/forms/".$dprofile_sections[$dsection]["form"].".inc.php");
				 
					copy(DOC_ROOT."/themes/".$theme."/profiles/".$sprofile_sections[$profile]["profile"].".inc.php",
						 DOC_ROOT."/themes/".$dtheme."/profiles/".$dprofile_sections[$dsection]["profile"].".inc.php");
					
				}//if
				elseif($replace == "merge"){
					
					$mform = merge_form(DOC_ROOT."/themes/".$theme."/forms/".$sprofile_sections[$profile]["form"].".inc.php",
										DOC_ROOT."/themes/".$dtheme."/forms/".$dprofile_sections[$dsection]["form"].".inc.php",
										"form");
								   
					save_form_data($dtheme,$dprofile_sections[$dsection]["form"],$mform);
				 
					$mprofile = merge_profile(DOC_ROOT."/themes/".$theme."/profiles/".$sprofile_sections[$profile]["profile"].".inc.php",
											  DOC_ROOT."/themes/".$dtheme."/profiles/".$dprofile_sections[$dsection]["profile"].".inc.php",
											  "output");
									  
					save_output_data($dtheme,$dprofile_sections[$dsection]["profile"],$mprofile);
					
				}//elseif
				
			}//else
			
		}//if
		if(!$dsection){
			
			$section_name = $sprofile_sections[$profile]["name"];
			$key = "account_".$sprofile_sections[$profile]["section"];
			$account_page = $titles[$key];
			$key = "profile_".$sprofile_sections[$profile]["section"];
			$profile_page = $titles[$key];
			
			unset($profile_sections);
			include(DOC_ROOT."/themes/".$dtheme."/profile_sections.inc.php");
			$dprofile_sections = $profile_sections;
			
			$exists = 0;
			foreach($dprofile_sections as $key => $val){
				if($val["section"] == $sprofile_sections[$profile]["section"]){
					$exists = 1;
					break;
				}//if
			}//foreach
			
			if($exists){
				$section_name.= " (2)";
				$account_page.= " (2)";
				$profile_page.= " (2)";
			}//if
		
			$names["section_name"] = $section_name;
			$names["account_page"] = $account_page;
			$names["profile_page"] = $profile_page;
			$new_section = new_profile_section($names,$dtheme);
			
			unset($profile_sections);
			include(DOC_ROOT."/themes/".$dtheme."/profile_sections.inc.php");
			$dprofile_sections = $profile_sections;
			
			copy(DOC_ROOT."/themes/".$theme."/forms/".$sprofile_sections[$profile]["form"].".inc.php",
				 DOC_ROOT."/themes/".$dtheme."/forms/".$dprofile_sections[$new_section]["form"].".inc.php");
				 
			copy(DOC_ROOT."/themes/".$theme."/profiles/".$sprofile_sections[$profile]["profile"].".inc.php",
				 DOC_ROOT."/themes/".$dtheme."/profiles/".$dprofile_sections[$new_section]["profile"].".inc.php");
			
		}//if		
			 
		load_page("index.php?section=profile_sections&theme=".$dtheme);
		
	}//else
	
}//copy_profile_section

function reposition_input_item(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$profile = $GLOBALS["Get"]->val("profile");
	$form_name = $GLOBALS["Get"]->val("form");
	$item = $GLOBALS["Get"]->val("item");
	$direction = $GLOBALS["Get"]->val("direction");
	
	include(DOC_ROOT."/themes/".$theme."/forms/".$form_name.".inc.php");
	
	$keys = array_keys($form["names"]);
	$currkey = array_search($item,$keys);
	if($direction == "up")
		$newposition = $keys[$currkey-1];
	elseif($direction == "down")
		$newposition = $keys[$currkey+1];
		
	list($form["labels"][$item],$form["labels"][$newposition]) = array ($form["labels"][$newposition],$form["labels"][$item]);
	list($form["names"][$item],$form["names"][$newposition]) = array ($form["names"][$newposition],$form["names"][$item]);
	list($form["fields"][$item],$form["fields"][$newposition]) = array ($form["fields"][$newposition],$form["fields"][$item]);
	list($form["types"][$item],$form["types"][$newposition]) = array ($form["types"][$newposition],$form["types"][$item]);
	
	save_form_data($theme,$form_name,$form);
	
	load_page($GLOBALS["Get"]->referer());
	
}//reposition_input_item

function reposition_output_item(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$profile = $GLOBALS["Get"]->val("profile");
	$output_name = $GLOBALS["Get"]->val("output");
	$item = $GLOBALS["Get"]->val("item");
	$direction = $GLOBALS["Get"]->val("direction");
	
	include(DOC_ROOT."/themes/".$theme."/profiles/".$output_name.".inc.php");
		
	$keys = array_keys($output["names"]);
	$currkey = array_search($item,$keys);
	if($direction == "up")
		$newposition = $keys[$currkey-1];
	elseif($direction == "down")
		$newposition = $keys[$currkey+1];
		
	list($output["labels"][$item],$output["labels"][$newposition]) = array ($output["labels"][$newposition],$output["labels"][$item]);
	list($output["names"][$item],$output["names"][$newposition]) = array ($output["names"][$newposition],$output["names"][$item]);
	list($output["types"][$item],$output["types"][$newposition]) = array ($output["types"][$newposition],$output["types"][$item]);
	
	save_output_data($theme,$output_name,$output);
	
	load_page($GLOBALS["Get"]->referer());
	
}//reposition_output_item

function edit_input_item(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$profile = $GLOBALS["Get"]->val("profile");
	$form_name = $GLOBALS["Get"]->val("form");
	$item = $GLOBALS["Get"]->val("item");
	
	$form_data = form_data($theme,$form_name);
	
	$data["label"] = $form_data["labels"][$item];
	$data["field"] = form_extract_field($form_data["fields"][$item],$theme);
	$data["type"]  = $data["field"]["system_type"];	
	$data["link"]  = "index.php?section=profile_sections&action=edit&theme=".$theme."&profile=".$profile;	
	
	$data["is_searched"] = is_searched_item($theme,$form_data["names"][$item]);
	
	include(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php");
	$data["theme"]        = $theme;
	$data["profile"]      = $profile;
	$data["section_name"] = $profile_sections[$profile]["name"];
	$data["form"]         = $form_name;
	$data["item"]         = $item;
	
	$data["section"]    = "customize";
	$data["subsection"] = 3;
	$data["title"]      = "Handshakes::Admin Console - Profile Sections";
	
	$types = field_types($theme);
	$data["types"] = $types["names"];
	
	$layout[0] = "edit_input_item";
	
	admin_output($layout,$data);
	
}//edit_input_item

function save_input_item(){
	
	admin_validate();
	
	$vals = array("theme","form","profile","item","label","type","properties","add_to_search");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	$values["label"] = stripslashes(html_entity_decode($values["label"]));
	
	if(!is_array($values["properties"]))
		$values["properties"] = array();
	
	while(list($key,$val) = each($values["properties"])){
		$key = stripslashes(html_entity_decode($key));
		$val = stripslashes(html_entity_decode($val));
		$values["properties"][$key] = $val;
	}//while
	
	include(DOC_ROOT."/themes/".$values["theme"]."/forms/".$values["form"].".inc.php");
	include(DOC_ROOT."/themes/".$values["theme"]."/profile_sections.inc.php");
	
	if(extract_null_value($values["item"])){
		$values["item"] = @max(array_keys($form["names"]))+1;
		$new_item = new_input_item($values["theme"],$values["label"],$values["type"]);
		$form["names"][$values["item"]]  = $new_item["name"];
		$form["fields"][$values["item"]] = $new_item["field"];
		$form["types"][$values["item"]]  = $new_item["type"];
		$form["labels"][$values["item"]] = $new_item["label"];
		$assoc = handle_empties(explode(",",$form["assoc"]["profiles"]));
		$assoc[] = $new_item["name"];
		$form["assoc"]["profiles"] = implode(",",$assoc);
		$output_name = $profile_sections[$values["profile"]]["profile"];
		include(DOC_ROOT."/themes/".$values["theme"]."/profiles/".$output_name.".inc.php");
		$new_key = @max(array_keys($output["names"]))+1;
		$output["labels"][$new_key] = $new_item["label"];
		$output["names"][$new_key]  = $new_item["name"];
		$output["types"][$new_key]  = $new_item["output"];
		$assoc = handle_empties(explode(",",$output["assoc"]["profiles"]));
		$assoc[] = $new_item["name"];
		$output["assoc"]["profiles"] = implode(",",$assoc);
		save_output_data($values["theme"],$output_name,$output);
	}//if	
	
	$form["labels"][$values["item"]] = $values["label"];
	
	$field = explode("|",$form["fields"][$values["item"]]);	
	$field_type = array_shift($field);		
	
	$fields = field_types($values["theme"],$values["properties"]);
	
	if($field_type != $values["type"] || ($field_type == "options" && $values["properties"][2] != $field[2])){				
		$form["fields"][$values["item"]] = $values["type"]."|".$fields["settings"][$values["type"]];
		$form["types"][$values["item"]] = $fields["types"][$values["type"]];
		$sql_query = "ALTER TABLE `profiles` CHANGE `".$form["names"][$values["item"]]."` `".$form["names"][$values["item"]]."` ".$fields["sql"][$values["type"]];
		$GLOBALS["DB"]->execute($sql_query);
		if(is_searched_item($values["theme"],$form["names"][$values["item"]])){
			$search_data = form_data($values["theme"],"search_advanced");
			$change = array_search($form["names"][$values["item"]],$search_data["names"]);
			if(!extract_null_value($change)){
				$search_data["fields"][$change] = $form["fields"][$values["item"]];
				$search_data["types"][$change]  = $form["types"][$values["item"]];
			}//if
			save_form_data($values["theme"],"search_advanced",$search_data);
		}//if
	}//if
	
	if($field_type == $values["type"]){
		if(is_array($values["properties"])){
			$new_properties = array_keys($values["properties"]);
			$allkeys = array_merge($new_properties,array_keys($field));
			$settings_end = max($allkeys);
			for($i=0;$i<=$settings_end;$i++){
				if(in_array($i,$new_properties))
					$field[$i] = $values["properties"][$i];
			}//for
			$new_field = $field_type."|".implode("|",$field);
			$form["fields"][$values["item"]] = $new_field;
		}//if
	}//if
	
	save_form_data($values["theme"],$values["form"],$form);
	
	if(is_searched_item($values["theme"],$form["names"][$values["item"]])){
		$form_data = form_data($values["theme"],"search_advanced");		
		$remove = array_search($form["names"][$values["item"]],$form_data["names"]);
		if(!extract_null_value($remove)){
			unset($form_data["labels"][$remove]);
			unset($form_data["fields"][$remove]);
			unset($form_data["names"][$remove]);
			unset($form_data["types"][$remove]);		
			$assoc = handle_empties(explode(",",$form_data["search_assoc"]["profiles"]));
			$remove2 = array_search($form["names"][$values["item"]],$assoc);
			if(!extract_null_value($remove2))
				unset($assoc[$remove2]);
			$form_data["search_assoc"]["profiles"] = implode(",",$assoc);
			save_form_data($values["theme"],"search_advanced",$form_data);
		}//if
	}//if
	
	if($values["add_to_search"]){
		$form_data = form_data($values["theme"],"search_advanced");
		$new_key = @max(array_keys($form_data["names"]))+1;
		$form_data["labels"][$new_key] = $form["labels"][$values["item"]];
		$form_data["fields"][$new_key] = $form["fields"][$values["item"]];
		$form_data["names"][$new_key]  = $form["names"][$values["item"]];
		$form_data["types"][$new_key]  = $form["types"][$values["item"]];
		$assoc = handle_empties(explode(",",$form_data["search_assoc"]["profiles"]));
		$assoc[] = $form["names"][$values["item"]];
		$form_data["search_assoc"]["profiles"] = implode(",",$assoc);
		save_form_data($values["theme"],"search_advanced",$form_data);
	}//if
	
	
	load_page("index.php?section=profile_sections&action=edit_input_item&theme=".$values["theme"]."&form=".$values["form"]."&item=".$values["item"]."&profile=".$values["profile"]);
	
}//save_input_item

function edit_output_item(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$profile = $GLOBALS["Get"]->val("profile");
	$output_name = $GLOBALS["Get"]->val("output");
	$item = $GLOBALS["Get"]->val("item");
	
	$output_data = output_data($theme,$output_name);
	
	$data["label"] = $output_data["labels"][$item];
	$data["field"] = output_extract_field($output_data["types"][$item],$theme);
	$data["type"]  = $data["field"]["system_type"];	
	$data["link"]  = "index.php?section=profile_sections&action=edit&theme=".$theme."&profile=".$profile;
	
	include(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php");
	$data["theme"]        = $theme;
	$data["profile"]      = $profile;
	$data["section_name"] = $profile_sections[$profile]["name"];
	$data["output"]       = $output_name;
	$data["item"]         = $item;
	
	$data["section"]    = "customize";
	$data["subsection"] = 3;
	$data["title"]      = "Handshakes::Admin Console - Profile Sections";
	
	$types = output_types($theme);
	$data["types"] = $types["names"];
	
	$layout[0] = "edit_output_item";
	
	admin_output($layout,$data);
	
}//edit_output_item

function save_output_item(){
	
	admin_validate();
	
	$vals = array("theme","profile","output","item","label","properties","type");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	$output_data   = output_data($values["theme"],$values["output"]);	
	$default_types = output_types($values["theme"],$values["properties"]);
	
	$type = explode("|",$output_data["types"][$values["item"]]);
	$field_type = array_shift($type);	
	
	if(!is_array($values["properties"]))
		$values["properties"] = array();
		
	$values["label"] = stripslashes(html_entity_decode($values["label"]));
	while(list($key,$val) = each($values["properties"])){
		$key = stripslashes(html_entity_decode($key));
		$val = stripslashes(html_entity_decode($val));
		$values["properties"][$key] = $val;
	}//while
	
	if($field_type != $values["type"]){
		$output_data["types"][$values["item"]] = $values["type"]."|".$default_types["settings"][$values["type"]];
	}//if
	else{		
		$output_data["types"][$values["item"]] = $values["type"]."|".implode("|",$values["properties"]);
	}//else
	
	$output_data["labels"][$values["item"]] = $values["label"];

	save_output_data($values["theme"],$values["output"],$output_data);
	
	load_page("index.php?section=profile_sections&action=edit_output_item&theme=".$values["theme"]."&output=".$values["output"]."&profile=".$values["profile"]."&item=".$values["item"]);
	
}//save_output_item

function delete_item($data=0){
	
	admin_validate();
	
	if(!$data){
		$theme = $GLOBALS["Get"]->val("theme");
		$profile = $GLOBALS["Get"]->val("profile");
		$form_name = $GLOBALS["Get"]->val("form");
		$item = $GLOBALS["Get"]->val("item");
		$background = 0;
	}//if
	else{
		$background = 1;
		extract($data);
	}//else
	
	include(DOC_ROOT."/themes/".$theme."/profile_sections.inc.php");
	$output_name = $profile_sections[$profile]["profile"];
	
	$form_data = form_data($theme,$form_name);
	$remove_name = $form_data["names"][$item];
		
	unset($form_data["labels"][$item]);
	unset($form_data["fields"][$item]);
	unset($form_data["types"][$item]);
	unset($form_data["names"][$item]);
	
	$assoc = handle_empties(explode(",",$form_data["assoc"]["profiles"]));
	$remove = array_search($remove_name,$assoc);
	if(!extract_null_value($remove))
		unset($assoc[$remove]);

	if(count($assoc))
		$form_data["assoc"]["profiles"] = implode(",",$assoc);
	else
		unset($form_data["assoc"]["profiles"]);
	
	save_form_data($theme,$form_name,$form_data);
	
	$output_data = output_data($theme,$output_name);
	$remove_id = array_search($remove_name,$output_data["names"]);
	if(!extract_null_value($remove_id)){
		unset($output_data["labels"][$remove_id]);
		unset($output_data["names"][$remove_id]);
		unset($output_data["types"][$remove_id]);
	}//if
	
	$assoc = handle_empties(explode(",",$output_data["assoc"]["profiles"]));
	$remove2 = array_search($remove_name,$assoc);
	if(!extract_null_value($remove2))
		unset($assoc[$remove2]);
	$output_data["assoc"]["profiles"] = implode(",",$assoc);
	
	save_output_data($theme,$output_name,$output_data);
	
	if(is_searched_item($theme,$remove_name)){
		$search_data = form_data($theme,"search_advanced");
		$remove_id = array_search($remove_name,$search_data["names"]);
		if(!extract_null_value($remove_id)){
			unset($search_data["labels"][$remove_id]);
			unset($search_data["fields"][$remove_id]);
			unset($search_data["types"][$remove_id]);
			unset($search_data["names"][$remove_id]);
		}//if
		$assoc = handle_empties(explode(",",$search_data["search_assoc"]["profiles"]));
		$remove = array_search($remove_name,$assoc);
		if(!extract_null_value($remove))
			unset($assoc[$remove]);
		$search_data["search_assoc"]["profiles"] = implode(",",$assoc);
		save_form_data($theme,"search_advanced",$search_data);
	}//if
	
	if(!$background)
		load_page("index.php?section=profile_sections&action=edit&theme=".$theme."&profile=".$profile);
	
}//delete_item

function new_input_item($theme,$label,$type){
	
	$sql_query = "SHOW FIELDS FROM profiles";
	$result = $GLOBALS["DB"]->result($sql_query);
	$token_fields = array();
	foreach($result as $res){
		$token_fields[] = $res["Field"];
	}//foreach
	
	$sql_query = "SHOW FIELDS FROM members";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$token_fields[] = $res["Field"];
	}//foreach
	
	$name = ereg_replace("[^A-Za-z|0-9|_]","_",strtolower($label));
	$name = ereg_replace("[_]+","_",$name);
	if(in_array($name,$token_fields) || !eregi("^[a-z]",$name)){
		mt_srand((double) microtime() * 100000);
		$name = "field_".hs_substr(md5(time().mt_rand(0,1000)),0,10);
	}//if
	
	$fields = field_types($theme);
	
	$item["field"]  = $type."|".$fields["settings"][$type];
	$item["label"]  = $label;
	$item["type"]   = $fields["types"][$type];	
	$item["name"]   = $name;
	$item["output"] = $fields["output"][$type];
	
	$sql_query = "ALTER TABLE `profiles` ADD `".$name."` ".$fields["sql"][$type];
	$GLOBALS["DB"]->execute($sql_query);
	
	return $item;
	
}//new_input_item

function is_searched_item($theme,$name){
	
	$form_data = form_data($theme,"search_advanced");
	return in_array($name,$form_data["names"]);
	
}//is_searched_item

function form_data($theme,$form_name){
	
	include(DOC_ROOT."/themes/".$theme."/forms/".$form_name.".inc.php");
	return $form;
	
}//form_data

function save_form_data($theme,$form_name,$form){
	
	$form_str = "<?php\n\n";
	$form_str.= go_through_array("\$form",$form);
	$form_str.= "\n?>";
	
	$form_file = DOC_ROOT."/themes/".$theme."/forms/".$form_name.".inc.php";
	$file_handler = fopen($form_file,"w");
	fwrite($file_handler,$form_str);
	fclose($file_handler);
	
}//save_form_data

function save_output_data($theme,$output_name,$output){
	
	$output_str = "<?php\n\n";
	$output_str.= go_through_array("\$output",$output);
	$output_str.= "\n?>";
	
	$output_file = DOC_ROOT."/themes/".$theme."/profiles/".$output_name.".inc.php";
	$file_handler = fopen($output_file,"w");
	fwrite($file_handler,$output_str);
	fclose($file_handler);
	
}//save_output_data

function output_data($theme,$output_name){
	
	include(DOC_ROOT."/themes/".$theme."/profiles/".$output_name.".inc.php");
	return $output;
	
}//output_data

function output_extract_field($field_code,$theme){
	
	$details = explode("|",$field_code);
	$field_type = array_shift($details);
	$field = array();
	
	$field["properties"] = array();
	
	switch($field_type){
		case 'yes_no':
			$field["type"] = "Yes/No single chekbox output";
			$field["properties"]["Yes"] = array("type" => "input", "size" => 10, "name" => "properties[0]", "value" => $details[0]);
			$field["properties"]["No"] = array("type" => "input", "size" => 10, "name" => "properties[1]", "value" => $details[1]);
			break;
		case 'option':
			$field["type"] = "Selected option (dropdown, radio buttons, etc.) output";
			$src_options = admin_get_options($theme);
			$field["properties"]["Options Source"] = array("type" => "select", "options" => $src_options, "name" => "properties[0]", "value" => $details[0], "javascript" => "onChange='this.form.submit()'");
			break;
		case 'set':
			$field["type"] = "Selected options (multiple drodown, checkboxes set) output";
			$src_options = admin_get_options($theme);
			$field["properties"]["Options Source"] = array("type" => "select", "options" => $src_options, "name" => "properties[0]", "value" => $details[0], "javascript" => "onChange='this.form.submit()'");
			break;
		case 'txt_set':
			$field["type"] = "Set of text fields output";
			break;
		case 'txt_url_set':
			$field["type"] = "Set of text fields output converted into external links";
			break;
		case 'link':
			$field["type"] = "Output converted into external link";
			break;
		case 'txt_block':
			$field["type"] = "Text with paragraphs output";
			break;
		case 'html_block':
			$field["type"] = "HTML output";
			break;
		case 'text':
			$field["type"] = "Simple text output";			
			break;
		case 'date':
			$field["type"] = "Date output";
			break;
		case 'date_time':
			$field["type"] = "Date &amp; Time output";
			break;
		case 'time':
			$field["type"] = "Time output";
			break;
		case 'hotlinks':
			$field["type"] = "Output converted into hotlinks";
			break;
		case 'media':
			$field["type"] = "Embed multimedia";
			break;
	}//switch
	
	$field["system_type"] = $field_type;
	return $field;
	
}//output_extract_field

function output_types($theme,$properties=array()){
	
	$options = array_keys(admin_get_options($theme));
	
	if($properies[0] && in_array($properties[0],$options))
		$option = $properties[0];
	else
		$option = $options[0];
		
	$fields["names"] = array(
		"yes_no" => "Yes/No single chekbox output",
		"option" => "Selected option (dropdown, radio buttons, etc.) output",
		"set" => "Selected options (multiple drodown, checkboxes set) output",
		"txt_set" => "Set of text fields output",
		"txt_url_set" => "Set of text fields output converted into external links",
		"link" => "Output converted into external link",
		"txt_block" => "Text with paragraphs output",
		"html_block" => "HTML output",
		"text" => "Simple text output",
		"date" => "Date output",
		"date_time" => "Date &amp; Time output",
		"time" => "Time output",
		"hotlinks" => "Output converted into hotlinks",
		"media" => "Embed multimedia"
	);
	
	$fields["settings"] = array (
		"yes_no" => "Yes|No",
		"option" => $option,
		"set" => $option,
		"txt_set" => "",
		"txt_url_set" => "",
		"link" => "",
		"txt_block" => "",
		"html_block" => "",
		"text" => "",
		"date" => "",
		"date_time" => "",
		"time" => "",
		"hotlinks" => "",
		"media" => ""
	);
	
	return $fields;
	
}//output_types

function field_types($theme,$properties=array()){
	
	$options = array_keys(admin_get_options($theme));
	
	if($properies[0] && in_array($properties[0],$options))
		$option = $properties[0];
	else
		$option = $options[0];
	
	$fields["names"] = array (
		"textfield" => "Text Field",
		"textfield_set" => "Set of Text Fields",		
		"checkbox" => "Checkbox",
		"options" => "Dropdown List",
		"radio" => "Set of Radio Buttons",
		"set" => "Set of Checkboxes",
		"date" => "Date Fields",
		"date_time" => "Date &amp; Time Fields",
		"time" => "Time Fields",
		"textarea" => "Text Box",
		"htmlarea" => "WYSIWYG HTML Editor"
	);
	
	$fields["types"] = array (
		"textfield" => "val",
		"textfield_set" => "set",
		"checkbox" => "switch",
		"options" => "val",
		"radio" => "val",
		"set" => "set",
		"date" => "date",
		"date_time" => "date",
		"time" => "time",
		"textarea" => "val",
		"htmlarea" => "html"
	);
	
	$fields["settings"] = array (
		"textfield" => "30",
		"textfield_set" => "30|3|1",		
		"checkbox" => "0",
		"options" => $option,
		"radio" => $option,
		"set" => $option,
		"date" => "",
		"date_time" => "",
		"time" => "",
		"textarea" => "6|40",
		"htmlarea" => "550|400"
	);
	
	$fields["sql"] = array (
		"textfield" => "VARCHAR(250) NOT NULL",
		"textfield_set" => "TEXT NOT NULL",
		"checkbox" => "ENUM('0','1') NOT NULL",
		"options" => "INT UNSIGNED NOT NULL",
		"radio" => "TINYINT UNSIGNED NOT NULL",
		"set" => "VARCHAR(250) NOT NULL",
		"date" => "BIGINT UNSIGNED NOT NULL",
		"date_time" => "BIGINT UNSIGNED NOT NULL",
		"time" => "BIGINT UNSIGNED NOT NULL",
		"textarea" => "TEXT NOT NULL",
		"htmlarea" => "TEXT NOT NULL"
	);
	
	if($properties[2]){
		$fields["types"]["options"] = "set";
		$fields["sql"]["options"] = "VARCHAR(250) NOT NULL";
	}//if
		
	$fields["output"] = array (
		"textfield" => "text",
		"textfield_set" => "txt_set",
		"checkbox" => "yes_no|Yes|No",
		"options" => "option|".$option,
		"radio" => "option|".$option,
		"set" => "set|".$option,
		"date" => "date",
		"date_time" => "date_time",
		"time" => "time",
		"textarea" => "txt_block",
		"htmlarea" => "html_block"
	);
	
	return $fields;
	
}//field_types

function form_extract_field($field_code,$theme){
	
	$details = explode("|",$field_code);
	$field_type = array_shift($details);
	$field = array();
	
	$field["properties"] = array();
	
	switch($field_type){
		case 'textfield':
			$field["type"] = "Text Field";
			$field["properties"]["Field Length"] = array ("type" => "input", "size" => 10, "name" => "properties[0]", "value" => $details[0]);
			break;
		case 'textfield_set':
			$field["type"] = "Set of Text Fields";
			$field["properties"]["Each Field Length"] = array ( "type" => "input", "size" => "10", "name" => "properties[0]", "value" => $details[0] );
			$field["properties"]["Number of Fields"] = array ( "type" => "input", "size" => "10", "name" => "properties[1]", "value" => $details[1] );
			$options = array(
				"Fields One by One",
				"Multiple Lines"
			);
			$field["properties"]["Output Style"] = array ( "type" => "select", "options" => $options, "name" => "properties[2]", "value" => $details[2] );
			break;
		case 'checkbox':
			$field["type"] = "Checkbox";
			$options = array (
				"No",
				"Yes"
			);
			$field["properties"]["Ticked by Default"] = array ("type" => "select", "options" => $options, "name" => "properties[0]", "value" => $details[0]);
			break;
		case 'options':
			$field["type"] = "Dropdown List";
			$options = array ("No","Yes");
			$field["properties"]["Multiple"] = array ("type" => "select", "options" => $options, "name" => "properties[2]", "value" => $details[2]);
			$src_options = admin_get_options($theme);			
			$field["properties"]["Options Source"] = array("type" => "select", "options" => $src_options, "name" => "properties[0]", "value" => $details[0], "javascript" => "onChange='this.form.submit()'");
			$options = array();
			include(DOC_ROOT."/themes/".$theme."/options/".$details[0].".inc.php");
			$def_options[""] = "Please, select...";
			while(list($key,$val) = each($options)){
				$def_options[$key] = $val;
			}//while
			$field["properties"]["Selected by Default"] = array ("type" => "select", "options" => $def_options, "name" => "properties[1]", "value" => $details[1]);
			$options = array (
				"No",
				"Yes"
			);
			$field["properties"]["Enable Empty Option"] = array ("type" => "select", "options" => $options, "name" => "properties[3]", "value" => $details[3]);
			break;
		case 'radio':
			$field["type"] = "Set of Radio Buttons";
			$options[""] = "Please, select...";
			include(DOC_ROOT."/themes/".$theme."/options/".$details[0].".inc.php");
			$src_options = admin_get_options($theme);
			$field["properties"]["Options Source"] = array("type" => "select", "options" => $src_options, "name" => "properties[0]", "value" => $details[0], "javascript" => "onChange='this.form.submit()'");
			$field["properties"]["Selected by Default"] = array ("type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1]);
			$options = array(
				"Fields One by One",
				"Multiple Lines"
			);
			$field["properties"]["Output Style"] = array ( "type" => "select", "options" => $options, "name" => "properties[2]", "value" => $details[2] );
			break;
		case 'set':
			$field["type"] = "Set of Checkboxes";
			$src_options = admin_get_options($theme);
			$field["properties"]["Options Source"] = array("type" => "select", "options" => $src_options, "name" => "properties[0]", "value" => $details[0], "javascript" => "onChange='this.form.submit()'");
			$options = array(
				"Fields One by One",
				"Multiple Lines"
			);
			$field["properties"]["Output Style"] = array ( "type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1] );
			break;
		case 'date':
			$field["type"] = "Date Fields";
			$options = array(
				"Date in Past",
				"Date in Future"
			);
			$field["properties"]["Date Type"] = array ( "type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1] );
			break;
		case 'date_time':
			$field["type"] = "Date &amp; Time Fields";
			$field["properties"] = array();
			break;
		case 'time':
			$field["type"] = "Time Fields";
			$field["properties"] = array();
			break;
		case 'textarea':
			$field["type"] = "Text Box";
			$field["properties"]["Rows"] = array ("type" => "input", "size" => 10, "name" => "properties[0]", "value" => $details[0]);
			$field["properties"]["Cols"] = array ("type" => "input", "size" => 10, "name" => "properties[1]", "value" => $details[1]);
			break;
		case 'htmlarea':
			$field["type"] = "WYSIWYG HTML Editor";
			$field["properties"]["Width"] = array ("type" => "input", "size" => 10, "name" => "properties[0]", "value" => $details[0]);
			$field["properties"]["Height"] = array ("type" => "input", "size" => 10, "name" => "properties[1]", "value" => $details[1]);
			break;
	}//switch
	
	$field["system_type"] = $field_type;
	$field["preview"] = build_field($field_type,array("dets" => $details),$theme);
	return $field;
	
}//form_extract_field

function menu_data($theme,$menu_name){
	
	$menu_xml = implode("",file(DOC_ROOT."/themes/".$theme."/menus/".$menu_name.".inc.php"));
	
	$menu_xml = handle_xml_entities($menu_xml);	
	$parser = xml_parser_create();	
	
	if(!IGNORE_XML_ENCODING){
		$encoding = extract_encoding("html",$theme);
		xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $encoding);
	}//if
	
	xml_parse_into_struct($parser,$menu_xml,$xml_elements,$xml_index);
	
	$i=1;
	
	foreach($xml_elements as $element){
		$tag        = hs_strtolower($element["tag"]);
		$type       = $element["type"];
		$value      = $element["value"];
		$level      = $element["level"];
		$attributes = $element["attributes"];
		
		if($tag == "menu" && $type == "open"){
			$menu["name"] = $attributes["NAME"];
			$menu["type"] = $attributes["TYPE"];
		}//if
		elseif($tag == "item" && $type == "open"){
			$menu["items"][$i]["group"] = $attributes["GROUP"];
			$menu["items"][$i]["page"]  = $attributes["PAGE"];
		}//elseif
		elseif($tag == "item" && $type == "close"){
			$i++;
		}//elseif
		elseif($tag == "on" && $type == "complete"){			
			$menu["items"][$i]["on"] = $value;
		}//elseif
		elseif($tag == "off" && $type == "complete"){
			$menu["items"][$i]["off"] = $value;
		}//elseif
		elseif($tag == "link" && $type == "complete"){
			$menu["items"][$i]["link"] = $value;
		}//elseif
		
	}//foreach	
	
	return $menu;
	
}//menu_data

function save_menu_data($theme,$menu_name,$data){
	
	$settings = "<menu name=\"".htmlspecialchars($data["name"])."\" type=\"".htmlspecialchars($data["type"])."\">\n";
	while(list($key,$item) = each($data["items"])){
		$settings.= "\t<item page=\"".htmlspecialchars($item["page"])."\" group=\"".htmlspecialchars($item["group"])."\">\n";
		$settings.= "\t\t<on>".htmlspecialchars($item["on"])."</on>\n";
		$settings.= "\t\t<off>".htmlspecialchars($item["off"])."</off>\n";
		$settings.= "\t\t<link>".htmlspecialchars($item["link"])."</link>\n";
		$settings.= "\t</item>\n";
	}//while
	$settings.= "</menu>";
	
	$file_handler = fopen(DOC_ROOT."/themes/".$theme."/menus/".$menu_name.".inc.php","w");
	fwrite($file_handler,$settings);
	fclose($file_handler);
	
}//save_menu_data

function merge_form($source,$dist){
	
	include($source);
	$sdata = $form;
	
	include($dist);
	$ddata = $form;
	
	unset($form);
	
	$result = $sdata;
	
	//hidden
	foreach($ddata["hidden"] as $val){
		if(!array_search($val,$result["hidden"]))
			$result["hidden"][] = $val;
	}//foreach
	
	//fields
	foreach($ddata["names"] as $key => $val){
		if(!array_search($val,$result["names"])){
			$result["labels"][] = $ddata["labels"][$key];
			$result["names"][]  = $ddata["names"][$key];
			$result["fields"][] = $ddata["fields"][$key];
			$result["types"][]  = $ddata["types"][$key];
		}//if
	}//foreach
	
	//assocs
	foreach($ddata["assoc"] as $table => $fields){
		if(!isset($result["assoc"][$table]))
			$result["assoc"][$table] = $fields;
		else{
			$f = array_flip(handle_empties(explode(",",$fields)));
			$f2 = array_flip(handle_empties(explode(",",$result["assoc"][$table])));
			$res = handle_empties(array_keys(array_merge($f,$f2)));
			$result["assoc"][$table] = implode(",",$res);
		}//else
	}//foreach

	return $result;
	
}//merge_form

function merge_profile($source,$dist){
	
	include($source);
	$sdata = $output;
	
	include($dist);
	$ddata = $output;
	
	unset($output);
	
	$result = $sdata;
	
	//fields
	foreach($ddata["names"] as $key => $val){
		if(!array_search($val,$result["names"])){
			$result["labels"][] = $ddata["labels"][$key];
			$result["names"][]  = $ddata["names"][$key];
			$result["types"][]  = $ddata["types"][$key];
		}//if
	}//foreach
	
	//assocs
	foreach($ddata["assoc"] as $table => $fields){
		if(!isset($result["assoc"][$table]))
			$result["assoc"][$table] = $fields;
		else{
			$f = array_flip(handle_empties(explode(",",$fields)));
			$f2 = array_flip(handle_empties(explode(",",$result["assoc"][$table])));
			$res = handle_empties(array_keys(array_merge($f,$f2)));
			$result["assoc"][$table] = implode(",",$res);
		}//else
	}//foreach
	
	return $result;
	
}//merge_profile

?>