/*!
 * Copyright (c) 2009 Sandrine PERENES for Pixelfusion.
 * New Zealand Bone Marrow Donor Registry
 */


$(function(){
	/* ******************* */
	/* FAQ slideshow */

/* -- Add style -- */
	$('.faq h1').addClass('slideshow').attr('title', 'Read the answer');
	$('.faq .answer').hide();
/* -- Add action -- */
	$('.faq h1').click(function(){
		$(this).toggleClass('open');
		$(this).next('div.answer').slideToggle('fast');
		if ($(this).attr('title') == 'Read the answer') 
		{ $(this).attr('title', 'Close the answer'); }
		else
		{ $(this).attr('title', 'Read the answer'); }
	});
	
	/* ******************* */
	/* Optimize CSS Forms */
	if ($('form').length) {
	 	$('input.text').focus(function(){ $(this).addClass('focus'); });	
		$('input.text').blur(function(){ $(this).removeClass('focus'); });
		$('textarea').focus(function(){ $(this).addClass('focus'); });	
		$('textarea').blur(function(){ $(this).removeClass('focus'); });
		
		$('#consent-form .doubleFields :checkbox').each(function(){
			if($(this).val()!='Maori') {
				if ($(this).attr('checked')) {
					$(this).parents('.doubleFields').find(':text').attr('class', 'validate[required] text');
					$(this).parents('.doubleFields').find('.clnone span').addClass('required');
				} 
			}			
		});
		$('#consent-form .doubleFields :checkbox').click(function(){
				if ($(this).attr('checked')) {
					if($(this).val()!='Maori') {
						$(this).parents('.doubleFields').find(':text').attr('class', 'validate[required] text');
						$(this).parents('.doubleFields').find(':text').focus();
						$(this).parents('.doubleFields').find('span').addClass('required');
					}
				} else {	
					$(this).parents('.doubleFields').find(':text').val('');
					if($(this).val()!='Maori') {
						$(this).parents('.doubleFields').find(':text').removeClass('validate[required]');
						$(this).parents('.doubleFields').find('span').removeClass('required');
						$('.'+$(this).parents('.doubleFields').find(':text').attr('id')+'formError').remove();
					}
				}
		});
		$('#consent-form .doubleFields :text').keyup(function(){
				if($(this).val()) {
					if($(this).attr('id')!='ethnic-details-2') {
						$(this).attr('class', 'validate[required] text focus');
						$(this).parents('.doubleFields').find('span').addClass('required');
					}
					$(this).parents('.doubleFields').find(':checkbox').attr('checked', 'checked');
				} else {
					if($(this).attr('id')!='ethnic-details-2') {
						$(this).parents('.doubleFields').find(':checkbox').attr('checked', '');
						$(this).parents('.doubleFields').find('span').removeClass('required');
						$(this).parents('.doubleFields').find(':text').removeClass('validate[required]');
						$('.'+$(this).parents('.doubleFields').find(':text').attr('id')+'formError').remove();
					}
				}
		});
		
		$('#sidebar form').css({'width':'180px', 'margin':'0 auto'});
		$('#sidebar form .submit').remove();
		$('#sidebar form select').change(function(){
			if($(this).val()) { $(this).parents('form').submit(); }
		});
	}	
		
	$("#sidebar a[href$='/online-consent-form/']").click(function() {
		if($(this).html().match('overseas')) 	{ document.cookie = "donor=overseas"; }
		if($(this).html().match('family')) 		{ document.cookie = "donor=family"; }
		if($(this).html().match('platelet')) 	{ document.cookie = "donor=platelet"; }
	});

	/* Transform email address in mailto */
	$('.email').each(function(){
		var email = ($(this).html().replace(/\[AT\]/gi, "@").replace(/\[DOT\]/gi, "."));
		$(this).html("<a href='mailto:" + email + "'>" + email + "</a>");
	});

	/* ******************* */
	/* Validation Forms */
	$("#contact-us").validationEngine();
	$("#consent-form").validationEngine();
	$("#update-form").validationEngine();

});


	function printForm() {
		if($('#consent-form').validationEngine({returnIsValid:true})){
			var doc = '<div style="font-family: Verdana; font-size: 10px;">';
			 doc += '<table cellpading="0" cellspacing="0">';
			 doc += '<tr>';
			 doc += '<td><h1 style="margin: 0; font-size: 13px;">New Zealand Bone Marrow Donor Registry</h1><br />';
			 doc += '<p>Consent form – please print &amp; sign</p></td>';
			 doc += '</tr>';
			 doc += '</table>';
			 doc += '<br /><hr /><p style="font-size: 9px; margin: 0;">Thank you for completing the consent form.  Please print, sign &amp; send this form back to us & we\'ll add your tissue type record to the NZ Registry.';
			 doc += '<br /><br /><b>Post</b>: New Zealand Bone Marrow Donor Registry, PO Box 74336, Auckland 1543';
			 doc += '<br /><b>Fax</b>: (09) 523 5757';
			 doc += '<br /><b>Or email a scanned version, with signature to</b>: nzbmdr@nzblood.co.nz</p><hr /><br />';
			 doc += '<table cellpading="0" cellspacing="0">';
			 doc += '<tr>';
			 doc += '<td valign="top" style="width: 50px; text-align-left"><input class="radio" type="radio" ';
			 if($('#donor_1').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += ' /></td>';
			 doc += '<td valign="top">I had my white blood cells tissue typed when I became a platelet donor.</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top" style="width: 50px; text-align-left"><input class="radio" type="radio" ';
			 if($('#donor_2').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += ' /></td>';
			 doc += '<td valign="top" align="left">I had my white blood cells tissue typed in the search for a donor for a member of my family.<br />';
			 doc += '<table cellpading="0" cellspacing="0">';
			 doc += '<tr>';
			 doc += '<td valign="top">';
			 doc += 'Please enter the full name of the patient you were typed for:</td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000; width: 200px;">';
			 doc += $('#patient').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '</table>';
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top" style="width: 50px; text-align-left"><input class="radio" type="radio" ';
			 if($('#donor_3').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += '/></td>';
			 doc += '<td valign="top">I joined a bone marrow donor registry in another country but am now resident in NZ .</td>';
			 doc += '</tr>';
			 doc += '</table><br />';
			 doc += '<table cellpading="0" cellspacing="0">';
			 doc += '<tr>';
			 doc += '<td valign="top" colspan="2">You must tick this box to proceed: <span class="required">*</span></td></tr>';
			 doc += '<tr>';
			 doc += '<td valign="top" style="width: 50px; text-align-left"><input class="checkbox" type="checkbox" ';
			 if($('#permission').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += '/></td>';
			 doc += '<td valign="top">I hereby consent to the transfer of my tissue type onto the NZBMR &amp; give permission to be contacted for further tests should I be a potential donor for a patient needing a bone marrow transplant. I understand that I will be entered in numerical coding onto NZBMDR, which is a part of a world-wide network of bone marrow donor registries.</td>';
			 doc += '</tr>';
			 doc += '</table>';
			 doc += '<br />';
			 doc += '<table cellpading="0" cellspacing="0">';
			 doc += '<tr>';
			 doc += '<td valign="top">Mr</td>';
			 doc += '<td valign="top" style="width: 50px; text-align-left"><input class="radio" type="radio" ';
			 if($('#salutation_1').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += '/></td>';
			 doc += '<td valign="top">Mrs</td>';
			 doc += '<td valign="top" style="width: 50px; text-align-left"><input class="radio" type="radio" ';
			 if($('#salutation_2').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += '/></td>';
			 doc += '<td valign="top">Miss</td>';
			 doc += '<td valign="top" style="width: 50px; text-align-left"><input class="radio" type="radio" ';
			 if($('#salutation_3').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += '/></td>';
			 doc += '<td valign="top">Ms</td>';
			 doc += '<td valign="top" style="width: 50px; text-align-left"><input class="radio" type="radio" ';
			 if($('#salutation_4').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += '/></td>';
			 doc += '<td valign="top"><span class="required">*</span></td>';
			 doc += '</tr>';
			 doc += '</table>';
			 doc += '<table cellpading="0" cellspacing="0">';
			 doc += '<tr>';
			 doc += '<td valign="top" style="width: 200px;">Full name <span class="required">*</span></td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000; width: 350px">';
			 doc += $('#name').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Street address: <span class="required">*</span></td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#street1').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Suburb: <span class="required">*</span></td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#city').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Postcode: <span class="required">*</span></td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#postalcode').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Phone (home):</td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#phone1').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Phone (work):</td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#phone2').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Phone (mobile):</td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#phone3').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Email:</td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#email').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Date of Birth</td>';
			 doc += '<td>';
			 doc += '<table cellpadding="0" cellspacing="0">';
			 doc += '<tr>';
			 doc += '<td valign="bottom" width="180" style="border-bottom: 1px solid #000;">';
			 if ($('#dob_year').val()) {
				 doc += $('#dob_day').val()+'&nbsp;/&nbsp;'+$('#dob_month').val()+'&nbsp;/&nbsp;'+$('#dob_year').val();
			 } else {
				doc += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/';
				doc += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/';
			 }
			 doc += '</td>';
			 doc += '<td valign="bottom" style="padding: 0 20px 0 50px;">Sex:</td>';
			 doc += '<td valign="tbottomop">';
			 doc += '<table cellpading="0" cellspacing="0">';
			 doc += '<tr>';
			 doc += '<td valign="bottom"><input class="radio" type="radio"';
			 if($('#sex_1').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += ' /></td>';
			 doc += '<td valign="bottom" style="padding-right: 20px;">Male</td>';
			 doc += '<td valign="bottom"><input class="radio" type="radio"';
			 if($('#sex_2').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += ' /></td>';
			 doc += '<td valign="bottom">Female</td>';
			 doc += '</tr>';
			 doc += '</table>';
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '</table>';
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr><td valign="top" colspan="2"><br />Please supply the name and telephone number of a family member or a friend who does <u>not</u> live with you, in case we are unable to contact you.</td></tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Name</td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#relative_name').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Relationship</td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#relative_relationship').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top">Phone</td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#relative_phone').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '</table>';
			 doc += '<table cellpading="0" cellspacing="0">';
			 doc += '<tr><td valign="top" colspan="3"><br /><em>It is of assistance when searching for a suitable donor to know if the donor and the recipient are of similar ethnic background. Please tick the ethnic group(s) your parents or other ancestors come from.</em></td></tr>';
			 doc += '<tr>';
			 doc += '<td valign="top" style="width: 50px; text-align-left"><input type="checkbox"';
			 if($('#ethnic-1').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += ' /></td>';
			 doc += '<td valign="top" style="width: 200px;">EUROPEAN&nbsp;-&nbsp;<em>Country</em> <span>*</span></td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000; width:350px;">';
			 doc += $('#ethnic-details-1').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top"><input type="checkbox" ';
			 if($('#ethnic-2').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += '/></td>';
			 doc += '<td valign="top">MAORI&nbsp;-&nbsp;<em>Iwi</em></td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#ethnic-details-2').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top"><input type="checkbox" ';
			 if($('#ethnic-3').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += '/></td>';
			 doc += '<td valign="top">PACIFIC ISLANDER&nbsp;-&nbsp;<em>Country</em> <span>*</span></td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#ethnic-details-3').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top"><input type="checkbox" ';
			 if($('#ethnic-4').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += '/></td>';
			 doc += '<td valign="top">ASIAN&nbsp;-&nbsp;<em>Country</em> <span>*</span></td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#ethnic-details-4').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '<tr>';
			 doc += '<td valign="top"><input type="checkbox" ';
			 if($('#ethnic-5').is(':checked')) {
				 doc += ' checked="checked" ';
			 }
			 doc += '/></td>';
			 doc += '<td valign="top"><label for="ethnic-5">OTHER&nbsp;-&nbsp;<em>Please specify</em> <span>*</span></td>';
			 doc += '<td valign="bottom" style="border-bottom: 1px solid #000;">';
			 doc += $('#ethnic-details-5').val();
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '</table><br />';
			 doc += '<p style="font-size: 9px;"><em>By completing this form, you authorise NZBMDR to:</em></p>';
			 doc += '<ul style="font-size: 9px;">';
			 doc += '<li><em>Collect and hold the details on this form, along with genetic information from your blood testing</em></li>';
			 doc += '<li><em>Release necessary personal (no names) and genetic information to the NZBMDR or other affiliated international bone marrow donor registries, for the purposes of matching with potential bone marrow transplant recipients</em></li>';
			 doc += '</ul>';
			 doc += '<br/><br/><table>';
			 doc += '<tr>';
			 doc += '<td style="width: 360px; border: 1px solid #000;"><i style="font-size: 8px;">Sign</i><br /><br /><br /><br /></td>';
			 doc += '<td valign="bottom"style="width: 50px;padding-left: 60px; font-size: 9px;">Date:</td>';
			 doc += '<td valign="bottom" style="width: 120px; border-bottom: 1px solid #000;">';
			 doc += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/';
			 doc += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/';
			 doc += '</td>';
			 doc += '</tr>';
			 doc += '</table>';
			 doc += '</div>';

			 $('<div />').attr('id','printable').css('position','absolute').css('z-index','0').html(doc);
			 $('body').append($('<div />').attr('id','printable').html(doc));

			 //"here is a test".print();
			 $("#printable").print();
			 $("#printable").remove();
		}
	}

		// Create a jquery plugin that prints the given element.
jQuery.fn.print = function(){
	// NOTE: We are trimming the jQuery collection down to the
	// first element in the collection.
	if (this.size() > 1){
		this.eq( 0 ).print();
		return;
	} else if (!this.size()){
		return;
	}

	// ASSERT: At this point, we know that the current jQuery
	// collection (as defined by THIS), contains only one
	// printable element.

	// Create a random name for the print frame.
	var strFrameName = ("printer-" + (new Date()).getTime());

	// Create an iFrame with the new name.
	var jFrame = $( "<iframe name='" + strFrameName + "'>" );

	// Hide the frame (sort of) and attach to the body.
	jFrame
		.css( "width", "1px" )
		.css( "height", "1px" )
		.css( "position", "absolute" )
		.css( "left", "-9999px" )
		.appendTo( $( "body:first" ) )
	;

	// Get a FRAMES reference to the new frame.
	var objFrame = window.frames[ strFrameName ];

	// Get a reference to the DOM in the new frame.
	var objDoc = objFrame.document;

	// Grab all the style tags and copy to the new
	// document so that we capture look and feel of
	// the current document.

	// Create a temp document DIV to hold the style tags.
	// This is the only way I could find to get the style
	// tags into IE.
	var jStyleDiv = $( "<div>" ).append(
		$( "style" ).clone()
		);

	// Write the HTML for the document. In this, we will
	// write out the HTML of the current element.
	objDoc.open();
	objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
	objDoc.write( "<html>" );
	objDoc.write( "<body>" );
	objDoc.write( "<head>" );
	objDoc.write( "<title>" );
	objDoc.write( document.title );
	objDoc.write( "</title>" );
	objDoc.write( jStyleDiv.html() );
	objDoc.write( "</head>" );
	objDoc.write( this.html() );
	objDoc.write( "</body>" );
	objDoc.write( "</html>" );
	objDoc.close();

	// Print the document.
	objFrame.focus();
	objFrame.print();

	// Have the frame remove itself in about a minute so that
	// we don't build up too many of these frames.
	setTimeout(
		function(){
			jFrame.remove();
		},
		(60 * 1000)
		);
}