$(document).ready(function() {

   // adjust the positioning of the badges on the right side
   var leftcolheight = parseInt($('#leftcol').height()) - 350;
   var css = {position: 'relative', height: leftcolheight}
   $('#submit').css(css);
   $('.securitybadges').css('display', 'block');

	$('#error').css({display:"none"});
	$('#fee').css({display:"none"});
    $('#industry').change(function() {
       
       $('#results').html('<div style="text-align:center; padding-top: 30px;"><img src="pub/img/greybgloader.gif" /> <p>loading...</p></div>');
       $('#fee').css({display:"block"});
       
       var intid = $(this).children('option:selected').val();
       var fee = $('#fee').children('option:selected').val();
       // get rid of the first one
       $('#Fake').remove(); 
        /**
         * add a click event listenter to the submit
         * link so it will submit the form
         */
        $('#submit a').click(function () {
	
			var errors = false;
	        var fields = new Array();
	
			$('#error').css({display:"none"});	
			
		    if ($('#firstname').val().length < 3 || $('#firstname').val().match(/[^a-zA-Z0-9-\.]+/))
			{
				$('#firstname').addClass("error");
				$('#error').css({display:"inline"});	
				errors = true;
			}
			else
			{ 
				$('#firstname').removeClass("error"); 
			}
			if ($('#lastname').val().length < 3 || $('#lastname').val().match(/[^a-zA-Z0-9-\.]+/))
			{
				$('#lastname').addClass("error");
				$('#error').css({display:"inline"});	
				errors = true;
			}
			else
			{ 
				$('#lastname').removeClass("error");	
			}

			if(!ValidPhone($('#phone').val()))
			{
				$('#phone').addClass("error");
				$('#error').css({display:"inline"});	
				errors = true;
			}
			else
			{ 
				$('#phone').removeClass("error");
			}

			if (!emailCheck($('#email').val()))
			{
				$('#email').addClass("error");
				$('#error').css({display:"inline"});	
				errors = true;
			}
			else
			{ 
				$('#email').removeClass("error");
			}

			if(!ValidZip($('#zipcode').val()))
			{
				$('#zipcode').addClass("error");
				$('#error').css({display:"inline"});	
				errors = true;
			}
			else
			{ 
				$('#zipcode').removeClass("error");
			}

	        $('#contactform input').each(function(i, elem) {

	           var name = $(elem).attr("name");

	           if (name != 'userpassword' && name != 'useremail' && $(elem).attr("type") != 'checkbox') {
	                if ($(elem).val() == "") {
	                    errors = true;

	                    fields.push($(elem).prev().html());
	                }
	           }

	        });

            if ($('#reslist').children('li[id^=franchiseinfo_]').length == 0) {
                alert('No Franchise Selections Have Been Made.');
                return false;
            }
	        if (fields.length > 0) {
	            alert("The following fields are required: \r\n" + fields.join('\r\n'));
	        }
			if (errors == false)  {
			    $('#contactform').submit();
			}

        });

       /**
        * this is where the ajax request is made for results on the
        * browse profiles page. the url should be swapped out with
        * the dynamic url for fetching results
        */
		/**** for testing... ***/
		//results.html for search with results
		//results2.html for search wtih with no results
	   	
        $.post('BrowseHandler.ashx', {selindustry:intid, franfee:fee}, showFranchiseResults, "html");
        
        function showFranchiseResults(data)
        {
			
           	if(data != '')
			{
                /**
                 * set the result franchises in the view
                 */
                $('#results').html(data);
                franchiseDetPopups();

                // swap out the content in the right column
		
				 $('#defaultcontent').css('display', 'none');
		         $('#resultcontent').css('display', 'block');
		
	
	            // adjust the positioning of the badges on the right side
	            var leftcolheight = parseInt($('#leftcol').height()) - 390;
	            var css = {position: 'relative', height: leftcolheight}
	            $('#submit').css(css);
	            $('.securitybadges').css('display', 'block');

	            /**
	             * listen for checkbox selections to show which
	             * ones are selected by adjusting css
	             */
	            $('input[id*=franchisesel_]').click(function() {
       
	                var intid = $(this).attr("id").match(/\d+$/)[0];
       
	                var franchise = $('#franchise_' + intid);
       
	                var checked = false;
       
	                if ($(franchise).hasClass('resultsel')) {
	                    $(franchise).removeClass('resultsel');
                   
	                 /*   if (jQuery.browser.msie && !isIEGreaterThan6()) {
	                        $('#franchiselogo_' + intid).css('backgroundPosition', 'left top');
	                    }*/
	                } else {
                 
	                    $(franchise).addClass('resultsel');
	                    checked = true;
       
	                 /*   if (jQuery.browser.msie && !isIEGreaterThan6()) {
	                        $(franchise).children('.logo').css('backgroundPosition', 'left bottom');
	                    }*/

	                }
               
	                if (checked == true) {
                   
		                var franchisename = $('#franchiselogo_' + intid).attr("FranName");
	                    var data = {id: intid, name:franchisename};
	                    getFranchiseInfo(data);
                   
	                } else {
                   
	                    $('#franchiseinfo_' + intid).remove();
                   
	                    $('#franchiseid_' + intid).remove();
                   
	                    if ($('#reslist').children('li[id^=franchiseinfo_]').length == 0) {
	                        $('#reslist').html('<li>No Franchise Selections Have Been Made.</li>');
	                    }
			
                   
	                }
	
               
	                function getFranchiseInfo(data)
	                {
	                    if ($('#reslist').children('li[id^=franchiseinfo_]').length == 0) {
	                        $('#reslist').html('<li id="franchiseinfo_' + data.id + '">' + data.name  + '<a id="franchiseremove_' + data.id + '">remove</a></li>');
	                    } else if ($('#reslist').children('#franchiseinfo_' + data.id).length == 0) {
	                        $('#reslist').append('<li id="franchiseinfo_' + data.id + '">' + data.name + '<a id="franchiseremove_' + data.id + '">remove</a></li>');
	                    }
                   
	                    $('#contactform').append('<input type="hidden" id="franchiseid_' + data.id + '" name="franchise[]" value="' + data.id + '"/>');
                   
	                    /**
	                     * if a remove link is clicked, issue a click event on
	                     * the corresponding checkbox so the handeling is consistent
	                     */
	                    $('a[id^=franchiseremove_]').click(function() {
	                         var removeid = $(this).attr("id").match(/\d+$/)[0];
	                         $('#franchisesel_' + removeid).click();
	                    });
                   
	                } 
  
	            });
		 	}
		
			else
			{

				$('#results').html('<div style="margin-left:-25px; display:inline;"><img src="pub/img/noresult.gif" /></div>');
				$('#defaultcontent').css('display', 'block');		
		        $('#resultcontent').css('display', 'none');
			}
            
        }
        
    });

	$('#fee').change(function() {
       
            var fee = $(this).children('option:selected').val();
            var intid = $('#industry').children('option:selected').val();
    		
            $.post('BrowseHandler.ashx', {selindustry:intid, franfee:fee}, showFranchiseResults, "html");			
			function showFranchiseResults(data)
	        {

	           	if(data != '')
				{
	            /**
	             * set the result franchises in the view
	             */
	            $('#results').html(data);

	          /*  if (jQuery.browser.msie && !isIEGreaterThan6()) {
	                $('a[id*=franchiselogo_]').hover(
	                    function() {
	                        if (!$(this).parent().hasClass('resultsel')) {
	                            $(this).css('backgroundPosition', 'left bottom');
	                        }
	                    },
	                    function() {
	                        if (!$(this).parent().hasClass('resultsel')) {
	                            $(this).css('backgroundPosition', 'left top');
	                        }
	                    }
	                );
	            }*/

	            // swap out the content in the right column

					 $('#defaultcontent').css('display', 'none');
			         $('#resultcontent').css('display', 'block');


		            // adjust the positioning of the badges on the right side
		            var leftcolheight = parseInt($('#leftcol').height()) - 390;
		            var css = {position: 'relative', height: leftcolheight}
		            $('#submit').css(css);
		            $('.securitybadges').css('display', 'block');

		            /**
		             * listen for checkbox selections to show which
		             * ones are selected by adjusting css
		             */
		            $('input[id*=franchisesel_]').click(function() {

		                var intid = $(this).attr("id").match(/\d+$/)[0];

		                var franchise = $('#franchise_' + intid);

		                var checked = false;

		                if ($(franchise).hasClass('resultsel')) {
		                    $(franchise).removeClass('resultsel');

		                  /*  if (jQuery.browser.msie && !isIEGreaterThan6()) {
		                        $('#franchiselogo_' + intid).css('backgroundPosition', 'left top');
		                    }*/
		                } else {

		                    $(franchise).addClass('resultsel');
		                    checked = true;

		                   /* if (jQuery.browser.msie && !isIEGreaterThan6()) {
		                        $(franchise).children('.logo').css('backgroundPosition', 'left bottom');
		                    }*/

		                }

		                if (checked == true) {

		                    var franchisename = $('#franchiselogo_' + intid).attr("FranName");
		                    var data = {id: intid, name:franchisename};
		                    getFranchiseInfo(data);

		                } else {

		                    $('#franchiseinfo_' + intid).remove();

		                    $('#franchiseid_' + intid).remove();

		                    if ($('#reslist').children('li[id^=franchiseinfo_]').length == 0) {
		                        $('#reslist').html('<li>No Franchise Selections Have Been Made.</li>');
		                    }


		                }


		                function getFranchiseInfo(data)
		                {
		                    if ($('#reslist').children('li[id^=franchiseinfo_]').length == 0) {
		                        $('#reslist').html('<li id="franchiseinfo_' + data.id + '">' + data.name  + '<a id="franchiseremove_' + data.id + '">remove</a></li>');
		                    } else if ($('#reslist').children('#franchiseinfo_' + data.id).length == 0) {
		                        $('#reslist').append('<li id="franchiseinfo_' + data.id + '">' + data.name + '<a id="franchiseremove_' + data.id + '">remove</a></li>');
		                    }

		                    $('#contactform').append('<input type="hidden" id="franchiseid_' + data.id + '" name="franchise[]" value="' + data.id + '"/>');

		                    /**
		                     * if a remove link is clicked, issue a click event on
		                     * the corresponding checkbox so the handeling is consistent
		                     */
		                    $('a[id^=franchiseremove_]').click(function() {
		                         var removeid = $(this).attr("id").match(/\d+$/)[0];
		                         $('#franchisesel_' + removeid).click();
		                    });

		                } 

		            });
			 	}

				else
				{

					$('#results').html('<div style="margin-left:-25px; display:inline;"><img src="pub/img/noresult.gif" /></div>');
					$('#defaultcontent').css('display', 'block');		
			        $('#resultcontent').css('display', 'none');
				}

	        }
	});

});

function franchiseDetPopups()
{
    $('a[id*=franchisedet_]').click(franchisePopups);

    $('a[id*=franchiselogo_]').click(franchisePopups);

            //todo jja remove ??

    function franchisePopups()
    {
        var intid = $(this).attr("id").match(/\d+$/)[0];

        $.post("BrowseHandler.ashx", {franchiseid:intid,franchisedet:true}, franchiseLightBox, "html");

        function franchiseLightBox(data)
        {
            if(data != '')
		        $('#franchiselb').html(data);
            $('#franchiselb').modal({overlayId:'franOverlay', containerId:'franContainer', closeClass:'close'});
        }

        return false;
    }

}
/**
 * utility function to see if ie version greater
 * than six is being used
 */
function isIEGreaterThan6()
{
    return (jQuery.browser.msie && jQuery.browser.version.substr(0,1) > 6) ? true : false;
}

