﻿/// <reference path="jquery-1.2.6-vsdoc.js" />
function commSelectSideBarItem(curItem) {
    $("#side_bar_menu ul li a").each(function(){
        $(this).removeClass("current"); //reset
        if ($(this).text() == curItem) {
            $(this).addClass("current");
        }
    });      
}

function commfindFranchiseList() {
	if ($('#comsrvcsb_zip_code').val() === '') { return false; }

	$('.comsrvcsb_wait').css('display', '');

	$.cookie(SESSION_GUID, 'ffbz_zip_code', $('#comsrvcsb_zip_code').val(), {path:'/'});
	
	$('#comsrvcsb_search_results').empty();
	$('.comsrvcsb_wait').hide();
	
	AjaxManager.Operation.getOperationsForZipCode($('#comsrvcsb_zip_code').val() , false);
}

function commFindFranchiseOperationsRetrieved(operations){
	$('#comsrvcsb_search_results').empty();
	if(operations === null || operations.length === 0){
		$('#comsrvcsb_search_results').append("<div>We're sorry, Stanley Steemer does not provide service in this area.</div>");
	} else  {
		for(var index = 0; index < operations.length; index++){
			commdisplayFranchiseInfo(operations[index]);
		}
	}
	$('#comsrvcsb_zip_code').val($.cookie(SESSION_GUID, 'ffbz_zip_code'));
	$('.comsrvcsb_wait').css('display', 'none');
	$('#comsrvcsb_search_results').show();
}

function commdisplayFranchiseInfo(franchise) {
	$('.comsrvcsb_wait').hide();

	var checkAvail = $('.commcheckAvailability').val();
	if (checkAvail  == 'True')
	{
		var requiresAirDuct = $('.commrequiresAirDuct').val();
		var requiresHardwood = $('.commrequiresHardwood').val();
		var requiresFlooring = $('.commrequiresFlooring').val();	
		$('#comsrvcsb_search_body').hide();
		$('#comsrvcsb_availability').show();
		if (requiresAirDuct == "True" && franchise.doesAirDuctCleaning)
		{
			$('#comsrvcsb_availability').css('display', ' ' );
			$('#comsrvcsb_check_availability_link').text("Service is available");
			return true;
		}
		else
		{
			$('#comsrvcsb_availability').css('display', ' ' );
			$('#comsrvcsb_check_availability_link').text("Sorry, this service is not available in your area at this time");
			return false;
		}
	}
	else
	{
		var detail = "<div id='comsrvcsb_franchise_" + franchise.OperationID + "'>" + "<strong>" + franchise.Name + "</strong><br />" + franchise.AddressLine1 + "<br />";
		
		if (franchise.Zip === '99999') {
			detail = "<div><strong>We're Sorry...</strong><br/>Currently we do not have an operation that can "
			 + "service your zip code.  If you would be interested in learning about franchise opportunities, please "
			 + "<a href='/Home/Franchising.aspx'>click here</a>.";
			 $('#comsrvcsb_search_results').append(detail);
			 return false;
		}
		
		if (franchise.AddressLine2 !== null && franchise.AddressLine2.length > 0) {
			detail += franchise.AddressLine2 + "<br />";
		}
		
		detail += franchise.City + ", " + franchise.State + " " + franchise.Zip + "<br />";
				
		if (franchise.PhoneNumber1 !== null && franchise.PhoneNumber1.length > 0) {
			detail += "<strong>Phone:</strong> " + formatPhone(franchise.PhoneNumber1) + "<br />";
		}
		
		if (franchise.PhoneNumber2 !== null && franchise.PhoneNumber2.length > 0) {
		    detail += formatPhone(franchise.PhoneNumber2) + "<br />";
		}
		
		if (franchise.FaxNumber !== null && franchise.FaxNumber.length > 0) {
		    detail += "<strong>Fax:</strong> " + formatPhone(franchise.FaxNumber);
		}
		detail += "<hr />";
		detail += "</div>";
		
		$('#comsrvcsb_search_results').append(detail);
	}
}

function commshowErrorMessage(err) {
	$('#comsrvcsb_search_results').empty();
	$('.comsrvcsb_wait').css('display', 'none');
	$('#comsrvcsb_search_results').append('<div><span class=\"red\">We\'re sorry, there was an error looking up your zip code.  Please try again.</span></div>');
	$('#comsrvcsb_search_results').fadeIn('slow');
	
}

function commdisplayFranchiseSearch() {
	$('.comsrvcsb_franchise').show();
	$('.comsrvcsb_franchise').css('display', '');
}

function isNumberKey(evt){
	var charCode = evt.which ? evt.which : event.keyCode;
	if ((charCode > 31 && (charCode < 48 || charCode > 57)) || charCode == 13) {
		return false;
	}
	return true;
}

function commFindFranchiseOnAreasAndServiceChanged(){
	var territory = AjaxManager.Territory.getTerritory();
	if(territory !== null){
		$('#comsrvcsb_zip_code').val(territory.Zip);	
		var operation = AjaxManager.Operation.getOperation();
		if(operation !== null){
			commdisplayFranchiseInfo(operation);
		} else {
			commfindFranchiseList();
		}
	}
}

function commSidebarSelectionSubscribeToPublishers(){
	commFindFranchiseOperationsRetrieved.subscribe(AjaxManager.Operation.onOperationsRetrieved);
	commFindFranchiseOnAreasAndServiceChanged.subscribe(AjaxManager.AreasAndServices.onAreasAndServicesChanged);
}

$(document).ready(function() {
	$('.comsrvcsb_wait').hide();
	commSidebarSelectionSubscribeToPublishers();

	$('#comsrvcsb_check_availability_link').click(function(e) {
		e.stopPropagation();
		e.preventDefault();
		$('#comsrvcsb_availability').hide();
		$('.comsrvcsb_wait').hide();
		$('#comsrvcsb_search_body').show();
	});

	$('#comsrvcsb_zip_code').keydown(function(event) {
		if (event.keyCode == 13) {
			$('#comsrvcsb_go').click();
			return false;
		}
	});

	try {
		if ($('#comsrvcsb_zip_code').val() !== '' || ($.cookie(SESSION_GUID, 'ffbz_zip_code') !== '' && $.cookie(SESSION_GUID, 'ffbz_zip_code') !== null)) {
			commfindFranchiseList();
			try {
				$('#ffbz_zip_code').val($('#comsrvcsb_zip_code').val());
				$('#ffbz_go').click();
			} catch (ex) {
				// do nothing
			}
		}
	}
	catch (ex) {

	}
});
