// 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);

// Show warning message when user changes language?
var show_warning = 0;


// Some basic cookie functions
function getCookieValue (pos) {
	var e=document.cookie.indexOf(";",pos);
	if (e==-1) e=document.cookie.length;
	return (unescape(document.cookie.substring(pos, e)));
}
function getCookie (name) {
	if (document.cookie.length<1) return(null);
	var l=name.length+1, i=0, j=0;
	while (i<document.cookie.length) {
		j=i+l;
		if (document.cookie.substring(i, j)==name+'=') return(getCookieValue(j));
		i = document.cookie.indexOf(" ", i)+1;
		if (i==0) return(null);
	}
	return(null);
}
function setCookie (name,value,expires,path,domain,secure) {
	deleteCookie(name,path,domain);
	document.cookie = name+"="+escape(value)+((expires)?"; expires="+expires.toGMTString():"")+((path)?"; path="+path:"; path=/")+((domain)?"; domain="+domain:"")+((secure)?"; secure":"");
}
function deleteCookie (name,path,domain) {
	if ( getCookie(name) ) { 
		document.cookie = name+"="+((path)?"; path="+path:"; path=/")+((domain)?"; domain="+domain:"")+"; expires=Thu, 01-Jan-70 00:00:01 GMT"; 
		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;
}

// Show warning message if domain is to be changed and the cart has items.
function testLanguageChange() {
	iPos = getCookie("Items");
	if ( iPos == null ) iPos = 0;
	if (show_warning==0 || iPos==0) { return; }
	else { return strWarningChange; }
}
window.onbeforeunload = testLanguageChange;


