//------------------------------------------------------------
//	 Load Events
//------------------------------------------------------------

if (W3CDOM) {

	addLoadEvent(prepareHelpLinks);
	addLoadEvent(preparePolicySwitcher);
	//addLoadEvent(prepareAddressFinder);
}

//------------------------------------------------------------
//	 Form Functionality Setup
//------------------------------------------------------------

function prepareHelpLinks() {

	var links = document.getElementsBySelector('form a.help');

	for(var i = 0; i < links.length; i++) {

	/*	Tried to use addEvent but IE would still execute the 
		default	action (opening a new window) in addtion to 
		the popup, so needed to use a standard on-x handler. */

		links[i].onclick = function() {
			openHelpPopup(this.href);
			return false;
		}
	}
}

function preparePolicySwitcher() {

	// Get the Div we want to toggle
	//debugger;
	var details = document.getElementById('partners_details');
	var single = document.getElementById('StageOne1_PersonalDetails1_policy_type_single');
	var joint = document.getElementById('StageOne1_PersonalDetails1_policy_type_joint');
//	var single = document.getElementById('policy_type_single');
//	var joint = document.getElementById('policy_type_joint');
	
	// We need all of these elements for the switcher
	if (!(joint && single && details)) return false;
	
	if (joint.checked) {
		makeApplicable(details);
	}
	else {
	    makeNonApplicable(details);
	}
	/*
	// Disable the second details set for single policy
	addEvent(single, 'click', function() { 
		var details = document.getElementById('partners_details')
		makeNonApplicable(details);
		
	});
	
	// Enable the second details set for joint policy
	if (joint)
	addEvent(joint, 'click', function() { 
		var details = document.getElementById('partners_details')
		makeApplicable(details);
	});	*/
}

function prepareAddressFinder() {
	
	var link = document.getElementById('find_address_link');
	if (link) {
		addEvent(link, 'click', function(evt) { 
			var post_code = document.getElementById('find_post_code');
			var house_num = document.getElementById('house_number');
			findAddress(house_num, post_code);
			if (evt.preventDefault) {
				evt.preventDefault(); 
			}
			return false;
		}, false);
	}

	//
	link = document.getElementById('manual_address_link');
	if (link) {
		addEvent(link, 'click', function(evt) { 
			var select = document.getElementById('select_address');
			makeNonApplicable(select);
			makeApplicable(document.getElementById('address_details'));
			if (evt.preventDefault) {
				evt.preventDefault(); 
			}
			return false;
		} );
	}
}

//------------------------------------------------------------
//	 Form Functionality
//------------------------------------------------------------

function makeApplicable(elem) {
	
	removeClass(elem, 'non_applicable');
	addClass(elem, 'applicable');
}

function makeNonApplicable(elem) {
	
	removeClass(elem, 'applicable');
	addClass(elem, 'non_applicable');
}

//------------------------------------------------------------
//	 Popups
//------------------------------------------------------------

function openHelpPopup(url) {
	var opts = "location=1,status=1,scrollbars=1,width=500,height=500";
	openPopup(url,"_help", opts);
}

//------------------------------------------------------------
//	 Address Finding And Selecting
//------------------------------------------------------------

function findAddress(house_num, post_code) {

	// Ajax call goes here.

	var options = new Array();
	options[0] = {address1: '121 Fake Street', address2: '', town: 'London', country: 'UK', postcode: 'NW8 8NW'}
	options[1] = {address1: '122 Fake Street', address2: '', town: 'London', country: 'UK', postcode: 'NW8 8NW'}
	options[2] = {address1: '123 Fake Street', address2: '', town: 'London', country: 'UK', postcode: 'NW8 8NW'}
	options[3] = {address1: '124 Fake Street', address2: '', town: 'London', country: 'UK', postcode: 'NW8 8NW'}
	options[4] = {address1: '125 Fake Street', address2: '', town: 'London', country: 'UK', postcode: 'NW8 8NW'}
	options[5] = {address1: '126 Fake Street', address2: '', town: 'London', country: 'UK', postcode: 'NW8 8NW'}
	// Callback function would be probably be createAddressSelector()
	createAddressSelector(options)
}

function createAddressSelector(options) {

	options = options || new Array();

	// Destroy old
	var old = document.getElementById('select_address') 
	if (old) old.parentNode.removeChild(old);
	
	// Build Select
	var selectAttr = {name: 'found_address', id: 'found_address', size: '8'};
	var select = easyCreateElement('select', selectAttr)
	
	// Build the options
	for (var i=0; i < options.length; i++){
		
		// Build the text string to go in the options (this is ugly and a bit hacky,
		// but you can't .join() objects, and we need to ignore blank ones.
		var text = '';
		var opt = options[i];
		
		for(x in opt) {
			if (opt[x]) {
				text += opt[x] + ', ';
			}
		}
		// remove a trailing ', '
		text = text.substring(0, text.length - 2);
		
		// Create the option
		var optElemAttr = opt
		var optElem = new Option(text, '');
		
		for (key in opt) { optElem.setAttribute(key, opt[key]); }
		
		// Add it to the select
		select.options[i] = optElem;

		// Create it's event handler
		addEvent(select.options[i], 'click', function() {
			populateAddressDetails(
				this.getAttribute('address1'),
				this.getAttribute('address2'),
				this.getAttribute('town'),
				this.getAttribute('country'),
				this.getAttribute('postcode')
			);
			makeNonApplicable(document.getElementById('select_address'));
		});
	}
	
	// Build Field Set
	var fieldset = easyCreateElement('fieldset')
	addClass(fieldset, 'found_address');
	fieldset.appendChild(select);
	
	// Build container div
	var select_address = easyCreateElement('div', {id: 'select_address'});
	select_address.appendChild(fieldset);

	// Add to document
	node = document.getElementById("find_address_link");
	insertAfter(select_address, node);
}


function populateAddressDetails(address1, address2, town, country, postcode) {
	
	var elem = document.getElementById('address_line1');
	if (elem) elem.value = address1;
	
	elem = document.getElementById('address_line2');
	if (elem) elem.value = address2;
	
	elem = document.getElementById('address_town');
	if (elem) elem.value = town;
	
	elem = document.getElementById('address_county');
	if (elem) elem.value = country;
	
	elem = document.getElementById('address_post_code');
	if (elem) elem.value = postcode;
	
	// Ensure The box is shown
	var details = document.getElementById('address_details');
	if(details)
	    makeApplicable(details);
}



function set_form(areas) {

	var area_array = areas.split(", ");
	
	for (keyVar in area_array){
		if(document.getElementById){
			if(document.getElementById(area_array[keyVar]))
			    var el = document.getElementById(area_array[keyVar]);
			    if(el) el.style.display = "none";		
		}
	}
}

function show_form(areas) {

	var area_array = areas.split(", ");
	
	for (keyVar in area_array){
		if(document.getElementById){
		    var el = document.getElementById(area_array[keyVar]);
			if(el) el.style.display = "block";		
		}
	}
}

function show_form_ex(areas, displayVal) {

	var area_array = areas.split(", ");
	
	for (keyVar in area_array){
		if(document.getElementById){
			var el = document.getElementById(area_array[keyVar]);
			if(el) el.style.display = displayVal;		
		}
	}
}

function hide_form(areas) {

	var area_array = areas.split(", ");
	
	for (keyVar in area_array){
		if(document.getElementById){
			var el = document.getElementById(area_array[keyVar]);
			if(el) el.style.display = "none";
		}
	}
}
