// functions to run onload
$(function(){
	$("body").removeClass("js-disabled");
	$("body").addClass("jsloading");
	
	mainNavLastItem();
	startList();

	//if there any double quotes in the keyword, replace it with a space
	//if there any special characters, replace it with a space
	$("#header-search form").submit(function(e){
		var SpecialCharPattern = /[`~!@#$%^&*()_+-={}[\]|:";'<>?,./]/;
		if(SpecialCharPattern.test($("#queries_site_keyword_query").val()) == true){
			var contxtLocation = '"'+$("#queries_site_keyword_query").val().replace('"', " ")+'"';
			$("#queries_site_keyword_query").val(contxtLocation);
		}
	});

	$("#main-content #refine-search #siteSearchFormResults").submit(function(e){
		var SpecialCharPattern = /[`~!@#$%^&*()_+-={}[\]|:";'<>?,./]/;
		if(SpecialCharPattern.test($("#txtKeyword").val()) == true){
			var contxtLocation = '"'+$("#txtKeyword").val().replace('"', " ")+'"';
			$("#txtKeyword").val(contxtLocation);
		}
	});

	//Run only if directory search form is on page
	if($("#directory #searchForm").size() > 0){
		try
		{
			$.getJSON(autocomplete_suburbs, getAutocompleteSuburbs);
		}
		catch(e){}
		
		splitCategorySelect();
	} 

	//Run only on directory search Results
	if($("#directory #searchFormResults").size() > 0){
		try
		{
				$.getJSON(autocomplete_suburbs, getAutocompleteSuburbs);		   

		}
		catch(e){}
		
		splitCategorySelect();
		buildPagination();

		//createResultsMap();
		try
		{
			createResultsMap();
		}
		catch(e){}

	}

	//Singel Business Details
	if($("#single-business-details ").size() > 0){
		//createSingleBusinessMap();
		try
		{
			createSingleBusinessMap();
		}
		catch(e){}

	}
	
	//Site Search
	if($("#siteSearchFormResults").size() > 0){
		buildPagination();
	}

	//Business Registration Form
	if($("#form_email_440922").size() > 0){
		validateBusinessRegistration();
		var hasData = false;
		$("#q440934_q7, #q440934_q8, #q440934_q9, #q440934_q10").each(function(){
			if($(this).val() != ""){
				hasData = true;
				return false;
			}
		});
		if(hasData){
			showSlidingDivOnPageLoad(document.getElementById('diffDirectoryAddress'));
		}
        
        // Add in link to expand different directory address and hide fields on load
        $('#directoryAddress').hide();
        $('#directoryAddress').before('<p><a href="#" class="slidingDivLink" id="diffDirectoryAddress">Address to be listed in the directory is different to the above.</a></p><!--[if lte IE 6]><div class="clear-left"></div><![endif]-->');
        $('#diffDirectoryAddress').click(function(){
             showSlidingDiv(this);
             return false;
        });
        
        // Make info tooltip keyboard accessible
        $('#businessMessageInfoBubble').focus(function(){
            $(this).addClass('hover');
        });
        $('#businessMessageInfoBubble').blur(function(){
            $(this).removeClass('hover');
        });
        $('#businessMessageInfoBubble').click(function(){
            return false;
        });
	}
    
    // Custom validation rules
	jQuery.validator.addMethod("carerCard", function(value, element) {
		return this.optional(element) || /^[0-9]{14}[A-Za-z]{1}$/i.test(value);
	}, "Your Carer Card number consists of 14 numbers followed by 1 letter (eg. 12345678901234A) and should not contain any spaces or other non-alphanumeric characters.");
	jQuery.validator.addMethod("nameOnly", function(value, element) {
		return this.optional(element) || /^[A-Za-z'\-\s]+$/i.test(value);
	}, "Your name can only contain letters and apostrophe characters.");
	jQuery.validator.addMethod("phone", function(value, element) {
		return this.optional(element) || /^[a-zA-Z0-9-.,()'\"\s]+$/i.test(value);
	}, "Invalid entry");
	jQuery.validator.addMethod("CRN", function(value, element) {
		return this.optional(element) || /^[0-9]{9}[A-Za-z]{1}$/i.test(value);
	}, "Your Centrelink Reference Number (CRN) consists of 9 numbers followed by 1 letter (eg. 123456789A) and should not contain any spaces or other non-alphanumeric characters.");
	jQuery.validator.addMethod("DHS", function(value, element) {
		return this.optional(element) || /^[0-9]{6}$/i.test(value);
	}, "Your DHS number consists of 6 numbers only (eg. 123456) and should not contain any spaces or other non-alphanumeric characters.");

	//Carer Application Form
	if($("#form_email_439753").size() > 0){validateCarerCardApplication();}
	if($("#form_email_528161").size() > 0){validateCarerCardApplication();}
	if($("#form_email_527545").size() > 0){validateCarerCardUpdate();}

	//Contact Us Form
	if($("#form_email_441206").size() > 0){
		validateContactUs();
	}
    
    
    // Keyboard Accessibility for Home Menu
    $('ul#offer-categories li a').each(function(){
        $(this).focus(function(){
            $(this).closest('li').addClass('over');
        });
        $(this).blur(function(){
            $(this).closest('li').removeClass('over');
        });
    });

	$("body").removeClass("jsloading");
});
