//
// common.js
//

// Author: Colin Jaggs
// Date: 6th October 2004
// Description: Common JS functions use throughout the site

// common variables
var popUpWin = false;
var xOffset = 10, yOffset = 10;
var floatingLayers = new Array();

// close any existing popup windows
function closePopups()
{
	if (popUpWin) popUpWin.close();
}

function popUpPage(url)
{
	if (popUpWin.open) popUpWin.close();
	
	popUpWin = window.open(url, "popupwin", "width=650,height=500,scrollbars=1,toolbar=1")
}

// cross browser function to find an element by id
function objectById(id)
{ 
	if (document.getElementById) return document.getElementById(id); 
	else if (document.all) return document.all[id]; 
	else if (document.layers) return document.layers[id]; 
} 

// textbox focus and blur events (change active css)
function focusTxt(obj) { obj.className = obj.className + " Selected"; }
function blurTxt(obj) { obj.className = obj.className.replace(" Selected", ""); }

// inline cv2 help on card payment form
function showCV2Help()
{
	if (objectById('cv2Help').style.display == "none") objectById('cv2Help').style.display = "";
	else objectById('cv2Help').style.display = "none";
}

// change manufacturer selection in product search
function changeManufacturer(sender)
{
	// determine url without the current attribute filter, and append the new value if one is selected
	eval("var url = attrFilterBaseUrl.replace(/&Manufacturer=[0-9]+/, \"\")");
	if (sender.selectedIndex > 0) url += "&Manufacturer=" + sender.options[sender.selectedIndex].value;
	location.href = url;
}

// change attribute selection in product search
function changeAttributeFilter(sender, keyName)
{
	// determine url without the current attribute filter, and append the new value if one is selected
	eval("var url = baseUrl.replace(/&P." + keyName + "=[0-9]+/, \"\")");
	if (sender.selectedIndex > 0) url += "&P." + keyName + "=" + sender.options[sender.selectedIndex].value;
	location.href = url;
}

// change the number of results per page on product search
function changeRPP(sender, defaultRPP)
{
	// determine url without the RPP selection
	eval("var url = attrFilterBaseUrl.replace(/&RPP=[0-9]+/, \"\")");
	if (sender.options[sender.selectedIndex].value != defaultRPP) url += "&RPP=" + sender.selectedIndex;
	if (typeof(updSrch) != "undefined") updSrch(url); else location.href = url;
}

// change the sort order on product search
function changeSortBy(sender, defaultSort)
{
	// determine url without the sort by selection
	eval("var url = attrFilterBaseUrl.replace(/&Sort=[0-9]+/, \"\")");
	if (sender.options[sender.selectedIndex].value != defaultSort) url += "&Sort=" + sender.selectedIndex;
	if (typeof(updSrch) != "undefined") updSrch(url); else location.href = url;
}

// custom validation function to manually call page validation without actually submitting a form
function validateGroup(group)
{
	Page_IsValid = false;
	if (typeof(WebForm_DoPostBackWithOptions) == "function")
	{
		WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("", "", true, group, "", false, false));
		return Page_IsValid;
	}
	else return true;
}

// toggle visibility of floating layer
function toggleFloatLayer(e, layer, state, offsetX, offsetY)
{
	offsetX = offsetX || 10;
	offsetY = offsetY || 10;

	if (objectById(layer))
	{
		if (state == 1)
		{
			// set x and y offsets
			xOffset = offsetX;
			yOffset = offsetY;

			// show the floating layer
			objectById(layer).style.display = "inline";
		}
		else objectById(layer).style.display = "none";
	}
}

// load a banner, checking for a flash banner first (if flash is available) and then a jpg
function autoBanner()
{
	var bWidth = 580, bHeight = 202;
	//if (PageName == "home") bHeight = 202;

	// check if there are any banners specifically for this page
	if (avlPageBanners != "")
	{
		// check for flash or image based banners for this page
		if (avlPageBanners.indexOf("swf") > -1 && DetectFlashVer(6, 0, 0)) WriteFlashBanner(FolderRoot + "DynamicContent/Flash/Banners/" + PageName.replace(/\//g, "-"), bWidth, bHeight);
		else if (avlPageBanners.indexOf("png") > -1) WriteImgBanner(FolderRoot + "DynamicContent/Images/Banners/" + PageName.replace(/\//g, "-") + ".png", bWidth, bHeight);
		else if (avlPageBanners.indexOf("jpg") > -1) WriteImgBanner(FolderRoot + "DynamicContent/Images/Banners/" + PageName.replace(/\//g, "-") + ".jpg", bWidth, bHeight);
	}

	// otherwise look for default banners instead
	else
	{
		// check for default flash or image based banners
		if (avlDefBanners.indexOf("swf") > -1 && DetectFlashVer(6, 0, 0)) WriteFlashBanner(FolderRoot + "DynamicContent/Flash/Banners/Default", bWidth, bHeight);
		else if (avlDefBanners.indexOf("png") > -1) WriteImgBanner(FolderRoot + "DynamicContent/Images/Banners/Default.png", bWidth, bHeight);
		else if (avlDefBanners.indexOf("jpg") > -1) WriteImgBanner(FolderRoot + "DynamicContent/Images/Banners/Default.jpg", bWidth, bHeight);
	}
}

// write a flash banner to the document
function WriteFlashBanner(swfPath, width, height)
{
	AC_FL_RunContent
    (
	    "src", swfPath,
	    "width", width,
	    "height", height,
	    "align", "middle",
	    "id", "autoBanner",
	    "quality", "high",
	    "bgcolor", "#FFFFFF",
	    "name", "autoBanner",
	    "allowScriptAccess", "sameDomain",
	    "type", "application/x-shockwave-flash",
	    "wmode", "transparent",
	    'codebase', 'http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab',
	    "pluginspage", "http://www.adobe.com/go/getflashplayer"
    );
}

// write an image banner to the document
function WriteImgBanner(imgPath, width, height)
{
	document.write("<img src=\"" + imgPath + "\" alt=\"\" width=\"" + width + "\" height=\"" + height + "\" />")
}