// BEGIN random image display script
// Set up the image files to be used.
var theImages = new Array() // do not change this

// To add more image files, continue with the
// pattern below, adding to the array.
theImages[0] = 'images/customerlogo_byerlys.gif'
theImages[1] = 'images/customerlogo_farmfresh.gif'
theImages[2] = "images/customerlogo_giant.gif";
theImages[3] = "images/customerlogo_hyvee.gif";
theImages[4] = "images/customerlogo_meijer.gif";
theImages[5] = "images/customerlogo_pharmaca.gif";
theImages[6] = "images/customerlogo_raleys.gif";
theImages[7] = "images/customerlogo_smiths.gif";
theImages[8] = "images/customerlogo_ukrops.gif";
theImages[9] = "images/customerlogo_wildoats.gif";
theImages[10] = "images/customerlogo_fredmeyer.gif";
theImages[11] = "images/customerlogo_safeway.gif";
theImages[12] = "images/customerlogo_wholefoods.gif";
theImages[13] = "images/customerlogo_bi-lo.gif";
theImages[14] = "images/customerlogo_martins.gif";
theImages[15] = "images/customerlogo_biggs.gif";
theImages[16] = "images/customerlogo_longsdrugs.gif";
theImages[17] = "images/customerlogo_heb.gif";
theImages[18] = "images/customerlogo_publix.gif";
theImages[19] = "images/customerlogo_revolution.gif";
theImages[20] = "images/customerlogo_singapore.gif";
theImages[21] = "images/customerlogo_albertsons.gif";
theImages[22] = "images/customerlogo_harristetter.gif";

var old = 0;
var current = 0;

// start the function, this is called in the BODY tag with onload="randomImageInit()"
function randomImageInit() {
	if (!document.images) return
	while (current == old) {
		current = Math.floor(Math.random()*theImages.length);
	}
	old = current;
	document.images['image'].src = theImages[current];
	// the numerical value at the end (default is 5000) is the time
	// between swapping images, 5000 = 5 seconds.
	setTimeout('randomImageInit()',5000);
}

/* 
in order for the above to work you have to place the following into your page
where you want the code to appear.

<img name="image"><br>

note, you can put a src="image.gif" in there for browsers that don't support
document.images and it will  simply display the first photo and nothing more, 
though modern IE still briefly shows that image, so it isn't in the code.
The name="image" has to stay that way since 'image' is being called in the 
script above.
*/

/*
causing problems with external libraries!
Object.prototype._repr = function() {
	var s = '';
	var k = '';
	var v = '';
	for ( k in this ) {
		if ( (v = this[k]) !== null ) {
			if ( typeof v == 'function' ) {
				s += k + ': [function]\n';
			} else if ( typeof v == 'object' ) {
				s += k + ': [object]\n';
			} else {
				s += k + ': ' + v + '\n';
			}
		} else {
			s += k + ': [null]\n';
		}
	}
	return s;
}*/

String.prototype.repeat = function(count) {
	var s = this;
	for ( var i = 0; i < count; i++ ) {
		s += s;
	}
	return s;
}

function getCookie(name) {
	var dc = document.cookie;
	name = name + '=';
	var s = dc.indexOf('; ' + name);
	s = (s > -1) ? (s + 2) : dc.indexOf(name);
	if (s < 0) {
		return null;
	}
	var e = dc.indexOf(';', s);
	if (e == -1) {
		e = dc.length;
	}
 	return unescape(dc.substring(s + name.length, e));
}

function findField(name, where) {
	var current, i, x;
	if (!where) where = document;
	if (!(x = where[name]) && where.all) {
		current = where.all[name];
	}

	// search through forms
	for (i = 0; !current && i < where.forms.length; i++) {
		current = where.forms[i][name];
	}

	// search through layers (divs)
	for(i = 0; !current && where.layers && i < where.layers.length; i++) {
		current = findField(name, where.layers[i].document);
	}
	
	return current;
}

function findFieldsByPattern(pattern, element, level) {
	var s = [];
	// DEBUG: alert('pattern: ' + pattern + '\nelement: ' + element + '\nlevel: ' + level);
	// no valid reference
	if ( !element ) {
		return s;
	}
	// not a DOM object
	if ( !element.nodeType ) {
		return s;
	}
	// safe-guard against too deep nesting
	if ( level > 30 ) {
		return s;
	}
	// see if this element has a matching id
	if ( element.id && element.id.match(pattern) ) {
		// alert(element.id + ': ' + pattern );
		s.push(element);
	}
	// test all children an combine the result arrays
	for( e in element.childNodes ) {
		if ( e != 'item' && e != '_repr' && e != 'length' ) {
			// DEBUG: alert('pattern: ' + pattern + '\nelement: ' + element + '\nlevel: ' + level + '\nchild: ' + e);
			s = s.concat(findFieldsByPattern(pattern, element.childNodes[e], level + 1));
		}
	}
	return s;
}

function focusField(name) {
	var f = findField(name);
	if ( f && f.focus ) {
		f.focus();
	}
	if ( f && f.select ) {
		f.select();
	}
}

function forgotPW(field, page) {
	f = findField(field);
	if (f) {
		if (f.value.length)
			location.href = page + '?e=' + escape(f.value);
		else
			location.href = page;
	}
	else {
		location.href = page;
	}
}

//
function prevpopup(url, width, height) {
	width = width ? width : 1024;
	height = height ? height : 768;
	openWin('mypopup', width, height, url);
}

// Opens new window
function openWin(winName, width, height, url) {
	thisWin = eval('window.' + winName);
	if (thisWin) {
		thisWin = eval(winName);
		if (!thisWin.closed) thisWin.close();
	}
	newStr = 'window.' + winName + ' = window.open(\'' + url + '\', \'' + winName +
		'\', \'resizable=no,width=' + width + ',height=' + height +
		',scrollbars=yes,dependent=yes,menubar=no,titlebar=no,statusbar=no\')';
	eval(newStr);
}


/*
SOME of the following detections NEED to be performed
in the root context AND NOT inside a function
*/

var Detection = {};

// Javascript Version
Detection.jsVersion = '0';
for (var i = 6; i > 0; i--) {
	document.write('<script language="JavaScript1.' + i + '">Detection.jsVersion = ' + i + ';</script>');
	if (Detection.jsVersion != '0') break;
}

// Screen Information
if (self.screen) {
	Detection.screenX = screen.width;
	Detection.screenY = screen.height;
    Detection.color = screen.colorDepth;
} else {
	Detection.screenX = '';
	Detection.screenY = '';
    Detection.color = '';
}

// Browser Language
Detection.language = '';
if (navigator.language) {
	Detection.language = navigator.language.toLowerCase();
} else if (navigator.browserLanguage) {
	Detection.language = navigator.browserLanguage.toLowerCase();
}

// Java
Detection.Java = (navigator.javaEnabled() ? true : false);

// Flash
var flashinstalled = 0;
var flashversion = 0;
var MSDetect = false;
if (navigator.plugins && navigator.plugins.length) {
	var x = navigator.plugins['Shockwave Flash'];
	if (x) {
		flashinstalled = 2;
		if (x.description) {
			var y = x.description;
			var match = /([0-9]+)/.exec(y);
			flashversion = match ? match[0] : (y.charAt(y.indexOf('.') - 1));
		}
	} else {
		flashinstalled = 1;
	}
	if (navigator.plugins['Shockwave Flash 2.0']) {
		flashinstalled = 2;
		flashversion = 2;
	}
} else if (navigator.mimeTypes && navigator.mimeTypes.length) {
	x = navigator.mimeTypes['application/x-shockwave-flash'];
	if (x && x.enabledPlugin) {
		flashinstalled = 2;
	} else {
		flashinstalled = 1;
	}
} else {
	MSDetect = true;
}
if (MSDetect) {
	document.writeln('<script language="VBScript">');
	document.writeln('on error resume next');
	document.writeln('	For ii = 20 to 2 step -1');
	document.writeln('		If Not(IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & ii))) Then');
	document.writeln('		Else');
	document.writeln('			flashinstalled = 2');
	document.writeln('			flashversion = ii');
	document.writeln('			Exit For');
	document.writeln('		End If');
	document.writeln('	Next');
	document.writeln('</SCRIPT>');
}
Detection.Flash = (flashinstalled == 2 ? flashversion : (flashinstalled == 1 ? '0' : '-1') );

function initJS() {
	// alert(Detection._repr());
}

function setLoginPanel() {
	var name = getCookie('hniusr');
	var notInBox = document.getElementById('loginBoxNotLoggedIn');
	var inBox = document.getElementById('loginBoxLoggedIn');
	var unH = document.getElementById('liUserNameH');
	var un = document.getElementById('liUserName');
	if (notInBox && notInBox.style && inBox && notInBox.style) {
		if (name && name.length > 1) {
			inBox.style.display = 'block';
			notInBox.style.display = 'none';
			if (unH)
				unH.innerHTML = name;
			if (un)
				un.innerHTML = name;
		} else {
			inBox.style.display = 'none';
			// notInBox.style.display = 'block';
		}
	}
}

// stuff that can only be done AFTER the entire page loaded
function initDOM() {
	setLoginPanel();
	FlashMovies.loadMovies();
}

function submitForm(formId) {
	var thisForm = document.forms[formId];
	if (thisForm && thisForm.submit) {
		thisForm.submit();
	}
}

function updateCountryCallCode(id) {
	var ccField = document.getElementById('countrycallingcode');
	if (ccField && ccField.innerHTML && Countries[id]) {
		ccField.innerHTML = ' +' + Countries[id].call;
	}
}


function focusLoginForm() {
	var el_email = document.getElementById('Email');
	var el_password = document.getElementById('Password');
	var el_remember = document.getElementById('remember');
	if (el_remember && el_remember.checked) {
		if (el_password && el_password.focus) {
			el_password.focus();
		}
	} else {
		if (el_email && el_email.focus) {
			el_email.focus();
		}
	}
}

function echo(list) {
    var el = null;
	for (i = 0; i < list.length; i++) {
		el = list[i];
		if (el) {
			document.write(el);
		}
	}
}

function echo_m(e_list, subject) {
	document.write('<a href="mailto:');
	echo(e_list);
	if (subject) {
		document.write('?subject=' + subject);
	}
	document.write('">');
	echo(e_list);
	document.write('</a>');
}

function scheduleImageRefresh(id) {
	window.setTimeout('performImageRefresh(' + id + ');', 2000);
}

function performImageRefresh(id) {
	var img = null;
	for (var i = 0; i < document.images.length; i++) {
		img = document.images[i];
		src = img.src;
		if (src.indexOf('/' + id + '.') > -1) {
			break;
		}
		img = null;
	}
	if (img) {
		img.src = img.src + '?rn=' + Math.floor( Math.random() * 1000000 );
	}
}

function setbg(el, color) {
	if (el && el.style) {
		el.style.backgroundColor = color;
	}
}

function paintFlash(id, movie, width, height, minVersion, flashvars) {
	if (Detection.Flash < minVersion) {
		return false;
	}
	var element = document.getElementById(id);
	if (flashvars == undefined) {
		flashvars = '';
	}
	if (element && element.innerHTML) {
		element.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +
			' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' +
			minVersion + ',0,0,0"' +
			' width="' + width + '" height="' + height + '">' +
			' <param name="movie" value="' + movie + '" />' +
			' <param name="quality" value="high" />' +
			' <param name="wmode" value="transparent" />' +
			' <param name="FlashVars" value="' + flashvars + '" />' +
			' <embed src="' + movie + '" quality="high" width="' + width +
			'" height="' + height +'" flashvars="' + flashvars +
			'" wmode="transparent" type="application/x-shockwave-flash"' +
			' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>' +
			'</object>';
		return true;
	} else {
		// do nothing
	}
	return false;
};

var FlashMovies = new Object();
FlashMovies.movies = {};
FlashMovies.registerMovie = function(movie) {
	this.movies[movie.id] = movie;
}
FlashMovies.loadMovies = function() {
	var name;
	var mov;
	for (name in this.movies) {
		mov = this.movies[name];
		if (typeof mov == 'object') {
			if (mov.video) {
			    var flashvars = '&MM_ComponentVersion=1&' +
			    	'skinName=/resources/Halo_Skin_3' +
			    	'&streamName=' + mov.video +
			    	'&autoPlay=' + (mov.noAutoPlay ? 'false' : 'true') +
                    '&autoRewind=false';
				paintFlash(
					mov.id, mov.movie, mov.width, mov.height, mov.minVersion,
					flashvars
				);
			} else {
				paintFlash(
					mov.id, mov.movie, mov.width, mov.height, mov.minVersion
				);
			}
		}
	}
}

function MovieRequest(id, movie, width, height, video, minPlayer, noAutoPlay) {
	this.minVersion = minPlayer || 8;
	this.id = id;
	this.movie = movie;
	this.width = width;
	this.height = height;
	this.noAutoPlay = noAutoPlay || false;
	this.video = video || false;
};

var MC = {
	list: {},
	timeout: 2500,
	
	showMenu: function(element) {
		this.hideAllOthers();
		var e = document.getElementById(element);
		if (e) {
			var menu;
			if (this.list[element]) {
				menu = this.list[element];
			} else {
				menu = { element: e, display: false, interval: 0 };
				this.list[element] = menu;
			}
			if (!menu.display) {
				menu.element.style.display = 'block';
			}
		}
	},
	
	hideAllOthers: function() {
		var menu;
		for(element in this.list) {
			this.hideMenu(element);
		}
	},
	
	hideMenu: function(menu) {
		if (!menu.element) {
			menu = this.list[menu];
		}
		if (menu && menu.element) {
			window.clearInterval(menu.interval);
			menu.interval = 0;
			menu.element.style.display = 'none';
		}
	},
	
	scheduleHideMenu: function(element) {
		var menu;
		if (menu = this.list[element]) {
			window.clearInterval(menu.interval);
			menu.interval = window.setInterval(
				'MC.hideMenu(\'' + element + '\');',
				this.timeout
			);
		}
	}
	
};

function findAll(pattern, start) {
	var elements = [];
	if (start.id && start.id.match(pattern)) {
		elements.push(start);
	}
	if (start.childNodes) {
		for(c in start.childNodes) {
			var more = findAll(pattern, start.childNodes[c]);
			for(var i = 0; i < more.length; i++) {
				elements.push(more[i]);
			}
		}
	}
	return elements;
}


name = 'main';

function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

