C=new Array(
'А','Б','В','Г','Д','Е','Ж','З','И','Й','К','Л','М','Н','О',
'П','Р','С','Т','У','Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ь','Ю','Я',
'а','б','в','г','д','е','ж','з','и','й','к','л','м','н','о',
'п','р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ь','ю','я');
L=new Array(
'A','B','V','G','D','E','Zh','Z','I','J','K','L','M','N','O',
'P','R','S','T','U','F','H','Ts','Ch','Sh','Sht','A','A','Yu','Ya',
'a','b','v','g','d','e','zh','z','i','j','k','l','m','n','o',
'p','r','s','t','u','f','h','ts','ch','sh','sht','a','a','yu','ya');


function Validatephone()
{

 document.f.username.value=clearSP(document.f.username.value);
 if (document.f.username.value == '' )
  {    
      alert("Моля, попълнете Мобилен номер!");     
      document.f.username.focus();
      return false;
  }
 if(document.f.username.value)
  {
    if(!ValidatePhone(document.f.username.value))
    {
       alert("Моля, попълнете коректен Мобилен номер!");
   	   document.f.username.focus();
       return false;
    }
  }
  if(document.f.username.value.length < 9)
 {
    alert("Моля, попълнете коректен Мобилен номер с не по-малко от 9 цифри!");
    document.f.username.focus();
    return false;
 }
   uph=document.f.username.value;

      if(uph.substring(0,1) == '0')
      {
        document.f.uphone.value=uph.substring(1,uph.length); 
      } 
      else
      { 
        document.f.uphone.value=document.f.username.value;
      }
//  'uphone=' +  document.f.uphone.value;  
//  document.f.submit();
  return(true);
 }

function ValidateLog()
{
  document.f.username.value=clearSP(document.f.username.value);
  document.f.password.value=clearSP(document.f.password.value);

  if (document.f.username.value == '' )
  {    
      alert("Моля, попълнете Мобилен номер!");     
      document.f.username.focus();
      return false;
  }

  if(document.f.username.value)
  {
    if(!ValidatePhone(document.f.username.value))
    {
       alert("Моля, попълнете коректен Мобилен номер!");
   	   document.f.username.focus();
       return false;
    }
  }
  if(document.f.username.value.length < 9)
 {
    alert("Моля, попълнете коректен Мобилен номер с не по-малко от 9 цифри!");
    document.f.username.focus();
    return false;
 }
  
  if(document.f.password.value.length<=1)
  {
    alert ("Моля, попълнете Парола - не по-малка от 2 символа!")
    document.f.password.focus();
    return(false); }

   pas1 = document.f.password.value;
   
   if((pas1.length < 2) || (pas1.length > 32)){
    alert ("Паролата трябва да бъде не по-малка от 2 и не-голяма от 32 символа!")
    return(0);
  }
  document.f.submit();
  return(true);
 }
 
function ClearFields()
{
  document.f.username.value="";
  document.f.submit();
  return(true);

}

function ValidateReg()
{
   if (document.f.temp.value=="home.html")
   {
     document.f.mphone.value=clearSpace(document.f.mphone.value);
   }

   document.f.age.value=clearSpace(document.f.age.value);
   document.f.sex.value=clearSpace(document.f.sex.value);
   document.f.city.value=clearSP(document.f.city.value);
   document.f.first_name.value=clearSP(document.f.first_name.value);
   document.f.last_name.value=clearSP(document.f.last_name.value);
   document.f.email.value=clearSpace(document.f.email.value);

   if (document.f.temp.value=="home.html")
   {
      if (document.f.mphone.value == '' )
      {    
         alert("Моля, попълнете Мобилен номер!");     
         document.f.mphone.focus();
         return false;
      }
      if (document.f.mphone.value)
      {
        if(!ValidatePhone(document.f.mphone.value))
        {
            alert("Моля, попълнете коректен Мобилен номер!");
            document.f.mphone.focus();
            return false;
         }
      }
      if(document.f.mphone.value.length < 9)
      {
         alert("Моля, попълнете коректен Мобилен номер с не по-малко от 9 цифри!");
         document.f.mphone.focus();
         return false;
      }
	  mph=document.f.mphone.value 
      if(mph.substring(0,1) == '0')
      {
        document.f.lphone.value=mph.substring(1,mph.length); 
      } 
      else
      { 
        document.f.lphone.value=document.f.mphone.value;
      }
      document.f.control.value=1;
   }


/*   pas3 = document.f.username.value; 

 if((pas3.length < 6) || (pas3.length > 32))
 {
   alert ("Моля, попълнете Потребителското име по-голямо от 6 и по-малко от 32 символа!");
   document.f.username.focus();
   return(0);
 }

  if(!ValidateUserName(document.f.username.value))
 {
    alert ("Моля, попълнете коректно Потребителско име!")
    document.f.username.focus();
    return(false); 
 }

 
  if(document.f.password.value.length<=1){
    alert ("Моля, попълнете Парола - не по-малка от 2 символа!")
    document.f.password.focus();
    return(false); }

   pas1 = document.f.password.value;
   
  if((pas1.length < 2) || (pas1.length > 32)){
    alert ("Паролата трябва да бъде не по-малка от 2 и не-голяма от 32 символа!")
 return(0);
}
*/

 if(document.f.first_name.value.length<=1){
   alert ("Моля, попълнете Име - не по-малко от 2 символа!")
   document.f.first_name.focus();
  return(false); }

 if(document.f.last_name.value.length<=1)
 {
     alert ("Моля, попълнете Фамилия - не по-малка от 2 символа!")
     document.f.last_name.focus();
     return(false); 
 }

/* if (document.f.email.value == '' )
 {    
      alert("Моля, попълнете E-mail!");     
      document.f.email.focus();
      return false;
  }
*/

 if (document.f.email.value != '' )
 {
     if(!ValidateMail(document.f.email.value))
     {
        alert("Моля, попълнете коректен E-mail!");
        document.f.email.focus();
        return false;
     }
}

 if (document.f.sex.value == '' )
  {    
      alert("Моля, попълнете Пол!");    
      document.f.sex.focus();
      return false;
  }

 if (document.f.age.value == '' )
  {    
      alert("Моля, попълнете Години!");     
      document.f.age.focus();
      return false;
  }

  if (!checkNum(document.f.age.value)) 
  {    
      alert("Моля, попълнете коректно Години!\nПозволени са символи от 0 до 9!");     
      document.f.age.focus();
      return false;
  }


 if (document.f.city.value == '' )
  {
     alert("Моля, попълнете Населено място!");
     document.f.city.focus();
     return false;
  }
  if (document.f.temp.value=="home.html")
  {
    if (!document.f.cond.checked)
    {
       alert("За да използвате VivaSMS, е необходимо да приемете Общите условия.");     
       return false;
    }
  }

  if (document.f.temp.value!="home.html")
  {
     document.f.control.value=1;
  } 
    document.f.submit();
    return(true);
 }


function ValidateUserName(name)
{
  if(name.length < 6 || name.length > 32){ return(0); }
  if(name.charAt(0) >= '0' && name.charAt(0) <= '9'){ return(0);}
  for(j=0; j<name.length; j++){
      c = name.charAt(j);
      if((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='а' && c<='я') || (c>='А' && c<='Я') || (c>='0' && c<='9') || (c=='.') || (c=='_') || (c=='/') || (c=='\\'))
          continue;
      return(0);
  }

  return(1);
}

function ValidateMail(mail){
  var reg = /[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.a-z0-9-])/i;
  var result = mail.match(reg);
  return result;
}

function ValidatePhone(phone){
  if (phone.length==0) return (false);
  for(i=0; i<phone.length; i++){
	  if(phone.charAt(i) >= '0' && phone.charAt(i) <= '9') 
      continue;
      else return(false);
  }
 return (true);
}

function clearSpace(field)
{
    var le = field.length;
    for(i=0; i<30; i++){
	field  = field.replace(" ","");
     if (le == field.length) return(field);
     le=field.length;
        }
	return 0;
}

function clearSP(str)
{
  var str1="";
    
    for (i=0; i<str.length; i++)
    {
      trChar =str.charAt(i)
      if (trChar != " ") 
      {
        str1= str.substring(i,str.length);
        break;
      }
    }
  return(str1);
}

function checkNum(number)
{
    if (number.length==0) return true;
		trueChars = "0123456789";
		for (i=0; i<number.length; i++) {
			trChar =number.charAt(i)
			if (trueChars.indexOf(trChar,0) < 0) {
				return false;
			}
		}

  return true;
}  

function ClearReg()
{
  document.f.mphone.value="";
  document.f.submit();
  return(true);

}

function ValidateSend()
{
  document.f.phones.value=clearSpace(document.f.phones.value);
  document.f.det.value=clearSP(document.f.det.value);

  if (document.f.phones.value == '' )
  {    
      alert("Моля, попълнете Мобилен номер!");     
      document.f.phones.focus();
      return false;
  }
   if(document.f.phones.value.length < 9)
  {
    alert("Моля, попълнете коректен Мобилен номер с не по-малко от 9 цифри!");
 	 document.f.phones.focus();
     return false;
  }

  if (document.f.det.value == '' )
  {    
      alert("Моля, попълнете Текст!");     
      document.f.det.focus();
      return false;
  }
  if(document.f.phones.value)
  {
     if(!ValidatePhone(document.f.phones.value))
	 {
        alert("Моля, попълнете коректен Мобилен номер!");
 	    document.f.phones.focus();
        return false;
     }
  	 
     mph=document.f.phones.value 
     if(mph.substring(0,1) == '0')
    {
       document.f.lphone.value=mph.substring(1,mph.length); 
    } 
    else
    { 
       document.f.lphone.value=document.f.phones.value;
    }
  }	
    mph1=document.f.loginuser.value 
     if(mph1.substring(0,1) == '0')
    {
       document.f.ltphone.value=mph1.substring(1,mph1.length); 
    } 
    else
    { 
       document.f.ltphone.value=document.f.loginuser.value;
    }
  document.f.submit();
  return(true);
}



function textSMSLeft()
{
    var chars, message, senderNew;
    var anz=0, len=0,s;
    var msg = document.f.det.value;
    msg = ConvertCyrToLat(msg);
    msg=clearcrlf(msg);
    if (msg != document.f.det.value)
    document.f.det.value=msg;

    chars = 100;
    for (var i=0; i<msg.length; i++) 
    {
       if (anz==chars) 
       {
          count = 0;
          break;
       }
         anz++;

//			alert(msg.charCodeAt(i));
    }
    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;
	   document.f.det.value = msg.substring(0,len);
	} 
              else 
              {
		if ( (IE && !Opera) || (Mozilla && Build) || (NS6 && !Opera) || (NS7 && !Opera) ) 
                            {
		  document.f.det.value = msg.substring(0,len);
		} 
                            else 
                            {
		  if (count == 0) 
                              {
			document.f.det.value = msg.substring(0,len);
			count++;
    		  } 
                              else 
                              {
		              document.f.det.value = msg.substring(0,len);
		  }
		  document.f.det.value = msg;
	 }
         }
      }
      senderVal = senderNew;
      document.f.msgLen.value = chars-anz;
      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;
      }
}


function clearcrlf(field)
{
	var le = field.length;
    for(i=0; i<30; i++){
	field  = field.replace("\r\n\r\n","\n");
     if (le == field.length) return(field);
     le=field.length;
        }
	return 0;
}

function ClearSend()
{
  document.f.phones.value="";
  document.f.det.value="";
  document.f.submit();
  return(true);

}


function ConvertCyrToLat(cyr)
{
  lat="";
  for (q=0;q<cyr.length;q++)
  {
    ch=cyr.charAt(q);
    chnumb=ch.charCodeAt(0) 
  if(chnumb<127)
    lat+=ch;
  else
  {
     for (q1=0;q1<60;q1++)
     {
        ch1=C[q1];
        ch2 = ch1.charCodeAt(0)
        if(ch2 != chnumb)
          continue;
        else 
        {
           lat+=L[q1];
           break; 
        }
      } 
    }
 }
 return lat;
} 


 function ValidatePassword()
{

  if(document.fl.oldpass.value.length<1)
  {
      alert ("Моля, въведете стара парола!");
      document.fl.oldpass.focus();
      return (false); 
  }

  if(document.fl.newpass1.value.length<1)
  {
      alert ("Моля, въведете Нова парола!");
      document.fl.newpass1.focus();
      return (false); 
  }

  if(document.fl.newpass2.value.length<1)
  {
      alert ("Моля, повторете паролата!");
      document.fl.newpass2.focus();
      return (false); 
   }

   pas1 = document.fl.newpass1.value;
   pas2 = document.fl.newpass2.value;

  if((pas1.length < 2) || (pas1.length > 32))
  {
     alert ("Паролата трябва да съдържа от 2 до 32 символа!");
     return (false);
  }

   if(document.fl.newpass1.value != document.fl.newpass2.value)
   {
        alert ("Въведените от вас пароли не са идентични. Паролите в Полета Нова парола и Повторение на нова парола трябва да са еднакви!");
        return (false);
   }

   if(document.fl.oldpass.value == document.fl.newpass1.value)
   {
        alert ("Новата парола трябва да е различна от старата!");
        return (false);
   }
   return (true);
}


 
