var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) { // automatically tab to the next field when current field is full
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
		input.form[(getIndex(input)+1) % input.form.length].select();
	}
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
				found = true;
		else
			index++;
		return found;
	}
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
		else i++;
		return index;
	}
	return true;
}	

function placeFocus() {
	if (document.forms.length > 0) {
		var field = document.forms[0];
		for (i = 0; i < field.length; i++) {
			if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") ) {
				document.forms[0].elements[i].focus();
				break;
			}
		}
	}
}

function openWindow(mylocation) {
	width = 550;
	height = 500;
	window.open(mylocation,"new", "top=10, left=10, minimize=no, status=no, height=" + height + ",resizable=yes ,scrollbars=yes");	
	//document.location=mylocation;
}

function getCookieVal (offset) { // retrieve the value of cookie, works with GetCookie(name)
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) { // retrieve a cookie
	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;
}

function setCookie (name, value) {  // set a cookie with specified (passed in) parameters
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var largeExpDate = new Date (); // create new date object
	largeExpDate.setTime(largeExpDate.getTime() + (24 * 3600 * 1000));  // set timeout to one day from today
	var expires = (argc > 2) ? argv[2] : largeExpDate;
	var path = (argc > 3) ? argv[3] : '/';
	var domain = (argc > 4) ? argv[4] : '/';
	var secure = (argc > 5) ? argv[5] : false;	
	document.cookie = name + "=" + escape(value) + ";expires="+expires+";path="+path;
}

function emptyCart() {
	setCookie("smmcart",null,23);
	alert("Shopping cart is now empty.");
}
