/* ########################################################################### *
/* ***** DOCUMENT INFO  ****************************************************** *
/* ########################################################################### *
 * ##### NAME:  global_function.js
 * ##### VERSION: v1.0
 * ##### UPDATED: 16/06/08 (Mick Chmielewski, Eclipse Group)
/* ########################################################################### *

/* ########################################################################### *
/* ***** INDEX *************************************************************** *
/* ########################################################################### *
/* ##### INITIALISATION
/* ##### SELECT ALL
/* ##### TOOLTIPS
/* ##### DATE PICKER
/* ##### REPLICATE ADDRESS
/* ##### POPUP WINDOW
/* ##### MODAL WINDOW
/* ########################################################################### */

/* ########################################################################### *
/* ##### INITIALISATION
/* ########################################################################### */

$(document).ready(ecl_init_global_func);

function ecl_init_global_func()
{
	// Lists
	ecl_select_all();
	
	// Tooltips
	ecl_tooltip();
	
	// Replicate address
	ecl_replicate_address();
	
	// Popup window
	ecl_popup_window();
	
	// Modal window
	ecl_terms_modal();
}



/* ########################################################################### *
/* ##### SELECT ALL
/* ##### Note: This will only work for one instance per page
/* ########################################################################### */

function ecl_select_all()
{
	$('.checkboxall').click(function(){
		var checked_status = this.checked;
		$('.checkall').each(function(){
			this.checked = checked_status;
		});
	});
}



/* ########################################################################### *
/* ##### TOOLTIPS
/* ########################################################################### */

function ecl_tooltip()
{
	$('.fieldinfo').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fixPNG: true,
		//showBody: " - ",
		extraClass: "pretty",
		top: -24,
		left: 6
	});
}



/* ########################################################################### *
/* ##### REPLICATE ADDRESS
/* ########################################################################### */

function ecl_replicate_address()
{
	$('#bcheck').click(function(){
		var same_status = this.checked;
		if (same_status) {
			var address1Value = $('#address1').val();
			$('#baddress1').val(address1Value);
			var address2Value = $('#address2').val();
			$('#baddress2').val(address2Value);
			var countryValue = $('#country').val();
			$('#bcountry').val(countryValue);
			$('#baddress1,#baddress2,#bcountry').attr("disabled","disabled");
		}
		else {
			$('#baddress1,#baddress2,#bcountry').val('');
			$('#baddress1,#baddress2,#bcountry').attr("disabled","");
		}						
	});
	$('#address1').change(function(){
		var same_status = $('#bcheck').attr("checked");
		if (same_status) {
			var address1Value = $('#address1').val();
			$('#baddress1').val(address1Value);	
		}
	});
	$('#address2').change(function(){
		var same_status = $('#bcheck').attr("checked");
		if (same_status) {
			var address2Value = $('#address2').val();
			$('#baddress2').val(address2Value);	
		}
	});
	$('#country').change(function(){
		var same_status = $('#bcheck').attr("checked");
		if (same_status) {
			var countryValue = $('#country').val();
			$('#bcountry').val(countryValue);	
		}
	});
}



/* ########################################################################### *
/* ##### POPUP WINDOW
/* ########################################################################### */

function ecl_popup_window() {
	$('.popUp').click(function(){
		var targetHref = $(this).attr('href');
		window.open(targetHref,"_blank","height=640,width=700,toolbar=no,menubar=no,location=no,scrollbars=yes,status=no");
		// Return false to prevent the link click navigation occuring.
		return false;
	});
}



/* ########################################################################### *
/* ##### MODAL WINDOW
/* ########################################################################### */

function ecl_terms_modal() {
	$('.termsLink').click(function (e) {
		e.preventDefault();
		// load the terms & conditions using ajax
		$.get("wf_103_3_terms.html", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: true,
				overlay: 70,
				overlayId: 'contactModalOverlay',
				containerId: 'contactModalContainer',
				iframeId: 'contactModalIframe',
				onOpen: contact.open,
				onClose: contact.close
			});
		});
	});
	
	var contact = {
		message: null,
		open: function (dialog) {
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.content.fadeIn(200, function () {
						$('#contactModalContainer #buttClose').focus();
					});
				});
			});
		},
		close: function (dialog) {
			dialog.content.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.remove(dialog);
					});
				});
			});
		}
	};
}
