	/* --- COMMON JS FUNCTIONS --- */

	function setTextInTextField(id, text, deftext) {
		if ($('#' + id).val() == '' || $('#' + id).val() == deftext) 
			$('#' + id).val(text);
	}

	var tabs = ['details', 'accessories', 'suits-for', 'related', 'packages', 'bundling'];
	function switchTab(name) {
		var basedir = '/images/tab/';
		var sufix = 'tab-';
		
		for (i = 0; i <= tabs.length; i++) {
			if (tabs[i] == name) {
				if (document.getElementById(sufix + tabs[i])) {
					document.getElementById(sufix + tabs[i]).src = basedir + tabs[i] + '-hover.gif';
					document.getElementById(tabs[i]).style.display = 'block';
				}
			}
			else {
				if (document.getElementById(sufix + tabs[i])) {
					document.getElementById(sufix + tabs[i]).src = basedir + tabs[i] + '.gif';
					document.getElementById(tabs[i]).style.display = 'none';
				}
			}
		}
	}

	function setItemsPerPage() {
		var selectObj = document.getElementById('id_items_per_page');
		var defaultVal = 'lim=' + selectObj.options[selectObj.selectedIndex].value;
		var curAction = document.getElementById('id_sort_form').action;
		var re = /&lim=([^&$]+)/;
		curAction = curAction.replace(re, "");
		curAction += '&' + defaultVal;
		
		document.location = curAction;
	}

	// SEARCH PAGE Functions
	function searchPerPage(obj) {
		var value = obj.options[obj.selectedIndex].value,
			url = String(document.location.href);
				
		document.location = url.replace(/\&?per_page=[^&$]+/, '').replace(/page=\d+/, 'page=1').concat('&per_page=' + value);
	}

	function searchFilterInStock(value) {
		var url = String(document.location.href).replace(/\&in_stock=(1|0)/, '').replace(/page=\d+/, 'page=1');	
	
		if (value == 1) {
			url += '&in_stock=1';
		}
		
		document.location = url;
	}

	function searchAddCategory(id) {
		var url = document.location.href;
		
		url = url.replace(/page=\d+/, 'page=1');
		url += '&category[]=' + id;		
		
		document.location = url;
	
		return false;
	}

	function searchRemoveCategory(id) {
		var url = decodeURI(document.location.href), 
			r = new RegExp("&category\\[\\d*\\]=" + id);
			
		url = url.replace(r, '').replace(/page=\d+/, 'page=1');
		document.location = url;
		
		return false;
	}

	function searchRemoveAllCategories() {
		var url = decodeURI(document.location.href),
			r = new RegExp("&category\\[\\d*\\]=\\d+", 'g');	
			
		url = url.replace(r, '').replace(/page=\d+/, 'page=1');
		document.location = url;
		
		return false;	
	}

	function searchSort(obj) {
		var value = obj.options[obj.selectedIndex].value,
			url = String(document.location.href);
			
		document.location = url.replace(/\&?order_by=[^&$]+/, '').concat('&order_by=' + value);
	}

	function setOrderBy() {
		var selectObj = document.getElementById('id_orderBy');
		var defaultVal = 'order_by=' + selectObj.options[selectObj.selectedIndex].value;
		var curAction = document.getElementById('id_sort_form').action;
		var re = /&order_by=(([^&$]+))/;
		curAction = curAction.replace(re, "");
		curAction += '&' + defaultVal;
		
		document.location = curAction;
	}

	function showInStock(param) {
		var defaultVal = 'in_stock=' + param;
		var curAction = document.getElementById('id_sort_form').action;
		var re = /&in_stock=([^&$]+)/;
		
		curAction = curAction.replace(re, "");
		curAction += '&' + defaultVal;
		
		document.location = curAction;
	}

	function subscribtionProcess(url, id, place) {
		
		$.get(url + '/subscribe/check/', {email: $('#' + id).val()}, function(data, textStatus) {
			var res = data.split(";");
			var status = res[0];
			var message = res[1];

			if (status == '1') {
				if (place == 'home') {
					message = '<p><img title="THANK YOU" alt="THANK YOU" src="/images/hl/hl-subscribe-thankyou.png"/><br /><br />Please add <a href="mailto:bpav@email.bpav.com">bpav@email.bpav.com</a> to your address book to ensure successful delivery.</p> <p>' + message;
				}
				else {
					message = ' <p><span class="red-bold-text">Thank You!</span> Please add <a href="mailto:bpav@email.bpav.com">bpav@email.bpav.com</a> to your address book to ensure successful delivery. ' + message;
				}
				$('#id_signup_block').html(message);
			} else {
				$('#id_signup_error').html(message);
			}
		});
		
		return false;
	}

	
	// ajax request
	function ajaxRequest(url, func) {
		if (window.XMLHttpRequest) {
			request = new XMLHttpRequest();
			if (request) {
				request.onreadystatechange = function() {
					func(request);
				}
				request.open("GET", url, true);
				request.send(null);
			}
		}
		else 
			if (window.ActiveXObject) {
				request = new ActiveXObject("Microsoft.XMLHTTP");
				if (request) {
					request.onreadystatechange = function() {
						func(request);
					}
					request.open("GET", url, true);
					request.send();
				}
			}
	}
