function getQueryVariable(variable, query) {
	if (!query) {
		query = document.location.search.substring(1);
	}
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			referral = pair[0];
			return pair[1];
		}
	}
	return "";
}

function addOnloadEvent(fnc) {
	if (typeof window.addEventListener !== "undefined") {
		window.addEventListener("load", fnc, false);
	} else if (typeof window.attachEvent !== "undefined") {
		window.attachEvent("onload", fnc);
	} else {
		if (window.onload !== null) {
			var oldOnload = window.onload;
			window.onload = function (e) {
				oldOnload(e);
				window[fnc]();
			};
		} else {
			window.onload = fnc;
		}
	}
}

function attachOnChangeToInputs(formName, fnct){
	var f=document.forms[formName];
	for(var i=0;i<f.elements.length;i++){
		$(f.elements[i]).observe('change', fnct);
	}
}

function showHideElement(divId, toDo) {
	var el = $(divId);
	if (el) {
		if (toDo === 'show') {
			el.show();
		} else if (toDo === 'hide') {
			el.hide();
		} else {
			(el.visible() == true) ? el.hide() : el.show();
		}
	}
}

function sincronizeSelects(formName, s1, s2, def) {
	formObj = document.forms[formName];
	s1 = formObj[s1];
	s2 = formObj[s2];
	s2.options[s1.selectedIndex + def].selected = true;
}

function restrictCharacters(myfield, e, restrictionType) {
	
	if (restrictionType == "digitsOnly") {
		restrictionType = /[1234567890]/g;
	} else if (restrictionType == "integerOnly") {
		restrictionType = /[0-9\.]/g;
	} else if (restrictionType == "alphaOnly") {
		restrictionType = /[A-Z]/g;
	} else {
		return false;
	}

	
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);
	
	// if they pressed ESC... remove focus from field...
	if (code==27) { myfield.blur(); return false; }

	// ignore if they are press other keys
	// strange because code: 39 is the down key AND ' key...
	// and DEL also equals .
	if (!e.ctrlKey && code!=9 && code!=8 && code!=36 && code!=37 && code!=38 && (code!=39 || (code==39 && character=="'")) && code!=40) {
		if (character.match(restrictionType)) {
			return true;
		} else {
			return false;
		}
	}
}

function limitTextInput(field, maxLen) {
	if (field.value.length > maxLen) {
		field.value = field.value.substring(0, maxLen);
	}
}

function limitCheckInGroup (idForm, selObj, grpName) {
	selObjCheckStatus = selObj.checked;
	form = $(idForm);
	elements = form.getInputs('checkbox', grpName);
	elements.each(function(e, index) {
		e.checked = false;
	});
	selObj.checked = selObjCheckStatus;
}

var changeLinksGroupingOldGroup = 0;
function changeLinksGrouping(newGroup, id) {
	tmp1 = $(id + 'Group' + changeLinksGroupingOldGroup);
	tmp2 = $(id + 'Group' + newGroup);
	if (tmp1 && tmp2) {
		tmp1.style.display = "inline";
		tmp2.style.display = "none";
		tmp1 = tmp2 = null;
		for (i = changeLinksGroupingOldGroup * 10;i <= (changeLinksGroupingOldGroup * 10 + 10);i++) {
			tmp1 = $(id + 'Page' + i);
			if (tmp1) {
				tmp1.style.display = 'none';
			}
		}
		for (i = newGroup * 10;i < (newGroup * 10 + 10);i++) {
			tmp2 = $(id + 'Page' + i);
			if (tmp2) {
				tmp2.style.display = 'inline';
			}
		}
		changeLinksGroupingOldGroup = newGroup;
	} else {
		return;
	}
}

if (Object.isUndefined(Bikie)) { var Bikie = { } }
if (Object.isUndefined(Bikie.Commons)) { Bikie.Commons = Class.create(); }
Bikie.Commons.ClickTracking = Class.create({
	clicks: 0,
	recordedDataWidth: [],
	recordedDataMouseX: [],
	recordedDataMouseY: [],
	recordedDataTime: [],
	recordedDataPage: [],
	initialize: function(options) {
		this.options = {
			trackingURL: 'clickTracking.php',
			sendOnClicks: 5
		}
		
		Object.extend(this.options, options || {});
	},
	
	startRecording: function () {
		// observe clicks
		this.recordClick = this.recordClick.bindAsEventListener(this);
		Event.observe(document, "click", this.recordClick);
		
		this.beforeUnloadSendData = this.beforeUnloadSendData.bindAsEventListener(this);
		Event.observe(window, "unload", this.beforeUnloadSendData);
	},
	
	recordClick: function (e) {
		
		this.recordedDataWidth.push(document.viewport.getWidth());
		this.recordedDataMouseX.push(Event.pointerX(e));
		this.recordedDataMouseY.push(Event.pointerY(e));
		
		page = window.location.pathname.substring(1);
		page = page ? page : "index.php";
		this.recordedDataPage.push(page);
		
		date = new Date();
		date = (date.getFullYear())+'-'+(1+date.getMonth())+'-'+date.getDate()+' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
		this.recordedDataTime.push(date);
		
		this.clicks++;
		if(this.clicks >= this.options.sendOnClicks) {
			this.sendRecordedData();
		}
	},
	
	beforeUnloadSendData: function  () {
		this.sendRecordedData();
	},
	
	sendRecordedData: function () {
		
		parameters  = 'recordedDataTime='+this.recordedDataTime.toJSON();
		parameters += '&recordedDataPage='+this.recordedDataPage.toJSON();
		parameters += '&recordedDataWidth='+this.recordedDataWidth.toJSON();
		parameters += '&recordedDataMouseX='+this.recordedDataMouseX.toJSON();
		parameters += '&recordedDataMouseY='+this.recordedDataMouseY.toJSON();
		parameters += '&clicks='+this.clicks;
		var opt = {
			method: 'post',
			parameters: parameters,
			onSuccess: function(t) {
				//alert(t.responseText);
			}
		}
		new Ajax.Request(this.options.trackingURL, opt);
		// reset everything
		this.recordedDataTime = [];
		this.recordedDataPage = [];
		this.recordedDataWidth = [];
		this.recordedDataMouseX = [];
		this.recordedDataMouseY = [];
		this.clicks = 0;
	}
});

function limitCheckInGroup (form, selObj, grpName) {
	selObjCheckStatus = selObj.checked;
	form = $(form);
	elements = form.getInputs('checkbox', grpName);
	elements.each(function(e, index) {
		e.checked = false;
	});
	selObj.checked = selObjCheckStatus;
}
function changeHeaderSearchTab (tabToActivate) {
	if (tabToActivate == 'searchHeaderTabBikes') {
		$('searchHeaderTabBikeComponents').removeClassName('searchHeaderTabActive');
		$('searchHeaderTabBikeComponents').addClassName('searchHeaderTabRightInactive');
		$('searchHeaderTabBikes').removeClassName('searchHeaderTabLeftInactive');
		$('searchHeaderTabBikes').addClassName('searchHeaderTabActive');
		$('headerSearchBikesFormContainer').show();
		$('headerSearchBikeComponentsFormContainer').hide();
	} else {
		$('searchHeaderTabBikes').removeClassName('searchHeaderTabActive');
		$('searchHeaderTabBikes').addClassName('searchHeaderTabLeftInactive');
		$('searchHeaderTabBikeComponents').removeClassName('searchHeaderTabRightInactive');
		$('searchHeaderTabBikeComponents').addClassName('searchHeaderTabActive');
		$('headerSearchBikeComponentsFormContainer').show();
		$('headerSearchBikesFormContainer').hide();
	}
}