$(document).ready(function (){
	
	// console.log(section)
	$('form.form_section').submit(function (){
		$('.bttn.next').click();
	});
	// cohab controls start
	$('#cohabyes').click(function (){		$('#cohabInfo').show();	});
	$('#cohabno').click(function (){		$('#cohabInfo').hide();	});
	$('#cohabyes:checked').trigger('click').click();
	$('#cohabno:checked').trigger('click').click();
	// cohab controls end;
	// children controls start;
	$('#moreChildrenSelect').change(function (e){
		var num = parseInt($(this).val());
		var currLen = $('.newChild').length || 0;
		if(num > currLen){
			for(i=0;i<(num-currLen);i++){
				var chnum = i+currLen;
				var clone = $('.childtemplate').clone();
				clone.find('.csex.male').attr({'name':'children['+chnum+'][sex]','id':'children_sex_m_'+chnum}).siblings('[for=csexm]').attr('for','children_sex_m_'+chnum);
				clone.find('.csex.female').attr({'name':'children['+chnum+'][sex]','id':'children_sex_f_'+chnum}).siblings('[for=csexf]').attr('for','children_sex_f_'+chnum);
				clone.find('.chday').attr({'name':'children['+chnum+'][day]'});
				clone.find('.chmonth').attr({'name':'children['+chnum+'][month]'});
				clone.find('.chyear').attr({'name':'children['+chnum+'][year]'});
				clone.removeClass('childtemplate').addClass('newChild').appendTo($('.childtemplate').parents('form')).show();
			}
			$('.buttonset_wait:not(.buttonset_done)').addClass('buttonset_done').buttonset();
		}
	});
	// left section selector
	$('.sec_link').die().live('click',function (e){
		e.preventDefault();
		// alert('Go on');
		if(!$(this).hasClass('inactive')){
			window.location.hash = $(this).attr('href');
			$('.sections').hide()
			$('.sections').eq($(this).attr('rel')-1).show();
			$('.bttn.finish').hide();
			$('.bttn.next:not(.finish)').show();
			if($(this).attr('rel')==1){
				$('.bttn.prev').hide();	
			}
			left_sec_handle();
		}
		return;
	});
	// left section selector END
	if($('#moreChildrenSelect').val()){
		$('#moreChildrenSelect').change();
	}
	$('.removeChildren').live('click',function (e){
		e.preventDefault();
		$(this).parents('.newChild').remove();
		var currLen = $('.newChild').length || 0;
		$('#moreChildrenSelect').val(currLen);
		return false;
	});
	// more children controls end;
	// banks start
	$('#showBank').click(function (e){
		e.preventDefault();
		$(this).remove();
		$('#banks_list').queue(function (){
			jQuery('#banks_list').children('input:not(:checked)').hide()
			.each(function(i,n){
			   var att = jQuery(n).attr('id');
			   jQuery(n).siblings('label[for='+att+']').remove();
			   jQuery(n).remove();
			});
			$(this).dequeue();
			$('td p:empty').remove();
			$('#otherBankDiv').show();
		});
		$('#otherBank').focus();
		return false;
	});
	// add more banks
	$('#otherBankDivAdd').click(function (e){
		e.preventDefault();
		var tmp = $('#otherBank').val();
		
		var b = $('#banks_list label').filter(function(keyword) {
			return ($(this).text().toLowerCase() == tmp.toLowerCase()); 
		});
		if($('#otherBank').val().length && b.length ===0 ){
			$('#banks_list').buttonset('destroy');
			$('<input type="checkbox" checked="checked" name="bank[]" value="'+$('#otherBank').val()+'" id="bank'+$('#otherBank').val()+'" /><label for="bank'+$('#otherBank').val()+'">'+$('#otherBank').val()+'</label>')
			.appendTo('#banks_list').queue(function (){
				// $('#banks_list').buttonset('destroy').queue(function (){$(this).buttonset().dequeue()});
				$('#otherBank').val('').focus();
				$(this).dequeue();
			});
		}else{
			$('#otherBank').val('').focus();
			$('#bankerror').html('<div class="ui-state-error ">Error: The bank is already on the list</div>').stop().delay(7000).queue(function (){
				$(this).children().slideUp().queue(function (){$(this).remove().dequeue();}).dequeue();
			});
		}
		$('#banks_list').buttonset('destroy')
		.buttonset();
		return false;
	});
	// add more banks
	$('#otherBank').keyup(function (e){
		//enter button
		e.preventDefault();
		if(e.which==13){
			$('#otherBankDivAdd').click();
		}
		return false; 
	})
	//banks end;
	//building society start
	$('#showBS').click(function (e){
		e.preventDefault();
		$(this).remove();
		$('#buildingSociety').slideUp().queue(function (){
			$(this).remove().dequeue();
			$('td p:empty').remove();
			$('#otherBSdiv').slideDown();
			$('#otherBS').attr('name','buildingSociety');
		});
		return false;
	});
	//building society end
	
	
	
	//Internet service provider
	$('#internetProvider').bind('change focus',function (){
		if($(this).val()=='other'){
			var inp = $('<input type="text" name="internetProvider" />').insertAfter($(this));
			$(this).unbind('change focus').remove();
			inp.focus();
		}
		
	});
	
	//internet service provider
	
	// other console contrls start
	$('#otherConsole').keyup(function (e){ 
			e.preventDefault();
		if(e.which == 13){
			$(this).siblings('.add').click();
		}
			return false;
	});
	$('.otherConsole.add').click(function (e){
		e.preventDefault();
		var tmp = $('#otherConsole').val();
		
		var b = $('#consolelist label').filter(function(keyword) {
			return ($(this).text().toLowerCase() == tmp.toLowerCase()); 
		});
		if($('#otherConsole').val().length && b.length ===0 ){
			$('#consolelist').buttonset('destroy');
			$('<input type="checkbox" checked="checked" name="bank[]" value="'+$('#otherConsole').val()+'" id="bank'+$('#otherConsole').val()+'" /><label for="bank'+$('#otherConsole').val()+'">'+$('#otherConsole').val()+'</label>')
			.appendTo('#consolelist').queue(function (){
				$('#otherConsole').val('').focus();
				$(this).dequeue();
			});
		}else{
			$('#otherConsole').val('').focus();
			$('#consoleerror').html('<div class="ui-state-error ">Error: The bank is already on the list</div>').stop().delay(7000).queue(function (){
				$(this).children().slideUp().queue(function (){$(this).remove().dequeue();}).dequeue();
			});
		}
		$('#consolelist').buttonset('destroy')
		.buttonset();
		return false;
	});
	$('#showConsole').click(function (e){
		e.preventDefault();
		$('#otherConsoleDiv').toggle(1000);
		return false;
	});
	// other console contrls end 
	// Special none values on check boxes
		// this clears any previews checked values that
	$('.none_radio').click(function (e){
		$(this).siblings('input').attr('checked',false);
	});
	$('.none_radio:checked').click();
	// Special none values END
	// subscribe button, if pop ups are disallowed
	$('.pp_pop_killer').die().live('click',function (e){
		e.preventDefault();
		$('form[target="lmrpp"]').attr('target','').submit();
		$(this).text('clicked').delay(500).queue(function (){
			$('#pp_pop_kill_text').fadeOut(2500);
			$(this).dequeue();
		});
		return;
	});
	// subscribe button, if pop ups are disallowed
	//smoke brand//
	if($('input[name=smoke]:checked').val()=='yes'){$('#smokeInfo').show();}
	$('input[name=smoke]').click(function (){
		if($('input[name=smoke]:checked').val()=='yes'){$('#smokeInfo').show();}
		else{$('#smokeInfo').hide();}
	});
	//smoke brand//
	
	//medical//
	$('#medical').keyup(function (e){
		if(e.which==13){
			$(this).siblings('#add_condition').click();
		}
	});
	$('#add_condition').click(function (e){
		e.preventDefault();
		var d 				= new Date();
		var timer 			= d.getTime();
		var val 			= $('#medical').val();

		var already_in_list  = false;
		var list = $('#medical').siblings('.buttonset').find('label');
		for(i=0;i<list.length;i++){
			if(list.eq(i).text()==val){
				already_in_list = true;
			}
		}
		
		if(val.length && already_in_list==false){
			var but 	= $('<input type="checkbox" checked="checked" id="'+timer+'" name="medical" /><label for="'+timer+'">'+val+'</label>');
			$(this).siblings('.buttonset').buttonset('destroy');
			$(this).siblings('.buttonset').append(but).buttonset();
		}else{
			$(this).siblings('.ui-state-error').slideDown(500).delay(5000).queue(function (){
				$(this).slideUp(500).dequeue();
			});
		}
		return false;
	});
	//medical//
		
	
	
	//computers//
	$('#com_none,label[for=com_none]').bind('change',function (){
		// if(!$(this).is(':checked')){
			$(this).siblings().not('label[for=com_none]').removeClass('ui-state-active').attr('checked',false);
			// $(this).attr('checke',false).removeClass('ui-state-active');
		// }else{
			// $(this).attr('checke',true).addClass('ui-state-active');
		// }
	}).siblings('input,label').not('#com_none,label[for=com_none]').bind('change',function (){
		$('#com_none,label[for=com_none]').removeClass('ui-state-active').attr('checked',false);
	});
	//computers//
	// add_mob_os mobile
	$('#add_mob_os').click(function (e){
		e.preventDefault();
		if(!$('#add_mob_os_text').is(':visible')){
			$('#add_mob_os_text').show();
			$(this).html('Add');
		}else{
			var val 	= $('#add_mob_os_text').val();
			// .toLowerCase
			var valid	= val.replace(' ','');
			if(val && $('#mobos_'+valid).length==0){
				$('<input name="mob_os" value="'+val+'" type="radio" checked="checked" id="mobos_'+valid+'" />			<label for="mobos_'+valid+'">'+val+'</label>')
					.appendTo($(this).siblings('.buttonset'));
				$(this).siblings('.buttonset').buttonset('destroy').buttonset();
				
				
			}else{
				$(this).siblings('.ui-state-error').slideDown().delay(5000).queue(function (){
					$(this).slideUp().dequeue();
				});
			}
		}
		return;
	});
	$('#add_mob_os_text').keydown(function (e){
		if(e.which == 13){
			e.preventDefault();
			$(this).siblings('#add_mob_os').click();
			$(this).focus();
			return;
		}
	});
	// add_mob_os mobile os
	// tablet pc
	$('.tablet_option').click(function (){
		var val = $(this).val();
		if(val=='yes'){
			$('.table_pc_table').show();
		}else{
			$('.table_pc_table').hide();
		}
	});
	$('.tablet_option:checked').click()
	// tablet pc
	//access web at
	// web_access_at
	
	$('#web_other').click(function (){
		$('.web_access_at').show();
	});
		//pet_insured
	$('.pet').click(function (){
		var val = $(this).val();
		$(this).parent().buttonset('destroy')
		if(val=='none'&&$(this).is(':checked')){
			$('#pet_insured,#petLabel').hide();
			$('.pet.none_radio').attr('checked','checked')
				.siblings('input')
				.attr('checked',false)
		}else{
			$('#pet_insured,#petLabel').show();//.val('');
			$('.pet.none_radio').attr('checked',false)
		}
		$(this).parent().buttonset()
	});
	$('.pet.none_radio:checked').click();
	//access web at
	// newspaper
	$('.newspaper').click(function (e){
		var val = $(this).val();
		$(this).parent().buttonset('destroy');
		if(val.toLowerCase() == 'none'&&$(this).is(':checked')){
			$('.newspaper:eq(0)').attr('checked','checked')
				.siblings('input')
				.attr('checked',false); 
			$('.newspaper_txt,.newspadd').hide();
		}else{
			$('.newspaper:eq(0)').attr('checked',false);
		}
		if(val.toLowerCase()=='other'){
			$('.newspaper_txt,.newspadd').show();
			$('.newspaper_txt').focus();
		}
		$(this).parent().buttonset();
	});
	$('.newspaper:eq(0):checked').click();
		//other enter key
	$('.newspaper_txt').keydown(function (e){
		if(e.which==13){
			$(this).siblings('a.newspadd').click();
		}
	});
		//othe click
	$('.newspadd').click(function (e){
		e.preventDefault();
		var val = $('.newspaper_txt').val();
		var chk = $('.newspaper:contains("'+val+'")').length;
		var dte = new Date();
		var tme = dte.getTime();
		$(this).siblings('.buttonset').buttonset('destroy');
		// console.log($(this).siblings('.buttonset').find('[value="'+val.trim()+'"]').length);
		if(val&&!chk&&!$(this).siblings('.buttonset').children('[value="'+val.trim()+'"]').length){
				var date = new Date();
				date = date.getTime();
				var name = val.trim();
				$('<input type="checkbox" name="newspaper[]" class="newspaper" id="newsp'+date+'" value="'+name+'" checked="checked" />						<label for="newsp'+date+'">'+name+'</label>')
				.appendTo($(this).siblings('.buttonset'));
		}else{
			$(this).siblings('.ui-state-error').slideDown().delay(5000).queue(function (){
				$(this).slideUp().dequeue();
			});
		}
		$(this).siblings('.buttonset').buttonset();
		return;
	});
	// newspaper
	//verify 
	$('#verify_ajax').dialog({
		autoOpen :false,
		modal: true,
		width: 700,
		position: 'center center'
	});
	$('.ui-widget-overlay').die('click').live('click',function(){
		$('#verify_ajax').html('Please Wait...').dialog('close')
	});
	$('#email-verify').click(function(e){
		e.preventDefault(); 
		$d = $('form.form_section1').serialize();
		// if(!$d.length){
			// $('#verify_ajax').html('Please add a personal email address or work address').dialog('open');
			// return false;
		// }
		$.ajax({
			url 	:site_url+'home/verify/',
			type	:'POST',
			dataType:'html',
			data	:$d,
			success :function(data){
				
				$('#verify_ajax').html(data).dialog('open');
			},
			error	:function(){
				$('#verify_ajax').html('<div class="ui-state-error p10">There was a problem while trying to get page information</div>');
			}
		});
		$('#verify_ajax').dialog('open');
		return false;
	});
	//verify
});





