<?php
$show_init_form = false;	/* To display or not the initialization form */

/* 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;

	// 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']);
	}
	
	$module_use_defaults = 0;
	$yp_middle_hide_default = 1;
	$module_yp_categs = array();
	$module_yp_title = "";
	$module_yp_description = "";
    $module_yp_robots = "";
	
	$current_catdom_module_id = $admin_catdom_module_id;

}

if($show_init_form) {
	
	//require_once("modules/contents/functions.php");
	//require_once("functions/root_display.php");
	//require_once("classes/tree_radio_class.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;
		}
	}
	?>
	<style type="text/css">
		#ws_categs ul {
			margin:0;padding:0;list-style:none;
		}
		
		#ws_categs ul li{
			margin:3px;
			border-bottom: 1px solid #cccccc;
		}
		
		#categs ul {
			margin:0;padding:0;list-style:none;
		}
		
		#categs ul li{
			margin:3px;
			border-bottom: 1px solid #cccccc;
		}
		
	</style>
	<div id="yp_middle" class="inline_container">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<th colspan="3" align="left">
					Webservices Yellow Pages Categories Selection
				</th>
			</tr>
			<tr>
				<td nowrap class="itdh" width="20%">Categories Search</td>
				<td class="itd" width="80%">
					<input type="text" name="ws_categ_term" id="ws_categ_term" value="" style="width:100%;">
				</td>
				<td class="itd_lb" align="center">
					<input type="submit" id="search_yp_categs" value="Search" class="submit_button">
				</td>
			</tr>
			<tr>
				<td class="itd" colspan="3">
					<div id="ws_categs" style="height:250px; overflow-y:auto;">
						<ul id="ws_categs_ul"></ul>
					</div>
				</td>
			</tr>
		</table>
		
		<form method="POST" action="custom_module_update.php" name="yp_middle_frm" id="yp_middle_frm">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<th colspan="2" align="left">
					Selected Categories
				</th>
			</tr>
			<tr>
				<td class="itd" colspan="3">
					<div id="categs" style="height:250px; overflow-y:auto;">
						<ul id="categs_ul">
						<?php
						if(count($module_yp_categs) > 0) {
							foreach ($module_yp_categs as $uid => $title) {
								echo '<li id="s_' . $uid . '"><input type="checkbox" id="' . $uid. '" name="module_yp_categs['.$uid.']" value="'.$title.'" checked="checked" class="yp_categs"> '.$title.'</li>';
							}
						}
						?>
						
						</ul>
					</div>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">YP Title</td>
				<td class="itd">
					<input type="text" name="module_yp_title" value="<?php echo $module_yp_title; ?>" style="width:100%;"/>
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">YP Meta Description</td>
				<td class="itd">
					<textarea name="module_yp_description" rows="4"  style="width:100%;"><?php echo $module_yp_description; ?></textarea>
				</td>
			</tr>
            <tr>
                <td nowrap class="itdh">YP META ROBOTS</td>
                <td class="itd">
                    <input type="text" name="module_yp_robots" value="<?php echo $module_yp_robots; ?>" style="width:100%;"/>
                    If empty acts as: "index, follow". Possible values separated by comma: index, noindex, follow, nofollow (only 2 at a time)
                </td>
            </tr>
			<tr>
				<td nowrap class="itdh">Use defaults?</td>
				<td class="itd">
					<input type="radio" name="module_use_defaults" value="1" <?php echo ($module_use_defaults == 1) ? "checked" : ""; ?> style="vertical-align:middle;"/> Yes 
					<input type="radio" name="module_use_defaults" value="0" <?php echo ($module_use_defaults == 0) ? "checked" : ""; ?> style="vertical-align:middle;"/> No
				</td>
			</tr>
			<tr>
				<td nowrap class="itdh">Hide as a middle module?</td>
				<td class="itd">
					<input type="radio" name="yp_middle_hide_default" value="1" <?php echo (intval($yp_middle_hide_default) == 1) ? 'checked="checked"' : ""; ?> style="vertical-align:middle;"/> Yes 
					<input type="radio" name="yp_middle_hide_default" value="0" <?php echo (intval($yp_middle_hide_default) == 0) ? 'checked="checked"' : ""; ?> style="vertical-align:middle;"/> No
				</td>
			</tr>
			
			<tr>
				<td colspan="2" align="right" class="footer">
					<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">
	function enableCkSelect() {
		$('input.ws_categs').click(function(ev){
			var uid = $(this).val();
			if($(this).prop('checked')) {
				var title = $(this).attr('rel');
				var li = '<li id="s_' + uid + '"><input type="checkbox" id="' + uid + '" name="module_yp_categs['+uid+']" value="'+title+'" class="yp_categs" checked="checked"> '+title+'</li>';
				$('#categs_ul').append(li);
			}
			else {
				if($('#s_' + uid).length > 0) {
					$('#s_' + uid).remove();
				}
			}
		});
	}
	
	$(document).ready(function(){
		var $p = $('#yp_middle');
		var $ws = $('#ws_categs');
		var $yp = $('#categs');
		
		$('#search_yp_categs', $p).click(function(ev){
			ev.preventDefault();
			$.ajax({
				url: 'https://<?php echo $_SERVER['HTTP_HOST'];?>/amodules/yp_middle/ajax/get_ws_categories.php',
				data: 'term=' + $('#ws_categ_term').val(),
				cache: false,
				type: 'post',
				dataType: 'json',
				success: function(msg) {
					//console.log(msg);
					$('#ws_categs_ul').empty();
					if(msg.result == 1) {
						var data = msg.categs;
						$.each(data['data'], function(uid, cat){
							var checked = '';
							if($('#s_' + uid).length > 0) {
								checked = 'checked="checked"';
							}
							var li = '<li>';
							li = li + '<input type="checkbox" rel="' + cat.cy_admin_title_en + '" id="ws_'+uid+'" value="' + uid + '" '+checked+' class="ws_categs"> ';
							li = li + '' + cat.cy_admin_title_en + ' (' + cat.num_rec + ')'; 
							li = li + '</li>'
							
							$('#ws_categs_ul').append(li);
						});
						enableCkSelect();
					}
				}
			});
			
			/*
			
			*/
			
			return false;
		});
		
		
		
	});
	</script>
	<?php
	
	$module_forms = array(
		"form_name" => "yp_middle_frm",
		"form_action" => "",
		"form_method" => "POST"
	);
}
else {
	// We are in the front-end
	// Initialize defaults
	$module_use_defaults = 0;
	$yp_middle_hide_default = 1;
	$module_yp_categs = array();
    $module_yp_title = "";
    $module_yp_description = "";
    $module_yp_robots = "";
	
	// Try to get any customization done
	if(isset($module_params) && is_array($module_params)) {
		foreach ($module_params as $key=>$value) {
			${$key} = $value;
		}
	}
}
?>
