window.url_id = 1;
function CommentsCore() {
	this.currentPage = Object();
	this.ident ='';
	//this.formHTML = 'abcd';
	this.currentReplyForm = 0;
	
	this.openReplyForm  = function(ident,_commentid,order) {

		if(this.currentReplyForm!=0) $a('replyform_'+this.currentReplyForm).innerHTML = '<div align="right"><a align="right" style="cursor:pointer;" onclick="Comments.openReplyForm(\''+ident+'\',\''+this.currentReplyForm+'\',\''+order+'\')" align="right">(ответить)</a></div>';
		
		
		tmp = '<form method="post" action="/orders/addcomment" enctype="multipart/form-data" name="form_'+_commentid+'" id="form_'+_commentid+'" onSubmit="return checkComment(\'form_'+_commentid+'\')"><table width="100%"><tr><td>Сумма: <input type="text" name="summ_" size="6" value=""/><br/><font size="1px">Укажите предлагаемую сумму.</font></td></tr><tr><td>Срок: <input type="text" name="period_" size="6" value=""/><br/><font size="1px">Укажите предполагаемый срок в днях.</font></td></tr><tr><td><font size="1">Напишите комментарий:<br/></font><textarea id="start_content_" name="comment_content" style="border:1px solid #3579A8; height:80px; width:100%"></textarea><br/><input type="checkbox" value="1" name="hide"> <font size="1">Скрыть от посторонних (будет виден только заказчику)</font><input type="hidden" name="orderid" value="'+order+'"><input type="hidden" name="commentid" value="'+_commentid+'"><td></tr><tr><td><table width="100%" border="0" cellspacing="5" cellpadding="0"><tr><td><b>Добавление файлов</b></td></tr><tr><td><div id="form_'+_commentid+'_files"><div id="file_1"><input type="file" name="file_1" onchange="file_add(\'form_'+_commentid+'\');"></div><span onclick="file_del(\"form_'+_commentid+'\",\'1\')"></div></td></tr></table></td></tr><tr><td align="center"><input type="submit" name="send" value="Отправить"></td></tr></table></form>';
		
$a('replyform_'+_commentid).innerHTML = tmp;
		if(this.currentReplyForm == 0){
		if($a('main_form')!=null)
		$a('main_form').innerHTML = '<a style="cursor:pointer;" onclick="Comments.openMainReplyForm(\''+ident+'\',\''+order+'\');">форма основного ответа</a>';
		}
		this.currentReplyForm = _commentid;
	
	}
	
	this.openMainReplyForm = function(ident,order){
		if(this.currentReplyForm!=0){ $a('replyform_'+this.currentReplyForm).innerHTML = '<div align="right"><a style="cursor:pointer;" onclick="Comments.openReplyForm(\''+ident+'\',\''+this.currentReplyForm+'\',\'+order+\')" align="right">(ответить)</a></div>';
		html = '<form method="post"  action="/orders/addcomment" enctype="multipart/form-data" name="main_form" id="main_form" onSubmit="return checkComment(\'main_form\')"><table width="100%"><tr><td>Сумма: <input type="text" name="summ_" size="6" value=""/><br/><font size="1px">Укажите предлагаемую сумму.</font></td></tr><tr><td>Срок: <input type="text" name="period_" size="6" value=""/><br/><font size="1px">Укажите предполагаемый срок в днях.</font></td></tr><tr><td><font size="1">Напишите комментарий:<br/></font><textarea id="start_content_" name="comment_content" style="border:1px solid #3579A8; height:80px; width:100%"></textarea><br/><input type="checkbox" value="1" name="hide"> <font size="1">Скрыть от посторонних (будет виден только заказчику)</font><input type="hidden" name="orderid" value="'+order+'"></td></tr><tr><td><table width="100%" border="0" cellspacing="5" cellpadding="0"><tr><td><b>Добавление файлов</b></td></tr><tr><td><div id="main_form_files"><div id="file_1"><input type="file" name="file_1" onchange="file_add(\'main_form\');"></div><span onclick="file_del(\'main_form\',\'1\')"></div></td></tr></table></td></tr><tr><td align="center"><input type="submit" name="send" value="Отправить"></td></tr></table></form>';
		

		$a('main_form_div').innerHTML = html;
		this.currentReplyForm = 0;
		window.file_id = 1;		
		}
	}
	
	this.Delete = function(ident,commentid,type) {
		this.ident = ident;
		var data = new Object();
		data['commentid'] = commentid;
		data['type'] = type;
		data['action'] = 'delete';
		data['widget'] = 'comments';

		this.InitializeRequest('POST', '/ajax.php');
        this.Commit(postquery(data));
		
	}
	
	this.DeleteInternal = function(ident,messid){
		this.ident = ident;
		
		var data = new Object();
		data['messid']=messid;
		data['action']='deleteinternal';
		data['widget']='comments';
		this.InitializeRequest('POST', '/ajax.php');
        this.Commit(postquery(data));
	}
	
	this.Post = function(ident,namefull,nickname,email,content,captcha,parentid) {	
		this.ident = ident;
	    setFade(0,"form_"+ident);
		$a("loading_"+this.ident).style.display = 'block';
		var data = new Object();	
		data['content'] = escape(content);
		data['nickname'] = escape(nickname);
		data['email'] = escape(email);
		data['captcha'] = escape(captcha);
		data['ident'] = ident;
		data['parentid'] = parentid;
		data['namefull'] = namefull;
		data['action'] = 'post';
		data['widget'] = 'comments';

		this.InitializeRequest('POST', '/ajax.php');
        this.Commit(postquery(data));
	}
	
	this.OnSuccess = function() {
      //	alert(this.GetResponseText());
      	eval(this.GetResponseText());
      	//fade(0,"form_"+this.ident);
      	$a("loading_"+this.ident).style.display = 'none';
    }
	
}
CommentsCore.prototype = new ajax();
Comments = new CommentsCore();

function file_del(name_form,id) {

	$a(name_form+'_files').removeChild($a(name_form+'_file_'+id));
}
function url_del(name_form,id) {

	$a(name_form+'_urls').removeChild($a(name_form+'_url_'+id));
}

function file_add(name_form,type) {
	
	 var newup =document.createElement('div');
	window.file_id += 1;
	id = window.file_id;
	newup.id = name_form+'_file_'+id;
	newup.innerHTML = '<input type="file" id="file_'+id+'" name="file_'+id+'" onchange="file_add(\''+name_form+'\');">&nbsp;&nbsp;';	
	newup.innerHTML+='<span onclick="file_del(\''+name_form+'\',\''+id+'\')"><img src="/admin/img/icons/no.png"></span>';
	$a(name_form+'_files').appendChild(newup);
}

function url_add(name_form,id) {
	
	 var newup =document.createElement('div');
	 if($a(name_form+'_url_'+(id+1))==null){
	window.url_id += 1;
	id = window.url_id;
	newup.id = name_form+'_url_'+id;
	newup.innerHTML+='URL: <input type="text" name="URL_'+id+'" width="100" title="Введите URL на который будет ссылаться файл (только если это рисунок)" onkeydown="url_add(\''+name_form+'\','+id+');">';	
	newup.innerHTML+='<span onclick="url_del(\''+name_form+'\',\''+id+'\')"><img src="/admin/img/icons/no.png"></span>';
	$a(name_form+'_urls').appendChild(newup);
	}
}

function checkComment(formname){
	var form = $a(formname);
	if(form == null){alert('Невозможно найти форму!');return false;}
	if(form.summ_.value.length>0&&/\D/i.test(form.summ_.value)){alert('Сумма должна быть числом!');return false;}
	if(form.period_.value.length>0&&/\D/i.test(form.period_.value)){alert('Срок должен быть числом!');return false;}
	if(form.comment_content.value.length<3||form.comment_content.value.length>500){
		alert('Комментарий должен быть не менее 3 и не более 500 символов!');return false;
	}
	return true;
	
}

function checkCommentContest(formname){
	var form = $a(formname);
	if(form == null){alert('Невозможно найти форму!');return false;}
	if(form.summ_.value.length>0&&/\D/i.test(form.summ_.value)){alert('Сумма должна быть числом!');return false;}
	if(form.period_.value.length>0&&/\D/i.test(form.period_.value)){alert('Срок должен быть числом!');return false;}
	if(form.comment_content.value.length>0&&form.comment_content.value.length>500){
		alert('Комментарий должен быть не более 500 символов!');return false;
	}
        if(form.file_1.value.length<3){alert('Добавьте файл');return false;}
	return true;
	
}

function showInternalDialogue(commentid){
	var element = document.getElementById('internal_'+commentid);
	var button = document.getElementById('internal_button_'+commentid);
	if(element!=null){
		if(element.style.display=='none'){element.style.display='block';button.value='Скрыть';button.title='Скрыть историю диалога';return true;}
		else {element.style.display='none';button.value='Показать';button.title='Показать историю диалога';return true;}
	}else return false;
}






