function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}


function GetHashTag()
{
	var url = window.location.href; 
	
	var url_parts = url.split('#');
	
	if(url_parts.length > 1)
		return URLDecode(url_parts[1]);
		
	return null;
}

function ScrollToElement(theElement){

  var selectedPosX = 0;
  var selectedPosY = 0;
              
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
                        		      
 window.scrollTo(selectedPosX,selectedPosY);

}



function GotoHashTag()
{
	var tag = GetHashTag();
	
	if(tag)
	{
		
		/*if(typeof(window.currenthashtag) != 'undefined')
			if(window.currenthashtag == tag) 
				return;
		*/
		
		(function($)
		{
			$('#ctl00_CntPlcHld_ucProductDetail10_divFAQ td > a.NormalLink').each(
				function()
				{
					if($(this).text() == tag)
					{
						window.currenthashtag = tag; 
						$(this).parents('table[id^=innertable]').css('display','block');
                        //$(this).css('border','solid 1px red');
						
						$(this).next().css('display','block');
						ScrollToElement(this);
					    //showFaq(id, id.replace('div1', ''));
					}
				
				}
			
			);
		})(jQuery);
	}
}

function HashtagLoad()
{
	GotoHashTag();
	jQuery('a').click(function(){
	    var url = this.href.toLowerCase(); 
		
		var pageUrl = window.location.href.split('#')[0].toLowerCase();
		
		if(url.indexOf(pageUrl) != -1 && url.replace(pageUrl,'').length > 2)
		{
			setTimeout(GotoHashTag, 200);
		}
	});
}

function RemoveFAQSWithBrokenLinks()
{
    (function($)
    {
        $('#ctl00_CntPlcHld_ucProductDetail10_FAQRepeater_ctl01_contentBlock a').each(function(){
            var removeFAQ = false;
            if( (this.href.indexOf('kodak.com') < 0 
                   || (this.href.indexOf('kodak.com') >= 0 && this.href.toLowerCase().indexOf('docimaging') ) < 0) 
                    && this.href.indexOf('javascript:') < 0
                    && this.href.length > 0)
            {
	            if((this.hostname) && this.href.indexOf('ftp:') < 0)
	            {
	                //Remove all FAQs with links to BBHSCanners.com
		             if(this.href.indexOf('bbhscanners.com')>0 || this.href.indexOf('bhscanners.com')>0)
                     {
			            removeFAQ = true;
		             }
		             else
		             {
			            //Check broken link using YQL
			            var jsonurl = 'http://query.yahooapis.com/v1/public/yql?'+
			            'q=select%20*%20from%20html%20where%20url%3D%22'+
			            encodeURIComponent(this.href) +
			            '%22&format=xml&callback=?';
			            $.getJSON(jsonurl,
			              function(data){
			              if(data.results[0] == null)
			              {
				            removeFAQ = true;  
			              }
			              });
		             }
	            }
	            //Remove all FAQs with FTP links and bbh email address
	            else if (this.href.indexOf('ftp:') >= 0 || this.href.indexOf('@bowebellhowell.com') > 0)
	            {
		            removeFAQ = true;
	            }
	            else
	            {
	                removeFAQ = true;
	            }
            }
            if(removeFAQ)
            $(this).parents('.tableBorder999').parent().remove();
        });
        
        })(jQuery);
}

function WaitForJQ()
{
if(typeof jQuery == 'undefined') { setTimeout(WaitForJQ,100); }
    else { jQuery = jQuery; RemoveFAQSWithBrokenLinks(); HashtagLoad();}
}

WaitForJQ();
