var myWidth = 0, myHeight = 0;

//Non-IE
if( typeof( window.innerWidth ) == 'number' ) {
	winWidth = window.innerWidth;
	winHeight = window.innerHeight;
//IE 6+ in 'standards compliant mode'
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	winWidth = document.documentElement.clientWidth;
	winHeight = document.documentElement.clientHeight;
//IE 4 compatible
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	winWidth = document.body.clientWidth;
	winHeight = document.body.clientHeight;
}


function popup(mylink, windowname, myWidth, myHeight){
	if(!window.focus){
		return true;
	}
	
	win_center_y = winHeight / 2;
	win_center_x = winWidth / 2;

	target_y = win_center_y - (myHeight / 2);
	target_x = win_center_x - (myWidth / 2);

	var href;
	
	if(typeof(mylink) == 'string'){
		href = mylink;
	} else {
		href = mylink.href;
	}
	
	popupwindow = window.open(href, windowname, 'width=' + myWidth + ', height=' + myHeight + ', scrollbars=no');
	popupwindow.moveTo(target_x, target_y);
	popupwindow.resizeTo(myWidth, myHeight);
	return false;
}

function initDiv(myObject, myObjWidth, myObjCover){
	if(myObjCover == true){
		initCover();
	}
	
	myObjWidth = myObjWidth / 2;
	var obj = document.getElementById(myObject);
	obj.style.display = 'block';
	obj.style.top = 150 + 'px';
	obj.style.left = (myWidth / 2 - myObjWidth) + 'px';
}

function initCover(){
	var obj = document.getElementById('back_cover');
	obj.style.display = 'block';
	obj.style.width = myWidth + 'px';
	obj.style.height = myHeight + 'px';
}

// load content based on list box selection
function selectItem(url){
	window.location = url;
}

function goToAnchor(pageAnchor){
	window.location.hash = '#'+pageAnchor;
}

// move cursor to specific form field
function putFocus(formInst, elementInst) {
	if (document.forms.length > 0) {
		document.forms[formInst].elements[elementInst].focus();
	}
}

// detect enter button press inside form text input fields, to prevent posting form!
function submitEnter(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13){
		// add product to cart
		add_product_to_cart();
		return false;
	}
	else
		return true;
}

// only allow integers 0-9 in form input fields!
function isNumericKey(e){
	var k = document.all ? e.keyCode : e.which;
	return ((k > 47 && k < 58) || k == 8 || k == 0);
}

function extractNumeric(str){
	return str.replace(/\D/g,"");
}

function limitInteger(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode < 46 || keycode > 57 || keycode == 191 || keycode == 190) {
		window.event.keyCode = 0;
		return true;
	}
	else
		return true;
}

// dynamic content loader
function javaLoader(url,id) {
	// show loading message
	initLoading_msg();

	if (document.getElementById) {
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
		
	if (x) {
		x.onreadystatechange = function() {
		
			if (x.readyState == 4 && x.status == 200) {
				el = document.getElementById(id);
				el.innerHTML = x.responseText;
				// hide loading message
				setTimeout('hideLoading_msg()', 800);
			}
		}
		
		x.open("GET", url, true);
		x.send(null);
	}
}

// toggle between two div layers
var currLayerId = "Layer1"; 

function togLayer(id) { 
	if(currLayerId){
		setDisplay(currLayerId, "none");
	}
	if(id){
		setDisplay(id, "block");
	}
	currLayerId = id; 
} 

function setDisplay(id,value) { 
	var elm = document.getElementById(id); 
	elm.style.display = value; 
}

// toggle any div layer between hidden & visable
function toggleObj(id){
	var elm = document.getElementById(id); 
	if(elm.style.display = 'none'){
		setDisplay(id, 'block');
	} else if(elm.style.display = 'block'){
		setDisplay(id, 'none');
	}
}

// show div layer
function showObj(myObject){
	setDisplay(myObject,"block");
}

// hide div layer
function hideObj(myObject){
	setDisplay(myObject,"none");
}

function showhide(layerID, mode){
		var currentRef = document.getElementById(layerID).style
		//Display modes 1 for on 0 for off
		modes = new Array
		modes[0] = 'none'
		modes[1] = 'block'
	if(isNaN(mode))
		currentRef.display = (currentRef.display == 'none') ? 'block' : 'none'
	else
		currentRef.display = modes[mode]
}