// Donberg Electronics main functions

// Browser check
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

var is_nav   = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_gecko = (agt.indexOf('gecko') != -1);
var is_opera = (agt.indexOf("opera") != -1);
var is_ie    = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_safari= (agt.indexOf("safari") != -1);

// OS check
var is_win  = (agt.indexOf('win')!=-1);
var is_mac  = (agt.indexOf('mac')!=-1);


// Get a specific value from a cookie
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if ( endstr == -1 ) endstr = document.cookie.length;
	return(unescape(document.cookie.substring(offset, endstr)));
}


//  Retrieves cookie of specific name
function getCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;

	while ( i < clen ) {
		var j = i + alen;
		if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));
		i = document.cookie.indexOf(" ", i) + 1;
		if ( i == 0 ) break;
	}

	return(null);
}


// Store cookie
function setCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
							((expires) ? "; expires=" + expires : "") +
							((path) ? "; path=" + path : "") +
							((domain) ? "; domain=" + domain : "") +
							((secure) ? "; secure" : "");
}


// Removes a cookie
function deleteCookie (name,path,domain) {
	if ( getCookie(name) ) {
		document.cookie = name + "=" +
								((path) ? "; path=" + path : "") +
								((domain) ? "; domain=" + domain : "") +
								"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}



// Set focus to search field.
function setFocus() {
	// Set focus to input field
	document.qsearch.query.focus();
	// Activate field if a key is pressed...
	if (is_opera && is_mac) { // Special handling for opera and mac.
		document.onkeypress = autoFocus;
	} else if (is_major>5) {
		document.onkeydown = autoFocus;
	}
}

// Set focus on keypress to search field automatically, if it is not set to another field.
function autoFocus(e) {
	try {
		e = (e)?e:((event)?event:null);
		if (e.target) {
			var target = e.target;
		}
		else {
			var target = e.srcElement;
		}

		if (!e.metaKey && !e.altKey && !e.ctrlKey && isChar(e.keyCode) && (target.nodeName != "INPUT" || (target.type.toLowerCase() != "text" && target.type.toLowerCase() != "password")) && target.nodeName != "TEXTAREA") {
			setFocusPos(document.qsearch.query, document.qsearch.query.value.length);
			document.qsearch.query.value = "";
			document.qsearch.query.focus();
		}
	}
	catch(e) {
		alert(e);
	}
}

// Set Focus to field and select range
function setFocusPos(field, pos)
{
	if (document.selection) {
		field.focus ();			// Set focus on the element  (IE)

		// Create empty selection range
		var selection = document.selection.createRange ();

		// Move selection start and end to 0 position
		selection.moveStart ('character', -field.value.length);

		// Move selection start and end to desired position
		selection.moveStart ('character', pos);
		selection.moveEnd ('character', 0);
// 		selection.select ();
	}
	// Firefox support
	else if (field.selectionStart || field.selectionStart == '0') {
		field.selectionStart = pos;
		field.selectionEnd = pos;
		field.focus ();
	}
}

// Check if key is a letter...
function isChar(k) {
	if (k >= 65 && k <= 90) // A-Z
		return true;
	else if (is_opera && is_mac && k >= 97 && k < 123)
		return true;
	else if (k >= 192)
		return true;
	return k == 0;
}
