﻿function sn_action(tbName,tbEmail,action) 
{
  var tbName = document.getElementById(tbName);
  var tbEmail = document.getElementById(tbEmail);
    
  if (CheckRuUkSymbols(tbName.value))
  {
    alert("Имя должно состоять только из русских букв!");
    return;
  }  
    
  if ((tbName != null)&&(tbEmail != null))
  {
    if (tbEmail.value != "")
    {
      //проверяем на корректность
      var p_email = tbEmail.value;
      var t = p_email.indexOf("@");
      if( (CheckEnSymbols(p_email)) || (p_email.indexOf("@",t+1) != -1) || (p_email.indexOf(".") == -1) || (t == -1) || (t < 1) || (t > p_email.length - 5) || (p_email.charAt(t - 1) == '.') || (p_email.charAt(t + 1) == '.')) 
      {
        alert("Некорректно указан e-mail !");
        tbEmail.focus();
        return;
      }

      if (action == "subscribe")
      {
        if (tbName.value != "")
          window.location = "News.aspx?action=subscribe&name="+tbName.value+"&mail="+tbEmail.value,null,"height=600,width=1000,resize=yes";            
        else
        {
          alert("Пожалуйста, введите Ваше имя.");
          tbName.focus();
        }
      }
      else
        window.location = "News.aspx?action=unsubscribe&mail="+tbEmail.value,null,"height=600,width=1000,resize=yes";
    }
    else
    {
      alert("Пожалуйста, введите Ваш e-mail.");
      tbEmail.focus();
    }
  }
}

function CheckRuUkSymbols(st)
{
  var k=0;
  for(i=0; i<st.length; i++)
  {
    if ((st.charCodeAt(i)<1040) || (st.charCodeAt(i)>1104))
    {
      if ( (st.charCodeAt(i)!=1110) && (st.charCodeAt(i)!=1111) && (st.charCodeAt(i)!=1030) && (st.charCodeAt(i)!=1031) && (st.charCodeAt(i)!=1108) && (st.charCodeAt(i)!=1028))
        k++;
    }    
  }
  if (k>0)
    return true;
  else return false;  
}  

function CheckEnSymbols(st)
{
  var k=0;
  for(i=0; i<st.length; i++)
  {
    if ((st.charCodeAt(i)>1040) && (st.charCodeAt(i)<1104))
      k++;
    else if ( (st.charCodeAt(i)==1110) && (st.charCodeAt(i)==1111) && (st.charCodeAt(i)==1030) && (st.charCodeAt(i)==1031) && (st.charCodeAt(i)==1108) && (st.charCodeAt(i)==1028))
      k++;
  }
  if (k>0)
    return true;
  else return false;    
}