<%function sqlSelect(selectType, page)

	set obj_db = db_open()       'connect to db


if page = "" then
	if selectType="" then selectType="p_groups"
	select case selectType


	case "p_list"
	sSQL = "SELECT DISTINCT  investmentid, partnershipname, description, price, date_open, date_close,  " & _
				"rationale, partnershipid, partnershiptype " & _
				"FROM admin_partnerships WHERE 1=1  "
	order = "partnershipname"


	case "p_details"
	sSQL = "SELECT DISTINCT  (unitspurch/unitstotal)*100 as ownership, (price / grossacres) / 43559.66 as unitprice,  investmentid, partnershipname, description, price, date_open, date_close,  " & _
				"rationale, partnershipid, partnershiptype " & _
				"FROM admin_partnerships " & _
				"WHERE partnershipid = " & partnershipid & " AND [investment type] = 'Partnership Land Sale' " & _
				"UNION " & _
				"SELECT DISTINCT  1 as ownership, 1 as unitprice,  investmentid, partnershipname, description, price, date_open, date_close,  " & _
				"rationale, partnershipid, partnershiptype " & _
				"FROM admin_partnerships " & _
				"WHERE partnershipid = " & partnershipid & " AND [investment type] <> 'Partnership Land Sale' "
	order = "partnershipname"


	case "p_groups"
		sSQL = "SELECT * " & _
			"FROM admin_partnerships_groups "
			order = "partnershiptype"

	case "p_names"
		sSQL = "SELECT *, invname as partnershipname " & _
			"FROM wfpc_investments "
			order = "partnershipname"

	case "p_names_avail"
		sSQL = "SELECT *, invname as partnershipname " & _
			"FROM wfpc_investments " & _
			"WHERE investmentid NOT IN (SELECT investmentid FROM partnership_wfpcinvestments)"
			order = "partnershipname"

	case  "p_relevantFacts"
			sSQL = "SELECT DISTINCT * " & _
						"FROM admin_partnerships_relevantfacts WHERE 1=1 "
			order = "facttitle"
			if factGroup <> "" then	sSQL = sSQL & " AND factgroup = '" & factGroup & "' "
			if factid <> "" then	sSQL = sSQL & " AND factid = " & factid & " "
		    if partnershipid <> "" then sSQL = sSQL & "	AND partnershipid = " & partnershipid & " " 
			if factYear <> "" then	sSQL = sSQL & " AND factpostdate like '% " & factYear & "%' "
			order = "factpostdate DESC"



	case "p_relevantFactGroups"
		sSQL = "SELECT DISTINCT groupname as factgroup  " & _
			          "FROM relevantfactgroups WHERE 1=1 "
		order = "groupname"
'		if factGroup <> "" then	sSQL = sSQL & " AND groupname = '" & factGroup & "' "
'	    if partnershipid <> "" then sSQL = sSQL & "	AND partnershipid = " & partnershipid & " " 

	case  "p_relevantFactfactYears"
		sSQL = "SELECT factpostdate " & _
			          "FROM admin_partnerships_relevantfacts WHERE 1=1 "
	  order = "factPostDate ASC"
	  if partnershipid <> "" then sSQL = sSQL & "	AND partnershipid = " & partnershipid & " " 

	case "p_phototour"
		sSQL = "SELECT p.photoid, p.photo " & _
	          "FROM photos p " & _
	          "WHERE p.phototourid = " & ptid

	case "p_clients"
		sSQL = "SELECT DISTINCT userid, firstname, lastname " & _
				"FROM admin_partnerships WHERE partnershipid = " & partnershipid
		order = "lastname"

	case "p_announcements"
		sSQL = "SELECT DISTINCT partnershiptype, partnershipid, announcementid " & _
					"FROM admin_announcements " & _						
					"WHERE announcementid = " & aid & " "
		order = "partnershiptype"

	case "p_userPartnershipGroups"    '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		sSQL = "SELECT DISTINCT  partnershipname, description, price, date_open, date_close, " & _
				"rationale, logo, partnershipid, partnershiptype, emailnotification " & _
				"FROM user_partnerships WHERE userid = " & userid
		order = "partnershiptype"	






	case "announce_list"
		sSQL = "SELECT DISTINCT announcementid, name, text, date FROM admin_announcements WHERE 1=1 "
		order = "date"
		if aid <> "" then sSQL = sSQL & "	AND announcementid = " & aid & " " 

	case "announce_details"
		sSQL = "SELECT * FROM admin_announcements WHERE 1=1 "
		order = "date"
		if aid <> "" then sSQL = sSQL & "	AND announcementid = " & aid & " " 

	case "announce_types"
		sSQL = "SELECT *  FROM announcementtypes WHERE 1=1 "
		order = "announcementtype"

	case "announce_user_new"
		sSQL = "SELECT * FROM user_announcements WHERE userid = " & userid & " AND expires >= '" & date() & "' "
		order = "[date] DESC"

	case "announce_user_all"
		sSQL = "SELECT * FROM user_announcements WHERE userid = " & userid & " "
		order = "[date] DESC"

	case "announce_types_user"
		sSQL = "SELECT * FROM announcementusertypes aut, announcementtypes at WHERE at.announcementtypeid = aut.announcementtypeid AND usertype = '" & usertype & "'"
		order = "at.announcementtypeid"

	case "announce_accept"
		'sSQL = "SELECT * FROM user_announcement_accept WHERE userid = " & userid
		sSQL = "SELECT * FROM user_announcement_accept WHERE userid = " & userid
		order = "announcementtypeid"

	case "announce_recipients"
		sSQL = "SELECT DISTINCT dbo.usertypes.usertype_name " & _
					"FROM         dbo.announcementtypes INNER JOIN " & _
                     "dbo.announcementusertypes ON dbo.announcementtypes.announcementtypeid = dbo.announcementusertypes.announcementtypeid INNER JOIN " & _
                     "dbo.usertypes ON dbo.announcementusertypes.usertype = dbo.usertypes.usertype  " & _
					 "WHERE announcementtypes.announcementtypeid = " & atid & " "
		order = "usertypes.usertype_name"






	case "i_details"
	sSQL = "SELECT investmentid, invName as partnershipname, invgroup as partnershiptype, totalprice as price, closedate as date_close, opendate as date_open " & _
				"FROM wfpc_investments WHERE 1=1 "
	order = "partnershipname"
	if investmentid <> "" then sSQL = sSQL & "	AND investmentid = " & investmentid & " " 







	case "u_planner"
	sSQL = "SELECT employeeID, planner_firstname, planner_lastname, planner_email " & _
				"FROM admin_partnerships WHERE userid = " & userid
'	if investmentid <> "" then sSQL = sSQL & "	AND investmentid = " & investmentid & " " 
	order = "employeeID"

	case "u_portfolio"
		sSQL = "SELECT *, (price / grossacres) / 43559.66 as unitprice, (unitspurch/unitstotal)*100 as ownership FROM user_portfolio WHERE userid = " & userid & " AND [Investment type] = 'Partnership Land Sale' "
		if investmentid <> "" then sSQL = sSQL & "	AND investmentid = " & investmentid & " " 
		order = "invname"

	case "u_usertypes"
		sSQL = "SELECT DISTINCT usertype_name, pagename, pageURL, homepageid, usertypeid, menutypeid FROM menu WHERE 1=1 "
		if utid <> "" then sSQL = sSQL & " AND usertypeid = " & utid & " "
       order = "usertype_name"
	   
	 case "u_profile"
	 	sSQL = "IF EXISTS (SELECT * FROM userprofileupdate WHERE contactid = " & userid & ") " & _
				"SELECT userprofileupdate.*, 'processing' as Status, users.spouse FROM users, userprofileupdate LEFT OUTER JOIN userlogin_log ON userprofileupdate.contactid = userlogin_log.userid WHERE userprofileupdate.contactid = " & userid & " AND users.userid = userprofileupdate.contactid; " & _
				"ELSE " & _
				"SELECT *, 'complete' as Status FROM users LEFT OUTER JOIN userlogin_log ON users.userid = userlogin_log.userid WHERE users.userid = " & userid & "; "

	case "u_investment" 
		sSQL = "SELECT price, investmentid, (price / grossacres) / 43559.66 as unitprice, (unitspurch/unitstotal)*100 as ownership " & _
				"FROM admin_partnerships " & _
				"WHERE investmentid = " & obj_rs("investmentid")

	case "u_planner_clients"
		sSQL = "SELECT DISTINCT * FROM planner_clients WHERE employeeid = " & userid
		order = "lastname"
	
	case "u_phone"
		sSQL = "IF EXISTS (SELECT * FROM userphoneupdate WHERE foreignkey = " & userid & ") " & _
				"SELECT * FROM userphoneupdate WHERE foreignkey = " & userid & " " & _
				"ELSE " & _
				"SELECT * FROM wfpc_phones WHERE foreignkey = " & userid & " "





	case "menu_p_list"
		sSQL = "SELECT DISTINCT partnershipname, partnershipid " & _
					"FROM user_partnerships WHERE 1=1 "
		order = "partnershipname"
		if usertype = "C" then sSQL = sSQL & "AND userid = " & userid & " "

	case "menu_p_list_small"
		sSQL = "SELECT DISTINCT partnershipname, partnershipid " & _
					"FROM user_partnerships " & _
					"WHERE userid = " & userid & " "
		order = "partnershipname"

	case "menu_p_list_small_planner"
		sSQL = "SELECT DISTINCT partnershipname, partnershipid " & _
					"FROM admin_partnerships " & _
					"WHERE employeeid = " & userid & " "
		order = "partnershipname"

	case "menu_global"
		sSQL = "SELECT *  FROM admin_partnerships"	'dummy select

	case "menu_rfgroups"
		sSQL = "SELECT rfg.groupname, rfg.factgroupid " & _
          "FROM relevantfactgroups rfg "

	case "menu_rfyears"
		sSQL = "SELECT factpostdate " & _
	          "FROM admin_partnerships_relevantfacts " & _
			  "WHERE partnershipid = " & partnershipid & " " & _
		order = "factPostDate ASC"

	case "menu_user"
		sSQL = "SELECT DISTINCT * " & _
			"FROM menu " & _
			"WHERE usertype = '" & usertype & "' "
		order = "[order] ASC"

	end select


	if order <> "" then sSQL = sSQL & " ORDER BY " & order


elseif page = "myprofile" then

		select case selectType

			case "p_userPartnershipGroups"    '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			sSQL = "SELECT DISTINCT  partnershipname, description, price, date_open, date_close, " & _
						"rationale, logo, partnershipid, partnershiptype, emailnotification " & _
						"FROM user_partnerships WHERE userid = " & userid
			order = "partnershiptype"	

	end select
	
end if













'response.write sSQL
	set rs = obj_db.execute(sSQL)              'execute the sql statement
	set sqlSelect = rs

end function


































'elseif page = "mypartnerships" then
'	if selectType="" then selectType="p_details"     'null case check
'	select case selectType
'		
'			case "p_details"
'				sSQL = "SELECT DISTINCT  partnershipname, description, price, date_open, date_close,  " & _
'							"rationale, partnershipid, partnershiptype, emailnotification " & _
'							"FROM admin_partnerships "
'				order = "partnershipname"


	'	case "p_blankPage"
	'				sSQL = "SELECT DISTINCT partnershipid, partnershipname " & _
	'							"FROM partnershipsView "
	'				order = "partnershipid" 'has no effect, just keeps sql happy
	'				factGroup = ""
	'				rfid = ""

		'	case else
		'		response.redirect("default.asp")      'no option chosen

'end select


'	sSQL = sSQL & " WHERE 1=1"    'dummy just to insert the WHERE keyword before following lines...

	'limit the sql select using where statements based on querystring
	'sSQL = sSQL & " ORDER BY " & order


'elseif page = "admin" then

'	if aid = "" then aid = -1
'	if selectType="" then selectType="admin_partnership_groups"
'	select case selectType'


'			case "p_details"
'					sSQL = "SELECT DISTINCT  partnershipname, partnershiptype, partnershipid " & _
'								"FROM admin_partnerships " & _						
'								"ORDER by partnershiptype"

	'		case "p_groups"
	'				sSQL = "SELECT DISTINCT  partnershipname, partnershiptype, partnershipid, announcementid " & _
	'							"FROM admin_announcements " & _						
	'							"WHERE partnershiptype is not null " & _
	'							"ORDER by partnershiptype"



'	end select


%>