var doSearch=false;
// Get query string data and set as page variables
var sData = new Array();
var sTemp;
var sess = '';
var sQueryString = document.location.search.substr(1);
if(sQueryString!=""){
	var doubles =  sQueryString.split("&");
	for (var i = 0; i < doubles.length; i ++){
		sTemp = doubles[i].split("=");
		window[sTemp[0]] = sTemp[1];
		if((sTemp[0]=='act')&&(sTemp[1]=="search")) {doSearch=true;}
		if((sTemp[0]=='act')&&(sTemp[1]=="absconded")) {doSearch=true;}
		if (doubles[i].indexOf('PHPSESSID')!=-1){
			sess = doubles[i];
		}
	}
}

// Browser detection
var b=navigator.userAgent.toLowerCase();
n_ie_win=((b.indexOf('msie')>0)&&(b.indexOf('windows')>0))?true:false;
n_ie_mac=((b.indexOf('msie')>0)&&(b.indexOf('mac')>0))?true:false;
n_ff_win=((b.indexOf('firefox')>0)&&(b.indexOf('windows')>0))?true:false;
n_ff_mac=((b.indexOf('firefox')>0)&&(b.indexOf('mac')>0))?true:false;
n_s_mac=((b.indexOf('safari')>0)&&(b.indexOf('mac')>0))?true:false;


// Set local domain (dynamically)
//var myDomain=(n_ff_win)?"http://217.199.184.118/top-test.com" : "http://"+window.location.host;
var myDomain="http://"+window.location.host;
//var myDomain="http://217.199.184.118/top-test.com";


//Send search data to search script within Iframe.
/*
window.onload=function(){
	if(doSearch){
		var theFrame=window.frames.length-1;
		switch(act){
			case 'search':			window.frames[theFrame].document.location=myDomain+"/php/search.php?search="+search+"&county="+county+"&type="+type;break;
			case 'absconded':	gowhere=(link=='limited_records.php')?myDomain+"/php/"+link+"?reg="+reg:myDomain+"/php/"+link+"?id="+id;window.frames[theFrame].document.location=gowhere;break;
			default:					break;
		}
	}
	if(!(n_ie_win)||(n_ie_mac)){
		if (document.forms.length>0){
			for (i=0; i < document.siteform.elements.length;i++ ) {
				if (document.siteform.elements[i].type=='') {
					document.siteform.elements[i].type='text';
				}
			}
		}
	}
}
*/

// Member authentification - set cookie and send to members page (if cookie not present they will be denied access)
var ckie="";
if(document.cookie){
	ckie=document.cookie.substring(document.cookie.indexOf('ldat='),document.cookie.length);
}

if((document.location.pathname.indexOf("LI_")!=-1)&&(ckie.indexOf("ldat=")==-1)){
	//document.location="http://www.top-service.co.uk";
}

function goTop() {
	document.location='#';
}

function goHome()  {
	document.location=myDomain;
}


// Image Pop-up (onclick) // Also used for gallery images
function popUp(page, winname, width, height, text ){
	var theWidth=(text!="")?width+20:width;
	var theScroll=(theWidth==width)?'no':'yes';
	var imageWindow=window.open('about:blank', winname, 'width='+theWidth+', height='+height+',scrollbars='+theScroll);
	imageWindow.document.write('<body style="padding:0px;margin:0px">');
	imageWindow.document.write('<img align="left" style="margin-right:10px;" src='+page+' border=0>');
	var theText="</body>";
	if(text) theText=(text!="")?'<div style="font-family:arial;font-size:11px;color:black">'+text+'</div></body>':'</body>';
	imageWindow.document.write(unescape(theText));
	imageWindow.document.close();
}

// Image Rollovers
function chngImOver(image1, id){
	document.images[id].src=myDomain+"/images/"+image1;
}

function chngImOut(image2, id){
	document.images[id].src=myDomain+"/images/"+image2;
}

// Gallery - open large image window
function showIm(image){
	window.open("viewimage.html?image="+image, "_imageview");
}

// Gallery - Move through pages and re-populate the Gallery table
function chgIms(page){
	var siteurl = myDomain+"/images/";
	page = (page == '' || page == 'NULL' || !(page)) ? 0 : page;
	var totalpages=(theGallery.length%limit==0)?theGallery.length/limit:Math.floor(theGallery.length/limit)+1;
	for (i=0;i < limit ; i++){
		var arPos=page*limit+i;
		if(!theGallery[arPos]){
			document.all['space_'+i].innerHTML = '';
		}
		else{
			document.all['space_'+i].innerHTML = '<a href="#" onclick="popUp(\''+siteurl+theGallery[arPos]['image_'+arPos+'_large']+'\',\'Gallery_image\','+theGallery[arPos]['image_'+arPos+'_width']+','+theGallery[arPos]['image_'+arPos+'_height']+',\''+theGallery[arPos]['image_'+arPos+'_text']+'\');return false;"><img src="'+siteurl+theGallery[arPos]['image_'+arPos+'_thumb']+'" border="0"></a>';		
		}
	}
	pagelinks = "<font face='arial' size='2'>Page </font>";
	for (k=1;k <= totalpages ;k++ ){
		var arraynum = k - 1;
		pagelinks += (arraynum == page) ? '<a href="#" onclick="chgIms('+arraynum+');return false;">['+k+']</a>&nbsp;&nbsp;' : '<a href="#" onclick="chgIms('+arraynum+');return false;">'+k+'</a>&nbsp;&nbsp;';
	}
	document.all.pages.innerHTML = pagelinks;
}

// check links and add session data
function chkLinks() {
	for (i=0;i<=document.links.length-1;i++){
		if ((document.links[i].host.indexOf(document.location.host)!=-1)&&(sess!='')){
			document.links[i].href+= ((document.links[i].pathname.indexOf("display.php")!=-1)||(document.links[i].pathname.indexOf("reserve.php")!=-1)) ? "&"+sess : "?"+sess;
		}
	}
}

// Functionality for Drop Down Menus
function show(id){
	document.getElementById(id).style.display='block';
}
function hide(id){
	document.getElementById(id).style.display='none';
}

// Change the src of a specific image
function chngIm(newSrc,id){
	document.getElementById(id).src=newSrc
}

// Change tabs on search results
function swapTab(which, id){
	for(i in tabs){
		if(document.getElementById("tab_"+tabs[i]+"_"+id).className!="nodatatab"){
			document.getElementById("tab_"+tabs[i]+"_"+id).style.borderBottomColor="white";
			document.getElementById("tab_"+tabs[i]+"_"+id).style.borderStyle="groove";
			document.getElementById("tab_"+tabs[i]+"_"+id).className="hidetab";
			document.getElementById(tabs[i]+"_"+id).style.display="none";
		}
	}
	document.getElementById(which+"_"+id).style.display="inline";
	document.getElementById("tab_"+which+"_"+id).style.borderBottomColor="#eeeeee";
	document.getElementById("tab_"+which+"_"+id).className="showtab";
}

// Change tabs on Directors List results
function showRow(which){
	var tds=document.getElementsByTagName('td');
	for(i=0;i<tds.length;i++){
		if(tds[i].type!=undefined) {tds[i].style.display="none";}
		if(tds[i].type==which) {tds[i].style.display="inline";}
	}
	for(i=0;i<tds.length;i++){
		if(tds[i].tabtype!=undefined) {
			tds[i].style.borderBottomColor="white";
			tds[i].style.borderStyle="groove";
			tds[i].style.textDecoration="underline";
			tds[i].className="hidetab";
		}
		if(tds[i].tabtype==which) {
			tds[i].style.borderBottomColor="#eeeeee";
			tds[i].style.textDecoration="none";
			tds[i].className="showtab"
		}
	}
}

// get ICC reports
function getReport(which){
	document.siteform.action=myDomain+"/php/report.php"
	//document.siteform.target='_self';
	document.getElementById('id').value=reg_id
	return true;
}

function checkForm(which){
	var enter=false;
	if(which!="licontact"){
		if(document.getElementById('usr').value!=""){
			enter=true;
			if(document.getElementById('pswrd').value==""){
				alert('Please enter your password.');
				return false;
			}
		}
		if(document.getElementById('pswrd').value!=""){
			enter=true;
			if(document.getElementById('usr').value==""){
				alert('Please enter your username.');
				return false;
			}
		}
		if(enter){
			document.siteform.action=myDomain+'/php/userlogin.php';
			return true;
		}
	}
	var chkList;
	switch(which){
		case 'callmeback':			chkList=new Array('Name','Telephone');break;
		case 'nlicontact':				chkList=new Array('Name');break;
		case 'licontact':				chkList=new Array('Name');break;
		case 'debtrecov':			chkList=new Array('Your_Name');break;
		case 'submitinfo':			chkList=new Array('Address','Trade_Name','Amount');break;
		case 'testimonials':			chkList=new Array('Name','Testimonial');break;
		case 'account':				chkList=new Array('company_name','telephone', 'email');break;
		case 'internetaccess':		chkList=new Array('Name', 'Company_Name', 'Email');break;
		case 'signup':					chkList=new Array('Name', 'Company_Name', 'Telephone', 'Email', 'Password');break;
		case 'free_demo':			chkList=new Array('Email','telephone','heard');break;
		default:							return false; break;
	}
	var msg="";
	for(i in chkList){
		if(document.siteform[chkList[i].toLowerCase()].value=="") msg+="Please complete the field: "+chkList[i].replace(/_/," ")+"\n";
		if(chkList[i]=='email' && !validEmail(document.siteform['email'].value)){
			msg+="The email address entered is not a valid one.\n";
		}
	}
	if(msg!=""){
		alert(msg);
		return false;
	}else return true;
}

function validEmail(email) {
	invalidChars = " /:,;"

	if (email == "") {					
		return false
	}
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)			
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {	
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {					
		return false
	}
	if (periodPos+3 > email.length)	{
		return false
	}
	return true
}

function checkPass(){
	var msg="";
	if(document.chgpass.oldpass.value=="") msg+="Please enter your Old password\n";
	if(document.chgpass.newpass.value=="") msg+="Please enter your New password\n";
	if(document.chgpass.newpass2.value=="") msg+="Please confirm your New password\n";
	if(msg!=""){alert(msg);return false;}
	return true;
}

function randImage(){
	index = Math.floor(Math.random() * randList.length);
	document.write("<img src='"+myDomain+"/images/"+randList[index].image+"' alt='"+unescape(randList[index].text)+"' border='0' />");
	document.close();
}

function exportWin(type, name){
	var inputboxes = '';
	var doc=window.document;
	var reload = '';

	if ((type=="savePDF" && name!="ICC") || type=="print" || type=="saveHTML"){
		if(doc.getElementById('tsdetails'))doc.getElementById('tsdetails').style.display="inline";
		if(doc.getElementById('swording'))doc.getElementById('swording').style.display="inline";
		if(doc.getElementById('icclist'))doc.getElementById('icclist').style.display="none";
		if(name=="decision4"||name=="ccj"){
			if(doc.getElementById('print_but'))doc.getElementById('print_but').innerHTML="";
			//doc.getElementById('topim').src=myDomain+"/xml-xslt/extras/reportheader3_print.gif";
			var num = 1;
			while (doc.getElementById('arrow_'+num)){
				doc.getElementById('arrow_'+num).innerHTML="";
				num++;
			}
		}
	}
	var count = 1;
	while (doc.getElementById('score_im_'+count)){
		doc.getElementById('score_im_'+count).innerHTML="";
		count++;
	}
	if (doc.getElementById('tsdetails')){
		doc.getElementById('tsdetails').style.display="inline";
	}

/*
	if(type=="savePDF" && name!="ICC"){
		if (doc.getElementById('inputboxes'))
		{
			doc.getElementById('inputboxes').outerHTML="";
		}
		
	}
	else{
		if (doc.getElementById('inputboxes'))
		{
			doc.getElementById('inputboxes').style.display="none";
		}
		
	}
	*/
	if (doc.getElementById('inputboxes'))
		{
			inputboxes = doc.getElementById('inputboxes').innerHTML;
			doc.getElementById('inputboxes').innerHTML="";
		}

	var tds=doc.getElementsByTagName("td");
	for(i in tds){
		if(tds[i].className=="tableheader"){
			tds[i].className="printheader";
		}
		if(tds[i].className=="tablecell"){
			tds[i].className="printcell";
		}
	}
	var trs=doc.getElementsByTagName("tr");
	for(i in trs){
		if(trs[i].className=="qlinks"){
			trs[i].style.display="none";
		}
	}
	var tab=doc.getElementsByTagName("table");
	for(i in tab){
		if(tab[i].className=="noborder"){
			tab[i].className="border";
		}
		if(tab[i].className=="qlinks"){
			tab[i].style.display="none";
		}

	}
	var as=doc.getElementsByTagName("a");
	for(i in as){
		if(as[i].style){
			as[i].style.color="black";
		}
	}
	switch(type){
		case 'print':		if (name=='ICC') { window.print(); } break;
		case 'saveHTML':	doc.write("<html>\n<head>\n<title>"+name+"</title>\n</head>\n<body>\n"+doc.getElementById('right').innerHTML+"\n</body></html>");
									doc.close();
									var thepage=name.replace(/\s/g,"_");
									doc.execCommand('SaveAs',true,'TS_'+thepage+'.htm');
									
									break;
		case 'savePDF':		doc.getElementById('html_source').value="<html>\n<head>\n<title></title>\n</head>\n<body>\n"+doc.getElementById('right').innerHTML+"\n</body></html>";
									doc.getElementById('convert').submit();

									if (doc.getElementById('inputboxes')) {
										doc.getElementById('inputboxes').innerHTML=inputboxes;
									}
									if (doc.getElementById('tsdetails')){
										doc.getElementById('tsdetails').style.display="none";
									}
									
									var tds=doc.getElementsByTagName("td");
									for(i in tds){
										if(tds[i].className=="printheader"){
											tds[i].className="tableheader";
										}
										if(tds[i].className=="printcell"){
											tds[i].className="tablecell";
										}
									}
									var trs=doc.getElementsByTagName("tr");
									for(i in trs){
										if(trs[i].className=="qlinks"){
											trs[i].style.display="";
										}
									}
									var tab=doc.getElementsByTagName("table");
									for(i in tab){
										if(tab[i].className=="border"){
											tab[i].className="noborder";
										}
										if(tab[i].className=="qlinks"){
											tab[i].style.display="";
										}
									}
									break;

		default:					break;
	}

}

function printICC(which) {
	var doc=window.document
	doc.getElementById('print_but').innerHTML="";
	//doc.getElementById('topim').src=myDomain+"/xxxml/extras/reportheader3_print.gif";
	if (which == 'decision4')
	{
		var num = 1;
		while (doc.getElementById('arrow_'+num))
		{
			doc.getElementById('arrow_'+num).innerHTML="";
			num++;
		}
	}
	window.print();
}

function CallPrint(strid)
{
var prtContent = document.getElementById(strid);

var WinPrint = window.open('','','left=0, top=0,toolbar=0,status=0, resize=1, scrollbars=1');
WinPrint.document.open();
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();

var doc=WinPrint.document;
doc.getElementById('print_but').innerHTML="";

var num = 1;
while (doc.getElementById('arrow_'+num))
{
	doc.getElementById('arrow_'+num).innerHTML="";
	num++;
}

/*
var tds=doc.getElementsByTagName("td");
for(i in tds){
	if(tds[i].className=="tableheaderdark" || tds[i].className=="tableheaderlight"){
		tds[i].className="printheader";
	}
	else if(tds[i].className=="textheader" || tds[i].className=="textlarge" || tds[i].className=="textlargelight" || tds[i].className=="textmediumlight"  || tds[i].className=="tableheaderlight" ){
		tds[i].className="printcell";
	}
	else {
		tds[i].className="printcell";
	}
}
*/

WinPrint.print();
//WinPrint.close();
//prtContent.innerHTML=strOldOne;
}


function printRecord() {
  var divToPrint=document.getElementById('print-area');
  newWin= window.open("");
  newWin.document.write(divToPrint.outerHTML);
	var doc = newWin.document;
	var tds=doc.getElementsByTagName("td");
	for(i in tds){
		if(tds[i].className=="tableheader"){
			tds[i].className="printheader";
		}
		if(tds[i].className=="tablecell"){
			tds[i].className="printcell";
		}
	}
	var trs=doc.getElementsByTagName("tr");
	for(i in trs){
		if(trs[i].className=="qlinks"){
			trs[i].style.display="none";
		}
	}
	var tab=doc.getElementsByTagName("table");
	for(i in tab){
		if(tab[i].className=="noborder"){
			tab[i].className="border";
		}
		if(tab[i].className=="qlinks"){
			tab[i].style.display="none";
		}

	}
	var as=doc.getElementsByTagName("a");
	for(i in as){
		if(as[i].style){
			as[i].style.color="black";
		}
	}
  //newWin.print();
 // newWin.close();
}


function setUrl(){
	document.getElementById('backlink').value=escape(document.location);
	return true;
}

function go(num){
	if((n_ff_mac)||(n_ff_win)||(n_s_mac))history.go(num+2);
	else history.go(num);
}


function gloss(anc) {
	window.open(myDomain+"/Glossary.html#"+anc, "_gloss",'width=790,height=325,left=5,top=5,scrollbars=yes');

}

function chkNum(which){
	if(isNaN(document.getElementById(which).value)){
		alert('Please only enter numbers in the Amount box');
		document.getElementById(which).value=(isNaN(parseFloat(document.getElementById(which).value)))?"":parseFloat(document.getElementById(which).value);
		document.getElementById(which).focus();
		return false;
	}
	return true
}