<?php

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

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

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

							switch($action){

								case '':
									sources();
									break;
									
								case 'edit':
									edit_source();
									break;
									
								case 'save':
									save_source();
									break;
									
								case 'delete':
									delete_source();
									break;

							}//switch

						///////////////////////////////////////
						
function sources(){
	
	admin_validate();
	
	$data["themes"]  = admin_get_themes();
	$data["sources"] = array();
	
	$theme = $GLOBALS["Get"]->val("theme");
	
	if(!$theme)
		$theme = $GLOBALS["THEME"];
	
	$sources = admin_get_options($theme);
	
	while(list($key,$val) = each($sources)){
		$data["sources"][$key]["name"]   = $val;
		$data["sources"][$key][edit]   = "index.php?section=option_sources&action=edit&theme=".$theme."&source=".$key;
		$data["sources"][$key][delete] = "index.php?section=option_sources&action=delete&theme=".$theme."&source=".$key;
	}//while
	
	$layout[0] = "sources_list";
	
	$data["theme"] = $theme;
	
	$data["section"] = "customize";
	$data["subsection"] = 9;
	$data["title_id"] = "41";	
	admin_output($layout,$data);
	
}//sources

function edit_source(){
	
	admin_validate();
	
	$theme  = $GLOBALS["Get"]->val("theme");
	$source = $GLOBALS["Get"]->val("source");
	
	$data["theme"]  = $theme;
	$data["source"] = $source;
	
	$data["section"] = "customize";
	$data["subsection"] = 9;
	$data["title_id"] = "41";	$data["link"] = "index.php?section=option_sources&theme=".$theme;
	
	if($source){
		include(DOC_ROOT."/themes/".$theme."/options/".$source.".inc.php");	
		$data["name"] = $options_name;
		$data["options"] = $options;
	}//if
	else{
		$data["name"] = "";
		$data["options"] = array();
	}//else
	
	$layout[0] = "edit_option_source";
	
	$data["javascript"] = <<<JAVASCRIPTCODE
<script language="JavaScript">
<!--
	var HS_SelectedItem = -1;
	var HS_NewOption = 0;
	function HS_EditItem(){
		var FormHandler = document.edit_item;
		var OptionsHandler = FormHandler.options_list;
		var ChosenOption = OptionsHandler.selectedIndex;
		HS_SelectedItem = ChosenOption;
		var OptionName = OptionsHandler.options[ChosenOption].text;
		FormHandler.option_name.value = OptionName;
	}//HS_EditItem
	function HS_UpdateItem(){
		if(HS_SelectedItem>=0){
			var FormHandler = document.edit_item;
			var OptionsHandler = FormHandler.options_list;			
			OptionsHandler.options[HS_SelectedItem].text = FormHandler.option_name.value;
			FormHandler.option_name.value = "";
			OptionsHandler.selectedIndex = -1;
			HS_SelectedItem = -1;
		}//if	
	}//HS_UpdateItem
	function HS_DeleteItem(){
		if(HS_SelectedItem>=0){
			var FormHandler = document.edit_item;
			var OptionsHandler = FormHandler.options_list;			
			OptionsHandler.options[HS_SelectedItem] = null;
			FormHandler.option_name.value = "";
			OptionsHandler.selectedIndex = -1;
			HS_SelectedItem = -1;
		}//if
	}//HS_DeleteItem
	function HS_AddItem(){
		var FormHandler = document.edit_item;
		var OptionsHandler = FormHandler.options_list;
		NewOptionName = FormHandler.new_option.value;
		OptionsHandler.options[OptionsHandler.options.length] = 
		new Option(NewOptionName, "HSNEWOPTION"+HS_NewOption);
		FormHandler.new_option.value = "";		
		HS_NewOption++;
	}//HS_AddItem
	function HS_MoveItem(Direction){
		var DoMove = 1;
		var NewPosition;
		var FormHandler = document.edit_item;
		var OptionsHandler = FormHandler.options_list;
		var ChosenOption = OptionsHandler.selectedIndex;		
		if(ChosenOption>=0){
			var TempName = OptionsHandler.options[ChosenOption].text;
			var TempIndex = OptionsHandler.options[ChosenOption].value;
			if(Direction == "up"){
				if(ChosenOption == 0)
					DoMove = 0;
				NewPosition = ChosenOption - 1;
			}//if
			else if(Direction == "down"){
				if(ChosenOption == OptionsHandler.options.length-1)
					DoMove = 0;
				NewPosition = ChosenOption + 1;
			}//else if			
			if(DoMove){
				OptionsHandler.options[ChosenOption].text = OptionsHandler.options[NewPosition].text;
				OptionsHandler.options[ChosenOption].value = OptionsHandler.options[NewPosition].value;	
				OptionsHandler.options[NewPosition].text = TempName;
				OptionsHandler.options[NewPosition].value = TempIndex;
				OptionsHandler.selectedIndex = NewPosition;
				HS_EditItem();
			}//if
		}//if
	}//HS_MoveItem
	function HS_Submit(){
		var OptionsLine = "";
		var FormHandler = document.edit_item;
		var OptionsHandler = FormHandler.options_list;
		for(var i=0;i<OptionsHandler.options.length;i++){			
			OptionsLine+= OptionsHandler.options[i].value+"|"+OptionsHandler.options[i].text+"-|-";
		}//for
		FormHandler.options_line.value = OptionsLine;
		FormHandler.submit();
	}//HS_Submit
//-->
</script>
JAVASCRIPTCODE;
	
	admin_output($layout,$data);
	
}//edit_source

function save_source(){
	
	admin_validate();
	
	$new_options = array();
	
	$theme  = $GLOBALS["Get"]->val("theme");
	$source = $GLOBALS["Get"]->val("source");
	$name   = stripslashes(html_entity_decode($GLOBALS["Get"]->val("name")));
	$options_line = hs_ereg_replace("&#124;","|",$GLOBALS["Get"]->val("options_line"));
	
	if(!empty($source)){
		include(DOC_ROOT."/themes/".$theme."/options/".$source.".inc.php");
		$new_key = @max(array_keys($options));
	}//if
	else{
		$source = hs_ereg_replace("[^A-Za-z|0-9|_]","_",strtolower($name));
		$source = hs_ereg_replace("[_]+","_",$source);	
		$exists = file_exists(DOC_ROOT."/themes/".$theme."/options/".$source.".inc.php");
		if($exists || !eregi("^[a-z]",$source)){
			mt_srand((double) microtime() * 100000);
			$source = "source_".hs_substr(md5(time().mt_rand(0,1000)),0,5);
		}//if
		$new_key = 0;
	}//else
	
	$options_codes = handle_empties(explode("-|-",$options_line));
	foreach($options_codes as $opcode){
		$option = explode("|",$opcode);
		if(hs_eregi("HSNEWOPTION",$option[0])){
			$new_key++;
			$option[0] = $new_key;
		}//if
		$new_options[$option[0]] = $option[1];
	}//foreach
	
	$write_str = "<?php\n\n";
	$write_str.= "\$options_name = \"".hs_ereg_replace('"','\"',$name)."\";\n";
	$write_str.= go_through_array("\$options",$new_options);
	$write_str.= "\n?>";
	
	$file_handler = fopen(DOC_ROOT."/themes/".$theme."/options/".$source.".inc.php","w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	
	load_page("index.php?section=option_sources&action=edit&theme=".$theme."&source=".$source);
	
}//save_source

function delete_source(){
	
	admin_validate();
	
	$theme  = $GLOBALS["Get"]->val("theme");
	$source = $GLOBALS["Get"]->val("source");
	
	@unlink(DOC_ROOT."/themes/".$theme."/options/".$source.".inc.php");
	
	load_page("index.php?section=option_sources&theme=".$theme);
	
}//delete_source
						
?>