﻿function Search() {
    var domain = $('#txtSearch').val();
    DoSearch(domain);
}

function HPSearch() {
    var domain = $('#txtHPSearch').val();
    DoSearch(domain);
}

function WordSearch() {
    var domain = $('#txtWordSearch_Site').val();
    domain = domain.replace('http://', '').replace('https://', '');
    GetGooglePositionFromPhrase(domain, 'txtWordSearch_Word', 'spnWordSearch', true);
}

function DoSearch(domain) {
    if (domain == '') return;
    //document.location = $('#hdnSiteUrl').val() + 'חיפוש-דומיין/' + domain + '/' + 'מדריך-האינטרנט-הישראלי' + '/';
    document.location = $('#hdnSiteUrl').val() + 'Search.aspx?d=' + domain;
}

//document.onkeydown = function(e) { keydown(e, Search); }

function keydown(e,method) {
    if (!e ) {
        if (window.event) e = window.event;
        else return;
    }

    var keycode;
    if (typeof (e.keyCode) == 'number') 
        keycode = e.keyCode;
      else if (typeof (e.which) == 'number') 
        keycode = e.which;
      else if (typeof (e.charCode) == 'number')
        keycode = e.charCode;
      else return;

      if (keycode == 13) {
          // prevent the default postback using enter key
          if (e.preventDefault)
            e.preventDefault();
          e.returnValue = false; 
          method();
      }
}

function Search_Keypress(e, method) {
  if (typeof (e.keyCode) == 'number')
      keycode = e.keyCode;
  else if (typeof (e.which) == 'number')
      keycode = e.which;
  else if (typeof (e.charCode) == 'number')
      keycode = e.charCode;
  else return;

  if (keycode == 13) {
      method();
      return false;
  }

  return true;
}

var isRunning = false; // GetGooglePositionFromKeyword
function GetGooglePositionFromKeyword(a, domain, phrase) {
    if (phrase == '' || domain == '') return;

    // canceled in order to support multi keywords
    ///if (isRunning) return;
    
    $('div.ajaxKeyword').css('display', 'block');
    isRunning = true;
   
    $.ajax({
        cache: false,
        async: true,
        type: "GET",
        url: $('#hdnSiteUrl').val() + 'GetGooglePosition.ashx',
        data: 'd=' + domain + '&p=' + escape(phrase),
        success: function(res) {
            a.style.color = 'red';
            a.onclick = function() { };

            // res = phrase position in google
            // exp: 23 - page 2 ad 3
            res = parseInt(res);
            if (res == 0) {
                a.innerHTML = 'לא נמצא';
                return;
            }
            var page = Math.floor(res / 10) + 1;
            var pos = res % 10;
            a.innerHTML = 'דף ' + page + ' מיקום ' + pos;
        },
        //error: function() { },
        complete: function() {
            isRunning = false;
            $('div.ajaxKeyword').css('display', 'none');
        }
    });
}

function GetGooglePositionFromPhrase(domain, input_name, span_name, showMoreInfo) {
    var phrase = $('input:text[id="' + input_name + '"]').val();
    if (phrase == '' || domain == '') return;

    if (isRunning) return;

    showMoreInfo = showMoreInfo | false;

    $('div.ajaxPhrase').css('display', 'block');
    isRunning = true;

    $.ajax({
        cache: false,
        async: true,
        type: "GET",
        url: $('#hdnSiteUrl').val() + 'GetGooglePosition.ashx',
        data: 'd=' + domain + '&p=' + escape(phrase),
        success: function(res) {
            var span = $('span[id="' + span_name + '"]');
            // res = phrase position in google
            // exp: 23 - page 2 ad 3
            res = parseInt(res);

            var aMoreInfo = showMoreInfo ? '<br /><a href="Search.aspx?d=' + domain + '" class="aAction">למידע נוסף ומעניין על אתר ' + domain + ' לחץ כאן</a>' : '';
            
            if (res == 0) {
                span.html('הביטוי ' + phrase + ' לא נמצא.' + aMoreInfo);
                return;
            }
            var page = Math.floor(res / 10) + 1;
            var pos = res % 10;

            span.html('הביטוי ' + phrase + ' נמצא בדף ' + page + ' מיקום ' + pos + ' בתוצאות החיפוש בגוגל ישראל ' + aMoreInfo);
        },
        //error: function() { },    
        complete: function() {
            isRunning = false;
            $('div.ajaxPhrase').css('display', 'none');
        }
    });
}

function GetGooglePositionsForAllKeywords() {
    $('a.aKeyword').each(function() { this.click(); })  
}

// easing effect slider
function JumpTo(anchorName) {
    //document.location = '#' + anchorName;
    var top = $('a[name="' + anchorName + '"]').offset().top;
    $("html:not(:animated),body:not(:animated)").animate({ scrollTop: top }, 1100, null);
}
