var bbTagFlag = new Array();

function IsIE() {

  return isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1)); 
}
  
function gotoEdit(o,id) {
//only submit if no named object was clicked on. this means the user can click on the checkboxes
var s 

  s = document.activeElement.name
	if (s=="") { s = null;}
	if (s!=null) { if (s.substring(0,3) != "chk") { s = null } }
		
	if (s==null) {
    search.action.value = 'edit';
	  search.dataid.value=id;
  	search.submit();
  }
}

function getCheckedItemsList(form, sChkPrefix) {
//returns a comma delimited string array of integer values of ticked checkboxes in the given form
var sList = ""
var o
var i
var count = 0;
 
 for (i=0;i<form.elements.length;i++) {
   o = form.elements[i];

	 if (o.name.substring(0,3)==sChkPrefix) {
	   if (o.checked) { 
		   sList = sList + o.name.substring(3) + ","; //NB: don't put a space here!
			 		 count +=1;
		 }
	 }
	} 

	this.idlist = sList;
	this.length = count;
}


function handleDelete() {
var l = 0
var sList
var f
var o

  o = new getCheckedItemsList(search,"chk");
	
	if (o.length==0) {
	  alert("Please tick the items you want to delete!");
	} else {
	  if (o.length==1) 
		  f = (confirm("Are you sure you want to delete this item?"))
		else
		  f = (confirm("Are you sure you want to delete these " + o.length + " items?"));
			
	 if (f) {
	   search.action.value = "delete"
		 search.ids.value = o.idlist;
		 search.submit();
	 }		
	}	
}

function checkPwd(o1, o2) {
//returns true if the given passwords match, displays a prompt if they don't

  if (o1.value != o2.value) {
	  alert("The passwords do not match!");
		return false;
	} else {
	  return true;
	}
	
}

function mailDrop_Continue() {
var o
  
  if (maildropcontinue.mailhistoryid.value == 0) {
	  alert("Please select a mail drop from the list!");
		return false;
	} else {
	  maildropcontinue.finish.disabled=true;
    return true;
	}		

}

function mailDrop_Step1() {
var o

  if (maildrop.mailcontentid.value == 0) {
	  alert("Please select a message body from the list!");
		return false;
	} else {
    o = new getCheckedItemsList(maildrop,"chk");

	  if (o.length==0) {
		  alert("Please select at least 1 recipient group!");
		  return false;
		} else {
		  maildrop.recipgrouplist.value = o.idlist;
	    return true;
		}
	}		

}

function mailDrop_Step2() {

  if (maildrop.recipients.value == "") {
    alert("Please specify some recipients!");
	  return false;
 } else {
   if (maildrop.replyaddress.value == "") {
	   alert("Please specify the reply address!");
		 return false;
		} else { 
     return true;
		} 
	} 
 
}

function handleBBMarkup(o, btn, tagOpen, tagClose) {
var o2 = document.selection.createRange();
	
	if (o2.text!="") {
	  o2.text = "[" + tagOpen + "]" + o2.text + "[/" + tagClose + "]"
	}	else {
	  //nb javascript in ie converts to lower case!
    if (btn.style.borderColor!="#ff0000") {
		  //change to red
		  o.value += "[" + tagOpen + "]";
			btn.style.borderColor= "#ff0000" 
		} else {
		  //change to blue
		  btn.style.borderColor = "#5C709E"
		  o.value += "[/" + tagClose + "]";
		}	
	}	
	
  o.focus();
}

function checkSQL(sql) {
//if the user is executing a select statment, check if a top clause is included and warn if it isn't
var f = true;

  if (sql.toLowerCase().indexOf("select")==0) {
	  if (sql.toLowerCase().indexOf("select top ")!=0) {
	    if (!confirm("You have a SELECT statement without a TOP clause - are you sure you want to continue?")) {
			  f = false;
			}
		}
	}
	
	return f;
}

function getTableList(form) {
var o = new getCheckedItemsList(form,"tbl");
 
  return o.idlist;
}

function checkDisable(f,form) {
var i;
var o;

  for (i=0;i<form.elements.length;i++) {
   o = form.elements[i];

	 if (o.name.substring(0,3)=="tbl") {
	   o.checked = f;
		 o.disabled = f;
	 }
	} 
}

function checkReset() {

  if (confirm("Are you sure you want to do this?")) {
	  if (confirm("Are you really sure you want to do this? This will reset all administration data!")) {
		  return true;
		} else {
		  return false;
		}
	} else {
	  return false;
	}			
}

function showPopupMenu(name) {
var o = document.all.item("pm_" + name);

  o.style.visibility='hidden';
	o.style.left = event.x+document.body.scrollLeft;
	o.style.top = event.y+document.body.scrollTop;
	o.style.visibility='visible';
	
}

function hidePopupMenus() {
var i;
var o;
 
 for (i=0;i<document.all.tags("TABLE").length;i++) {
   o = document.all.tags("TABLE")[i];
	 
	 if (o.className=="popup_menu") {
	   if (o.id!=null) {
		   if (o.id.substring(0,3)=="pm_") {
			   o.style.visibility="hidden";
				}
			}
		}
 }
 
  return true;
}

function performAction(action) {
var o =  new getCheckedItemsList(search,"chk");

  if (o.length==0) {
	  alert("You must tick the items you want to apply this action to!");
	} else {	
	  search.ids.value = o.idlist;
  	search.action.value = "action";
	  search.dataid.value = action;
  	search.submit();
	}	
	
}

function checkHasValue(o,caption) {

  if (o.value=="") {
	  alert("Please fill in " + caption);
		return false;
	} else {
	  return true;
	}
			
}

function checkEmailAddr(o) {
var v;

 v = o.value;
 
 if (v=="") {
   alert("Please fill in an email address!") 
	 return false;
 }	
 
 i = v.indexOf("@")
 
 j = v.lastIndexOf(".")
 
 if ((i<1) || (j<=i+1) || (j==v.length-1) || (v.indexOf(" ")>-1)) {
   alert("Please fill in a valid email address!") 
	 return false;
 } else {
   return true;
 } 	 
 
}

function showBookInfo(bno) {
  //change this to a popup window
//var v = new window;

	w = window.open("bookdetails.asp?bno="+bno,"",
	                "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,"+
				     			"resizable=no,copyhistory=no,width=500,height=400");
  w.focus();
  
  return false;
	
}

function openNew(href) {

	w = window.open("openlink.asp?url="+href,"")
  w.focus();
	
}

var uploadIsOpen
var uploadDir
var uploadFile
var uploadDisp
var uploadPreview
var uploadWnd

function handleImgUpload(dir,curfile,dispfile,preview) {

 if(uploadIsOpen) {
   alert("Please close the upload window you currently have open first!");
	 uploadWnd.focus();
	 return false;
 }

 uploadDir = dir;
 uploadFile = curfile;
 uploadDisp = dispfile;
 uploadPreview = preview;

 uploadWnd = window.open("upload.asp?dir="+encodeURI(dir)+"&file="+ curfile.value, "upload",
                         "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,"+
				                 "resizable=no,copyhistory=no,width=300,height=200");
 uploadWnd.focus();

}

function setr(o) { //raise
	o.className = "imgR";
}

function setn(o) { //normal
	o.className = "imgN";
}

function setd(o) { //drop
	o.className = "imgD";
}

function addPollAnswer(parent, index, id, answer) {
var thisName = parent.id+'_'+index;
var nextName = parent.id+'_'+(index+1);
var lastName;

 //can't have -1 as part of the name - JScript returns NaN
 if(index==0) {lastName = parent.id+'_0'} else {lastName = parent.id+'_'+(index-1)}

 parent.innerHTML += 
   '<div style="position:relative;overflow:hidden;height:23px">' +
   '<input type="text" style="width:480px;" value="'+answer+'" name="'+thisName+'" onkeydown="if(event.keyCode==13){slAppend(event,document.all.'+parent.id+',this)}" onchange="slAppend(event,document.all.'+parent.id+',this)">&nbsp;' +
   '<input type="hidden" value="'+id+'" name="'+thisName+'_id">&nbsp;' +
   '<img style="position:relative;top:4px" height="16" src="../images/site/move_up.gif" class="imgN" onmouseover="setr(this)" onmousedown="setd(this)" onmouseout="setn(this)" onmouseup="setr(this)" onclick="slSwap(\''+lastName+'\',\''+thisName+'\')">&nbsp;' +
   '<img style="position:relative;top:4px" height="16" src="../images/site/move_down.gif" class="imgN" onmouseover="setr(this)" onmousedown="setd(this)" onmouseout="setn(this)" onmouseup="setr(this)" onclick="slSwap(\''+thisName+'\',\''+nextName+'\')">&nbsp;' +
   '<img style="position:relative;top:4px" height="16" src="../images/site/delete.gif" class="imgN" onmouseover="setr(this)" onmousedown="setd(this)" onmouseout="setn(this)" onmouseup="setr(this)" onclick="slDelete(\''+thisName+'\')">&nbsp;' +
   '</div>'
}

/*SUB LIST FUNCTIONS*/
function slAppend(evt, parent, current) {
//check if there's any text in the last item, if there is, add another item
var baseName = current.name.substring(0, current.name.length-1);
   
 i=99;
 while (!document.all[baseName+i]) i-=1;
 
 //while (document.all[baseName+i].style.visibility=='hidden') i-=1;

 if (document.all[baseName+i].value!='') {
   addPollAnswer(parent,i+1,0,'');
 }
 
}

function slSwap(i1, i2) {
var o1 = document.all[i1];
var o2 = document.all[i2];
var v;

  if((o1)&&(o2)) {
    //swap the text around
    v = o1.value;
    o1.value = o2.value;
    o2.value = v;
    
    //swap the id's around
    o1 = document.all[i1+'_id'].value;
    o2 = document.all[i2+'_id'].value;
    
    v = o1.value;
    o1.value = o2.value;
    o2.value = v;
  }
  
}

function slDelete(item) {

 document.all[item].value = '';
  
}

function orderBook(bno, qs) {
  
  //changed 23/05/2006
  window.location = "default.asp?orderbno="+bno+"&"+qs;
  
}

function showCat(letter,by) {

  if(IsIE){
    search.by.value = by;
  	search.booktext.value = letter;
	  search.page.value=1;
	  search.submit();
	} else {
	  document.getElementById("by").value = by;
  	document.getElementById("booktext").value = letter;
	  document.getElementById("page").value=1;
	  document.getElementById("search").submit();
	}  

}

function gotoPage(num) {
  if(IsIE) {
    alert(search.by.value);
    search.page.value = num;
    search.submit();
  } else {  
    document.getElementById("page").value = num;
    document.getElementById("search").submit();
  }  
}

function handleCancelOrder() {

  if(confirm("Are you sure you want to cancel this order?")){
	  order.action.value='cancelorder';
		order.submit()
	}
	
}

function handlePlaceOrder() {

  if(order.memname.value.length<=2){
	  alert("Please fill in your name!");
		return false;
	}
	
	if(!checkEmailAddr(order.mememail)){
	  return false //checkEmailAddr displays its own alerts
	}	
	
	order.update.value=1
	order.action.value="placeorder";
	order.submit();
	
}

function checkFeedback() {
//alert("adfd");


  if(!checkEmailAddr(contact.email)) return false;
	
	if(contact.message.value=="") {
	  alert("Please type a message!");
		return false;
	}
	
	return true;

}
