/** Supporting functions **/
function classCheck(obj,c) { var re = new RegExp('\\b'+c+'\\b'); return re.test(obj.className); }

/** Common functions **/

/**[Page Load Mask]**/
var	maskBG = new Image();
	maskBG.src = '/guiComponents/guiGraphix/progressbar.gif';

unMaskPage = function () {
	maskDIV = document.getElementById('loadmask');
	maskDIV.style.display = 'none';
}


function backLink() {
	var isPopUp = false;
	adj = 0;
	if (document.all) {adj = 1};
	if (top.location) topWinURL = top.location;
	if (window.location) thisWinURL = window.location;
	var openerWinURL;

	if (window.opener) {
		openerWinURL = window.opener.location;
		if ((openerWinURL != thisWinURL)&&(topWinURL == thisWinURL))
			isPopUp = true;
	}

	if (isPopUp == true) {
		document.write('<a href="javascript:window.close();">Close Window<img src="/guiComponents/guiGraphix/iconClose.gif" class="icon"></a>');
	}
	else if (window.history.length > 1-adj&&(isPopUp != true)) {
		document.write('<a href="javascript:window.history.back();">Go Back<img src="/guiComponents/guiGraphix/iconBack.gif" class="icon"></a>');
	}
}

function PrintOnly(printID) {
	var allDivs = document.getElementsByTagName('DIV');
	for (i=0; i < allDivs.length; i++) {
		if (((allDivs[i].id)&&(allDivs[i].id != printID)&&(allDivs[i].id != 'backdrop'))||(classCheck(allDivs[i],'noprint'))) allDivs[i].style.display='none';
		document.body.style.backgroundImage = 'none';
		document.body.style.backgroundColor = '#FFFFFF';
		backDrop = document.getElementById('backdrop');
		if (backDrop) {
			backDrop.style.backgroundImage = 'none';
			backDrop.style.backgroundColor = '#FFFFFF';
			backDrop.style.borderWidth = '0px';
			backDrop.style.top = '0px';
			backDrop.style.left = '0px';
			backDrop.style.margin = '0px';
			backDrop.style.width = '7in';
		}
		document.getElementById(printID).style.margin = '10px';
		document.getElementById(printID).style.borderWidth = '0px';
	}

	if (window.print)
		window.print();

	else alert('Use your browser\'s "Print" function to print this page.');
}

var pcount = 0;
var ccount = 0;
var adcount = 0;
var audiocount = 0;
var offset = 30;
var productPOP = new Array();
var couponPOP = new Array();
var adPOP = new Array();
var audioPOP = new Array();

function PopUpProduct(URL)  {
	var ppin = pcount*25+offset;
	productPOP[pcount] = window.open(URL,'product'+pcount+'','top='+ppin+',left=0,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes')
	for (j=0; j < productPOP.length; j++) if (productPOP[j].closed != true) {productPOP[j].focus();}
	pcount ++;
}

function PopUpCoupon(URL)   {
	var cpin = ccount*25 + offset;
	couponPOP[ccount] = window.open(URL,'coupon'+ccount+'','top='+cpin+',left='+cpin+',width=725,height=500,resizable=yes');
	for (i=0; i < couponPOP.length; i++) if (couponPOP[i].closed != true) {couponPOP[i].focus();}
	ccount ++;
}

function PopUpAd(URL)   {
	var adpin = adcount*25+offset;
	adPOP[adcount] = window.open(URL,'ad'+adcount+'','top='+adpin+',left='+adpin+',width=640,height=480,scrollbars=no,status=no,resizable=yes');
	for (i=0; i < adPOP.length; i++) if (adPOP[i].closed != true) {adPOP[i].focus();}
	adcount ++;
}

/*[Ad window sizing]*/

function fitWindowSize() {
	var adPage = document.getElementById('ad');
	var couponPage = document.getElementById('coupon');

	if (adPage) {
	var adImage = adPage.getElementsByTagName('img');
	if (adImage) {
	/*[cosmetic adjustments]*/
	var adjW = 4; adjH = 31; tempSize = 500;
	if (document.clientHeight && document.clientWidth) { //alert('Safari support is in progress');
	width = adImage[0].width + adjW;
	height = adImage[0].height + adjH + 22;
	window.resizeTo(width, height);
	} else if (window.innerHeight && window.innerWidth) { //alert('Firefox, Mozilla, Netscape, Camino, Opera, Safari');
	window.innerWidth = adImage[0].width*1 + adjW;
	window.innerHeight = adImage[0].height*1 + adjH;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight )) { //alert('IE 6+ in "standards compliant mode"');
	window.resizeTo(tempSize, tempSize);
	width = tempSize - (document.documentElement.clientWidth -  adImage[0].width) + adjW;
	height = tempSize - (document.documentElement.clientHeight -  adImage[0].height) + adjH;
	window.resizeTo(width, height);
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) { //alert('IE in "quirks mode"');
	window.resizeTo(tempSize, tempSize);
	width = tempSize - (document.body.clientWidth -  adImage[0].width) + adjW;
	height = tempSize - (document.body.clientHeight -  adImage[0].height) + adjH;
	window.resizeTo(width, height);
	}}}
	if (couponPage) {
	var pageEnd = document.getElementById('pageend');
	if (pageEnd) {
	/*[cosmetic adjustments]*/
	var adjH = 31; tempSize = 500; pageWidth = 725; pageLength = findPosY(pageEnd);
	if (document.clientHeight && document.clientWidth) { //alert('Safari');
	height = pageLength*1 + 25;
	window.resizeTo(pageWidth, height);
	} else if (window.innerHeight && window.innerWidth) { //alert('Firefox, Mozilla, Netscape, Camino, Opera, Safari');
	window.innerHeight = pageLength*1;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight )) { //alert('IE 6+ in "standards compliant mode"');
	window.resizeTo(pageWidth, tempSize);
	height = tempSize - (document.documentElement.clientHeight -  pageLength*1) - adjH;
	window.resizeTo(pageWidth, height);
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) { //alert('IE in "quirks mode"');
	window.resizeTo(pageWidth, tempSize);
	height = tempSize - (document.body.clientHeight -  pageLength*1) - adjH;
	window.resizeTo(pageWidth, height);
	}}}
}

function PopUpAudio(URL)    {
	var audiopin = audiocount*25+offset;
	audioPOP[audiocount] = window.open(URL,'audio'+audiocount+'','top='+audiopin+',left='+audiopin+',width=150,height=176,scrollbars=no,status=no,resizable=no');
	for (i=0; i < audioPOP.length; i++) if (audioPOP[i].closed != true) {audioPOP[i].focus();}
	audiocount ++;
}


function PopUpCart(URL) {
	window.open(URL,'cart','top=30,left=30,width=750,height=500,scrollbars=yes,resizable=yes')
}

function PopUpText(URL, width, height) {
	width =  parseInt(width) || 600;
	height = parseInt(height) || 500;
	window.open(URL,'note','top=30,left=30,width='+width+',height='+height+',scrollbars=yes,resizable=yes')
}

function PopUpVideo(URL)    {
	window.open(URL,'video','top=30,left=30,width=660,height=550,scrollbars=no,resizable=no')
}
function PopUpFCK() {
	popFCK = window.open('/admin/fckeditor.html','editor','top=20,left=20,width=820,height=400,scrollbars=no,resizable=no');
	popFCK.window.focus();
}

function PopUpCallSched(URL)    {
	popCallSched = window.open(URL,'schedulecall','top=50,left=50,width=200,height=450,scrollbars=no,resizable=no');
	popCallSched.window.focus();
}

/**[DOM Scroller]**/
var hostElement;
var scrElement;
var scrInt;
var scrDel;
var scrInfo;
var pickDesc = new Array(); /*pull array values from the DB on every page*/

function  SlideShow(elemId) {
    scrElement = document.getElementById(elemId);
    hostElement = scrElement.parentNode.parentNode; //alert(hostElement.id);
    itemLinks = scrElement.getElementsByTagName('a');
    for (i = 0; i < itemLinks.length; i++) if (pickDesc[i]) itemLinks[i].title = pickDesc[i]; else itemLinks[i].title = '';
}

function setScroll(elemId) {
	scrElement = document.getElementById(elemId);
}

function scrollUp() {
    if ((scrElement.childNodes)&&(scrElement.childNodes.length > 1)) {
        elFirst = scrElement.childNodes[0];
        scrElement.appendChild(elFirst);
    /*[show info for the certain item]*/
    if (hostElement.id == 'main') showInfo(scrElement.childNodes[1].title);
    else showInfo(scrElement.childNodes[0].title);}
}

function scrollDown(){
    if ((scrElement.childNodes)&&(scrElement.childNodes.length > 1)) {
        elFirst = scrElement.childNodes[0];
        elLast = scrElement.childNodes[scrElement.childNodes.length - 1];
    if (elLast) scrElement.insertBefore(elLast, elFirst);
    /*[show info for the certain item]*/
    if (hostElement.id == 'main') showInfo(scrElement.childNodes[1].title);
    else showInfo(scrElement.childNodes[0].title);}
}

function startScroll(dir,elemId,scrDel){
    scrElement = document.getElementById(elemId);
    if (dir == 'U') scrInt = setInterval('scrollUp()', scrDel);
    else scrInt = setInterval('scrollDown()', scrDel);
}

function stopScroll(){
	if (scrInt != null) clearInterval(scrInt);
}

function showInfo(info) {
    scrInfo = document.getElementById('ShowDescr');
    if (scrInfo && info) scrInfo.innerHTML = info;
}

function setupShowcaseInfo() {
    scrInfo = document.getElementById('ShowDescr');
    if (scrInfo) scrInfo.style.display = 'none';
    if (pickDesc.length > 0) {
    for (i = 0; i < pickDesc.length; i++) if ((pickDesc[i] != '')&&(scrInfo)) scrInfo.style.display = 'block';}
}

function setupScrollers() {
    tableScrollIds = new Array('scrollCoupons','scrollProducts');
    for (i = 0; i < tableScrollIds.length; i++) {
            scrollBody = document.getElementById(tableScrollIds[i]);
            tableButtonT = document.getElementById(tableScrollIds[i]+'T')
            tableButtonB = document.getElementById(tableScrollIds[i]+'B')
            if (scrollBody) {
				scrollRows = scrollBody.getElementsByTagName('DIV');
				if (scrollRows.length < 4) {
					scrollBody.parentNode.style.height = 'auto';
					scrollBody.parentNode.style.paddingTop = '5px';
					scrollBody.parentNode.style.paddingBottom = '5px';
					if (tableButtonT) tableButtonT.style.display = 'none';
					if (tableButtonB) tableButtonB.style.display = 'none';
				}
			}
	}
}

/**[Top Menu Dropdown orientation setup - all browsers]**/
function setupTopMenu() {
    var topMenuLIs = new Array();
    var topLevelLIs = new Array(); j = 0;
    if (document.getElementById('toplevel')) {
		topMenuLIs = document.getElementById('toplevel').getElementsByTagName('LI');
		//alert('Top menu contains total of '+topMenuLIs.length+' lists');
		/*apply additional class to items #3 and up*/
		for (i = 0; i < topMenuLIs.length; i++) if (topMenuLIs[i].parentNode.id == 'toplevel')/**/ {
			//alert(topMenuLIs[i].parentNode.id);
			topLevelLIs[j] = topMenuLIs[i]; j++;
		}
	}
	//alert('Top-level menu contains '+topLevelLIs.length+' lists');
	for (n = 3; n < 6; n++) if (topLevelLIs[n]) {topLevelLIs[n].className = 'inv';}
}

/**[Side Menu]**/
function MenuClick() {

	var menuDIVs = new Array();
	var menuLINKs = new Array();
	if (document.getElementById && document.getElementsByTagName);
	{
	menuDIVs = document.getElementById('menu').getElementsByTagName('DIV');
	menuLINKs = document.getElementById('menu').getElementsByTagName('A');
	function closeAll() {   for ( var i=0; i < menuDIVs.length; i++ ) {/* close all child DIVs */
				if (classCheck(menuDIVs[i],'child')) menuDIVs[i].style.display = 'none';} }
	closeAll();
	/* start clicking */
		for ( var j=0; j < menuLINKs.length; j++ ) {
		menuLINKs[j].onclick = function() {
			if (classCheck(this,'folder'))  {
				var children = this.parentNode.getElementsByTagName("DIV");
			if (classCheck(children[0],'down')) {
			if (children[0].style.display == 'none')
				{
				children[0].style.display = 'block';
				for ( var f=0; f < menuLINKs.length; f++ ) if (classCheck(menuLINKs[f],'folder')) {
				menuLINKs[f].style.backgroundImage = 'url(/guiComponents/guiGraphix/enclDown.gif)';
				}   this.style.backgroundImage = 'url(/guiComponents/guiGraphix/enclUpOver.gif)';
				}
				else if (children[0].style.display == 'block')
				{
				children[0].style.display = 'none';
				this.style.backgroundImage = 'url(/guiComponents/guiGraphix/enclDown.gif)';
				}
			}
			if (classCheck(children[0],'side')) {
			if (children[0].style.display == 'none')
				{
				children[0].style.display = 'block';
				for ( var f=0; f < menuLINKs.length; f++ ) if (classCheck(menuLINKs[f],'folder')) {
				menuLINKs[f].style.backgroundImage = 'url(/guiComponents/guiGraphix/enclDown.gif)';
				}   this.style.backgroundImage = 'url(/guiComponents/guiGraphix/enclOver.gif)';
				}
				else if (children[0].style.display == 'block')
				{
				children[0].style.display = 'none';
				for ( var f=0; f < menuLINKs.length; f++ ) if (classCheck(menuLINKs[f],'folder'))
				menuLINKs[f].style.backgroundImage = 'url(/guiComponents/guiGraphix/enclDown.gif)';
				}
			}}}}}}

/**[Top menu support for Internet Explorer (will not activate in other browsers)]**/
ieHover = function() {
	if (document.getElementById('nav')) {
	var ieULs = document.getElementById('nav').getElementsByTagName('ul');
	/** IE script to cover <select> elements with <iframe>s **/
	var ifSelects = document.getElementsByTagName('select');
	if (ifSelects.length > 0) {
	for (j=0; j<ieULs.length; j++) {
	ieULs[j].innerHTML = '<iframe src="about:blank" scrolling="no" frameborder="0"></iframe>' + ieULs[j].innerHTML;
		var ieMat = ieULs[j].firstChild;
			ieMat.style.width=ieULs[j].offsetWidth+'px';
			ieMat.style.height=ieULs[j].offsetHeight+'px';
			ieULs[j].style.zIndex='99';
	}
	}
	/** IE script to change class on mouseover **/
		var ieLIs = document.getElementById('nav').getElementsByTagName('li');
		for (var i=0; i<ieLIs.length; i++) if (ieLIs[i]) {
			ieLIs[i].onmouseover=function() {this.className+=' iehover';}
			ieLIs[i].onmouseout=function() {this.className=this.className.replace(' iehover', '');}
		}
	}
}

if (window.attachEvent)
	window.attachEvent('onload', ieHover);

/** IE script to change class on mouseover - city list trim **/
ieOver = function() {
	var contEiD = 'citybucket';
	var flipTag = 'p';
		if (document.getElementById(contEiD)) {
	var ieOs = document.getElementById(contEiD).getElementsByTagName(flipTag);
		for (var i=0; i<ieOs.length; i++) if (ieOs[i]) {
			ieOs[i].onmouseover=function() {this.className='ieover';}
			ieOs[i].onmouseout=function() {this.className='ieout';}
		}}
}
if (window.attachEvent)
	window.attachEvent('onload', ieOver);


if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {


	/*
	 * This was causing bugs with some of the other images loaded with javascript
	 * document.writeln('<style type="text/css">img { visibility: hidden; }</style>');
	 *
	 */
    window.attachEvent("onload", AlphaChannel);
}

function AlphaChannel() {
    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
    for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
        if (itsAllGood && img.src.match(/\.png$/i) != null) {
            var src = img.src;
            img.style.width = img.width + "px";
            img.style.height = img.height + "px";
            img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='image')"
            img.src = "/guiComponents/guiGraphix/trans01.gif";
        }   img.style.visibility = "visible";}
    for (i=0; i<document.all.length; i++){
        var bg = document.all[i].currentStyle.backgroundImage;
        if (itsAllGood && bg){
            if (bg.match(/\.png/i) != null){
                var bgsrc = bg.substring(5,bg.length-2);
                document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bgsrc + "', sizingMethod='crop')";
                document.all[i].style.backgroundImage = "url(/guiComponents/guiGraphix/trans01.gif)";
            }
        }
    }
}



/**[in-page ad display for portal page]**/
/*[support routines]*/
function findPosX(obj) {
	var curleft = 0;
		if (obj.offsetParent){
			while (obj.offsetParent)
			{curleft += obj.offsetLeft;
				obj = obj.offsetParent;}
		} return curleft;
}

function findPosY(obj) {
	var curtop = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent)
			{curtop += obj.offsetTop;
				obj = obj.offsetParent;}
		} return curtop;
}





/*[show-hide routines]*/
var adLife;

function showAd(obj,adHref,objId,adImage) {
    var newX = findPosX(obj);
    var newY = findPosY(obj);
    var ad = document.getElementById('pagead');
    var adpic = document.getElementById('adbody');
    var adcall = document.getElementById('adlink');
    ad.style.display = 'block';
    ad.style.top = newY + 'px';
    ad.style.left = '5px';
/*[assign link]*/
    adcall.href = adHref;
/*[assign image]*/
    adpic.innerHTML = '<img src="'+adImage+'" height="240" width="320">';
/*[set lifespan]*/
    if (adLife) window.clearTimeout(adLife);
    adLife = window.setTimeout('hideAd()',5000);
}

function hideAd() {
    var ad = document.getElementById('pagead');
    ad.style.display = 'none';
}

/**[confirm action]**/

function confirmAction(message) {
	var agree = confirm(message);
	if (agree == true)
			return true ;
	else
			return false ;
}


function ShowHide(target,state) {
	if (document.getElementById(target)) document.getElementById(target).style.display = state;
}

/**[hew show/hide routine]**/

/**[Confirm CSS values for use in Toggle Display]**/
function checkHideShow()    {
	allSpans = document.getElementsByTagName('span');
	for (i = 0; i < allSpans.length; i++)
	if (allSpans[i])    {
						if (classCheck(allSpans[i],'hideshow')) allSpans[i].style.display = 'inline';
						if (classCheck(allSpans[i],'showhide')) allSpans[i].style.display = 'none';
						/*[special case]*/
						if ((allSpans[i].id == 'menubody')&&(allSpans[i].parentNode.parentNode.parentNode.id == 'sidepanel')) allSpans[i].style.display = 'none';
						}
}
/**[Toggle Display]**/
function toggleDisplay(target, trigger) {
	var toggle = document.getElementById(target);
	var tsd = toggle.style.display == 'inline';
		toggle.style.display = tsd ? 'none' : 'inline';
		if (trigger.innerHTML) trigger.innerHTML = tsd ? trigger.innerHTML.replace(/hide/, 'show') : trigger.innerHTML.replace(/show/, 'hide');
		if (trigger.title) trigger.title = tsd ? trigger.title.replace(/hide/, 'show') : trigger.title.replace(/show/, 'hide');
		if (trigger.id == 'smcontrol') trigger.style.backgroundImage = tsd ? 'url(/guiComponents/guiGraphix/iconShow.gif)' : 'url(/guiComponents/guiGraphix/iconX.gif)';
}

/**[Expand description area - external page display]**/

function expandParent(trigger) {
	var parEL = trigger.parentNode;
	var psh = parEL.style.height == '36px';
		parEL.style.height= psh ? 'auto' : '36px';
		if (trigger.src) trigger.src = psh ? trigger.src = '/guiComponents/guiGraphix/iconLess.gif' : trigger.src = '/guiComponents/guiGraphix/iconMore.gif';
}

function collapseParent(trigger) {
	var parEL = trigger.parentNode;
	var psh = parEL.style.height == 'auto';
		parEL.style.height= psh ? '36px' : 'auto';
		if (trigger.src) trigger.src = psh ? trigger.src = '/guiComponents/guiGraphix/iconMore.gif' : trigger.src = '/guiComponents/guiGraphix/iconLess.gif';
}

/**[fighting bug attempt to stretch bucket to match the product photo]**/

function stretchBucket(img,vsize) { /*[call from image tag: onLoad="stretchBucket(this,this.height);"]*/
	var ie = document.all; //alert(vsize);
	ie ? img.parentNode.parentNode.style.height = vsize + 'px' : img.parentNode.parentNode.style.minHeight = vsize + 'px';
}

/**[fighting Safari bug - banner image load]**/

function setBanner(img,vsize) { /*[call from image tag: onLoad="setBanner(this,this.height);"]*/
var sideBarLeft = document.getElementById('sidebarleft');
var sideBarRight = document.getElementById('sidebarright');
var sidePanel = document.getElementById('sidepanel');
if ((sideBarLeft||sideBarRight)&&(!sidePanel)) {
img.parentNode.style.height = vsize + 'px';
 //if (sideBarLeft) {sideBarLeft.style.top = vsize*1 + 36*1 + 'px';}
 //if (sideBarRight) {sideBarRight.style.top = vsize*1 + 36*1 + 'px';}
 if (sideBarLeft) {sideBarLeft.style.top = vsize*1 + 64*1 + 'px';}
 if (sideBarRight) {sideBarRight.style.top = vsize*1 + 64*1 + 'px';}
}}

/**[checkboxes]**/

function checkAll(container) {
	var holder = document.getElementById(container);
	if (!holder) {alert('Element with the ID="'+container+'" is not found.');}
	else {var boxes = new Array();
	boxes = holder.getElementsByTagName('input');
	for (i=0;i<boxes.length; i++) { if (boxes[i].type == 'checkbox') boxes[i].checked = true;
	}}
}

function uncheckAll(container) {
	var holder = document.getElementById(container);
	if (!holder) {alert('Element with the ID="'+container+'" is not found.');}
	else {var boxes = new Array();
	boxes = holder.getElementsByTagName('input');
	for (i=0;i<boxes.length; i++) { if (boxes[i].type == 'checkbox') boxes[i].checked = false;
	}}
}

/**[admin frameset refresh]**/
function reloadTop()    {
	if ((window.parent && top.document.getElementById('adminbanner'))&&(window.name != 'adminbanner')) {
		adminBanner = top.document.getElementById('adminbanner');
		/*clear banner*/
		adminBanner.src = 'blankblack.html';
		/*load banner*/
		adminBanner.src = 'adminbanner.php';
	}
}
function reloadTabs()   {
			if ((window.parent && top.document.getElementById('tabline'))&&(window.name != 'tabline')) {
			adminTabs = top.document.getElementById('tabline');
						/*clear tabs*/
						adminTabs.src = 'blankblack.html';
						/*load tabs*/
						adminTabs.src = 'tabs.php';
						}
}


/**[script activation]**/
window.onload = function()  {


	if (document.getElementById('menu'))
		MenuClick();

	if (document.getElementById('imgScroller'))
		SlideShow('imgScroller');

	setupScrollers();
	setupShowcaseInfo();
	setupTopMenu();
	checkHideShow();
	//fitWindowSize();
	//reloadTop();
	//reloadTabs();
	if (document.getElementById('loadmask')) unMaskPage();


}
