<?php


function getAllowReservations($ipAddress)
{
	$strippedIpAddress = str_replace(strrchr($ipAddress,'.'),"",$ipAddress) . '.';
	$query3 = "SELECT wacip FROM calendarconfig where wacip like '".$strippedIpAddress."%'";
	$check3 = mysql_query($query3)or die(mysql_error());
	$check4 = mysql_num_rows($check3);

	mysql_free_result($check3);

	return $check4;
}


function stringToDate($input, $failsafedate, $error_msg)
{
	if(!preg_match("/[0-9]*-[0-9]*-[0-9]*/",$input))
	{
		echo $error_msg . ": " . $input;
		return $failsafedate;
	}

	$a = strpos($input,"-",0);
	$b = strpos($input,"-",$a+1);

	$year = substr($input,$b+1,4);
	$month = substr($input,0,$a);
	$day = substr($input,$a+1,$b-($a+1));


    return mktime(0,0,0,$month,$day,$year);
}

function parseInput($input)
{
	$input = str_replace("<", "", $input);
	$input = str_replace(">", "", $input);
	$input = str_replace("'", "", $input);
	$input = str_replace("\"", "", $input);
	$input = str_replace("--", "", $input);
	$input = str_replace("<", "", $input);
	return $input;
}

function datetime($datetime)
{
    $year = substr($datetime,0,4);
    $month = substr($datetime,5,2);
    $day = substr($datetime,8,2);
    $hour = substr($datetime,11,2);
    $min = substr($datetime,14,2);
    $sec = substr($datetime,17,2);

    return mktime($hour,$min,$sec,$month,$day,$year);
}


// the following function get_holiday() is based on the work done by
// Marcos J. Montes: http://www.smart.net/~mmontes/ushols.html
//
// if $week is not passed in, then we are checking for the last week of the month
function get_holiday($year, $month, $day_of_week, $week="") {
    if ( (($week != "") && (($week > 5) || ($week < 1))) || ($day_of_week > 6) || ($day_of_week < 0) ) {
        // $day_of_week must be between 0 and 6 (Sun=0, ... Sat=6); $week must be between 1 and 5
        return FALSE;
    } else {
        if (!$week || ($week == "")) {
            $lastday = date("t", mktime(0,0,0,$month,1,$year));
            $temp = (date("w",mktime(0,0,0,$month,$lastday,$year)) - $day_of_week) % 7;
        } else {
            $temp = ($day_of_week - date("w",mktime(0,0,0,$month,1,$year))) % 7;
        }

        if ($temp < 0) {
            $temp += 7;
        }

        if (!$week || ($week == "")) {
            $day = $lastday - $temp;
        } else {
            $day = (7 * $week) - 6 + $temp;
        }

        return mktime(0,0,0,$month, $day, $year);
    }
}


function isHoliday($date)
{
	$y = date("Y",$date);
	$m = date("m",$date);
	$d = date("d",$date);
	$ddate = mktime(0,0,0,"$m","$d","$y");
	$isAHoliday=0;

	if($ddate == mktime(0,0,0,1,1,"$y")) //new years
		$isAHoliday=1;
	else if($ddate == mktime(0,0,0,2,2,"$y")) //groundhog day
		$isAHoliday=1;
	else if($ddate == mktime(0,0,0,2,14,"$y")) //valentine's
		$isAHoliday=1;
	else if($ddate == mktime(0,0,0,3,17,"$y")) //st. patrick's
		$isAHoliday=1;
	else if($ddate == mktime(0,0,0,5,5,"$y")) //cinco de mayo
		$isAHoliday=1;
	else if($ddate == mktime(0,0,0,7,4,"$y")) //independence day
		$isAHoliday=1;
	else if($ddate == mktime(0,0,0,10,31,"$y")) //halloween
		$isAHoliday=1;
	else if($ddate == mktime(0,0,0,12,25,"$y")) //christmas
		$isAHoliday=1;
	else if($ddate == mktime(0,0,0,11,11,"$y")) //veterans day
		$isAHoliday=1;

	else if($ddate == get_holiday($y, 1, 1, 3)) //mlk day
		$isAHoliday=1;
	else if($ddate == get_holiday($y, 2, 1, 3)) //president's day
		$isAHoliday=1;
	else if($ddate == get_holiday($y, 5, 1)) //memorial day
		$isAHoliday=1;
	else if($ddate == get_holiday($y, 9, 1, 1)) //labor day
		$isAHoliday=1;
	else if($ddate == get_holiday($y, 10, 1, 2)) //columbus day
		$isAHoliday=1;
	else if($ddate == get_holiday($y, 11, 4, 4)) //thanksgiving
		$isAHoliday=1;
	else if($ddate == get_holiday($y, 5, 0, 2)) //mother's day
		$isAHoliday=1;
	else if($ddate == get_holiday($y, 6, 0, 3)) //father's day
		$isAHoliday=1;

	else if($ddate == easter_date($y)) //easter
		$isAHoliday=1;

	return $isAHoliday;
}

function countHolidays($datefrom, $dateto)
{
	$count=0;
	for($i=$datefrom;$i<=$dateto;$i=mktime(0,0,0,date("m",$i),date("d",$i)+1,date("Y",$i)))
	{
		if(isHoliday($i)==1)
		{
			$count=$count+1;
		}
	}
	return $count;
}


?>
