// userWrap

formFarm=function(action,opt,he){
	var form=new Array();
	
	form["html"]="<form name='comment' action='"+action+"' method='POST' id='xncform"+opt["form_id"]+"' onSubmit=\"var check_len=check_field_val('xform_body"+opt["form_id"]+"'); check_len2=check_field_val('xform_email"+opt["form_id"]+"'); var check_len1=check_field_val('xform_name"+opt["form_id"]+"'); if(!check_len || !check_len2 || !check_len1)return false;\">";
	form["html"]+="<div class='t_grey'>Имя:</div>";
	form["html"]+="<input style='border:1px solid #b3b3b3; width:151px; padding:2px 0px 2px 5px;' id='xform_name"+opt["form_id"]+"' name='xform[userlogin]'><br>";
	form["html"]+="<div class='t_grey'>Email:</div>";
	form["html"]+="<input style='border:1px solid #b3b3b3; width:151px; padding:2px 0px 2px 5px;' id='xform_email"+opt["form_id"]+"' name='xform[email]'><br>";
	form["html"]+="<div class='t_grey'>Ваш коментарий:</div>";
	form["html"]+="<textarea style='border:1px solid #b3b3b3; width:70%; padding:2px 0px 2px 5px;' id='xform_body"+opt["form_id"]+"' name='xform[body]' style='width:80%;height:50px;'></textarea><br>";
	form["html"]+="<input type='submit' value='добавить'><br><br>";
	//form["html"]+="<input type='hidden' name='magic' id='pcm' value='failed'>";
		
	for(var h in he){
		form["html"]+="<input type='hidden' name='"+h+"' value='"+he[h]+"'>";
	}
	form["html"]+="<input type=hidden name='formID"+opt["form_id"]+"' value='comment'>";
	form["html"]+="<input type=hidden name='magics"+opt["form_id"]+"' id='xMagic"+opt["form_id"]+"' value='3c35034703f9a820d6002fb3877a0679'> ";
	form["html"]+="<input type=hidden name='magic' value='magic' id='cfmagic"+opt["form_id"]+"'>";

	
	form["html"]+="</form><script src='/fixmagic/'></script><script>if(window.fixMagic)fixMagic('xMagic"+opt["form_id"]+"');</script>	";
	return form["html"];
}


chkEmailField=function(field)
{
		var elm=document.getElementById(field);
    if (elm.value.indexOf("@") + "" != "-1" &&
        elm.value.indexOf(".") + "" != "-1" &&
        elm.value != "")
   {       	
    null_border(field);
    return true;
    }else
		{
			focus_border(field,'red');
			elm.focus();
			return false;
		}	
}


var openedForm=0;

magicForm=function(id){
	var formDiv=document.getElementById('ncForm'+id);
	if(window.openedForm && formDiv.style.display=='none'){
		magicForm(window.openedForm);
	}
	formDiv.style.display=( formDiv.style.display=='block' ? 'none' : 'block');
	if(formDiv.style.display=='none'){
		document.getElementById('xncform'+id).reset();
		window.openedForm=0;
	}
	else{
		window.openedForm=id;
	}

}

replyto=function(id,he,opt,lnk){
	var formDiv=document.getElementById('ncForm'+id);
	
	if(formDiv && formDiv.innerHTML){
		try{

		if(formDiv.innerHTML=="..."){
			
			formDiv.innerHTML=formFarm("/addcomment/"+lnk+"/",opt,he);
		}
		magicForm(id);
		}catch(e){
			alert('error'+e);
		}
	}
	else
	{
		alert('hmm..{ncForm'+id+'"}');
	}
}

function check_field_val(fname){
	
	var email_re=/xform_email([0-9_]+)/g;
	var val=document.getElementById(fname).value;
	var result_reg = email_re.test(fname);
	
	if(result_reg && val.length > 0)
	{
		return chkEmailField(fname);
	}else{
		if(result_reg==false && (val.length <= 0 || val==null)){
	 		focus_border(fname,'red');
	 		return false;
	 	}else{
	 		null_border(fname);
	 		return true;
	 	}
	}
} 

	function focus_border(field){
		document.getElementById(field).style.borderWidth='2px';
		document.getElementById(field).style.borderStyle='solid';
		document.getElementById(field).style.borderColor='red';
	}

	function null_border(field){
		document.getElementById(field).style.borderWidth='1px';
		document.getElementById(field).style.borderColor='grey';
	}
