var datachanged = false;
function promptSave(todo){
	if (!datachanged) { 
		eval(todo);
	}else{
		if(confirm('You have changed information on this page but have not updated it.\n\nTo save your changes, click Cancel to this message and then click Update.\n\nTo Continue and lose your changes click OK')){
		eval(todo);
		}
	}
}
function chkSubForm(){
	f = document.frmSub;
	if (f.tc.checked == false){
		alert('You must agree to the terms and conditions')
		return;
	}
	f.submit();
}
function changeInvOrder(iDisp){
	iVar = document.frmNav.invOrder.options[document.frmNav.invOrder.selectedIndex].value
	self.location.href='Invoices.asp?disp='+ iDisp + '&sort=' + iVar
}
function changeCustOrder(iDisp){
	iVar = document.frmNav.custOrder.options[document.frmNav.custOrder.selectedIndex].value
	self.location.href='Customers.asp?disp='+ iDisp + '&sort=' + iVar
}
function changeSupOrder(iDisp){
	iVar = document.frmNav.supOrder.options[document.frmNav.supOrder.selectedIndex].value
	self.location.href='Suppliers.asp?disp='+ iDisp + '&sort=' + iVar
}
function changeSourceOrder(iDisp){
	iVar = document.frmNav.sourceOrder.options[document.frmNav.sourceOrder.selectedIndex].value
	self.location.href='Sources.asp?disp='+ iDisp + '&sort=' + iVar
}
function changeInvDisp(iSort){
	iVar = document.frmNav.invDisp.options[document.frmNav.invDisp.selectedIndex].value
	self.location.href='Invoices.asp?disp=' + iVar + '&sort=' + iSort
}

function changeRecDisp(iSort){
	iVar = document.frmNav.recDisp.options[document.frmNav.recDisp.selectedIndex].value
	self.location.href='Receipts.asp?disp=' + iVar + '&sort=' + iSort
}
function changeRecOrder(iDisp){
	iVar = document.frmNav.recOrder.options[document.frmNav.recOrder.selectedIndex].value
	self.location.href='Receipts.asp?disp='+ iDisp + '&sort=' + iVar
}
function delInvLine (lineid, invid){
	if (confirm('Are you sure you want to delete this item from the invoice?')){
		self.location.href='viewInvoice.asp?id=' +  invid + '&do=delline&lineid=' +lineid;
	}
}
function delRecInvLine (lineid, invid){
	if (confirm('Are you sure you want to delete this item from the invoice?')){
		self.location.href='editRecurringInvoice.asp?id=' +  invid + '&do=delline&lineid=' +lineid;
	}
}
function delRecInv (invid){
	if (confirm('Are you sure you want to delete this recurring invoice?\n\nInvoices that have already been generated as a result of it will\nnot be effected but no new invoices will be generated as a result\nof this recurring invoice.')){
		self.location.href='RecurringInvoices.asp?id=' +  invid + '&do=delrec';
	}
}
function deleteProduct (iCTid){
	if (confirm('Are you sure you want to delete this product/service?\n\nIf you proceed then all invoice items previously\nassigned to the category will be reset to \'Other\'.')){
		self.location.href='products.asp?id=' +  iCTid + '&do=delprod'
	}
}
function deletePaymentMethod (iPMid){
	if (confirm('Are you sure you want to delete this Payment Method?\n\nIf you proceed then all receipt payments previously\nassigned to the category will be reset to \'Other\'.')){
		self.location.href='paymentmethods.asp?id=' +  iPMid + '&do=delpm'
	}
}
function deleteOutgoingType (iPMid){
	if (confirm('Are you sure you want to delete this Outgoing Type?\n\nIf you proceed then all receipt payments previously\nassigned to the category will be reset to \'Miscellaneous Expenses\'.')){
		self.location.href='outgoingtypes.asp?id=' +  iPMid + '&do=delpm'
	}
}

function jsHelp(i){
	window.open('jshelp.asp?id=' + i,'jsHelp','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=650,height=400,')
}
function deleteSource (iSrc, iCusts){
	if (iCusts > 0) {
		alert('You can\'t delete this source until you move all the customers listed to another source');
		return;
	}
	if (confirm('Are you sure you want to delete this source?')){
		self.location.href='Sources.asp?id=' +  iSrc + '&do=delsrc'
	}
}
function sourceEditCust(){
	varObj = document.frmSrc.sourceCust;
	var sel = false;
    for (i=0;i<varObj.options.length;i++)
    {
      var current = varObj.options[i];
      if (current.selected)
        {
          sel = true;
        }
    }
  
	if (sel){
		iVar = varObj.options[varObj.selectedIndex].value
		self.location.href='editCustomer.asp?id='+  iVar
	}else{
		alert('Please select a customer to view');
	}
}
function custViewInv(){
	varObj = document.frmCI.invoices;
	var sel = false;
    for (i=0;i<varObj.options.length;i++)
    {
      var current = varObj.options[i];
      if (current.selected)
        {
          sel = true;
        }
    }
  
	if (sel){
		iVar = varObj.options[varObj.selectedIndex].value
		if (iVar > 0) {self.location.href='viewInvoice.asp?id='+  iVar}
	}else{
		alert('Please select an Invoice to view');
	}
}

function supViewReceipt(){
	varObj = document.frmSI.receipts;
	var sel = false;
    for (i=0;i<varObj.options.length;i++)
    {
      var current = varObj.options[i];
      if (current.selected)
        {
          sel = true;
        }
    }
  
	if (sel){
		iVar = varObj.options[varObj.selectedIndex].value
		if (iVar > 0) {self.location.href='viewReceipt.asp?id='+  iVar}
	}else{
		alert('Please select a receipt to view');
	}
}


function chkInvLine(){
	var x = document.frmInvView
	if (!isNumeric(x.qty.value) || x.qty.value.length==0) {
		alert('You must enter numbers only in the box for Quantity.\n\nFor Example, if you are selling three items for £100 each\nthen enter 3 as the Quantity and 100 as the rate.\n\nIf you want to use this line for a comment rather than an\nitem then set both the Qty and Rate to 0.');
		return;
	}
	varRate = x.rate.value
	if (!isNumeric(varRate.replace('-','')) || varRate.replace('-','').length==0) {
		alert('You must enter numbers only in the box for Rate.\n\nFor Example, if you are selling three items for £100 each\nthen enter 3 as the Quantity and 100 as the rate.\n\nIf you want to use this line for a comment rather than an\nitem then set both the Qty and Rate to 0.');
		return;
	}
	if (x.desc.value.length==0) {
		alert('You have not entered a description for the invoice item.');
		return;
	}
	if (x.desc.value.length > 900) {
		alert('The description you entered is too long, please shorten it.');
		return;
	}
	x.submit();	
}

function isNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
 }
 
 function editInvLine(invLine,invNum){
	window.open('editInvItem.asp?id=' + invNum + '&line=' + invLine,'editInvItem','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=720,height=80,')
 }
function updateInv(){
	if(confirm('Are you sure you want to update this invoice?')){document.frmInv.submit();}
}
function ciVATEnabler(){
	var f = document.frmCI;
	var boo =! f.vatregd.checked;
	f.vatregnum.disabled=boo;
}
function invPayEnabler(){
	var f = document.frmInvPay;
	var boo =! f.chkPaid.checked;
	f.paidmethod.disabled=boo;
	f.invPaidNote.disabled=boo;
}
function recPayEnabler(){
	var f = document.frmRec;
	var boo =! f.paid.checked;
	f.datepaid.disabled=boo;
	f.paidnote.disabled=boo;
	f.pm.disabled=boo;
}
function updateInvHeader(){
	if(confirm('Are you sure you want to change the details of this invoice?\n\nIf you have already printed it then you will\nneed to re-print it for your customer.')){
		document.frmInvDetails.submit();
	}
}
function updateRecInvHeader(){
	if(confirm('Are you sure you want to change the details of this recurring invoice?')){
		document.newInv.submit();
	}
}
function SrcFromSec(frmVar){
	self.location.href='editSource.asp?id=' + frmVar
}
function SrcFromSec2(frmVar, iInvoiced, iDate1, iDate2){
	self.location.href='editSource.asp?id=' + frmVar + '&issued=' + iInvoiced + '&date1=' + iDate1 + '&date2=' + iDate2 + '&rep=1'
}
function ProdFromSec(frmVar){
	self.location.href='editProduct.asp?id=' + frmVar
}
function ProdFromSec2(frmVar, iInvoiced, iDate1, iDate2){
	self.location.href='editProduct.asp?id=' + frmVar + '&issued=' + iInvoiced + '&date1=' + iDate1 + '&date2=' + iDate2 + '&rep=1'
}
function supFromSec(frmVar){
	self.location.href='editSupplier.asp?id=' + frmVar
}
function custFromSec(frmVar){
	self.location.href='editCustomer.asp?id=' + frmVar
}

function setDivState(divName, iDisp){
	if (iDisp==1) {
		document.getElementById(divName).style.display = "none"
	}
	else
	{
		document.getElementById(divName).style.display = "block"
	}
}
function showHideInvImageDiv() {
	if (document.frmIO.imageOnInvoice.checked == false) {
		setDivState('imageDetails',1)
	}
	else
	{
		setDivState('imageDetails',0)
	}
}

function showHideSaveEmailDiv() {
	if (document.frmEmail.emailaddress.value == document.frmEmail.origemail.value) {
		setDivState('saveCust',1)
	}
	else
	{
		setDivState('saveCust',0)
	}
}
function showHideSaveChangeDiv() {
	if ((document.frmEmail.subject.value != document.frmEmail.origsubject.value) || (document.frmEmail.message.value != document.frmEmail.origmessage.value)) {
		setDivState('saveEmail',0)
	}
	else
	{
		setDivState('saveEmail',1)
	}
}
function qJump(){
	iVar = document.frmNav.qjump.options[document.frmNav.qjump.selectedIndex].value
	if (iVar != 0){
	 self.location.href=iVar
	}
}

function printPage(sURL){
//	window.open(sURL,'jsHelp','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=200,height=200,')
frames['printFrame'].location.href =sURL
}

function changePW(){
	var x = document.frmPW;
	varSub = true;
	if (x.newpw1.value.length < 5) {
		alert('Your new password must be atleast 5 characters long.');
		varSub =false;
	}
	
	if (x.newpw1.value != x.newpw2.value) {
		alert('Your new passwords do not match.');
		varSub = false;
	}
	if (varSub) { x.submit();}
}
function changeMemWord(){
	var x = document.frmMemWord;
	varSub = true;
	if (x.newword1.value.length < 5) {
		alert('Your new memorable must be atleast 5 characters long.');
		varSub = false;
	}
	
	if (x.newword1.value != x.newword2.value) {
		alert('Your new memorable words do not match.');
		varSub = false;
	}
	if (varSub) {x.submit();}
}

function closeTicket(tID){
	if(confirm('Are you sure you want to close this request for help?\n\nDoing so will mean that the support team\nwill no longer be looking into this issue for you.')){
		self.location.href='viewTicket.asp?id=' + tID + '&action=close'
	}
}

function setListTo(id){
		var obj = document.frmRec.outtype;
		var optionCounter;
		for (optionCounter = 0; obj.options;optionCounter++){
		   if (obj.options[optionCounter].value == id) {
			   //alert (optionCounter);	
				obj.options[optionCounter].selected = true;
				break;
		   }
		}
  	
}

function setInvItemTo(proddesc,prodprice){
	document.frmInvView.desc.value=proddesc;
	document.frmInvView.rate.value=prodprice
	if (document.frmInvView.qty.value=='') { document.frmInvView.qty.value='1';}
}

function charOnly(){
				var pattern=/[a-zA-Z0-9]/g;
					if (!e) var e = window.event;
					if (e.keyCode) code = e.keyCode;
						else if (e.which) code = e.which;
					var character = String.fromCharCode(code);

				if (pattern.test(character) == false){
					alert("Only Characters Please\nOnly characters A-Z, a-z and digits 0-9 can be entered into this field.\nThank you.")
					return false
				}
				return true;
			}
function kCode(e){
if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if(code==13) {
		document.frmLogin.submit();
		return false;
	}else{
		return true;
	}
}
function noRemoveLogo(){
	document.frmIO.incLogox.checked=true;
	alert('You cannot remove the logo from your invoices whilst\nyou are using your free trial.\n\nOnce you upgrade to the paid-for service you\nwill be able to remove this logo')
}


function sentLetter(letid, iInvID){
	self.location.href='ReminderLetters.asp?do=send&letter=' + letid + '&id=' + iInvID;
}

function recurEnabler() {
	var f = document.newInv
	if (f.recurtype[0].checked){
		f.day.disabled=false;
		f.annualdate.disabled=true;
		f.datetrigger.disabled=true;
	}
	else
	{
		f.day.disabled=true;
		f.annualdate.disabled=false;
		f.datetrigger.disabled=false;
	
	}
}
function recurUntilEnabler() {
	var f = document.newInv
	if (f.invuntil[0].checked || f.invuntil[3].checked){
		f.invtickdie.disabled=true;
		f.invtodate.disabled=true;
	}
	else if (f.invuntil[1].checked){
		f.invtickdie.disabled=false;
		f.invtodate.disabled=true;
	}
	else
	{
		f.invtickdie.disabled=true;
		f.invtodate.disabled=false;
	}
}
function delRcpt(recnum){
	if (confirm('Are you sure you want to delete this receipt?\n\nThe receipt will be permanently deleted.')){
		self.location.href='receipts.asp?do=del&id=' + recnum
	}
}
function delTX(txnum){
	if (confirm('Are you sure you want to delete this transaction?\n\nThe transaction will be permanently deleted.')){
		self.location.href='bank.asp?do=del&id=' + txnum
	}
}
function delInvPay(invid, payid){
	if (confirm('Are you sure you want to delete this payment?')){
		self.location.href='payInvoice.asp?id=' + invid + '&delpayid=' + payid
	}	
}
function delRecPay(recid, payid){
	if (confirm('Are you sure you want to delete this payment?')){
		self.location.href='viewReceipt.asp?id=' + recid + '&delpayid=' + payid + '&do=payments'
	}	
}
function setPos(targetObj){
	var obj = document.getElementById('faq');
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	targetObj.style.left = curleft + 0 + 'px';
	targetObj.style.top = curtop + 0 + 'px';
}

function revealLayer(imageId) {
	//alert(imageId);
   //return 
   image = document.getElementById(imageId);
   setPos(image)
   setOpacity(image, 0);
   image.style.visibility = "visible";
   fadeIn(imageId,0);

}

function obscureLayer(imageId) {

   image = document.getElementById(imageId);
   setOpacity(image, 100);
   image.style.visibility = "visible";
   fadeOut(imageId,100);
}

function fadeIn(objId,opacity) {
   if (document.getElementById) {
               obj = document.getElementById(objId);
               if (opacity <= 100) {
                           setOpacity(obj, opacity);
                           opacity += 10;
 							window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
               }
   }
}

function fadeOut(objId,opacity) {
   if (document.getElementById) {
               obj = document.getElementById(objId);
               if (opacity >= 0) {
                           setOpacity(obj, opacity);
                           opacity -= 10;
							window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
               } else {obj.style.visibility = "hidden";}
  }
}

function setOpacity(obj, opacity) {

   opacity = (opacity == 100)?99.999:opacity;

   // IE/Win
   obj.style.filter = "alpha(opacity:"+opacity+")";

   // Safari<1.2, Konqueror
   obj.style.KHTMLOpacity = opacity/100;

   // Older Mozilla and Firefox
   obj.style.MozOpacity = opacity/100;

   // Safari 1.2, newer Firefox and Mozilla, CSS3
   obj.style.opacity = opacity/100;

}

 

function runLayers(x,i) {

   var layerList = document.getElementById(x).childNodes;

   if (i < layerList.length) {
               if (layerList[i].nodeName == 'P') {
			   		revealLayer(layerList[i].id );
			   }
               i++;
   } else {
           for (n=0 ; n < layerList.length ;n++){
		   		if (layerList[n].nodeName == 'P') {
					obscureLayer(layerList[n].id );
				}
		   }
           i = 0
   };

   window.setTimeout("runLayers('" + x + "'," + i + ")",5000);

}
function flipInfo(i){
	document.getElementById('info_btn1_ar').className = 'info_btn_noSel';
	document.getElementById('info_btn2_ar').className = 'info_btn_noSel';
	document.getElementById('info_btn3_ar').className = 'info_btn_noSel';
	document.getElementById('info_btn' + i + '_ar').className = 'info_btn_Sel';
	
	
	document.getElementById('infoTxt1').className = 'invisi';
	document.getElementById('infoTxt2').className = 'invisi';
	document.getElementById('infoTxt3').className = 'invisi';
	document.getElementById('infoTxt' + i).className = 'infoTxt';

}
function clearPrompt(obj, val){
	if (obj.value == val) {obj.value = '';}
}
function LaunchVideo(url, vtitle){
	window.open(url, 'title', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=775,height=540,')
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}
function saveUsername(){
	var username = document.getElementById('user').value
	Set_Cookie( 'rem', username, 60, '/', '', '' );
}
function tweetContents(obj){
	var txt = obj.innerHTML;
	txt = escape(txt);
	var url = 'http://twitter.com/home/?status=' + txt;
	self.location.href = url;
}
   
function showLoginBox(){
	$("fieldset#signin_menu").toggle();
	try {
		if (document.getElementById('user').value==''){
			document.getElementById('user').focus()
		}else{
			document.getElementById('pass').focus()
		}	
	}catch(er) {}
}
$(document).ready(function() {

            //$(".signin").click(function(e) {
            //    e.preventDefault();
            //    $("fieldset#signin_menu").toggle();
                //$(".signin").toggleClass("menu-open");
            //});
			if (document.getElementById('bigbtns_flv_tour')) {
				
				 $("#bigbtns_flv_tour").fancybox({ 
                                'ajax' : { 
                                        cache : false 
                                } 
                        }); 
				}
			if (document.getElementById('sidebar_tour')) {
			
			 $("#sidebar_tour").fancybox({ 
							'ajax' : { 
									cache : false 
							} 
					}); 
			}
            $("fieldset#signin_menu").mouseup(function() {
                return false
            });
            $(document).mouseup(function(e) {
                if($(e.target).parent("a.signin").length==0) {
                   // $(".signin").removeClass("menu-open");
                    $("fieldset#signin_menu").hide();
                }
            });            

        });
		
		
		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];}
}