<?

function drop_down($name, $options, $current, $class='', $function='') {
    $menu = '<select name="'.$name.'" id="'.$name.'" class="'.$class.'" onchange="'.$function.'">'."\n";
    if (is_array($options)) {
        foreach ($options as $option) {
            if ($option == $current) {
                $selected = 'selected';
            } else {
                $selected = '';
            }
            $menu .= '<option  value="'.$option.'" '.$selected.">".$option.'</option>'."\n";
        }
    }
    $menu .= '</select>'."\n";
    return $menu;
}

function cleanFileName($fileName) {
    $problemChars = array(" ", "/", "\\", "&", "'",
        '"', "�", "#", "%", "-", "+",
        "?", "=", "(", ")", "^", "$",
        "@", "!", "~", "`", "*", ":",
        ";", ">", "<", ",", "{", "}",
        "[", "]", "|");

    $cleanedFileName = $fileName;
    foreach ($problemChars as $char) {

        switch ($char) {
            case "&":
                $replaceChar = "and";
                break;
            case " ":
                $replaceChar = "_";
                break;
            default:
                $replaceChar = "";
        }

        $cleanedFileName = str_replace($char, $replaceChar, $cleanedFileName);
    }
    $cleanedFileName = str_replace('__', '_', $cleanedFileName);
    $cleanedFileName = ltrim($cleanedFileName, "_");
    $cleanedFileName = rtrim($cleanedFileName, "_");
    return strtolower($cleanedFileName);
}

function cleanSpecialChars($pageURL) {
    $problemChars = array(" ", ".", "/", "\\", "&", "'",
        '"', "�", "#", "%", "-", "+",
        "?", "=", "(", ")", "^", "$",
        "@", "!", "~", "`", "*", ":",
        ";", ">", "<", ",", "{", "}",
        "[", "]", "|");

    $cleanedPageURL = $pageURL;
    foreach ($problemChars as $char) {

        switch ($char) {
            case "&":
                $replaceChar = "and";
                break;
            case " ":
                $replaceChar = "_";
                break;
            default:
                $replaceChar = "";
        }

        $cleanedPageURL = str_replace($char, $replaceChar, $cleanedPageURL);
    }
    $cleanedPageURL = str_replace('__', '_', $cleanedPageURL);
    $cleanedPageURL = ltrim($cleanedPageURL, "_");
    $cleanedPageURL = rtrim($cleanedPageURL, "_");
    return strtolower($cleanedPageURL);
}

function clean($string) {
    return htmlentities(mysql_real_escape_string(stripslashes(trim($string))), ENT_COMPAT, 'UTF-8', true);
}

function cleanInput($string) {
    return trim(stripslashes($string));
}

function copyright($image, $overlay_path) {

    imagealphablending($image, true);

    $image_x = imagesx($image);
    $image_y = imagesy($image);
    //imagefilter($image, IMG_FILTER_GRAYSCALE);

    $overlay = imagecreatefrompng($overlay_path);
    imagealphablending($overlay, true);

    $overlay_x = imagesx($overlay);
    $overlay_y = imagesy($overlay);
    //imagecolortransparent($overlay, imagecolorat($overlay, 0, 0));

    imagecopymerge($image, $overlay, (($image_x - $overlay_x) / 2), (($image_y - $overlay_y) / 2), 0, 0, $overlay_x, $overlay_y, 25);

    //imagejpeg($image, $image_dir.'_'.$img_url, 90);

    imagedestroy($overlay);

    return $image;
}

function is_image($img_path) {
    $image_types = array("image/jpeg", "image/jpg", "image/pjpg", "image/pjpeg", "image/gif", "image/pgif", "image/png", "image/ppng");
    $image = getimagesize($img_path);
    if (in_array($image['mime'], $image_types)) {
        return true;
    } else {
        return false;
    }
}

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 safe($text) {
    $value = 'value="'.$text.'"';
    $pos = strpos($text, '"');
    if ($pos === false) {
        
    } else {
        $value = "value='".$text."'";
    }
    return $value;
}

function get_month($month, $day, $year) {
    $timestamp = mktime(0, 0, 0, $month, $day, $year);
    return date('F', $timestamp);
}

function get_day($month, $day, $year) {
    $timestamp = mktime(0, 0, 0, $month, $day, $year);
    return date('l', $timestamp);
}

# PHP Calendar (version 2.3), written by Keith Devens 
# http://keithdevens.com/software/php_calendar 
#  see example at http://keithdevens.com/weblog 
# License: http://keithdevens.com/software/license 

function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array()) {
    $first_of_month = gmmktime(0, 0, 0, $month, 1, $year);
    #remember that mktime will automatically correct if invalid dates are entered 
    # for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998 
    # this provides a built in "rounding" feature to generate_calendar() 

    $day_names = array(); #generate all the day names according to the current locale 
    for ($n = 0, $t = (3 + $first_day) * 86400; $n < 7; $n++, $t+=86400) #January 4, 1970 was a Sunday 
        $day_names[$n] = ucfirst(gmstrftime('%A', $t)); #%A means full textual day name 

    list($month, $year, $month_name, $weekday) = explode(',', gmstrftime('%m,%Y,%B,%w', $first_of_month));
    $weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day 
    $title = htmlentities(ucfirst($month_name)).'&nbsp;'.$year;  #note that some locales don't capitalize month and day names 
    #Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03 
    @list($p, $pl) = each($pn);
    @list($n, $nl) = each($pn); #previous and next links, if applicable 
    if ($p)
        $p = ($pl ? '<a href="'.htmlspecialchars($pl).'">'.$p.'</a>' : $p);
    if ($n)
        $n = ($nl ? '<a href="'.htmlspecialchars($nl).'">'.$n.'</a>' : $n);
    $calendar = '<table id="calendar_table" border="0" cellpadding="0" cellspacing="0" align="center">'."\n".
            '<tr><td id="calendar_prev">'.$p.'</td><td colspan="5" id="calendar_month">'.($month_href ? '<a href="'.htmlspecialchars($month_href).'">'.$title.'</a>' : $title).'</td><td id="calendar_next">'.$n."</td></tr>\n<tr>";

    if ($day_name_length) { #if the day names should be shown ($day_name_length > 0) 
        #if day_name_length is >3, the full name of the day will be printed 
        foreach ($day_names as $d)
        //$calendar .= '<th id="'.htmlentities($d).'">'.htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d).'</th>'; 
            $calendar .= '<td class="calendar_day_header">'.htmlentities($day_name_length < 4 ? substr($d, 0, $day_name_length) : $d).'</td>';
        $calendar .= "</tr>\n<tr>";
    }

    if ($weekday > 0)
        $calendar .= '<td colspan="'.$weekday.'">&nbsp;</td>';#initial 'empty' days 
    for ($day = 1, $days_in_month = gmdate('t', $first_of_month); $day <= $days_in_month; $day++, $weekday++) {
        if ($weekday == 7) {
            $weekday = 0; #start a new week 
            $calendar .= "</tr>\n<tr>";
        }
        if (isset($days[$day]) and is_array($days[$day])) {
            @list($link, $classes, $content) = $days[$day];
            if (is_null($content))
                $content = $day;
            $calendar .= '<td'.($classes ? ' id="'.htmlspecialchars($classes).'">' : '>').
                    ($link ? '<a href="'.htmlspecialchars($link).'">'.$content.'</a>' : $content).'</td>';
        }
        else
            $calendar .= '<td class="calendar_day">'.$day.'</td>';
    }
    if ($weekday != 7)
        $calendar .= '<td colspan="'.(7 - $weekday).'">&nbsp;</td>';#remaining "empty" days 

    return $calendar."</tr>\n</table>\n";
}

function pipe($array, $pipe='|') {
    $string = '';
    if (is_array($array)) {
        foreach ($array as $item) {
            $string .= $pipe.$item;
            $pipe = '|';
        }
        $string .= $pipe;
    }
    return $string;
}

function overlay($image_dir, $img_url, $overlay_path) {

    $image_path = $image_dir.$img_url;

    $image = imagecreatefromjpeg($image_path);
    $image_x = imagesx($image);
    $image_y = imagesy($image);
    imagefilter($image, IMG_FILTER_GRAYSCALE);

    $overlay = imagecreatefrompng($overlay_path);

    $overlay_x = imagesx($overlay);
    $overlay_y = imagesy($overlay);
    imagecolortransparent($overlay, imagecolorat($overlay, 0, 0));

    imagecopymerge($image, $overlay, (($image_x - $overlay_x) / 2), (($image_y - $overlay_y) / 2), 0, 0, $overlay_x, $overlay_y, 100);

    imagejpeg($image, $image_dir.'_'.$img_url, 90);
    imagedestroy($image);

    return $image_dir.'_'.$img_url;
}

function page_name() {
    return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);
}

function curPageName() {
    return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);
}

function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {
        $pageURL .= "s";
    }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

function cssPageLinks($total_pages, $current_page, $page_size, $parameter, $vars) {
    $page = 1;
    $record_start = 0;
    $page_links = '';

    while ($page <= $total_pages) {
        if ($page != $current_page) {
            $page_links .= '<a href="'.$_SERVER['PHP_SELF'].'?'.$parameter.'='.$record_start.$vars.'" target="_self" class="page_link">'.$page.'</a> ';
        } else {
            $page_links .= '<font class="page_link_active">'.$page.'</font> ';
        }
        $record_start += $page_size;
        $page++;
    }
    return $page_links;
}

function pageLinks($total_pages, $current_page, $page_size, $parameter, $vars) {
    $page = 1;
    $record_start = 0;
    $page_links = '';

    while ($page <= $total_pages) {
        if ($page != $current_page) {
            $page_links .= '<a href="'.$_SERVER['PHP_SELF'].'?'.$parameter.'='.$record_start.$vars.'" target="_self">'.$page.'</a> ';
        } else {
            $page_links .= $page.' ';
        }
        $record_start += $page_size;
        $page++;
    }
    return $page_links;
}

function ajaxPageLinks($total_pages, $current_page, $page_size, $variables) {
    $page = 1;
    $record_start = 0;
    $page_links = '';

    while ($page <= $total_pages) {
        if ($page != $current_page) {
            $page_links .= '<a href="javascript:fetchPage(\''.$record_start.$variables.'\');">'.$page.'</a> ';
        } else {
            $page_links .= '<strong>'.$page.'</strong> ';
        }
        $record_start += $page_size;
        $page++;
    }
    return $page_links;
}

function _is_valid_email($email = "") {
    return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $email);
}

function shorten($text, $limit) {
    $text = str_replace('</p>', ' ', str_replace('<p>', '', $text));
    $text = str_replace('<br/>', ' ', $text);
    $text = str_replace('<br>', ' ', $text);
    $text = str_replace('</strong>', '', str_replace('<strong>', '', $text));
    $text = str_replace('</b>', '', str_replace('<b>', '', $text));
    if (strlen($text) > $limit) {
        $text = substr($text, 0, strrpos(substr($text, 0, $limit), ' ')).'...';
    }
    return $text;
}

function cut($text, $limit) {
    if (strlen($text) > $limit) {
        $text = substr($text, 0, $limit).'...';
    }
    return $text;
}

function paragraph($text) {
    $count = 0;
    $text_array = split("[.]", $text);
    foreach ($text_array as $sentence) {
        if (strlen($sentence) > 10) {
            if ($count % 2 != 0) {
                $formatted_text .= ucfirst($sentence).".<br>"."\n";
                $count++;
            } else {
                $formatted_text .= ucfirst($sentence).".<br><br>"."\n";
                $count++;
            }
        }
    }
    return $formatted_text;
}

function unparagraph($text) {
    $text = rtrim(str_replace('</p>', '<br/><br/>', str_replace('<p>', '', $text)), '<br/><br/>');
    return $text;
}

function deleteFile($filename) {
    $file_deleted = false;
    if (file_exists($filename)) {
        unlink($filename);
        $file_deleted = true;
    }
    return $file_deleted;
}

function gen_drop_menu($menu_name, $option_list, $current_option, $function='') {
    $menu = '<select name="'.$menu_name.'" id="'.$menu_name.'" onchange="'.$function.'">'."\n";
    if (is_array($option_list)) {
        foreach ($option_list as $option) {
            if (str_replace(' ', '_', strtolower($option)) == $current_option) {
                $selected = 'selected';
            } else {
                $selected = '';
            }
            $menu .= '<option  value="'.str_replace(' ', '_', strtolower($option)).'" '.$selected.">".$option.'</option>'."\n";
        }
    }
    $menu .= '</select>'."\n";
    return $menu;
}

function gen_drop_menu_extra($menu_name, $select='Please Select', $option_list, $current_option, $function='') {
    $menu = '<select name="'.$menu_name.'" id="'.$menu_name.'" onchange="'.$function.'">'."\n";
    if (is_array($option_list)) {
        $menu .= '<option value="select" selected="selected">'.$select.'</option>';
        foreach ($option_list as $option) {
            if (str_replace(' ', '_', strtolower($option)) == $current_option) {
                $selected = 'selected';
            } else {
                $selected = '';
            }
            $menu .= '<option  value="'.str_replace(' ', '_', strtolower($option)).'" '.$selected.">".$option.'</option>'."\n";
        }
    }
    $menu .= '</select>'."\n";
    return $menu;
}

function integerDrop($menu_name, $select='', $option_list, $current_option, $function='') {
    $menu = '<select name="'.$menu_name.'" id="'.$menu_name.'" onchange="'.$function.'">'."\n";
    if (is_array($option_list)) {
        if ($select != '') {
            $menu .= '<option value="0" selected="selected">'.$select.'</option>';
        }
        foreach ($option_list as $option) {
            if (str_replace(' ', '_', strtolower($option)) == $current_option) {
                $selected = 'selected';
            } else {
                $selected = '';
            }
            $menu .= '<option  value="'.str_replace(' ', '_', strtolower($option)).'" '.$selected.">".$option.'</option>'."\n";
        }
    }
    $menu .= '</select>'."\n";
    return $menu;
}

?>
