var tabHeight=72;
var frameHeight;
var bodyHeight;
var bodyWidth;
var interval;
var frame=document.getElementById('frame');
var iframe=document.getElementById('iframe');
var tab=document.getElementById('tab');
var debug=0;


if(debug==1){
document.body.innerHTML+='<div id="debug" style="width:500px;height:500px;overlay:auto;background:#FFFFFF;border:solid 1px #666666"></div>';	
}
function setDefTabSize(){
setTabSize(72);	
}

function detectPageSize(){
	bodyHeight = 0;
	if( typeof( window.innerHeight ) == 'number' ) {
	  bodyHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
	  //IE 6+ in 'standards compliant mode'
	  bodyHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientHeight ) ) {
	  //IE 4 compatible
	  bodyHeight = document.body.clientHeight;
	}
}

function fixFrame(){
detectPageSize();
frameHeight=Math.round(bodyHeight-tabHeight)+'px';
frame=document.getElementById('frame')
frame.style.height=frameHeight;
}
function runBodyFix(){
fixFrame();
var interval = setInterval("fixFrame()",1000);
}

function setTabSize(tabHe){
tab=document.getElementById('tab');
tabHeight=tabHe;
tab.style.height=tabHe+'px';
fixFrame();
}

function debug(fillBy){
var debugDiv=document.getElementById('debug');
debugDiv.innerHTML+=fillBy+'|';
}
function closeBar(){
var whereto=document.getElementById('iframe').src;
window.location=whereto;
}
/* Start of requests */
function doLogin(force) {
	
	var link_id=$('link_id').value;
 JsHttpRequest.query(
            '/ajaxlogin2.php', // backend
            {
                'username': $('username').value,
				'password': $('password').value,
				'processlogin':'1'	
            },
            // Function is called when an answer arrives. 
            function(result, errors) {
			if(errors){
		//	$('debugz').innerHTML=''+errors+'';
			}
	//	alert(errors);
                if (result) {
				
				fillLogin(result["q"]);
				$('user_id').value=result["user_id"];
				setVoting(result["user_id"]);
				hideBlock('hide');
				
                }
            },
            false  // do not disable caching
        )
}
function kklklklkl(){
var login = '' + document.getElementById('username').value;
var pass = '' + document.getElementById('password').value;
		
		var req = new Subsys_JsHttpRequest_Js();

		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.responseJS.q) {
				fillLogin(req.responseJS.q);
				$('user_id').value=req.responseJS.user_id;
				setVoting(req.responseJS.user_id);
				hideBlock('hide');
				}
					else
					{
					fillLogin("...");
					}
			}
				else
				{
				fillLogin('<span style="width:150px;padding-left:20px;background:url(/img/1-1.gif) no-repeat;">Forbinder…</span>');
				}
		}
		req.caching = true;
		req.open('GET', '/ajaxlogin.php?username='+login+'&password='+pass+'&processlogin=1', true);
		//alert('/ajaxlogin.php?username='+login+'&password='+pass+'&processlogin=1');
		req.send(null);
	}
	
	
function fillLogin (data){
if(data!='no_error'){
	$('loginInformation').innerHTML=''+data+'';  
	}else{
		$('hide').style.display='none';
		$('logTab').style.display='none';
		setDefTabSize();
	
		$('s_logTab').innerHTML='&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:hideBlock(\'addto\');setTabSize(\'450\');showBlock(\'comAddTab\');showBlock(\'comTab\');">'+langComment+'</a>';
		$('s_regTab').innerHTML='&nbsp;&nbsp;|&nbsp;&nbsp;<a href="'+Logout+'">'+langLogout+'</a>';
				
		$('mainuserbar').style.width='350px';
		$('userTab').style.width='350px';
	}
}
function setVoting(user_id){
var link_id=$('link_id').value;
var rand=$('rand').value;

//alert(user_id);
		var req = new Subsys_JsHttpRequest_Js();

		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.responseJS) {
					fillVotes(req.responseJS.vote);
					fillUnvotes(req.responseJS.unvote);
					///alert(req.responseJS.vote+'|'+req.responseJS.unvote);
				}
					else
					{
					//fillLogin("...");
					}
			}
				else
				{
				//fillLogin('<span style="width:150px;padding-left:20px;background:url(/img/1-1.gif) no-repeat;">Forbinder…</span>');
				}
		}
		req.caching = true;
		req.open('GET', '/ajaxlogin.php?gMD5=1&user_id='+user_id+'&link_id='+link_id+'&rand='+rand+'', true);
		req.send({ user_id: user_id, link_id: link_id, rand: rand, gMD5:'1'});
}
function fillVotes(data){
$('d_vote').innerHTML='	<a href="javascript:'+data+'"><img id="i_vote" onmouseover="this.src=\'/img/top_o.gif\'" onmouseout="this.src=\'/img/top_a.gif\'" src="/img/top_a.gif" border="0" alt="{#PLIGG_Visual_Vote_For_It#}" /></a>';
}
function fillUnvotes(data){
$('d_unvote').innerHTML='	<a href="javascript:'+data+'"><img id="i_vote" onmouseover="this.src=\'/img/bot_o.gif\'" onmouseout="this.src=\'/img/bot_a.gif\'" src="/img/bot_a.gif" border="0" alt="{#PLIGG_Visual_Vote_For_It#}" /></a>';
}

function hideVoting(){
$('d_vote').innerHTML='<img id="i_vote" src="/img/top_i.gif" border="0" alt="{#PLIGG_Visual_Vote_For_It#}" />';
$('d_unvote').innerHTML='<img id="i_vote" src="/img/bot_i.gif" border="0" alt="{#PLIGG_Visual_Vote_For_It#}" /></a>';
}
function doReg(){
	
		var login = '' + document.getElementById('reg_username').value;
		var pass = '' + document.getElementById('reg_password').value;
		var pass2 = '' + document.getElementById('reg_verify').value;
		var email = '' + document.getElementById('reg_email').value;
		
//	req.open('GET', '/ajaxregister.php?reg_username='+login+'&reg_password='+pass+'&reg_password2='+pass2+'&reg_email='+email+'&regfrom=full', true);
//	req.send({ reg_username: login, reg_password: pass, reg_password2: pass2, reg_email: email, regfrom: 'full' });

JsHttpRequest.query(
            '/ajaxlogin2.php', // backend
            {
                'reg_username': login,
				'reg_password': pass,
				'reg_password2': pass2,
				'reg_email':email,
				'register':'1'	
            },
            // Function is called when an answer arrives. 
            function(result, errors) {
			if(errors){
		//	$('debugz').innerHTML=''+errors+'';
			}
		//alert(errors);
                if (result) {
				
				if (result["error"]) {
				fillReg(result["error"]);
				fillLogin(result["error"]);
				setVoting(result["user_id"]);
				}
					else
					{
					fillReg("...");
					}
                }
            },
            false  // do not disable caching
        )

}

	
function fillReg (data){
	if(data!='no_error'){
	
	$('regInformation').innerHTML=''+data+'';  
	}else{
		hideBlock('hide');
		$('regTab').style.display='none';
		setDefTabSize();
		$('s_logTab').innerHTML='';
		$('s_regTab').innerHTML='';
				$('s_logTab').innerHTML='&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:hideBlock(\'addto\');setTabSize(\'450\');showBlock(\'comAddTab\');showBlock(\'comTab\');">'+langComment+'</a>';
		$('s_regTab').innerHTML='&nbsp;&nbsp;|&nbsp;&nbsp;<a href="'+Logout+'">'+langLogout+'</a>';
//		$('userTab').innerHTML+='&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:showBlock(\'comTab\');setTabSize(\'150\');">'+langComment+'</a>';
	}
}

function doPass(force) {
var login = '' + document.getElementById('forgot-name').value;
		
		//alert(login +' - '+ pass);
		var req = new Subsys_JsHttpRequest_Js();

		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.responseJS.q) {
				fillPass(req.responseJS.q);
				if(req.responseJS.smth=='no_error'){
					setTimeout(setDefTabSize,3000);	
					setTimeout(function (){
										 $('logTab').style.display='none';
										 },2500);
				}
				//alert(req.responseJS.q);
				}
					else
					{
					fillPass("...");
					}
			}
				else
				{
				fillPass('<span style="width:150px;padding-left:20px;background:url(/img/1-1.gif) no-repeat;">Forbinder...</span>');
				}
		}
		req.caching = true;
		req.open('GET', '/ajaxlogin.php?username='+login+'&processlogin=3', true);
		req.send({ username: login});
	}
	
	
function fillPass (data){
	if(data!='no_error'){
	$('passInformation').innerHTML=''+data+'';  
	}else{
		//$('logTab').style.display='none';
		//setTabSize(52);
		//$('loginInformation').innerHTML='logged in!';  
		//$('s_logTab').innerHTML='';
		//$('s_regTab').innerHTML='';
		//$('userTab').innerHTML+='&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:showBlock(\'comTab\');setTabSize(\'150\');">'+langComment+'</a>';
	}
}

function doComment(force) {
	$("subComment").disabled="disabled";
	fillComment('<span style="width:150px;padding-left:20px;background:url(/img/1-1.gif) no-repeat;">Forbinder...</span>');
	//var editor_data = CKEDITOR.instances.comment_content.getData();
	var editor_data = WebEditorGetContent("comment_content");

	JsHttpRequest.query(
            '/ajaxlogin2.php', // backend  comment_content: login, user_id: user_id, link_id:link_id, randkey: rand
            {
                'user_id': $('user_id').value,
				'link_id': $('link_id').value,
                'randkey': $('rand').value,
				'comment_content': editor_data,
				'comment':'1'	
            },
            // Function is called when an answer arrives. 
            function(result, errors) {
				fillComment('');
                if (result) {
				
				
				//fillComment(result["q"]);
				$('commInfo').innerHTML=result["mess"];
			
				setTimeout(setDefTabSize,2200);
				setTimeout(function(){
						hideBlock('comAddTab');
						hideBlock('comTab');
									},2000);
				
				
				setTimeout(function(){$('commInfo').innerHTML='';$("subComment").disabled="";
				$("comment").value="";},2300);
				loadComms();
				
				
                }
            },
            false  // do not disable caching
        )
	
	}
	
	
function fillComment (data){
	if(data!='no_error'){
	$('commInfo').innerHTML=''+data+'';  
	}else{
		
	}
}


function onEnter( evt, frm ) {
var keyCode = null;
if( evt.which ) {
keyCode = evt.which;
} else if( evt.keyCode ) {
keyCode = evt.keyCode;
}
//alert(keyCode);
if( 13 == keyCode && frm=='password' ) {
doLogin();
return false;
}
if(13 == keyCode && frm=='reg_verify' ) {
doReg();
return false;
}
if(13 == keyCode && frm=='forgot-name' ) {
doPass();
return false;
}
return true;
}