/* And all that Malarkey // Trimming form elements

Please feel free to use this JavaScript file in any way that you like, although please
keep the comments intact and a link back to http://www.stuffandnonsense.co.uk/archives/trimming_form_fields.html
would be appreciated.

If you come up with a stunning design based on this technique, it would be really nice
if you would post a comment containing a URL on 
http://www.stuffandnonsense.co.uk/archives/trimming_form_fields.html#Comments 

Thanks to Brothercake (http://www.brothercake.com/) for his help with the Javascript.
His fantastic UDM 4 fully-featured and accessible website menu system is a must!
(http://www.udm4.com/) */

function flip()
{
	if(document.getElementById)
	{
		var billing = document.getElementById('billing-hide');
		if(billing.length != 0)
		{
			billing.style.display = (billing.style.display == 'none') ? 'block' : 'none';
		}
	
		var same = document.getElementById('billingSame');
		if(same.textLength != 0)
		{
			same.value = (same.value == '1') ? '0' : '1';
		}
	}
}

function toggle_text(toggle)
{
	if(document.getElementById)
	{
		var linkText = toggle.firstChild.nodeValue;
		toggle.firstChild.nodeValue = (linkText == 'Enter different billing info...') ? 'Billing info same as shipping info.' : 'Enter different billing info...';

		flip();
		return false;
	}
}

window.onload = function()
{
	if(document.getElementById)
	{
		var linkContainer = document.getElementById('same-as');
		var toggle = linkContainer.appendChild(document.createElement('a'));
		toggle.href = '#';
		toggle.appendChild(document.createTextNode('Billing info same as shipping info...'));
		toggle.onclick = function ()
		{
			var linkText = toggle.firstChild.nodeValue;
			toggle.firstChild.nodeValue = (linkText == 'Enter different billing info...') ? 'Billing info same as shipping info.' : 'Enter different billing info...';

			flip();
			return false;
		}
		
		var same = document.getElementById('billingSame');
		if(same.value != '0')
		{
			same.value = '0';
			toggle_text(toggle);
		}
	}
}

