function getDimensions()
{
	if (window.innerWidth) {WindowHeight = window.innerHeight;}
	else if (document.documentElement && document.documentElement.clientWidth) {WindowHeight = document.documentElement.clientHeight; }
	
	if (document.documentElement && document.documentElement.scrollWidth) { DocumentHeight = document.documentElement.scrollHeight; }
	else if (document.body.scrollWidth) { DocumentHeight = document.body.scrollHeight; }
	
	if (window.innerWidth) {VerticalScroll = window.pageYOffset;}
	else if (document.documentElement && document.documentElement.clientWidth) {VerticalScroll = document.documentElement.scrollTop;}
}

function getX(e) {
	var x = 0; 
	while(e) { 
		x += e.offsetLeft; 
		e = e.offsetParent; 
	}
	return x; 
}
function getY(e) {
	var y = 0; 
	while(e) { 
		y += e.offsetTop; 
		e = e.offsetParent; 
	}
	return y; 
}

function hideTrialRequest() {
		document.getElementById("tp").style.display = "none";

}
function showTrialRequest(obj, product, productName, place) {
	panel = document.getElementById("tp");
	form = document.getElementById("tf");
	linkText = document.getElementById("tl");
	linkHref = document.getElementById("tl_a");

	panelY = getY(obj) + obj.offsetHeight + 10;
	//panelX = obj.offsetLeft + 20 + 179;
	if (place == 'left') panelX = 288;
	else panelX = 688;

	
	panel.style.top = panelY + "px";
	panel.style.left = panelX + "px";
	document.forms['tf'].send.onclick = function() { sendRequest(obj.href); return false;};
	document.forms['tf'].product.value = product;
	document.forms['tf'].productName.value = productName;
	linkHref.href = obj.href;
	linkHref.onclick = function() { hideTrialRequest();};
	form.style.display = 'block';
	linkText.style.display = 'none';
	
	panel.style.display = "block";
	if (form.offsetHeight + 20 > panel.offsetHeight) panel.style.height = (form.offsetHeight + 20) + "px";


	getDimensions();
	
	panelBottom = panel.offsetTop + panel.offsetHeight;
	windowBottom = VerticalScroll + WindowHeight;
	if (panelBottom > windowBottom) { 
		windowBottomCount = windowBottom + 5;
		scrolltime = 40;
		scrollto = 20;
		scrollBy(0,scrollto);
		while (panelBottom+20 > windowBottomCount) {
			setTimeout(function() { scrollBy(0,scrollto); } , scrolltime);
			windowBottomCount += scrollto;
		}
	}
}

function sendRequest(trialLink) {

	req = false;
		if(window.XMLHttpRequest && !(window.ActiveXObject)) {
			try {
			req = new XMLHttpRequest();
			} catch(e) {
				req = false;
			}
    	} else if(window.ActiveXObject) {
       		try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					req = false;
				}
			}
		}

	if(req) {
		name = document.forms['tf'].name.value;
		lastName = document.forms['tf'].lastName.value;
		email = document.forms['tf'].email.value;
		lang = document.forms['tf'].lang.value;
		source = document.forms['tf'].source.value;
		product = document.forms['tf'].product.value;
		productName = document.forms['tf'].productName.value;
		loc = document.location;

		DestinationUrl = "http://qst.abbyy.com/utf-8/49.process";
		ReadContent = true;
		
		params = 'name=' + name + '&lastName=' + lastName + '&email=' + email + '&lang=' + lang + '&source=' + source + '&product=' + product + '&productDistrib=' + product + '&productName=' + productName + '&distrib=' + trialLink + "&location=" + loc + '&DestinationUrl=' + DestinationUrl + '&ReadContent=' + ReadContent;
		//url = "http://www.abbyy.com/requestsender.aspx";
		url = document.location.protocol + "//"+ document.location.host + "/requestsender.aspx";

		req.onreadystatechange = function () { onResultLoaded(trialLink); };
		
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(params);
	}
}


function onResultLoaded(trialLink) {
	if (req.readyState == 4) {
		if (req.status == 200) {
			fileContent = req.responseText;
			document.getElementById("tf").style.display = 'none';
			document.getElementById("tl").style.display = 'block';
			panel.style.height = (document.getElementById("tl").offsetHeight + 20) + "px";
			setTimeout(function() { document.location = trialLink; } , 2000);
		}
	}
}