function checkRusText(str) {
	if(str=='') return false;
	if(str.replace(/[^йцукенгшщзхъфывапролджэячсмитьбюё]/gi,'').length>0) return true;
	return false;
}
function checkEmptyText(str) {
	if(str=='') return false;
	if(str.replace(/[\s]/g,'').length>0) return true;
	return false;
}

function checkNum(num,len){
	if(num=='') return false;
	if(num.replace(/[0-9]/g,'').length>0) return false;
    if(num.length!=len && len!=undefined) return false;
 return true;
}
function checkPhone(num){
	if(num=='') return false;
	if(num.replace(/[0-9+\-()\s]/g,'').length>0) return false;
 return true;
}
function echeck(str) {
	if(str=='') return false;
	if(str.replace(/([a-z_\-\.\d]+)+@+([a-z\-\d]+)+\.+([a-z]{2,4})/gi,'').length>0) return false;
	return true;
}
function check_captcha(o){
        var c = $('#captcha_md5').get(0);
		if ((o != null) && (c != null)) {
        if (hex_md5(o.value) != c.value) {
	        SmartyAjax.update('div_c', '/index.php', 'get', 'tpl=captcha&f=read_tpl');
	    return false;
        }else return true;
       } 
return false;
}



function Combine(inputs,textareas){
var ary=[];
for (var zxc0=0;zxc0<inputs.length;zxc0++){
ary.push(inputs[zxc0]);
}
for (var zxc1=0;zxc1<textareas.length;zxc1++){
ary.push(textareas[zxc1]);
}
return ary;
}


function checkForm(oForm)
{
if(oForm==undefined || oForm.name==undefined) return false;
formID=oForm.id;
var err=0;
var inp=null;
var stype=null;
var erMsgContainer=null;
erMsgContainer=document.getElementById('erMsgContainer');
if(erMsgContainer) erMsgContainer.style.display='none';
textAreas=jQuery("form#"+formID+" textarea").get();
inputs=jQuery("form#"+formID+" input").get();
inputs=Combine(inputs,textAreas);
for(i=0;i<=inputs.length;i++){
	inp=inputs[i];
		if(inp!=undefined)
		{
		stype=inp.getAttribute("stype");
		if(stype){
			$(inp).attr("class","fe");//восстанавливаем цвета полей
			if(stype=="rtxt"){
				if(!checkRusText(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value){
					err=1;
					inp.value=inp.getAttribute("erMsg");
					$(inp).attr("class","fe2");
					}
			}else if(stype=="txt"){
				if(!checkEmptyText(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value){
					err=1;
					inp.value=inp.getAttribute("erMsg");
					$(inp).attr("class","fe2");
					}
			}else if(stype=="num"){
				if(!checkNum(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value){
					err=1;
					inp.value=inp.getAttribute("erMsg");
					$(inp).attr("class","fe2");
					}
			}else if(stype=="captcha"){
				if(!check_captcha(inp)){
					err=1;
					inp.value=inp.getAttribute("erMsg");
					$(inp).attr("class","fe2");
					}
			}else if(stype=="phone"){
				if(!checkPhone(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value){
					err=1;
					inp.value=inp.getAttribute("erMsg");
					$(inp).attr("class","fe2");
					}
			}else if(stype=="email"){
				if(!echeck(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value){
					err=1;
					inp.value=inp.getAttribute("erMsg");
					$(inp).attr("class","fe2");
					}
				}
			}
		}
	}

	if(err){
		 if(erMsgContainer) erMsgContainer.style.display='block';
		 return false;
	}
	oForm.submit();
}

$(document).ready(function(){
	$(".form_table input, .form_table textarea").focus(function(){
		$(this).attr("class","fe");
		if($(this).attr("erMsg")!=undefined && $(this).attr("erMsg")==$(this).val()) $(this).val("");
	});
	$("td.captcha a").click(function(){
		SmartyAjax.update('div_c', '/index.php', 'get', 'tpl=captcha&f=read_tpl');
	    return false;
	});
});
