﻿function getAjax()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Sorry, this function is possible only for Ajax enabled Browsers.");
      return false;
      }
    }
  }
  return xmlHttp;
}

function Vote(link, index,img)
{
    var xmlHttp = getAjax();
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            document.getElementById("IsSimilarCaption" + index).innerHTML='Thanks for voting';
            if(link.substring(0,10) == "/ThumbDown")
            {
                document.getElementById("ResultButtons" + index).innerHTML = '<div title="Not Similar" class="t-d-s">&nbsp;</div>';
            }
            else
            {
                document.getElementById("ResultButtons" + index).innerHTML = '<div title="Similar" class="t-u-s">&nbsp;</div>';
            }
        }
    }
    xmlHttp.open("GET",link,true);
    xmlHttp.send(null);
}

function SuggestNewSite()
{
    var xmlHttp = getAjax();
    var params = "newSite=" + document.getElementById("newSite").value + "&newSiteCaptcha=" + document.getElementById("newSiteCaptcha").value;

    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            if (xmlHttp.status == 200)
            {
                FinishSuggest();
            } else if (xmlHttp.status == 401)
            {
                WrongCaptcha();
            }
        }
    }
    
    xmlHttp.open("POST","/suggest",true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.send(params);
}

function RefreshCaptcha()
{
    var s=document.getElementById("SuggestCaptchaImage").src;
    var temp = new Array();
    temp = s.split('/');
    var index = temp[temp.length-2];
    var num = parseInt(index);
    num=num+1;
    document.getElementById("SuggestCaptchaImage").src = "/images/" + num + "/captcha.gif";
}

function WrongCaptcha()
{
    RefreshCaptcha();
    document.getElementById("newSiteCaptcha").value = "";
    
    document.getElementById("newSiteCaptcha").style.border = "1px solid red";
    document.getElementById("SuggestSiteWrongCaptchaBar").style.display = "block";
}

function FinishSuggest()
{
    document.getElementById("suggestThanks").style.display = "inline";
    setTimeout("NewSuggest()",3000);
}

function NewSuggest()
{
    document.getElementById("suggestThanks").style.display = "none";
    RefreshCaptcha();
    document.getElementById("newSiteCaptcha").style.border = "";
    document.getElementById("SuggestSiteWrongCaptchaBar").style.display = "";
    document.getElementById("newSiteCaptcha").value = "";
    document.getElementById("newSite").value = "";
}

function ShowSearchProviderNote()
{
 if (window.external && ("AddSearchProvider" in window.external)) {
    if (!Get_Cookie('SearchProviderNote'))
    {
        document.getElementById("SearchProviderNote").style.display = "block";
    }
 }
}

function AddSearchProvider()
{
 if (window.external && ("AddSearchProvider" in window.external)) {
   // Firefox 2 and IE 7, OpenSearch
   window.external.AddSearchProvider("http://www.similarsites.com/SimilarSitesSearchProvider.xml");
 } else {
   // No search engine support (IE 6, Opera, etc).
   alert("No search engine support");
 }
    DontShowNote();
}

function DontShowNote()
{
    document.getElementById("SearchProviderNote").style.display = "none";
    
    Set_Cookie( 'SearchProviderNote', 'removed', 365, '/', '', '' );
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
	
// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function HPQuickSearchInit()
{
    var changeBox = document.getElementById('PS');
    if (changeBox != null)
    {
        var myItems = changeBox.getElementsByTagName('li');

        for (var i=0;i<myItems.length;i++) 
        {
	        myItems[i].onmouseover = function(){this.className = 'siteWrapper siteWrapperHover';};
	        myItems[i].onmouseout = function(){this.className = 'siteWrapper';};
        }
    }            
}

function VoteChangeInit()
{
    var changeBox = document.getElementById('VoteChangeBox');

    if (changeBox != null)
    {
        var myItems = changeBox.getElementsByTagName('li');

        for (var i=0;i<myItems.length;i++) 
        {
            if (myItems[i].id != 'VoteChangeTitle')
            {
	            myItems[i].onmouseover = VoteItem_MouseOver;
	            myItems[i].onmouseout = VoteItem_MouseOut;
            }
        }
    }
}
function VoteItem_MouseOver()
{
    this.className = 'VoteItem VoteItemHover';
}

function VoteItem_MouseOut()
{
    this.className = 'VoteItem';
}

function AddFloatingDownload()
{
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
    document.write("<a title=\"SimilarWeb for Firefox\" rel=\"nofollow\" onclick=\"javascript: pageTracker._trackPageview('/Download/SimilarWebFF2.xpi?src=external-similarsites-corner');\"  href=\"/Download/SimilarWebFF2.xpi\"><img style=\"position: fixed; bottom: 0; right: 0;\" alt=\"SimilarWeb for Firefox\" title=\"SimilarWeb for Firefox\" src=\"/images/dl-sw.png\" /></a>");
}
else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 if (ieversion>=7) {
        document.write("<a title=\"SimilarWeb for Internet Explorer\" rel=\"nofollow\" onclick=\"javascript: pageTracker._trackPageview('/Download/SimilarWebIE.exe?src=external-similarsites-corner');\"  href=\"/Download/SimilarWebIE.exe\"><img style=\"position: fixed; bottom: 0; right: 2px;\" alt=\"SimilarWeb for Internet Explorer\" title=\"SimilarWeb for Internet Explorer\" src=\"/images/dl-sw-ie.png\" /></a>");
    }
}
}



