var fbuff = {'html':null};
function msg_click(div_id){
	
	msg_div=document.getElementById("post"+div_id);	
	if(msg_div.className=="msg_hid"){		
		msg_div.className="msg_show";	
	}else{		
		msg_div.className="msg_hid";
		nmCancel(div_id);
	}
}

function nmSubmit(id)
{
	var f = document.getElementById('nmForm'+id);
	JsHttpRequest.query('js_new_msg.php',f,endSubmit,true);
}

function edSubmit(id)
{
	var f = document.getElementById('edForm'+id);
	JsHttpRequest.query('js_edit_msg.php',f,endEdit,true);
}

function rmMsg(id)
{
	if (confirm('You are about to delete this message. Continue?'))
		JsHttpRequest.query('js_delete_msg.php',{'msgID':id},endSubmit,true);
}

function endSubmit(res,err)
{
	if (!res || res.status == undefined || parseInt(res.status))
		return alert(err);
	self.location.reload();
}

function endEdit(res,err)
{
	if (!res || res.status == undefined || parseInt(res.status))
		return alert(err);
	self.location.href = res.back_link;
}

function nmOpen(id)
{
	var base = document.getElementById('new_form'), trg = document.getElementById('new_'+id), bln = document.getElementById('dtl_'+id), html, tmp;
	if (base && trg){
		html = parseInt(id) ? base.innerHTML.replace(new RegExp('###','g'),id) : base.innerHTML.replace(new RegExp('###','g'),id).split('<B>תגובה להודעה</B>').join('');
		
		trg.innerHTML = '<form method="POST" id="nmForm'+id+'" style="margin:0px" enctype="multipart/form-data">'+html+'</form>';
		if (bln)
			bln.className = 'msg_hid';
		trg.className = 'msg_new';
		parseInt(id) ? null : trg.style.marginTop='15px';
	}
}

function nmCancel(id)
{
	trg = document.getElementById('new_'+id), bln = document.getElementById('dtl_'+id);
	trg.className = 'msg_hid';
	trg.innerHTML = '';
	if (bln)
		bln.className = 'msg_dtl_line';
}

function fLogin(prm)
{
	if(prm==3){
		var p = document.getElementById('flogFormMain');
	} else {
		var p = prm ? (prm == 1 ? document.getElementById('flogForm') : {'act':'logout'}) : {'act':'init'};
	}
	JsHttpRequest.query('/js_login.php',p,endLogin,true);
}

function endLogin(res, err)
{
	if(!res || res.status == undefined || parseInt(res.status)){
		alert(err);
		return res.link ? location.href = res.link : null;
	}
	
	var d = document.getElementById('sbDiv');
	
	if (d)
		fbuff.html = d.innerHTML;
	res.loginOK ? location.reload() : d.innerHTML = res.html;
}

function cancelLogin()
{
	document.getElementById('sbDiv').innerHTML = fbuff.html;
	fbuff.html = null;
}

function addSmile(id,emo)
{
	var ma = document.getElementById('tar'+id);
	
	ma.focus();
	if (document.selection)
		ma.caretPos = document.selection.createRange().duplicate();
	
	var txt = document.all ? ((ma.createTextRange && ma.caretPos) ? ma.caretPos.text : '') : ma.value.substr(ma.selectionStart, ma.selectionEnd - ma.selectionStart), _scroll = ma.scrollTop;
	
	txt = txt + ' ' + emo + ' ';
	
    if (ma.createTextRange && ma.caretPos) {
        var cp = ma.caretPos;
        cp.text = txt;
    } else {
        var sel1 = ma.value.substr(0,ma.selectionStart);
        var sel2 = ma.value.substr(ma.selectionEnd,ma.value.length - ma.selectionEnd);
        ma.value = sel1 + txt + sel2;
    }
    ma.focus();
    ma.scrollTop = _scroll;
}

function addPhoto(id)
{
	var tbl = document.getElementById('ftb'+id), c = tbl.rows.length-2, row = tbl.insertRow(c), td = document.createElement("TD");
	
	td.className = 'newMsg_txt';
	td.innerHTML = 'פוטו ' + (c - 1);
	row.appendChild(td);
	
	td = document.createElement("TD");
	td.innerHTML = '<input type="file" name="photo[]" class="newMsg_input">';
	row.appendChild(td);
}

function setElementOpacity(sElemId, nOpacity)
{
  var opacityProp = getOpacityProperty();
  var elem = document.getElementById(sElemId);

  if (!elem || !opacityProp)
  	return;
  
  if (opacityProp=="filter")
  {
    nOpacity *= 100;
    var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
    (oAlpha) ? oAlpha.opacity = nOpacity : elem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";
  }
  else
    elem.style[opacityProp] = nOpacity;
}

function getOpacityProperty()
{
  if (typeof document.body.style.opacity == 'string') // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9)
    return 'opacity';
  else if (typeof document.body.style.MozOpacity == 'string') // Mozilla 1.6 и младше, Firefox 0.8 
    return 'MozOpacity';
  else if (typeof document.body.style.KhtmlOpacity == 'string') // Konqueror 3.1, Safari 1.1
    return 'KhtmlOpacity';
  else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) // Internet Exploder 5.5+
    return 'filter';

  return false;
}

