var fld=''
var n=0
var startpos=1
var profilearray = new Array(-1,-1,-1,-1)
var selectedimage=0

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function send()

{
//	alert('This feature is unavailable at present.')
	var ok=1
	if (document.all.name.value=='') ok=0
	if (document.all.email.value=='') ok=0
	if (document.all.subject.value=='') ok=0
	if (document.all.message.value=='') ok=0			
	
	if (ok==0) alert('Please fill in all fields in the form.')
	
	if (ok==1) sendrequest.submit()
}
function showprofiles(stp)
{
	var tot=0
	var n=0
	var ml=''
	var nm=''
	
	startpos+=stp
	
	for (m=0;m<projectlist.length;m++)
		{
			if (document.all.projecttype.value!='All')
				{
					if (nthfield(projectlist[m],"~",2)==document.all.projecttype.value)
						{
							ml+=m.toString()+','
							tot++
						}
				}
			if (document.all.projecttype.value=='All') 
				{
					ml+=m.toString()+','
					tot++
				}
		}
	

	m=1
	
	if (stp==-1)
	{
		if (startpos==0) startpos=1
	}
	
	if (stp==1)
	{
		if (tot<5) startpos--
		
		if (tot>4)
		{
			if (nthfield(ml,",",startpos+4)=='') startpos--
		}
	}
	
	nm=nthfield(ml,",",startpos)	
	profilearray[0]=-1
	if (nm=='') document.all.pic1.src='img/metcurve.jpg'
	if (nm!='')
	{
		n=parseInt(nm,10)
		document.all.pic1.src='profile/thumbnails/'+nthfield(projectlist[n],"~",10)
		profilearray[0]=n
	}

	nm=nthfield(ml,",",startpos+1)
	profilearray[1]=-1
	if (nm=='') document.all.pic2.src='img/metcurve.jpg'
	if (nm!='')
	{
		n=parseInt(nm,10)
		document.all.pic2.src='profile/thumbnails/'+nthfield(projectlist[n],"~",10)	
		profilearray[1]=n
	}
	
	nm=nthfield(ml,",",startpos+2)	
	profilearray[2]=-1
	if (nm=='') document.all.pic3.src='img/metcurve.jpg'	
	if (nm!='')
	{
		n=parseInt(nm,10)		
		document.all.pic3.src='profile/thumbnails/'+nthfield(projectlist[n],"~",10)		
		profilearray[2]=n
	}
	
	nm=nthfield(ml,",",startpos+3)
	profilearray[3]=-1
	if (nm=='') document.all.pic4.src='img/metcurve.jpg'
	if (nm!='')
	{
		n=parseInt(nm,10)
		document.all.pic4.src='profile/thumbnails/'+nthfield(projectlist[n],"~",10)	
		profilearray[3]=n
	}
	
	document.all.lastprofile.innerHTML=''
	document.all.nextprofile.innerHTML=''
	
	if (startpos!=1)
		{
			document.all.lastprofile.innerHTML='<a href="javascript:showprofiles(-1)" style="text-decoration:none"><font color="#000000"><b><div align="center"><img src="img/prev-off.jpg" name="Image51" width="16" height="64" border="0"></div></b></font></a>'
			
		}	
		
	if (nthfield(ml,",",startpos+5)!='') 
		{
			document.all.nextprofile.innerHTML='<a href="javascript:showprofiles(1)" style="text-decoration:none"><font color="#000000"><b><div align="center"><img src="img/next-off.jpg" name="Image51" width="16" height="64" border="0"></div></b></font></a>'
	
		}
}

function tartup(fld,vl)
{
	var st=''
	
	st+='<table width="520" border="0" cellspacing="0" cellpadding="0"><tr><td width="128" bgcolor="#dddddd"><img src="img/blank.gif" width="2" height="2"></td><td width="2"><img src="img/blank.gif" width="2" height="2"></td><td width="390" bgcolor="#dddddd"><img src="img/blank.gif" width="2" height="2"></td></tr></table>'

	st+='<table width="520" border="0" cellspacing="0" cellpadding="0"><tr><td width="2" bgcolor="#dddddd"></td><td width="124" bgcolor="#dddddd" valign="top"><span class="maintext"><b>'+fld+'</b></span></td><td width="2" bgcolor="#dddddd"></td>      <td width="2"></td><td width="2" bgcolor="#dddddd"></td><td width="386" bgcolor="#dddddd" valign="top"><span class="maintext">'+vl+'</span></td><td width="2" bgcolor="#dddddd"></td></tr></table>'

	st+='<table width="520" border="0" cellspacing="0" cellpadding="0"><tr><td width="128" bgcolor="#dddddd"><img src="img/blank.gif" width="2" height="2"></td><td width="2"><img src="img/blank.gif" width="2" height="2"></td><td width="390" bgcolor="#dddddd"><img src="img/blank.gif" width="2" height="2"></td></tr></table>'

	st+='<table width="520" border="0" cellspacing="0" cellpadding="0"><tr><td width="520"><img src="img/blank.gif" width="2" height="2"></td></tr></table>'


	return (st)
}
function showpictures(idx,picnum)
{
	var picfile=''
	var st=''
	var idxs=idx.toString()
	
	idx=profilearray[idx]
	selectedimage=picnum	
	if (idx!=-1)
	{
		picfile=nthfield(projectlist[idx],"~",picnum+7)
		document.all.bigimage.src='profile/mainimages/'+picfile		
		
		document.all.projecttitle.innerHTML='<span class="pagelowertitle">'+nthfield(projectlist[idx],"~",1)+'</span><br><br>'

		if (nthfield(projectlist[idx],"~",3)!='') st+=tartup('Client',nthfield(projectlist[idx],"~",3))
		if (nthfield(projectlist[idx],"~",4)!='') st+=tartup('Details',nthfield(projectlist[idx],"~",4))
		if (nthfield(projectlist[idx],"~",5)!='') st+=tartup('Work carried out',nthfield(projectlist[idx],"~",5))
		if (nthfield(projectlist[idx],"~",7)!='') st+=tartup('Project Value',"£"+nthfield(projectlist[idx],"~",7))
		if (nthfield(projectlist[idx],"~",6)!='') st+=tartup('Completed',nthfield(projectlist[idx],"~",6))
		
		document.all.infobox.innerHTML=st
		
		document.all.otherpic.innerHTML=''
		st=''
		if (selectedimage==1)
			{
				if (nthfield(projectlist[idx],"~",9)!='') st='<a href="javascript:showpictures('+idxs+',2)"><font color="#000000"><b><span class="maintext">SEE IMAGE 2</span></b></font></a>'
			}
		if (selectedimage==2)
			{
				if (nthfield(projectlist[idx],"~",8)!='') st='<a href="javascript:showpictures('+idxs+',1)"><font color="#000000"><b><span class="maintext">SEE IMAGE 1</span></b></font></a>'
			}
		
		document.all.otherpic.innerHTML=st	
	}	

}	

function positionbanner()
{

	
	var x=document.body.clientWidth
	var y=document.body.clientHeight+document.body.scrollTop
	
	x=x-960
	x=x/2
	if (x<0) x=0

	
	y=y-64

	if (document.body.clientHeight<pageheight) y=pageheight

	document.all.banner.style.left=x.toString()+"px"
	document.all.banner.style.top=y.toString()+"px"
	document.all.banner.style.visibility='visible'

}

function savedetails(id,st)
{
	var st=''
	
	if (id==0)
	{
		
		st='list.asp?mode=saverecord&id='+document.all.newsheadlines.value+'&headline='+decodedesc(document.all.headline.value)+'&content='+decodedesc(document.all.content.value)+'&link='+decodedesc(document.all.newslink.value)+'&newsdate='+document.all.dd.value+'/'+document.all.mm.value+'/'+document.all.yy.value

		fld='savedetails'	
		http.open('GET',st)
		http.onreadystatechange = HttpDownload;
		http.send(null)		
	}
	
	if (id==1)	
	{	
		alert('Saved.')
		window.location=window.location
	}

}

function saveprofile(id,st)
{
	var st=''
	fld='saveprofile'	

	if (id==0)
	{
		
		st='list.asp?mode=saveprofile&id='+document.all.projectlist.value+'&projecttype='+decodedesc(document.all.projecttype.value)+'&projectname='+decodedesc(document.all.projectname.value)+'&client='+decodedesc(document.all.client.value)+'&projectdetail='+decodedesc(document.all.projectdetail.value)+'&typeofwork='+document.all.typeofwork.value+'&year='+decodedesc(document.all.year.value)+'&jobvalue='+decodedesc(document.all.jobvalue.value)+'&pic1='+decodedesc(document.all.pic1.value)+'&pic2='+decodedesc(document.all.pic2.value)+'&thumbnail='+decodedesc(document.all.thumbnail.value)
	
	
		http.open('GET',st)
		http.onreadystatechange = HttpDownload;
		http.send(null)		
	}
	
	if (id==1)	
	{	
		alert('Saved.')
		window.location=window.location
	}

}



function saveprofilecheck()
{
	var err=''
	
	if (document.all.projecttype.value=='') err+='Project Type is missing.\n'
	if (document.all.projectname.value=='') err+='Project Name is missing.\n'
	if (document.all.client.value=='') err+='Client name is missing.\n'
	if (document.all.pic1.value=='') err+='Picture 1 is missing.\n'
	if (document.all.thumbnail.value=='') err+='Thumbnail is missing.\n'
	
	if (err!='') alert(err)
	
	if (err=='') saveprofile(0,'')
	
}

function pickpicture(idx)
{
	if (document.all["pic"+idx.toString()].value=='') document.all["pic"+idx.toString()+"image"].src='img/thumb-blank.jpg'	
	
	if (document.all["pic"+idx.toString()].value!='') 
		{
			document.all["pic"+idx.toString()+"image"].src='profile/mainimages/'+document.all["pic"+idx.toString()].value
		}
}
function pickthumbnail()
{
	if (document.all.thumbnail.value=='') document.all.thumbnailimage.src='img/thumb-blank.jpg'
	if (document.all.thumbnail.value!='')
		{
			document.all.thumbnailimage.src='profile/thumbnails/'+document.all.thumbnail.value
		}
}

function picklinkfile()
{
	document.all.newslink.value=''
	if (document.all.filelist.value!='') document.all.newslink.value='uploads/'+document.all.filelist.value	
}

function getfiles(id,st)
{
	var n=0
	
	fld='getfiles'
	if (id==0)
		{			
			http.open('GET','list.asp?mode=getfiles')
			http.onreadystatechange = HttpDownload;
			http.send(null)	
		}
		
	if (id==1)
		{
			n=1
			while (nthfield(st,"~",n)!='')
				{
					document.all.filelist[document.all.filelist.length]=new Option(nthfield(st,"~",n),nthfield(st,"~",n))
					n++	
				}
		}
}

function getprojectlist(id,st)
{
	var n=0
	
	fld='getprojectlist'
	if (id==0)
		{			
			http.open('GET','list.asp?mode=getprojectlist')
			http.onreadystatechange = HttpDownload;
			http.send(null)	
		}
		
	if (id==1)
		{

			document.all.projectlist.length=0
			document.all.projectlist[0]=new Option('New project','0')
			n=1

			while (nthfield(st,"~",n)!='')
				{	
					document.all.projectlist[document.all.projectlist.length]=new Option(nthfield(nthfield(st,"~",n),"|",1),nthfield(nthfield(st,"~",n),"|",2))
					n++	
				}
		}		
}
function getthumbnails(id,st)
{
	var n=0
	
	fld='getthumbnails'
	if (id==0)
		{			
			http.open('GET','list.asp?mode=getthumbnails')
			http.onreadystatechange = HttpDownload;
			http.send(null)	
		}
		
	if (id==1)
		{
			n=1
			while (nthfield(st,"~",n)!='')
				{
					document.all.thumbnail[document.all.thumbnail.length]=new Option(nthfield(st,"~",n),nthfield(st,"~",n))
					n++	
				}
			getmainimages(0,'')
		}
}

function getmainimages(id,st)
{
	var n=0
	
	fld='getmainimages'
	if (id==0)
		{			
			http.open('GET','list.asp?mode=getmainimages')
			http.onreadystatechange = HttpDownload;
			http.send(null)	
		}
		
	if (id==1)
		{
			n=1
			while (nthfield(st,"~",n)!='')
				{
					document.all.pic1[document.all.pic1.length]=new Option(nthfield(st,"~",n),nthfield(st,"~",n))
					document.all.pic2[document.all.pic2.length]=new Option(nthfield(st,"~",n),nthfield(st,"~",n))
					n++	
				}
			getprojectlist(0,'')

		}
}

function savedetails(id,st)
{
	var st=''
	
	if (id==0)
	{
		
		st='list.asp?mode=saverecord&id='+document.all.newsheadlines.value+'&headline='+decodedesc(document.all.headline.value)+'&content='+decodedesc(document.all.content.value)+'&link='+decodedesc(document.all.newslink.value)+'&newsdate='+document.all.dd.value+'/'+document.all.mm.value+'/'+document.all.yy.value

		fld='savedetails'	
		http.open('GET',st)
		http.onreadystatechange = HttpDownload;
		http.send(null)		
	}
	
	if (id==1)	
	{	
		alert('Saved.')
		window.location=window.location
	}
	
	
}
function saverecord()
{
	var err=''
	var dd=''
	var mm=''
	var yy=''
	var ok=1

	
	if (document.all.headline.value=='') err+='Headline is missing.\n'
	if (document.all.content.value=='') err+='Content is missing.\n'
	if (document.all.dd.value=='') err+='Date (DD) is missing.\n'
	if (document.all.mm.value=='') err+='Month (MM) is missing.\n'
	if (document.all.yy.value=='') err+='Year (YY) is missing.\n'
	
	if (err=='')
		{
			dd=document.all.dd.value
			mm=document.all.mm.value
			yy=document.all.yy.value
			ok=1
			
			if ((mm=='04') || (mm=='06') || (mm=='09') || (mm=='11'))
				{
					if (dd>'30') ok=0
				}
				
			if (mm=='02')
				{
					if (dd>'29') ok=0	

					if ((yy!='2012') && (yy!='2016') && (yy!='2020'))
					{
						if (dd>'28') ok=0	
					}
				}
				
			if (ok==0) err+='Date is in incorrect format.\n'
		}
		
	if (err!='') alert(err)
	
	if (err=='') savedetails(0,'')
	
}

function decodedesc(st)

{
	var st2=''
	var n=0
	var ok=1
	
	if (st!='')
	{
	for(n=0;n<st.length;n++)
		{
			ok=1
			
			// definite reserved characters
			
			if (st.charAt(n)=='$') { st2+='%24'; ok=0 }
			if (st.charAt(n)=='&') { st2+='%26'; ok=0 }
			if (st.charAt(n)=='+') { st2+='%2B'; ok=0 }
			if (st.charAt(n)==',') { st2+='%2C'; ok=0 }
			if (st.charAt(n)=='/') { st2+='%2F'; ok=0 }
			if (st.charAt(n)==':') { st2+='%3A'; ok=0 }
			if (st.charAt(n)==';') { st2+='%3B'; ok=0 }
			if (st.charAt(n)=='=') { st2+='%3D'; ok=0 }
			if (st.charAt(n)=='?') { st2+='%3F'; ok=0 }
			if (st.charAt(n)=='@') { st2+='%40'; ok=0 }

			
			// unsafe characters
			
			if (st.charAt(n)==' ') { st2+='%20'; ok=0 }
			if (st.charAt(n)=='"') { st2+='%22'; ok=0 }
			if (st.charAt(n)=='<') { st2+='%3C'; ok=0 }
			if (st.charAt(n)=='>') { st2+='%3E'; ok=0 }
			if (st.charAt(n)=='#') { st2+='%23'; ok=0 }
			if (st.charAt(n)=='%') { st2+='%25'; ok=0 }
			if (st.charAt(n)=='{') { st2+='%7B'; ok=0 }
			if (st.charAt(n)=='}') { st2+='%7D'; ok=0 }
			if (st.charAt(n)=='|') { st2+='%7C'; ok=0 }
			if (st.charAt(n)=='\\'){ st2+='%5C'; ok=0 }
			if (st.charAt(n)=='^') { st2+='%5E'; ok=0 }
			if (st.charAt(n)=='~') { st2+='%7E'; ok=0 }
			if (st.charAt(n)=='[') { st2+='%5B'; ok=0 }
			if (st.charAt(n)==']') { st2+='%5D'; ok=0 }
			if (st.charAt(n)=='`') { st2+='%60'; ok=0 }
			if (st.charAt(n)=="'") { st2+='%60'; ok=0 }
			if (st.charAt(n)=="£") { st2+='%A3'; ok=0 }			
						
			if (ok==1) st2+=st.charAt(n)
		}
	}
	return(st2)
}

function deleteproject(id,st)

{
	if (document.all.projectlist.value=='0') alert('Click on an existing project to delete it.')
	if (document.all.projectlist.value!='0')
	{		
		if (id==0)
		{
			if (confirm("Are you sure you want to delete this project?")==true)
			{
			fld='deleteproject'
			http.open('GET','list.asp?mode=deleteproject&id='+document.all.projectlist.value)
			http.onreadystatechange = HttpDownload;
			http.send(null)				
			}			
		}
			
	}
}


function deletenews(id,st)
{
	if (document.all.newsheadlines.value=='0') alert('Click on an existing news item to delete it.')
	if (document.all.newsheadlines.value!='0')
	{		
		if (id==0)
		{
			if (confirm("Are you sure you want to delete this news item?")==true)
			{
			fld='deletenews'
			http.open('GET','list.asp?mode=deletenews&id='+document.all.newsheadlines.value)
			http.onreadystatechange = HttpDownload;
			http.send(null)				
			}			
		}
			
	}
}


function selectproject(id,st)
{
	fld='selectproject'
	if (document.all.projectlist.value=='0')
		{
			document.all.projecttype.value=''
			document.all.projectname.value=''
			document.all.client.value=''
			document.all.projectdetail.value=''
			document.all.typeofwork.value=''
			document.all.year.value=''
			document.all.jobvalue.value=''
			document.all.pic1.value=''
			document.all.pic2.value=''
			document.all.thumbnail.value=''
			document.all.pic1image.src='img/thumb-blank.jpg'
			document.all.pic2image.src='img/thumb-blank.jpg'
			document.all.thumbnailimage.src='img/thumb-blank.jpg'
		}
		
	if (document.all.projectlist.value!='0')
		{
			if (id==0)
				{
					http.open('GET','list.asp?mode=selectproject&id='+document.all.projectlist.value)
					http.onreadystatechange = HttpDownload;
					http.send(null)						
				}
			if (id==1)
				{
					document.all.projecttype.value=nthfield(st,"|",1)
					document.all.projectname.value=nthfield(st,"|",2)
					document.all.client.value=nthfield(st,"|",3)
					document.all.projectdetail.value=nthfield(st,"|",4)
					document.all.typeofwork.value=nthfield(st,"|",5)
					document.all.year.value=nthfield(st,"|",6)
					document.all.jobvalue.value=nthfield(st,"|",7)
					document.all.pic1.value=nthfield(st,"|",8)
					document.all.pic2.value=nthfield(st,"|",9)
					document.all.thumbnail.value=nthfield(st,"|",10)
					pickpicture(1)
					pickpicture(2)
					pickthumbnail()
					
				}
		}
}

function selectnews(id,st)
{
	fld='selectnews'
	if (document.all.newsheadlines.value=='0') 
		{
			document.all.headline.value=''
			document.all.content.value=''
			document.all.dd.value=''
			document.all.mm.value=''
			document.all.yy.value=''
			document.all.newslink.value=''
			document.all.filelist.value=''
			
		}
		
	if (document.all.newsheadlines.value!='0') 		
		{
			if (id==0)
			{
				http.open('GET','list.asp?mode=selectnews&id='+document.all.newsheadlines.value)
				http.onreadystatechange = HttpDownload;
				http.send(null)					
			}
			
			if (id==1)
			{
				document.all.headline.value=nthfield(st,"|",1)
				document.all.content.value=nthfield(st,"|",2)
				dte=nthfield(st,"|",3)
				document.all.dd.value=dte.charAt(0)+dte.charAt(1)
				document.all.mm.value=dte.charAt(3)+dte.charAt(4)				
				document.all.yy.value=dte.charAt(6)+dte.charAt(7)+dte.charAt(8)+dte.charAt(9)
				document.all.newslink.value=nthfield(st,"|",4)
				document.all.filelist.value=''
				if (nthfield(nthfield(st,"|",4),"/",1)=='uploads')
					{
						document.all.filelist.value=nthfield(nthfield(st,"|",4),"/",2)
					}
				
			}
		}
}
function getnews(id,st)
{
	fld='getnews'
	if (id==0)
		{
			
			http.open('GET','list.asp?mode=getnews')
			http.onreadystatechange = HttpDownload;
			http.send(null)		

		}
	if (id==1)
		{
			n=1
			document.all.newsheadlines[document.all.newsheadlines.length]=new Option('New news item','0')
			while (nthfield(st,"|",n)!='')
				{	
					document.all.newsheadlines[document.all.newsheadlines.length]=new Option(nthfield(nthfield(st,"|",n),"~",1),nthfield(nthfield(st,"|",n),"~",2))
					n++
				}
			
			
			document.all.newsheadlines.selectedIndex=document.all.newsheadlines.length-1
			getfiles(0,'')
		}
}

function nthfield(st,dl,n)	
{
    var rt = ""
    var idx = 1
	var m=0
	for (m=0;m<st.length;m++)
		{
      		if ((idx == n) && (st.charAt(m)!=dl)) rt += st.charAt(m)
       		if (st.charAt(m)==dl) idx++
		}
	return rt
}

function getXMLHTTPRequest() {
try {
req= new XMLHttpRequest();
} catch (err1) {
  try {
  req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (err2) {
    try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (err3) {
	  	req = false;
  	}
  }

}
return req;  
}


var http= getXMLHTTPRequest();

function HttpDownload()
{	
	if (http.readyState == 4) 
	{
	 	if (http.status == 200) 
			{

				if (fld=='getnews')	getnews(1,http.responseText)

				if (fld=='selectnews') selectnews(1,http.responseText)
				if (fld=='selectproject') selectproject(1,http.responseText)
				
				if (fld=='deletenews') window.location=window.location
				if (fld=='savedetails') savedetails(1,http.responseText)
				if (fld=='getfiles') getfiles(1,http.responseText)
				if (fld=='getthumbnails') getthumbnails(1,http.responseText)
				if (fld=='getmainimages') getmainimages(1,http.responseText)
				if (fld=='getprojectlist') getprojectlist(1,http.responseText)
				if (fld=='saveprofile') saveprofile(1,http.responseText)
				if (fld=='deleteproject') window.location=window.location
				
			}
	}
}

