<?php
function my_magic_quotes($var) {
   foreach ($var AS $key => $value) {
       if (is_array($value))
           $var[$key] = my_magic_quotes($value);
       else
           $var[$key] = addslashes($value);
	}
   return $var;
}

function redirect($strUrl) {
	if (!empty($strUrl)) {
		closedb();
		header("Location:" . $strUrl);
		exit;
	}
}
function redirects($strUrl) {
	if (!empty($strUrl)) {
		closedb();
		header("Location:" . $strUrl);
		exit;
	}
}
function setErr($strErrMsg) {
	$_SESSION["errorMessage"] = $strErrMsg;
}

function getErr($intColspan) {
	if (!empty($_SESSION["errorMessage"])) {
		echo("<tr><td align=\"center\" colspan=\"" . $intColspan ."\" class=\"err\">" . $_SESSION["errorMessage"] . "</td></tr>");
		$_SESSION["errorMessage"] = "";
	}
}

function showErr() {
	echo($_SESSION["errorMessage"]);
	$_SESSION["errorMessage"] = "";
}
function setusererr($strErrMsg) {
	$_SESSION["setusererr"] = $strErrMsg;
}
function showusererr() {
	echo("<br><font color=red size=2><B>".$_SESSION["setusererr"]);
	$_SESSION["setusererr"] = "";
}
function gotoerror($strErrMsg) {
	setErr($strErrMsg);
	redirect("error.php");
}

function selfx($strComp1, $strComp2) {
	if (!strcasecmp($strComp1, $strComp2))
		return(" selected");
}

function chkfx($strChkValue) {
	if (trim($strChkValue) != "")
		return (1);
	else
		return (0);
}

function cbfx($strCbValue) {
	if (trim($strCbValue) == "1")
		echo(" checked");
	elseif (trim($strCbValue) == "true")
		echo(" checked");
	elseif (trim($strCbValue) == "True")
		echo(" checked");
}


function getfx($key) {
	if (get_magic_quotes_gpc())
		return stripslashes($_GET[$key]);
	else
		return $_GET[$key];
}

function postfx($key) {
	if (get_magic_quotes_gpc())
		return stripslashes($_POST[$key]);
	else
		return $_POST[$key];
}

function dbfx($strVal) {
	return addslashes(trim($strVal));
}

function pdbfx($strVal) {
	if (get_magic_quotes_gpc())
		return trim($_POST[$strVal]);
	else
		return addslashes(trim($_POST[$strVal]));
}

function gdbfx($strVal) {
	if (get_magic_quotes_gpc())
		return trim($_GET[$strVal]);
	else
		return addslashes(trim($_GET[$strVal]));
}


function stsfx($strStatusValue) {
	//echo $strStatusValue;
	//die;
	if (trim($strStatusValue) == 1)
		echo("<img src=\"../images/icons/+.gif\" border=\"0\">");
	elseif (trim($strStatusValue) == 2)
		echo("<img src=\"../images/icons/+.gif\" border=\"0\">");
	else
		echo("<img src=\"../images/icons/-.gif\" border=\"0\">");
}
function stsfx2($strStatusValue) {
	//echo $strStatusValue;
	//die;
	if (trim($strStatusValue) == 1)
		echo("<img src=\"images/icons/+.gif\" border=\"0\">");
	elseif (trim($strStatusValue) == 2)
		echo("<img src=\"images/icons/+.gif\" border=\"0\">");
	else
		echo("<img src=\"images/icons/-.gif\" border=\"0\">");
}
function stsfx1($strStatusValue, $strTrue) {
	if (cmpfx($strStatusValue, $strTrue))
		echo("<img src=\"../images/icons/-.gif\" border=\"0\">");
	else
		echo("<img src=\"../images/icons/+.gif\" border=\"0\">");
}

function cmpfx($str1, $str2) {
	if (!strcasecmp($str1, $str2))
		return (1);
	else
		return (0);
}

function sendmail($strTo, $strFrom, $strCC, $strBCC, $strSubject, $strBody, $htmlMail) {
	$strHeaders = "";
	
	if ($htmlMail) {
		$strHeaders .= "MIME-Version: 1.0\r\n";
		$strHeaders .= "Content-type: text/html; charset=iso-8859-1\r\n";
	}
	
	if (strcmp($strTo, "") != 0)
		$strHeaders .= "To: " . $strTo . "\r\n";
	
	if (strcmp($strFrom, "") != 0)
		$strHeaders .= "From: " . $strFrom . "\r\n";
	
	if (strcmp($strCC, "") != 0) {
		$strHeaders .= "Cc: " . $strCC . "\r\n";
		if (strcmp($strTo, "") != 0)
			$strTo .= "," . $strCC;
		else
			$strTo .= $strCC;
	}
	
	if (strcmp($strBCC, "") != 0) {
		$strHeaders .= "Bcc: " . $strBCC . "\r\n";
		if (strcmp($strBcc, "") != 0)
			$strTo .= "," . $strBcc;
		else
			$strTo .= $strBcc;
	}
	
	return @mail($strTo, $strSubject, $strBody, $strHeaders);
}

function clearsql() {
	global $_where;
	$_where = "";
}

function appsql($strCond) {
	global $_where;
	if (!empty($strCond) && strcmp($strCond, "") != 0) {
		if (!empty($_where) && strcmp($_where, "") != 0)
			$_where .= " AND " . $strCond;
		else
			$_where = " WHERE " . $strCond;
	}
}

function getsql() {
	global $_where;
	return ($_where);
}

function iff($prmStr, $strTrue, $strFalse) {
	if ($prmStr)
		return $strTrue;
	else
		return $strFalse;
}

function cint($str) {
	return (round($str, 0));
}

function cdbl($str) {
	return (round($str, 2));
}


class paging {
	var $ps;			//Pagesize
	var $pss;			//Page scroll size
	
	var $pg;			//Current page
	var $tr;			//Total rows
	var $tp;			//Total pages
	var $rec;			//Start row for limit
	var $cps;
	
	var $sp;			//Start page
	var $ep;			//End page
	
	var $tps;			//Total page sets
	var $pps;			//Previous page set
	var $nps;			//Next page set
	
	var $i;				//Counter
	
	function paging() {
		$this->pss = 5;
		
		if (!empty($_GET["ps"]))
			$this->ps = (int) $_GET["ps"];
		else
			$this->ps = 35;
		
		if (empty($this->ps))
			$this->ps = 35;
		
		if (!empty($_GET["pg"]))
			$this->pg = (int) $_GET["pg"];
		else
			$this->pg = 1;
		
		if (empty($this->pg))
			$this->pg = 1;
		
		$this->i = 1;
	}
	
	function setpages($sql) {
		$rs = mysql_fetch_row(mysql_query($sql));
		$this->tr = $rs[0];
		
		//$this->tp = round($this->tr/$this->ps, 0)+1;
		$this->tp = round($this->tr/$this->ps, 0);
		
		if ($this->tp < 1) $this->tp = 1;
		
		if ($this->tr > $this->ps)
			if (($this->tr%$this->ps) != 0)
				$this->tp++;
		//echo $this->tr%$this->ps;
		if (!empty($this->pg)) {
			if ($this->pg > $this->tp)
				$this->pg = $this->tp;
		}
		else
			$this->pg = 1;
		
		$this->rec = ($this->pg*$this->ps)-$this->ps;
	}
	
	function getlimit() {
		return " limit " . $this->rec . ", " . $this->ps;
	}
	function showPagingw($url, $qs) {
		$this->cps = cint( ($this->pg / $this->pss) + .49 );
		$this->tps = ((cint( ($this->tp / $this->pss) + .49 )-1)*$this->pss)+1;
		$this->sp = (($this->cps-1)*$this->pss)+1;
		$this->ep = $this->sp + 5;
		
		if ($this->ep > $this->tp)
			$this->ep = $this->tp;
		
		$this->pps = $this->sp - 5;
		$this->nps = $this->sp + 5;
		
		echo "<span class=\"paging\">";
		
		//if ($this->pg == 1)
			//echo "&lt;&lt; &lt;";
		//else {
			//if ($this->pps > 0)
			//	echo "<a href=\"" . $url . "?pg=" . ($this->pps+4) . "&" . $qs . "\" class=botlinks>&lt;&lt;</a>";
			//else
			//	echo "&lt;&lt;";
			//echo "&nbsp;<a href=\"" . $url . "?pg=" . ($this->pg-1) . "&" . $qs . "\" //class=botlinks>&lt;</a>";
		//}
		
		echo " &nbsp; ";
		//$this->ep=($this->ep;
		for ($this->i=$this->sp; $this->i<=$this->ep; $this->i++) {
			if ($this->i == $this->pg)
				echo "[" . $this->i . "]";
			else
				echo "<a href=\"" . $url . "?pg=" . $this->i . "&" . $qs . "\" class=botlinks>" . $this->i . "</a>";
			echo "&nbsp;";
		}
		
		echo " &nbsp; ";
		
		//if ($this->pg == $this->tp)
			//echo "&gt; &gt;&gt;";
		//else {
			//echo "<a href=\"" . $url . "?pg=" . ($this->pg+1) . "&" . $qs . "\" class=botlinks>&gt;</a> ";
			//if ($this->nps <= $this->tps)
			//	echo "<a href=\"" . $url . "?pg=" . $this->nps . "&" . $qs . "\" class=botlinks>&gt;&gt;</a>";
			//else
			//	echo "&gt;&gt;";
		//}
	}
	function showPaging($url, $qs) {
		$this->cps = cint( ($this->pg / $this->pss) + .49 );
		$this->tps = ((cint( ($this->tp / $this->pss) + .49 )-1)*$this->pss)+1;
		$this->sp = (($this->cps-1)*$this->pss)+1;
		$this->ep = $this->sp + 4;
		if ($this->ep > $this->tp)
			$this->ep = $this->tp;
		$this->pps = $this->sp - 5;
		$this->nps = $this->sp + 5;
		
		echo "<span class=\"paging\">";
		
		if ($this->pg == 1)
			echo "&lt;&lt; &lt;";
		else {
			if ($this->pps > 0)
				echo "<a href=\"" . $url . "?pg=" . ($this->pps+4) . "&" . $qs . "\">&lt;&lt;</a>";
			else
				echo "&lt;&lt;";
			echo "&nbsp;<a href=\"" . $url . "?pg=" . ($this->pg-1) . "&" . $qs . "\">&lt;</a>";
		}
		
		echo " &nbsp; ";
		
		for ($this->i=$this->sp; $this->i<=$this->ep; $this->i++) {
			if ($this->i == $this->pg)
				echo "[" . $this->i . "]";
			else
				echo "<a href=\"" . $url . "?pg=" . $this->i . "&" . $qs . "\">" . $this->i . "</a>";
			echo "&nbsp;";
		}
		
		echo " &nbsp; ";
		
		if ($this->pg == $this->tp)
			echo "&gt; &gt;&gt;";
		else {
			echo "<a href=\"" . $url . "?pg=" . ($this->pg+1) . "&" . $qs . "\">&gt;</a> ";
			if ($this->nps <= $this->tps)
				echo "<a href=\"" . $url . "?pg=" . $this->nps . "&" . $qs . "\">&gt;&gt;</a>";
			else
				echo "&gt;&gt;";
		}
	}
}

function alert($str) {
	echo "<script language=\"javascript\" type=\"text/javascript\">" . chr(13);
	echo "	alert(\"" . $str . "\");" . chr(13);
	echo "</script>";
}


function getqs($nm) {
	$strqs = "";
	foreach ($_GET as $key=>$val) {
		if (!strpos("," . $nm . ",", $key))
			if ($strqs == "") {
				if (is_array($val))
					$strqs = $key . "[]=" . implode("&" . $key . "[]=", $val);
				else
					$strqs = $key . "=" . $val;
			}
			else {
				if (is_array($val))
					$strqs .= "&" . $key . "[]=" . implode("&" . $key . "[]=", $val);
				else
					$strqs .= "&" . $key . "=" . $val;
			}
	}
	return $strqs;
}
?>
<?php

function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
  /*
    $interval can be:
    yyyy - Number of full years
    q - Number of full quarters
    m - Number of full months
    y - Difference between day numbers
      (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
    d - Number of full days
    w - Number of full weekdays
    ww - Number of full weeks
    h - Number of full hours
    n - Number of full minutes
    s - Number of full seconds (default)
  */
  
  if (!$using_timestamps) {
    $datefrom = strtotime($datefrom, 0);
    $dateto = strtotime($dateto, 0);
  }
  $difference = $dateto - $datefrom; // Difference in seconds
   
  switch($interval) {
   
    case 'yyyy': // Number of full years

      $years_difference = floor($difference / 31536000);
      if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
        $years_difference--;
      }
      if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
        $years_difference++;
      }
      $datediff = $years_difference;
      break;

    case "q": // Number of full quarters

      $quarters_difference = floor($difference / 8035200);
      while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
        $months_difference++;
      }
      $quarters_difference--;
      $datediff = $quarters_difference;
      break;

    case "m": // Number of full months

      $months_difference = floor($difference / 2678400);
      while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
        $months_difference++;
      }
      $months_difference--;
      $datediff = $months_difference;
      break;

    case 'y': // Difference between day numbers

      $datediff = date("z", $dateto) - date("z", $datefrom);
      break;

    case "d": // Number of full days

      $datediff = floor($difference / 86400);
      break;

    case "w": // Number of full weekdays

      $days_difference = floor($difference / 86400);
      $weeks_difference = floor($days_difference / 7); // Complete weeks
      $first_day = date("w", $datefrom);
      $days_remainder = floor($days_difference % 7);
      $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
      if ($odd_days > 7) { // Sunday
        $days_remainder--;
      }
      if ($odd_days > 6) { // Saturday
        $days_remainder--;
      }
      $datediff = ($weeks_difference * 5) + $days_remainder;
      break;

    case "ww": // Number of full weeks

      $datediff = floor($difference / 604800);
      break;

    case "h": // Number of full hours

      $datediff = floor($difference / 3600);
      break;

    case "n": // Number of full minutes

      $datediff = floor($difference / 60);
      break;

    default: // Number of full seconds (default)

      $datediff = $difference;
      break;
  }    

  return $datediff;

}

function str_contains($haystack, $needle, $ignoreCase = false) {
   if ($ignoreCase) {
       $haystack = strtolower($haystack);
       $needle  = strtolower($needle);
   }
   $needlePos = strpos($haystack, $needle);
   return ($needlePos === false ? false : ($needlePos+1));
}
function encrypt($string, $key) {
   $result = '';
   for($i=0; $i<strlen($string); $i++) {
     $char = substr($string, $i, 1);
     $keychar = substr($key, ($i % strlen($key))-1, 1);
     $char = chr(ord($char)+ord($keychar));
     $result.=$char;
   }

   return base64_encode($result);
  }

  function decrypt($string, $key) {
   $result = '';
   $string = base64_decode($string);

   for($i=0; $i<strlen($string); $i++) {
     $char = substr($string, $i, 1);
     $keychar = substr($key, ($i % strlen($key))-1, 1);
     $char = chr(ord($char)-ord($keychar));
     $result.=$char;
   }

   return $result;
  }

function categorycombo($defaultcat = 0) {
	
	$sql = "SELECT * from category_master  order by category_name asc";
	$cmdcat = mysql_query($sql);
	
	while ($rscat = mysql_fetch_array($cmdcat))
		{
		
	
			if ($rscat["category_id"]==$defaultcat)
				$strcatp.= "<option value=".$rscat["category_id"]." selected >".$rscat["category_name"]."</option>";
			else
				$strcatp.= "<option value=".$rscat["category_id"].">".$rscat["category_name"]."</option>";
		}
		
		return $strcatp;
}
function genpasswordalpha($length){ 

    srand((double)microtime()*1000000); 
      
    $vowels = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p","q", "r", "s", "t", "u", "v", "w", "x", "y", "z"); 
    //$cons = array("1", "2", "3", "4", "5", "6", "7", "8", "9"); 
      
    $num_vowels = count($vowels); 
    $num_cons = count($cons); 
      
    for($i = 0; $i < $length; $i++){ 
        $password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)]; 
    } 
      
    return strtoupper(substr($password, 0, $length)); 
} 
function genpasswordnum($length){ 

    srand((double)microtime()*1000000); 
      
    $vowels = array("1", "2", "3", "4", "5", "6", "7", "8", "9"); 
    $cons = array("1", "2", "3", "4", "5", "6", "7", "8", "9"); 
      
    $num_vowels = count($vowels); 
    $num_cons = count($cons); 
      
    for($i = 0; $i < $length; $i++){ 
        $password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)]; 
    } 
      
    return substr($password, 0, $length); 
} 
function baseurl($haystack) {
		
	   $haystack1=$haystack;
	   $haystack=str_replace("http://","",$haystack);
       $haystack = strtolower($haystack);
	   $haystack=split("/",$haystack);
		for ($i = 0; $i<count($haystack)-1; $i++)
		{
			$str.=$haystack[$i]."/";
		}
		$str="http://".$str;
		
	   if (str_contains($haystack[count($haystack)-1],".")) 
			$haystack=$str;
		else
			$haystack=$haystack1;
		if ($haystack=="http://")
				$haystack=$haystack1;
		$haystack1='';
		
		
	   return $haystack;

}
function showstatusbox() {
	echo "<table><tr><td>";
	echo "<table class=textbold><tr><td><table bgcolor=black></tr><td bgcolor=white height=15 width=30></td></tr></table></td><td>".LanguageText($GLOBALS["site"]["Available"])."</td></tr></table>";
	echo "<table class=textbold><tr><td><table bgcolor=black></tr><td bgcolor=red height=15 width=30></td></tr></table></td><td>".LanguageText($GLOBALS["site"]["Confirmed"])."</td></tr></table>";
	echo "<table class=textbold><tr><td><table bgcolor=black></tr><td bgcolor=yellow height=15 width=30></td></tr></table></td><td>".LanguageText($GLOBALS["site"]["UnConfirmed"])."</td></tr></table>";
	echo "</td></tr></table>";
}
function showviewtype() {
	if($_GET["yearview"]==0 || $_GET["yearview"]=='')
		echo "<img src=images/icons/view.gif><a href='index.php?yearview=1' class=links>".LanguageText($GLOBALS["site"]["Show6MonthView"])."</a>";
	if($_GET["yearview"]==1)
		echo "<img src=images/icons/view.gif><a href='index.php?yearview=0' class=links>".LanguageText($GLOBALS["site"]["show-single-month-view"])."</a>";
	echo "<br><br><a href='BookingRates.php' class=links>".LanguageText($GLOBALS["site"]["booking-rates"])."</a>";
	
}
function addzero($str) {
	if (strlen($str)==1)
		$str="0".$str;
	return $str;
}
function daycombo($selected=0,$selectd2=0) {
	if ($selectd2!=0)
		$selected=$selectd2;
	for ($i = 1; $i<32; $i++)
		{
			if ($i==$selected)
				$str.="<option value=".$i." selected>".$i."</option>";	
			else
				$str.="<option value=".$i.">".$i."</option>";

		}
		return $str;
}
function monthcombo($selected=0,$selectd2=0) {
	if ($selectd2!=0)
		$selected=$selectd2;
for ($z=1;$z<=12;$z++)
	{
		if ($z==$selected) $str.="<option value=\"".$z."\" selected=\"selected\">".LanguageText($GLOBALS["site"][getMonthName($z)])."</option>\n";
		else $str.="<option value=\"".$z."\">".LanguageText($GLOBALS["site"][getMonthName($z)])."</option>\n";
	}
	return $str;
}

function yearcombo($selected=0,$startyear=2002,$endyear=2010,$selected2=0) {
	if ($selectd2!=0)
		$selected=$selectd2;
for ($z=$startyear;$z<=$endyear;$z++){
		if ($z==$selected) $str.="<option value=\"".$z."\" selected=\"selected\">".$z."</option>\n";
			else $str.="<option value=\"".$z."\">".$z."</option>\n";
		}
	return $str;
}
function getMonthName($var=false){
	
	switch($var){
		case 1: return "January";
		case 2: return "February";
		case 3: return "March";
		case 4: return "April";
		case 5: return "May";
		case 6: return "June";
		case 7: return "July";
		case 8: return "August";
		case 9: return "September";
		case 10: return "October";
		case 11: return "November";
		case 12: return "December";
	}
}
function timecombo($default='') {
	if ($default!='')
		$str.="<option value=\"".$default."\">".$default."</option>\n";
for ($z=0;$z<=11;$z++)
	{
		$str.="<option value=\"".addzero($z).":00 AM\">".addzero($z).":00 AM</option>\n";
		$str.="<option value=\"".addzero($z).":30 AM\">".addzero($z).":30 AM</option>\n";
	}
for ($z=0;$z<=11;$z++)
	{
		$str.="<option value=\"".addzero($z).":00 PM\">".addzero($z).":00 PM</option>\n";
		$str.="<option value=\"".addzero($z).":30 PM\">".addzero($z).":30 PM</option>\n";
	}
	return $str;
}
function GetRecortarTexto ($vTxt, $Car) {
  if (strlen($vTxt) > $Car) {
       return substr($vTxt, 0, $Car) . "...";
  } else return $vTxt;
}
function GetRecortarTextoNew ($vTxt, $Car) {
  if (strlen($vTxt) > $Car) {
       return substr($vTxt, 0, $Car);
  } else return $vTxt;
}
function LanguageText($str) {
	if ($str!='')
		{
			$str=split("~",$str);
			return ($str[$_SESSION["language"]-1]) ;
		}
}
function PrepareName($s)
	{
	$abc = "1234567890abcdefghijklmnopqrstuvwxyz";
	$s = strtolower($s);
	$r = "";
	for($i=0;$i<strlen($s);$i++)
		{
		if (strpos($abc,$s[$i])!==false)
			$r .= $s[$i];
		if (($s[$i]==' ')&&($r[strlen($r)-1]!=' '))
			$r .= ' ';
		}		
	$r = trim($r);
	return str_replace(' ','-',$r);
	}
function mk_week_to_dates($week, $year){
       //We start some time into prev year
       $searchdate = mktime(0,0,0,12,20,$year-1);
       //Then we advance $week-1 weeks ahead (no need to search through dates we know won't give results)
       $searchdate = strtotime("+".($week-1)." week",$searchdate);

       $found=false;
       while ($found==false){
               if (date("W",$searchdate) == $week)
                       $found = true;
               else
                       $searchdate = strtotime("+1 day",$searchdate);
       }

       $weekdates['firstday'] = $searchdate;

       $weekdates['lastday'] = strtotime("+6 day",$searchdate);
       ////return $weekdates;
	   return vbdate("m-d-Y", $weekdates['firstday'])." TO ".vbdate("m-d-Y", $weekdates['lastday']);
} 
function vbdate($format, $timestamp = TIMENOW, $doyestoday = false, $locale = true, $adjust = true, $gmdate = false)
{
	global $bbuserinfo, $vboptions, $vbphrase;

	$hourdiff = $vboptions['hourdiff'];

	if ($bbuserinfo['lang_locale'] AND $locale)
	{
		if ($gmdate)
		{
			$datefunc = 'gmstrftime';
		}
		else
		{
			$datefunc = 'strftime';
		}
	}
	else
	{
		if ($gmdate)
		{
			$datefunc = 'gmdate';
		}
		else
		{
			$datefunc = 'date';
		}
	}
	if (!$adjust)
	{
		$hourdiff = 0;
	}
	$timestamp_adjusted = max(0, $timestamp - $hourdiff);

	if ($format == $vboptions['dateformat'] AND $doyestoday AND $vboptions['yestoday'])
	{
		if ($vboptions['yestoday'] == 1)
		{
			if (!defined('TODAYDATE'))
			{
				define ('TODAYDATE', vbdate('n-j-Y', TIMENOW, false, false));
				define ('YESTDATE', vbdate('n-j-Y', TIMENOW - 86400, false, false));
				define ('TOMDATE', vbdate('n-j-Y', TIMENOW + 86400, false, false));
	   		}

			$datetest = @date('n-j-Y', $timestamp - $hourdiff);

			if ($datetest == TODAYDATE)
			{
				$returndate = $vbphrase['today'];
			}
			else if ($datetest == YESTDATE)
			{
				$returndate = $vbphrase['yesterday'];
			}
			else
			{
				$returndate = $datefunc($format, $timestamp_adjusted);
			}
		}
		else
		{
			$timediff = TIMENOW - $timestamp;

			if ($timediff < 3600)
			{
				if ($timediff < 120)
				{
					$returndate = $vbphrase['1_minute_ago'];
				}
				else
				{
					$returndate = construct_phrase($vbphrase['x_minutes_ago'], intval($timediff / 60));
				}
			}
			else if ($timediff < 7200)
			{
				$returndate = $vbphrase['1_hour_ago'];
			}
			else if ($timediff < 86400)
			{
				$returndate = construct_phrase($vbphrase['x_hours_ago'], intval($timediff / 3600));
			}
			else if ($timediff < 172800)
			{
				$returndate = $vbphrase['1_day_ago'];
			}
			else if ($timediff < 604800)
			{
				$returndate = construct_phrase($vbphrase['x_days_ago'], intval($timediff / 86400));
			}
			else if ($timediff < 1209600)
			{
				$returndate = $vbphrase['1_week_ago'];
			}
			else if ($timediff < 3024000)
			{
				$returndate = construct_phrase($vbphrase['x_weeks_ago'], intval($timediff / 604900));
			}
			else
			{
				$returndate = $datefunc($format, $timestamp_adjusted);
			}
		}

		return $returndate;
	}
	else
	{
		return $datefunc($format, $timestamp_adjusted);
	}
}
function checkleapyear($year){

   //$year must be YYYY
   //[gregorian] leap year math :
   
   if ($year < 0) $year++;
   $year += 4800;

   if ( ($year % 4) == 0) {
       if (($year % 100) == 0) {
           if (($year % 400) == 0) {
               return 1;
           } else {
               return 0;
           }
       } else {
           return 1;
       }
   } else {
       return 0;
   }
}

function dir_exists($dir_name = false, $path = './') {
   if(!$dir_name) return false;
   
   if(is_dir($path.$dir_name)) return true;
   
   $tree = glob($path.'*', GLOB_ONLYDIR);
   if($tree && count($tree)>0) {
       foreach($tree as $dir)
           if(dir_exists($dir_name, $dir.'/'))
               return true;
   }
   
   return false;
}
function categorypath($cat)
{
	$ret="";
	
	while($cat>0)
	{
		$sql="select category_id,category_name,category_parent_id from category_master where category_id=$cat ";
		$cmd = mysql_query($sql);
		
		$rs = mysql_fetch_array($cmd);
		if($rs!=0)
		{
		$catnameis = $rs[1];
						$catnameis = str_replace("/","-",$catnameis);
						$catnameis = str_replace(" ","-",$catnameis);
						$catnameis = str_replace("?","-",$catnameis);
						$catnameis = str_replace(":","-",$catnameis);
		
			$ret=($cat==1?"":" --> ").$catnameis.$ret;
			$cat=$rs[2];
		}
		else
			$cat=0;
	}
	return $ret;
}

function categorypathlink($cat,$default=0)
{
	$ret="";
	
	while($cat>0)
	{
		$sql="select category_id,category_name,category_parent_id from category_master where category_id=$cat";
		$cmd = mysql_query($sql);
		
		$rs = mysql_fetch_array($cmd);
		if($rs!=0)
		{
		$catnameis = $rs[1];
						$catnameis = str_replace("/","-",$catnameis);
						$catnameis = str_replace(" ","-",$catnameis);
						$catnameis = str_replace("?","-",$catnameis);
						$catnameis = str_replace(":","-",$catnameis);
		
			
			if ($cat!=1)
					if ($default!=$cat)
						$ret=($cat==1?"":" » ")."<a href='category".$rs[0].".html' class=bodylinks>".$catnameis."</a>".$ret;
					else
						$ret=($cat==1?"":" » ").$catnameis.$ret;
			else
				$ret=($cat==1?"":" » ")."<a href='index.php' class=bodylinks>".$catnameis."</a>".$ret;
			$cat=$rs[2];
		}
		else
			$cat=0;
	}
	return $ret;
}
function categorycount($cat,$ifroot)
{
	$sql="select count(*) from product_master where product_category_id=$cat";
	$cmd = mysql_query($sql);
	$rs = mysql_fetch_array($cmd);
	if ($ifroot==1) {
			$sql="select category_id from category_master where category_parent_id=".$cat;
			$cmdchildcat = mysql_query($sql);
			while ($rschildcat = mysql_fetch_array($cmdchildcat)) {
				if ($child=='')
					$child=$rschildcat["category_id"];
				else
					$child.=",".$rschildcat["category_id"];
			}
		}
		
		if 	($child==''){
			$sql="select count(*) from product_master where product_category_id=$cat ";
			$cmd = mysql_query($sql);
			
		}
		else {
			$sql="select count(*) from product_master where (product_category_id=$cat OR product_category_id in ($child) )";
			
			$cmd = mysql_query($sql);
		}
	
	$rs = mysql_fetch_array($cmd);
	
	return $rs[0];
}
function generateCategoriesroot($cat,$lev,$default)
{

	echo "<option value=$cat ".(isset($default)&&$default==$cat?"selected":"").">";
	for($x=0;$x<$lev;$x++)
		echo "- ";
	$result=mysql_query("select category_name from category_master where category_sub_yes_no=1");
	$rscat = mysql_fetch_array($result);
	echo $rscat["category_name"];
	$result=mysql_query("select category_id from category_master where category_sub_yes_no=1 order by category_name");
	while ($rscat = mysql_fetch_array($result))
	{
		
		generateCategories($rscat["category_id"],$lev+1,$default);
	}

	mysql_free_result($result);
}
function generateCategories($cat,$lev,$default)
{
	if ($cat!=1)
		echo "<option value=$cat ".(isset($default)&&$default==$cat?"selected":"").">";
	for($x=0;$x<$lev;$x++)
		echo "- ";
	$result=mysql_query("select category_name from category_master where category_id=$cat");
	$rscat = mysql_fetch_array($result);
	echo $rscat["category_name"];
	$result=mysql_query("select category_id from category_master where category_parent_id=$cat order by category_name");
	while ($rscat = mysql_fetch_array($result))
	{
		
		generateCategories($rscat["category_id"],$lev+1,$default);
	}

	mysql_free_result($result);
}
function getstoneType($stone_name)
{

	
	$result=mysql_query("select stone_type from stone_master where stone_name='".$stone_name."'");
	$rsstone = mysql_fetch_array($result);
	return $rsstone[0];
}
function getcostDiamond($ring_number)
{
	
		$sql="Select P.price_value from price_master P,ring_master R ,metal_master M where R.ring_number=P.ring_number AND M.metal_id =P.metal_id AND M.metal_name='Diamond' AND R.ring_number='".$ring_number."'";
			$result=mysql_query($sql);
			$rsprice= mysql_fetch_array($result);
			
	if ($rsprice[0]!='')
		return $rsprice[0];
	if ($rsprice[0]=='NA')
		return "0";
	if ($rsprice[0]=='')
		return "0";
	
}
function getcost($ring_number,$metal_name)
{
	$sql="Select P.price_value from price_master P,ring_master R ,metal_master M where R.ring_number=P.ring_number AND M.metal_id =P.metal_id AND M.metal_name='".$metal_name."' AND R.ring_number='".$ring_number."'";
	$result=mysql_query($sql);
	$rsprice = mysql_fetch_array($result);
	
	
	
	
	if ($rsprice[0]!='')
		return $rsprice[0];
	if ($rsprice[0]=='NA')
		return "The Combination Of Metal & Ring is not available.";
	if ($rsprice[0]=='')
		return "<small>The Price information Not available Now.Try again later or Email US.</small>";
}
function getcostsimple($ring_number,$metal_name)
{
	$sql="Select P.price_value from price_master P,ring_master R ,metal_master M where R.ring_number=P.ring_number AND M.metal_id =P.metal_id AND M.metal_name='".$metal_name."' AND R.ring_number='".$ring_number."'";
	$result=mysql_query($sql);
	$rsprice = mysql_fetch_array($result);
	if ($rsprice[0]=='' || $rsprice[0]=='NA')
		return "";
	else
		return $rsprice[0];
	
}
function getringoptions($ring_number)
{
	$sql="Select max_characters  from ring_master R where R.ring_number='".$ring_number."'";
	$result=mysql_query($sql);
	$rsprice = mysql_fetch_array($result);
	
		return $rsprice[0];
	
}
function getengraving($ring_number)
{
	$sql="Select outside_engraving,inside_engraving  from ring_master R where R.ring_number='".$ring_number."'";
	$result=mysql_query($sql);
	$rsprice = mysql_fetch_array($result);
	
		return $rsprice[0].",".$rsprice[1];
	
}
?>
