
/*
	setup_toggle()	
	James Dacosta 03/07 d0
	function that hides and shows form elements
	DEPENDANCIES: uses EJ written by Robert Nyman, http://www.robertnyman.com
		  		  standard.js
		  		  
		  		  Also uses addLoadEvent written by Simon Willison
	USAGE:
	
			the associated function init_toggle() initialises the
			id's involved in the toggle
			
			
			id's of the element to be toggled
			toggle[0].elements			=  ["section-date","section-premium"];
			
			id of the element which triggers the toggle
			toggle[0].trigger			= "trigger2";
			
			id of the element which reverses the toggle
			toggle[0].reverse			= "reverse2";

			id's of the elements which should be made required when made visible
			toggle[0].required			= ["element1","element2"];

			

			function init_toggle()
			{
 				var toggle = new Array();
				var risk = toggle[0] = [];
 				risk.elements			= ["section-date","section-premium"];
 				risk.trigger			= "risk-insurance_0";
 				risk.reverse			= "risk-insurance_1";
 				risk.required			= ["element1","element2"];
				
				...
   				
   				setup_toggle(toggle);
 			}
	
 		
	TODO:
 		
*/

 

function setup_toggle(toggle)
{
  	var i;
	for (i=0; i < toggle.length; i++)
	{	
 		
 		
 		var n;
 		for (n = 0; n < toggle[i].elements.length; n++)
 		{
  			
 			if (!$(toggle[i].elements[n])) return false;
			
  			
 			//initially hide the element to be toggled
 			if(!$(toggle[i].trigger)) return false;
  			if (!($(toggle[i].trigger).checked))
 			{
 			addClassName($(toggle[i].elements[n]),"hide-element"); 
  			}
  			else
  			{
  			if(toggle[i].required)
  			{
			set_required([toggle[i].required[n]]);
			
			var label = $(toggle[i].elements[n]).getElementsByTagName("label")[0];
			var required = document.createElement("em");
			var requiredText = document.createTextNode("required");
			required.appendChild(requiredText);
			
			label.appendChild(required);
			}
  			}
 		}
 		
 		
 		
 		
		// check for the existence of key objects
 		if (!$(toggle[i].trigger)) return false;
 		if (!$(toggle[i].reverse)) return false;
  		

  		
  		//assign the current object to props for referencing within onclick
 		$(toggle[i].trigger).props = $(toggle[i].reverse).props = toggle[i];
  		
  		
  		//add/remove classes depending on element clicked
  		$(toggle[i].trigger).onclick =  function()
										{
										
										
										var n;
										for (n = 0; n < this.props.elements.length; n++)
										{
											
											if (!$(this.props.elements[n])) return false;
											
 											addClassName($(this.props.elements[n]),"show-element");
											removeClassName($(this.props.elements[n]),"hide-element");
											
  											if(this.props.required)
											{
  												set_required([this.props.required[n]]);
  												
  												var label = $(this.props.elements[n]).getElementsByTagName("label")[0];
  												var required = document.createElement("em");
  												var requiredText = document.createTextNode("required");
  												required.appendChild(requiredText);
  												
  												label.appendChild(required);
 											}	
 											
										}

										
											 									
										} 		
  
  		$(toggle[i].reverse).onclick = function()
										{
 											
 											
 											var fieldTypes = ['input','textarea','select'];
 											
 											
											var n;
											for (n = 0; n < this.props.elements.length; n++)
											{
												
												if (!$(this.props.elements[n])) return false;
												
										
												
 											
												var k;
												 
												for (k = 0; k < fieldTypes.length; k++)
												{
												
													// reset the value/selected index of the hidden field
													if ($(this.props.elements[n]).getElementsByTagName(fieldTypes[k])[0])
													{
														
														var e = $(this.props.elements[n]).getElementsByTagName(fieldTypes[k])[0];
														switch (fieldTypes[k])
														{
															
															case 'input':
																	e.value = "";
															break;
															
															case 'textarea':
																	e.value = "";
															break;
														
															case 'select':
																e.selectedIndex = 0;
															break;
														
														}
													
													
													}
												
												
												}
 											 
												addClassName($(this.props.elements[n]),"hide-element");
												removeClassName($(this.props.elements[n]),"show-element");
												
 												if(this.props.required)
												{
  													unset_required([this.props.required[n]]);
 
												}
 
												
											}
										}	
		 									
 	}	
}


/*
	setup_transfers()	
	James Dacosta 03/07 d0
	function that copies data from/to specified fields
	DEPENDANCIES: uses EJ written by Robert Nyman, http://www.robertnyman.com
		  		  standard.js
		  		  
		  		  Also uses addLoadEvent written by Simon Willison
	USAGE:
			the associated function init_transfers() initialises the
			fields involved in data transfer and the triggers/reverse elements
			
			list of source fields
 			transfers[0].from			= ['name',		'address1'];
 			
			list of target fields
 			transfers[0].to				= ['targ_name',	'targ_address1'];
 			
 			id's of elements to trigger the copy and cut of the data
 			transfers[0].trigger		= ['trigger1'];
 			transfers[0].reverse		= ['reverse1'];
 			
			
			function init_transfers()
			{
				
				var transfers = new Array();
				
 				transfers[0]				= [];
 				transfers[0].from			= ['name',		'address1'];
 				transfers[0].to				= ['targ_name',	'targ_address1'];
 				transfers[0].trigger		= ['trigger1'];
 				transfers[0].reverse		= ['reverse1'];
 				
				transfers[1]				= [];
 				transfers[1].from			= ['postcode'];
 				transfers[1].to				= ['comment'];
 				transfers[1].trigger		= ['is-invoice-address'];
 				transfers[1].reverse		= ['is-not-invoice-address'];
 				
 				// easier way  (one reference to index)
				var addressCopy = transfers[2] = [];
 				addressCopy.from			= ['home-postcode'];
 				addressCopy.to				= ['business-postcode'];
 				addressCopy.trigger			= "trigger-id";
 				addressCopy.reverse			= "reverse-id";
 				
   				setup_transfers(transfers);
  				
 			}	
 		
	TODO:
 		
*/

function setup_transfers(transfers)
{
	var i;
	for (i = 0; i < transfers.length; i++)
	{ 
	
		// check for the existence of key objects
  		if (!$(transfers[i].trigger)) return false;
 		if (!$(transfers[i].reverse)) return false; 
 	
  		//assign the current object to props for referencing within onclick
 		$(transfers[i].trigger).props = $(transfers[i].reverse).props = transfers[i];
 		
 		//copy all the values across
  		$(transfers[i].trigger).onclick = function()
		{	
			var k = 0;
 			while(k < this.props.to.length)
			{
				$(this.props.to[k]).value = $(this.props.from[k]).value;
				k++;
			}
		} 
 		//remove all the values
 		$(transfers[i].reverse).onclick = function()
		{
			var k = 0;
 			while(k < this.props.to.length)
			{
				$(this.props.to[k]).value = "";
				k++;
			}
		} 
	}
  
}


/*
	init_helptext()	
	James Dacosta 03/07 d0
	function that prepares help popups
	DEPENDANCIES: uses EJ written by Robert Nyman, http://www.robertnyman.com
		  		  standard.js
		  		  
		  		  Also uses addLoadEvent written by Simon Willison
	USAGE:
			add the class "help" to a tags next to input fields e.g. 
			<input class="text" id="address1" /> <a href="#help1" title="helpful information" id="helpfor_input-element-id" class="help">help</a>
			
			use dl constructs for each help panel and give each an id related to the a tag e.g.
			
			<dl id="help1">
				<dt>this is the title or query</dt>
				<dd>this is the explanation</dd>
			</dl>
			
			then add:
			addLoadEvent(init_helptext); or onload="init_helptext();"
			
			users without javascript will be jumped to the associated help - users with javascript will have the popup
			
	EXTRAS:
	
			Give the help link an id of "helpfor_" + the associated input field id
			uncomment the link in the closehelp() function //$(associatedFieldId).focus();
			when the help panel is closed the associated help panel will get focus();
			
			
 		
	TODO:
 		 
*/

function init_helptext(transfers)
{
	if (!$('helpnotes')) return false;
	// get all the dl within the helpnotes
	var helpNotes = $('helpnotes').getElementsByTagName("dl");
	var i;
	// hide all the help notes
	for(i = 0; i < helpNotes.length; i++)
	{
		addClassName(helpNotes[i],'hide-help');	
	}
	
	// get all the a elements with class help
	var helpLinks = getElementsByClassName(document,"a","help");
	
	
	var this_page = document.location.href;
 	// Remove anchor if there is one -- anything following #, including #
	this_page = this_page.replace( /\#.*/, '' );
 	
	var j;
	for (j = 0; j < helpLinks.length; j++)
	{
		// transforms http://url/#id to id
 		var currentHelpPopup = helpLinks[j].href.replace(this_page, '' ).replace("#", '' );
  		
  		// pass the id of the current popup to the current object 
 		helpLinks[j].currentHelpPopup = currentHelpPopup;
 		
 		// initialise a global reference to the last popup
 		document.lastHelpPopup;
		
		// add onclick events for all the help links
 		helpLinks[j].onclick = function()
 		{

 			
 			if(!$(this.currentHelpPopup)) return false;
 			
  			var currentHelpPopupHelp = $(this.currentHelpPopup);
   			var lastHelpPopup = $(document.lastHelpPopup);
 			var currentlHelpLink = this;
  			var currentQuestion = currentHelpPopupHelp.getElementsByTagName("a")[0];
 			
 			currentQuestion.onclick = closeHelp;
 			
  			function closeHelp()
			{
				addClassName(currentHelpPopupHelp,"hide-help");
				removeClassName(currentHelpPopupHelp,'show-help');
  				
  				var associatedFieldId = currentlHelpLink.id.replace("helpfor_","");
				//$(associatedFieldId).focus();
				
				currentHelpPopupHelp.status = !currentHelpPopupHelp.status;
				return false;
			}

 			
 			//only create the close link once
 			if(!currentHelpPopupHelp.hasClose)
 			{
			var closeLink = document.createElement("a");
			closeLink.href = "#";
			addClassName(closeLink,"close");
			var closeText = document.createTextNode("close");
 			closeLink.onclick = closeHelp;
			closeLink.appendChild(closeText);
			currentHelpPopupHelp.appendChild(closeLink);
 			currentHelpPopupHelp.hasClose = true;
 			}
 			
 			// if there was previously a popup and it's not the currently
 			// requested popup then hide the previous popup and set its status to false
 			if (lastHelpPopup && (lastHelpPopup.id != currentHelpPopupHelp.id)) 
  			{
  				removeClassName(lastHelpPopup,"show-help");
  				addClassName(lastHelpPopup,"hide-help");
  				lastHelpPopup.status = false;
   			}; 
  			
  			
  			// every requested popup should be false initially
  			// if not hide the current popup
  			if (currentHelpPopupHelp.status) 
			{
   				addClassName(currentHelpPopupHelp,"hide-help");
				removeClassName(currentHelpPopupHelp,'show-help');
 			}
			else
			{
				//otherwise position the requested popup and show it
				
				var labelID = this.id.replace("helpfor_", 'helpLocatorfor_' );
 				var pos = findPos($(labelID));
				currentHelpPopupHelp.style.top = pos[1]-findPos($('quoteForm'))[1]-50+"px";
				currentHelpPopupHelp.style.left = pos[0]-findPos($(labelID))[0]+10+"px";
    			
				//alert("top: "+pos[1]+"px, left: "+pos[0]+"px");
 
    			
    			addClassName(currentHelpPopupHelp,"show-help");
				removeClassName(currentHelpPopupHelp,'hide-help');
  			}  			
			
			
			// assign the current popup to the last popup global
  			document.lastHelpPopup = this.currentHelpPopup;
  			
  			// switch the status of the currently requested popup
  			currentHelpPopupHelp.status = !currentHelpPopupHelp.status;
 			return false;
 		}
 		
 		
	
	}
	
	

}




/*
	Quirksmode find position of object script: http://www.quirksmode.org/js/findpos.html
*/
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}





function init_buttons_altext()
{
	if (!$('page-nav')) return false;
 	var b = $('page-nav').getElementsByTagName('input');
	
 	
 	if(b)
 	{
 	
		var i;
		for (i = 0; i < b.length; i++)
		{
			
			//b[i].value;
			b[i].onmouseover = function(){
 				alt = document.createElement("span");
 				altText = document.createTextNode(this.value);
 				alt.className="alt-text";
 				alt.appendChild(altText);
 				$('top-buttons').appendChild(alt);
  			}

			b[i].onmouseout = function(){
  				$('top-buttons').removeChild(alt);
  			}
		
		}
 	
 	}




}




// function to give hover effects to input buttons
function init_buttons()
{
 	var b1 = $('submit').getElementsByTagName('input');
 	
 	
 	if(b1)
 	{
		var i;
		for (i = 0; i < b1.length; i++)
		{
			b1[i].onmouseover = function(){
				this.src = this.src.replace('.gif','_over.gif');
			}
			b1[i].onmouseout = function(){
				this.src = this.src.replace('_over.gif','.gif');
			}
	
		}
	}
 

}

























 