if(window.ActiveXObject) var httpObject = new ActiveXObject('Microsoft.XMLHTTP');
else if(window.XMLHttpRequest) var httpObject = new XMLHttpRequest();
else alert('This application cannot function because your browser does not support Ajax');

function ajax(script, data, method) // ajax( script, data, method [ , async, callback ] ) -- use callback 'function(result)' to return result
{
	method = method.toUpperCase();
	
	if(method == 'GET')
	{
		script += '?' + data;
		data = null;
	}
	
	var async;
	arguments[3] ? async = arguments[3] : async = false;
	
	var callback;
	arguments[4] ? callback = arguments[4] : callback = false;
	
	httpObject.open(method, script, async);
	if(method == 'POST') httpObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	
	httpObject.send(data);
	
	if(async)
	{
		httpObject.onreadystatechange = function()
		{
			if(httpObject.readyState == 4 && httpObject.status == 200)
			{
				result = httpObject.responseText;
				if(callback) if(!setTimeout(callback, 0)) alert('AJAX callback error.');
			}
		}
	}
	else
	{
		return httpObject.responseText;
	}
}

window.onload = function()
{
	updateMiniCart();
}

function getCategories()
{
	var categories = [];
	if(get('categories')) categories = get('categories').split(',');
	return categories;
}

function filterCategories(id)
{
	var categories = getCategories();
	categories.push(id);
	
	var update = [];
	update['categories'] = categories.join(',');
	
	updateURL(update);
}

function unFilterCategories(id)
{
	var categories = getCategories();
	
	var outArr = [];
	for(var i in categories) if(categories[i] != id) outArr.push(categories[i]);
	
	var update = [];
	update['categories'] = outArr.join(',');
	
	updateURL(update);
}

function getAll()
{
	var out = [];
	
	var a = window.location.href.split('?');
	
	if(a.length == 1) return out;
	else var b = a[1].split('&');
	
	for(var i in b)
	{
		var c = b[i].split('=');
		if(c.length == 1) c.push('');
		out[c[0]] = c[1];
	}
	
	return out;
}

function get(a)
{
	var arr = getAll();
	if(arr[a]) return arr[a];
	else return false;
}

function updateURL(a)
{
	var arr = getAll();
	
	for(var i in a) for(var j in arr) if(j == i) arr[j] = a[i];
	
	var outArr = [];
	for(var i in arr) outArr.push(i + '=' + arr[i]);
	var out = outArr.join('&');
	
	var url = window.location.href.split('?')[0];
	if(outArr.length) url += '?' + out;
	
	window.location = url;
}

function validateField(value, type)
{
	if(type == 'text') if(value) return true;
	
	if(type == 'email')
	{
		if(!value) return false;
		var a = value.split('@');
		if(a.length != 2 || !a[0].length || !a[1].length) return false;
		var b = a[1].split('.');
		if(b.length < 2 || !b[0].length || !b[1].length) return false;
		var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@+-_.';
		for(var i = 0; i < value.length; i++) if(chars.indexOf(value.substr(i, 1)) == -1) return false;
		return true;
	}
	
	if(type == 'phone')
	{
		if(!value) return false;
		var chars = '0123456789 +';
		for(var i = 0; i < value.length; i++) if(chars.indexOf(value.substr(i, 1)) == -1) return false;
		return true;
	}

	return false;
}

function sendEmail()
{
	var name = document.getElementById('sendEmailName').value;
	var email = document.getElementById('sendEmailEmail').value;
	var message = document.getElementById('sendEmailMessage').value;
	
	if(!validateField(name, 'text'))
	{
		alert('Please enter your name.');
		return null;
	}
	
	if(!validateField(email, 'email'))
	{
		alert('Please enter a valid email address.');
		return null;
	}
	
	if(!validateField(message, 'text'))
	{
		alert('Please enter your message.');
		return null;
	}
	
	var dataArr = new Array();
		dataArr.push('recaptcha_challenge_field=' + document.getElementById('recaptcha_challenge_field').value);
		dataArr.push('recaptcha_response_field=' + document.getElementById('recaptcha_response_field').value);
		dataArr.push('name=' + name);
		dataArr.push('email=' + email);
		dataArr.push('message=' + message);
	
	var dataStr = dataArr.join('&');
	
	document.getElementById('contactForm').style.display = 'none';
	document.getElementById('contactFormWaiting').style.display = 'inline';
	
	ajax('../send_email.php', dataStr, 'post', true, 'sendEmailCallback(result)');
	
	Recaptcha.reload();
}

function sendEmailCallback(result)
{
	document.getElementById('contactFormWaiting').style.display = 'none';
	document.getElementById('contactForm').style.display = 'inline';
	
	if(result == 'okay')
	{
		document.getElementById('contactForm').style.display = 'none';
		document.getElementById('contactFormSuccess').style.display = 'inline';
	}
	else if(result == 'recaptcha')
	{
		alert('Incorrect reCaptcha input. Please try again.');
	}
	else
	{
		alert('Unexpected error: ' + result);
	}
	
}

function getBrowserSize()
{
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth ) == 'number')
	{
		// Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		// IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		// IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	return new Array(myWidth, myHeight);
}

function setLeftColumnHeight()
{
	var headerHeight = 100;
	var footerHeight = 35;
	var title = 35;
	var padding = 8;
	
	var newHeight = getBrowserSize()[1] - headerHeight - footerHeight - title - (padding * 2);
	
	document.getElementById('leftColumnCategories').style.height = newHeight + "px";
	
	window.onresize = function()
	{
		setLeftColumnHeight();
	}
}

function searchSite()
{
	var q = document.getElementById('searchBox').value;
	
	if(!q) return;
	
	q = q.replace(' ', '+');
	
	window.location = '../shop/search.php?q=' + q;
}
