d = document;
d.$ = function ( obj_id ) {
    var obj;
    if( document.all )
    {
        obj = document.all[obj_id];
    }
    else if( document.getElementById )
    {
        obj = document.getElementById(obj_id);
    }
    else if( document.getElementsByName )
    {
        obj = document.getElementsByName(obj_id);
    }
    return obj;
}

function SetRefer (link)
{
  if (link == null || GetCookie('refer') != null) { return; }
  if (link == '') { link='NV'; }
  var todayDate = new Date();
  todayDate.setDate(todayDate.getDate() + 366);
  document.cookie = "refer" + "=" + link + "; expires=" + todayDate.toGMTString() + ";"
}

function SetPartner(name)
{
  if (name == 'NV') { return; }
  var todayDate = new Date();
  todayDate.setDate(todayDate.getDate() + 366);
  document.cookie = "partner" + "=" + name + "; expires=" + todayDate.toGMTString() + ";"
}

function SetUID(name)
{
  if (name == 'NV' || GetCookie('UID') != null) { return; }
  var todayDate = new Date();
  todayDate.setDate(todayDate.getDate() + 366);
  document.cookie = "UID" + "=" + name + "; expires=" + todayDate.toGMTString() + ";"
}

function GetCookie (name)
{
    var arg  = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i    = 0;

    while (i < clen)
    {
	var j = i + alen;
	if (document.cookie.substring(i, j) == arg)
	  return getCookieVal (j);
	i = document.cookie.indexOf(" ", i) + 1;
	if (i == 0) break;
    }
    return null;
}

function getCookieVal( offset )
{
    var endstr = document.cookie.indexOf (";", offset);

    if (endstr == -1)
	endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function DeleteCookie (name)
{
    var exp  = new Date();
    var cval = GetCookie (name);

    exp.setTime (exp.getTime() - 1);  // This cookie is history
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function MobPhoneValid() {
  if (!/^[0-9]{7}$/.test(d.$("phonenum").value)) {alert("Неправильно указан номер мобильного телефона"); return(false);}
  return (true);
}

function PoolValid() {
  for (i = 0; i < d.$("rb").length; i++) {
     if (d.$("rb")[i].checked == true) { return(true); }
  }
  alert("Укажите ответ на вопрос");
  return(false);
}

function poll_results_4(action,theURL,winName,winWidth,winHeight,features) {
    var w = (screen.width - winWidth)/2;
    var h = (screen.height - winHeight)/2 - 20;
    features = features+',width='+winWidth+',height='+winHeight+',top='+h+',left='+w;
    var poll_ident = self.document.poll_4.poll_ident.value;
    option_id = '';
    for (i=0; i<self.document.poll_4.option_id.length; i++) {
	if(self.document.poll_4.option_id[i].checked == true) {
	    option_id = self.document.poll_4.option_id[i].value;
	    break;
	}
    }
    option_id = (option_id != '') ? '&option_id='+option_id : '';
    if (action=='results' || (option_id != '' && action=='vote')) {
	theURL = theURL+'?action='+action+'&poll_ident='+poll_ident+option_id;
	poll_popup = window.open(theURL,winName,features);
	poll_popup.focus();
    }
    return false;
}
