$.fn.contextMenu.addAction({name: 'era', title: 'Erakliendile', bind: eraAction});
$.fn.contextMenu.addAction({name: 'firma', title: '&Auml;rikliendile', bind: firmaAction});

function eraAction()
{
	var objDta = $(this).data('objectData');

	var myForm = document.createElement("form");
	myForm.target="www_krediidiinfo_ee";
	myForm.method="post" ;
	myForm.action = 'https://www.e-seif.ee/index.php?redirect=firma_taust';
	var myInput = document.createElement("input") ;
	myInput.setAttribute("name", "search_str");
	myInput.setAttribute("value", objDta.regcode);
	myForm.appendChild(myInput) ;
	document.body.appendChild(myForm) ;
	myForm.submit() ;
	document.body.removeChild(myForm) ;
	return false;
}

function firmaAction()
{
	var objDta = $(this).data('objectData');
	var myForm = document.createElement("form");
	myForm.target="www_krediidiinfo_ee";
	var myInput = document.createElement("input") ;
	myInput.setAttribute("name", "m");
	
	if (objDta.id == 'SEIRE')
	{
		myForm.action = 'http://www.krediidiinfo.ee/index.php';
		myInput.setAttribute("value", "44");
		myForm.appendChild(myInput) ;
	} else {
		myForm.action = 'http://www.krediidiinfo.ee/index.php';
		myInput.setAttribute("value", "93");
		myForm.appendChild(myInput) ;
		var myInput = document.createElement("input") ;
		myInput.setAttribute("name", "r");
		myInput.setAttribute("value", objDta.id);
		myForm.appendChild(myInput) ;
		var myInput = document.createElement("input") ;
		myInput.setAttribute("name", "rc");
		myInput.setAttribute("value", objDta.regcode);
		myForm.appendChild(myInput) ;
	}
	document.body.appendChild(myForm) ;
	myForm.submit() ;
	document.body.removeChild(myForm) ;
	return false;
}

$(document).ready(function ()
{
	$('.context_anchor').contextMenu();
});

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

function g(id){
	return document.getElementById(id);
}

/* Tooltip */

// false, kui tegemist pole InternetExplorer brauseriga.
var	ie=document.all;
// false, kui tegemist pole DOM standardit toetava brauseriga brauseriga.
var	dom=document.getElementById;
// false, kui tegemist pole Netscape4.X brauseriga.
var ns4=document.layers;

document.write('<div id="tooltip"><div id="tooltipInner"></div></div>');

function showTT(ref,txt){
	document.getElementById("tooltipInner").innerHTML = txt;
	document.getElementById("tooltip").style.display = 'block';
	var tooltipWidth = document.getElementById("tooltip").offsetWidth;
	var tooltipHeight = document.getElementById("tooltip").offsetHeight;

	var leftpos = 0;
	var toppos = 0;
	aTag = ref;
	do
	{
		aTag = aTag.offsetParent;
		if (aTag.tagName == "HTML") break;
		leftpos += aTag.offsetLeft;
		toppos += aTag.offsetTop;
	}
	while(aTag.tagName!="BODY");
	var tooltipTop = ref.offsetTop + toppos;
	var tooltipBottom = tooltipTop + tooltipHeight;
	var tooltipLeft = ref.offsetLeft + ref.offsetWidth + leftpos + 5;
	var tooltipRight = tooltipLeft + tooltipWidth;

	var x,y;
	if (self.pageYOffset || self.pageXOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement) // Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	var scrollLeft = x;
	var scrollTop = y;

	if (self.innerHeight || self.innerWidth) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement) // Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	var clientWidth = x;
	var clientHeight = y;

	var bottom = scrollTop + clientHeight;
	var right = scrollLeft + clientWidth;
	if (tooltipBottom > bottom)
	{
		tooltipTop = bottom - tooltipHeight - 10;
	}
	if (tooltipRight > right)
	{
		//tooltipLeft = ref.offsetLeft - tooltipWidth - ref.offsetWidth - 30;
		tooltipLeft = leftpos - tooltipWidth;
	}
	toppos = tooltipTop;
	leftpos = tooltipLeft;

	document.getElementById("tooltip").style.left = leftpos+'px';
	document.getElementById("tooltip").style.top = toppos+'px';

	hideElement( 'SELECT', document.getElementById("tooltip") );
	hideElement( 'APPLET', document.getElementById("tooltip") );

}
function hideTT(){
	document.getElementById("tooltip").style.display = 'none';
	showElement( 'SELECT', document.getElementById("tooltip") );
	showElement( 'APPLET', document.getElementById("tooltip") );
}

function hideElement( elmID, overDiv )
{
  if( ie )
  {
	for( i = 0;	i <	document.all.tags( elmID ).length; i++ )
	{
	  obj =	document.all.tags( elmID )[i];
	  if( !obj || !obj.offsetParent	)
	  {
		continue;
	  }
	  // Find the element's	offsetTop and offsetLeft relative to the BODY tag.
	  objLeft	= obj.offsetLeft;
	  objTop	= obj.offsetTop;
	  objParent	= obj.offsetParent;
	  while( objParent.tagName.toUpperCase() !=	"BODY" )
	  {
		objLeft	 +=	objParent.offsetLeft;
		objTop	 +=	objParent.offsetTop;
		objParent =	objParent.offsetParent;
		if (objParent.tagName == "HTML") break;
	  }
	  objHeight	= obj.offsetHeight;
	  objWidth = obj.offsetWidth;
	  if(( overDiv.offsetLeft +	overDiv.offsetWidth	) <= objLeft );
	  else if((	overDiv.offsetTop +	overDiv.offsetHeight ) <= objTop );
	  else if( overDiv.offsetTop >=	( objTop + objHeight ));
	  else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
	  else
	  {
		obj.style.visibility = "hidden";
	  }
	}
  }
}
function showElement( elmID	)
{
  if( ie )
  {
	for( i = 0;	i <	document.all.tags( elmID ).length; i++ )
	{
	  obj =	document.all.tags( elmID )[i];
	  if( !obj || !obj.offsetParent	)
	  {
		continue;
	  }
	  obj.style.visibility = "";
	}
  }
}


var ajax = new sack();
var selected_id = null;
var ostr = null;

function whenGet(){
	var e = document.getElementById('col-1'); 
	e.innerHTML = "<div class=\"box1\"><div class=\"w100p\"><p class=\"heading1\">Leitud ettev&otilde;tted</p>Laen andmeid..</div></div>";
}

function selectFirst()
{
	el = document.getElementById('f0');
	if (el) el.onclick(); else {
		e = document.getElementById('col-2'); 
		e.innerHTML = "<div class=\"box1\"><div class=\"w100p\"><h1 class=\"heading1\">Ettev&otilde;tte andmed</h1><table class=\"form-data\"><tr><th>Ettev&otilde;tte nimi:</th><td colspan=\"3\"><strong>-</strong></td></tr><tr class=\"even\"><th>Aadress:</th><td colspan=\"3\">-</td></tr><tr class=\"spaced\"><th>Registrikood:</th><td>-</td><th>P&otilde;hikapital:</th><td>-</td></tr><tr class=\"even\"><th>Registreeritud:</th><td>-</td><th>Registripiirkond:</th><td>-</td></tr><tr><th>K&auml;ibemaksu info:</th><td colspan=\"3\">-</td></tr><tr class=\"spaced\"><th>Telefon:</th><td colspan=\"3\">-</td></tr><tr><th>Faks:</th><td colspan=\"3\">-</td></tr><tr class=\"even\"><th>E-post:</th><td colspan=\"3\">-</td></tr><tr><th>Koduleht:</th><td colspan=\"3\">-</td></tr></table><h2 class=\"heading2\">Tooted</h2><table class=\"data\"><tr><td colspan=\"2\">&nbsp;</td><td class=\"link w50p\"><a href=\"index.php?m=32\" target=\"www_krediidiinfo_ee\">Ettev&otilde;tte makseh&auml;irete raport</a></td><td><a href=\"index.php?m=93&r=M\" target=\"www_krediidiinfo_ee\">Telli</a></td></tr><tr class=\"even\"><td class=\"link w50p\"><a href=\"index.php?m=11\" target=\"www_krediidiinfo_ee\">Krediidireiting</a></td><td><a href=\"index.php?m=93&r=FRE\" target=\"www_krediidiinfo_ee\">Telli</a></td><td class=\"link w50p\"><a href=\"index.php?m=31\" target=\"www_krediidiinfo_ee\">Ettev&otilde;ttega seotud isikud</a></td><td><a href=\"index.php?m=93&r=FU\" target=\"www_krediidiinfo_ee\">Telli</a></td></tr><tr><td class=\"link w50p\"><a href=\"index.php?m=138\" target=\"www_krediidiinfo_ee\">Krediidilimiidi raport</a></td><td><a href=\"index.php?m=93&r=FLE\" target=\"www_krediidiinfo_ee\">Telli</a></td><td class=\"link w100p\"><a href=\"index.php?m=30\" target=\"www_krediidiinfo_ee\">Bilanss ja kasumiaruanne</a></td><td><a href=\"index.php?m=93&r=F06\" target=\"www_krediidiinfo_ee\">Telli</a></td></tr><tr class=\"even\"><td class=\"link w50p\"><a href=\"index.php?m=12\" target=\"www_krediidiinfo_ee\">Faktiraport</a></td><td><a href=\"index.php?m=93&r=FE\" target=\"www_krediidiinfo_ee\">Telli</a></td><td class=\"link w50p\"><a href=\"index.php?m=34\" target=\"www_krediidiinfo_ee\">P&otilde;hikirja koopia</a></td><td><a href=\"index.php?m=93&r=FPK\" target=\"www_krediidiinfo_ee\">Telli</a></td></tr><tr><td class=\"link w50p\"><a href=\"index.php?m=33\" target=\"www_krediidiinfo_ee\">Aastaaruande koopia</a></td><td><a href=\"index.php?m=93&r=FA6\" target=\"www_krediidiinfo_ee\">Telli</a></td><td class=\"link w50p\"><a href=\"index.php?m=35\" target=\"www_krediidiinfo_ee\">Pandiinfo</a></td><td><a href=\"index.php?m=93&r=FP\" target=\"www_krediidiinfo_ee\">Telli</a></td></tr><tr class=\"even\"><td class=\"link w50p\"><a href=\"index.php?m=29\" target=\"www_krediidiinfo_ee\">B-kaardi andmed</a></td><td><a href=\"index.php?m=93&r=FRB\" target=\"www_krediidiinfo_ee\">Telli</a></td></tr></table></div></div>";
	}
}

function page_comp(ccount, show_page)
{
    selected_id = null;
    ajax.reset();
    ajax.setVar("s_onimi", ostr);
    ajax.setVar("s_ccount", ccount);
    ajax.setVar("s_show", show_page);
    ajax.requestFile = "index.php";
    ajax.method = 'POST';
    ajax.element = 'col-1';
    ajax.onLoading = whenGet;
    ajax.runAJAX();
}

function search_comp()
{
    if (ostr != document.forms['tfsform'].elements['s_onimi'].value && document.forms['tfsform'].elements['s_onimi'].value != "")
    {
		ostr = document.forms['tfsform'].elements['s_onimi'].value;
		selected_id = null;
		ajax.reset();
		ajax.setVar("s_onimi", ostr);
		ajax.requestFile = "index.php";
		ajax.method = 'POST';
		ajax.element = 'col-1';
		ajax.onLoading = whenGet;
	    ajax.onCompletion = selectFirst;
		ajax.runAJAX();
    }
}

function actionLinks()
{
	$('.context_anchor').contextMenu();
}

function get_item(el_id, regno, getkmk)
{
	if (el_id != selected_id || getkmk == true)
    {
			if (selected_id != null)
			{
				el = document.getElementById('f'+selected_id);
				if (el) el.className = null; 
			}

			el = document.getElementById('f'+el_id);
			if (el)
			{
				el.className = 'active';
				selected_id = el_id;
				ajax.reset();
				ajax.setVar("s_regcode", regno);
				ajax.setVar("s_iid", el_id);
				if (getkmk) ajax.setVar("s_kmk", "1");
				ajax.requestFile = "index.php";
				ajax.method = 'POST';
				ajax.element = 'col-2';
	            ajax.runAJAX();
			    ajax.onCompletion = actionLinks;

			}
    }
}
