var myrules = {
		'#go_gp' : function(element){
			element.onclick = function(){
				new Effect.toggle('guiding_principles');
			}
		},
		'#go_m' : function(element){
			element.onclick = function(){
				new Effect.toggle('mission');
			}
		},
		'#close_gp' : function(element){
			element.onclick = function(){
				new Effect.Fade('guiding_principles');
			}
		},
		'#close_m' : function(element){
			element.onclick = function(){
				new Effect.Fade('mission');
			}
		},
		'input[value=="cc"]' : function(element){
			element.onclick = function(){
				var section = document.getElementById('billing_cc_section');
				show(section);
			}
		},
		'input[value=="po"]' : function(element){
			element.onclick = function(){
				var section = document.getElementById('billing_cc_section');
				hide(section);
			}
		},
		'#users_optional' : function(element){
			element.onclick = function(){
				toggleOptionalFields('users');
			}
		},
		'#token_reg_optional' : function(element){
			element.onclick = function(){
				toggleDisplay('token_reg_section');
			}
		},
		'input[name=="client_contact"]' : function(element){
			element.onclick = function(){
				var client_billing_section = document.getElementById('client_billing_section');
				if (element.checked) {
					setBillingFields();
					hide(client_billing_section);
				} else {
					//clearBillingFields();
					show(client_billing_section);
				}
			}
		},
		'input[name=="client_credit_card"]' : function(element){
			element.onclick = function(){
				var client_billing_cc_section = document.getElementById('client_billing_cc_section');
				if (element.checked) {
					show(client_billing_cc_section);
				} else {
					hide(client_billing_cc_section);
				}
			}
		}/*,
		'a[href=="demo/"]' : function(element){
			element.onclick = function(){
				//popUp('flash/tfi_online_demo.swf', 900, 600);
				popUp(element + 'popup/', 900, 600);
				return false; // so link does not go to destination
			}
		}*/,
		'#demo_manual' : function(element){
			element.onclick = function(){
				//popUp('flash/tfi_online_demo.swf', 900, 600);
				popUp(element, 900, 600);
				return false; // so link does not go to destination
			}
		},
		'.demo_manual' : function(element){
			element.onclick = function(){
				popUp(element, 900, 600);
				return false; // so link does not go to destination
			}
		},
		'#chapter_manual' : function(element){
			element.onclick = function(){
				popUp(element, 900, 600);
				return false; // so link does not go to destination
			}
		},
		'.chapter_manual' : function(element){
			element.onclick = function(){
				popUp(element, 900, 600);
				return false; // so link does not go to destination
			}
		},
		'a[href=="why_incoterms/"]' : function(element){
			element.onclick = function(){
				//popUp('flash/tfi_online_demo.swf', 900, 600);
				popUp(element, 900, 600);
				return false; // so link does not go to destination
			}
		}
	};
	
Behaviour.register(myrules);
Behaviour.apply();

// function getElementsBySelector is defined in behaviour.js, returns array

function show(element) {
	element.style.display = 'block';
}

function hide(element) {
	element.style.display = 'none';
}

function toggleDisplay(element) {
	var section = document.getElementById(element);
	
	if (section.style.display == 'none') {
		show(section);
	} else {
		hide(section);
	}
	return false;
}

function toggleFormSection(element, activator, value) {
	if (activator) {
		if (value) {
			var selector = 'input[value=="' + value + '"]';
		} else {
			var selector = 'input[name=="' + activator + '"]';
		}
	} else {
		var selector = 'input[name=="is_' + element.split('_',1) + '"]';
	}
	var section = document.getElementById(element);
	var is = document.getElementsBySelector(selector);

	if (is[0].checked) {
		show(section);
	} else {
		hide(section);
	}
}

function toggleOptionalFields(element) {
	/*
	<li>
		<label>
			<span>* Email</span>	<<<--- Find * in this element
			<span><input name="email" type="text" /></span>
		</label>
	</li>
	*/
	
	var span_items = document.getElementsBySelector('#' + element + ' fieldset li label span');
		
	for (i in span_items) {
		if (span_items[i].nodeType == 1) {
			if (span_items[i].innerHTML.indexOf('*') == -1
			 && span_items[i].innerHTML.indexOf('<') == -1) {
				if (span_items[i].parentNode.parentNode.style.display == 'none') {
					show(span_items[i].parentNode.parentNode);
				} else {
					hide(span_items[i].parentNode.parentNode);
				}
			}
		}
	}
	return false;
}

function displayOptionalToggle(name, text, element_id) {
	if (!name || name == '') {
		name = '';
	} else {
		name = name + '_optional';
	}
	if (!text || text == '') {
		text = 'Toggle Optional Fields';
	}
	var display = '<span class="toggle"><a id="'+ name +'" href="javascript:;">' + text + '</a></span>';
	if (!element_id || element_id == '') {
		document.write(display);
	} else {
		var element = document.getElementById(element_id);
		var container = document.createElement('div');
		container.innerHTML = display;
		element.parentNode.insertBefore(container, element.parentNode.firstChild);
	}
}

function toggleRegisterFields() {
	displayOptionalToggle('users');
	toggleOptionalFields('users');
}

function setBillingFields() {
	var first_name = document.getElementsBySelector('input[name=="first_name"]');
	var last_name = document.getElementsBySelector('input[name=="last_name"]');
	var email = document.getElementsBySelector('input[name=="email"]');
	var company = document.getElementsBySelector('input[name=="company"]');
	var phone = document.getElementsBySelector('input[name=="phone"]');
	var address1 = document.getElementsBySelector('input[name=="address1"]');
	var address2 = document.getElementsBySelector('input[name=="address2"]');
	var city = document.getElementsBySelector('input[name=="city"]');
	var postal_code = document.getElementsBySelector('input[name=="postal_code"]');
	var non_us_province = document.getElementsBySelector('input[name=="non_us_province"]');
	var state_province_id = document.Users.state_province_id;
	var country_id = document.Users.country_id;

	var billing_first_name = document.getElementsBySelector('input[name=="billing_first_name"]');
	var billing_last_name = document.getElementsBySelector('input[name=="billing_last_name"]');
	var billing_email = document.getElementsBySelector('input[name=="billing_email"]');
	var billing_company = document.getElementsBySelector('input[name=="billing_company"]');
	var billing_phone = document.getElementsBySelector('input[name=="billing_phone"]');
	var billing_address1 = document.getElementsBySelector('input[name=="billing_address1"]');
	var billing_address2 = document.getElementsBySelector('input[name=="billing_address2"]');
	var billing_city = document.getElementsBySelector('input[name=="billing_city"]');
	var billing_postal_code = document.getElementsBySelector('input[name=="billing_postal_code"]');
	var billing_province = document.getElementsBySelector('input[name=="billing_province"]');
	var billing_state = document.Users.billing_state;
	var billing_country = document.Users.billing_country;
	
	// First, set billing info
	if (first_name[0].value != '') {
		billing_first_name[0].value = first_name[0].value;
	}
	if (last_name[0].value != '') {
		billing_last_name[0].value = last_name[0].value;
	}
	if (email[0].value != '') {
		billing_email[0].value = email[0].value;
	}
	if (company[0].value != '') {
		billing_company[0].value = company[0].value;
	}
	if (phone[0].value != '') {
		billing_phone[0].value = phone[0].value;
	}
	if (address1[0].value != '') {
		billing_address1[0].value = address1[0].value;
	}
	if (address2[0].value != '') {
		billing_address2[0].value = address2[0].value;
	}
	if (city[0].value != '') {
		billing_city[0].value = city[0].value;
	}
	if (postal_code[0].value != '') {
		billing_postal_code[0].value = postal_code[0].value;
	}
	if (non_us_province[0].value != '') {
		billing_province[0].value = non_us_province[0].value;
	}
	if (state_province_id.options.selectedIndex != 0) {
		billing_state.options.selectedIndex = state_province_id.options.selectedIndex;
	}
	if (country_id.options.selectedIndex != 0) {
		billing_country.options.selectedIndex = country_id.options.selectedIndex;
	}
	
	// Next, set missing contact info with billing info
	if (billing_first_name[0].value != '') {
		first_name[0].value = billing_first_name[0].value;
	}
	if (billing_last_name[0].value != '') {
		last_name[0].value = billing_last_name[0].value;
	}
	if (billing_email[0].value != '') {
		email[0].value = billing_email[0].value;
	}
	if (billing_company[0].value != '') {
		company[0].value = billing_company[0].value;
	}
	if (billing_phone[0].value != '') {
		phone[0].value = billing_phone[0].value;
	}
	if (billing_address1[0].value != '') {
		address1[0].value = billing_address1[0].value;
	}
	if (billing_address2[0].value != '') {
		address2[0].value = billing_address2[0].value;
	}
	if (billing_city[0].value != '') {
		city[0].value = billing_city[0].value;
	}
	if (billing_postal_code[0].value != '') {
		postal_code[0].value = billing_postal_code[0].value;
	}
	if (billing_province[0].value != '') {
		non_us_province[0].value = billing_province[0].value;
	}
	if (billing_state.options.selectedIndex != 0) {
		state_province_id.options.selectedIndex = billing_state.options.selectedIndex;
	}
	if (billing_country.options.selectedIndex != 0) {
		country_id.options.selectedIndex = billing_country.options.selectedIndex;
	}
}

function clearBillingFields() {
	var billing_first_name = document.getElementsBySelector('input[name=="billing_first_name"]');
	var billing_last_name = document.getElementsBySelector('input[name=="billing_last_name"]');
	var billing_email = document.getElementsBySelector('input[name=="billing_email"]');
	var billing_company = document.getElementsBySelector('input[name=="billing_company"]');
	var billing_phone = document.getElementsBySelector('input[name=="billing_phone"]');
	var billing_address1 = document.getElementsBySelector('input[name=="billing_address1"]');
	var billing_address2 = document.getElementsBySelector('input[name=="billing_address2"]');
	var billing_city = document.getElementsBySelector('input[name=="billing_city"]');
	var billing_postal_code = document.getElementsBySelector('input[name=="billing_postal_code"]');
	var billing_province = document.getElementsBySelector('input[name=="billing_province"]');
	var billing_state = document.Users.billing_state;
	var billing_country = document.Users.billing_country;
	
	billing_first_name[0].value  = '';
	billing_last_name[0].value   = '';
	billing_email[0].value       = '';
	billing_company[0].value       = '';
	billing_phone[0].value       = '';
	billing_address1[0].value    = '';
	billing_address2[0].value    = '';
	billing_city[0].value        = '';
	billing_postal_code[0].value = '';
	billing_province[0].value    = '';
	billing_state.options.selectedIndex   = 0;
	billing_country.options.selectedIndex = 0;
}


//
// Call countup_clock in the HTML source with
// date = UNIX timestamp of starting datetime
// server_date = time(), which is the PHP timestamp for right NOW on the server time
// format = 0 or 1 for simple and complex display
// limit = number of milliseconds before the counter redirects
// redirect = URL string to redirect to
//
// Example: <p><script>countup_clock(2000000,2100000,1)</script></p>
// PHP Ex:	<p><script>countup_clock('.$start.','.time().',1)</script></p>
//
function countup_clock(date, server_date, format, limit/*, redirect*/)
{
	document.write('<span id="countup"></span>');
	var Server_Time = (new Date(server_date)).getTime();
	var Client_Time = (new Date()).getTime();
	countup(date, (Server_Time - Client_Time), format);
	count_redirect(date, (Server_Time - Client_Time), limit/*, redirect*/);
}

function count_redirect(date, time_diff, limit/*, redirect*/) {
	var Todays_Date = (new Date()).getTime() + time_diff;
	var Target_Date = (new Date(date)).getTime();
	//Find their difference, and convert that into seconds.
	var Time_Elapsed = Math.floor((Todays_Date - Target_Date) / 1000);

	if(Time_Elapsed < 0) Time_Elapsed = 0;
	
	if (limit > 0) {
		if (Time_Elapsed >= limit) {
			location.href = getURL('challenge') + 'challenge/exam/?mode=summary';/*redirect;*/
		} else {
		//Recursive call, keeps the clock ticking.
		setTimeout('count_redirect(' + date + ',' + time_diff + ',' + limit /*+ ',' + redirect*/ + ');', 1000);
		}
	}
}

function countup(date, time_diff, format)
{
	var Todays_Date = (new Date()).getTime() + time_diff;
	var Target_Date = (new Date(date)).getTime();
	//Find their difference, and convert that into seconds.
	var Time_Elapsed = Math.floor((Todays_Date - Target_Date) / 1000);

	if(Time_Elapsed < 0) Time_Elapsed = 0;
	
	if (document.all) var element = document.all['countup'];
	else if (document.getElementById) var element = document.getElementById('countup');
	
	switch(format)
	{
		case 0:
			//The simplest way to display the time left.
			element.innerHTML = Time_Elapsed + ' seconds';
			break;
		case 1:
			//More datailed.
			var days = Math.floor(Time_Elapsed / (60 * 60 * 24));
			Time_Elapsed %= (60 * 60 * 24);
			var hours = Math.floor(Time_Elapsed / (60 * 60));
			Time_Elapsed %= (60 * 60);
			var minutes = Math.floor(Time_Elapsed / 60);
			Time_Elapsed %= 60;
			var seconds = Time_Elapsed;
			
			var dps = 's'; var hps = 's'; var mps = 's'; var sps = 's';
			//ps is short for plural suffix.
			if(days == 1) dps ='';
			if(hours == 1) hps ='';
			if(minutes == 1) mps ='';
			if(seconds == 1) sps ='';
			
			element.innerHTML = '';
			if(days > 0) element.innerHTML += days + ' day' + dps + ' ';
			if(hours > 0) element.innerHTML += hours + ' hour' + hps + ' ';
			if(minutes > 0) element.innerHTML += minutes + ' minute' + mps + ' ';
			if(days > 0 || hours > 0 || minutes > 0) element.innerHTML += 'and ';
			element.innerHTML += seconds + ' second' + sps;
			break;
		default: element.innerHTML = Time_Elapsed + ' seconds';
	}
	//Recursive call, keeps the clock ticking.
	setTimeout('countup(' + date + ',' + time_diff + ',' + format + ');', 1000);
}

function getURL(token) {
	if (!token || token == '') {
		return location.href;
	} else {
		var url = location.href;
		if (url.indexOf(token) == -1) {
			return url;
		} else {
			return url.substring(0, url.indexOf(token));
		}
	}
	
}