 var email_error_text = "Проверьте написание адреса";                   							// сообщение об ошибки email
 var name_error_text = "Напишите своё имя";                      								// сообщение об ошибки name
 var message_error_text = "Сообщение слишком короткое";  	// сообщение об ошибки message
 var message_error_agree = "Вы должны подтвердить, что прочитали Помощь";        // сообщение об ошибки agreement (галка)
 var allfiles = 3  		// Сколько файлов можно прикреплять  

/////////////////////////////////////////////
function show_agree(v){                          
  var prod = v.value;
  document.getElementById('l_agree').style.display = (prod == "WebDesign_General")? "none":"block"; 
}

function echeck(v) {                               // проверка email
    $("#err_email").html('');
    var str = v.value
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   $("#err_email").html(email_error_text)
		   v.focus()
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   $("#err_email").html(email_error_text)
		   v.focus()
       return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   $("#err_email").html(email_error_text)
		    v.focus()
        return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   $("#err_email").html(email_error_text)
		    v.focus()
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   $("#err_email").html(email_error_text)
        v.focus()
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   $("#err_email").html(email_error_text)
		    v.focus()
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   $("#err_email").html(email_error_text)
		    v.focus()
		    return false
		 }
 		 return true					
	}


function ncheck(str){                                // Проверка имени
  $("#err_name").html('')
var count = str.value.length;   
if(count<1){
  $("#err_name").html(name_error_text)
  str.focus()
  return false
  }
  return true
}

function tcheck(str){                                // проверка текста
$("#err_text").html('');
var count = str.value.length
if(count<7){            
  $("#err_text").html(message_error_text)
  str.focus()
  return false
  }
  return true
}

function acheck(str){
 $("#err_agree").html('')
  var agr = str.checked
  prod = $("#product").val()
  if(prod == "WebDesign_General" || agr)
  return true
  else {
  $("#err_agree").html(message_error_agree)
  return false
  }
}

function validate(v){                              // проверка формы перед отправкой
  if (ncheck(v.name ))
  if (echeck(v.email))
  if (tcheck(v.text ))
  if (acheck(v.agree)){
  $(".remove_btn").hide()
  return true
  }
return false;
}

var fc;
var clon;
var att = new Array()

for (var i=1; i<(allfiles+1); i++){
    att[i]=1
}

 function freecell()
 {
   for (var i=1; i<(allfiles+1); i++){
   if (att[i])return i
   }
   return false
 }

 function countCell()
 {   var count = 0
   for (var i=0; i<allfiles; i++){
   if (!att[i+1])count++;
   }
   return count
 }


 function AttBtn(v){
 var fc1 = countCell()
 	if (fc1 == 0)$("#att_btn").html("<a href='#' onClick='attfile("+v+")'>Прикрепить файл</a>")
 	else
 	if (fc1 < allfiles)$("#att_btn").html("<a href='#' onClick='attfile("+v+")'>Прикрепить ещё один файл</a>")
	else $("#att_btn").html("")
	
 }

 function remfile(v){
 att[v]=0
 	$("#file_"+v).next("i:first").html(" <a href='#' id='file_"+v+"' class='remove_btn' onclick='removeAtt("+v+")'>Удалить</a>");
 	AttBtn(v)
 }
 
 function removeAtt(v){
 	$("#div_"+v).hide("slow")
 	$("#file_"+v).next("i:first").remove();
 	$("#file_"+v).remove();
 	att[v]=1
  AttBtn(v) 	 	
 }
 
 function attfile(v){
  var fc = freecell()
 	$("#att_btn").html("")	
  clon = "<div id='div_"+ fc +"'><input id='file_"+ fc +"' type='file' name='file[]' onChange='remfile("+ fc +")'><i></i></div>" 
	$(clon).appendTo("#attach")
 };

