<?php
/******************************************************************************************************/
// Get Districts
$d = array();
$districts = array();
$q = "select * from cyprus_districts order by district asc;";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	while ($row = mysql_fetch_assoc($r)) {
		$d [] = array_map('stripslashes', $row);
		$districts [$row['dcode']] = stripslashes($row['district']);
	}
}

// Get Locations
$a = array();
$locations = array();
$q = "select * from cyprus_locations order by location asc;";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	while ($row = mysql_fetch_assoc($r)) {
		$dcode = $row['dcode'];
		$lcode = $row['lcode'];
		
		if(isset($locations[$dcode])) {
			$locations[$dcode][$lcode] = stripslashes($row['location']);
		}
		else {
			$locations[$dcode] = array();
			$locations[$dcode][$lcode] = stripslashes($row['location']);
		}
		
	}
}
/******************************************************************************************************/
$show_init_form = false;	/* To display or not the initialization form */

$num_results = array(
	"99999"		=> "All",
	"10"	=> "10",
	"15"	=> "15",
	"20"	=> "20",
	"25"	=> "25",
	"30"	=> "30",
	"40"	=> "40",
	"50"	=> "50",
	"100"	=> "100",
	"200"	=> "200"
);


/* Try to see if the call is from administration panel */
if(isset($admin_catdom_module_id) && $admin_catdom_module_id>0 && isset($_REQUEST['action']) && $_REQUEST['action'] == "custom_module") {
	$show_init_form = true;

	$wb_domainname = '';
	$wb_uid = '';
	$wb_subcats = array();

	// Get domain
	$p_catdom_id = intval($_REQUEST['catdom_id']);
	$p_domain_id = $p_catdom_id;
	$q = "select * from domaincategories where catdom_id='$p_catdom_id';";
	$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
	if($r && mysql_num_rows($r)>0) {
		$row = mysql_fetch_assoc($r);
		if(intval($row['parent_id'])>0) {
			$p_domain_id = intval($row['parent_id']);
		}
	}
	
	// Get the real domain name
	$q = "select * from domaincategories where catdom_id='$p_domain_id';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		$row = mysql_fetch_assoc($r);
		
		$wb_domainname = str_replace('www.', '', $row['domainname']);
	}
	
	
	// Get the wb UID
	$q = "select * from wb_categories_domains where wb_domainname='$wb_domainname';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		$row = mysql_fetch_assoc($r);
		
		$wb_uid = $row['wb_uid'];
	}
	
	// Get the main subcategories if any
	$q = "select * from wb_filters_mains where wb_uid='$wb_uid' and wb_uid!='' order by pos asc;";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		while ($row = mysql_fetch_assoc($r)) {
		
			$wb_subcats [$row['fm_uid']] = stripslashes($row['title']);
		}
	}
	
	// Initialize defaults

	$dcode = '';
	$lcode = '';
	
	$wb_search_form_title = '';
	$wb_search_post_url = '';
	$module_num_results = 20;
	$module_show_filter = 1;
	$fm_uid = '';
	$module_selected_fm_uid = '';
	
	$module_map_search = 0;
	$module_map_search_image = '';
	
	$current_catdom_module_id = $admin_catdom_module_id;

}

if($show_init_form) {
	
	?>
	<style type="text/css">
		#selected_categs ul {
			margin:4px;
			padding:0;
		}
		
		#selected_categs ul li {
			list-style:none;
			margin: 3px;
		}
	
	</style>
	<?php
	
	

	//print_r($module_params);
	// Try to get any previously saved customization
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
	
	
	?>
	<div class="inline_container">
	<form method="POST" action="custom_module_update.php" name="wb_search_results_frm" id="wb_search_results_frm">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<th colspan="2" align="left">
					WB Search Form : Standalone
				</th>
			</tr>
			<tr>
				<td nowrap class="itdh">Display Title</td>
				<td nowrap class="itd_lb">
					<input type="text" name="wb_search_form_title" id="wb_search_form_title" value="<?php echo $wb_search_form_title;?>" size="30" maxlength="56" style="width:100%;">
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh" nowrap width="20%">District</td>
				<td nowrap class="itd_lb" width="80%">
					<select name="dcode" id="dcode">
						<option value="" selected="selected">All Districts</option>
						<?php
						foreach ($districts as $t_dcode=>$t_district) {
							$selected = "";
							if($dcode == $t_dcode) $selected = 'selected="selected"';
							echo '<option value="', $t_dcode, '" ', $selected, '>', $t_district, '</option>';
						}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Location</td>
				<td nowrap class="itd_lb">
					<select name="lcode" id="lcode">
						<option value="" selected="selected">Any Location</option>
						<?php
						if($dcode != "") {
							foreach ($locations[$dcode] as $t_lcode=>$t_location) {
								$selected = "";
								if($lcode == $t_lcode) $selected = 'selected="selected"';
								echo '<option value="', $t_lcode, '" ', $selected, '>', $t_location, '</option>';
							}
						}
						
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Sub Category</td>
				<td nowrap class="itd_lb">
					<select name="fm_uid" id="fm_uid">
						<option value="" selected="selected">Any Subcategory</option>
						<?php
						if(count($wb_subcats) > 0) {
							foreach ($wb_subcats as $_fm_uid=>$_fm_title) {
								$selected = "";
								if($fm_uid == $_fm_uid) $selected = 'selected="selected"';
								echo '<option value="', $_fm_uid, '" ', $selected, '>', $_fm_title, '</option>';
							}
						}
						
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Pre-Selected Category</td>
				<td nowrap class="itd_lb">
					<select name="module_selected_fm_uid" id="module_selected_fm_uid">
						<option value="" selected="selected">None</option>
						<?php
						if(count($wb_subcats) > 0) {
							foreach ($wb_subcats as $_fm_uid=>$_fm_title) {
								$selected = "";
								if($module_selected_fm_uid == $_fm_uid) $selected = 'selected="selected"';
								echo '<option value="', $_fm_uid, '" ', $selected, '>', $_fm_title, '</option>';
							}
						}
						
						?>
					</select>
				</td>
			</tr>
			
			<tr>
				<td nowrap class="itdh">Friendly URL of the page displaying results</td>
				<td nowrap class="itd_lb">
					<input type="text" name="wb_search_post_url" id="wb_search_post_url" value="<?php echo $wb_search_post_url;?>" size="30" maxlength="56" style="width:70%;">
				</td>
			</tr>
			<tr>
				<td colspan="2" align="right" class="footer">
					<input type="hidden" value="standalone" name="wb_search_type"/>
					<input type="reset" value="Reset" name="submit" class="submit_button"/>
					<input type="submit" value="Save" name="submit" class="submit_button"/>
					<input type="hidden" value="<?php echo $current_catdom_module_id; ?>" name="admin_catdom_module_id" id="admin_catdom_module_id"/>
				</td>
			</tr>
		</table>
	</form>
	</div>

	<script type="text/javascript">
		$('#dcode').change(function(){
			if($(this).val() != "") {
				$.ajax({
					url: '<?php echo $protocol.$_SERVER['HTTP_HOST'];?>/admin/modules/cyprus/ajax/locations_by_district.php',
					data: 'district=' + $(this).val(),
					cache: false,
					type: 'post',
					dataType: 'json',
					success: function(r) {
						if(typeof(r) !== undefined) {
							$('#lcode').html('');
							var locations = '<option value="" selected="selected">Please Select</option>';
							$.each(r, function(i, ar){
								locations += '<option value="'+ar.lcode+'">'+ar.location+'</option>';
							});
							
							$('#lcode').html(locations);
						}
						
						
					}
				});
			}
			else {
				$('#lcode').html('');
				var locations = '<option value="" selected="selected">Please select</option>';
				$('#lcode').html(locations);
			}
		});
		
		var $diag = $('<div id="wb_res_browser"></div>').dialog({
			autoOpen: false,
			width: 800,
			title: 'Resource Browser'
		});
		
		$('.get_wb_res_image').live('click',function(ev){
			ev.preventDefault();
			
			
			
			var wb_uid = $(this).attr('rel');
			var wb_start = ($(this).attr('start') != undefined) ? $(this).attr('start') : 'cyprus';
			
			//console.log(wb_start);
			
			if(wb_uid != '') {
				$.ajax({
					url: '<?php echo $protocol.$_SERVER['HTTP_HOST'];?>/admin/ajax/browse_wb_res.php',
					data: 'wb_uid=' + wb_uid + '&wb_folder=' + wb_start,
					cache: false,
					type: 'POST',
					success: function(r) {
						$('#wb_res_browser').html(r);
						$diag.dialog('open');
					}
				});
			}
			
			return false;
		});
		
		
		$('.set_wb_res_image').live('click',function(ev){
			ev.preventDefault();
			var img_url = $(this).attr('rel');
			$('#module_map_search_image').val(img_url);
			$diag.dialog('close');
			return false;
		});
		
	</script>
	<?php
	
	$module_forms = array(
		"form_name" => "wb_search_results_frm",
		"form_action" => "",
		"form_method" => "POST"
	);
}
else {
	// We are in the front-end
	// Initialize defaults
	$dcode = '';
	$lcode = '';
	
	$wb_search_form_title = '';
	$wb_search_post_url = '';
	$module_num_results = 30;
	$module_show_filter = 1;
	$fm_uid = '';
	$module_selected_fm_uid = '';
	$module_map_search = 0;
	$module_map_search_image = '';
	
	// Try to get any customization done
	if(isset($module_params) && is_array($module_params)) {
		
		
		
		if(isset($_REQUEST['debug'])) {
			echo '<pre>', var_export($module_params), '</pre>';
		}
		
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
}
?>
