function showStockAgentDialog(item_id) {
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	//IE
	if(!window.pageYOffset)	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0)) {
			offsetY = document.documentElement.scrollTop;
		} else { // quirks mode
			offsetY = document.body.scrollTop;
		}
	} else { // w3c
		offsetY = window.pageYOffset;
	}
	
	var w = document.getElementById('stock_agent_dialog').offsetWidth;
	var h = document.getElementById('stock_agent_dialog').offsetHeight;

	leftpos = (windowWidth-w)/2;
	toppos = (windowHeight-h)/2;
	
	disableBrowser();
	
	document.getElementById('stock_agent_item_id_popup').value = item_id;
	document.getElementById('stock_agent_name_popup').value = '';
	document.getElementById('stock_agent_email_popup').value = '';
	document.getElementById("stock_agent_startup_text").style.position = 'static';
	document.getElementById("stock_agent_startup_text").style.visibility = 'visible';
	document.getElementById("stock_agent_response_text_popup").style.position = 'absolute';
	document.getElementById("stock_agent_response_text_popup").style.visibility = 'hidden';
	
	document.getElementById('stock_agent_dialog').style.top = Math.round(toppos)+offsetY+'px';
	document.getElementById('stock_agent_dialog').style.left = Math.round(leftpos)+'px';
	document.getElementById('stock_agent_dialog').style.visibility = 'visible';
	document.getElementById('stock_agent_content').style.visibility = 'visible';
}

function closeStockAgentDialog() {
	enableBrowser();
	
	document.getElementById('stock_agent_dialog').style.visibility = 'hidden';
	document.getElementById('stock_agent_content').style.visibility = 'hidden';
	
	document.getElementById('stock_agent_loader').style.position = 'absolute';
	document.getElementById('stock_agent_loader').style.visibility = 'hidden';
	
	document.getElementById("stock_agent_startup_text").style.position = 'absolute';
	document.getElementById("stock_agent_startup_text").style.visibility = 'hidden';
	
	document.getElementById("stock_agent_response_text_popup").style.position = 'absolute';
	document.getElementById("stock_agent_response_text_popup").style.visibility = 'hidden';
}

function checkStockAgentInputs(location) {
	var error = false;
	for (field_id in login_required_fields['stock_agent']) {
		if (!login_required_fields['stock_agent'][field_id]['param'].test(document.getElementById(field_id+'_'+location).value)) {
			error = true;
			alert(login_required_fields['stock_agent'][field_id]['error']);
			document.getElementById(field_id+'_'+location).className = 'textfield_singleline_error stock_agent_textfield';
			document.getElementById(field_id+'_'+location).onkeyup = function () {
				this.className = 'textfield_singleline stock_agent_textfield';
				this.onkeyup = null;
			}
			
			document.getElementById(field_id+'_'+location).focus();
			break;
		}
	}
	
	if (!error && location == 'popup') {
		document.getElementById('stock_agent_content').style.position = 'absolute';
		document.getElementById('stock_agent_content').style.visibility = 'hidden';
		
		document.getElementById('stock_agent_loader').style.position = 'static';
		document.getElementById('stock_agent_loader').style.visibility = 'visible';
		
		submitStockAgent(location);
	} else {
		submitStockAgent(location);	
	}
}

var stock_agent_connection = 0;

function submitStockAgent(location) {
	ran = Math.round(Math.random()*100000); 
	url = "/functions/stock_agent/check.php?Ran="+ran.toString();
	
	if (location == 'popup') {
		data = "name="+document.getElementById('stock_agent_name_'+location).value+"&email="+document.getElementById('stock_agent_email_'+location).value+"&item_id="+document.getElementById('stock_agent_item_id_'+location).value+"&location="+location;
	} else if (location == 'static') {
		data = "name="+document.getElementById('stock_agent_name_'+location).value+"&email="+document.getElementById('stock_agent_email_'+location).value+"&item_id="+document.getElementById('stock_agent_item_id_'+location).value+"&variation_id="+document.getElementById('stock_agent_variation_id_'+location).value+"&location="+location;
	}

	if (window.XMLHttpRequest) {
		stock_agent_connection = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		stock_agent_connection = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (stock_agent_connection) {
		stock_agent_connection.onreadystatechange=stockAgentEval
		stock_agent_connection.open("POST",url,true);
		stock_agent_connection.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		stock_agent_connection.send(data);
	}
}

function stockAgentEval() {
	if (stock_agent_connection.readyState==4) {
		if (stock_agent_connection.status==200){
			eval(stock_agent_connection.responseText);
		}
	}
}