<?
// mysql_time: Updated 31 may 2006
function mysql_time($hour, $minute,	$ampm) {
	if ($ampm == 'PM' && $hour != '12')	{
		$hour += 12;
	}
	if ($ampm == 'AM' && $hour == '12')	{
		$hour =	'00';
	}
	$mysql_time	= $hour	. ':' .	$minute	. ':00';
	return $mysql_time;
}


function price_format($price) {
	if ($price != '' &&	$price != '0') {
		$price = number_format($price, 2);
		if($price >="0") {
			return '$'.$price;
		}
		else{
			return str_replace("-","-$",$price);
		}
	}
}


function time_format($time) {
	if (strlen($time) >= 5)	{
		$hour =	substr($time, 0, 2);
		$hour =	str_pad($hour, 2, "0", STR_PAD_LEFT);

		return $hour . ':' . substr($time, 3, 2) . ' ' . $ampm;
	}else {
		return $s;
	}
}


function ms_print_r($var) {
	echo "<textarea rows='10' cols='148' style='font-size: 11px; font-family: tahoma'>";
	print_r($var);
	echo "</textarea>";
}


function ms_htmlentities($var) {
	return is_array($var) ? array_map('ms_htmlentities', $var) : htmlentities(trim($var),ENT_QUOTES);
}

function ms_htmlentities_decode($var) {
	return is_array($var) ? array_map('ms_htmlentities_decode', $var) : stripslashes(html_entity_decode(trim($var),ENT_QUOTES));
}


function ms_trim($var) {
	return is_array($var) ? array_map('ms_trim', $var) : trim($var);
}


// is_image_valid: Updated 31 may 2006
function is_image_valid($file_name) {
	global $ARR_VALID_IMG_EXTS;
	$ext = file_ext($file_name);
	if (in_array($ext, $ARR_VALID_IMG_EXTS))	{
		return true;
	}
	else {
		return false;
	}
}


// getmicrotime: Updated 31 may 2006
function getmicrotime() {
	list($usec,	$sec) =	explode(" ", microtime());
	return ((float)$usec + (float)$sec);
}


// file_ext: Updated 31 may 2006
function file_ext($file_name) {
	$path_parts = pathinfo($file_name);
	$ext = strtolower($path_parts["extension"]);
	return $ext;
}


// blank_filter: Updated 31 may 2006
function blank_filter($var) {
	$var = trim($var);
	return ($var != '' && $var != '&nbsp;');
}


// apply_filter: Updated 25 sep 2006
function apply_filter($sql,	$field,	$field_filter, $column) {
	if (!empty($field)) {
		if ($field_filter == "=" || $field_filter == "") {
			$sql = $sql	. "	and	$column	= '$field' ";
		}
		else if ($field_filter == "like") {
			$sql = $sql	. "	and	$column	like '%$field%'	";
		}
		else if ($field_filter ==	"starts_with") {
			$sql = $sql	. "	and	$column	like '$field%' ";
		}
		else if ($field_filter ==	"ends_with") {
			$sql = $sql	. "	and	$column	like '%$field' ";
		}
		else if ($field_filter ==	"not_contains") {
			$sql = $sql	. "	and	$column	not	like '%$field%'	";
		}
		else if ($field_filter == ">") {
			$sql = $sql . " and $column > '$field' ";
		}
		else if ($field_filter == "<") {
			$sql = $sql . " and $column < '$field' ";
		}
		else if ($field_filter ==	"!=") {
			$sql = $sql	. "	and	$column	!= '$field'	";
		}
	}
	return $sql;
}


// filter_dropdown: Updated 17 July 2006
// Should replace older version function
function filter_dropdown($name	= 'filter',	$sel_value) {
	$arr = array( "like" => 'Contains', '=' => 'Is Equal', "starts_with" => 'Starts with', "ends_with"	=> 'Ends with', "!=" => 'Is not' , "not_contains" => 'Not contains');
	return array_dropdown($arr, $sel_value, $name, " class='textfield'");
}


// move_up: Updated 31 may 2006
function move_up($table_name, $where_clause_all, $where_clause_item, $sort_order, $move_by) {
	$dest_order	= $sort_order -	$move_by;
	// $arr_ids_to_move=Array();
	// echo	"<br>$movie_artist_id, $movie_id, $artistcate_id, $sort_order, $move_by, $dest_order<br>";
	for($i = $sort_order-1;	$i > $dest_order-1;	$i--) {
		$sql = " update	$table_name	set	sort_order=sort_order+1	where $where_clause_all	and	sort_order='$i'";
		// echo	"<br>$sql<br>";
		db_query($sql);
	}
	$sql = " update	$table_name	set	sort_order=sort_order-$move_by where $where_clause_item";
	// echo	"<br>$sql<br>";
	db_query($sql);
}


// date_dropdown: Updated 31 may 2006
function date_dropdown($pre, $selected_date = '', $start_year='', $end_year = '', $sort = 'asc') {
	$cur_date =	date("Y-m-d");
	$cur_date_day =	substr($cur_date, 8, 2);
	$cur_date_month	= substr($cur_date,	5, 2);
	$cur_date_year = substr($cur_date, 0, 4);

	if ($selected_date != '') {
		$selected_date_day = substr($selected_date,	8, 2);
		$selected_date_month = substr($selected_date, 5, 2);
		$selected_date_year	= substr($selected_date, 0,	4);
	}
	$date_dropdown	.= month_dropdown($pre	. "month", $selected_date_month);
	$date_dropdown	.= day_dropdown($pre .	"day", $selected_date_day);
	// echo($pre . "year: ". $selected_date_year);
	$date_dropdown	.= year_dropdown($pre . "year", $selected_date_year, $start_year,	$end_year,	$sort);
	return $date_dropdown;
}


// month_dropdown: Updated 31 may 2006
function month_dropdown($name,	$selected_date_month = '', $extra='') {
	global $ARR_MONTHS;
	
	$date_dropdown	= "	<select	name='$name' $extra> <option value=''>MM</option>";
	$i = 0;
	foreach ($ARR_MONTHS as $key => $value) {
		$date_dropdown	.= " <option ";
		if ($key == $selected_date_month)	{
			$date_dropdown	.= " selected ";
		}
		$date_dropdown	.= " value='" .	str_pad($key, 2, "0",	STR_PAD_LEFT) .	"'>$value</option>";
	}
	$date_dropdown	.= "</select>";
	return $date_dropdown;
}


// day_dropdown: Updated 31 may 2006
function day_dropdown($name, $selected_date_day = '', $extra='') {
	$date_dropdown	.= "<select	name='$name' $extra>";
	$date_dropdown	.= "<option	value=''>DD</option>";
	for($i = 1;$i <= 31;$i++) {
		//$s = date('S', mktime(1, 0,	0, 3, $i, 1970));
		$date_dropdown	.= " <option ";
		if ($i == $selected_date_day) {
			$date_dropdown	.= " selected ";
		}
		$date_dropdown	.= " value='" .	str_pad($i,	2, "0",	STR_PAD_LEFT) .	"'>" . $i .	$s . "</option>";
	}
	$date_dropdown	.= "</select>";
	return $date_dropdown;
}


// year_dropdown: Updated 31 may 2006
function year_dropdown($name, $selected_date_year = '', $start_year =	'',	$end_year = '', $extra='') {
	if ($start_year	== '') {
		$start_year	= DEFAULT_START_YEAR;
	}
	
	if ($end_year == '') {
		$end_year =	DEFAULT_END_YEAR;
	}

	$date_dropdown	.= "<select	name='$name' $extra>";
	$date_dropdown	.= "<option	value=''>YYYY</option>";

	for($i = $start_year; $i <= $end_year; $i++) {
		$date_dropdown	.= " <option ";
		if ($i == $selected_date_year) {
			$date_dropdown	.= " selected ";
		}
		$date_dropdown	.= " value='" .	str_pad($i,	2, "0",	STR_PAD_LEFT) .	"'>" . str_pad($i, 2, "0", STR_PAD_LEFT) .	"</option>";
	}
	$date_dropdown	.= "</select>";
	return $date_dropdown;
}


// time_dropdown: Updated 31 may 2006
function time_dropdown($pre, $selected_time = '') {
	// echo("<br>selected_time:$selected_time");
	if ($selected_time != '' &&	$selected_time != ':') {
		$selected_hour = substr($selected_time,	0, 2);
		$selected_minute = substr($selected_time, 3, 2);		
	}
	$str .= hour_dropdown($pre, $selected_hour);
	$str .= '<b>:</b>';
	$str .= minute_dropdown($pre, $selected_minute);
	return $str;	
}


// hour_dropdown: Updated 31 may 2006
function hour_dropdown($pre, $selected_hour) {
	$str .= "<select	name='"	. $pre . "hour'>";
	$str .= "<option	value=''>Hour</option>";
	for($i = 0;	$i <= 23; $i++)	{
		$str .= " <option ";
		if ($i == $selected_hour &&	$selected_hour != '') {
			$str .= " selected ";
		}
		$str .= " value='" .	str_pad($i,	2, "0",	STR_PAD_LEFT) .	"'>" . str_pad($i, 2, "0", STR_PAD_LEFT) .	"</option>";
	}
	$str .= "</select>";
	return $str;
}


// minute_dropdown: Updated 31 may 2006
function minute_dropdown($pre, $selected_minute) {
	$str .= "<select	name='"	. $pre . "minute'>";
	$str .= "<option	value=''>Minute</option>";
	for($i = 0;	$i <= 59; $i = $i +	15)	{
		$str .= " <option ";
		if (str_pad($i,	2, "0",	STR_PAD_LEFT) === strval($selected_minute))	{
			$str .= " selected ";
		}
		$str	.= " value='" .	str_pad($i,	2, "0",	STR_PAD_LEFT) .	"'>" . str_pad($i, 2, "0", STR_PAD_LEFT) .	"</option>";
	}
	$str .= "</select>";
	return $str;
}


// ampm_dropdown: Updated 31 may 2006
function ampm_dropdown($pre, $selected_ampm) {
	$str .= "<select name='" . $pre . "ampm'>";
	$str .= " <option ";
	if ($selected_ampm=='AM') {
		$str .= " selected ";
	}
	$str .= " value='AM'>AM</option>";
	$str .= " <option ";
	if ($selected_ampm=='PM') {
		$str	.= " selected ";
	}
	$str .= " value='PM'>PM</option>";
	$str .= "</select>";
	return $str;
}


// get_qry_str: Updated 31 may 2006
function get_qry_str($over_write_key = array(),	$over_write_value =	array()) {
	global $_GET;
	$m = $_GET;
	if (is_array($over_write_key)) {
		$i = 0;
		foreach($over_write_key	as $key) {
			$m[$key] = $over_write_value[$i];
			$i++;
		}
	}
	else {
		$m[$over_write_key]	= $over_write_value;
	}
	$qry_str = qry_str($m);
	return $qry_str;
}


// qry_str: Updated 31 may 2006
function qry_str($arr, $skip = '') {
	$s = "?";
	$i = 0;
	foreach($arr as	$key =>	$value)	{
		if ($key !=	$skip) {
			if (is_array($value)) {
				foreach($value as $value2) {
					if ($i == 0) {
						$s .= $key . '[]=' . $value2;
						$i = 1;
					}
					else {
						$s .= '&' .	$key . '[]=' . $value2;
					}
				}
			}
			else {
				if ($i == 0) {
					$s .= "$key=$value";
					$i = 1;
				}else {
					$s .= "&$key=$value";
				}
			}
		}
	}
	return $s;
}


// check_radio: Updated 31 may 2006
function check_radio($s, $s2) {
	if (is_array($s2)) {
		// echo("<br>$s");
		// print_r($s2);
		if (in_array($s, $s2)) {
			return " checked ";
		}
	}
	else if ($s == $s2) {
		return " checked ";
	}
}


// sort_arrows: Updated 31 may 2006
function sort_arrows($column) {
	return '<A HREF="' . $_SERVER['PHP_SELF'] .	get_qry_str(array('order_by', 'order_by2'),	array($column, 'asc')) . '"><img src="images/up_arrow.gif" border="0"></a>	<a href="'	. $_SERVER['PHP_SELF'] . get_qry_str(array('order_by', 'order_by2'), array($column,	'desc')) . '"><img src="images/down_arrow.gif" border="0"></a>';
}


// select_option: Updated 31 may 2006
function select_option($s, $s1) {
	if ($s == $s1) {
		echo " selected	";
	}
}


// is_post_back: Updated 31 may 2006
function is_post_back() {
	if(count($_POST)>0) {
		return true;
	}
	else {
		return false;
	}	
}


// request_to_hidden: Updated 31 may 2006
function request_to_hidden($arr_skip='') {
	foreach($_REQUEST as $name => $value) {
		$s .= '<input type="hidden" name="'.$name.'" value="'.htmlspecialchars(stripslashes($value)).'">'."\n";
	}
	return $s;
}


// sql_to_array_file: Updated 31 may 2006
function sql_to_array_file($arr_name, $sql, $file, $full_table=false) {
	$str = "<?\n";
	$result = db_query($sql);
	while ($line = mysql_fetch_array($result)) {
		$line = ms_addslashes($line);
		if($full_table) {
			$key = $line[0];
			foreach($line as $name=>$value) {
				if(!is_numeric($name)) {
					$str .= '$'.$arr_name."['".$key."']['".$name."'] = '".$value."';\n";
				}
			}
			$str .= "\n";
		}
		else {
			$str .= '$'.$arr_name."['".$line[0]."'] = '".$line[1]."';\n";
		}
	}
	$str .= "\n?>";

	$fh = fopen($file, 'w');
	fwrite($fh, $str);
	fclose($fh);
	return true;
}


// array_radios: Updated 31 may 2006
function array_radios($arr, $sel_value = '', $name = '', $cols = 3, $extra = '') {
	if ($style != "") {
		$style = "class='" . $style . "'";
	}
	
	$colwidth = 100 / $cols;
	$colwidth = round($colwidth, 2);
	$j = 1;
	
	foreach($arr as $key => $value) {
		$tochecked = "";
		if (is_array($sel_value) && in_array($key, $sel_value)) {
			$tochecked = "checked";
		}
		if ($key != $missit) {
			if ($value != "") {
				if ($j == 1) {
					$checkstr .= "<table $tableattr><tr>\n";
				}
				else if (($j % $cols) == 1 || $cols==1) {
					$checkstr .= "</tr><tr>\n";
				}

				$checkstr .= "<td width='" . $colwidth . "%' $style valign=top><INPUT TYPE='radio' $javascript  NAME='$name' value='$key' $tochecked     > $value </td>\n";
				$j++;
			}
		}
	}
	$j--;
	
	for($x = $j % $cols;$x < 4;$x++) {
		if ($x != 3) {
			$checkstr .= "<td>&nbsp;</td>\n";
		}
		else {
			$checkstr .= "<td>&nbsp;</td></tr>\n";
		}
	}
	$checkstr .= "</table>";
	return $checkstr;
}


// Updated 24 aug 2006
function make_thumb_gd($imgPath, $destPath, $newWidth, $newHeight, $ratio_type = 'width', $quality = 60, $verbose = false) {
	// options for ratio type = width|height|distort|crop
	// get image info (0 width and 1 height, 2 is (1 = GIF, 2 = JPG, 3 = PNG)
	$size = @getimagesize($imgPath); 
	// break and return false if failed to read image infos
	if (!$size) {
		if ($verbose) {
			echo "Unable to read image info.";
		}
		return false;
	}
	$curWidth	= $size[0];
	$curHeight	= $size[1];
	$fileType	= $size[2];
	
	// width/height ratio
	$ratio =  $curWidth/$curHeight;

	$srcX = 0;
	$srcY = 0;
	$srcWidth = $curWidth;
	$srcHeight = $curHeight;

	if($ratio_type=='width') {
		// If the dimensions for thumbnails are greater than original image do not enlarge
		if($newWidth > $curWidth) {
			$newWidth = $curWidth;
		}
		$newHeight	= $newWidth / $ratio;
	}
	else if($ratio_type=='crop') {
		$thumbRatio = $newWidth / $newHeight;
		if($ratio < $thumbRatio) {
			$srcHeight = round($curHeight*$ratio/$thumbRatio);
			$srcY = round(($curHeight-$srcHeight)/2);
		}
		else {
			$srcWidth = round($curWidth*$thumbRatio/$ratio);
			$srcX = round(($curWidth-$srcWidth)/2);
		}		
	}
	else if($ratio_type=='height') {
		// If the dimensions for thumbnails are greater than original image do not enlarge
		if($newHeight > $curHeight) {
			$newHeight = $curHeight;
		}
		$newWidth	= $newHeight * $ratio;
	}
	else if($ratio_type=='distort') {
	}
	
	// create image
	switch ($fileType) {
		case 1:
		if (function_exists("imagecreatefromgif")) {
			$originalImage = imagecreatefromgif($imgPath);
		}
		else {
			if ($verbose) {
				echo "GIF images are not support in this php installation.";
				return false;
			}
		}
		$fileExt = 'gif';
		break;
		
		case 2:
		$originalImage = imagecreatefromjpeg($imgPath);
		$fileExt = 'jpg';
		break;
		
		case 3:
		$originalImage = imagecreatefrompng($imgPath);
		$fileExt = 'png';
		break;
		
		default:
		if ($verbose) {
			echo "Not a valid image type.";
		}
		return false;
	}
	// create new image

	$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
	imagecopyresampled($resizedImage, $originalImage, 0, 0, $srcX, $srcY, $newWidth, $newHeight, $srcWidth, $srcHeight);
	
	switch ($fileExt) {
		case 'gif':
		imagegif($resizedImage, $destPath, $quality);
		break;
		
		case 'jpg':
		imagejpeg($resizedImage, $destPath, $quality);
		break;
		
		case 'png':
		imagepng($resizedImage, $destPath, $quality);
		break;
	}
	// return true if successfull
	return true;
}

// show_thumb: Updated 6 feb 2007
function show_thumb($file_org, $width, $height, $ratio_type = 'width') {
	$file_name = str_replace(SITE_URL_PATH."/", "", $file_org);
	$file_name = str_replace("/", "^", $file_name);
	$cache_file = $width."x".$height.'__'.$ratio_type .'__'.$file_name;

	$file_fs_path = str_replace(SITE_URL_PATH, SITE_DIR_PATH, $file_org);
	if(!is_file(SITE_DIR_PATH."/".THUMB_CACHE_DIR."/".$cache_file)) {
		make_thumb_gd($file_fs_path, SITE_DIR_PATH."/".THUMB_CACHE_DIR."/".$cache_file, $width, $height, $ratio_type );
	}	
	return SITE_URL_PATH."/".THUMB_CACHE_DIR."/".$cache_file;
}


// ms_parse_keywords: Updated 31 may 2006
// Temporary function. Need to be made more elegant or replace with regular expression
function ms_parse_keywords($keywords) {
	$arr_keywords = array();
	$dq_end =true;
	$sp_end = true;
	for ($i=0;$i<strlen($keywords);$i++) {
		//echo "<br>cur_token:$cur_token, cur_keyword:$cur_keyword, dq_start:$dq_start, dq_end:$dq_end, sp_start:$sp_start, sp_end:$sp_end,";
		$cur_token = $keywords[$i];
		if($cur_token=='"') {
			if($dq_start) {
				$dq_end = true;
				$dq_start = false;
				$arr_keywords[] = $cur_keyword;
				$cur_keyword = '';
			}
			else if($dq_end) {
				$dq_end = false;
				$dq_start = true;
				$sp_start = false;
			}
			else {
				$dq_end = false;
				$dq_start = true;
			}
		}
		else if($cur_token==' ') {
			if($sp_start || $dq_end) {
				$sp_end = true;
				$sp_start = false;
				$arr_keywords[] = $cur_keyword;
				$cur_keyword = '';
			}
			else if($sp_end && !$dq_start) {
				$sp_end = false;
				$sp_start = true;
			}
			else if($dq_start) {
				$cur_keyword .= $cur_token;
			}
		}
		else {
			$cur_keyword .= $cur_token;
		}
	}

	$arr_keywords[] =$cur_keyword;
	return $arr_keywords;
}


// pagesize_dropdown: Updated 31 may 2006
function pagesize_dropdown($name, $value) {
	$arr = array('10'=>'10','25'=>'25','50'=>'50','100'=>'100');
	$m = $_GET;
	unset($m['pagesize']);
	return array_dropdown($arr, $value , $name, ' class="textfield"  onchange="location.href=\''.$_SERVER['PHP_SELF'].qry_str($m).'&pagesize=\'+this.value" ');
}

// sql_to_assoc_array: Updated 1 aug 2006
function sql_to_assoc_array($sql) {
	$arr = array();
	$result = db_query($sql);
	while ($line = mysql_fetch_array($result)) {
		$line = ms_form_value($line);
		$arr[$line[0]] = $line[1];
	}
	return $arr;
}

// sql_to_index_array: Updated 1 aug 2006
function sql_to_index_array($sql) {
	$arr = array();
	$result = db_query($sql);
	while ($line = mysql_fetch_array($result)) {
		$line = ms_form_value($line);
		$arr[] = $line[0];
	}
	return $arr;
}

// sql_to_array: Updated 1 aug 2006
function sql_to_array($sql) {
	$arr = array();
	$result = db_query($sql);
	while ($line = mysql_fetch_array($result)) {
		$line = ms_form_value($line);
		array_push($arr, $line);
	}
	return $arr;
}


// get_unique_file_name: Updated 2 aug 2006
function get_unique_file_name($file_name) {
	return str_shuffle(md5(uniqid(rand(), true))) . '.' . file_ext($file_name);
}


function qry_str_to_hidden($str) {
	$fields='';
	if(substr($str,0,1)=='?') {
		$str = substr($str,1);
	}
	$arr = explode('&', $str);
	foreach($arr as $pair) {
		list($name, $value) = explode('=',$pair);
		if($name!='') {
			$fields.='<input type="hidden" name="'.$name.'" id="'.$name.'" value="'.$value.'" />';
		}
	}
	return $fields;
}


// enum_to_array: Updated 14 sep 2006
function enum_to_array($table, $column) {
	$result = db_query("show fields from $table"); 
	while ($line_raw = mysql_fetch_assoc($result)) {
		$line = ms_display_value($line_raw);
		if($line['Field']==$column) {
			$Type = $line['Type'];
			$Type = substr($Type,6,-2);
			$arr_tmp = explode("','", $Type);
			foreach($arr_tmp as $val) {
				$arr[$val]=$val;
			}
			return $arr;
		}
	}
}


function mysql_to_date_withtime($date) {
	$date_arr	=	explode(' ',$date);
	list($year, $month, $day) = explode('-', $date_arr[0]);
	
	list($hour, $min, $sec) = explode(':', $date_arr[1]);
	return $month."/".$day."/".$year." ".$hour.":".$min.":".$sec;
}

function date_to_mysql_withtime($date) {
	$date_arr	=	explode(' ',$date);
	list($month, $day, $year) = explode('/', $date_arr[0]);
	
	list($hour, $min, $sec) = explode(':', $date_arr[1]);
	return $year."-".$month."-".$day." ".$hour.":".$min.":".$sec;
}

function readmyfile($path) {
	$text='';
	$fp = fopen($path,"r");
	while (!@feof($fp)) {
		$buffer = @fgets($fp, 4096);
		$text.= $buffer;
	}
	fclose($fp);
	return $text;
}


function validate_form() {
	return ' onsubmit="return validateForm(this,0,0,0,1,8);" ';
}


function protect_admin_page() {
	$cur_page = basename($_SERVER['PHP_SELF']);
	//echo "<br>cur_page: $cur_page";
	if($cur_page != 'index.php') {
		if ($_SESSION['sess_admin_login_id']=='') {
			header('Location: index.php');
			exit;
		}
	}
}


function searchSingleRecord($table,$field1,$field2,$value) {
  if($table!="" && $field1!="" && $field2!="" && $value!="") {
	  $sql	=	"select $field1 from $table where $field2='".$value."'";
	  $result	=	db_scalar($sql);
	  return $result;
  }
}


function count_character($str,$td_len) {
	$length=strlen($str);
	if($length > $td_len) {
		$i=$td_len;
	  do {
		  $i--;
		}
		while(substr($str,$i,1)!=" ");
	  return substr($str,0,$i)."...";
	}
	else {
		return $str;
	}
}


function set_session_msg($msg) {
	$_SESSION['sess_msg']=$msg;
}


function gen_random_string($length=6, $str='') {
	for($i=1; $i<=$length; $i++) {
		$ord = rand(48, 90);
		if( ( ($ord>=48) && ($ord<=57) ) || ( ($ord>=65) && ($ord<=90) ) ) {
			$str .= chr($ord);
		}
		else {
			$str .= gen_random_string(1);
		}
	}
	return $str;
}


function getRandomString($len=6) {
	$base='ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz123456789';
	$max=strlen($base)-1;
	$code='';
	mt_srand((double)microtime()*1000000);
	while (strlen($code)<$len+1) {
		$code.=$base{mt_rand(0,$max)};
	}
	return $code;
}
?>