<?php
function checkGoogleScript($g_script){
	$first_tag = substr($g_script, 0, 8);
	$last_tag = substr($g_script, -9);
	$script_error = '<!--google analytics script error-->';
	
	if($first_tag == '<script>' && $last_tag =='</script>'){
		return $g_script;
	} else {
		return $script_error;
	}
}

function checkSection($section_link){
	$sql = mysql_query('SELECT `link` FROM `section` WHERE `show` = 1 AND `link` != "#" ORDER BY `rank` ASC')or die(mysql_error());
	while ($row = mysql_fetch_assoc($sql)){
		$sections[] = $row['link'];
	}
	$return = false;
	
	foreach ($sections as $s_link): 
		if($section_link == $s_link){
			$return =  true;
			break;
		} 
	endforeach ;
	
	return $return;
}

function cdc_checkSection($section_link){
	$sql = mysql_query('SELECT `link` FROM `cdc_section` WHERE `show` = 1 AND `link` != "#" ORDER BY `rank` ASC')or die(mysql_error());
	while ($row = mysql_fetch_assoc($sql)){
		$sections[] = $row['link'];
	}
	$return = false;
	
	foreach ($sections as $s_link): 
		if($section_link == $s_link){
			$return =  true;
			break;
		} 
	endforeach ;
	
	return $return;
}

function getSectionInfo($section_link){
	global $section_name;
	
	$sql = mysql_query('SELECT `section_id`, `cp_name`, `ep_name`, `sp_name`, `seo_meta_key`, `seo_description` FROM `section` WHERE `link` = "'.$section_link.'"') or die(mysql_error());
	$row = mysql_fetch_assoc($sql);
        if (! $row) {
            $sql = mysql_query('SELECT `section_id`, `cp_name`, `ep_name`, `sp_name`, `seo_meta_key`, `seo_description` FROM `section` ORDER BY rank LIMIT 0,1') or die(mysql_error());
            $row = mysql_fetch_assoc($sql);
        }
	return array($row['section_id'], $row[$section_name], $row['seo_meta_key'], $row['seo_description']);
}

function cdc_getSectionInfo($section_link){
	global $section_name;
	
	$sql = mysql_query('SELECT `section_id`, `cp_name`, `ep_name`, `sp_name`, `seo_meta_key`, `seo_description` FROM `cdc_section` WHERE `link` = "'.$section_link.'"') or die(mysql_error());
	$row = mysql_fetch_assoc($sql);
	return array($row['section_id'], $row[$section_name], $row['seo_meta_key'], $row['seo_description']);
}

function defineSectionPHP($id){
	$sql = mysql_query('SELECT `type` FROM `section` WHERE `section_id` = '.$id) or die(mysql_error());
	$row = mysql_fetch_assoc($sql);
	
	switch ($row['type']){
		case 1:
			return 'news.php';
			break;
		case 2:
			return 'content.php';
			break;	
		case 3:
			return 'gallery.php';
			break;	
		case 4:
			// return 'product.php';
			return 'doctor.php';
			break;	
		case 5:
			return 'home.php';
			break;
		case 6:
			return 'sharing.php';
			break;
		case 7:
			return 'contact.php';
			break;
		case 8:
			return 'single.php';
			break;
		default :
			return 'content.php';
			break;	
	}
}
function cdc_defineSectionPHP($id){
	$sql = mysql_query('SELECT `type` FROM `cdc_section` WHERE `section_id` = '.$id) or die(mysql_error());
	$row = mysql_fetch_assoc($sql);
	
	switch ($row['type']){
		case 1:
			return 'cdc_news.php';
			break;
		case 2:
			return 'cdc_content.php';
			break;	
		case 3:
			return 'cdc_gallery.php';
			break;	
		case 4:
			// return 'product.php';
			return 'cdc_doctor.php';
			break;	
		case 5:
			return 'cdc_home.php';
			break;
		case 6:
			return 'cdc_sharing.php';
			break;
		case 7:
			return 'cdc_contact.php';
			break;
		default :
			return 'cdc_content.php';
			break;	
	}
}
function displayPagesNav($this_page, $last_page){
	global $section, $current_path;
	$prev_page = $this_page -1 ;
	$next_page = $this_page + 1;
	
	if($this_page == 1 && $this_page != $last_page){
		$btn_prev = '';
		$btn_next = '<a href="'.$current_path.'&page='.$next_page.'">'.TXT_NEXT_PAGE.'</a>';
	} elseif ($this_page == 1 && $this_page == $last_page){
		$btn_prev = '';
		$btn_next = '';
	} elseif($this_page == $last_page){
		$btn_prev = '<a href="'.$current_path.'&page='.$prev_page.'">'.TXT_PREV_PAGE.'</a>';
		$btn_next = '';
	} else {
		$btn_prev = '<a href="'.$current_path.'&page='.$prev_page.'">'.TXT_PREV_PAGE.'</a>';
		$btn_next = '<a href="'.$current_path.'&page='.$next_page.'">'.TXT_NEXT_PAGE.'</a>';
	}

	echo $btn_prev;
				
	for ($i = 1; $i <= $last_page; $i++){
		if ($this_page == $i){
			$selected_page = 'class="selected"';
		} else {
			$selected_page = '';
		}
		echo '<a href="'.$current_path.'&page='.$i.'" '.$selected_page.'>'.$i.'</a>';
	}
	echo $btn_next;
}


//for products
function listOptoins($p_id){
	global $option_name;
	$all_option ='';
	$sql_options = mysql_query('SELECT `'.$option_name.'`, `option_id` FROM `options` WHERE `show` = 1 ORDER BY `rank` ASC') or die(mysql_error());
	$row_optoins = mysql_fetch_assoc($sql_options);
	
	do {
		 $all_option .= $row_optoins[$option_name].': '.getValue($p_id, $row_optoins['option_id']).'<br>';
	} while ($row_optoins = mysql_fetch_assoc($sql_options));
	return $all_option;
}

function getValue($m_p_id, $m_o_id){
	global $option_value;
	
	$temp_sql = mysql_query('SELECT `value_id`, `match_p_id`, `match_option_id`, `'.$option_value.'` FROM `options_value` WHERE `match_p_id` = '.$m_p_id.' AND `match_option_id` = '.$m_o_id) or die(mysql_error());
	$temp_row = mysql_fetch_assoc($temp_sql);
	
	if($temp_row){
		return $temp_row[$option_value];
	} else {
		return '--';
	}
}
function getSubCatName($id){
	global $subcat_name;
	
	if ($id){
		$temp_sql = mysql_query('SELECT `subcat_id`, `match_cat_id`, `'.$subcat_name.'` FROM `subcate` WHERE `subcat_id` = '.$id) or die(mysql_error());
		$temp_row = mysql_fetch_assoc($temp_sql);
		$result = $temp_row[$subcat_name];
	} else {
		$result = '--';
	}
	return $result;
}

//shopping cart funciton
if (microtime_float() - $_SESSION['Add_Cart_Time'] > 3600){
	// remove all the session variables 
	unset($_SESSION["cart_array"]);
	// Redirect to login
} else {
	// update cart time
	$_SESSION['Add_Cart_Time'] = microtime_float();
}

function microtime_float(){
	list($usec, $sec) = explode(" ", microtime());
	//return ((float)$usec + (float)$sec);
	return ((float)$sec);
}

function htmlDecode($str){
	$data = htmlspecialchars_decode($str, ENT_QUOTES);
	return $data;
}

//search delay
define('SESSION_SEARCH_DELAY', '10'); // session_expire_time in seconds, 1200 = 60*20 = 20 mins

if (microtime_float() - $_SESSION['search_Time'] > SESSION_SEARCH_DELAY){
	// remove all the session variables 
	$_SESSION['search_Time'] = '';
	$_SESSION['search_counter'] = 0;
}
?>