$(document).ready(function(){var errorsDisplayed=false;var noPost=false;$('form#agentRegForm input[name="SAVE"], form#agentRegForm input[name="NEXT"], .saveEmployee').click(function(){focus_s='';if($(this).hasClass('saveEmployee')){focus_s='tr#'+$(this).parents('tr.employee').attr('id').toString();}else{focus_s='form#agentRegForm';}
$(focus_s+' input.required, '+focus_s+' textarea.required').change(function(){CheckForm(focus_s);return true;});$(focus_s+' select.required').change(function(){CheckForm(focus_s);return true;});$(focus_s+' input.numeric').change(function(){CheckForm(focus_s);return true;});$(focus_s+' input.email').change(function(){CheckForm(focus_s);return true;});$(focus_s+' select.topic').change(function(){CheckForm(focus_s);return true;});$(focus_s+' input.phone').change(function(){CheckForm(focus_s);return true;});$(focus_s+' input.url').change(function(){CheckForm(focus_s);return true;});$(focus_s+' textarea').change(function(){CheckForm(focus_s);return true;});check_form=CheckForm(focus_s);if(check_form)
{if($(this).hasClass('saveEmployee')){emol_id=$(this).parents("tr.employee:first").attr("id");saveEmployee(emol_id);};return true;}
else
{return false;}});$('#topic').change(function(){if($(this).children('option[@selected]').text()!='Россия'){$('#reg_null').attr('selected',true).show().parent().attr('disabled',true);}else{$('#reg_null').removeAttr('selected').hide().parent().removeAttr('disabled');}});});function CheckForm(fname)
{var valid=true;$(fname+' input.required, '+fname+' textarea.required').each(function(){var res=true;if($(this).attr('type')=='checkbox'||$(this).attr('type')=='radio'){if($('input[@checked]',$(this).parent()).length>0)
res=true;else
res=false;}else{res=(trim($(this).val())!='');}
if(!res){$(this).addClass('error');if(($(this).attr('type')=='checkbox')||($(this).attr('type')=='radio'))
{$(this).css("border","none");}
$(this).siblings('em').remove();$(this).parent().append('<em>Пожалуйста, заполните это поле</em>');valid=false;}else{$(this).removeClass('error');$(this).siblings('em').remove();}});$(fname+' select.required').each(function(){var res=$(this).val();if((!res)||(res==0)){$(this).addClass('error');$(this).siblings('em').remove();$(this).parent().append('<em>Пожалуйста, заполните это поле</em>');valid=false;}else{$(this).removeClass('error');$(this).siblings('em').remove();}});$(fname+' input.numeric').each(function(){var patt=/^(\d)+$/g;this_val=trim($(this).val());if(this_val)
{if(this_val.match(patt)==null)
{$(this).addClass('error');$(this).siblings('em').remove();$(this).parent().append('<em>Значение поля не соответствует формату</em>');valid=false;}
else
{$(this).removeClass('error');$(this).siblings('em').remove();}}
else
{if(!$(this).hasClass('required'))
{$(this).removeClass('error');$(this).siblings('em').remove();}}});$(fname+" input.email").each(function(){email_value=trim($(this).val());correct_value_input=true;if(email_value)
{email_value=email_value.replace(" ","");if(correct_value_input&&(substr_count(email_value,"@")>1))
{correct_value_input=false;}
email_value=email_value.replace(/[\;\#\n\r\*\'\"<>&\%\!\(\)\{\}\[\]\?\\/]/g,"");var patt=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/g;if(email_value.match(patt)==null&&correct_value_input)
{correct_value_input=false;}
else
{$(this).val(email_value);}
if(!correct_value_input)
{$(this).addClass('error');$(this).siblings('em').remove();$(this).parent().append('<em>Значение поля не соответствует формату</em>');valid=false;}
else
{$(this).removeClass('error');$(this).siblings('em').remove();}}
else
{if(!$(this).hasClass('required'))
{$(this).removeClass('error');$(this).siblings('em').remove();}}});$(fname+" input.phone").each(function(){var patt=/^.+\+/g;tel_value=trim($(this).val());correct_value_input=true;if(tel_value)
{if(tel_value.match(patt)!=null&&substr_count(tel_value,"+"))
{correct_value_input=false;}
if(correct_value_input&&(substr_count(tel_value,"+")>1))
{correct_value_input=false;}
if(correct_value_input&&(substr_count(tel_value,"(")>1))
{correct_value_input=false;}
if(correct_value_input&&(substr_count(tel_value,")")>1))
{correct_value_input=false;}
tel_value=tel_value.replace(/[\;\#\n\r\*\'\"<>&\%\!\{\}\[\]\?\\/]/g,"");$(this).val(tel_value);var patt=/^(\+?)[\(\)0-9\-\+ ]+$/g;if(tel_value.match(patt)==null&&correct_value_input)
{correct_value_input=false;}
if(!correct_value_input)
{$(this).addClass('error');$(this).siblings('em').remove();$(this).parent().append('<em>Значение поля не соответствует формату</em>');valid=false;}
else
{$(this).removeClass('error');$(this).siblings('em').remove();}}
else
{if(!$(this).hasClass('required'))
{$(this).removeClass('error');$(this).siblings('em').remove();}}});$(fname+" input.url").each(function(){var patt=/(http:\/\/)?([a-z_0-9-.]+\.[a-z]{2,3}(([ \"'>\r\n\t])|(\/([^ \"'>\r\n\t]*)?)))/g;url_value=trim($(this).val());correct_value_input=true;if(url_value)
{if(url_value.match(patt)==null&&correct_value_input)
{correct_value_input=false;}
if(!correct_value_input)
{$(this).addClass('error');$(this).siblings('em').remove();$(this).parent().append('<em>Значение поля не соответствует формату</em>');valid=false;}
else
{$(this).removeClass('error');$(this).siblings('em').remove();}}
else
{if(!$(this).hasClass('required'))
{$(this).removeClass('error');$(this).siblings('em').remove();}}});return valid;}
function trim(str)
{if(str){return str.replace(/(^\s+)|(\s+$)/g,"");}else{return false;}}
function substr_count(haystack,needle,offset,length){var pos=0,cnt=0;if(isNaN(offset))offset=0;if(isNaN(length))length=0;offset--;while((offset=haystack.indexOf(needle,offset+1))!=-1){if(length>0&&(offset+needle.length)>length){return false;}else{cnt++;}}
return cnt;}
