<?php

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

						///////////////////////////////////////
							
						admin_validate_section("manage",6);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){								
										
								case '':									
									ecards_manager();
									break;
									
								case 'create':
									create_ecard();
									break;
									
								case 'do_create':
									do_create_ecard();
									break;
									
								case 'edit':
									edit_ecard();
									break;
									
								case 'do_edit':
									do_edit_ecard();
									break;
									
								case 'delete':
									ecards_delete_ecard();
									break;
									
								case 'view':
									view_ecard();
									break;
									
								case 'featured_add':
									featured_add_ecard();
									break;
									
								case 'featured_remove':
									featured_remove_ecard();
									break;
									
								case 'create_collection':
									create_collection();
									break;
									
								case 'do_create_collection':
									do_create_collection();
									break;
									
								case 'edit_collection':
									edit_collection();
									break;
									
								case 'do_edit_collection':
									do_edit_collection();
									break;
									
								case 'delete_collection':
									delete_collection();
									break;
									
								case 'activate_collection':
									activate_collection();
									break;
									
								case 'deactivate_collection':
									deactivate_collection();
									break;
									
								case 'import':
									import_ecards();
									break;
									
								case 'do_import':
									do_import_ecards();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function ecards_manager(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 6;
	$data["title_id"] = "6";	
	$data["ecards"] = array();
	
	$p = $GLOBALS["Get"]->val("p");
	
	$sql_query_total = "SELECT COUNT(crd_id) ";
	$sql_query_result = "SELECT crd_id,name,type,sent,featured ";
	
	$sql_query = "FROM ecards
				  WHERE crd_id > 100000";
	
	$valnames = array("search_text","search_for","sort_by","sort_order","category","subcategory");
	
	$values = $GLOBALS["Get"]->arr($valnames,1);
	
	if(!empty($values["search_text"]) && !empty($values["search_for"])){
		
		switch($values["search_for"]){
			case 'crd_id':
				settype($values["search_text"],"integer");
				$sql_query.= " AND crd_id=".$values["search_text"];
				break;
			case 'name':
				$sql_query.= " AND name LIKE '%".$values["search_text"]."%' ";
				break;
			case 'description':
				$sql_query.= " AND description LIKE '%".$values["search_text"]."%' ";
				break;
		}//switch
		
	}//if
	
	if(!empty($values["category"]))
		$sql_query.= " AND category=".$values["category"];
		
	if(!empty($values["subcategory"]))
		$sql_query.= " AND subcategory=".$values["subcategory"];
	
	switch($values["sort_by"]){
		case '':
			$sql_query.= " ORDER BY name ASC ";
			break;
		case 'name':
			$sql_query.= " ORDER BY name ".$values["sort_order"];
			break;
		case 'crd_id':
			$sql_query.= " ORDER BY crd_id ".$values["sort_order"];
			break;
		case 'type':
			$sql_query.= " ORDER BY type ".$values["sort_order"];
			break;
		case 'sent':
			$sql_query.= " ORDER BY sent ".$values["sort_order"];
			break;
		case 'featured':
			$sql_query.= " ORDER BY featured ".$values["sort_order"];
			break;
	}//switch
	
	$data["values"] = $values;
	
	$start = $p*30;
	
	$total = $GLOBALS["DB"]->single($sql_query_total.$sql_query);
	
	$sql_query.= " LIMIT ".$start.",30";
	$results = $GLOBALS["DB"]->result($sql_query_result.$sql_query);
	
	$search_link = "index.php?section=ecards";
	while(list($key,$val) = each($values)){
		$search_link.= "&".$key."=".urlencode(stripslashes(html_entity_decode($val)));
	}//while
	$data["pages"] = handle_multipages($p,$total,$search_link,30);
	
	$data["link"] = $search_link."&p=".$p;
	
	foreach($results as $result){
		if(hs_strlen($result["name"] > 30))
			$result["name"] = hs_substr($result["name"],0,27)."...";
			
		$card["id"] = $result["crd_id"];
		$card["name"] = $result["name"];		
		$card["type"] = ($result["type"] == "static") ? "image" : "rich media";
		$card["sent"] = $result["sent"];
		$card["view"] = "javascript:HS_Popup('index.php?section=ecards&action=view&crd_id=".$result["crd_id"]."','width=400,height=400,scrollbars=no,menubar=no,resizable=no,location=no');";
		$card["edit"] = "index.php?section=ecards&action=edit&crd_id=".$result["crd_id"]."&ret_link=".urlencode($data["link"]);
		$card["delete"] = "index.php?section=ecards&action=delete&crd_id=".$result["crd_id"]."&ret_link=".urlencode($data["link"]);
		$card["featured"] = $result["featured"];
		$card["featured_add"] = "index.php?section=ecards&action=featured_add&crd_id=".$result["crd_id"]."&ret_link=".urlencode($data["link"]);
		$card["featured_remove"] = "index.php?section=ecards&action=featured_remove&crd_id=".$result["crd_id"]."&ret_link=".urlencode($data["link"]);
		$data["ecards"][] = $card;
	}//foreach
	
	$data["collections"] = array();
	
	$sql_query = "SELECT * FROM ecards_collections";
	$results = $GLOBALS["DB"]->result($sql_query);
	foreach($results as $result){
		$collection["col_id"] = $result["col_id"];
		$collection["name"] = $result["name"];
		$collection["cards"] = count(handle_empties(explode(",",$result["ecards"])));
		$collection["start"] = date("jS F",$result["start"]);
		$collection["end"] = date("jS F",$result["end"]);
		$collection["status"] = $result["status"];
		$collection["activate"] = "index.php?section=ecards&action=activate_collection&col_id=".$result["col_id"]."&ret_link=".urlencode($data["link"]);
		$collection["deactivate"] = "index.php?section=ecards&action=deactivate_collection&col_id=".$result["col_id"]."&ret_link=".urlencode($data["link"]);
		$collection["edit"] = "index.php?section=ecards&action=edit_collection&col_id=".$result["col_id"]."&ret_link=".urlencode($data["link"]);
		$collection["delete"] = "index.php?section=ecards&action=delete_collection&col_id=".$result["col_id"]."&ret_link=".urlencode($data["link"]);
		$data["collections"][] = $collection;
	}//foreach
	
	$import_files = array ();
	
	$import_folder = DOC_ROOT."/import/ecards";
	$dirhandler = opendir($import_folder);
	while($file = readdir($dirhandler)){
		if($file!="." && $file!=".." && eregi("\.txt$",$file)){
			$import_file["file"] = $file;
			$import_file["size"] = filesize($import_folder."/".$file);			
			if($import_file["size"] < 1024)
				$import_file["size"].= " bytes";
			elseif($import_file["size"] >= 1024 && $import_file["size"] < 1048576)
				$import_file["size"] = round($import_file["size"]/1024,2)." KB";
			elseif($import_file["size"] >= 1048576)
				$import_file["size"] = round($import_file["size"]/1048576,2)." MB";
				
			$import_files[] = $import_file;
		}//if
	}//while
	
	$data["files"] = $import_files;
	
	$layout[0] = "ecards_manager";
	$layout[1] = "ecards_collections";
	$layout[2] = "import_ecards";
	
	include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/system/ecards_categories.inc.php");
	
	$jscode = "";
	while(list($key,$val) = each($categories)){
		$jscode.= "HS_Categories[".$key."] = \"".hs_ereg_replace('"','\"',$val)."\";\n";
	}//while
	while(list($key,$val) = each($subcategories)){
		$jscode.= "HS_SubCategories[".$key."] = new Array;\n";
		while(list($k,$v) = each($val)){
			$jscode.= "HS_SubCategories[".$key."][".$k."] = \"".hs_ereg_replace('"','\"',$v)."\";\n";
		}//while
	}//while
	
	$data["javascript"] = <<<JAVASCRIPTCODE
<script language="JavaScript">
<!--
	var HS_Categories = new Array;
	var HS_SubCategories = new Array;
	var Category = 0;
	var SubCategory = 0;
	function HS_InitCategories(){
		$jscode
	}//HS_InitCategories
	function HS_PopulateSubCategoriesSimple(){
		var FormHandler = document.ecards_search;
		Category = FormHandler.category.options[FormHandler.category.selectedIndex].value;
		for(var i=FormHandler.subcategory.options.length-1; i>=0; i--){
			FormHandler.subcategory.options[i] = null;
		}//for
		FormHandler.subcategory.options[0] = new Option("Please, select...","0");
		var k=1;
		for(var Item in HS_SubCategories[Category]){
			FormHandler.subcategory.options[k] = new Option(HS_SubCategories[Category][Item],Item);
			k++;
		}//for
	}//HS_PopulateSubCategoriesSimple
	function HS_PopulateSubCategories(){
		var FormHandler = document.ecards_categories;
		Category = FormHandler.categories.options[FormHandler.categories.selectedIndex].value;
		for(var i=FormHandler.subcategories.options.length-1; i>=0; i--){
			FormHandler.subcategories.options[i] = null;
		}//for
		var k=0;
		for(var Item in HS_SubCategories[Category]){
			FormHandler.subcategories.options[k] = new Option(HS_SubCategories[Category][Item],Item);
			k++;
		}//for
		FormHandler.edit_category.value = HS_Categories[Category];
		FormHandler.edit_subcategory.value = "";
		HS_SubCategory = 0;
	}//HS_PopulateSubCategories
	function HS_EditSubCategory(){
		if(Category){
			var FormHandler = document.ecards_categories;
			SubCategory = FormHandler.subcategories.options[FormHandler.subcategories.selectedIndex].value;
			FormHandler.edit_subcategory.value = HS_SubCategories[Category][SubCategory];
		}//if
	}//HS_EditSubCategory
	function HS_AddCategory(){
		var FormHandler = document.ecards_categories;
		var CategoryName = FormHandler.new_category.value;
		var CatID = HS_Categories.length+1;
		HS_Categories[CatID] = CategoryName;
		HS_SubCategories[CatID] = new Array;
		FormHandler.categories.options[FormHandler.categories.options.length] = new Option(CategoryName,CatID);
		FormHandler.new_category.value = "";
	}//HS_AddCategory
	function HS_AddSubCategory(){
		if(Category){
			var FormHandler = document.ecards_categories;
			var SubCategoryName = FormHandler.new_subcategory.value;
			var SubCatID = HS_SubCategories[Category].length+1;
			HS_SubCategories[Category][SubCatID] = SubCategoryName;
			FormHandler.subcategories.options[FormHandler.subcategories.options.length] = new Option(SubCategoryName,SubCatID);
			FormHandler.new_subcategory.value = "";
		}//if
	}//HS_AddSubCategory
	function HS_UpdateCategory(){
		if(Category){
			var FormHandler = document.ecards_categories;
			var CategoryName = FormHandler.edit_category.value;
			HS_Categories[Category] = CategoryName;
			var Selected = FormHandler.categories.selectedIndex;
			FormHandler.categories.options[Selected] = new Option(CategoryName,Category);
			FormHandler.categories.selectedIndex = Selected;
		}//if
	}//HS_UpdateCategory
	function HS_UpdateSubCategory(){
		if(Category && SubCategory){
			var FormHandler = document.ecards_categories;
			var SubCategoryName = FormHandler.edit_subcategory.value;
			HS_SubCategories[Category][SubCategory] = SubCategoryName;
			var Selected = FormHandler.subcategories.selectedIndex;
			FormHandler.subcategories.options[Selected] = new Option(SubCategoryName,SubCategory);
			FormHandler.subcategories.selectedIndex = Selected;
		}//if
	}//HS_UpdateSubCategory
	function HS_DeleteCategory(){
		if(Category){
			var FormHandler = document.ecards_categories;
			HS_Categories[Category] = "EMPTY";
			FormHandler.categories.options[FormHandler.categories.selectedIndex] = null;
			FormHandler.edit_category.value = "";
			FormHandler.edit_subcategory.value = "";
			Category = 0;
			SubCategory = 0;
			for(var i=FormHandler.subcategories.options.length; i>=0; i--){
				FormHandler.subcategories.options[i] = null;
			}//for
		}//if
	}//HS_DeleteCategory
	function HS_DeleteSubCategory(){
		if(Category && SubCategory){
			var FormHandler = document.ecards_categories;
			HS_SubCategories[Category][SubCategory] = "EMPTY";
			FormHandler.subcategories.options[FormHandler.subcategories.selectedIndex] = null;
			FormHandler.edit_subcategory.value = "";
			SubCategory = 0;
		}//if
	}//HS_DeleteSubCategory
	function HS_UpdateData(){
		var CatsLine = "";
		var SubCatsLine = "";
		var Rules = new Array();
		var Error = 0;
		var FormHandler = document.ecards_categories;		
		for(var Item in HS_Categories){
			if(HS_Categories[Item] != "EMPTY"){
				CatsLine+= Item+"|"+HS_Categories[Item]+"==|==";
				Rules[Item] = 0;
				for(var SubItem in HS_SubCategories[Item]){
					if(HS_SubCategories[Item][SubItem] != "EMPTY"){
						SubCatsLine+= Item+"|"+SubItem+"|"+HS_SubCategories[Item][SubItem]+"==|==";
						Rules[Item] = 1;
					}//if					
				}//for
			}//if
		}//for
		
		for(var Rule in Rules){
			if(Rules[Rule] == 0){
				Error = 1;
			}//if
		}//for
		
		if(Error == 1){
			alert("Please, make sure to add at least one subcategory to each category.");
		}//if
		else{
			FormHandler.res_categories.value = CatsLine;
			FormHandler.res_subcategories.value = SubCatsLine;
			FormHandler.submit();
		}//else
	}//HS_UpdateData
//-->
</script>
JAVASCRIPTCODE;

	$data["onload"] = "HS_InitCategories();";
	
	$data["categories"] = $categories;
	$data["subcategories"] = $subcategories;
	
	admin_output($layout,$data);
	
}//ecards_manager

function create_ecard(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 6;
	$data["title_id"] = "7";	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	$data["link"] = $ret_link;
	
	$vals = array("category","subcategory","name","description","type","featured","tag");
	$data["values"] = $GLOBALS["Get"]->arr($vals,1);
	$data["values"][tag] = stripslashes(html_entity_decode($data["values"][tag]));
	$data["values"][tag] = hs_ereg_replace(preg_quote("&#124;"),"|",$data["values"][tag]);
	
	$data["javascript"] = <<<HTMLTEXT
<script language="JavaScript">
<!--
	var HS_SubCategories = new Array;
	var HS_CategoriesField;
	var HS_SubCategoriesField;
	function HS_SubCategoriesInit(SubCategoriesLine,CatField,SubCatField){
		var Items = SubCategoriesLine.split("=|=");
		var CatID;
		var SubCatID;
		for(var Item in Items){
			var SubItems = Items[Item].split("|");
			CatID = SubItems[0];
			SubCatID = SubItems[1];
			if(!HS_SubCategories[CatID])
				HS_SubCategories[CatID] = new Array;
				
			HS_SubCategories[CatID][SubCatID] = SubItems[2];
		}//for
		HS_CategoriesField = CatField;
		HS_SubCategoriesField = SubCatField;		
	}//HS_SubCategoriesInit
	function HS_SubCategoriesPopulate(){
		var Category = HS_CategoriesField.options[HS_CategoriesField.selectedIndex].value;
		for(var k = HS_SubCategoriesField.length-1; k>=0; k--){
			HS_SubCategoriesField.options[k] = null;
		}//for
		var i = 0;		
		for(var Item in HS_SubCategories[Category]){			
			HS_SubCategoriesField.options[i] = new Option(HS_SubCategories[Category][Item],Item);
			i++;
		}//for
	}//HS_SubCategoriesPopulate
	function HS_HideTagField(the_selector){
		var TagField = document.getElementById('tag_field');
		if(the_selector.value == "static" && the_selector.checked == true){
			TagField.style.display = "none";
		}//if
		else{
			TagField.style.display = "";
		}//else
	}//HS_HideTagField
//-->
</script>
HTMLTEXT;
	
	$data["onload"] = "HS_SubCategoriesInit('".build_categories("ecards_categories")."',document.forms[0].category,document.forms[0].subcategory);HS_HideTagField(document.forms[0].type[0])";
	
	$field["name"] = "category";
	$field["dets"][0] = "ecards_categories";	
	$field["value"] = $data["values"]["category"];
	$data["category_field"] = build_field("categories",$field);
	
	$field["name"] = "subcategory";
	$field["dets"][0] = "ecards_categories";
	$field["value"] = $data["values"]["subcategory"];
	$data["subcategory_field"] = build_field("subcategories",$field);
	
	$layout[0] = "ecards_create";
	
	admin_output($layout,$data);
	
}//create_ecard

function do_create_ecard(){
	
	admin_validate();
	
	include_once(DOC_ROOT."/functions/form_functions.php");
	
	$vals = array("category","subcategory","name","description","type","featured","tag");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	$rules = array("2|category,subcategory,name|0");	
	$result = form_validate_rules("",$values,$rules);
	
	if(!extract_null_value($result["err_id"])){
		$GLOBALS["uerr_id"] = $result["err_id"];		
		create_ecard();
		exit;
	}//if
	
	$file = $GLOBALS["GetFromForm"]->file("file");
	
	if(empty($file["tmp"])){
		$GLOBALS["aerr_id"] = 27;
		create_ecard();
		exit;
	}//if
	
	$thumbnail = $GLOBALS["GetFromForm"]->file("thumbnail");
	
	if(empty($thumbnail["tmp"])){
		$GLOBALS["aerr_id"] = 28;
		create_ecard();
		exit;
	}//if
	
	if(hs_eregi("flash",$file["type"])){
		$extension = ".swf";
		$values["type"] = "rich";
	}//if
	elseif(hs_ereg("gif",$file["type"])){
   		$extension = ".gif";
   		$values["type"] = "static";
   	}//elseif
   	elseif(hs_ereg("jpeg|jpg",$file["type"])){
   		$extension = ".jpeg";
   		$values["type"] = "static";
   	}//elseif
   	
   	mt_srand((double) microtime() * 100000);
	$filename = hs_substr(md5(time().mt_rand(0,100)),0,15);
	$thumbfile = $filename."th";
	$filename.= $extension;
	
	move_uploaded_file($file["tmp"], DOC_ROOT."/ecards/files/".$filename);
	chmod(DOC_ROOT."/ecards/files/".$filename, 0777);
	
	if(hs_ereg("gif",$thumbnail["type"])){
   		$extension = ".gif";
   	}//elseif
   	elseif(hs_ereg("jpeg|jpg",$thumbnail["type"])){
   		$extension = ".jpeg";
   	}//elseif
   	
   	$thumbfile.= $extension;
   	
   	move_uploaded_file($thumbnail["tmp"], DOC_ROOT."/ecards/files/".$thumbfile);
	chmod(DOC_ROOT."/ecards/files/".$thumbfile, 0777);
	
	$sql_query = "INSERT INTO ecards (name,description,category,subcategory,source,thumbnail,type,tag,featured,added)
				  VALUES (':name:',':description:',:category:,:subcategory:,':source:',':thumbnail:',':type:',':tag:',':featured:',:time:)";
	
	$values["source"] = $filename;
	$values["thumbnail"] = $thumbfile;
	if(!$values["featured"])
		$values["featured"] = 0;
	$values["time"] = time();
	
	$GLOBALS["DB"]->values = $values;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	load_page($ret_link);	
	
}//do_create_ecard

function edit_ecard(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 6;
	$data["title_id"] = "8";	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	$data["link"] = $ret_link;
	
	$data["crd_id"] = $GLOBALS["Get"]->type_val("crd_id","integer");
	
	$sql_query = "SELECT * FROM ecards
				  WHERE crd_id=".$data["crd_id"];
	
	$data["values"] = $GLOBALS["DB"]->row($sql_query);
	
	$data["values"][tag] = stripslashes(html_entity_decode($data["values"][tag]));
	$data["values"][tag] = hs_ereg_replace(preg_quote("&#124;"),"|",$data["values"][tag]);
	
	if($data["values"]["type"] == "rich"){
		if(!$data["values"][tag]){
			include_once(DOC_ROOT."/classes/SWFHeader.class.php");
			$GLOBALS["SWFHeader"]->loadswf(URL."/ecards/files/".$data["values"]["source"]);
			$data["display"] = $GLOBALS["SWFHeader"]->display();		
		}//if
		else
			$data["display"] = hs_ereg_replace("\|file\|",URL."/ecards/files/".$data["values"]["source"],$data["values"][tag]);
	}//if
	else{
		$data["display"] = admin_output_image($data["values"]["source"],0,"ecards/files","",0,0);
	}//else
	
	$data["thumbnail"] = admin_output_image($data["values"]["thumbnail"],0,"ecards/files","",0,0);
	
	$data["javascript"] = <<<HTMLTEXT
<script language="JavaScript">
<!--
	var HS_SubCategories = new Array;
	var HS_CategoriesField;
	var HS_SubCategoriesField;
	function HS_SubCategoriesInit(SubCategoriesLine,CatField,SubCatField){
		var Items = SubCategoriesLine.split("=|=");
		var CatID;
		var SubCatID;
		for(var Item in Items){
			var SubItems = Items[Item].split("|");
			CatID = SubItems[0];
			SubCatID = SubItems[1];
			if(!HS_SubCategories[CatID])
				HS_SubCategories[CatID] = new Array;
				
			HS_SubCategories[CatID][SubCatID] = SubItems[2];
		}//for
		HS_CategoriesField = CatField;
		HS_SubCategoriesField = SubCatField;		
	}//HS_SubCategoriesInit
	function HS_SubCategoriesPopulate(){
		var Category = HS_CategoriesField.options[HS_CategoriesField.selectedIndex].value;
		for(var k = HS_SubCategoriesField.length-1; k>=0; k--){
			HS_SubCategoriesField.options[k] = null;
		}//for
		var i = 0;		
		for(var Item in HS_SubCategories[Category]){			
			HS_SubCategoriesField.options[i] = new Option(HS_SubCategories[Category][Item],Item);
			i++;
		}//for
	}//HS_SubCategoriesPopulate
	function HS_HideTagField(the_selector){
		var TagField = document.getElementById('tag_field');
		if(the_selector.value == "static" && the_selector.checked == true){
			TagField.style.display = "none";
		}//if
		else{
			TagField.style.display = "";
		}//else
	}//HS_HideTagField
//-->
</script>
HTMLTEXT;
	
	$data["onload"] = "HS_SubCategoriesInit('".build_categories("ecards_categories")."',document.forms[0].category,document.forms[0].subcategory);HS_HideTagField(document.forms[0].type[0])";
	
	$field["name"] = "category";
	$field["dets"][0] = "ecards_categories";	
	$field["value"] = $data["values"]["category"];
	$data["category_field"] = build_field("categories",$field);
	
	$field["name"] = "subcategory";
	$field["dets"][0] = "ecards_categories";
	$field["value"] = $data["values"]["subcategory"];
	$data["subcategory_field"] = build_field("subcategories",$field);
	
	$layout[0] = "ecards_edit";
	
	admin_output($layout,$data);
	
}//edit_ecard

function do_edit_ecard(){

	admin_validate();
	
	include_once(DOC_ROOT."/functions/form_functions.php");
	
	$vals = array("crd_id","category","subcategory","name","description","type","featured","tag");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	$rules = array("2|category,subcategory,name|0");	
	$result = form_validate_rules("",$values,$rules);
	
	if(!extract_null_value($result["err_id"])){
		$GLOBALS["uerr_id"] = $result["err_id"];		
		edit_ecard();
		exit;
	}//if
	
	$sql_query = "SELECT source,thumbnail FROM ecards
				  WHERE crd_id=".$values["crd_id"];
		
	$source = $GLOBALS["DB"]->row($sql_query);
	
	$file = $GLOBALS["GetFromForm"]->file("file");
	$thumbnail = $GLOBALS["GetFromForm"]->file("thumbnail");
	
	if(!empty($file["tmp"])){	
		
		@unlink(DOC_ROOT."/ecards/files/".$source["source"]);
	
		if(hs_eregi("flash",$file["type"])){
			$extension = ".swf";
			$values["type"] = "rich";
		}//if
		elseif(hs_ereg("gif",$file["type"])){
   			$extension = ".gif";
   			$values["type"] = "static";
   		}//elseif
   		elseif(hs_ereg("jpeg|jpg",$file["type"])){
	   		$extension = ".jpeg";
   			$values["type"] = "static";
   		}//elseif
   		
   		mt_srand((double) microtime() * 100000);
		$filename = hs_substr(md5(time().mt_rand(0,100)),0,15).$extension;
	
		move_uploaded_file($file["tmp"], DOC_ROOT."/ecards/files/".$filename);
		chmod(DOC_ROOT."/ecards/files/".$filename, 0777);
   		
	}//if
	else{
		$filename = $source["source"];
	}//else
	
	if(!empty($thumbnail["tmp"])){	
		
		@unlink(DOC_ROOT."/ecards/files/".$source["thumbnail"]);
	
		if(hs_ereg("gif",$thumbnail["type"])){
   			$extension = ".gif";   			
   		}//if
   		elseif(hs_ereg("jpeg|jpg",$thumbnail["type"])){
	   		$extension = ".jpeg";   			
   		}//elseif
   		
   		mt_srand((double) microtime() * 100000);
		$thumbfile = hs_substr(md5(time().mt_rand(0,100)),0,15).$extension;
	
		move_uploaded_file($thumbnail["tmp"], DOC_ROOT."/ecards/files/".$thumbfile);
		chmod(DOC_ROOT."/ecards/files/".$thumbfile, 0777);
   		
	}//if
	else{
		$thumbfile = $source["thumbnail"];
	}//else
	
	$sql_query = "REPLACE INTO ecards (crd_id,name,description,category,subcategory,source,thumbnail,type,tag,featured)
				  VALUES (':crd_id:',':name:',':description:',:category:,:subcategory:,':source:',':thumbnail:',':type:',':tag:',':featured:')";
	
	$values["source"] = $filename;
	$values["thumbnail"] = $thumbfile;
	if(!$values["featured"])
		$values["featured"] = 0;
	$values["time"] = time();
	
	$GLOBALS["DB"]->values = $values;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	load_page("index.php?section=ecards&action=edit&crd_id=".$values["crd_id"]."&ret_link=".urlencode($ret_link));
	
}//do_edit_ecard

function ecards_delete_ecard(){
	
	admin_validate();
	
	$crd_id = $GLOBALS["Get"]->val("crd_id");
	
	include_once(DOC_ROOT."/functions/delete_functions.php");
	
	if(!is_array($crd_id) && !empty($crd_id)){
		delete_ecard($crd_id);
	}//if
	elseif(is_array($crd_id) && count($crd_id)){
		foreach($crd_id as $id){
			delete_ecard($id);
		}//foreach
	}//else
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	load_page($ret_link);
	
}//ecards_delete_ecard

function view_ecard(){
	
	admin_validate();
	
	$crd_id = $GLOBALS["Get"]->type_val("crd_id","integer");
	
	$sql_query = "SELECT * FROM ecards
				  WHERE crd_id=".$crd_id;
	
	$data["values"] = $GLOBALS["DB"]->row($sql_query);
	
	$data["values"][tag] = stripslashes(html_entity_decode($data["values"][tag]));
	$data["values"][tag] = hs_ereg_replace(preg_quote("&#124;"),"|",$data["values"][tag]);
	
	if($data["values"]["type"] == "rich"){
		if(!$data["values"][tag]){
			include_once(DOC_ROOT."/classes/SWFHeader.class.php");
			$GLOBALS["SWFHeader"]->loadswf(URL."/ecards/files/".$data["values"]["source"]);
			$data["display"] = $GLOBALS["SWFHeader"]->display();		
		}//if
		else
			$data["display"] = hs_ereg_replace("\|file\|",URL."/ecards/files/".$data["values"]["source"],$data["values"][tag]);
	}//if
	else{
		$data["display"] = admin_output_image($data["values"]["source"],0,"ecards/files","",0,0);
	}//else
	
	$data["thumbnail"] = admin_output_image($data["values"]["thumbnail"],0,"ecards/files","",0,0);
	
	$data["title_id"] = "9";	
	$data["javascript"] = <<<HTMLTEXT
<script language="JavaScript">
	function HS_ResizeWindow(){
		var DocWidth = document.body.scrollWidth;
		var DocHeight = document.body.scrollHeight;
		window.resizeTo(DocWidth + 20, DocHeight + 60);
	}//HS_ResizeWindow
</script>
HTMLTEXT;

	$data["onload"] = "HS_ResizeWindow()";
	
	$layout[0] = "ecards_view";
	
	admin_popup_output($layout,$data);
	
}//view_ecard

function create_collection(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 6;
	$data["title_id"] = "10";	
	$vals = array("name","date_start","date_end","ecards_send","period_number","period_type");
	$data["values"] = $GLOBALS["Get"]->arr($vals,1);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	$data["link"] = $ret_link;
	
	$sql_query = "SELECT crd_id,name FROM ecards
				  WHERE crd_id > 100000";
	
	$ecards = $GLOBALS["DB"]->result($sql_query);
	
	$data["javascript"] = <<<JAVASCRIPTCODE
<script language="JavaScript">
<!--
	function HS_LoadOptions(FieldHandler, OptionsLine){
		if(OptionsLine != ''){
			ListItems = OptionsLine.split("|");
			OptionsHandler = FieldHandler.options;
			for(i=0;i<(ListItems.length/2);i++){
				j = i*2;
				OptionsHandler[i] = new Option(ListItems[j+1],ListItems[j]);
			}//for
		}//if
	}//HS_LoadOptions
	function HS_MoveOptions(FieldHandlerFrom, FieldHandlerTo){
		var TransferOptions = new Array();
		var j=0;
		var k=0;		
		OptionsHandlerFrom = FieldHandlerFrom.options;
		OptionsHandlerTo = FieldHandlerTo.options;		
		for(i=0;i<OptionsHandlerFrom.length;i++){
			if(OptionsHandlerFrom[i].selected == true){
				OptionsHandlerTo[OptionsHandlerTo.length] = 
				new Option(OptionsHandlerFrom[i].text,OptionsHandlerFrom[i].value);
				OptionsHandlerFrom[i] = null;
				TransferOptions[j] = i;
				j++;
				i--;				
			}//if
		}//for
	}//HS_MoveOptions
	function HS_PrepareData(FieldHandlerSrc, FieldHandlerResult){
		var ResultLine = "";
		OptionsHandlerSrc = FieldHandlerSrc.options;
		for(i=0;i<OptionsHandlerSrc.length;i++){
			ResultLine+= OptionsHandlerSrc[i].value + ",";
		}//for
		FieldHandlerResult.value = ResultLine;		
	}//HS_PrepareData
//-->
</script>
JAVASCRIPTCODE;

	$ecard_onload = "";
	foreach($ecards as $ecard){
		$ecards_onload.= $ecard["crd_id"]."|".$ecard["name"]."|";
	}//foreach
	$ecards_onload = rtrim($ecards_onload,'|');
	
	$data["onload"] = "HS_LoadOptions(document.collection.ecards_ex,'".htmlspecialchars(ereg_replace("'","\'",$ecards_onload))."');";
	
	$layout[0] = "ecards_create_collection";
	
	admin_output($layout,$data);
	
}//create_collection

function do_create_collection(){
	
	admin_validate();
	
	$vals = array("name","date_start","date_end","ecards_send","period_number","period_type");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	if(empty($values["date_start"][0]) || empty($values["date_start"][1]) || 
	   empty($values["date_end"][0]) || empty($values["date_end"][1]) ||
	   empty($values["name"]) || empty($values["period_number"])){
		$GLOBALS["uerr_id"] = 0;
		create_collection();
		exit;
	}//if
	
	$ecards = handle_empties(explode(",",$values["ecards_send"]));
	if(!count($ecards)){
		$GLOBALS["aerr_id"] = 29;
		create_collection();
		break;
	}//if
	
	$values["ecards"] = ",".implode(",",$ecards).",";
	$values["start"]  = mktime(0,0,1,$values["date_start"][1],$values["date_start"][0],date("Y"));
	$values["end"]  = mktime(23,59,1,$values["date_end"][1],$values["date_end"][0],date("Y"));
	if($values["end"] < $values["start"])
		$values["end"] = strtotime("+1 year",$values["end"]);
	
	$values["status"] = $GLOBALS["GetFromForm"]->field("status","switch");
	
	$values["recurrence_weeks"]  = 0;
	$values["recurrence_months"] = 0;
	$values["recurrence_years"]  = 0;
	
	switch($values["period_type"]){
		case 'week':
			$values["recurrence_weeks"] = $values["period_number"];
			break;
		case 'month':
			$values["recurrence_months"] = $values["period_number"];
			break;
		case 'year':
			$values["recurrence_years"] = $values["period_number"];
			break;
	}//switch
	
	$sql_query = "INSERT INTO ecards_collections(ecards,name,`start`,`end`,`status`,recurrence_weeks,recurrence_months,recurrence_years)
				  VALUES (':ecards:',':name:',:start:,:end:,':status:',:recurrence_weeks:,:recurrence_months:,:recurrence_years:)";
	
	$GLOBALS["DB"]->values = $values;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	load_page($ret_link);
	
}//do_create_collection

function edit_collection(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 6;
	$data["title_id"] = "10";	
	$data["col_id"] = $GLOBALS["Get"]->type_val("col_id","integer");
	
	$sql_query = "SELECT * FROM ecards_collections
				  WHERE col_id=".$data["col_id"];
	
	$data["values"] = $GLOBALS["DB"]->row($sql_query);
	
	if($data["values"]["recurrence_weeks"] > 0){
		$data["values"]["period_type"] = "week";
		$data["values"]["period_number"] = $data["values"]["recurrence_weeks"];
	}//if
	
	if($data["values"]["recurrence_months"] > 0){
		$data["values"]["period_type"] = "month";
		$data["values"]["period_number"] = $data["values"]["recurrence_months"];
	}//if
	
	if($data["values"]["recurrence_years"] > 0){
		$data["values"]["period_type"] = "year";
		$data["values"]["period_number"] = $data["values"]["recurrence_years"];
	}//if
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	$data["link"] = $ret_link;
	
	$sql_query = "SELECT crd_id,name FROM ecards
				  WHERE crd_id > 100000";
	
	$result = $GLOBALS["DB"]->result($sql_query);
	$ecards = array();
	
	foreach($result as $res){
		$ecards[$res["crd_id"]] = $res["name"];
	}//foreach
	
	$data["javascript"] = <<<JAVASCRIPTCODE
<script language="JavaScript">
<!--
	function HS_LoadOptions(FieldHandler, OptionsLine){
		if(OptionsLine != ''){
			ListItems = OptionsLine.split("|");
			OptionsHandler = FieldHandler.options;
			for(i=0;i<(ListItems.length/2);i++){
				j = i*2;
				OptionsHandler[i] = new Option(ListItems[j+1],ListItems[j]);
			}//for
		}//if
	}//HS_LoadOptions
	function HS_MoveOptions(FieldHandlerFrom, FieldHandlerTo){
		var TransferOptions = new Array();
		var j=0;
		var k=0;		
		OptionsHandlerFrom = FieldHandlerFrom.options;
		OptionsHandlerTo = FieldHandlerTo.options;		
		for(i=0;i<OptionsHandlerFrom.length;i++){
			if(OptionsHandlerFrom[i].selected == true){
				OptionsHandlerTo[OptionsHandlerTo.length] = 
				new Option(OptionsHandlerFrom[i].text,OptionsHandlerFrom[i].value);
				OptionsHandlerFrom[i] = null;
				TransferOptions[j] = i;
				j++;
				i--;				
			}//if
		}//for
	}//HS_MoveOptions
	function HS_PrepareData(FieldHandlerSrc, FieldHandlerResult){
		var ResultLine = "";
		OptionsHandlerSrc = FieldHandlerSrc.options;
		for(i=0;i<OptionsHandlerSrc.length;i++){
			ResultLine+= OptionsHandlerSrc[i].value + ",";
		}//for
		FieldHandlerResult.value = ResultLine;		
	}//HS_PrepareData
//-->
</script>
JAVASCRIPTCODE;

	$col_cards = handle_empties(explode(",",$data["values"]["ecards"]));
	$ex_cards = handle_empties(array_diff(array_keys($ecards),$col_cards));
	
	$onload[0] = "";
	foreach($col_cards as $ecard){
		$onload[0].= $ecard."|".$ecards[$ecard]."|";
	}//foreach
	$onload[0] = rtrim($onload[0],'|');
	
	$onload[1] = "";
	foreach($ex_cards as $ecard){
		$onload[1].= $ecard."|".$ecards[$ecard]."|";
	}//foreach
	$onload[1] = rtrim($onload[1],'|');
	
	$data["onload"] = "HS_LoadOptions(document.collection.ecards_ex,'".htmlspecialchars(ereg_replace("'","\'",$onload[1]))."');";
	$data["onload"].= "HS_LoadOptions(document.collection.ecards,'".htmlspecialchars(ereg_replace("'","\'",$onload[0]))."');";
	
	$date_start[0] = date("j",$data["values"]["start"]);
	$date_start[1] = date("n",$data["values"]["start"]);
	
	$date_end[0] = date("j",$data["values"]["end"]);
	$date_end[1] = date("n",$data["values"]["end"]);
	
	$data["values"]["date_start"] = $date_start;
	$data["values"]["date_end"] = $date_end;
	
	$layout[0] = "ecards_edit_collection";
	
	admin_output($layout,$data);
	
}//edit_collection

function do_edit_collection(){
	
	admin_validate();
	
	$vals = array("name","date_start","date_end","ecards_send","period_number","period_type","col_id");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	if(empty($values["date_start"][0]) || empty($values["date_start"][1]) || 
	   empty($values["date_end"][0]) || empty($values["date_end"][1]) ||
	   empty($values["name"]) || empty($values["period_number"])){
		$GLOBALS["uerr_id"] = 0;
		edit_collection();
		exit;
	}//if
	
	$ecards = handle_empties(explode(",",$values["ecards_send"]));
	if(!count($ecards)){
		$GLOBALS["aerr_id"] = 29;
		edit_collection();
		break;
	}//if
	
	$values["ecards"] = ",".implode(",",$ecards).",";
	$values["start"]  = mktime(0,0,1,$values["date_start"][1],$values["date_start"][0],date("Y"));
	$values["end"]  = mktime(23,59,1,$values["date_end"][1],$values["date_end"][0],date("Y"));
	if($values["end"] < $values["start"])
		$values["end"] = strtotime("+1 year",$values["end"]);		
	
	$values["status"] = $GLOBALS["GetFromForm"]->field("status","switch");
	
	switch($values["period_type"]){
		case 'week':
			$values["recurrence_weeks"] = $values["period_number"];
			break;
		case 'month':
			$values["recurrence_months"] = $values["period_number"];
			break;
		case 'year':
			$values["recurrence_years"] = $values["period_number"];
			break;
	}//switch
	
	$sql_query = "REPLACE INTO ecards_collections(col_id,ecards,name,`start`,`end`,`status`,recurrence_weeks,recurrence_months,recurrence_years)
				  VALUES (:col_id:,':ecards:',':name:',:start:,:end:,':status:',:recurrence_weeks:,:recurrence_months:,:recurrence_years:)";
	
	$GLOBALS["DB"]->values = $values;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	load_page("index.php?section=ecards&action=edit_collection&col_id=".$values["col_id"]."&ret_link=".urlencode($ret_link));
	
}//do_edit_collection

function delete_collection(){
	
	admin_validate();
	
	$col_id = $GLOBALS["Get"]->val("col_id");	
	
	if(!is_array($col_id) && !empty($col_id)){			
		$sql_query = "DELETE FROM ecards_collections
					  WHERE col_id=".$col_id;
		$GLOBALS["DB"]->execute($sql_query);
	}//if
	elseif(is_array($col_id) && count($col_id)){
		$sql_query = "DELETE FROM ecards_collections
					  WHERE col_id IN (".implode(",",$col_id).")";
		$GLOBALS["DB"]->execute($sql_query);
	}//else
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	load_page($ret_link);
	
}//delete_collection

function activate_collection(){
	
	admin_validate();
	
	$col_id = $GLOBALS["Get"]->type_val("col_id","integer");
	
	$sql_query = "UPDATE ecards_collections
				  SET `status`='1'
				  WHERE col_id=".$col_id;
	
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	load_page($ret_link);
	
}//activate_collection

function deactivate_collection(){
	
	admin_validate();
	
	$col_id = $GLOBALS["Get"]->type_val("col_id","integer");
	
	$sql_query = "UPDATE ecards_collections
				  SET `status`='0'
				  WHERE col_id=".$col_id;
	
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = $GLOBALS["Get"]->val("ret_link");
	$ret_link = stripslashes(html_entity_decode($ret_link));
	
	load_page($ret_link);
	
}//activate_collection

function featured_add_ecard(){
	
	admin_validate();
	
	$crd_id = $GLOBALS["Get"]->val("crd_id");
	
	$sql_query = "UPDATE ecards SET featured='1' WHERE crd_id=".$crd_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//featured_add_ecard

function featured_remove_ecard(){
	
	admin_validate();
	
	$crd_id = $GLOBALS["Get"]->val("crd_id");
	
	$sql_query = "UPDATE ecards SET featured='0' WHERE crd_id=".$crd_id;
	$GLOBALS["DB"]->execute($sql_query);
	
	$ret_link = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ret_link")));
	load_page($ret_link);
	
}//featured_remove_ecard

function import_ecards(){
	
	admin_validate();
	
	$data["section"] = "manage";
	$data["subsection"] = 6;
	$data["title_id"] = "6";
	$vals = array("file");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	if(empty($values["file"]) || !file_exists(DOC_ROOT."/import/ecards/".$values["file"])){
		$GLOBALS["aerr_id"] = 12;
		ecards_manager();
		exit;
	}//if
	
	$data["file"] = $values["file"];
	
	$data["link"] = "index.php?section=ecards&action=do_import";
	
	while(list($key,$val) = each($values)){
		if(is_array($val)){
			while(list($k,$v) = each($val)){
				$data["link"].= "&".$key."[".$k."]=".urlencode($v);
			}//while
		}//if
		else
			$data["link"].= "&".$key."=".urlencode($val);
	}//while
	
	$data["javascript"] = "
<script language=\"JavaScript\">
	function HS_PullReport( status ){
		MessageHandler = document.getElementById('status');
		MessageHandler.innerHTML = status;
	}//HS_PullReport
	function HS_StartImport(){
		ImportHandler = document.getElementById('import_processor');
		ImportHandler.src = \"".$data["link"]."\";
	}//HS_StartImport
</script>
";

	$data["onload"] = "HS_StartImport();HS_PullReport('Import in process...');";
	
	$layout[0] = "import_ecards_process";
	
	admin_output($layout,$data);
	
}//import_ecards

function do_import_ecards(){
	
	set_time_limit(0);
	
	admin_validate();
	
	$vals = array("file");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	extract($values);
	
	if(empty($file) || !file_exists(DOC_ROOT."/import/ecards/".$file)){
		load_page("index.php?section=ecards&err_id=12");
	}//if
	
	$data["file"] = $file;
	
	Header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    Header("Cache-Control: no-cache, must-revalidate"); 
	Header("Pragma: no-cache");
	
	$line_count = 1;
	$imported = 0;
	$report = "";	
	$file_handler = fopen(DOC_ROOT."/import/ecards/".$file,"r");
	while($line = @fgets($file_handler, 1024)){
		trim($line);
		$line  = ereg_replace("\r|\n","",$line);
		$items = explode("\t",$line);
		
		$category 		= $items[0];
		$subcategory 	= $items[1];
		$name 			= $items[2];
		$description 	= $items[3];
		$type 			= $items[4];
		$ecard 			= $items[5];
		$thumb 			= $items[6];
		$tag 			= $items[7];
		
		if(empty($ecard) || !file_exists(DOC_ROOT."/import/ecards/".$ecard)){
			$report.= "Line ".$line_count.": eCard file is missing or is not actually available.";
			$line_count++;
			continue;
		}//if

		if(empty($thumb) || !file_exists(DOC_ROOT."/import/ecards/".$thumb)){
			$report.= "Line ".$line_count.": thumbnail file is missing or is not actually available.";
			$line_count++;
			continue;
		}//if
		
		$info = getimagesize(DOC_ROOT."/import/ecards/".$ecard);

		if(hs_eregi("flash",$info["mime"])){
			$extension = ".swf";
			$values["type"] = "rich";
		}//if
		elseif(hs_ereg("gif",$info["mime"])){
			$extension = ".gif";
			$values["type"] = "static";
		}//elseif
		elseif(hs_ereg("jpeg|jpg",$info["mime"])){
			$extension = ".jpeg";
			$values["type"] = "static";
		}//elseif

		mt_srand((double) microtime() * 100000);
		$filename = hs_substr(md5(time().mt_rand(0,100)),0,15);
		$thumbfile = $filename."th";
		$filename.= $extension;

		copy(DOC_ROOT."/import/ecards/".$ecard, DOC_ROOT."/ecards/files/".$filename);
		chmod(DOC_ROOT."/ecards/files/".$filename, 0777);
		
		$info2 = getimagesize(DOC_ROOT."/import/ecards/".$thumb);

		if(hs_ereg("gif",$info2["mime"])){
			$extension = ".gif";
		}//elseif
		elseif(hs_ereg("jpeg|jpg",$info2["mime"])){
			$extension = ".jpeg";
		}//elseif

		$thumbfile.= $extension;

		copy(DOC_ROOT."/import/ecards/".$thumb, DOC_ROOT."/ecards/files/".$thumbfile);
		chmod(DOC_ROOT."/ecards/files/".$thumbfile, 0777);
		
		include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/system/ecards_categories.inc.php");
		
		$cat_id = array_search($category,$categories);
		if($cat_id){
			$sub_cat_id = array_search($subcategory,$subcategories[$cat_id]);
			if(!$sub_cat_id){
				$subcategories[$cat_id][] = $subcategory;
				$sub_cat_id = max(array_keys($subcategories[$cat_id]));
			}//if
		}//if
		else{
			$categories[] = $category;
			$cat_id = $ind = max(array_keys($categories));
			$subcategories[$ind][] = $subcategory;
			$sub_cat_id = max(array_keys($subcategories[$cat_id]));
		}//else
		
		$wrt = "<?php\n\n";
		reset($categories);
		while(list($key,$val) = each($categories)){
			$wrt.= "\$categories[".$key."] = \"".ereg_replace('"','\"',$val)."\";\n";
			reset($subcategories[$key]);
			while(list($k,$v) = each($subcategories[$key])){
				$wrt.= "\$subcategories[".$key."][".$k."] = \"".ereg_replace('"','\"',$v)."\";\n";
			}//while
		}//while
		$wrt.= "\n?>";
		
		$f = fopen(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/system/ecards_categories.inc.php","w");
		fwrite($f,$wrt);
		fclose($f);

		$sql_query = "INSERT INTO ecards (name,description,category,subcategory,source,thumbnail,type,tag,featured,added)
				  VALUES (':name:',':description:',:category:,:subcategory:,':source:',':thumbnail:',':type:',':tag:',':featured:',:time:)";

		$values["name"] = $name;
		$values["description"] = $description;
		$values["category"] = $cat_id;
		$values["subcategory"] = $sub_cat_id;
		$values["type"] = $type;
		$values["tag"] = $tag;
		$values["source"] = $filename;
		$values["thumbnail"] = $thumbfile;
		if(!$values["featured"])
		$values["featured"] = 0;
		$values["time"] = time();

		$GLOBALS["DB"]->values = $values;
		$GLOBALS["DB"]->execute($sql_query);
			
		$imported++;
	}//while
	fclose($file_handler);
	
	$report = $imported." ecards has been successfully imported.<br>".$report;
	
	?><script language="JavaScript">
	<!--
		parent.HS_PullReport("<?php echo $report; ?>");
	//-->
	</script><?php
	
}//do_import_ecards

?>