function isDigit(c)
{
  if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' ||
      c == '5' || c == '6' || c == '7' || c == '8' || c == '9')
      return true;
   else
      return false;
}

function validatePhone(str)
{
  while (str.charAt(0) == ' ')
  {
    str = str.substring(1, str.length);
  }
  while (str.charAt(str.length) == ' ')
  {
    str = str.substring(0, str.length-1);
  }
  if (str == "") return false;
  var length = str.length;
  var c;
  for (var i=0; i < length; i++)
  {
    c = str.charAt(i);
    if (c != '(' && c != ')' && c != '-' && c != ' ' && !isDigit(c))
    {
       return false;
    }
  }
  return true;
}

function validateString(str)
{
  while (str.charAt(0) == ' ')
  {
    str = str.substring(1, str.length);
  }
  while (str.charAt(str.length) == ' ')
  {
    str = str.substring(0, str.length-1);
  }
  if (str == "")
     return false;
  else
     return true;
}

function validateDigit(str)
{
  while (str.charAt(0) == ' ')
  {
    str = str.substring(1, str.length);
  }
  while (str.charAt(str.length) == ' ')
  {
    str = str.substring(0, str.length-1);
  }
  if (str == "") return false;
  var length = str.length;
  var c;
  for (var i=0; i < length; i++)
  {
    c = str.charAt(i);
    if (c != ' ' && !isDigit(c))
    {
       return false;
    }
  }
  return true;
}



function validateEMail(str)
{
  while (str.charAt(0) == ' ')
  {
    str = str.substring(1, str.length);
  }
  while (str.charAt(str.length) == ' ')
  {
    str = str.substring(0, str.length-1);
  }
  if (str == "")
     return false;
  if (str.indexOf("@") < 0 || str.indexOf("@") >= str.length-1)
     return false;

  return true;
}



// Главное

var newpr = new Array(); 
var num = 2;

function Update( elem )
{
	if(elem.name.indexOf("%") != 0)
		elem.name = "%" + elem.name;
}

function UpdateBlur( elem )
{
	if(elem.value != "" && elem.name.indexOf("%") != 0)
		elem.name = "%" + elem.name;
}


function ShowMore()
{
	document.all.item('mp' + num).style.display = 'inline';
	num = num + 2;
	
	if(document.all.item('mp' + num) == null)
		document.all.item('mpnext').style.display = 'none';
	document.all.item('mpprev').style.display = 'inline';
}

function HideLast()
{
	num = num - 2;
	document.all.item('mp' + num).style.display = 'none';

	if(document.all.item('mp' + (num-2)) == null)
		document.all.item('mpprev').style.display = 'none';
	document.all.item('mpnext').style.display = 'inline';

}

function FormFiltring()
{
	var size = newpr.length;
	for(var i = 0;i < size;i++)
	{
		var e = newpr.pop(); 
		if(e.name.indexOf("%") != 0)
			e.name = "%" + e.name;
	}
}

function ClearForm( form )
{
	for(var i = 0;i < form.elements.length;i++)
	{
		var e = form.elements[i];
		if(e.name.indexOf("%") != 0 && e.type != "submit" && e.type != "hidden" && e.type != "button")
			e.value = "";
	}
}

function CheckForm( form, change )
{
	for(var i = 0;i < form.elements.length;i++)
	{
		var e = form.elements[i];
		if(e.id != "")
		{
			var value = e.value;
			if(e.id.indexOf("p") == 0 && e.id.indexOf("a") == -1)
			{
				if (!validateString(value) && !change)
				{
					alert("Неправильный пароль!\nПопытайтесь еще!");
					e.focus();
				    return false;
				}
				else if(document.all.item(e.id + "a") == null || document.all.item(e.id + "a").value != value)
				{
					alert("Введенные пароли не совпадают!\nПопытайтесь еще!");
					document.all.item(e.id + "a").focus();
				    return false;
				}
			}
			else if((e.name.indexOf("prs") == 0 || e.name.indexOf("%prs") == 0) && !change)
			{
				if (!validateEMail(value))
				{
					alert("Неправильный адрес электронной почты!\nПопытайтесь еще!");
					e.focus();
				    return false;
				}
			}
			
		}
	}
	return true;
}

function processSubmit( form )
{
	var flag = false;
	flag = CheckForm(form, false);
	document.all.item('filtered').value = 'true';
	if(flag == true)
		ClearForm(form);

	return flag;
}

function processSubmitChange( form )
{
	var flag = false;
	flag = CheckForm(form, true);
	document.all.item('filtered').value = 'true';
	if(flag == true)
		ClearForm(form);

	return flag;
}


