var request = document.location.href;
var request_nqs = request.split('?')[0];

if(Prototype.Browser.IE) {
	Event.observe(window, 'load', function() {
		$$('input[type="checkbox"], input[type="radio"]').each(function(c){
			c.setStyle({cursor: 'default'});
			c.observe('click', function() {this.blur();});
		});
	});
}

function $G(variable) {
	var query = window.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) {
			return pair[1];
		}
	}
	return null;
}

// Limit text box to certain characters. ussage: onkeypress="return chars_only(event, '...');"

var basic_chars = 'abcdefghijklmnopqrstuvwxyz0123456789';

function chars_only(e, chars) {
	e || (e = window.event);
	var code = e.charCode ? e.charCode: e.which;
	
	if(! code || e.metaKey || e.ctrlKey || code == 8 || code == 13) {
		return true;
	} else {
		var ch = String.fromCharCode(code).toUpperCase();
		chars = chars.toUpperCase();
		
		for(var i=0; i<chars.length; i++) {
			if(chars.substr(i, 1) == ch) {
				return true;
			}
		}
		
		return false;
	}
}

// Limit text box to numbers only. ussage: onkeypress="return numbers_only(event);" decimal=true allows decimals

function numbers_only(e, decimal) {
	e || (e = window.event);
	var code = e.charCode ? e.charCode: e.which;
	
	if(! code || e.metaKey || e.ctrlKey || code == 8 || code == 13 || (code == 46 && decimal) || (code >= 48 && code <= 57)) {
		return true;
	} else {
		return false;
	}
}

// Enforces x amount of characters

function max_length(e, box, length) {
	e || (e = window.event);
	var code = e.charCode ? e.charCode: e.which;
	
	return box.value.length < length || ! code ||
		e.metaKey || e.ctrlKey || code == 8 ||
		(box.tagName == 'INPUT' && code == 13) ? true : false;
}

// Toggle css selector with select/check box

function selectize(box, value, select) {
	box = $(box);
	
	if(box)
	{
		var tfunc = function() {
			containers = $$(select);
			if(box.getValue() == value) containers.each(function(ce) {ce.show();});
			else containers.each(function(ce) {ce.hide();});
		};
	
		tfunc.bind(box)();
		box.observe('change', tfunc);
	}
}

// Default "Type this here" for text boxes

function defaultbox(box, form, value, classname, eclass) {
	box = $(box);
	form = $(form);

	if(! box.value && ! box.hasClassName(eclass)) {
		box.value = value;
		box.has_default = true;
		
		box.observe('focus', (function() {
			this.clear();
			this.has_default = false;
			this.removeClassName(classname);
			this.stopObserving('focus');
		}));
		
		if(form) {
			form.observe('submit', function() {
				if(box.has_default) box.clear();
			});
		}
		
		box.addClassName(classname);
	}
}

// Function for toggling state drop down with province textbox using country drop down

function country_state(country, state, state_row, province_row, sindex) {
	country = $(country);
	
	if(typeof sindex == 'undefined') sindex = 0;
	
	var csel = function() {
		if(this.options[this.selectedIndex].value == 'US') {
			$(province_row).hide();
			$(state_row).show();
		} else {
			$(state_row).hide();
			$(province_row).show();
			$(state).selectedIndex = sindex;
		}
	};
	
	country.observe('change', csel);
	var csel_now = csel.bind(country);
	csel_now();
}

// Using select box to populate another select box

function select_filter(filter, selectbox, keep, loading, url) {
	filter = $(filter);
	selectbox = $(selectbox);
	loading = $(loading);
	
	if(filter) {
		var keep_options = [];
		
		if(keep > selectbox.options.length) {
			keep = selectbox.options.length;
		}
		
		for(var i=0; i<keep; i++) {
			keep_options[i] = {value: selectbox.options[i].value, text: selectbox.options[i].text};
		}
		
		filter.observe('change', function() {
			var idx = 0;
			selectbox.options.length = 0;
			
			loading.show();
			
			for(var i=0; i<keep_options.length; i++) {
				selectbox.options[idx] = new Option(keep_options[i].text, keep_options[i].value, false, false);
				idx++;
			}
			
			if(filter.multiple) {
				var vals = new Array();
				 
				for(var i=0; i<filter.options.length; i++) if(filter.options[i].selected) {
					vals.push(filter.options[i].value);
				}
				
				newurl = url + escape(vals.join(','));
			} else {
				newurl = url + escape(filter.options[filter.selectedIndex].value);
			}
			
			new Ajax.Request(newurl, {
				method:'get',
				onSuccess: function(transport){
					var response = transport.responseText.toString().evalJSON();

					if(response) {
						response.each(function(opt) {
							selectbox.options[idx] = new Option(opt.text, opt.value, false, false);
							idx++;
						});
					}
					
					loading.hide();
				}
			});
		});
	}
}

// javascript based delete

function jxdelete(obj, linkobj, message) {	
	obj = $(obj);
	linkobj = $(linkobj);
	
	if(obj && linkobj && linkobj.tagName == 'A') {
		var oldbkg = obj.style.backgroundColor;
		obj.style.backgroundColor = '#FFDDDD';
		obj.select('img, a').each(function(img) {img.setOpacity(0.5)});
		
		if(typeof message == 'undefined' || ! message) {
			message = 'Are you sure you want to delete this?';
		}
		
		if(confirm(message)) {
			new Ajax.Request(linkobj.href + '&ajax=1', {
				method:'get',
				onSuccess: function(transport){
					var response = transport.responseText;
					obj.remove();
				}
			});
		} else {
			obj.style.backgroundColor = oldbkg;
			obj.select('img, a').each(function(img) {img.setOpacity(1)});
		}
	}
	
	return false;
}

// Check all boxes

function check_all(cb_all, select)
{
	cb_all = $(cb_all);
	checkboxes = $$(select);
	
	checkboxes.each(function(c) {
		c.observe('change', function() {if(! this.checked) cb_all.checked = false});
	});
	
	cb_all.observe('change', function() {
		checkboxes.each(function(cb) {cb.checked = cb_all.checked});
	});
}
