function openWindow(mypage, myname, w, h, scroll) 
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	gmtWindow = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { gmtWindow.window.focus(); }
}

function find_all_checkbox_values(what, checkbox_name) {
	parameters = '';
	what = document.getElementById(what);
	for (var i=0, j=what.elements.length; i<j; i++) {
		if(what.elements[i].name == checkbox_name && what.elements[i].checked == true) {
			parameters += "&" + what.elements[i].name + "=" + encodeURI(what.elements[i].value);
		}
	}
	return parameters;
}

function find_all_form_elements(form_name) {
	parameters = '';
	what = document.getElementById(form_name);
	for (var i=0, j=what.elements.length; i<j; i++) {
		if( (what.elements[i].type == 'radio' || what.elements[i].type == 'checkbox') && what.elements[i].checked != true) {
		} else {
			tmp = encodeURI(what.elements[i].value);
			tmp = tmp.replace(/\+/g, '%2B');
			parameters += "&" + what.elements[i].name + "=" + tmp;
			//alert(what.elements[i].name+ "=" + tmp);
		}
	}
	return parameters;
}
function submit_form(formid) {
	
	tmp = document.getElementById('new_status');
	if(tmp.value.length > 140) {
		alert('Текстът не трябва да е по-голям от 140 символа');
		return;
	}
	
	params = find_all_form_elements(formid);
	makePOSTRequestStatus('ajax/statusUpdate.php', params, 'status');
	setTimeout( refresh, 1000);
}
function refresh () {
	var ids=new Array("showStatus", "showFavorites", "showAll", "showMe");
	if ( document.getElementById(ids[1]) == null ) {
		return;
	} else {
		for (var i=0;i<ids.length;i++){
			if (document.getElementById(ids[i]).className == 'current') {
				makeRequest('ajax/'+ids[i]+'.php', 'show');
				//alert(ids[i]+'.php');
			}
		}
	}
}
function alert_mysymbols(mytext) {
	tmp = document.getElementById('symbols_left');
	tmp1 = 140 - mytext.value.length;
	if(tmp1 < 0) {
		mytext.value = mytext.value.substring(0, 139);
	} else {
		tmp.innerHTML = (tmp1);
	}
}
function alert_mysymbols2(mytext) {
	tmp = document.getElementById('symbolsleft');
	tmp1 = 140 - mytext.value.length;
	if(tmp1 < 0) {
		mytext.value = mytext.value.substring(0, 139);
	} else {
		tmp.innerHTML = (tmp1);
	}
}
function prepareHints() {
	var els = document.getElementsByTagName("a");
	for (var i=0; i<els.length; i++){
		if ( els[i].title == '' ) {
			continue;
		}
		eval('els[i].onmouseover = function (event) {show_hint(event, \'' + els[i].title  + '\');	}');
		els[i].onmouseout = function () {
			hide_hint();
		}
		els[i].onmousemove = function (event) {
			move_hint(event);
		}
		els[i].setAttribute("title", "");
	}
}

function show_hint(e, msg) {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY)
    {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)
    {
        posx = e.clientX + document.documentElement.scrollLeft;
        posy = e.clientY + document.documentElement.scrollTop;;
    }
	
	posy += 20;

	if ( document.getElementById('hintDiv' ) ) {
		document.getElementById('hintDiv').innerHTML = msg;
		document.getElementById('hintDiv').style.left = posx + "px";
		document.getElementById('hintDiv').style.top = posy + "px";
		document.getElementById('hintDiv').style.display = 'block';
	}
}

function move_hint(e) {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY)
    {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)
    {
        posx = e.clientX + document.documentElement.scrollLeft;
        posy = e.clientY + document.documentElement.scrollTop;;
    }
	
	posy += 20;

	if ( document.getElementById('hintDiv' ) ) {
		document.getElementById('hintDiv').style.left = posx + "px";
		document.getElementById('hintDiv').style.top = posy + "px";
	}
}

function hide_hint() {
	if ( document.getElementById('hintDiv' ) ) {
		document.getElementById('hintDiv' ).style.display = 'none';
	}
}

/////




function make_active(id){	
	deactivateall();
	activate(id);
}

function deactivateall(){
	var ids=new Array("showStatus", "showFavorites", "showReplies", "showAll", "showMy");
	if ( document.getElementById(ids[1]) == null ) {
		ids=new Array("showProfile", "showPassword", "showPicture");
	}
	if ( document.getElementById(ids[1]) == null ) {
		ids=new Array("showReceived", "showSent", "showArchive", "showNew");
	}
	for (var i=0;i<ids.length;i++){
		deactivate(ids[i]);
	}		  
}
function deactivate(id) {
	if (document.getElementById) {
		document.getElementById(id).className = '';
	}
	else {
		if (document.layers) {
			document.id.className = '';
		}
		else {
			document.all.id.className = '';
		}
	}
}

function activate(id) {
	if (document.getElementById) {
		document.getElementById(id).className = 'active';
	}
	else {
		if (document.layers) {
			document.id.className = 'active';
		}
		else {
			document.all.id.className = 'active';
		}
	}
}
/////		
function statusupdate1(area, len) {
	var v	= area.value;
	if( v.length > len ) {
		v	= v.substr(0, len);
	}
	if( area.value != v ) {
		area.value	= v;
	}
	var rem	= len - v.length;
	var lenrem	= document.getElementById('new_status');
	lenrem.innerHTML	= rem;
	setTimeout( function() { statusupdate1(area, len); }, 200 );
}

function statusupdate2(area, len) {
	var v	= area.value;
	var n	= false;
	while( v.indexOf("\n")!=-1 || v.indexOf("\r")!=-1 ) {
		v	= v.replace(/\r\n|\n|\r/, " ");
		n	= true;
	}
	if( n ) {
		while( v.indexOf("  ")!=-1 ) {
			v	= v.replace("  ", " ");
		}
	}
	if( v.length > len ) {
		v	= v.substr(0, len);
	}
	if( area.value != v ) {
		area.value	= v;
	}
	setTimeout( function() { statusupdate2(area, len); }, 2000 );
}
function delete_msg (mid) {
	if ( confirm("Сигурни ли сте, че желаете да изтриете това съобщение?") ) {
		makePOSTRequestDelete('delete.php', '&id='+mid+'&', mid); 
	}
}
function delete_msg2 (mid, box) {
	if ( confirm("Сигурни ли сте, че желаете да изтриете това съобщение?") ) {
		makePOSTRequestDelete('delete2.php', '&id='+mid+'&box='+box+'&', mid); 
	}
}
function block_user (uid, uname) {
	if ( confirm('Сигурни ли сте, че желаете да забраните на '+uname+' да ви следва?') ) {
		makePOSTRequestStatus('block.php', '&id='+uid+'&', 'status', 'window.location.reload()');
	} else {
		document.getElementById('blockme').style.display='block';
	}
}