function ltrim(s)
{
  return s.replace(/^\s+/, ''); 
}

function rtrim(s)
{
  return s.replace(/\s+$/, ''); 
}

function validateEmail(val_mail)
{ 
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
	return emailPattern.test(val_mail); 
} 

function reloadCaptcha ()
{	
	document.captcha.src = '/captcha.php?' + Math.random();
}

function checkCaptcha(form_id, lang)
{
    if (typeof(lang) == 'undefined') {
        lang = 'rus';
    }
    
	var result = true;
	var c = new IO('/checkCaptcha.php?code='+document.getElementById('code').value, 'utf-8',false, true); 
	c.handle(
				function (data) 
				{	
					try{
						var  json = eval('('+data+')');
						switch(json)
						{
							case '404':
                                if (lang == 'rus') {
								    alert('Форма не была отправлена по следующей причине: \r\n\n *Код не был сгенерирован! \r\n\n Пожалуйста исправьте проблему и повторите попытку.');
                                } else {
                                    alert('The form was not sent for the following reason: \r\n\n * ID was not generated! \r\n\n Please correct the problem and try again.');
                                }
								reloadCaptcha();
								return false;
							break;
							case 'err':
                                if (lang == 'rus') {
								    alert('Форма не была отправлена по следующей причине: \r\n\n  *Введенный код не верен! \r\n\n Пожалуйста исправьте проблему и повторите попытку.');
                                } else {
                                    alert('The form was not sent for the following reason: \r\n\n * Your code is not correct! \r\n\n Please correct the problem and try again.');
                                }
								reloadCaptcha();
								return false;
							break;
							case 'ok':
								document.forms[form_id].submit();
								return true;
							break;
						}
					}
					catch (e) {
						alert(e);
						return false;
					}						
				}
			); 			
			c.say('get',{});
			
}	
