<?php

ini_set("mssql.datetimeconvert","0");

function db_connect()
{
    global $host,$user,$pwd,$dbname;
	$conn=mssql_connect($host,$user,$pwd);
	if(!$conn)
		trigger_error(db_error(),E_USER_ERROR);
	mssql_select_db($dbname,$conn);
	return $conn;
}

function db_close($conn)
{
	return mssql_close($conn);
}

function db_query($qstring,$conn)
{
	if(!($ret=mssql_query($qstring,$conn)))
	{
	  trigger_error(db_error(), E_USER_ERROR);
	}
	return $ret;
	
}

function db_exec($qstring,$conn)
{
	return mssql_query($qstring,$conn);

}

function db_pageseek($qhandle,$pagesize,$page)
{
	db_dataseek($qhandle,($page-1)*$pagesize);
}

function db_dataseek($qhandle,$row)
{
   if($row>0)
	   mssql_data_seek($qhandle,$row);
}

function db_numrows($qhandle)
{
	// return only if qhandle exists, otherwise 0
	if ($qhandle) 
		return @mssql_num_rows($qhandle);
	else 
		return 0;
}



function db_fetch_array($qhandle)
{
	return @mssql_fetch_array($qhandle,MSSQL_ASSOC);
}

function db_fetch_numarray($qhandle)
{
	return @mssql_fetch_array($qhandle,MSSQL_NUM);
}

function db_error()
{
	return @mssql_get_last_message();
}





function db_addslashes($str)
{
	return str_replace("'","''",$str);
}

function db_stripslashes($str)
{
	return str_replace("''","'",$str);
}

function db_addslashesbinary($str)
{
	return "0x".bin2hex($str);
}

function db_stripslashesbinary($str)
{
	return $str;
}



// adds wrappers to field name if required
function AddFieldWrappers($strName)
{
	global $strLeftWrapper,$strRightWrapper;
	if(substr($strName,0,1)==$strLeftWrapper)
		return $strName;
	return $strLeftWrapper.$strName.$strRightWrapper;
}

function AddTableWrappers($strName)
{
	global $strLeftWrapper,$strRightWrapper;
	if(substr($strName,0,1)==$strLeftWrapper)
		return $strName;
	$arr=explode(".",$strName);
	$ret=$strLeftWrapper.$arr[0].$strRightWrapper;
	if(count($arr)>1)
		$ret.=".".$strLeftWrapper.$arr[1].$strRightWrapper;
	return $ret;
}

// removes wrappers from field name if required
function RemoveFieldWrappers($strName)
{
	global $strLeftWrapper,$strRightWrapper;
	if(substr($strName,0,1)==$strLeftWrapper)
		return substr($strName,1,strlen($strName)-2);
	return $strName;
}

function RemoveTableWrappers($strName)
{
	global $strLeftWrapper,$strRightWrapper;
	if(substr($strName,0,1)!=$strLeftWrapper)
		return $strName;
	$arr=explode(".",$strName);
	$ret=substr($arr[0],1,strlen($arr[0])-2);
	if(count($arr)>1)
		$ret.=".".substr($arr[1],1,strlen($arr[1])-2);
	return $ret;
}

function db_upper($dbval)
{
	return "upper(".$dbval.")";
}

function db_datequotes($val)
{
	return "'".$val."'";
}


function db_numfields($lhandle)
{
	return @mssql_num_fields($lhandle);
}

function db_fieldname($lhandle,$fnumber)
{
	return @mssql_field_name($lhandle,$fnumber);
}

function db_fieldtypen($lhandle,$fnumber)
{
	return mssql_field_type($lhandle,$fnumber);
}

function db_fieldtype($lhandle,$fname)
{
	for($i=0;$i<db_numfields($lhandle);$i++)
		if(db_fieldname($lhandle,$i)==$fname)
		{
			$type=mssql_field_type($lhandle,$i);
			return $type;
		}
	return "";
}

function FieldNeedQuotes($rs,$field)
{
	$type=db_fieldtype($rs,$field);
	$t=strtoupper($type);
	if($t=="BIGINT" || $t=="BINARY" || $t=="BIT" || $t=="DECIMAL" || $t=="FLOAT"
     || $t=="INT" || $t=="MONEY" || $t=="REAL" || $t=="SMALLINT" || $t=="SMALLMONEY"
     || $t=="TIMESTAMP" || $t=="TINYINT")
		return false;
	return true;
}


/*





function db_fieldnull($rs,$i)
{
	return true;
}

function db_fieldlen($rs,$i)
{
	return mssql_field_length($rs,$i);
}
*/

/*


function IsBinaryType($t)
{
	$type=strtoupper($t);
	if($type=="IMAGE")
		return true;
	return false;
}



function IsDateFieldType($stype)
{
	$type=strtoupper($stype);
	if($type=="DATETIME")
		return true;
	return false;
}

function IsCharType($stype)
{
	$type=strtoupper($stype);
	if($type=="BINARY" || $type=="CHAR" || $type=="NCHAR" || $type=="NTEXT" || $type=="NVARCHAR"
	|| $type=="TEXT" || $type=="VARBINARY" || $type=="VARCHAR")
		return true;
	return false;
}

function IsAutoIncrementField($field)
{
	global $strTableName;
	if(@$_SESSION[$strTableName."_fieldinfo"][$field]["auto_increment"])
		return true;
	return false;
}

function IsTextType($type)
{
	if(strtoupper($type)=="TEXT" || strtoupper($type)=="NTEXT")
		return true;
	return false;
}

function GetTableInfo()
{
	global $strTableName,$conn;
	$strSQL = "sp_columns @table_name='".$strTableName."'";
	$rs = db_query($strSQL,$conn);
	$_SESSION[$strTableName."_fieldinfo"]=array();
	while($data=db_fetch_array($rs))
	{
		$_SESSION[$strTableName."_fieldinfo"][$data["COLUMN_NAME"]] = array();
		$f = & $_SESSION[$strTableName."_fieldinfo"][$data["COLUMN_NAME"]];
		$type=$data["TYPE_NAME"];
//	remove type modifiers
		$t=strpos($type," identity");
		if(!($t===false))
		{
			$type=substr($type,0,$t);
			$f["auto_increment"]=1;
		}
		
//	parse field sizes
		if($pos=strpos($type,"("))
		{
			if($pos1=strpos($type,",",$pos))
			{
				$f["size"]=(integer)substr($type,$pos+1,$pos1-$pos-1);
				$f["scale"]=(integer)substr($type,$pos1+1,strlen($type)-$pos1-2);
			}
			else
			{
				$f["size"]=(integer)substr($type,$pos+1,strlen($type)-$pos-2);
				$f["scale"]=0;
			}
			$type=substr($type,0,$pos);
		}
		$f["type"]=$type;
		$f["null"]=@$data["IS_NULLABLE"];
	}
}
*/
?>