
function doCookieCheck() {
	var pos = document.cookie.indexOf("test=0")
	//alert('pos='+pos)
	if (pos==-1) {
		if (arguments.length==0) setTimeout('document.location.href="error_cookies.html"',2000)
		else setTimeout('document.location.href="../error_cookies.html"',2000)
	}
}
function setTestCookie() {
	if (document.cookie.length==0) {
		document.cookie = "test=0"
		return true
	}
	return false
}
function checkUserCookiesOn(path) {
	if (setTestCookie()) {
		if (arguments.length==0) setTimeout("doCookieCheck()",100)
		else setTimeout("doCookieCheck('subDir')",100)
	}
}

function FormLoad(myforms) {
	//pDB is a global database object - DEAD???
	if (typeof(pDB) == "undefined") {alert('Form Load unsuccessful, you must create/load a cookie.');return }

}


function DatabaseUpdateCookie(myforms) {
//DEAD
}

function DatabaseInitialiseExpires() {
	with (pDB.DBaseCookieRecordset) {
		MoveFirst()
		UpdateField('Path','/')
		UpdateField('ExpireHours',(1*24*100)) //1 hour * 24 * 100
		UpdateField('ExpireMinutes',1) //1 minute
	}
}

function DatabaseSaveCookie(myforms) {
	//pDB is a global database object
	if (typeof(pDB) == "undefined") {alert('Save unsuccessful, you must create a cookie.');return }
	DatabaseUpdateCookie(myforms)
	DatabaseInitialiseExpires()
	
	if (pDB.CookieSave()) {
		//wait - make sure cookie is saved before continuing
	}
}


function DatabaseRemoveCookie() {
	//pDB is a global database object
	if (typeof(pDB) == "undefined") {alert('Remove unsuccessful, you must create a cookie.');return }
	if (pDB.DBaseCookieProperties.Loaded == false) {alert('Remove unsuccessful, no cookie to remove.');return }
	
	if (pDB.CookieRemove()) { 
			//wait - make sure cookie is removed before continuing
	}
}



function DatabaseCreateCookie() {
	//pDB is a global database object, iMyCookieVersion is global value set in Initialise()
	var foundCookie = false, correctCookie = false;

	pDB = new Database('pDB')

	if  (pDB.CookieLoad()) { //initialise cookie database and load cookie if present
		 //wait - make sure cookie loaded before continuing
	}

	if (pDB.DBaseCookieProperties.Loaded == true) foundCookie = true

	if (foundCookie) {
		if (pDB.Recordsets('admin') == true) {
			pDB.admin.MoveFirst()
			if (pDB.admin.CookieVersion == iMyCookieVersion) correctCookie = true
		}
	}

	if (!correctCookie) {
		pDB = new Database('pDB')
		if  (pDB.CookieLoad()) { 		
		}


		pDB.CreateRecordset('admin')

		with (pDB.admin) {
			CreateField('CookieVersion')
		
			RsetProperties.CookieOn = true 
		}
		pDB.admin.AddRec([iMyCookieVersion]) //default record

		/*pDB.CreateRecordset('publisher')
		with (pDB.publisher) {
			CreateField('PUBLISHER_ID')
			CreateField('PASSWORD')
			RsetProperties.CookieOn = true 
		}*/

		pDB.CreateRecordset('basket')
		with (pDB.basket) {
			CreateField('CLIP_ID')
			CreateField('CLIP_TITLE')
			CreateField('CONTRIBUTOR_NAME')
			CreateField('SALE_PRICE')
			CreateField('PUBID') //for image
			CreateField('CONTID') //for image
			RsetProperties.CookieOn = true 
		}
	}

	DatabaseInitialiseExpires()
}


function BuyPiece(iCID) {
	var win,
		s="";

	var sBuyEnquire="&buyone=true";
 
	s = iCID
	s = "CID=" + s + sBuyEnquire

	if (window.name == 'clipview') window.name=''
	//win = window.open(('send_a_reel.asp?'+s),'clipsend','status,resizable,menubar,location,toolbar=yes,scrollbars,width=760,height=350')
	win = window.open(('/send_a_reel.asp?'+s),'clipsend','status,resizable,toolbar=yes,scrollbars,width=720,height=570')
	win.focus()
}

function SendReel() {
	var win,
		s="";
	if (pDB.basket.Count() == 0) {
		//alert('Send Reel','Send Reel cancelled as you must have at least one clip in your reel.')
		return false
	}

	var sBuyEnquire = "";
	if (arguments.length==1) {
		if (arguments[0]=="enquire") {
			sBuyEnquire="&enquire=true"
		} 
		
		else if (arguments[0]=="buymany") {
			sBuyEnquire="&buymany=true"
		}
		
		else {
			sBuyEnquire="&buyone=true"
		}
	}
	
	
	pDB.basket.MoveFirst()
	while (!pDB.basket.RsetProperties.Eof) {
		if (s != "") s+="*"
		s += pDB.basket.CLIP_ID
		pDB.basket.MoveNext()
	}
	s = "CID=" + s + sBuyEnquire
	
	if (window.name == 'clipview') window.name=''
	//win = window.open(('send_a_reel.asp?'+s),'clipsend','status,resizable,menubar,location,toolbar=yes,scrollbars,width=760,height=350')
	win = window.open(('/send_a_reel.asp?'+s),'clipsend','status,resizable,toolbar=yes,scrollbars,width=720,height=570')
	win.focus()
}

function ViewReel() {
	var win,
		s="";
	if (pDB.basket.Count() == 0) {
		Error('View Reel','View Reel cancelled as you must have at least one clip in your reel.')
		return false
	}

	pDB.basket.MoveFirst()
	while (!pDB.basket.RsetProperties.Eof) {
		if (s != "") s+="*"
		s += pDB.basket.CLIP_ID
		pDB.basket.MoveNext()
	}
	s = "CID=" + s
	 alert() 
	if (window.name == 'clipview') window.name=''
	win = window.open(('/getareel_viewreel.asp?'+s),'reelview','status,resizable,menubar,location,toolbar=yes,scrollbars,width=450,height=500')
	win.focus()
}

function ViewClip(clip_id,sPreURL) {
	var win
	if (arguments.length==1) {
	win = window.open(('/getareel_viewclip.asp?CID='+clip_id),'clipview','resizable,toolbar=no,scrollbars,width=650,height=570')
	} else {
	win = window.open((sPreURL + 'getareel_viewclip.asp?CID='+clip_id),'clipview','resizable,toolbar=no,scrollbars,width=650,height=570')
	}
	win.focus()
}

function ViewBiog(talent_id) {
	var win
	win = window.open(('getareel_viewbiog.asp?talent_id='+talent_id),'biogview','status,resizable,toolbar=yes,scrollbars,width=650,height=570')
	win.focus()
}

function GetReelIDs() {
	var win,
		s="";
	if (pDB.basket.Count() == 0) {
		Error('Send Reel','Action cancelled as you must have at least one clip in your reel.')
		return false
	}

	pDB.basket.MoveFirst()
	while (!pDB.basket.RsetProperties.Eof) {
		if (s != "") s+="*"
		s += escape(pDB.basket.CLIP_ID)
		pDB.basket.MoveNext()
	}
	s = "CID=" + s
	return s
}

function priceConvertor(p) {
	p = p + ""
	var t1 = p.split(".")
	if (t1.length==1) {
		p = p + ".00"
	} else {
		var t2 = t1[1]
		if (t2.length==1) {
			p = p + "0"
		}
	}
	return p
}

function ToggleForm() {
	basketpad.Hide()
	basketpad.MoveTo(-1000,0)
	
	logonpad2.MoveTo(721,0)
	logonpad2.Show()
}

function ToggleBasket() {
	logonpad2.Hide()
	logonpad2.MoveTo(-1000,0)
	
	basketpad.MoveTo(721,0)
	basketpad.Show()
}

function getHTML_Basket() {
	var s="", bDB = pDB.basket;
	var iClipCount = bDB.Count();

	var homeText1rev = 'color: #000000 ; font-family: arial, sans-serif ; font-weight: bold ; font-size: 11pt ; letter-spacing: 0pt;',
		homeText1 = 'color: #ffffff ; font-family: arial, sans-serif ; font-weight:bold ; font-size: 11pt ; letter-spacing: 0pt ; text-decoration: none;',
		baetText1 = 'color: #ffffff ; font-family: arial, sans-serif ; font-weight: normal ; font-size: 9pt ; letter-spacing: 0pt ;';
		
		//outer table
		outer ="<table border='0' cellspacing=0 cellpadding=0 width='220'>"
		outer+='<tr valign="top"><td>'

		endouter ='</td></tr></table>'

		var iNofPricedItems = 0, iNofNonPricedItems = 0, iTotalPricedItems = 0;
		bDB.MoveFirst()
		while (!bDB.RsetProperties.Eof) {
			var sImgURL =  "media/mediaprocessed/" + bDB.PUBID + "_" + bDB.CONTID + "_" + bDB.CLIP_ID + "__TH_FILE1.jpg"
			//basket entry div
			s+="<div style='position:relative;width:220;' id='d" + bDB.CLIP_ID + "'>"
			s+="<table border='0' cellspacing=0 cellpadding=2 width='220'>"
			s+="<tr valign='top'>"
			s+="<td colspan='3' class='basketText1'><img src='/pics/spacer.gif' width='1' height='3' border='0'></td>"
			s+="</tr>"
			s+="<tr valign='top'>"
			s+="<td rowspan='2' width='20' class='basketText1'>"
			s+="&nbsp;<a class='basketText1' href=# onclick='RemoveBasket(" + bDB.CLIP_ID + ");return false' title='Click to remove piece from this basket.'><img src='/icns/minus_on.gif' width='10' height='10' border='0'></a></td>"
			s+="<td rowspan='2' align='center' width='50'><img src='/pics/spacer.gif' height='2' border='0' width='50'><br><a class='basketText1' href=# onclick='ViewClip(" + bDB.CLIP_ID + ")' title='Click to enlarge view.'><img src='/" + sImgURL + "' border='0'></a></td>"
			var stSalePrice = bDB.SALE_PRICE*1
			if (bDB.SALE_PRICE == 0) {stSalePrice = "on request";iNofNonPricedItems++;}
			else {stSalePrice = priceConvertor(stSalePrice);iNofPricedItems++;iTotalPricedItems+=(parseFloat(stSalePrice));}
			s+="<td class='basketText1' width='150'><a class='basketText1' href=# onclick='ViewClip(" + bDB.CLIP_ID + ")' title='Click to enlarge view.'>" +bDB.CLIP_TITLE + "</a> £" + stSalePrice + "</td>"
			s+="</tr>"
			
			s+="<tr valign='top'>"
			s+="<td class='basketText1'>" + bDB.CONTRIBUTOR_NAME + "&nbsp; - <a class='basketText1' href=# onclick=\"BuyPiece(" + bDB.CLIP_ID + ",'buyone');return false\" target='new_win' title='Click to enquire about this piece.'>Piece enquiry?</a></td>"

			s+="</tr>"
			
			s+="<tr valign='top'>"
			s+="<td colspan='3' class='basketText1'><img src='/pics/line1.gif' width='1' height='1' border='0'></td>"
			s+="</tr>"
			s+="</table>"
			s+="</div>"
			bDB.MoveNext()
		}
		

		sH='' //sH = Header
		sH+='<table border=0 cellspacing=0 cellpadding=0 width="218">'
		sH+=''
		sH+='<tr valign="middle">'
		sH+='<td class="rightHeader" colspan="2" align="center">'
		sH+='<img src="/pics/basket_1.jpg" border="0" width="223" height="38" usemap="#basketInfo2">'
		sH+='</td>'
		sH+='</tr>'
		sH+='<!tr valign="middle">'
		sH+='<td class="rightHeader" colspan="2" align="center">'
		sH+='<img src="/pics/basket_2.jpg" border="0"></td>'
		sH+='</tr>'
		sH+='<tr valign="middle">'
		sH+='<td class="rightHeader" colspan="2" align="center">'
		sH+='<img src="/pics/basket_3.jpg" border="0" usemap="#basket"></td>'
		sH+='</tr>'
		
		sH+='<map name="basket">'
		sH+='<area shape="rect" coords="9,3,78,14" href=# onclick="parent.ToggleForm();return false">'
		sH+='<area shape="rect" coords="118,3,202,14" href=# onclick="parent.ToggleForm();return false">'
		sH+='</map>'
		
		sH+='<map name="basketInfo2">'
		if (bDB.Count()==0) {
		sH+='<area shape="rect" coords="3,23,67,35" href=# onclick="alert(\'You currently have no pieces in Your New Art Collection. Please add pieces BEFORE sharing them.\');" '
		} else {
		sH+='<area shape="rect" coords="3,23,67,35" href=# onclick="SendReel();return false" title="Share your selections with your, family, friend, client or professional advisor, to discuss and decide on a course of action.">'
		}
		sH+='<area shape="rect" coords="79,23,144,35" href=# '
		if (bDB.Count()==0) {
		sH+='onclick="alert(\'You currently have no pieces in Your New Art Collection. Please add pieces BEFORE enquiring about  them.\');" '
		} else {
		sH+='onclick="SendReel(\'enquire\');return false" '
		}
		sH+='title="Discuss your selection with the agency, such as more details regarding the piece(s) that interest you, framing options or shipping and packing options.">'

		sH+='<area shape="rect" coords="156,23,220,35" href=# '
		
		if (bDB.Count()==0) {
		sH+='onclick="alert(\'You currently have no pieces in Your New Art Collection. Please add pieces BEFORE clicking the BUY button.\');" '
		} else {
		sH+='onclick="SendReel(\'buymany\');return false" '
		}
		sH+='title="View your selection, total cost & shipping costs and check out to our secure payment site.">'
		sH+='</map>'
		
		
		//sH+='</table>'
	
		if ((iNofNonPricedItems!=0) || (iNofPricedItems != 0)) {
			//sH+='<table border="0" cellspacing=0 cellpadding=1 width="220" bgcolor="#81B4DB">'
			
			if (iNofPricedItems!=0) sH+='<tr><td class="homeText3" align="right">&nbsp;<br>&nbsp;' + iNofPricedItems + '&nbsp;Priced Pieces : Total : £' + priceConvertor(iTotalPricedItems) + ''
			if (iNofNonPricedItems!=0) sH+='<tr><td></td><td class="homeText3">&nbsp;<br>&nbsp;' + iNofNonPricedItems + '&nbsp;pieces : price on request.'

			sH+='&nbsp;&nbsp;&nbsp;<a class="homeText3a" href=# onclick="RemoveBasketAll();return false" title="click to remove all these pieces from your collection"><b>CLEAR</b></a>&nbsp;&nbsp;&nbsp;</td></tr>'

			//sH+='<tr><td>&nbsp;</td><td class="homeText3"><img src="/pics/spacer.gif" width="1" height="3" border="0"></td></tr>'
			//sH+='</table>'
		}
		
		//if empty
		if (bDB.Count()==0) {
		//introduction when basket is empty
		
		sH+='<tr><td class="basketText1" colspan="4">'
		sH+='<table border=0 cellspacing=0 cellpadding=0 width="200" class="basketText1">'
		sH+='<tr><td>&nbsp;&nbsp;&nbsp;</td><td>&nbsp;<br>'
		sH+='Add pieces to your collection from any search results by clicking the "+" icon above each piece.<br>&nbsp;<br>'
		sH+='Once you have built your collection, you can share pieces with friends, enquire about the collection or buy the pieces in the collection.'
		sH+=''
		sH+='</td></tr>'
		sH+='</td></tr>'
		sH+='</table>'
		} else {
		// basket has content
		sH+='<!--tr>'
		sH+='<td>'
		sH+='<table border=0 cellspacing=3 cellpadding=0 width="218">'
		sH+='<tr>'
		sH+='<td align="center"><a class="homeText3a" href=# onclick="RemoveBasketAll();return false" title="click to remove all these pieces from your collection"><b>CLEAR</b></a></td>'

		sH+='<td class="homeText3a">|</td>'
		
		sH+='<td align="center"><a class="homeText3a" href=# onclick="SendReel();return false" title="Share your selections with your, family, friend, client or professional advisor, to discuss and decide on a course of action."><b>SHARE</b></a></td>'

		sH+='<td class="homeText3a">|</td>'
		
		sH+='<td class="homeText3a" align="center">'
		sH+='<a class="homeText3a" href=# onclick="SendReel(\'enquire\');return false" title="Discuss your selection with the agency, such as more details regarding the piece(s) that interest you, framing options or shipping and packing options."><b>ENQUIRE</b></a>'
		sH+='</td>'
		
		sH+='<td class="homeText3a">|</td>'

		sH+='<td class="homeText3a" align="center">'
		sH+='<a class="homeText3a" href=# onclick="SendReel(\'buymany\');return false" title="View your selection, total cost & shipping costs and check out to our secure payment site."><b>BUY</b></a>'
		sH+='</td>'
		sH+='</tr>'
		
		sH+='</table>'
		sH+='</td>'
		sH+='</tr-->'
		
		
		}
		sH+='<tr><td class="homeText3" colspan="5"><img src="/pics/spacer.gif" width="1" height="3" border="0"></td></tr>'
		sH+='</table>'
		
		
	return (outer + sH + s + endouter)
}

function AddBasket(CLIP_ID,CLIP_TITLE,CONTRIBUTOR_NAME,SALE_PRICE,PUBID,CONTID) {
	if (pDB.DBaseCookieProperties.CookieLength > 4000) {
		Error('add piece','Add piece to basket cancelled as you have reached the max size of your reel. Delete a piece from basket and try again.')
		return false
	} 
	//showBasketPad()
	pDB.basket.FindFirst("CLIP_ID == " + CLIP_ID)
	
	if (!(pDB.basket.RsetProperties.NoMatch == true)) {
		Error('add piece','Add piece to basket cancelled as you already have this piece in your basket.')
		return false
	}

	pDB.basket.AddRec([CLIP_ID,CLIP_TITLE,CONTRIBUTOR_NAME,SALE_PRICE,PUBID,CONTID])
	//pDB.basket.Sort('CLIP_TITLE','ascend')
	basketpad.Print(getHTML_Basket())
	
	ToggleBasket()
	/*
	with (pDB.DBaseCookieRecordset) {
		MoveFirst()
		UpdateField('Domain','localhost')
		UpdateField('ExpireHours',(1*24*100)) //1 hour * 24 * 100
		UpdateField('ExpireMinutes',1) //1 minute
	}
	*/
	if (pDB.CookieSave()) {
		//wait
	}
}

function RemoveBasket(CLIP_ID) {
	pDB.basket.FindFirst("CLIP_ID == " + CLIP_ID)
	
	if (pDB.basket.RsetProperties.NoMatch == true) {
		return false
	}

	pDB.basket.DeleteRec()
	//pDB.basket.Sort('CLIP_TITLE','ascend')
	basketpad.Print(getHTML_Basket())

	if (pDB.basket.Count() == 0) {
		ToggleForm()
	}
	
	if (pDB.CookieSave()) {
		//wait
	}
}
function RemoveBasketAll() {
	if (!confirm('Remove all items from basket?')) return
	pDB.basket.MoveFirst()
	while (!pDB.basket.RsetProperties.Eof) {
		pDB.basket.DeleteRec()
		pDB.basket.MoveNext()
	}

	basketpad.Print(getHTML_Basket())

	if (pDB.basket.Count() == 0) {
		ToggleForm()
	}
	
	if (pDB.CookieSave()) {
		//wait
	}
}

function checkTalentSelection() {
	var myform = searchpad.Form('searchtalent')
	if (myform.name[0].checked==false) {
		Error('Search for Talent','Choose the "on" option to use this field.')
		myform.mytext.blur()
	}
}
function checkClipsSelection() {
	var myform = searchpad.Form('searchclips')
	if ((myform.product[0].checked==false) && (myform.keywords[0].checked==false) && (myform.client[0].checked==false)) {
		Error('Search for Clips','Choose at least one "on" option to use this field.')
		myform.mytext.blur()
	}
}


function submitSearchTalent(){
	var myform = searchpad.Form('searchtalent')
	if (myform.FK_SKILL_ID.value == -1) {
		Error('Search for Talent','You must select a talent to submit form.')
		return false
	}

	hideAllPads()
	//searchpad.Show()
	dosearchingtalentpad.Show()
	basketpad.Show()
	//navigationpad.Show()

	DatabaseSaveCookie(searchpad.Forms())
	searchpad.Form('searchtalent').submit()
}

function submitSearchClipsKeywords(mydd){
	var myform = searchpad.Form('searchclips')
	if (mydd.selectedIndex==0) return
	myform.searchtext.value = mydd[mydd.selectedIndex].value
	myform.SEARCH_FIELD.selectedIndex = 2
	return submitSearchClips()
}

function submitSearchClips(){
	var myform = searchpad.Form('searchclips')
	//if (myform.searchtext.value=="") {alert('You must enter a searchword. Only part of a searchword is required.');return false}

	hideAllPads()
	//searchpad.Show()
	dosearchingclippad.Show()
	basketpad.Show()
	//DatabaseSaveCookie(searchpad.Forms())
	searchpad.Form('searchclips').submit()
	return true
}


function hideAllPads() {
	if (typeof(homepad) == "undefined") return
	basketpad.Hide()
	searchpad.Hide()
	dosearchingtalentpad.Hide()
	dosearchingclippad.Hide()
	navigationpad.Hide()
	resultspad.Hide()
}

function showSearchPad() {
	hideAllPads()
	searchpad.Show()
	basketpad.Show()
	//resultspad.obj.src='blank.html' 
	resultspad.Hide()
}

function showBasketPad() {
	hideAllPads()

	basketpad.Show()
}

function initialise() {
	resultspad = new Obj("resultspad")
	resultspad.Hide()
	//resultspad.MoveTo(600,60)
	
	navigationpad = new Obj("navigationpad")
	navigationpad.Hide()

	dosearchingtalentpad = new Obj("dosearchingtalentpad")
	dosearchingtalentpad.Hide()

	dosearchingclippad = new Obj("dosearchingclippad")
	dosearchingclippad.Hide()

	bgpad = new Obj("bgpad")
	bgpad.Show()	

	homepad = new Obj("homepad")
	homepad.Show()
	
	searchpad = new Obj("searchpad")
	searchpad.Hide()
	
	recentpad= new Obj("recentpad")
	recentpad.Show()
	
	logonpad2 = new Obj("logonpad2")
	basketpad = new Obj("basketpad")

	myforms = searchpad.Forms()

	DatabaseCreateCookie()
	//alert(pDB.basket.Count())
	if (pDB.basket.Count()>0) {
		ToggleBasket()
	} else {
		ToggleForm()
	}
	
	FormLoad(myforms)
	basketpad.Print(getHTML_Basket())
}

//global vars
var pDB, 
	iMyCookieVersion = 3, 
	searchpad="", dosearchingtalentpad, dosearchingclippad, 
	basketpad="", 
	navigationpad="", homepad="", resultspad="", recentpad="", logonpad2 = "", bgpad = "";
	
var bDev = false;

