function createRequestObject() {
        var ro;
        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer") {
                ro = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
                ro = new XMLHttpRequest();
        }
        return ro;
}

var http = createRequestObject();
        
function sendReq(idiv,arg) {
	point = window.center({width:100,height:100});
	if(idiv == 'rpc_cart') { showLayer('Processing',point); }
        http.open('get', '/'+idiv+'.html?'+arg);
        http.onreadystatechange = handleResponse;
        http.send(null);
	if(idiv == 'rpc_cart') { setTimeout("hideLayer('Processing')",600); }
}

function handleResponse() {
        if(http.readyState == 4) {
                var response = http.responseText;
                var update = new Array();
                if(response.indexOf('|' != -1)) {
                        update = response.split('|');
			if(update[0] != 'no_update') {
				document.getElementById(update[0]).innerHTML = update[1];
			} else if(typeof(update[2]) != 'undefined') {
				document.getElementById(update[2]).innerHTML = update[3];
			}
                }
        }
}

function showLayer(Id,point){
        if (document.layers){
                document.layers[Id].visibility = "show";
		document.layers[Id].left = point.x+"px";
		document.layers[Id].top = point.y+"px";
        } else if (document.getElementById){
                document.getElementById(Id).style.visibility = "visible";
		document.getElementById(Id).style.left = point.x+"px";
		document.getElementById(Id).style.top = point.y+"px";
        }
}
function hideLayer(Id){
        if (document.layers){
                document.layers[Id].visibility = "hide";
        } else if (document.getElementById){
                document.getElementById(Id).style.visibility = "hidden";
        }
}

function cart_add(qty_field,sku,f) {
	var qty = qty_field.value;
	if (parseInt(qty) != qty) {
		alert(qty + " is not a positive number");
		qty_field.value = '';
		qty_field.focus();
	} else {
		sendReq('rpc_cart','i='+sku+'&q='+qty+'&f='+f.name.value+'&n='+f.name.value);
		qty_field.value='1';
	}
	return false;
}

function CurrencyFormatted(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

window.size = function() {
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

window.center = function() {
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;

	return{x:_x,y:_y};
}

function toggleVisibility(Id,focus_id) {  
        if( document.getElementById(Id).style.display == 'none' ) {
                document.getElementById(Id).style.display = 'block';
		document.getElementById(focus_id).focus();
        } else {
                document.getElementById(Id).style.display = 'none';
		document.getElementById('s').focus();
        }
}

