// JavaScript Document
function isblank(s) {
for(var i=0; i<s.length; i++) {
	var c = s.charAt(i);
	if((c != ' ') && (c != '\n') && (c != '')) return false;
}
return true;
}

function isEMailAddr(elem) {
	var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
         return false;
    } else {
        return true;
    }
}
function isPhoneAddr(phone) {
    return !(phone.value.match('[^0-9]'));
}

function isPhone_mobAddr(phone_mob,oper) {
    return !(phone_mob.value.match('[^0-9]')||oper.value.match('[^0-9]'));
}

function isFirmAddr(firm) {
    return !(firm.value.match('[^а-яА-Я"" ]'));
}

function isSityAddr(sity) {
    return !(sity.value.match('[^а-яА-Я"" ]'));
}

function isNameAddr(name) {
	var n = name.value;
    var k=0;
	    function chcnt(inb){
     var l=0;
     if((l=n.indexOf(' ',inb+1))>0){
      k++;
      chcnt(l);

     } else
      return false;
}
chcnt(0);
 if (k<2) {
         return false;
    } else {
        return true;
    }
}

function isName2Addr(name) {
    return !(name.value.match('[^а-яА-Я ]'));
}

function FormReset(fn){
	fn.cname.value="";
	fn.cemail.value="";
	fn.cphone.value="";
	fn.ccod.value="";
	fn.ccomments.innerText="";

  return false;
}
function FormValid(fn){
	var sError = "";
if ((fn.email.value=="")||(isblank(fn.email.value))) {
sError = sError+"\n 'Ваш e-mail'";
document.getElementById('email').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('email').style.backgroundColor='#FFFFFF';
}

if ((fn.name.value=="")||(isblank(fn.name.value))) {
 sError = sError+"\n 'Контактное лицо Ф.И.О.'";
 document.getElementById('name').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('name').style.backgroundColor='#FFFFFF';
}

if ((fn.profil.value=="")||(isblank(fn.profil.value))) {
sError = sError+"\n 'Профиль деятельности Вашей компании'";
document.getElementById('profil').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('profil').style.backgroundColor='#FFFFFF';
}

if ((fn.profil.value=="Другое")&&(fn.profil_dr.value=="")) {
sError = sError+"\n 'Другое'";
document.getElementById('profil_dr').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('profil_dr').style.backgroundColor='#FFFFFF';
}



if(!isPhoneAddr(fn.phone,fn.cod)) {
sError = sError+"\n\n 'Поле \"Телефон\" может содержать только цифры'";
document.getElementById('phone').style.backgroundColor='#FFD1A1';
document.getElementById('cod').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('phone').style.backgroundColor='#FFFFFF';
document.getElementById('cod').style.backgroundColor='#FFFFFF';
}

if ((fn.phone.value=="" && fn.cod.value=="")||(isblank(fn.phone.value)) || (isblank(fn.cod.value))) {
sError = sError+"\n 'Ваш телефон'";
document.getElementById('phone').style.backgroundColor='#FFD1A1';
document.getElementById('cod').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('phone').style.backgroundColor='#FFFFFF';
document.getElementById('cod').style.backgroundColor='#FFFFFF';
}

if ((fn.firm.value=="")||(isblank(fn.firm.value))) {
sError = sError+"\n 'Юр. название компании'";
document.getElementById('firm').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('firm').style.backgroundColor='#FFFFFF';
}

if (sError != "") sError = "Внимание!\nПоля "+sError+"\nдолжны быть заполнены!";

if (fn.forma.value=="Выбрать") {
sError = sError+"\n\n 'Не выбрана форма собственности компании'";
document.getElementById('forma').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('forma').style.backgroundColor='#FFFFFF';
}

if(!isEMailAddr(fn.email)) {
sError = sError+"\n\n 'Некорректный email'";
document.getElementById('email').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('email').style.backgroundColor='#FFFFFF';
}

if(((fn.phone_mob.value!="") || (fn.oper.value!="")) && !isPhone_mobAddr(fn.phone_mob,fn.oper)) {
sError = sError+"\n\n 'Поле \"Моб. телефон\" может содержать только цифры'";
document.getElementById('phone_mob').style.backgroundColor='#FFD1A1';
document.getElementById('oper').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('phone_mob').style.backgroundColor='#FFFFFF';
document.getElementById('oper').style.backgroundColor='#FFFFFF';
}


if(!isFirmAddr(fn.firm) || (fn.firm.value=="")) {
sError = sError+"\n\n 'Поле \"Юр. название компании\" может содержать только русские буквы и \"\"'";
document.getElementById('firm').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('firm').style.backgroundColor='#FFFFFF';
}

if(!isNameAddr(fn.name)) {
sError = sError+"\n\n 'Поле \"Контактное лицо Ф.И.О.\" должно содержать Фамилию Имя Отчество'";
document.getElementById('name').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('name').style.backgroundColor='#FFFFFF';
}
if(!isName2Addr(fn.name) || (fn.name.value=="")) {
sError = sError+"\n\n 'Поле \"Контактное лицо Ф.И.О.\" может содержать только русские буквы'";
document.getElementById('name').style.backgroundColor='#FFD1A1';
}
else {
document.getElementById('name').style.backgroundColor='#FFFFFF';
}
	if (sError != "") {
		alert (sError);
		return false;
	}
	else return true;
}
