var config = new Array(), ready = true; jQuery(document).ready(function(){ jQuery("form").live("submit", function() { fsubmit(jQuery(this).attr('name')); return false; }); }); function fsubmit(key) { jQuery("form[name='"+key+"'] *").removeClass("error"); jQuery(".error-desc").text(""); jQuery(".alert").hide(); jQuery(".good-alert").show(); eval('validate_showErrorMethod = validate_'+key+'_showErrorMethod;'); if (validate_showErrorMethod.indexOf("#")!= -1) { jQuery(validate_showErrorMethod+" > *").remove("*"); } var res = new Array(0); res = checkForm(key); for (element in res) { if (element.length > 0) { var tttt = 11; displayErrors(res, key); break; } } if (tttt != 11) { eval('validate_sendMethod = validate_'+key+'_sendMethod;'); if (validate_sendMethod == 'ajax') { post(key); } else { jQuery("form[name='"+key+"']").submit(); } } } function post(fName) { if (ready) { var b = jQuery("form[name='"+fName+"'] input, form[name='"+fName+"'] textarea, form[name='"+fName+"'] select"); var newstr = ""; b.each(function() { if (jQuery(this).attr("type") == "radio") { if (jQuery(this).attr("checked") == "checked") { radioname = jQuery(this).attr("name"); radioval = jQuery(this).val(); newstr += radioname + '=' + radioval + '&'; } } else if (jQuery(this).attr("type") == "checkbox") { if (jQuery(this).attr("checked") == "checked") { radioname = jQuery(this).attr("name"); radioval = jQuery(this).val(); newstr += radioname + '=' + radioval + '&'; } } else { if (jQuery(this).val() == "") { jQuery(this).val(""); } newstr += this.name + '=' + jQuery(this).val() + '&'; } }); newstr += 'ajax=true'; newstr += '&formName='+fName; jQuery.ajax({ type: "POST", url: jQuery("form[id='"+fName+"']").attr('action'), data: newstr, success: function(msg){ if (msg.match('@s@')) { eval('validate_preloaderId = validate_'+fName+'_preloaderId;'); jQuery(validate_preloaderId).hide(); var text1 = msg.replace(/@s@/g,''); eval('validate_showErrorMethod = validate_'+fName+'_showErrorMethod;'); eval('validate_lastaction = validate_'+fName+'_lastaction;'); eval('var validate_callback = validate_'+fName+'_callback;'); eval('var validate_param = validate_'+fName+'_param;'); if (validate_lastaction == 'callback') { if (typeof(window[validate_callback]) == "function") { window[validate_callback](validate_param); } } if (validate_showErrorMethod.match("#")) { jQuery(validate_showErrorMethod).html(text1); } if (validate_showErrorMethod == "alert") { if(validate_lastaction =='hide' || validate_lastaction == 'hideSend') { alert(text1); } } if (validate_lastaction == 'hide') { jQuery("form[id='"+fName+"']").css('display','none'); } if (validate_lastaction == 'hideSend') { eval ('validate_send = validate_'+fName+'_sendId;'); jQuery(validate_send).hide(); } if (validate_lastaction.indexOf("sp") > - 1) { window.location.href = '/'+validate_lastaction+'/'; } ready = false; setTimeout('readyFalse()', 1000); } else { eval('validate_capId = validate_'+fName+'_capId;'); var src = jQuery("captcha", msg).text(); if (src.length == 0) { src = msg.substr(14, 32); } src = "/libs/imgcode/"+src+".jpg"; jQuery(validate_capId).attr("src", src); text1 = jQuery("errors", msg).text(); if (text1.length == 0) { text1 = msg.substr(msg.indexOf("")+8, msg.indexOf("")-msg.indexOf("")-8); } eval('validate_preloaderId = validate_'+fName+'_preloaderId;'); jQuery(validate_preloaderId).hide(); eval('validate_showErrorMethod = validate_'+fName+'_showErrorMethod;'); if (validate_showErrorMethod.indexOf("#")!= -1) { text1 = "

"+text1+"

"; jQuery(text1).appendTo(validate_showErrorMethod); } else { alert(text1); } } } }); eval('validate_preloaderId = validate_'+fName+'_preloaderId;'); jQuery(validate_preloaderId).show(); } else { eval('validate_showErrorMethod = validate_'+fName+'_showErrorMethod;'); if (validate_showErrorMethod.indexOf("#")!= -1) { jQuery("

Слишком частое отправление запроса, подождите, пожалуйста

").appendTo(validate_showErrorMethod); } else { alert("Слишком частое отправление запроса, подождите, пожалуйста"); } } } function readyFalse() { ready = true; } function displayErrors(array, formName) { eval('validate_highlight = validate_'+formName+'_highlight;'); eval('validate_showErrorMethod = validate_'+formName+'_showErrorMethod;'); if (validate_highlight) { highlight(array, formName); } if (validate_showErrorMethod == "alert") { alertError(array); } if (validate_showErrorMethod.indexOf("#")!= -1) { showErrorInDiv(array, validate_showErrorMethod); } } function highlight(array, formName) { for (field in array) { for (error in array[field]) { var errorCaption = jQuery("#"+field+"-error"); if (errorCaption.text() == "") { var newError = getError(error, array[field]['caption'], error); errorCaption.text(newError); } //jQuery("form[name='"+formName+"'] *[name='"+field+"']").css('background','#e97451'); jQuery("form[name='"+formName+"'] *[name='"+field+"']").addClass("error"); jQuery("#validate-bad_"+field).show(); jQuery("#validate-good_"+field).hide(); } } } function alertError(array) { var string = ""; out: for (field in array) { for (error in array[field]) { var newError = getError(error, array[field]['caption'], array[field][error]); string += newError+"\n"; continue out; } } alert(string); } function showErrorInDiv(array, id) { var string = ""; out: for (field in array) { for (error in array[field]) { var newError = getError(error, array[field]['caption'], array[field][error]); string += "

"+newError+"

"; continue out; } } $(id).append(string); } function getError($errorType, $fieldName, $value) { var $string; switch ($errorType) { case "maxlength": $string = "У поля `" + $fieldName + "` превышена максимальная длина - " + $value; break; case "minlength": $string = "Поле `" + $fieldName + "` - недостаточно символов"; break; case "email": $string = "Поле `" + $fieldName + "` заполнено неверно"; break; case "noempty": $string = "Поле `" + $fieldName + "` обязательно для заполнения"; break; case "nonumber": $string = "Поле `" + $fieldName + "` не должно содержать цифры"; break; case "numberonly": $string = "Поле `" + $fieldName + "` должно содержать только цифры"; break; case "captcha": $string = "В поле `" + $fieldName + "` введен неверный код"; break; case "login": $string = "Адрес E-mail в поле `" + $fieldName + "` уже зарегистрирован"; break; case "sameas": $string = "Пароли не совпадают"; break; default: $string = "Ошибка" }; return $string; } //Проверка формы //formName - имя формы function checkForm(formName) { var res; var error = new Array(0); var newArr = config[formName]; for(elem in newArr) { res = checkField(elem, newArr[elem], formName); for (what in res) { if (what.length > 0) { error[elem] = new Array(); error[elem] = res; error[elem]['caption'] = newArr[elem]['caption']; break; } else { delete error[elem]; break; } } } return error; } //Проверяет Field //field - имя поля //value - массив с ограничителями и их значения //formName - имя формы function checkField(field, value, formName) { var error2 = new Array(0); var res; var newArr = value; for (var handler in newArr) { if (handler != 'caption') { functionName = "check_"+handler; res = eval(functionName+"('"+field+"','"+ newArr[handler]+"','"+ formName+"')"); if (!res) { error2[handler] = newArr[handler]; } else { delete error2[handler]; } } } return error2; } /*Обработчики*/ function check_maxlength(field, value, formName) { try{ var string = jQuery("#"+field+"").val(); }catch(e){ var string = jQuery("#"+field+"").html(); } if (string.length > parseInt(value)) { return false; } return true; } function check_captcha(field, value, formName) { return true; } /*Min-lenght validation*/ function check_minlength(field, value, formName) { try{ var string = jQuery("#"+field+"").val(); }catch(e){ var string = jQuery("#"+field+"").html(); } if (string.length < parseInt(value)) { return false; } return true; } /*Only numbers validation*/ function check_numberonly(field, value, formName) { try{ var string = jQuery("#"+field+"").val(); }catch(e){ var string = jQuery("#"+field+"").html(); } var reg = new RegExp("[^0-9]+", 'i'); var result = reg.test(string); if (result) { return false; } return true; } /*No numbers validation*/ function check_nonumber(field, value, formName) { try{ var string = jQuery("#"+field+"").val(); }catch(e){ var string = jQuery("#"+field+"").html(); } var reg = new RegExp("[0-9]", 'i'); var result = reg.test(string); if (result) { return false; } return true; } /*Only numbers and symbols validation*/ function check_numberandsimbols(field, value, formName) { try{ var string = jQuery("#"+field+"").val(); }catch(e){ var string = jQuery("#"+field+"").html(); } var reg = new RegExp("[^0-9-_+@\s]", 'i'); var result = reg.test(string); if (!result) { return false; } return true; } /*E-mail validation*/ function check_email(field, value, formName) { try{ var string = jQuery("#"+field+"").val(); }catch(e){ var string = jQuery("#"+field+"").html(); } var reg = new RegExp("[0-9a-z_]+@[0-9a-z_^.]+\\.[a-z]{2,3}", 'i'); var result = reg.test(string); if (!result) { return false; } return true; } /*No empty validation*/ function check_noempty(field, value, formName) { try{ var string = jQuery("#"+field+"").val(); }catch(e){ var string = jQuery("#"+field+"").html(); } if (string.length < 1) { return false; } if (string === value && value > 1) { return false; } return true; } /*checkbox validation*/ function check_checkbox(field, value, formName) { var string = jQuery("#"+field+"").attr('checked'); if (!string) { return false; } return true; } /*sameAs (password reply) validation*/ function check_sameas(field, value, formName) { try{ var string = jQuery("#"+field+"").val(); }catch(e){ var string = jQuery("#"+field+"").html(); } try { var string2 = jQuery("#"+value+"").val(); }catch(e) { var string2 = jQuery("#"+value+"").html(); } if (string != string2) { return false; } return true; }