var userAccountType = "";

function HX_InstallEICons(){
	var alltables = document.getElementsByTagName("TABLE");
	
	for (var i=0;i<alltables.length;i++){
		
		var tbl = alltables[i];
		var tblid = tbl.id;
		
		if (tblid.length > 0){
			if (tblid.indexOf("comm_tbl_")==0){
				var mycell = tbl.rows.item(0).cells.item(1);
				mycell.innerHTML = HX_putEmoticons(mycell.innerHTML);
			}
			if( '-' == 'admin' || 'roxanatoma' == 'unknown' ){
				var _newrow = tbl.insertRow(2);
				var _cell1 = _newrow.insertCell(0);
				var _cell2 = _newrow.insertCell(1);
				_cell2.innerHTML = '&nbsp;';
				var cid = tblid.substring(9);
				var _buttons = '<div style="width:300px;height:20px;padding:5px;"><a href="javascript:HX_banIP(\''+cid+'\')" class="mac" onclick="this.blur()"><span>Blochează</span><a href="javascript:HX_deleteComm(\''+cid+'\')" class="mac_on" onclick="this.blur()"><span style="color:red">Şterge</span></a></div>';

				_cell2.innerHTML = _buttons;
			}

		}
	}//for
}

function HX_saveComment(blogID,postID,allowUnknown){
	var _mytextarea = document.getElementById("_tmc_textarea");
	if (_mytextarea.value.length==0){
		_mytextarea.focus();
		return;
	}
	var _hxu = document.getElementById("hx_username");
	var myuser = _hxu.value;
	if (myuser.length == 0){
		_hxu.value = "Anonim";
		myuser = "Anonim";
	}
	if ("unknown" == "unknown"){
		//salvez in cookie ce user a pus
		createCookie("comment.username",myuser,100);
	}

	var _parent = document.getElementById("_parent_tmc_textarea");
	var _myval = _mytextarea.value;
	var _myhtml = _myval;
	if (userAccountType.length == 0 || userAccountType == "1"){
		_myhtml = removeAllTags(_myhtml);
	}
	var _comText = _myval;
	_myval = replaceStr(_myval,"\n","<br>");
	_myval = HX_putEmoticons(_myval);
	_parent.innerHTML = _myhtml;
	_parent.style.border = "none";
	
	
	var _ctitle = document.getElementById("_cm_title");
	_ctitle.innerHTML = "<big>"+myuser+"</big> spunea ...";
	var haa = document.getElementById("_ha_comm_a");
	haa.href = "javascript:HX_addComment('"+blogID+"','"+postID+"',"+allowUnknown+")";
	var hat = document.getElementById("_ha_comm_text");
	//haa.className = "hbtn_green";
	hat.innerHTML = "Adaugă&nbsp;comentariu";
	_parent.style.height = 0;
	_parent.style.height = parseInt(_parent.scrollHeight)+20;
	_parent.id = "HX_parent_"+_mytextarea.id;
	_hxu.id = "HX_user_"+_mytextarea.id;
	_ctitle.id = "HX_COMM_T_"+_mytextarea.id;
	var _pdate = document.getElementById("_tmc_pdate");
	_pdate.innerHTML = "Publicat la " +xg.util.getTime();
	_pdate.style.height =20;
	_pdate.style.visibility = "visible";
	_pdate.id = "HX_pdate_"+_mytextarea.id;
	installDblClick();
	//saving
	var params = "bid="+encodeURIComponent(blogID)+"&pid="+encodeURIComponent(postID)+"&user="+encodeURIComponent(removeAllTags(myuser))+"&comment="+
	encodeURIComponent(_comText);
	var _spinner = document.getElementById("_hx_spinner");
	_spinner.style.visibility = "visible";
	
	HX_loadXMLDoc("http://roxanatoma.eu/exp/addcomment/?"+params);
}

function installDblClick(){
	var _divs = document.getElementsByTagName("DIV");
	for (var i=0;i<_divs.length;i++){
		try{var _did = _divs[i].id;
		if (_did && _did.indexOf("HX_")>-1){
			//_divs[i].style.border = "solid red 1px";
			_divs[i].ondblclick = function(){
				HX_EditComment(this);
			}
		}
		}catch(E){}
	}
}

function HX_EditComment(obj){
	/*obj.style.border = "solid orange 1px";
	var txtid = obj.id; txtid = txtid.substring(10);
	var txta = document.getElementById(txtid);
	alert(txtid+":"+txta);*/
}

function HX_addComment(blogID,postID,allowUnknown){

	if ("unknown" == "unknown" && !allowUnknown){
		window.open("https://www.heeloo.net/login/ro/?.done="+window.location+"&token=811B875D707B3C809FF9D475A285F7C2","_self");
		return;
	}
	var haa = document.getElementById("_ha_comm_a");
	haa.href = "javascript:HX_saveComment('"+blogID+"','"+postID+"',"+allowUnknown+")";
	var hat = document.getElementById("_ha_comm_text");
	//haa.className = "hbtn_green";
	hat.innerHTML = "<b>Salvează&nbsp;comentariul</b>";
	hat.tabIndex = 18;
	var _cc = document.getElementById("_comments_container");
	var _cuser = "Anonim";
	var cookieUN = readCookie("comment.username");
	if (cookieUN != null){
		_cuser = cookieUN;
	}

	var _html = '<table  border="0" cellspacing="5" cellpadding="5" width="100%">';
	_html+='<tr>';
		_html+='<td width="1%" align="left" valign="top">';
		if ("unknown" != "unknown"){
		_html+='<img src="http://www.heeloo.net/ui" width="50"/>';
		}else{
			_html+="<img src='http://roxanatoma.eu/ui/anonim/' width='50'/>";
		}
		_html+="</td>";
		_html+="<td width='99%' align='left' valign='top'>";
		if ("unknown" != "unknown"){
		_html+="<div id='_cm_title'><input type='text' id='hx_username' value='<a href=\\\"' style='width:1px;height:1px;visibility:hidden;' /><big></big> spune ...</div>";
		}else{
		_html+="<div id='_cm_title'><div style='float:left;width:150px;height:20px;overflow:hidden;border:solid silver 1px;'>";
		_html+="<input type='text' id='hx_username' class='hx_username' style='font-weight:bolder;position:relative;padding-left:5px;left:-3px;top:-3px;width:160px;height:26px;' value='"+_cuser+"' tabindex='16'/></div><div style='float:left;padding-top:3px;padding-left:3px;'> spune ...</div></div>";
		}
		_html+="<div id='_parent_tmc_textarea' style='margin-top:5px;width:400px;height:120px;border:solid silver 1px;overflow:hidden;float:left'>";
		_html+='<textarea id="_tmc_textarea" TABINDEX="17" style="overflow:hidden;position:relative;left:-2px;top:-2px;width:405px;height:130px;padding:5px;visibility:visible;border:solid red 1px;" name="p$htmlbody"></textarea>';
		_html+="</div>";
		_html+="</td>";
	_html+="</tr>";
	_html +="<tr>";
		_html +="<td>&nbsp;</td>";
		_html +="<td>-, -</td>";
	_html +="</tr>";
	_html+="<tr>";
		_html+="<td><img id='_hx_spinner' src='http://imgs.heeloo.net/spinner_small.gif' border='0' style='visibility:hidden'/></td>";
		_html+="<td>";
			_html+="<div id='_tmc_pdate' style='width:100%;height:0px;visibility:hidden'></div>";
		_html+="</td>";
	_html+="</tr>";
	_html+="</table>";
	var _newDiv = document.createElement("DIV");
	_newDiv.style.position = "relative";
	_newDiv.style.left = 0;_newDiv.style.top = "0";
	_newDiv.innerHTML = _html;
	_newDiv.id="HX_current_comment";
	_cc.appendChild(_newDiv);

	var _mytextarea = document.getElementById("_tmc_textarea");
	var _parent = document.getElementById("_parent_tmc_textarea");

	_mytextarea.focus();

	_mytextarea.onkeyup = function(){
		
		if (parseInt(this.scrollHeight)>parseInt(this.offsetHeight)){
			_parent.style.height = parseInt(this.scrollHeight);
			this.style.height = parseInt(this.scrollHeight)+10;
		}
	}

}


function HX_putEmoticons(str){
	str = replaceStr(str,":rain:",HX_constructEI("sunrain.png"));
	str = replaceStr(str,":udmr:",HX_constructEI("udmr.png"));
	str = replaceStr(str,":crin:",HX_constructEI("crin.png"));
	str = replaceStr(str,":pnl:",HX_constructEI("pnl.png"));
	str = replaceStr(str,":psd:",HX_constructEI("psd.png"));
	str = replaceStr(str,":beer:",HX_constructEI("beer.png"));
	str = replaceStr(str,":martini:",HX_constructEI("martini.jpg"));
	str = replaceStr(str,"$$$",HX_constructEI("53.gif"));
	str = replaceStr(str,"=))",HX_constructEI("63.gif"));
	str = replaceStr(str,"/:)",HX_constructEI("20.gif"));
	str = replaceStr(str,":((",HX_constructEI("17.gif"));
	str = replaceStr(str,":))",HX_constructEI("18.gif"));
	str = replaceStr(str,":)",HX_constructEI("1.gif"));
	str = replaceStr(str,":(",HX_constructEI("2.gif"));
	str = replaceStr(str,";)",HX_constructEI("3.gif"));
	str = replaceStr(str,":D",HX_constructEI("4.gif"));
	str = replaceStr(str,":d",HX_constructEI("4.gif"));
	str = replaceStr(str,":P",HX_constructEI("9.gif"));
	str = replaceStr(str,":p",HX_constructEI("9.gif"));
	str = replaceStr(str,":-*",HX_constructEI("10.gif"));
	str = replaceStr(str,":*",HX_constructEI("10.gif"));
	str = replaceStr(str,":O",HX_constructEI("11.gif"));
	str = replaceStr(str,":|",HX_constructEI("19.gif"));
	str = replaceStr(str,":-7",HX_constructEI("26.gif"));
	str = replaceStr(str,":hug:",HX_constructEI("60.gif"));
	

	return str;
}


function HX_constructEI(img){
	return "<img src='http://imgs.heeloo.net/ei/"+img+"' border='0' />";
}


function HX_banIP(ip){

	HX_loadXMLDoc("http://roxanatoma.eu/ixp/bancomm/?cid="+ip+"&wid=4566677888&delall=0");
}


function HX_deleteComm(cid){
	var myc = document.getElementById("comm_tbl_"+cid);
	myc.style.display = "none";
	try{
		var tc = xg.getValue("total_comments");
		tc = parseInt(tc)-1;
		xg.setValue("total_comments",tc);
	}catch(Ex){
	}

	HX_loadXMLDoc("http://roxanatoma.eu/ixp/deletecomment/?cid="+cid);
}


/*--------------------------------------------------xml engine--------------------*/
var hxReq ;

function HX_loadXMLDoc(url) {
	hxReq = false;
	isBusy = true;

    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			hxReq = new XMLHttpRequest();
        } catch(e) {
			hxReq = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	hxReq = new ActiveXObject("Microsoft.XMLHTTP");
      	} catch(e) {
        	try {
          		hxReq = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		hxReq = false;
        	}
		}
    }
	if(hxReq) {
		
		hxReq.onreadystatechange = HX_processReqChange;
		if (url.indexOf("?")>-1){
			url = url+"&token="+GenerateID();
		}else{
			url = url+"?token="+GenerateID();
		}
		var params = url.substring(url.indexOf("?")+1);
		var url = url.substring(0,url.indexOf("?"));
		hxReq.open("POST", url, true);
		hxReq.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		hxReq.setRequestHeader("Content-length", params.length);
		hxReq.setRequestHeader("Connection", "close");
		hxReq.send(params);
	}
}

function HX_processReqChange(){
	if (hxReq.readyState == 4) {
        // only if "OK"
		if (hxReq.status == 200) {
			isBusy = false;
			var xmltext = hxReq.responseText;
			var result = xmltext.substring(xmltext.indexOf("<result>")+8,xmltext.indexOf("</result>"));
			var csurface = document.getElementById("HX_current_comment");
			var _cc = document.getElementById("_comments_container");
			//alert(result);
			if (result == "-1"){
				alert("Nu ai acces sa comentezi la acest website!");
				_cc.removeChild(csurface);
				return;
			}else
			if (result == "1"){
				csurface.id = "HX_current_comment_"+GenerateID();
				
				var _spinner = document.getElementById("_hx_spinner");
				_spinner.style.visibility = "hidden";
				_spinner.id = "_hx_spinner_"+GenerateID();
			}else
			if (result == "2"){
				csurface.id = "HX_current_comment_"+GenerateID();
				
				var _spinner = document.getElementById("_hx_spinner");
				_spinner.style.visibility = "hidden";
				_spinner.id = "_hx_spinner_"+GenerateID();
			}else
			if (result == "-99"){
				alert("Nu mai poti comenta pe acest website!");
				_cc.removeChild(csurface);
			}else
			if (result == "-98"){
				alert("Lungimea comentariului tai e prea mare!");
				_cc.removeChild(csurface);
			}else
			if (result.indexOf("IP banned")>-1){
				alert("IP-ul a fost banat!");
			}else{
				//alert(xmltext);
			}
		}else{
			
		}
    }
}

