//<![CDATA[
var button_CookieName = 'button'
var button_PopupTimeout = 800
var button_ClickCount = 0
var button_NewWindowURL = 'http://www.alpari.ru/?partner_id=79387'
var button_CookieLifeDays = 30
var button_CookieLifeTime = button_CookieLifeDays * 24 * 60 * 60 * 1000

function button_getCookie(CookieName){
  return (document.cookie.indexOf(CookieName) >= 0)
}

function button_setCookie(CookieName){
  var myDate = new Date()
  myDate.setTime(myDate.getTime() + button_CookieLifeTime)
  document.cookie = CookieName + '=15; expires=' + myDate.toGMTString() + '; path=/'
}

function button_clearCookie(CookieName){
  var myDate = new Date()
  myDate.setYear(myDate.getYear() - 1)
  document.cookie = CookieName + '=0; expires=' + myDate.toGMTString()
}

function button_DocumentClick(){
  if (button_ClickCount < 1){
    var NewWindow = window.open(button_NewWindowURL, '123', 'toolbar=0,statusbar=0, top=8000,left=8000,menubar=0,scrollbars=0,resizable=0,width=1,height=1')
    setTimeout(function(){NewWindow.close()}, button_PopupTimeout)
    button_setCookie(button_CookieName)
    window.focus()
  }
  button_ClickCount++
}

function button_gotoURL(url){
  setTimeout(function(){document.location = url}, Number(button_PopupTimeout+500))
}

function button_handleAllLinks() {
  var Links = document.body.getElementsByTagName('a')
  for(var i = 0, len = Links.length; i < len; i++){
    if(Links[i].onclick){
      var OldOnClick = Links[i].onclick
      Links[i].onclick = function(){
        OldOnClick()
        button_setCookie(button_CookieName)
        button_gotoURL(this.href)
        return false
      }
    }
    else {
      Links[i].onclick = function(){
        button_setCookie(button_CookieName)
        button_gotoURL(this.href)
        return false
      }
    }
  }
}
    //]]>
