jQuery.noConflict();

		 jQuery(document).ready(function() {

		 		 
		 		 jQuery('#userType').change(function() {
		 		   rebuildDropdown();
		 		   rebuildProductType();
		 		 });



		 		   // check if cookie is set and set login form as required, else set form by the top level nav tab that is selected:
		 		 if (jQuery.cookie('mlcLoginProductType') && jQuery.cookie('mlcLoginUserType') ) {

					 jQuery('#userType').val(jQuery.cookie('mlcLoginUserType'));
					 rebuildDropdown();
					 jQuery('#productType').val(jQuery.cookie('mlcLoginProductType'));
					 jQuery('input[name=loginOptions]')[0].checked = true;

				 } else {
					 jQuery('select#productType').selectmenu('destroy');
					 rebuildDropdown();
				 }

		 		 // submit login form, checking if we need to remember details for next time, and set a cookie if we do.
		 		  jQuery('#loginForm').submit(function() {
		 		 		  if (jQuery('#productType').val() == '') {
		 		 		 		 alert('Please select a product or platform');
		 		 		 		 return false;
		 		 		  }
		 		 		  if (jQuery('input[name=loginOptions]').is(':checked')) {
		 		 		 		//todo, set cookies to be site specific, just uncomment as necessary:
		 		 		 		jQuery.cookie('mlcLoginProductType', jQuery('#productType').val(), { expires: 1000, domain: 'mlc.com.au' });
		 		 		 		jQuery.cookie('mlcLoginUserType', jQuery('#userType').val(), { expires: 1000, domain: 'mlc.com.au' });
		 		 		  } else {
		 		 		 		jQuery.cookie('mlcLoginProductType', null, { expires: 1000, domain: 'mlc.com.au' });
   		 		 		 		jQuery.cookie('mlcLoginUserType', null, { expires: 1000, domain: 'mlc.com.au' });
		 		 		  }
		 		 		  switch (jQuery('#productType').val())
		 		 		  {
		 		 		  case 'mlc_personal':
								jQuery('#loginForm').attr('action','/mlc/im_with_mlc/personal');								
		 		 		 		 break;
		 		 		  case 'mlc_business':
			 					jQuery('#loginForm').attr('action','/mlc/im_with_mlc/business');
		 		 		  		 break;	  		 
		 		 		  case 'mlc_adviser':
						 		jQuery('#loginForm').attr('action','/mlc/im_with_mlc/adviser');
		 		 		 		 break;
		 		 		  case 'mlc_institutional':
								jQuery('#loginForm').attr('action','/mlc/im_with_mlc/institutional');
		 		 		 		 break;
		 		 		  case 'superezy_business':
		 		 		  		jQuery('#loginForm').attr('action','/mlc/im_considering_mlc/business/products_and_services/superannuation/mlc_superezy');		 		 		  		
		 		 		  		break;	
						  case 'superezy_adviser':
								jQuery('#loginForm').attr('action','/mlc/im_considering_mlc/adviser/my_clients/mlc_superezy ');
						  		break;	
		 		 		  case 'mlc_wealth':
			 					jQuery('#loginForm').attr('action','https://wealth.mlc.com.au/site/cms/web/public/login');
		 		 		  		 break;
		 		 		  }
		 		 		  return true;
		 		 		 });
		 		 		 
		 		 		 //jQuery('select#productType').selectmenu('destroy');
		 		 		 //rebuildDropdown();
		 		 		 
						 });
				
								 jQuery(function(){
				
										 jQuery('select#userType').selectmenu({
						icons: [{
							find: '.profileicon'
						}],
								 width: 187,
								 style: 'dropdown',
								 format: productFormatting
						 });
						 jQuery('select#productType').selectmenu({
						icons: [{
							find: '.typeicon'
						}],
		 		 		 		 width: 187,
		 		 		 		 style: 'dropdown',
		 		 		 		 menuWidth: 187,
		 		 		 		 format: productFormatting
		 		 		 });

		 		 });

		 		 var rebuildDropdown = function() {
		 		 		 switch (jQuery('#userType').val())
					{
					case 'personal':
						jQuery('select#productType')
						.selectmenu('destroy') //destroy the custom select menu
						.replaceWith('<select name="productType" id="productType"></select>'); //rebuild the select element
						jQuery('select#productType').append('<option value="">Login to...</option>');
						jQuery('select#productType').append('<option value="mlc_personal">MLC MasterKey | MLC Insurance</option>');
						jQuery('select#productType').append('<option value="mlc_wealth">MLC Navigator | MLC Wrap | Employer Super | Savings &amp; Investments</option>');					
						break;
					case 'business':
						jQuery('select#productType')
						.selectmenu('destroy') //destroy the custom select menu
						.replaceWith('<select name="productType" id="productType"></select>'); //rebuild the select element
						jQuery('select#productType').append('<option value="">Login to...</option>');
						jQuery('select#productType').append('<option value="mlc_business">MLC Group Insurance | MLC Insurance | MLC Business Super</option>');
						jQuery('select#productType').append('<option value="mlc_wealth">Employer Super</option>');	
						jQuery('select#productType').append('<option value="superezy_business">MLC SuperEzy</option>');
						break;
					case 'adviser':
						jQuery('select#productType')
						.selectmenu('destroy') //destroy the custom select menu
						.replaceWith('<select name="productType" id="productType"></select>'); //rebuild the select element
						jQuery('select#productType').append('<option value="">Login to...</option>');
						jQuery('select#productType').append('<option value="mlc_adviser">MLC MasterKey | MLC Insurance | MLC Business Super | MLC Group Insurance</option>');
						jQuery('select#productType').append('<option value="mlc_wealth">MLC Navigator | MLC Wrap | Employer Super | Protectionfirst | Savings &amp; Investments</option>');	
						jQuery('select#productType').append('<option value="superezy_adviser">MLC SuperEzy</option>');						
						break;
					case 'institutional':
						jQuery('select#productType')
						.selectmenu('destroy') //destroy the custom select menu
						.replaceWith('<select name="productType" id="productType"></select>'); //rebuild the select element
						jQuery('select#productType').append('<option value="">Login to...</option>');
						jQuery('select#productType').append('<option value="mlc_institutional">MLC Institutional</option>');
						break;
					}		 
		 		 }

		 		 //a custom format option callback
		 		 var productFormatting = function(text){
					 var newText = text;
					 //array of find replaces
					 var findreps = [{
						find: /^([^\-]+) \- /g,
						rep: '<span class="ui-selectmenu-item-header">$1</span>'
					 },
					 {
						find: /([^\|><]+) \| /g,
						rep: '<span class="ui-selectmenu-item-content">$1</span>'
					 },
					 {
						find: /([^\|><\(\)]+) (\()/g,
						rep: '<span class="ui-selectmenu-item-content">$1</span>$2'
					 },
					 {
						find: /([^\|><\(\)]+)$/g,
						rep: '<span class="ui-selectmenu-item-content">$1</span>'
					 },
					 {
						find: /(\([^\|><]+\))$/g,
						rep: '<span class="ui-selectmenu-item-footer">$1</span>'
					 }];

					 for(var i in findreps){
							 newText = newText.replace(findreps[i].find, findreps[i].rep);
					 }
					 return newText;
		 		 }

		 		 var rebuildProductType = function(){
		 		 		jQuery('select#productType').selectmenu({
						icons: [{
						    find: '.typeicon'
						}],
						width: 187,
						style: 'dropdown',
						menuWidth: 187,
						format: productFormatting
				 		});
		 		 }
