//jQuery.noConflict();

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) 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 = jQuery('#captcha_md5').get(0);
	if ((o != null) && (c != null)) {
		//alert(hex_md5(o.value)+' '+o.value+' '+c.value)
        if (hex_md5(o.value) != c.value) {
	        //SmartyAjax.update('img_code', '/index.php', 'get', 'tpl=captcha&f=read_tpl');
		    return false;
        }else return true;
    }
	return true;
}



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 error="";
	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){
				jQuery(inp).attr("class","fe");//восстанавливаем цвета полей
				if(stype=="rtxt"){
					if(!checkEmptyText(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value){
						err=1;
						if(error!="") error+="<br />"
						error+=inp.getAttribute("erMsg");
						//jQuery(inp).attr("class","fe2");
						}
				}else if(stype=="txt"){
					if(!checkEmptyText(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value){
						err=1;
						if(error!="") error+="<br />"
						error+=inp.getAttribute("erMsg");
						//jQuery(inp).attr("class","fe2");
						}
				}else if(stype=="captcha"){
					if(!check_captcha(inp)){
						err=1;
						if(error!="") error+="<br />"
						error+=inp.getAttribute("erMsg");
						//jQuery(inp).attr("class","fe2");
						}
				}else if(stype=="phone"){
					if(!checkPhone(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value){
						err=1;
						if(error!="") error+="<br />"
						error+=inp.getAttribute("erMsg");
						//jQuery(inp).attr("class","fe2");
						}
				}else if(stype=="email"){
					if(!echeck(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value){
						err=1;
						if(error!="") error+="<br />"
						error+=inp.getAttribute("erMsg");
						//jQuery(inp).attr("class","fe2");
						}
					}
				}
			}
		}

		if(err){
			show_error(error);
			 if(erMsgContainer) erMsgContainer.style.display='block';
			 return false;
		}

		//oForm.submit();
		var options = {
			url: '/ajax.php',
			type: 'post',
			dataType: 'json',
			success: function(data){
				if(data['sended']){
				    show_notice('Ваше письмо отправленно');
				}else{
					show_error('Ваше письмо не отправленно, ошибка передачи');
				}
			}
		};
		//alert(oForm.id);
		jQuery('#'+oForm.id).ajaxSubmit(options);
}

function checkCallmeForm(oForm)
{
	if(oForm==undefined || oForm.name==undefined) return false;
	formID=oForm.id;

	var err=0;
	var inp=null;
	var stype=null;
	var error="";
	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)
			{
				if(stype=="rtxt")
				{
					if(!checkRusText(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value)
					{
						err=1;
						if(error!="") error+="<br />";
						error+=inp.getAttribute("erMsg");
					}
				}
				else if(stype=="phone")
				{
					if(!checkPhone(inp.value) || inp.getAttribute("erMsg")==inp.value || inp.getAttribute("defvalue")==inp.value)
					{
						err=1;
						if(error!="") error+="<br />";
						error+=inp.getAttribute("erMsg");
					}
				}
			} //if(stype)
		} //if(inp!=undefined)
	} // for

	if(err)
	{
		show_error(error);
		 if(erMsgContainer) erMsgContainer.style.display='block';
		 return false;
	}

	//oForm.submit();
	var options = {
		url: '/ajax.php',
		type: 'post',
		dataType: 'json',
		success: function(data)
		{
			if(data['sended'])
			{
			    show_notice('Ваше сообщение отправленно');
				jQuery('#callme').toggle(400);
			}
			else
			{
				show_error('Ваше сообщение не отправленно, ошибка передачи');
			}
		} // success
	};
	jQuery(oForm).ajaxSubmit(options);
}

jQuery(document).ready(function(){
	jQuery(".form_table input").focus(function(){
		jQuery(this).attr("class","fe");
		if(jQuery(this).attr("erMsg")!=undefined && jQuery(this).attr("erMsg")==jQuery(this).val()) jQuery(this).val("");
	});
	jQuery(".form_table textarea").focus(function(){
		jQuery(this).attr("class","fe");
		if(jQuery(this).attr("erMsg")!=undefined && jQuery(this).attr("erMsg")==jQuery(this).val()) jQuery(this).val("");
	});
});




