
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 DatabaseInitialiseExpires() {
	with (pDB.DBaseCookieRecordset) {
		MoveFirst()
		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 }
	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')
	pDB.DBaseCookieProperties.SeparatorRecords = '<*'
	
	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')
			RsetProperties.CookieOn = true 
		}
	}

	DatabaseInitialiseExpires()
}

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
	}

	pDB.basket.MoveFirst()
	while (!pDB.basket.RsetProperties.Eof) {
		if (s != "") s+="*"
		s += pDB.basket.CLIP_ID
		pDB.basket.MoveNext()
	}
	s = "CID=" + s
	
	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
	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 getHTML_Basket() {
	var s="", bDB = pDB.basket;
	var iClipCount = bDB.Count();

		s+='<table border=0 cellspacing=0 cellpadding=0 width="150">\n'
		s+='<tr>\n'
		s+='<td valign="top" align="center">\n'
		s+='<span class="menuText1"><b><img src="pics/make_a_reel.jpg" width="150" height="22" border="0" alt="make a reel"></b></span>\n'
		s+='</td></tr></table>\n'
		
		if (iClipCount == 1)  {
			s+= '&nbsp;<span class="basketText1">' + bDB.Count() + ' clip in this e-reel</span>\n'
		} else {
			s+= '&nbsp;<span class="basketText1">' + bDB.Count() + ' clips in this  e-reel</span>\n'
		}	

		s+='<table border="0" cellspacing=0 cellpadding=1 width="150">\n'
		s+='<tr><td class="homeText3"><img src="pics/spacer.gif" width="1" height="3" border="0"></td></tr>\n'
		if (bDB.Count()==0) {
		s+='<tr bgcolor="#ff9900">\n'
		s+='<td class="basketText1">Add clips to this e-reel from<br>your search results<br>and send via email.</td>\n'
		s+='</tr>\n'
		s+='<tr>'
		s+='<td class="basketText1">Build a custom e-reel by clicking "add" in your search results and then send the e-reel to any email address...</td>'
		s+='</tr>'
		} else { //RemoveBasketAll()
		s+='<tr bgcolor="#ff9900">'
		s+='<td class="homeText3a">'
		s+='<a class="homeText3a" href=# onclick="RemoveBasketAll();return false" title="click to remove all clips from e-reel">[x...]</a>'
		s+='&nbsp;&nbsp;|&nbsp;&nbsp;'
		//s+='<td><a class="homeText3a" href=# onclick="parent.SendReel();return false"><img src="pics/send_reel.gif" width="25" height="18" border="0" title="click to send this reel to any email address"></a></td>'
		s+='<a class="homeText3a" href=# onclick="SendReel();return false" title="click to send this custom e-reel to any email address">send this e-reel</a>'
		s+='</td>'
		s+='</tr>'
		}
		s+='</table>'
		
		bDB.MoveFirst()
		while (!bDB.RsetProperties.Eof) {
			s+="<div style='position:relative;' id='d" + bDB.CLIP_ID + "'><span class='basketText1'>&nbsp;<a class='basketText1' href=# onclick='RemoveBasket(" + bDB.CLIP_ID + ");return false' title='Click to remove clip from reel.'>[x]</a> <a class='basketText1' href=# onclick='ViewClip(" + bDB.CLIP_ID + ")' title='Click to view clip.'>" + bDB.CLIP_TITLE + "</a></span></div>"
			bDB.MoveNext()
		}

		s+='<div style="position:relative;"><img src="pics/spacer.gif" width="1" height="3" border="0"></div>'
	
	//popup('',300,300,s)
	return s
}



function AddPublisher(PUBLISHER_ID,PASSWORD) {
	pDB.publisher.AddRec([PUBLISHER_ID,PASSWORD])
	if (pDB.CookieSave()) {
		//wait
	}
}

function CheckVideoPassword(PUBLISHER_ID) {
	if (typeof(PUBLISHER_ID) == "undefined") return
	var query = "PUBLISHER_ID == " + PUBLISHER_ID
	pDB.publisher.FindFirst(query)
	if (!pDB.publisher.RsetProperties.NoMatch) {
		return pDB.publisher.PASSWORD
	} else {
		return false
	}
}

function AddBasket(CLIP_ID,CLIP_TITLE) {
	if (pDB.DBaseCookieProperties.CookieLength > 4000) {
		Error('add clip','Add clip to reel cancelled as you have reached the max size of your reel. Delete a clip from reel and try again.')
		return false
	} 
	//showBasketPad()
	pDB.basket.FindFirst("CLIP_ID == " + CLIP_ID)
	
	if (!(pDB.basket.RsetProperties.NoMatch == true)) {
		Error('add clip','Add clip to reel cancelled as you already have this clip in your reel.')
		return false
	}

	pDB.basket.AddRec([CLIP_ID,CLIP_TITLE])
	//pDB.basket.Sort('CLIP_TITLE','ascend')
	basketpad.Print(getHTML_Basket())
	
	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.CookieSave()) {
		//wait
	}
}
function RemoveBasketAll() {
	if (!confirm('Remove all clips from reel?')) return
	pDB.basket.MoveFirst()
	while (!pDB.basket.RsetProperties.Eof) {
		pDB.basket.DeleteRec()
		pDB.basket.MoveNext()
	}

	basketpad.Print(getHTML_Basket())

	if (pDB.CookieSave()) {
		//wait
	}
}

function checkTalentSelection() {
	var myform = searchpeoplepad.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 = searchpeoplepad.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(searchpeoplepad.Forms())
	searchpeoplepad.Form('searchtalent').submit()
}

function submitSearchClipsKeywords(mydd){
	var myform = searchpeoplepad.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()
	searchpeoplepad.Hide()
	dosearchingtalentpad.Hide()
	dosearchingclippad.Hide()
	navigationpad.Hide()
	resultspad.Hide()
}

function showSearchPeoplePad() {
	hideAllPads()
	searchpad.Show()
	basketpad.Show()
	//resultspad.obj.src='blank.html' 
	resultspad.Hide()
}

function showBasketPad() {
	hideAllPads()

	basketpad.Show()
}

function initialise() {
	//nav pads
	navigationpad = new Obj("navigationpad")
	dosearchingtalentpad = new Obj("dosearchingtalentpad")
	dosearchingclippad = new Obj("dosearchingclippad")

	//fixed pads - visible
	bgpad = new Obj("bgpad")	
	homepad = new Obj("homepad")

	//search pads
	searchpeoplepad = new Obj("searchpeoplepad")
	searchspotpad = new Obj("searchspotpad")

	basketpad= new Obj("basketpad")
	//data pads - div
	resultslatestspotspad = new Obj("resultslatestspotspad") //initially visible
	
	//data pads - iframes
	resultsclippad = new Obj("resultsclippad")
	resultstalentpad = new Obj("resultstalentpad")
	resultscliptalentpad = new Obj("resultscliptalentpad")
	resultstalentduplicatepad = new Obj("resultstalentduplicatepad")
	
	DatabaseCreateCookie()
	basketpad.Print(getHTML_Basket())
}

//global vars
var pDB, 
	iMyCookieVersion = 4, 
	searchpeoplepad="", searchspotpad="", resultslatestspotspad="",
	navigationpad="", dosearchingtalentpad, dosearchingclippad, 
	basketpad="", 
	homepad="",  bgpad = "";
	
var bDev = false;

