var imageIndex, img;
var displayMode, blSelectedCore = false;
var ie = document.all;
var nn6 = document.getElementById && !document.all;
var isdrag = false;
var x, y;
var dobj;

function swapImages() {
	var img = getCookie('imgind');
	imageIndex = parseInt('12345678910'.indexOf(img) == -1 ? 1 : img);
	var ambulance = document.all("slide");
	ambulance.src = 'images/slide' + imageIndex + '.jpg';
	setInterval('fnToggle()', 10000);
}

// this function is preset to toggle 10 images
function fnToggle() {
	imageIndex = (imageIndex == 10) ? (1) : (imageIndex + 1);
	document.cookie = 'imgind=' + imageIndex;
	var ambulance = document.all("slide");
	ambulance.filters.item(0).Apply();
	ambulance.src = 'images/slide' + imageIndex + '.jpg';
	ambulance.filters.item(0).Play();
}

function mouseHover() {
	var f, e = window.event;
	f = (e.type == "mouseover") ? ('_f2') : ('');
	e.srcElement.src = "images/btn_" + e.srcElement.buttontag + f + ".gif";
}

function mouseHoverCSS() {
	var e = window.event, g = e.srcElement.className;
	e.srcElement.className = (e.type == "mouseover") ? (g + "_over") : (g.substr(0, g.length - 5));
}

function validateNumeric() {
	var kc = event.keyCode;
	var txt = event.srcElement;
	var bl = kc < 48 || kc > 57 ? false : true;
	return bl;
}

function validateAddEditMode() {
	if (displayMode == "Add" || displayMode == "Edit") {
		return confirm("Are you sure you want to navigate away from this page and lose all entered information?");
	} else {
		return true;
	}
}

function navigatePage(url) {
	if (validateAddEditMode()) {
		location.href = url;
	}
}

function navigateReport(url) {
	ctl00_uprContent.style.display = "block";
	location.href = url;
}

function changeCategory() {
	if (!validateAddEditMode()) {
		void (0);
	}
}

function checkUncheckAll(blCheck) {
	var ctrl, i;
	for (i = 0; i < document.all.length; i++) {
		ctrl = document.all[i];
		if (ctrl.type == 'checkbox') {
			ctrl.checked = blCheck;
		}
	}
}

function checkSingleSelection(chk) {
	var ctrl, i;
	blSelectedCore = chk.checked;
	if (blSelectedCore) {
		for (i = 0; i < document.all.length; i++) {
			ctrl = document.all[i];
			if (ctrl.type == 'checkbox' && ctrl.id.indexOf("chkSelect") != -1) {
				ctrl.checked = ctrl == chk;
			}
		}
	}
}

function validateSelected() {
	var blSelectedNew = false;
	if (!blSelectedCore) {
		alert("Please select a Core Merchandise Sub Sub Group to merge to!");
		return false;
	}
	else {
		for (i = 0; i < document.all.length; i++) {
			ctrl = document.all[i];
			if (ctrl.type == 'checkbox' && ctrl.id.indexOf("chkMerge") != -1 && ctrl.checked) {
				blSelectedNew = true;
				break;
			}
		}
		if (!blSelectedNew) {
			alert("Please select a at least one New Merchandise Sub Sub Group to merge!");
			return false;
		}
		return true;
	}
}

function confirmCancel() {
	return confirm("Are you sure you want to cancel editing and lose all the information you entered?");
}

function confirmReset() {
	return confirm("Are you sure you want to forfeit changes you made and revert to the last saved state?");
}

function confirmDelete(name) {
	return confirm("Are you sure you want to delete this " + name + "?");
}

// *** start movable popup
function movemouse(e) {
	if (isdrag) {
		dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
		dobj.style.top = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
		return false;
	}
}

function selectmouse(e) {
	var fobj = nn6 ? e.target : event.srcElement;
	var topelement = nn6 ? "HTML" : "BODY";

	if (fobj.className == "dragme") {
		isdrag = true;
		dobj = document.getElementById(fobj.box);
		tx = parseInt(dobj.style.left + 0);
		ty = parseInt(dobj.style.top + 0);
		x = nn6 ? e.clientX : event.clientX;
		y = nn6 ? e.clientY : event.clientY;
		document.onmousemove = movemouse;
		return false;
	}
}

function closePopupWindow(box, hf) {
	var hfPopupState = document.getElementById(hf);
	var div = document.getElementById(box);
	// set the flag
	hfPopupState.value = "hidden";
	// close popup
	div.style.visibility = "hidden";
}
// *** end moveable popup

function printReport() {
	var ifPrint = frames["ifPrint"], tblReport = document.all("tblReport");
	if (ifPrint && tblReport) {
		ifPrint.divPrint.innerHTML = tblReport.outerHTML;
		ifPrint.focus();
		ifPrint.print();
	}
}

function showLocations() {
	if (event.type == "mouseover") {
		var pos = findPos(event.srcElement);
		divLocations.style.top = pos[1];
		divLocations.style.left = pos[0] + 20;
		divLocations.style.display = "block";
	} else {
		divLocations.style.display = "none";
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft, curtop];
}

function getCookie(name) {
	var start = document.cookie.indexOf(name + '=');
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
	if (start == -1) return null;
	var end = document.cookie.indexOf(';', len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + '=' + escape(value) +
        ((expires) ? ';expires=' + expires.toGMTString() : '') +
        ((path) ? ';path=' + path : '') +
        ((domain) ? ';domain=' + domain : '') +
        ((secure) ? ';secure' : '');
}

