//********************************************************************
//*	       Name:	common_scripts.js
//*	Description:	JavaScript functions for topcosales.us
//*	    Created:	02/09/2006
//*	    Updated:	11/13/2008
//********************************************************************

//********************************************************************
//* init()
//* Great window.onload hack from http://dean.edwards.name/
//* Takes care of funky screen remnants due to resizing DOM elements
//********************************************************************
function init() {
	// quit if this function has already been called
	if (arguments.callee.done) return;
	
	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	
	// kill the timer
	if (_timer) clearInterval(_timer);
	
	//* Call the IE select text fix function
	fnFixSelectBug();
	
	//* Call the fix content height function
	fnFixContentHeight();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
	if (this.readyState == "complete") {
	init(); // call the onload handler
}
};
/*@end @*/


/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = init;

//********************************************************************
//* fnFixContentHeight()
//* Compare the content column height with the height of the subnav bar.
//* If the content is shorter then it is automatically adjusted so it doesn't look like shit.
//********************************************************************
function fnFixContentHeight() {
	var objSubNav = document.getElementById('SubNav');
	var objContent = document.getElementById('ContentContainer');
	var intSubNav;
	var intContent;
	
	var IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
	if (!IE7)
	{
		// Get the height of the sub nav column
		if (objSubNav.offsetHeight) {
			intSubNav = objSubNav.offsetHeight;
		}
		else if (objSubNav.style.pixelHeight) {
			intSubNav = objSubNav.style.pixelHeight;
		}
		
		// Get the height of the content column
		if (objContent.offsetHeight) {
			intContent = objContent.offsetHeight;
		}
		else if (objContent.style.pixelHeight) {
			intContent = objContent.style.pixelHeight;
		}
		
		// Compare the two column heights.
		// If the content is shorter then fill out to match the sub nav
		if (intContent < intSubNav) {
			objContent.style.height = intSubNav + 20 + 'px';
		}
	}
	if (IE7)
	{
	}
}

//********************************************************************
//* fnFixSelectBug()
//* Fixes a bug in IE6 that screws up selecting text on a page with either
//* absolute positioning or floats (various reports of both).
//********************************************************************
function fnFixSelectBug() {
	if (window.createPopup && document.compatMode &&  
	document.compatMode=="CSS1Compat")
	{
	  document.onreadystatechange = onresize = function fixIE6AbsPos() 
	  {
		if (!document.body) return; 
		if (document.body.style.margin != "0px") document.body.style.margin = 0; 
		onresize = null; 
		document.body.style.height = 0; 
		setTimeout(function(){ document.body.style.height =  
	document.documentElement.scrollHeight+'px'; }, 1); 
		setTimeout(function(){ onresize = fixIE6AbsPos; }, 100); 
	  }
	}
}

//********************************************************************
//* fnCenterEnlargeLink()
//* Sets the div width to center the link to enlarge an item image 
//* based on that image's width
//********************************************************************
function fnCenterEnlargeLink() {
	var objItemImage = document.getElementById('ItemImage');
	var objEnlargeImage = document.getElementById('EnlargeImage');
	var intImageWidth;
	
	// Get the width of the item image
	if (objItemImage.offsetWidth) {
		intImageWidth = objItemImage.offsetWidth;
	}
	else if (objItemImage.style.pixelWidth) {
		intImageWidth = objItemImage.style.pixelWidth;
	}
	
	// Set the enlarge link width
	objEnlargeImage.style.width = intImageWidth + 'px';
}

//********************************************************************
//* fnSubNavHover(intID, blnActive)
//* Mouseover effect for the subnavigation menu items
//* intID = Item ID to apply effect
//* blnActive = Boolean flag for mouseover or mouseout effect
//********************************************************************
function fnSubNavHover(intID, blnActive) {
	var objSubNav = document.getElementById('Group' + intID);
	if (blnActive) {
		objSubNav.style.background = 'url(_images/icon_menu_right.gif) no-repeat right';
		objSubNav.style.color = '#0D004C';
	}
	else {
		objSubNav.style.background = '';
		objSubNav.style.color = '#333333';
	}
}

//********************************************************************
//* fnGlobalNavSearch()
//* Submits search criteria, unless blank in which case it forwards to 
//* the search page
//********************************************************************
function fnGlobalNavSearch() {
	//* Check to see if the search input is blank
	if (document.frmGlobalNavSearch.txtGlobalNavSearch.value.length > 0) {
		document.frmGlobalNavSearch.submit();
	}
	else {
		document.location.href='search.asp';
	}
}

//********************************************************************
//* fnSubmitSearch()
//* Submits search criteria when the page or number of results is changed
//********************************************************************
function fnSubmitSearch(intCurrentPage) {
	//* Change the value to the page selected
	document.frmSearch.inpPageCurrent.value = intCurrentPage;
	
	//* Submit the search form
	document.frmSearch.submit();
}

//********************************************************************
//* fnSubmitProductList()
//* Resubmits the product list results when the page or number of results is changed
//* strQueryString = The querystring to use when resubmitting the page
//********************************************************************
function fnSubmitProductList(strQueryString, intSelect) {
	var intCurrentPage
	var intPageSize
	intCurrentPage = document.getElementById('selPageCurrent' + intSelect).value;
	intPageSize = document.getElementById('selPageSize' + intSelect).value;
	document.location.href = 'product_list.asp?' + strQueryString + '&Page=' + intCurrentPage + '&PageSize=' + intPageSize;
}

//********************************************************************
//* fnEnlargeImage(strImageName)
//* Opens a larger version of a product image in a popup window
//* strImageName = The image to display
//********************************************************************
function fnEnlargeImage(strImageName) {
	var winWidth = 600;
	var winHeight = 420;
	var scrWidth = (screen.width - winWidth)/2;
	var scrHeight = (screen.height - winHeight)/2;
	winPopup = window.open('_includes/item_enlarged.asp?ID=' + strImageName, 'ItemEnlarged', 'width=' + winWidth + ', height=' + winHeight + ', top=' + scrHeight + ', left=' + scrWidth + ', toolbar=no, menubar=no, scrollbars=yes, resizable=yes');
	if (window.focus) {
		winPopup.focus();
	}
	return false;
}

//********************************************************************
//* fnPopupWindow(strFileName)
//* Opens a new popup window
//* strFileName = The name of the page to display in the window
//* intWinWidth = The desired width of the popup window
//* intWinHeight = The desired height of the popup window
//********************************************************************
function fnPopupWindow(strFileName, intWinWidth, intWinHeight) {
	var winWidth = intWinWidth;
	var winHeight = intWinHeight;
	var scrWidth = (screen.width - winWidth)/2;
	var scrHeight = (screen.height - winHeight)/2;
	winPopup = window.open(strFileName, 'winPopup', 'width=' + winWidth + ', height=' + winHeight + ', top=' + scrHeight + ', left=' + scrWidth + ', toolbar=no, menubar=no, scrollbars=yes, resizable=yes');
	if (window.focus) {
		winPopup.focus();
	}
	return false;
}

//********************************************************************
//* fnFormFocus(objElement, blnEvent)
//* Changes the background color of a form element when it is in focus
//* objElement = The form element to change
//* blnEvent = Whether the element is in or out of focus
//********************************************************************
function fnFormFocus(objElement, blnEvent) {
	if (blnEvent == 0) {
		objElement.style.backgroundColor = '#FFFFFF';
	}
	else {
		objElement.style.backgroundColor = '#FFFFCC';
	}
}

//********************************************************************
//* fnCheckEmail(strEmail)
//* Check if the string is a valid e-mail address. Returns true or false.
//* strEmail = The string to validate
//********************************************************************
function fnCheckEmail(strEmail) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(strEmail)) {
		return true;
	}
	else {
		return false;
	}
}