var maxLength = 160
function count(str) {
var count = 0;
for (i=0;i<str.length;i++) {
val = str.charCodeAt(i);
if (val >31) count++; 
}
return count;
}

function find(str,num) {
var count = 0;
var i=0;
while (count < num) {
val = str.charCodeAt(i);
if (val >31) count++;
i++;
}
return i;
}

function changeValues(text, textlgth) {
cutoff = find(text,maxLength)
document.dialog_form.message.value=text.substring(0,cutoff);
text=document.dialog_form.message.value;
textlgth = text.length;
document.dialog_form.message_length.value = maxLength;
}

function doChange() {
text = document.dialog_form.message.value;
textlgth = count(text);
document.dialog_form.message_length.value = textlgth;
if (textlgth > maxLength) {
changeValues(text, textlgth);
}

  return true;
}

function check(theForm)
{
  if (theForm.phone.value == "")
  {
    alert("Необходимо указать телефонный номер абонента!");
    theForm.phone.focus();
    return false;
  }

  if (theForm.phone.value.length < 7)
  {
    alert("Необходимо указать последние 7 цифр телефонного номера абонента!");
    theForm.phone.focus();
    return false;
  }

  var checkOK = "0123456789";
  var checkStr = theForm.phone.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }

  if (!allValid)
  {
    alert("Номер абонента должен содержать только цифры!");
    theForm.phone.focus();
    return false;
  }

  if ((theForm.message.value == "") || (theForm.message.value.length == 0))
  {
    alert("Необходимо ввести текст SMS-сообщения!");
    theForm.message.focus();
    return false;
  }

  if (theForm.message.value.length > 160)
  {
    alert("Текст SMS-сообщения слишком большой!");
    theForm.message.focus();
    return false;
  }

  if (theForm.code.value.length < 4)
  {
    alert("Пожалуйста, введите код, указанный на изображении!");
    theForm.code.focus();
    return false;
  }

  return true;
}
