/*
window.onload = function() {
	resize_content();
}

window.onresize = function() {
	resize_content();
}
*/

function resize_content() {
	var windowHeight = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
	var windowWidth = window.innerWidth ? window.innerWidth : document.body.offsetWidth;
	var badHeight = getEl('logowrap').offsetHeight + getEl('bottomMenu').offsetHeight + 30;
	getEl('mainContent').style.height = (windowHeight - badHeight) + 'px';
	getEl('mainContentWrap').style.width = (windowWidth - 425) + 'px';
}

function getEl(el) {
	return document.all ? document.all[el] : document.getElementById(el);
}

function is_integer(maybe_num) {
	if (/^\d+$/.test(maybe_num)) {
		return true;
	}
	return false;
}

function add_to_cart(item_id) {
	var quantity = getEl('qty_' + item_id).value;
	if (is_integer(item_id) && is_integer(quantity) && quantity <= 100) {
		var url = 'Add_To_Cart_Ajax.php';
		ajax(url, handle_add_to_cart, { params: { item_id: item_id, quantity: quantity }});
		show_popup("Adding items to cart...");
	}
	else {
		alert('You must input an integer value less than 100.');
	}
}

function remove_cart_item(item_id) {
	if (is_integer(item_id)) {
		url = 'Remove_Cart_Item.php';
		ajax(url, handle_remove_cart_item, { params: { item_id: item_id }});
		show_popup("Please wait...");
	}
}

function mark_order_sent(order_id) {
	var url = 'Mark_Order_Sent_Ajax.php';
	ajax(url, handle_order_sent, { params: { order_id: order_id }});
	show_popup("Marking order as sent...");
}

function handle_order_sent(req) {
	hide_popup();
	location.reload(true);
}

function handle_remove_cart_item(req) {
	var response = req.responseText;
	if (response == 'OK') {
		hide_popup();
		update_shopping_cart();
	}
	else {
		var data = response + '<br><input type="submit" value="OK" onclick="javascript:hide_popup(); return false;"/>';
		show_popup(data);	
	}
}

function handle_add_to_cart(req) {
	var response = req.responseText;
	if (response == 'OK') {
		hide_popup();
		update_shopping_cart();
	}
	else {
		var data = response + '<br><input type="submit" value="OK" onclick="javascript:hide_popup(); return false;"/>';
		show_popup(data);	
	}
}

function update_shopping_cart() {
	ajax('cart.php', handle_cart_update);
}

function handle_cart_update(req) {
	getEl('shopping_cart').innerHTML = req.responseText;
}

function show_popup(text) {
	show_big_blank_div();
	if (getEl('popup')) {
		getEl('popup').innerHTML = text;
		getEl('popup').style.display = '';
		return;
	}
	var width = 300;
	var height = 50;
	var windowWidth = getWidth();
	var windowHeight = document.body.offsetHeight ? document.body.offsetHeight : document.body.document.height;
	var x = (windowWidth / 2) - (width / 2);
	var y = (windowHeight / 2) - (height / 2);
	var new_div = document.createElement('div');
	new_div.style.backgroundColor = '#ccc';
	new_div.style.color = '#000';
	new_div.style.fontWeight = 'bold';
	new_div.style.fontSize = '16px';
	new_div.style.position = 'absolute';
	new_div.style.zIndex = '100002';
	new_div.style.border = '1px solid #555';
	new_div.style.width = width + 'px';
	new_div.style.height = height + 'px';
	new_div.style.top = y + 'px';
	new_div.style.left = x + 'px';
	new_div.style.textAlign = 'center';
	new_div.style.paddingTop = '10px';
	new_div.id = 'popup';
	new_div.innerHTML = text;
	document.body.appendChild(new_div);
}

function hide_popup() {
	getEl('big_blank_div').style.display = 'none';
	getEl('popup').style.display = 'none';
}

// Thanks Multiply!

function show_big_blank_div() {
	if (getEl('big_blank_div')) { 
		getEl('big_blank_div').style.display = '';
		return; 
	}
	var new_div = document.createElement('div');
	new_div.style.backgroundColor = '#fff';
	height = document.body.offsetHeight ?  document.body.offsetHeight : document.body.document.height;
	width = getWidth();
	new_div.style.height = height + 'px';
	new_div.style.width= width + 'px';
	new_div.style.position = 'absolute';
	new_div.style.top = '0px';
	new_div.style.left = '0px';
	new_div.style.zIndex = '100001';
	new_div.id = 'big_blank_div';
	set_opacity(new_div,60);
	document.body.appendChild(new_div);
}

function set_opacity(el, amount) {
	if (typeof el == 'string') {
		el = getEl(el);
	}
	if (!el || typeof el == 'undefined') {
		return;
	}
	el.style.opacity = amount/100;
	el.style.MozOpacity = amount/100;
	el.style.filter = 'alpha(opacity=' + amount + ')';
}

function ajax(url, callback, options) {
	var method = 'POST';
	if (options && options.method != null) {
		method = options.method;
	}
	var data = options && options.params ? make_param_string(options.params) : null;
	var new_callback;
	if (callback) {
		new_callback = function(req) {
			if (req.readyState != 4) return null;
			if (req.status != 200) return null;
			return callback(req);
		};
	}
	var req = new_ajax_request(options ? options.name : null);
	var async = true;
	if (options && options.async != null) {
		async = options.async;
	}
	if (req) {
		if (new_callback)
		req.onreadystatechange = function() { new_callback(req); };
		req.open(method, url, async);
		if (method == 'POST') {
			req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
			req.setRequestHeader('Content-length', data ? data.length : 0);
			req.setRequestHeader('Connection', 'close');
		}
		req.send(data);
	}
	return req;
}

function make_param_string(params) {
	if (params == null) {
		return null;
	}
	var type = typeof params;
	if (type == 'undefined') {
		return null;
	}
	if (type == 'string') {
		return params;
	}
	var p = [];
	if (params.constructor == Array) {
		for (var i in params) {
			p.push(encodeURIComponent(params[i].name) + "=" + encodeURIComponent(params[i].value));
		}
	}	
	else {	// assume params is an object of key/value pairs
		for (var key in params) {
			var value = params[key];
			if (value && value.constructor == Array) {
				for (var i in value) {
					p.push(encodeURIComponent(key) + "=" + encodeURIComponent(value[i]));
				}
			}
			else {
				p.push(encodeURIComponent(key) + "=" + encodeURIComponent(value));
			}
		}
	}
	if (p.length > 0) {
		return p.join('&').replace(/%20/g, "+");
	}
	else {
		return null;
	}
}

var xml_reqs = new Array();

function new_ajax_request(name) {
	var req = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	if (req) {
		if (!name) {
			name = 'req' + xml_reqs.length;
		}
		xml_reqs[name] = req;
		return req;
	}
	return null;
}

function getWidth() {
	if(typeof(window.innerWidth) == 'number') {
		myWidth = window.innerWidth;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		myWidth = document.documentElement.clientWidth;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		myWidth = document.body.clientWidth;
	}
	return myWidth;
}

function getHeight() {
	if(typeof(window.innerWidth) == 'number') {
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}

