
var businessName = null;
var abnLookupUrl = 'http://www.abr.business.gov.au/(rvoups55jungzt45w3lv2w45)/search.aspx?StartSearch=True&SearchText='
var current = null;


$(function() {
	//Lightbox popups
	if ($.fancybox && $('.cf_fancybox').length >= 1) {
		$('.cf_fancybox').fancybox({
			'autoScale'			: false,
			'autoDimensions'	: false,
			'centerOnScroll'		: true,
			'overlayOpacity'		: 0.5,
			'overlayColor'		: '#000', 
			'showNavArrows'	: false,
			'width'					: 600,
			'height'			  		: 330,
			'transitionIn'			: 'elastic',
			'transitionOut'		: 'elastic'
		});
	}
	
	// fire change on events that have other binded events that require click [go]

	var is_reseller=( $('input#is_reseller_subclient').length>0 || $('input#Is_GO_Designing_Site_Yes').length>0 );
	var is_reseller_subclient=( $('input#is_reseller_subclient').length>0 );

	if ($('FORM#new_client_form').length === 1) {

		$('FORM#new_client_form').parent().mouseover(function() {
			generateTipsyTips();
		})

		$.validator.addMethod("passwordIsSecureEnough", function(value, element)
		{
			return (element===undefined || GetPasswordMeterOk(element.id));
	 	}, "Password is not secure enough");


		$('FORM#new_client_form').validate({
			rules: {
				Client_Name: { required: true},
				Company_Name: { required: true},
				ABN_or_ACN: { required: true },
				Registration_Number: { required: true},
				Company_Directors:  { required: true},
				Office_Address: { required: true},
				Postal_Address: { required: true},
				Phone: { required: true},
				Email: { required: true, email:true },

				Point_Of_Contact_Name: { required: true},
				Point_Of_Contact_Email: { email:true },
				Point_Of_Contact_Agree: { required: true},
				Point_Of_Contact_Additional_Charges_Agree: { required: true},
				Authorization_Primary_Business_Owner: { required: true},
				CMS_Username: { required: true},
				CMS_Password: { required: true, passwordIsSecureEnough: true },
				Secret_Question: { required: true},
				Secret_Answer: { required: true},

				GO_Hosting: { required: true},
				GO_Hosting_Billable_Time: { required: true },

				Domain_Name: { required: true, domain: true },
				Domain_Registration: { required: true},
				Email_Hosting: { required: true},

				Info_Site_Name: { required: true},
				Info_Business_Name: { required: true},
				Info_Email: { required: true, email:true },

				Website_Type: { required: true },

				Design_Website_Style: { required: true },
				Design_Resolution_Width: { required: true },
				Design_Fixed_Height: { required: true },

				Content_Website_Pages: { required: true },
				Content_Website_Images: { required: true },
				Content_Database_Transfer: { required: true },

				Final_Hear_About: { required: true },
				Final_Agree_Terms: { required: true },
				Final_Agree_Info_Correct: { required: true },
				
				Is_GO_Designing_Site: { required: is_reseller },
				
				Final_Agree_Quote_Correct: { required: true },
				Final_Agree_Quote_Info_Missing: { required: true }

			}
		});


		// STEP 1
		// Quick Link to Goverment Database to lookup ABN or ACN
		$('#abn_lookup').bind('click', function(e) {
			
			businessName = $('#Company_Name').val().trim();
			
			if (businessName.length > 0) {
				var lookupUrl = abnLookupUrl + escape(businessName);
			} else {
				var lookupUrl = 'http://www.abr.business.gov.au';
			}
			
			$(this).attr('href', lookupUrl);

			e.preventDefault();
	
		});
		
		
		// STEP 2
		
		AddPasswordMeter('CMS_Password', '', 'b');
		$('div#CMS_Password_simplePassMeter').css('width', '260px');


		// STEP 3
		// Toggle display of fields
		$('#GO_Hosting_Yes').click(function(){
			$('#cf_step3_hosting_yes').slideDown();
		});

		$('#GO_Hosting_No').click(function(){
			$('#cf_step3_hosting_yes').slideUp();
		});
		
		$('#GO_Hosting_Shared_Yes').click(function(){
			$('#cf_step3_shared_yes').slideDown();
		});

		$('#GO_Hosting_Shared_No').click(function(){
			$('#cf_step3_shared_yes').slideUp();
		});
		
		
		
		// If the client chooses "Hosting with us", the "billing period" and "shared software y/n" questions become required
		$('input[name=GO_Hosting]').change(function() {
			$('.step_3_required2').removeClass('required');
			if ($(this).val() == 'Yes') {
				$('.step_3_required2').addClass('required');
			} 
		});
		
		// Then if the client chooses "shared software y", the "billing period" question becomes required
		$('input[name=GO_Hosting_Shared]').change(function() {
			$('.step_3_required3').removeClass('required');
			if ($(this).val() == 'Yes') {
				$('.step_3_required3').addClass('required');
			} 
		});

		
		//Lightbox popups
		if ($.fancybox && $('.cf_fancybox').length >= 1) {
			$('.cf_fancybox').fancybox({
				'autoScale'			: false,
				'autoDimensions'	: false,
				'centerOnScroll'		: true,
				'overlayOpacity'		: 0.5,
				'overlayColor'		: '#000', 
				'showNavArrows'	: false,
				'width'					: 600,
				'height'			  		: 330,
				'transitionIn'			: 'elastic',
				'transitionOut'		: 'elastic'
			});
		}
		
		
		// STEP 4
		// Make fields required depending on which option the user chooses
		$('input[name=Domain_Registration]').change(function() {
			$('.step_4_required2').removeClass('required');
			$('.step_4_required3').removeClass('required');
			
			
			if ($(this).val() == 'Already_Registered') {
				$('.step_4_required2').addClass('required');
			} else if ($(this).val() == 'Please_Register') {
				$('.step_4_required3').addClass('domain');
			}
			
		});
		
		// Toggle displays
		$('input[name=Domain_Registration]').change(function() {

			var section = $(this).val();

			if (section == 'Client_Will_Register') {
				if (current !== null) {
				current.slideUp();
				}
			} else {
				var show  = function() {
				current = $('#cf_step4_' + section);
				current.slideDown();
				}

				if (current) {
					current.slideUp(show);
				} else {
					show();
				}
			}
		});
		
		//STEP 5
		// Tip fancybox
		$('#cf_step5_tip_link').fancybox({
			'transitionIn'			: 'elastic',
			'transitionOut'		: 'elastic'
		});
		
		
		
		// Toggle displays
		$('input[name=Email_Hosting]').change(function() {

			var value = $(this).val();
			
			var show  = function() {
				current = $('#cf_step5_email_hosting_' + value);
				current.slideDown();
			}

			if (current) {
				current.slideUp(show);
			} else {
				show();
			}
			
			if(value == 'Yes'){
				$('.cf_step5_emails_cell1 input').each(function(){
					$(this).addClass('noemail');
				});
				
				$('.cf_step5_forwarders_cell1 input').each(function(){
					$(this).addClass('noemail');
				});
			} else {
				$('.cf_step5_emails_cell1 input').each(function(){
					$(this).removeClass('noemail');
				});
				
				$('.cf_step5_forwarders_cell1 input').each(function(){
					$(this).removeClass('noemail');
				});
			}
			
		});
		

		
		// Add new rows for email accounts
		$('#cf_step5_email_add').click(function() {
		
			var newRow = $('#cf_step5_email_accounts tbody>tr:first').clone(true);
			
			// clear any fields
			newRow.find('input').each(function() {
				$(this).val('');
			});
			
			// wrap in divs
			newRow.find('td').each(function() {
				$(this).wrapInner('<div>');
			});
			
			// hide divs
			newRow.find('div').hide();
			
			// insert row
			newRow.insertAfter('#cf_step5_email_accounts tbody>tr:last')
			
			// slide divs
			newRow.find('div').slideDown();
			
			$('#cf_step5_email_accounts tbody>tr:last input:first').focus();
		
		
		});
		
		// Add new rows for email accounts
		$('#cf_step5_forwarder_add').click(function() {
		
			var newRow = $('#cf_step5_forwarders tbody>tr:first').clone(true);
			
			// clear any fields
			newRow.find('input').each(function() {
				$(this).val('');
			});
			
			// wrap in divs
			newRow.find('td').each(function() {
				$(this).wrapInner('<div>');
			});
			
			// hide divs
			newRow.find('div').hide();
			
			// insert row
			newRow.insertAfter('#cf_step5_forwarders tbody>tr:last')
			
			// slide divs
			newRow.find('div').slideDown();
			
			$('#cf_step5_forwarders tbody>tr:last input:first').focus();
		
		
		});
		
		
		//STEP 6
		// Automatically fill in website information from first step contact details
		var mapping = {
			Info_Site_Name : 'Company_Name',
			Info_Business_Name: 'Company_Name',
			Info_Office_Address: 'Office_Address',
			Info_Postal_Address: 'Postal_Address',
			Info_Phone: 'Phone',
			Info_Fax: 'Fax',
			Info_Email: 'Email'
		}

		$('#info_quick_fill').bind('click', function(e) {
			
			for (fld in mapping) {
				$('#' + fld).val(function() {
					return $('#' + mapping[fld]).val();
				});
			}
		});
		
		
		//STEP 7
		$('#Website_Type').change(function() {
			$('.step_7_required2').removeClass('required');
			var current = $(this).val();
			
			if(current == 'Store' || current == 'Social'){
				$('#cf_step7_payment_options').slideDown();
				$('.step_7_required2').addClass('required');
			} else {
				$('#cf_step7_payment_options').slideUp();
			}

		});
		
		// Toggle displays
		$('input[name=Website_Accept_Credit_Cards]').change(function() {

			var value = $(this).val();
			var section = '';
			
			if(value == 'Yes'){
				div = 'ssl';
			} else {
				div = 'redirect';
			}

			
			var show  = function() {
			current = $('#cf_step7_payment_options_' + div);
			current.slideDown();
			}

			if (current) {
				current.slideUp(show);
			} else {
				show();
			}
			
		});
		
		
		//STEP 8

		if (is_reseller)
		{
			$('#cf_step8_go_designing_site_no').hide();
			$('#cf_step8_designquestions').hide();

			$('input[name=Is_GO_Designing_Site]').change(function()
			{
				var current = $(this).val();
				if (current=='Yes')
				{
					$('#cf_step8_go_designing_site_no').slideUp();
					setTimeout(function()
					{
						$('#cf_step8_designquestions').slideDown();
					}, 500);

					$('#Design_Website_Style').rules('add', 'required');
					$('#Design_Resolution_Width').rules('add', 'required');
					$('input[name=Design_Fixed_Height]').rules('add', 'required');
				}
				else
				{
					$('#Design_Website_Style').rules('remove', 'required');
					$('#Design_Resolution_Width').rules('remove', 'required');
					$('input[name=Design_Fixed_Height]').rules('remove', 'required');

					$('#cf_step8_designquestions').slideUp();
					setTimeout(function()
					{
						$('#cf_step8_go_designing_site_no').slideDown();
					}, 500);
				}
			});
		}

		// Display other field if other is chosen

		$('#Design_Website_Style').change(function() {
			$('#Design_Website_Style_Other').removeClass('required');
			var current = $(this).val();
			
			if(current == 'Other'){
				$('#Design_Website_Style_Other').addClass('required');
				$('#Design_Website_Style_Other_Field').slideDown();
			} else {
				$('#Design_Website_Style_Other_Field').slideUp();
			}

		});

		// Add new row for comments
		$('#cf_step8_website_comments_add').click(function() {
		
			var newRow = $('#cf_step8_website_comments tbody>tr:first').clone(true);
			
			// clear any fields
			newRow.find('input').each(function() {
				$(this).val('');
			});
			
			// wrap in divs
			newRow.find('td').each(function() {
				$(this).wrapInner('<div>');
			});
			
			// hide divs
			newRow.find('div').hide();
			
			// insert row
			newRow.insertAfter('#cf_step8_website_comments tbody>tr:last')
			
			// slide divs
			newRow.find('div').slideDown();
			
			$('#cf_step8_website_comments tbody>tr:last input:first').focus();
		
		
		});
		
		// Tip fancybox
		$('#cf_step8_tip_link').fancybox({
			'transitionIn'			: 'elastic',
			'transitionOut'		: 'elastic'
		});
		
		// Database Upload lightbox
		$('#Content_Database_Transfer_Yes').click(function(){
			$("#database_upload_info_link").trigger("click");
		});
		
		$('#database_upload_info_link').fancybox({
				'autoScale'			: false,
				'autoDimensions'	: false,
				'centerOnScroll'		: true,
				'overlayOpacity'		: 0.5,
				'overlayColor'		: '#000', 
				'showNavArrows'	: false,
				'width'					: 500,
				'height'			  		: 250
		});
		
		//STEP 9
		// User must scroll to the bottom of the terms and conditions before they can enable the agree checkbox
		$('#cf_step9_terms_scroll').scroll(function() {
			var divHeight = $(this).height();
			var scrollFromTop = divHeight + $(this).scrollTop();
			var innerHeight = $(this).find('#cf_step9_terms').height();

			if (scrollFromTop >= innerHeight) {
				$('#Final_Agree_Terms').removeAttr('disabled');
			}

		});
		
		$('#Final_Hear_About').change(function() {
			$('#Final_Hear_About_Other').removeClass('required');
			var current = $(this).val();
			
			if(current == 'Other'){
				$('#Final_Hear_About_Other').addClass('required');
				$('#Final_Hear_About_Other_Field').slideDown();
			} else {
				$('#Final_Hear_About_Other_Field').slideUp();
			}

		});

		
	}

	
	
	
	$('a[rel=lightbox]').fancybox({
		'autoScale'			: false,
		'scrolling'				: 'x',
		'centerOnScroll'		: true,
		'overlayOpacity'		: 0.5,
		'overlayColor'		: '#000', 
		'showNavArrows'	: false,
		'width'					:895,
		'height'					: 500,
		'transitionIn'			: 'elastic',
		'transitionOut'		: 'elastic',
		'type'					: 'iframe'
	});
	
	
	
	// fire change on events that have other binded events that require click [go]
	setTimeout("$('input[type=radio]:checked').change().click()", 10);
	$('select').change();


});



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function GetPasswordMeterText(in_name)
{
	var pwdrating=$('#'+in_name+'_simplePassMeter .simplePassMeterText').html();
	return pwdrating;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function GetPasswordMeterOk(in_name)
{
	var pwdrating;

	pwdrating=GetPasswordMeterText(in_name);

	if (pwdrating=='You need a stronger password' || pwdrating.indexOf('Passwords need to be')>=0) // || pwdrating=='Your password is a bit weak'
		return false;
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function AddPasswordMeter(fld1, fld2, pos, nfo)
{
	var req, fset;

	if (fld2===undefined)
		fld2='';
	if (nfo===undefined || nfo=='' || !nfo)
		nfo=0;
	if (pos===undefined)
		pos='';

	if (pos=='')
		pos='r';

	if (fld2=='')
		req={ 'minLength':  { 'value': 6 } };
	else
		req={ 'minLength':  { 'value': 6 }, 'matchField': { 'value': '#'+fld2 } };

	if (pos=='b')
	{
		if (document.all)
			fset=0;
		else
			fset=-2;
	}
	else
		fset=10;

	if (nfo)
	{
		$('input#'+fld1).simplePassMeter( nfo );
	}
	else
	{
		$('#'+fld1).simplePassMeter(
		{
			location: pos,
			offset: fset,
//			showOnValue: true,
			//requirements: 
			//{
			//	'minLength':  { 'value': 6 },
			//	'matchField': { 'value': '#in_new_passw_rd2' }
			//}
			requirements: req
		});
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



