var send = 0;
function srch()
{
 document.f.city.value=clearSP(document.f.city.value);
 document.f.street.value=clearSP(document.f.street.value);
 document.f.fname.value=clearSP(document.f.fname.value);
 document.f.keywords.value=clearSP(document.f.keywords.value);

 if ((document.f.city.value.length<1) && (document.f.street.value.length<1) && (document.f.fname.value.length<1) && (document.f.keywords.value.length<1))
 {
   alert("Моля, попълнете критерии за търсене!");
   return(false);
 }

 if ((document.f.city.value.length<1) && (document.f.street.value.length<1) && (document.f.fname.value.length<1))
 {
   alert("Моля, попълнете задължителните полета!");
   return(false);
 }
 
 if ((document.f.fname.value.length>1) && (document.f.city.value.length<1) && (document.f.street.value.length<1))
 {
   alert("Моля, попълнете населено място или адрес!");
   return(false);
 }
 
 if ((document.f.fname.value.length>1) && (document.f.city.value.length<1) && (document.f.street.value.length>1))
 {
   alert("Моля, попълнете населено място!");
   return(false);
 } 

 if ((document.f.fname.value.length<1) && ((document.f.city.value.length>1) || (document.f.street.value.length>1)) && document.f.custtype[1].checked)
 {
   alert("Моля, попълнете име и фамилия!");
   return(false);
 }
 if ((document.f.fname.value.length<1) && ((document.f.city.value.length>1) || (document.f.street.value.length>1)) && document.f.custtype[0].checked)
 {
   alert("Моля, попълнете име на фирма!");
   return(false);
 }
 if ((document.f.city.value.length>1 && document.f.city.value.length<3) || (document.f.street.value.length>1 && document.f.street.value.length<3) || ( document.f.fname.value.length>1 && document.f.fname.value.length<3))
 {
     alert("Критериите за търсене не са достатъчни!");
     return(false); 
 }
 if (document.f.custtype[1].checked==true)
 {
   if (!findSP(document.f.fname.value))
   {   
     alert("Моля, попълнете име и фамилия!");
     return (false); 
   }
 }
 if (send==0){    send=1;
   return true;
 }
}


function cntr()
{
 document.f.control.value='0';

}

function srchcity()
{
 if ((document.f.city.value.length<1) && (document.f.codecity.value.length<1))
 {
   alert("Моля, въведете критерии за търсене!");
   return(false);
 }
 if ((document.f.city.value.length<1) && (document.f.codecity.value==0))
 {
   alert("Моля, въведете коректен код!");
   return(false);
 }
 
 if ((document.f.city.value.length>0) && (document.f.codecity.value.length>0))
 {
   alert("Моля, въведете само населено място или само код!");
   return(false);
 }

 document.f.submit();   
}

function srchcountry()
{
 if ((document.f.city.value.length<1) && (document.f.codecity.value.length<1))
 {
   alert("Моля, въведете критерии за търсене!");
   return(false);
 }
 if ((document.f.city.value.length>0) && (document.f.codecity.value.length>0))
 {
   alert("Моля, въведете само държава или само код!");
   return(false);
 }
 if ((document.f.city.value.length<1) && (document.f.codecity.value==0))
 {
   alert("Моля, въведете коректен код!");
   return(false);
 }
 document.f.submit();   
}

function FillArea(name)
{
  document.f.city.value=name;
  if (document.f.temp.value=="city.html")	
  {
    if ((document.f.codecity.value<0) || (document.f.codecity.value==0) || (document.f.codecity.value>0))
    {
    document.f.codecity.value="";
    }
    document.f.submit();
  }
  
}

function ClearText(par)
{
  if (par=='1')	
  {
    document.f.codecity.value="";
  }
  if (par=='2')	
  {
    document.f.city.value="";
  }
  
}

function ClearFields()
{
  if (document.f.temp.value=="home.html")
  {
    document.f.city.value="";
    document.f.street.value="";
    document.f.fname.value="";
    document.f.keywords.value="";
    document.f.control.value=0;
    document.f.submit();
  }
  if ((document.f.temp.value=="city.html") || (document.f.temp.value=="country.html"))
  {
    document.f.city.value="";
    document.f.codecity.value="";
    document.f.control.value=0;
    document.f.submit();
  }
  
}

function initPage(ch)
{
  if (ch!="0")
  {
    document.f.custtype[0].checked = true;
    if (ch=="1") 
    {
      document.f.custtype[1].checked = true;
    }
  }
}

function findSP(str)
{
  for (i=0;i<str.length-2;i++)
  {
    a=str.substr(i,1);
    if (a == " " && str.substr(i+1,1)!=" ")
      return true;
  }
 return false;
}

function clearSP(str)
{
  var str1="";
  for (i=0; i<str.length; i++) 
  {
   trChar =str.charAt(i)
   if (trChar != " ") break;
  }
           j=i;
  for (i=str.length-1; i>0; i--) 
  {
   trChar =str.charAt(i);
   if (trChar != " ") break;
  }
    if (i>0) str1= str.substring(j,i+1);
  return(str1);
}

function ValidateForm()
{
  document.f.city.value=clearSP(document.f.city.value);
  document.f.fnamebg.value=clearSP(document.f.fnamebg.value);
  document.f.fnameen.value=clearSP(document.f.fnameen.value);
  document.f.trademark.value=clearSP(document.f.trademark.value);
  document.f.addr.value=clearSP(document.f.addr.value);
  document.f.cphone.value=clearSP(document.f.cphone.value);
  document.f.person.value=clearSP(document.f.person.value);
  document.f.sentfrom.value=clearSP(document.f.sentfrom.value);
  document.f.faction.value=clearSP(document.f.faction.value);
  document.f.pcode.value=clearSP(document.f.pcode.value);
  document.f.fax.value=clearSP(document.f.fax.value);
  document.f.email.value=clearSP(document.f.email.value);
  document.f.homepage.value=clearSP(document.f.homepage.value);
  document.f.description.value=clearSP(document.f.description.value);
  var err = "";
  if (document.f.city.value.length<1)
  {
    err = err + "Селище\n";
    document.f.city.focus();
  }
  if (!checkValue(document.f.city.value))
  {
    alert("Некоректно име на Селище!");
    document.f.city.focus();
    return(false);	
  }
  if (document.f.fnamebg.value.length<1)
  {
    err = err + "Фирма\n";
    document.f.fnamebg.focus();
  }
  if (!checkValue(document.f.fnamebg.value))
  {
    alert("Некоректно име на Фирма!");
    document.f.fnamebg.focus();
    return(false);	
  }
  if (!checkValue(document.f.fnameen.value))
  {
    alert("Некоректно име на Фирма/английски вариант/!");
    document.f.fnameen.focus();
    return(false);	
  }
 
  if (!checkValue(document.f.trademark.value))
  {
    alert("Некоректен запис за Търговска марка!");
    document.f.trademark.focus();
    return(false);	
  }
  if (document.f.faction.value.length<1)
  {
    err = err + "Предмет на дейност\n";
    document.f.faction.focus();
  }
  if (!checkValue(document.f.faction.value))
  {
    alert("Некоректен запис за Предмет на дейност!");
    document.f.faction.focus();
    return(false);	
  }

  if (!checkValue(document.f.addr.value))
  {
    alert("Некоректен запис за Адрес!");
    document.f.addr.focus();
    return(false);	
  }
  if (!checkValue(document.f.pcode.value))
  {
    alert("Некоректен запис за Пощенски код!");
    document.f.pcode.focus();
    return(false);	
  }
  if (document.f.cphone.value.length<1)
  {
    err = err + "Телефонен код/Телефон/Служба\n";
    document.f.cphone.focus();
  }
  if (!checkValue(document.f.cphone.value))
  {
    alert("Некоректен запис за Телефонен код/Телефон/Служба!");
    document.f.cphone.focus();
    return(false);	
  }
  
  if (!checkValue(document.f.fax.value))
  {
    alert("Некоректно въведен Факс!");
    document.f.fax.focus();
    return(false);	
  }
  if(document.f.email.value)
  {
    if(!ValidateMail(document.f.email.value))
    {
      alert("Моля, въведете валиден e-mail!");
      document.f.email.focus();
      return false;
    }
  }

  if (!checkValue(document.f.homepage.value))
  {
    alert("Некоректно въведен Home page!");
    document.f.homepage.focus();
    return(false);	
  }
  if (document.f.person.value.length<1)
  {
    err = err + "Отговорно лице\n";
    document.f.person.focus();
  }
  if (!checkValue(document.f.person.value))
  {
    alert("Некоректно въведено име на Отговорно лице!");
    document.f.person.focus();
    return(false);	
  }
  if (document.f.sentfrom.value.length<1)
  {
    err = err + "Изпратил\n";
    document.f.sentfrom.focus();
  }
  if (!checkValue(document.f.sentfrom.value))
  {
    alert("Некоректно въведено име на Изпратил!");
    document.f.sentfrom.focus();
    return(false);	
  }
  if (!checkValue(document.f.description.value))
  {
    alert("Некоректно въведен Коментар!");
    document.f.description.focus();
    return(false);	
  }
  
  if (err!="") 
  {
   alert("Моля, попълнете:\n"+err);
   return false;
  }



  if (err=="") 
   return true;
  alert("Моля, попълнете:\n "+err);
   return false;

}

function checkValue(field)
{
  var vle  = field.length -1;
  var j  = 0;
    for(i=0; i<vle; i++)
  {
    if (field.charAt(i) == field.charAt(i+1)) j++;
  }
  //alert("j="+j+" vle="+vle);
   if(j==vle)  return false;
  return true;
}

function ValidateMail(email)
{
  var reg = /[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.a-z0-9-])/i;
  var result = email.match(reg);
  return result;
}

var count=0;
var senderVal;
function textMsgLeft(par)
{
  var chars, message, senderNew;
  var anz=0, len=0,s;
  if (par==1)
    var par1 = document.f.cphone.value;
  else
    var par1 = document.f.description.value;
  var msg=par1;
//  msg=clearcrlf(msg);
  if (msg != par1)
    par1=msg;
  chars = 254;
  for (var i=0; i<msg.length; i++) 
  {
    if (anz==chars) 
    {
	count = 0;
	break;
    }
    anz++;
  }
  len = i;
  if (msg.length >= chars) 
  {
	var IE=(navigator.appName.indexOf("Explorer")!=-1)?true:false;
	var Mozilla=(navigator.userAgent.indexOf("Mozilla")!=-1)?true:false;
	var Build=(navigator.userAgent.indexOf("rv:")!=-1)?true:false;
	var Opera=(navigator.userAgent.indexOf("Opera")!=-1)?true:false;
	var Netscape=(navigator.appName.indexOf("Netscape")!=-1)?true:false;
	var NS6=(navigator.userAgent.indexOf("netscape6") != -1)?true:false;
	var NS7=(navigator.userAgent.indexOf("netscape7") != -1)?true:false;
	if (senderNew!=senderVal) 
        {
           senderVal = senderNew;
					if (par==1) document.f.cphone.value = msg.substring(0,len);
					else        document.f.description.value = msg.substring(0,len);
	}
        else 
        {
	   if ( (IE && !Opera) || (Mozilla && Build) || (NS6 && !Opera) || (NS7 && !Opera) ) {

					if (par==1) document.f.cphone.value = msg.substring(0,len);
					else        document.f.description.value = msg.substring(0,len);
				}
                                else {
					if (count == 0) 
                                        {
					if (par==1) document.f.cphone.value = msg.substring(0,len);
					else        document.f.description.value = msg.substring(0,len);
						count++;
					} else {
					if (par==1) document.f.cphone.value = msg.substring(0,len);
					else        document.f.description.value = msg.substring(0,len);
					}
					if (par==1) document.f.cphone.value = msg.substring(0,len);
					else        document.f.description.value = msg.substring(0,len);
				}
			}
		}
 		senderVal = senderNew;
//		document.f.msgLen.value = chars-anz-1;
		if (navigator.appName =="Netscape" && parseInt(navigator.appVersion) <= 4) {
			document.captureEvents(Event.KEYPRESS);document.f.text.onkeypress = textMsgLeft;
			document.captureEvents(Event.CHANGE);document.f.sender.onchange = textMsgLeft;
			document.captureEvents(Event.KEYPRESS);document.f.From.onkeypress = textMsgLeft;
		}
	}
