/********************************************************
Set of Global JavaScript functions to be used
troughout!

Adam Crawford, v1.0, 14/08/02 
********************************************************/

/*doSearch is called from the button 
below simple query box on all forms */
/*********************************************************
Set of JavaScript functions used throughout the database

JavaScript Validation 2.0, 
Adam Crawford - netKis
**********************************************************/

/***********************************************************
errorHandler()
Used by Netscape to return the error message to the user.
Best used only when debuggind in development. Turn on by 
adding the following line to a form:
if (navigator.appName=="Netscape") window.onerror = errorHandler;
***********************************************************/
function errorHandler( e, f, l ) {
	alert("Error\nFile: " + f + "\nLine: " + l + "\nError:" + e);
	return true;
}


/***********************************************************
doDelete is used to delete the from the server

The user is first asked to confirm that this is what they
want to do. Giving them the chance to cancel the action.
This works SIMPLY by changing the end of the URL from 
"?OpenDocument" to "?DeleteDocument"

************************************************************/

function doDelete() {
	if ( confirm('Are you sure you want to delete this document?') ){
		location.search = "?DeleteDocument";
	}
}


/***********************************************************
trim is a simple function to remove leading/trailing spaces
************************************************************/

function trim(aStr) {
	return aStr.replace(/^\s{1,}/, "").replace(/\s{1,}$/, "")
}



/***********************************************************
following two functions call the date picking dialog box
************************************************************/

function myDateDialog(){
	var retDay;
	var retMonth;
	var retYear;
}

function openDatePickerDialog(wnd, field, dateFormat) {

	myDateDialog.retDay="";
	myDateDialog.retMonth="";
	myDateDialog.retYear="";

	var pathname = window.location.pathname;
	var dlgURL = pathname.substring(0,(pathname.lastIndexOf(".nsf") + 5))+'dlgDatePicker?OpenForm';
	if(wnd.showModalDialog(dlgURL,myDateDialog,"dialogHeight:380px;dialogWidth:280px;center")==true){
		field.value=dateFormat.replace(/yyyy/, myDateDialog.retYear).replace(/mm/, myDateDialog.retMonth).replace(/dd/, myDateDialog.retDay);
	}else{
		return;
	}
}
function doSearch ( s ) {
//temporarily disabled
alert("This function is not yet completed");
return false;
	var regExp1 = /\bfield\b/i; //used to test for reserved word field in query string
	var regExp2 = /[(,),<,>,\[,\]]/; //used to test for reserved char(s) in the query string
	var str = s.value;

	if ( trim(str) == "" ){
		alert("Please be sure to enter something to search for.");
		s.focus();
		return false;
	} else {
		if ( typeof regExp1.source != 'undefined' ) //supports regular expression testing
			if ( regExp1.test( str ) || regExp2.test( str ) ){
					var alrt = "Please note that you can not include:";
					alrt += "\n\nThe reserved word 'field'\nthe characters [, ], (, ), < or >";
					alrt += "\n\nin your search query!\n\nIf you are confident that you know";
					alrt += "\nwhat you are doing, then you can\nmanually produce the URL required."
					alert( alrt );
					s.focus();
					return false;
			}
			
			document.location = "/A55692/store.nsf/srch?SearchView&Query=" + escape( str ) + "&start=1&count=10&SearchFuzzy=True";
	}
}

/*open search results and append the search term in the URL..*/
function openAndHighlight( docID ) {
	appTerm = document.forms[0].SearchTerm.value;
	
	document.location = "/prominic/codestore/stortemp.nsf/all/" + docID + "?OpenDocument&Highlight=0," + escape( appTerm );
}

//Remove leading and trailing spaces from a string
//Originally written by Jim Fricker
function trim(aStr) {
	return aStr.replace(/^\s{1,}/, "").replace(/\s{1,}$/, "")
}

/*
getPathName
Required due to Opera bug where location.pathname
returns location.search as well.
*/
function getPathName(){
var pth = location.pathname.split('?');
return pth[0];
}

/*
NavigateView cycles through a view using the
start and count paramaters of the ?OpenView 
method.

Required as @Commands do not work with single
category views on the web !! 

Arguments:
d = direction, either "prev" or "next"
n = incremental count number
*/

function navigateView (d, n) {
	var c = 0;
	var args = location.search.split('&');

	var news = new Array();
	var ii = 0;

	for (var i = 0; i < args.length; i ++) {
		if ( args[i].toLowerCase().indexOf('start=') != -1){
			c = parseInt(args[i].split('=')[1]);
		} else {
			news[ii] = args[i];
			ii ++;
		}
	}
	
 	var strt = getPathName();
  	
	if (args[0] == ''){
		location.href = strt + '?OpenView&start=' + n;
	} else if (c == 0 && d == 'next'){
		location.href += '&start=' + n;
	} else if (c == 0 && d == 'prev'){
		return alert('There are no more documents in that direction.');
	} else 	if (c <= n && d == 'prev'){
		location.href = strt + news.join('&');
	} else 	if (d == 'next'){
		location.href = strt + news.join('&') + '&start=' + ( c + n );
	} else 	if (d == 'prev'){
		location.href = strt + news.join('&') + '&start=' + ( c - n);
	}
}

/*
addMozillaSidebar add codestore sidebar
page to Mozilla browsers. Contains a set
of useful links and search box.
*/

function addMozillaSidebar() { 
	if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) { 
		window.sidebar.addPanel ("CodeStore", "http://www.codestore.net/A55692/store.nsf/sidebar?ReadForm",""); 
	} else { 
		alert("This is only available in Mozilla variant browsers.");
	} 
} 