// return object containing all get varaibles
function get()
{
	var qs = location.search.substring(1);
	var nv = qs.split('&');
	var url = new Object();
	for(i = 0; i < nv.length; i++) {
		var eq = nv[i].indexOf('=');
		url[nv[i].substring(0,eq).toLowerCase()] = unescape(nv[i].substring(eq + 1));
	}
	return url;
}

function go(url)
{
	window.location = url;
}

function repaint(el)
{
	el = el || document.body;
	el.className = el.className;
	var disp = el.style.display;
	el.style.display = 'none';
	var h = el.offsetHeight;
	el.style.display = disp;
}

function $()
{
	var elements = new Array();

	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') {
			element = document.getElementById(element);
		} if (arguments.length == 1) {
			return element;
		}
		elements.push(element);
	}

	return elements;
}

function add_node(parent, name, content)
{
	/* create element first */
	var node = document.createElement(name);

	/* create content node if required */
	if (typeof(content) == 'object' && content && content.nodeType) {
		node.appendChild(content);
	} else if (typeof content != 'undefined') {
		add_text(node, content);
	}

	/* set attributes */
	if (arguments.length > 3) {
		if ((arguments.length % 2) === 0) {
			return null;
		}

		for (var i=3; i<arguments.length; i+=2) {
			node.setAttribute(arguments[i], arguments[i+1]);
		}
	}

	return parent.appendChild(node);
}

function add_text(parent, text)
{
	var node = document.createTextNode(text);
	return parent.appendChild(node);
}

function get_text(node)
{
	var s = "";
	if (node.childNodes) {
		var nodes = node.childNodes;
		for (var i = 0; i < nodes.length; i++) {
			if (nodes[i].nodeType == 3) {
				s += nodes[i].data;
			}
		}
	}
	return s;
}

function rem_nodes(parent)
{
	while (parent.lastChild)
	parent.removeChild(parent.lastChild);
}

function highlight_input(el)
{
	el.style.border = "1px solid red";
	el.style.background = "#ffeeee";
}

function hide_element(id)
{
	$(id).style.display = "none";
}

function ask_show(b)
{
	var el = $('ask-question-form');
	if (b) {
		el.style.display = 'block';
	} else {
		el.style.display = 'none';
	}
	repaint();
}

function ask_switch()
{
	var el = $('ask-question-form');
	ask_show(true);
	location.href = "#ask-question-form";
	return false;
}

function update_account_inputs()
{
	var form = $('checkout1');
	form.className = 'form form-checkout-' + form.useaccount.value;
}

function update_checkout_form()
{
	var form = $('checkout2');
	form.update.value = "1";
	form.submit();
}

// switch language by reloading current page with lang attribute changed to
// code
function switch_lang(code)
{
	var nqsa = new Array(0);
	var lang_found = false;

	var qs = location.search.substring(1);
	var nv = qs.split('&');
	for (i=0; i<nv.length; i++) {
		if (nv[i].length === 0) {
			continue;
		}
		var eq = nv[i].indexOf('=');
		var name = nv[i].substring(0,eq).toLowerCase();
		if (!lang_found && name == 'lang') {
			nqsa.push('lang='+escape(code));
			lang_found = true;
		} else {
			nqsa.push(nv[i]);
		}
	}

	if (!lang_found)  {
		nqsa.push('lang='+escape(code));
	}

	var nqs = '?'+nqsa.join('&');
	window.location = nqs;
}

function logout()
{
	var nqsa = new Array(0);
	var lo_found = false;
	var qs = location.search.substring(1);
	var nv = qs.split('&');
	for(i=0; i<nv.length; i++)
	{
		if (nv[i].length === 0)
		continue;
		var eq = nv[i].indexOf('=');
		var name = nv[i].substring(0,eq).toLowerCase();
		if (!lo_found && name == 'logout') {
			nqsa.push('logout=1');
			lo_found = true;
		} else {
			nqsa.push(nv[i]);
		}
	}

	if (!lo_found) {
		nqsa.push('logout=1');
	}

	var nqs = '?'+nqsa.join('&');
	window.location = nqs;
}

function highlight_invalid(el, valid)
{
	if (valid) {
		el.style.backgroundColor="white";
	} else {
		el.style.backgroundColor="#ffaaaa";
	}
}

function check_regex(el, regex, req)
{
	highlight_invalid(el, el.value.match(regex) || (el.value === "" && !req));
}

function check_email(el, req)
{
	check_regex(el, /^[a-zA-Z0-9\-\.]+@[a-zA-Z0-9\-\.]+$/, req);
}

function check_phone(el, req)
{
	check_regex(el, /^(\+)?[-0-9(),\ ]{3,}$/, req);
}

function check_isbn(el, req)
{
	check_regex(el, /^[0-9\-]+$/, req);
}

function check_num(el, req)
{
	check_regex(el, /^([0-9](\.[0-9]+)?)+$/, req);
}

function check_dic(el, req)
{
	check_regex(el, /^([a-zA-Z]{2})([0-9]{8,12})$/, req);
}

function check_ico(el, req)
{
	check_regex(el, /^([0-9]{8}|[0-9]{6})$/, req);
}

function check_psc(el, req)
{
	check_regex(el, /^(.{1,10})$/, req);
}

function check_nonempty(el)
{
	highlight_invalid(el, !el.value.match(/^([ ]*)$/));
}

function submit_form_self(n)
{
	var obj = $(n);
	if (!obj) {
		alert('ISE: cant find form '+n);
		return;
	}
	var pageurl = window.location.href;
	obj.action = pageurl;
	obj.submit();
}

