/*--------------------------------------------------------------------------------------------------------*/
/*                                                                                                        */
/* File: js.js                                                                                            */
/*                                                                                                        */
/* Author: Emily Gillcoat                                                                                 */
/* Date:  03/31/2009                                                                                      */
/* Version: 1.0                                                                                           */
/*                                                                                                        */
/* This file performs all actions (show/hide) that are dependant on fields in the html                    */
/*   and kicks off validation for all input buttons.                                                      */
/*                                                                                                        */
/* For validation of global values (is it a number, correct length ect,) this file should just            */
/*   gather the fields thatneed to be validated and call the appropriate method in validation.js          */
/*   to do the validation                                                                                 */
/*                                                                                                        */
/* ----- this file needs to be minimized in a production enviroment  -----                                */
/*                                                                                                        */
/*--------------------------------------------------------------------------------------------------------*/
jQuery(function () {


	/*----------------------------------------------------------------------------------------------------*/
	/* tooltip                                                                                            */
	/*                                                                                                    */
	/* each tooltip is defined with class cluetip and the name of the tooltip to display                  */
	/* Note: this will not work if there are more than two classes on the tooltip in the html             */
	/*                                                                                                    */
	/* look for a div with the same class and display it as the  tooltip                                  */
	/* if there is not a tooltip defined give an alert to the  programmer                                 */
   	/*----------------------------------------------------------------------------------------------------*/
	jQuery(".cluetip").tooltip({
		bodyHandler: function()
		{
			var	my_class = jQuery(this).attr('class').replace('cluetip ','');;
			var my_div = 'div.'+my_class;
			var my_html = (jQuery(my_div).html() == null) ? 'Tooltip is not available' : jQuery(my_div).html();
			return my_html;
		},
		showURL: false
	});


	/*-----------------------------------------------------------------*/
	/* All                                                             */
	/* remove the error messages and classes when the input is clicked */
	/* this must be before any other input click methods are declared  */
	/*-----------------------------------------------------------------*/

	jQuery('#standard-app-menu input').click(function()
	{

		jQuery('fieldset div').removeClass('input-error');
		jQuery('#top-error-block').remove();
	});


	/*--------------------------------------------------------------*/
	/* Medicare - Other Coverage                                    */
	/*--------------------------------------------------------------*/

	/* if yes is selected and there are sub-questions, display them, hide them if no is selected  */
	jQuery('tr.has-sub-questions input.radio').click(function()
	{

		if (jQuery(this).val()=='true')
		{
			jQuery(this).parents('tr').next().showSubQuestions();
		}
		else
		{
			jQuery(this).parents('tr').next().hideSubQuestions();
			
		}
	});

	jQuery.fn.showSubQuestions = function()
	{
		jQuery(this).show();
		if (jQuery(this).next().hasClass('sub-question'))
		{
			jQuery(this).next().showSubQuestions();
		}
		return this ;
	}

	jQuery.fn.hideSubQuestions = function()
	{
		jQuery(this).hide();
		if (jQuery(this).next().hasClass('sub-question'))
		{
			jQuery(this).next().hideSubQuestions();
		}
		return this ;
	}

	
	jQuery('tr.has-sub-questions input.radio:checked').each(function()
	{
		if(jQuery(this).val() == 'true')
		{
			jQuery(this).parents('tr').next().showSubQuestions();
		}
	});

	

	/*--------------------------------------------------------------*/
	/* Review Application                                           */
	/*--------------------------------------------------------------*/

	/* show new dates if change date button is clicked              */

	jQuery('div.review button.change').click(function()
	{
		jQuery(this).parent().find('fieldset.change-date').show();
		jQuery(this).addClass('change-disabled');
	});


	/*--------------------------------------------------------------*/
	/* Health - Other Coverage                                      */
	/* Dental  - Prior Coverage                                     */
	/*                                                              */
	/* Note - If HQResponse is ever to be displayed on a 'no'       */
	/*  radio button, this logic will have to be changed            */
	/*--------------------------------------------------------------*/

	/* show HQresponse if radio button yes is selected, hide if no  */
	jQuery('td.q div.HQresponse').parents('tr').find('td.a input').click(function()
	{
		jQuery(this).showHideHQResponse();
	});

	jQuery.fn.showHideHQResponse = function()
	{
		if (jQuery(this).val() == 'true')
		{
			jQuery(this).parents('tr').find('div.HQresponse').show('slow');
		}
		else
		{
			jQuery(this).parents('tr').find('div.HQresponse').hide();
		}
	}

	/* on page load, show the HQresponse if it exists and radio button is yes    */
	jQuery('td.q div.HQresponse').parents('tr').find('td.a input:checked').each(function()
	{
		jQuery(this).showHideHQResponse();
	});


	/*--------------------------------------------------------------*/
	/* Payment                                                      */
	/*--------------------------------------------------------------*/

	/* show the appropriate billing method information */

	jQuery('input#payment-method-cc').click(function()
	{
		jQuery('.payment-method-cc').show();
		jQuery('.payment-method-bd').hide();
	} );

	jQuery('input#payment-method-bd').click(function()
	{
		jQuery('div.payment-method-cc').hide();
		jQuery('div.payment-method-bd').show();
	} );

	jQuery('input#payment-method-db').click(function()
	{
		jQuery('div.payment-method-bd').hide();
	} );

	/*  show the appropriate billing method information on page load */

	var pm = jQuery('.payment input[name="payment-method"]:checked').val();
	jQuery('div.'+pm).show();

	/*  fill in billing data                           */

	jQuery('input#same-address').click(function()
	{
		/* dummy data for demostration purposes only */
		jQuery('input#billing-street').val('123 Main St');
		jQuery('input#billing-apt').val('321');
		jQuery('input#billing-city').val('Raleigh');
		jQuery('input#billing-zip').val('27613');
	});


	/*--------------------------------------------------------------*/
	/*  Applicant Information                                       */
	/*--------------------------------------------------------------*/

	/* trigger zip code onblur */
	//jQuery('#primary-zip').blur( function() { populateCounties(jQuery(this), jQuery('#primary-county')); });
	//jQuery('#primary-county').onchange( function() { clearErrors(); });

	/*  show and hide spouse and dependent information   */

	/*  show response not NC resident */

	jQuery.fn.setResident = function()
	{
		if (jQuery(this).find('input#resident-false:checked').length > 0)
		{
			jQuery(this).next('.HQresponse').show();
		}
	}

	jQuery('input#resident-false').click(function()
	{
		var parent = jQuery(this).parents('fieldset.v-resident');
		parent.addClass('input-error');
		parent.next('.HQresponse').show();
	} );

	jQuery('input#resident-true').click(function()
	{
		var parent = jQuery(this).parents('fieldset.v-resident');
		parent.removeClass('input-error');
		parent.next('.HQresponse').hide();
	} );

	jQuery('fieldset.v-resident').setResident();


	/* if no ss number is checked, display a response based on the age and gray out the ss# fields   */
	/* if no ss number is not checked, remove any response and enable the ss# fields                 */

	jQuery.fn.setSSNum = function()
	{
		var ss = jQuery(this).parents('div.row').prev().find('input');
		var ss_parent = jQuery(this).parents('.ss-info');

		if (jQuery(this).is(':checked'))
		{
			ss.attr({disabled:'disabled'});
			ss_parent.find('div.no-ss-num-response').remove();

			resp = jQuery(this).validateSSAge();
			ss_parent.append('<div class="no-ss-num-response HQresponse"><p>'+resp+'</p></div>');
			ss_parent.find('div.no-ss-num-response').show();
		}
		else
		{
			ss.removeAttr('disabled');
			ss_parent.find('div.no-ss-num-response').remove();
		}
	}

	/* gray out ss if no-ss-num clicked */

	jQuery('input.no-ss-num').click(function()
	{
		jQuery(this).setSSNum();

	});


	/* gray out ss on page load, if no-ss-num checked  */

	jQuery('input.no-ss-num:checked').each(function(){ jQuery(this).setSSNum() });


	/* give a response if dependent age is greater than 26               */

	jQuery.fn.setDepAge = function()
	{

		jQuery(this).each(function()
		{

			var dob = jQuery(this).parents('div.v-dob');

			if (dob.find('input[value=""]').length == 0)
			{

				var this_parent = jQuery(this).parents('.ss-info');
				var age = dob.getAge();
				if (age > 26)
				{
					if (this_parent.find('div.dep-response').length == 0 )
					{
						var resp = jQuery(this).getResponse('dep001');
						this_parent.append('<div class="dep-response HQresponse"><p>'+resp+'</p></div>');
						this_parent.find('div.dep-response').show();
					}
				}
				else
				{
					this_parent.find('div.dep-response').remove();
				}
			}
		});
	}

	/* check the dependent age as it is entered */

	jQuery('div.child-stats div.v-dob input').blur(function()
	{
		jQuery(this).setDepAge();

	});


	/* gray out ss on page load, if no-ss-num checked  */

	jQuery('div.child-stats div.v-dob').each(function(){jQuery(this).setDepAge() });
	

	/* give a response if no ss num checked, check the age               */

	jQuery.fn.setThisAge = function()
	{

		if ((jQuery(this).find('input[value=""]').length == 0) && (jQuery(this).parents('fieldset').find('.no-ss-num:checked').length > 0))
		{
			/* this should be broken out with the other validateSSAge code on this page  */
			var ss_parent = jQuery(this).parents('.ss-info');
			ss_parent.find('div.no-ss-num-response').remove();

			var resp = jQuery(this).validateSSAge();
			ss_parent.append('<div class="no-ss-num-response HQresponse"><p>'+resp+'</p></div>');
			ss_parent.find('div.no-ss-num-response').show();
		};
	}

	/* check the dependent age as it is entered */

	jQuery('div.v-dob input').blur(function()
	{
		jQuery(this).setThisAge();

	});


	/* gray out ss on page load, if no-ss-num checked  */

	jQuery('div.v-dob').each(function(){ jQuery(this).setThisAge() });
	



	jQuery.fn.setSpouseGender = function()
	{

		var gender = jQuery(this).val();
		if (gender == 'male')
		{
			jQuery('select.spouse-gender').val('female');
		}
		else
		{
			jQuery('select.spouse-gender').val('male');
		}

		return this;
	}


	jQuery.fn.showSpouse = function()
	{
		if (jQuery(this).val() != '')
		{
			jQuery(this).parents('div.spouse-stats').show();
			jQuery('button.add-spouse').hide();
			jQuery('div.spouse button.remove').show();
		}
		return this;
	}


//	jQuery('div.spouse-stats').hide();
//	jQuery('div.spouse button.remove').hide();

	//jQuery('div.spouse button.remove').hide();
	//jQuery('div.spouse-stats').hide();
	//jQuery('button.add-spouse').show();

/* this is a hack to if the spouse is there (by checking full name) display the spouse on page load  */
/* if pat is adding spouse by ajax like the dependents, this should change. This should change anyway */
/* so determining if spouse has been added is not dependent on first name  */

	//jQuery('div.spouse-stats input#spouse-firstname').showSpouse();


//	jQuery('div.child-group').hide();

	jQuery('select.primary-gender').change(function()
	{
		jQuery(this).setSpouseGender();

	});

	jQuery('select.primary-gender').setSpouseGender();


	/* show the spouse info, hide the add spouse button, show the remove button */

	jQuery('button.add-spouse').click(function()
	{
		jQuery(this).parent().next().slideDown();
		jQuery(this).next().show();
		jQuery(this).hide();
	} );

	/* show the dependent info,  show the remove button     */
	/* if last dependent, disable the add dependent button  */

/* remove this functionality...this is now an ajax call

	jQuery('button.add-child').click(function()
	{

		jQuery('div.child-group button.remove').show();
		jQuery('div.child-group:hidden:first').show();
		if (jQuery('div.child-group:hidden').length == 0)
		{
			jQuery('button.add-child').addClass('child-disabled');
		}
	} );
*/


	/* remove the spouse info, show the add spouse button, hide the remove button */

	jQuery('button.remove-spouse').click(function()
	{
		jQuery(this).parent().next().hide();
		jQuery(this).prev().show();
		jQuery(this).hide();
	} );


/* remove this functionality...see below

	jQuery('button.remove-child').click(function()
	{
		jQuery(this).parents('div.child-group').hide();
		jQuery(this).hide();
		jQuery('button.add-child').removeClass('child-disabled');
	} );
*/
	/*---------------------------------------------------------------------------------------*/
	/* this is my fake ajax call to insert the dependent info when add dependent is clicked  */
	/*---------------------------------------------------------------------------------------*/

	jQuery('button.add-child').click(function()
	{
	/*
		if (jQuery('div.child-group').length == 0)
		{
			var i = 1;
		}
		else
		{


			var temp = jQuery(this).prev().children('h3').text().split(' ');
			var i = temp[1]*1 + 1;
		}

		var child = '<div class="child-group"><h3>Dependent '+i+'</h3><div class="float-left"><button type="button" class="remove remove-child">Remove</button></div>';
		child += '<div class="child-stats ss-info"><fieldset class="personal"><legend>Personal information</legend><div class="row">';
		child += '<div class="float-left"><label for="dependent-'+i+'-firstname">First name</label><input class="v-first-name" maxlength="50" type="text" id="dependent-'+i+'-firstname" name="dependent-'+i+'-firstname"></div>';
		child += '<div class="float-left"><label for="dependent-'+i+'-mi">MI</label><input class="three v-optional" maxlength="1"  type="text" id="dependent-'+i+'-mi" name="dependent-'+i+'-mi"><p class="note">optional</p></div></div>';
		child += '<div class="row clear-float"><label class="no-top-margin" for="dependent-'+i+'-lastname">Last name</label><input class="big v-last-name" maxlength="50" type="text" id="dependent-'+i+'-lastname" name="dependent-'+i+'-lastname"></div>';
		child += '<div class="row v-dob"><label for="dependent-'+i+'-bd-month">Birth date</label><input class="two v-date-month" maxlength="2" type="text" id="dependent-'+i+'-bd-month" name="dependent-'+i+'-bd-month">/ <input class="two v-date-day" maxlength="2" type="text" id="dependent-'+i+'-bd-day" name="dependent-'+i+'-bd-day">/ <input class="four v-date-year" maxlength="4" type="text" id="dependent-'+i+'-bd-year" name="dependent-'+i+'-bd-year"></div>';
		child += '<div class="row v-ss-num"><label for="dependent-'+i+'-socialSecurity-1">Social Security #</label><input class="two v-optional" maxlength="3" type="text" id="dependent-'+i+'-socialSecurity-1" name="dependent-'+i+'-socialSecurity-1">- <input class="two v-optional" maxlength="2" type="text" id="dependent-'+i+'-socialSecurity-2" name="dependent-'+i+'-socialSecurity-2">- <input class="four v-optional" maxlength="4" type="text" id="dependent-'+i+'-socialSecurity-3" name="dependent-'+i+'-socialSecurity-3"></div>';
		child += '<div class="row"><label for="dependent-'+i+'-no-socialSecurity"><input class="checkbox no-ss-num" type="checkbox" id="dependent-'+i+'-no-socialSecurity" name="dependent-'+i+'-no-socialSecurity" >No Social Security #</label>	<p><span class="cluetip ct-social-security">Why is this required?</span></p></div></fieldset>';

		child += '<fieldset class="stats"><legend>Stats</legend><div class="row"><label for="dependent-'+i+'-gender">Gender</label><select id="dependent-'+i+'-gender" name="dependent-'+i+'-gender"><option value="male">Male</option><option value="female">Female</option></select></div>';
		child += '<div class="row -health -dental"><label for="dependent-'+i+'-status">Status</label><select id="dependent-'+i+'-status" name="dependent-'+i+'-status"><option value="biological">Biological</option><option value="adopted">Adopted</option><option value="foster">Foster</option><option value="step">Step</option></select></div>';


		// don't put height and weight in dental  
		if (jQuery('#primary-height-feet').length > 0)
		{
			child += '<div class="row v-height -health"><label for="dependent-'+i+'-height-feet">Height</label><div class="input-note"><select class="four v-height-feet" id="dependent-'+i+'-height-feet" name="dependent-'+i+'-height-feet"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option></select><p class="note">feet</p></div>';
			child += '<div class="input-note"><select class="four v-height-inch" id="dependent-'+i+'-height-inch" name="dependent-'+i+'-height-inch"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option></select><p class="note">inches</p></div></div>';
			child += '<div class="row clear-float -health"><label for="dependent-'+i+'-weight">Weight</label><input class="four v-weight  v-number" maxlength="3" type="text" id="dependent-'+i+'-weight" name="dependent-'+i+'-weight"><p class="note">pounds</p></div>';
		}
		child += '</fieldset></div></div>';
		jQuery(this).before(child);

		jQuery(this).parent().find('div.child-group').hide().slideDown();
	


		// bind remove button functionality   

		jQuery('button.remove-child').click(function()
		{
			jQuery(this).parents('div.child-group').remove()
		});
	*/
		/* bind remove  tooltip functionality  */

		jQuery(".cluetip").tooltip({
			bodyHandler: function()
			{
				var	my_class = jQuery(this).attr('class').replace('cluetip ','');;
				var my_div = 'div.'+my_class;
				var my_html = (jQuery(my_div).html() == null) ? 'Tooltip is not available' : jQuery(my_div).html();
				return my_html;
			},
			showURL: false
		});

		/* bind gray out ss# functionality  */
		jQuery(this).parent().find('div.child-group input.no-ss-num').click(function()
		{
			jQuery(this).setSSNum();
		});

		/* check the dependent age as it is entered */

		jQuery('div.child-stats div.v-dob input').blur(function()
		{
			jQuery(this).setDepAge();

		});

		/* check the age as it is entered */

		jQuery('div.v-dob input').blur(function()
		{
			jQuery(this).setThisAge();

		});



	});


	/*remove the corresponding dependent info, enable the add dependent button  */


		jQuery('button.remove-child').click(function()
		{
			jQuery(this).parents('div.child-group').remove()
		});






	/*--------------------------------------------------------------*/
	/* About Your Health                                            */
	/*--------------------------------------------------------------*/

	/* this is a hack to make safari behave and display the label and name list correctly */
	/* but now it is working okay with it commented out ????   3/19/2009                  */
//	if( jQuery.browser.safari ) jQuery('.cc li label').css('display', 'block' );


	/* some health questions should start out displayed */

	jQuery('td.q fieldset.always-show').show();

	jQuery('td.q fieldset div.applicant-cklist').hide();


	/* q42 - show temporary textarea if checkbox checked   */
	jQuery.fn.setImpairment = function()
	{

		jQuery(this).each(function()
		{
			var row = jQuery(this).parents('div.row').next();
			if (jQuery(this).is(':checked'))
			{
				row.show();
			}
			else
			{
				row.hide();
			}
		});
	}

	/* gray out ss if no-ss-num clicked */

	jQuery('td.q fieldset input.impaired').click(function()
	{
		jQuery(this).setImpairment();

	});


	/* hide on page load,   */

	jQuery('td.q fieldset input.impaired').each(function(){ jQuery(this).setImpairment() });


	/* show the applicants                                        */

	jQuery.fn.showApplicants = function()
	{

		/* remove previous ck-procedure and applicant-cklist                     */
		jQuery('div.condition-cklist li').removeClass('ck-procedure');
		jQuery('div.condition-cklist li').removeClass('ck-background');
		jQuery('div.applicant-cklist').hide();

		/* add ck-procedure to and display applicant-cklist for the current row  */
		jQuery(this).addClass('ck-procedure');


		/* if there is more than one applicant and there is an applicant list, show the background arrow */
		if (jQuery(this).find('div.applicant-cklist').val() != undefined)
		{
			jQuery(this).addClass('ck-background');
			jQuery(this).find('div.applicant-cklist').show();
			jQuery(this).find('ul.applicant-names').hide();
		}

/* -- this should be split out with the insert edit code on line 777 --      */
		/* if edit text exists, change it to close                               */
		edit_close = (jQuery(this).find('div.applicant-cklist:hidden').length > 0) ? '[edit]' : '[close]';
		
		if (jQuery(this).find('span.edit').length == 0 && jQuery(this).find('div.applicant-cklist').length > 0)
		{
			var edit_close = '<span class="edit">[close] hi</span>';
			jQuery(this).append(edit_close);

			/* bind the click event to the new edit span                                                       */
			jQuery(this).find('span.edit').click(function()
			{
				if (jQuery(this).text() == '[edit]')
				{
					jQuery(this).parent().showApplicants();
				}
				else
				{
					jQuery(this).parent().hideApplicants();
				}
			});



		}
		else if (jQuery(this).find('div.applicant-cklist').length > 0)
		{
			jQuery(this).find('span').text('[close]');
		}

		if(jQuery(this).find('div.applicant-cklist').length > 0)
		{
			jQuery(this).find('span').show();
		}
		
		return this;
	};


	/* hide the applicants           */

	jQuery.fn.hideApplicants = function()
	{
		jQuery(this).removeClass('ck-background');
		jQuery(this).removeClass('ck-procedure');
		jQuery('div.applicant-cklist').hide();


/* I don't like having to show them all but if clicking a new applicant instead of using close   */
/* we don't know which one we came from and the name list will not be shown.                     */
		jQuery(this).parent().find('ul.applicant-names').show();

		jQuery('span.edit').text('[edit]');

		return this;
	};


	/* create an unordered list of applicants from the div applicant-cklist                         */
	/* This puts all applicants in the applicant name list and then shows the ones that             */
	/* are checked...this is to keep the same order of applicant names as in the applicant-cklist   */
	jQuery.fn.getApplicantNames = function()
	{

		var names = '<ul class="applicant-names">';

		/* find all the applicant in the applicant checklist and make an unordered list with their names  */
		jQuery(this).find('div.applicant-cklist ul').children('li').each(function()
		{
			var m_id = jQuery(this).find('input').attr('id');
			var m_label = jQuery(this).find('label').text();
			names += '<li class="'+m_id+'">'+m_label+'</li>';
		});

		names += '</ul>';

		/* append the unordered list of names to the current list item                                     */
		jQuery(this).find('span.edit').after(names);

		jQuery(this).find('ul.applicant-names').hide();
		return this;
	}


	/* if the user moves off of the condition list item, hide the applicants */

	jQuery('td.q fieldset div.condition-cklist ul li').blur(function()
	{
		jQuery(this).hideApplicants();
	});


	/* if a condition is selected, show the applicants */

	jQuery('td.q fieldset div.condition-cklist ul li label').click(function()
	{

		jQuery(this).parents('tr').find('td.a input[value="true"]').attr({checked:'checked'});

		if (jQuery(this).find('input').is(':checked'))
		{
			jQuery(this).parents('li').showApplicants();
		}
		else
		{
			var parent_li = jQuery(this).parents('li');
			parent_li.hideApplicants();
			parent_li.find('ul.applicant-names li').hide();
			parent_li.find('div.applicant-cklist li input').attr({checked:false});
			parent_li.find('span.edit').hide();
		}
	} );


	/* unbind the inner list (applicant-cklist) from show/hide       */

	jQuery('td.q fieldset div.condition-cklist ul li ul li label').unbind('click');


	/* when an applicant is clicked, add them to the name list        */
	/* show/hide the list item that is checked/unchecked              */

	jQuery('td.q fieldset div.condition-cklist ul li ul li label').click(function()
	{

		var my_input = jQuery(this).find('input');
		var my_id = my_input.attr('id');
		var app = 'ul.applicant-names li.'+my_id;


		if (my_input.is(':checked'))
		{
			var parent_li = jQuery(this).parents('li.ck-procedure');
			if (parent_li.find('ul.applicant-names').length == 0)
			{
				parent_li.getApplicantNames();
				parent_li.find('ul.applicant-names li').hide();
			}

			jQuery(app).show();
			parent_li.find('span.edit').show();

		}
		else
		{
			var my_id = jQuery(this).id;
			jQuery(app).hide();
		}
	});


	/* on page load, if a condition is yes and applicants are checked, display the applicants in the name list  */

	jQuery('td.q fieldset div.condition-cklist ul.cc').children('li').children('label:last-child').children('input:checked').each(function()
	{
		var parent_li = jQuery(this).parents('li');

/* -- this should be split out witht the insert edit code on line 622  --     */
		if (parent_li.find('span.edit').length == 0 && parent_li.find('div.applicant-cklist').length > 0)
		{
			var edit_close = '<span class="edit">[edit]</span>';
			parent_li.append(edit_close);

			/* bind the click event to the new edit span                                                       */
			parent_li.find('span.edit').click(function()
			{
				if (jQuery(this).text() == '[edit]')
				{
					jQuery(this).parent().showApplicants();
				}
				else
				{
					jQuery(this).parent().hideApplicants();
				}
			});
		}

		/* show the applicant name list and  hide the list items then show the list item that are checked           */
		/* in the applicant-cklist list                                                                             */
		jQuery(this).parents('li').getApplicantNames();

		parent_li.find('ul.applicant-names').show();
		parent_li.find('ul.applicant-names li').hide();

		parent_li.find('div.applicant-cklist ul li label input:checked').each(function()
		{
			var my_id = jQuery(this).attr('id');
			var app = 'ul.applicant-names li.'+my_id;
			jQuery(app).show();
		});

	});


	/* show or hide additional health question information */

	jQuery('td.a input[value="true"]').click(function()
	{
		var row = jQuery(this).parents('tr');
		row.find('td.q fieldset').show();
		row.removeClass('error');
	} );


	/* if no is selected, remove all previously entered information  */
	jQuery('td.a input[value="false"]').click(function()
	{
		var row = jQuery(this).parents('tr');
		var fieldset = row.find('td.q fieldset');
		row.find('td.q fieldset:not(".always-show")').hide();
		fieldset.find('input:checkbox:checked').attr({checked:''});
		fieldset.find('input:textbox').val('');
		fieldset.find('textarea').val('');
		fieldset.hideApplicants()
		fieldset.find('.condition-cklist li').removeClass('ck-background');
		fieldset.find('ul li span.edit').remove();
		fieldset.find('ul.applicant-names').remove();
		fieldset.find('td.input-error').removeClass('input-error');
		fieldset.find('tr.error').removeClass('error');
		row.removeClass('error');
	} );


	/* on pageload, if the yes radio button is selected, show the addtional information */

	jQuery('td.a input:checked').each(function()
	{
		if (jQuery(this).val() == 'true')
		{
			jQuery(this).parents('tr').find('td.q fieldset').show();
		}
	} );


	/*--------------------------------------------------------------*/
	/* Other Coverage,                                              */
	/* About Your Health                                            */
	/*--------------------------------------------------------------*/

	/* validation on continue */

	jQuery('#save-continue-button input').click(function(event)
	{
		jQuery('td.q table.table-add-q td.input-error').removeClass('input-error');

		/* if a radio button is visible is not selected for a question, put the error class on the row and show the top error message */
		jQuery('td.a:visible:not(:has(input:radio:checked))').parents('tr').setErrorHQ(event);

		/* validate all rows that are marked yes                   */
		jQuery('td.a input:checked[value="true"]').parents('tr').validateHQ(event);

		jQuery('fieldset input:not("input.v-optional"):text:visible[value=""]').validateTextbox(event);

		jQuery('input.v-date-month:not(.v-optional):visible').validateDateMonth(event);
		jQuery('input.v-date-day:not(.v-optional):visible').validateDateDay(event);
		jQuery('input.v-date-year:not(.v-optional):visible').validateDateYear(event);

		jQuery('div.v-date:not(.v-optional):visible').validateDate(event);

		jQuery('div.v-date-optional').validateDateOptional(event);

	} );



	jQuery('table.table-add-q textarea').keypress(function(event)
	{
		jQuery(this).limitCharacters(event,249);
	});


	jQuery('table.table-add-q textarea').each(function()
	{
		count = 250 - jQuery(this).val().length;
		jQuery(this).parent().find('span.char').text(count);
	});


	/* if all applicants is checked, check all the individual applicants and disable the individual checkboxes */
	/* if all applicants is not checked, enable the checkboxes  */

	jQuery.fn.checkAllApplicants = function()
	{

		if (jQuery(this).is(':checked'))
		{
			jQuery(this).parents('ul.fields').find('li label input:not(".app-all")').attr({disabled:'disabled',checked:'checked'});
		}
		else
		{
			jQuery(this).parents('ul.fields').find('li label input:not(".app-all")').removeAttr('disabled');
		}
		return this;
	}


	/* when All Applicants is clicked, check/disable or enable the corresponding applicants   */

	jQuery('input.app-all').click(function()
	{
		jQuery(this).checkAllApplicants();
	});


	/* on page load, if All Applicants is checked, check and disable the corresponding applicants  */

	jQuery('input.app-all:checked').each(function()
	{
		jQuery(this).checkAllApplicants();
	} );


 	/* show additional questions
	jQuery('td.q fieldset ul.fields li label').click(function()
	{


		if (jQuery(this).find('input').is(':checked'))
		{
			jQuery(this).next('div.additional-q').show();
		}
		else
		{
			jQuery(this).next('div.additional-q').hide();
		}
	} );

*/





	/* validate HQ                                                  */
	/* show the appropriate error indicators                        */

	jQuery.fn.validateHQ = function(event)
	{

		return this.each(function()
		{
			/* if radio button Yes is selected and there are checkboxes, at least one must be checked */
			var this_tr = jQuery(this);
			var checkboxes = this_tr.find('td.q fieldset input[type="checkbox"]');
			if ((checkboxes.val() != undefined) && (! checkboxes.is(':checked')))
			{
					this_tr.setErrorHQ(event);
			}

			if (this_tr.find('td.q table.table-add-q').length > 0)
			{
				this_tr.find('td.q table.table-add-q td input:checked').parents('td').next('td').find('input:text[value=""]').validateHQTextbox(event);
				this_tr.find('td.q table.table-add-q td input:checked').parents('td').next('td').find('textarea:visible[value=""]').validateTextarea(event)
				this_tr.find('td.q table.table-add-q td input:checked').parents('td').next('td').find('input.v-date-day').validateDateDay(event, 'days_nd_label')
				this_tr.find('td.q table.table-add-q td input:checked').parents('td').next('td').find('input.v-date-month').validateDateMonth(event, 'months_nd_label')
				this_tr.find('td.q table.table-add-q td input:checked').parents('td').next('td').find('input.v-date-year').validateDateYear(event,'years_nd_label')
				this_tr.find('td.q table.table-add-q td input:checked').parents('td').next('td').find('input.v-number').validateNumber(event,'number_label')
				if (this_tr.find('.input-error').length > 0)
				{
					this_tr.addClass('error');
				}

			}
			else
			{
				this_tr.find('td.q fieldset input:not(.v-optional):text[value=""]').validateHQTextbox(event);
			}

			this_tr.find('td.q div.condition-cklist input.v-condition:checked').each(function()
			{

				if (jQuery(this).parents('li').find('div.applicant-cklist input:checked').length == 0)
				{
					this_tr.setErrorHQ(event);
				}
			});



		} );
	}


	/* Validate that if the textboxes exist, they are not empty  */

	jQuery.fn.validateHQTextbox = function(event)
	{

		if ((jQuery(this).val() != undefined) && (jQuery(this).length != 0))
			{
				jQuery(this).parents('tr').setErrorHQ(event);
			}
		return this;
	}


	/* show the appropriate error indicators on the row       */

	jQuery.fn.setErrorHQ = function(event)
	{

		if (jQuery(this).length != 0)
		{
			jQuery(this).displayTopError(event).addClass('error');
		}
		return this;
	}


	/* remove the error class if they fix the problem */

	jQuery('td.q fieldset input').click(function()
	{
		jQuery(this).parents('tr').removeClass('error');
	} );

	jQuery('td.q fieldset textarea').click(function()
	{
		jQuery(this).parents('tr').removeClass('error');
	} );


	/*--------------------------------------------------------------*/
	/* Final Review                                                 */
	/* Review Application                                           */
	/*--------------------------------------------------------------*/

	/* validate that any visible signature boxes are checked  */
	jQuery.fn.validateSignature = function(event)
	{
		jQuery('fieldset.signature input[type="checkbox"]:not(:checked)').validateAllCheckboxChecked(event,'sig001');


		jQuery('fieldset.signature input:not("input.v-optional"):text[value=""]').validateTextbox(event);

		return this;
	}


	jQuery('fieldset.signature ul li label').click(function()
	{
		jQuery(this).removeClass('input-error');
	});

	/*--------------------------------------------------------------*/
	/* Final Review                                                 */
	/*--------------------------------------------------------------*/

	jQuery('li#accept-pay-button input').click(function(event)
	{
		jQuery(this).validateSignature(event);
	});


	/*--------------------------------------------------------------*/
	/* Review Application                                           */
	/*--------------------------------------------------------------*/

	jQuery('li#submit-button input').click(function(event)
	{

		jQuery(this).validateSignature(event);
	});




	/*--------------------------------------------------------------*/
	/* Payment                                                      */
	/*--------------------------------------------------------------*/

	jQuery('li#confirm-payment-button input').click(function(event)
	{
		jQuery('div.radio-button').validateRadio(event);

		jQuery('fieldset input:not("input.v-optional"):text:visible[value=""]').validateTextbox(event);

		jQuery('fieldset input.v-number-length:visible').validateNumberLength(event);
		jQuery('fieldset input.v-number:visible').validateNumber(event);

		jQuery('div.v-account-numbers:visible').validateSame(event,'acct_num_label');

		jQuery('fieldset input.v-cc-number:visible').validateCreditCardNumber(event);
		jQuery('fieldset input.v-routing-number:visible').validateRoutingNumber(event);

	});


	/*------------------------------------------------------*/
	/* Applicant Information Common methods                 */
	/*------------------------------------------------------*/

	jQuery.fn.applicantInfoCommon = function(event)
	{

		jQuery('fieldset').validateRadio(event,'resident_label');
		jQuery('fieldset.v-resident input#resident-false:checked').validateRadioChecked(event, 'req004');
		jQuery('fieldset input:not("input.v-optional"):text:visible[value=""]').validateTextbox(event);

		jQuery('input.v-date-month:visible').validateDateMonth(event);
		jQuery('input.v-date-day:visible').validateDateDay(event);
		jQuery('input.v-date-year:visible').validateDateYear(event);

		jQuery('fieldset input.v-number-length:visible').validateNumberLength(event);
		jQuery('fieldset input.v-number:visible').validateNumber(event);

		jQuery('div.v-dob:visible').validateDOB(event);

		jQuery('div.v-ss-num:visible').validateSocialSecurity(event);

		jQuery('div.v-phone-optional:visible').validatePhoneOptional(event);


	}


	/*-----------------------------------------------------------------------*/
	/* Applicant Information   - health                                      */
	/*-----------------------------------------------------------------------*/
	jQuery('#applicantInformation2000cfm li#continue-button input').click(function(event)
	{

		jQuery(this).applicantInfoCommon(event);

		jQuery('div.v-height:visible').validateHeight(event);
		jQuery('input.v-weight:visible').validateGTZero(event);

	});


	/*---------------------------------------------------------------------------------------*/
	/* Applicant Information  - medicare                                                     */
	/*---------------------------------------------------------------------------------------*/

	jQuery('#medicare_applicantInformation2000cfm li#continue-button input').click(function(event)
	{

		jQuery(this).applicantInfoCommon(event);

	});


	/*---------------------------------------------------------------------------------------*/
	/* Applicant Information  - dental                                                       */
	/*---------------------------------------------------------------------------------------*/

	jQuery('#dental_applicantInformation2000cfm li#continue-button input').click(function(event)
	{

		jQuery(this).applicantInfoCommon(event);

	});


	/*--------------------------------------------------------------*/
	/* Before You Begin                                             */
	/*--------------------------------------------------------------*/
	/* when the continue or submit button is clicked, validate any radio buttons  */

	jQuery('#beforeYouBegin1000cfm li#continue-button input').click(function(event)
	{
		jQuery('fieldset').validateRadio(event,'eff_date_label');
	});

	/*--------------------------------------------------------------*/
	/* Before You Begin                                             */
	/*--------------------------------------------------------------*/
	/* when the continue or submit button is clicked, validate any radio buttons  */

	jQuery('#dental_beforeYouBegin1000cfm li#continue-button input').click(function(event)
	{
		jQuery('fieldset').validateRadio(event,'eff_date_label');
	});

	/*--------------------------------------------------------------*/
	/* Before You Begin   - Medicare                                */
	/*--------------------------------------------------------------*/

	jQuery('#medicare_beforeYouBegin1000cfm li#continue-button input').click(function(event)
	{
		jQuery('fieldset select').validateDropdown(event);
	});


	/*--------------------------------------------------------------*/
	/* Register                                                     */
	/*--------------------------------------------------------------*/

	jQuery('#register3000cfm li#save-continue-button input').click(function(event)
	{
		jQuery('fieldset input:password[value=""]').validateTextbox(event);
		jQuery('input.v-min-len-6').validateMinLength(event,6);
		jQuery('div.v-email').validateEmail(event);
		jQuery('fieldset.v-pw').validatePassword(event);
	});


	/*--------------------------------------------------------------*/
	/* All                                                          */
	/*--------------------------------------------------------------*/

	jQuery('li#submit-button input').click(function(event)
	{
		jQuery('fieldset').validateRadio(event);

	});
	
	
	/*--------------------------------------------------------------*/
	/* Provider 360 App                                                         */
	/*--------------------------------------------------------------*/
	
	$(".tooltip").tooltip({
		bodyHandler: function()
		{
			var	my_class = $(this).attr('class').replace('tooltip ','');;
			var my_div = 'div.'+my_class;
			var my_html = ($(my_div).html() == null) ? 'Tooltip is not available' : $(my_div).html();
			return my_html;
		},
		showURL: false
	});
	
	$('#mailing-address').hide();
	
	$('#same-address').click(function() 
	{ 
		if ($(this).is(':checked'))
		{ 
			$('#mailing-address').slideDown() 
		} 
		else 
		{ 
			$('#mailing-address').slideUp()
			$('input#mailing-street').val('');
			$('input#mailing-apt').val('');
			$('input#mailing-city').val('');
			$('select#mailing-state').val('AL');
			$('input#mailing-zip').val('');
		}
	});
		
	
	jQuery('.locationBlur1').blur(function(){ 
			if ($('input#extra1-name').val() > '')
			{
				$('#extra1').html('<a href="#" id="edit1" onclick="return false;" class="plus">[&ndash;]</a> ' + $('input#extra1-name').val());
				$('#container1').slideDown('slow');
			}
	});
	jQuery('.locationBlur2').blur(function(){
			if ($('input#extra2-name').val() > '')
			{
				$('#extra2').html('<a href="#" id="edit2" onclick="return false;" class="plus">[&ndash;]</a> ' + $('input#extra2-name').val());
				$('#container2').slideDown('slow');
			}
	});
	jQuery('.locationBlur3').blur(function(){
			if ($('input#extra3-name').val() > '')
			{
				$('#extra3').html('<a href="#" id="edit3" onclick="return false;" class="plus">[&ndash;]</a> ' + $('input#extra3-name').val());
				$('#container3').slideDown('slow');
			}
	});
	
	jQuery('#add-location').click(function(){ 
			if(!$(this).hasClass('disabledbutton'))
			{
				Address = $('#addressCount').val()
				if(Address < 3)
				{
					$('#add-location').removeClass('disabledbutton');
					
					if	   (($('#extra1-collapse').css('display') == 'none') && ($('#container1').css('display') == 'none'))
						{ 
							newAddress = parseInt($('#addressCount').val()) + 1;
							if (($('input#extra2-name').val() > ''))
								{
									$('#extra2-collapse').slideUp('slow');
									$('#container2').slideUp('slow');
									$('#extra2').html('<a href="#" id="edit2" onclick="return false;" class="plus">[+]</a> ' + $('input#extra2-name').val());
									$('#container2').slideDown('slow');
								}
							if (($('input#extra3-name').val() > ''))
								{
									$('#extra3-collapse').slideUp('slow');
									$('#container3').slideUp('slow');
									$('#extra3').html('<a href="#" id="edit3" onclick="return false;" class="plus">[+]</a> ' + $('input#extra3-name').val());
									$('#container3').slideDown('slow');
								}
							$('#extra1-collapse').slideDown('slow');
						}
					if	   (($('#extra2-collapse').css('display') == 'none') && ($('#container2').css('display') == 'none') && ($('input#extra1-name').val() > ''))
						{ 
							newAddress = parseInt($('#addressCount').val()) + 1;
							if (($('input#extra1-name').val() > ''))
								{
									$('#extra1-collapse').slideUp('slow');
									$('#container1').slideUp('slow');
									$('#extra1').html('<a href="#" id="edit1" onclick="return false;" class="plus">[+]</a> ' + $('input#extra1-name').val());
									$('#container1').slideDown('slow');
								}
							if (($('input#extra3-name').val() > ''))
								{
									$('#extra3-collapse').slideUp('slow');
									$('#container3').slideUp('slow');
									$('#extra3').html('<a href="#" id="edit3" onclick="return false;" class="plus">[+]</a> ' + $('input#extra3-name').val());
									$('#container3').slideDown('slow');
								}
							$('#extra2-collapse').slideDown('slow');
						}
					if	   (($('#extra3-collapse').css('display') == 'none') && ($('#container3').css('display') == 'none') && ($('input#extra1-name').val() > '') && ($('input#extra2-name').val() > ''))
						{ 
							newAddress = parseInt($('#addressCount').val()) + 1;
							if (($('input#extra1-name').val() > ''))
								{
									$('#extra1-collapse').slideUp('slow');
									$('#container1').slideUp('slow');
									$('#extra1').html('<a href="#" id="edit1" onclick="return false;" class="plus">[+]</a> ' + $('input#extra1-name').val());
									$('#container1').slideDown('slow');
								}
							if (($('input#extra2-name').val() > ''))
								{
									$('#extra2-collapse').slideUp('slow');
									$('#container2').slideUp('slow');
									$('#extra2').html('<a href="#" id="edit2" onclick="return false;" class="plus">[+]</a> ' + $('input#extra2-name').val());
									$('#container2').slideDown('slow');
								}
							$('#extra3-collapse').slideDown('slow');
						}
					
					$('#addressCount').val(newAddress);
					
					if(newAddress == 3)
					{
						$('#add-location').addClass('disabledbutton');
					}
					
				}
				else if (Address == 3)
				{
					$('#add-location').addClass('disabledbutton');
				}
			}
		});
		$('#removeAdd1').click(function(){ $('#addressCount').val(parseInt($('#addressCount').val()) - 1); $('#extra1-collapse').slideUp('slow'); $('#container1').slideUp('slow'); $('#add-location').removeClass('disabledbutton'); $('#extra1-name').val(''); $('#extra1-street').val(''); $('#extra1-apt').val(''); $('#extra1-city').val(''); $('#extra1-state').val('AL'); $('#extra1-zip').val(''); $('#extra1-phone').val(''); $('#extra1-phone2').val(''); $('#extra1-phone3').val(''); $('#extra1-fax').val(''); $('#extra1-fax2').val(''); $('#extra1-fax3').val('');  });
		$('#removeAdd2').click(function(){ $('#addressCount').val(parseInt($('#addressCount').val()) - 1); $('#extra2-collapse').slideUp('slow'); $('#container2').slideUp('slow'); $('#add-location').removeClass('disabledbutton'); $('#extra2-name').val(''); $('#extra2-street').val(''); $('#extra2-apt').val(''); $('#extra2-city').val(''); $('#extra2-state').val('AL'); $('#extra2-zip').val(''); $('#extra2-phone').val(''); $('#extra2-phone2').val(''); $('#extra2-phone3').val(''); $('#extra2-fax').val(''); $('#extra2-fax2').val(''); $('#extra2-fax3').val('');  });
		$('#removeAdd3').click(function(){ $('#addressCount').val(parseInt($('#addressCount').val()) - 1); $('#extra3-collapse').slideUp('slow'); $('#container3').slideUp('slow'); $('#add-location').removeClass('disabledbutton'); $('#extra3-name').val(''); $('#extra3-street').val(''); $('#extra3-apt').val(''); $('#extra3-city').val(''); $('#extra3-state').val('AL'); $('#extra3-zip').val(''); $('#extra3-phone').val(''); $('#extra3-phone2').val(''); $('#extra3-phone3').val(''); $('#extra3-fax').val(''); $('#extra3-fax2').val(''); $('#extra3-fax3').val('');  });
		
		
	$("#add-button").click(function(){	
			var total = parseFloat($('#hiddentotal').val());
			var newtotal = total+1;
			
			$('.addition').unbind('click');
			$('.remove').unbind('click');
			$('.showContainer').unbind('blur');
			
			$('#hiddentotal').val(newtotal) ;
			$("#physician-id").clone(true).insertAfter('.container:last').attr('id', "physician-" + newtotal);
			$('#physician-' + newtotal).html($('#physician-' + newtotal).html().replace(/-id/gi,'-' + newtotal));
			
			$('.addition').click( function() { jQuery(this).toggleHeader() });
			$('.remove').click( function() { jQuery(this).deletePhys() });
			$('.showContainer').blur(function() { $(this).showContainer() });
		});	
	
	$('.showContainer').blur(function() { $(this).showContainer() });
	
	$.fn.showContainer = function()
	{
		if ($(this).val() > '')
		{
			var name =  $(this).val();
			$(this).parents('div.container').children('div.header').children('div.addition').html('<a href="#" id="" onclick="return false;" class="plus">[&ndash;]</a> ' + name);
			$(this).parents('div.container').children('div.header').slideDown('slow');
		}
	}
	
		
	$('.addition').click( function() { $(this).toggleHeader() });	
		
	$.fn.toggleHeader = function()	
	{
		$('#' + $(this).attr("id") + "-collapse" ).slideToggle(); 
		($('#' + $(this).attr("id") + ' a.plus').html() == '[+]') ? $('#' + $(this).attr("id") + ' a.plus').html('[&ndash;]') : $('#' + $(this).attr("id") + ' a.plus').html('[+]');
	}
	
	$('.remove').click( function() { jQuery(this).deletePhys() });	
		
	$.fn.deletePhys = function()	
	{
		$(this).parents('div.container').remove(); 
	}
	
	/*-----------------------------------------------------------------------*/
	/* Applicant Information   - health                                      */
	/*-----------------------------------------------------------------------*/
	jQuery('#applicantInformation li#continue-button input').click(function(event)
	{
		jQuery(this).applicantInfoCommon(event);
		jQuery('div.v-height:visible').validateHeight(event);
		jQuery('input.v-weight:visible').validateGTZero(event);
	});
	

});


