//-------------------------------------------------------------------------------------------------------
// Javascript utility functions. Jonathan Smith 03/02/2002
// Anvil Chorus version
//-------------------------------------------------------------------------------------------------------

// global variables
var quit_debug; // will be set to true once debug is cancelled from any of the alerts 
var jsDebug = false; // this only affects the PopDebugJavascript function and is simply set in this file

//-------------------------------------------------------------------------------------------------------
// PopDebugJavascript: 
//-------------------------------------------------------------------------------------------------------

function PopDebugJavascript(text) {
	if (!quit_debug && jsDebug) {quit_debug=!confirm('PopDebugJavascript:\n'+text+'\n\nContinue with debug?');}
}

//-------------------------------------------------------------------------------------------------------
// getUrlArgs: 
// Javascript definitive Guide - example 13-5. Downloaded from http://examples.oreilly.com/jscript3/
// This function parses 'sep'-separated name=value argument pairs from the query string of the URL.
// It stores the name=value pairs in properties of an object and returns that object.
// The default value of the separator is &
//-------------------------------------------------------------------------------------------------------

function getUrlArgs(sep) {
	var args = new Object();
	var query = location.search.substring(1);     // Get query string, ignoring first character '?'
	if (sep=='' || !sep) {sep="&";}               // Set the default separator 
	var pairs = query.split(sep);                 // Break at the separator
	for(var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');          // Look for "name=value".
		if (pos == -1) continue;                  // If not found, skip.
		var argname = pairs[i].substring(0,pos);  // Extract the name.
		var value = pairs[i].substring(pos+1);    // Extract the value.
		args[argname] = unescape(value);          // Store as a property.
	}
	return args;                                  // Return the object.
}

/* Example
var args = getUrlArgs();              // Get arguments.
if (args.x) x = parseInt(args.x);     // If arguments are defined override default values.
*/

//-------------------------------------------------------------------------------------------------------
// sendEmailTo: Create the <a> tag to pop a new window with a form to send an email.
// The parameters are defined in one text string as javascript statements separated by ;
//-------------------------------------------------------------------------------------------------------
function sendEmailTo(arg_string) {

	// define defaults for all parameters
	recipients_name='not set';
	target='not set';
	subject='';
	link_text='';
	link_class='inlinelink';
	status='';
	title='';
	senders_name='';
	senders_email='';
	width=650;
	height=420;
	senders_comments='';

	// evaluate the single parameter string, which will replace any defined values of the parameters
	x=eval(arg_string);
	
	// the following list of recipients is complete as at Jan 2004. The 'target' parameter is not used in any source pages, except diary maintainance
	// and replying to guestbook entries  
	if (recipients_name=='webmaster') {
		// allow recipients_name='webmaster' to be a short-cut for these parameters
		recipients_name='Jonathan Smith (webmaster)';
		target='vc_i|rrbn;@lsegBflnpr,lnb-sh';
	}
	else if (recipients_name=='Phil Mobsby') {target='Offh)Lm_o]x%3E%3Ejqhj@djqsp*jqe}qf'}
	else if (recipients_name=='George Badland') {target='Fclnbd,%3F]_k_k`;@lsegBflnpr,lnb-sh'}
	else if (recipients_name=='John Ellis') {target='Imej)Djien%3F%3FkrdkAekmtq}kmf,rg'}
	else if (recipients_name=='John Brough') {target='imej)aplqbg%3E%3Ejqhj@djqsp*jqe}qf'}
	else if (recipients_name=='Chris Cherry') {target='Bfoen-Aeamqw{{iugi%3Fcnpro)npd*pj'}
	else if (recipients_name=='Henry Foster') {target='Gcknt-Dloodp{{iugi%3Fcnpro)npd*pj'}
	else if (recipients_name=='Second City') {target='Rc`kicAfptRmrj_%3Fflph`gi*^nk'}
	else if (recipients_name=='Singing Valentine Quartet') {target='RgkcdmeS]gdlqeidq{{iugi%3Fcnpro)npd*pj'}
	else if (recipients_name=='IWouldLikeAnAnvilChorusEmailAddress@AnvilChorus.org.uk') {
		target='HUlqgcJfg`@l%3Ejqhj@djqspAh`gi{_cpbon%3F_krdkaekmtq}kmf,rg'}
	else if (recipients_name=='Lacy Kyle') {target='k_`uZjwia;l_`*^nk'}
	else if (recipients_name=='John Eardley') {target='Imej)D_o`gdw{{iugi%3Fcnpro)npd*pj'}
	else if (recipients_name=='Tony Curtin') {target='Smku)Bsopdm%3E%3Ejqhj@djqsp*jqe}qf'}
	else if (recipients_name=='Pete Ryley') {target='Ocqa)Qwiat%3F%3FkrdkAekmtq}kmf,rg'}
	
	if (target=='not set') {
		alert('Function SendMailTo\n\nNo address for '+recipients_name+'\n\nPlease report this to the webmaster\nThanks');
		return false;
	}
	
	//alert('recipients_name='+recipients_name+'\nsubject='+subject+'\nlink_text='+link_text);
	
	// adjust certain values if they are still blank
	if (status=='') {status='Send an email to '+recipients_name;}
	if (title=='') {title='Send an email to '+recipients_name;}

	across=(screen.availWidth-width)/2;
	down=(screen.availHeight-height)/2.5;
	document.write('<a href=" " onClick=\'mailwindow=window.open("scripts/send_it.pl?'+
		'recipients_name='+recipients_name+'&target='+target+'&subject='+subject+'&senders_name='+senders_name+
		'&senders_email='+senders_email+'&senders_comments='+senders_comments+'&submit_button=entry_form&return_link_id=close","mail",'+
		'"width='+width+',height='+height+',status=no,scrollbars=no,resizable=yes,'+
		'left='+across+',screenX='+across+',top='+down+',screenY='+down+'");return false;\' \n'+
		'class='+link_class+' onMouseOver=\'window.status="'+status+'";return true;\'\n'+
		'onMouseOut=\'window.status=" ";return true;\'\ntitle="'+title+'">');
		
	// if the link text has been specified then write it and close the <a> tag
	// otherwise do not close the <a> tag, to allow other items (eg images) to be included in the link (written in the calling page)
	if (link_text !='') {document.write(link_text+'</a>\n');}
	
	// set the status to blank. Otherwise the link status from above is left in the status bar on loading the page
	window.status=" ";
	return true;
}

//-------------------------------------------------------------------------------------------------------
// loadMenu: used to synchronise a left hand menu when a page is linked to.
// menuName will be one of the vbar menus
//-------------------------------------------------------------------------------------------------------

function loadMenu (menuName) {
	// alerts sometimes dont work when in framed page, but do when in fullscreen mode.
	// but the re-load is not affected, it still works fine. This is odd. Other times the alerts are ok.
	PopDebugJavascript('this is loadMenu in utility_a.js\n'
		+'vbar.location.href='+top.vbar.location.href+'\nmenuName='+menuName);
	if (top.vbar) {
		var path=top.vbar.location.pathname.split("/");
		if (path[path.length-1]!=menuName) {
			top.vbar.location.replace(menuName);
			top.main.focus();
			PopDebugJavascript('top.vbar exists\nmenu '+menuName+' reloaded');
		} else {PopDebugJavascript('reload of '+menuName+' if not required');}
	}
}

//-------------------------------------------------------------------------------------------------------
// showAllFormValues: display all values in the given form
//-------------------------------------------------------------------------------------------------------

function showAllFormValues (arg_string) {
	PopDebugJavascript('this is showAllFormValues in utility_a.js');

	// define defaults for all parameters
	form_name='0';

	// evaluate the single parameter string, which will replace any defined values of the parameters
	x=eval(arg_string);
	var msg='';
	var f=document.forms[form_name];
	for (i=0;i<f.length;i++) {
		msg+='('+i+')'+f.elements[i].name+'='+f.elements[i].value+' ';
		if ((i+1) % 2 ==0) {msg+='\n';}
	}
	alert(msg);
}