//
// 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";
	}
}

function fixIEAlpha(img)
{
    var arVersion = navigator.appVersion.split("MSIE");
    var version = parseFloat(arVersion[1]);

    if ((version >= 5.5) && (version < 7.0) && (document.body.filters))
    {
        if (img.altsrc)
        {
            var tempImg = new Image(); tempImg.src = img.altsrc;        // preload image just in case
            img.src = img.altsrc;
        }
        else
        {
            var tempImg = new Image(); tempImg.src = img.src;        // preload image just in case
            img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src + "')";
            img.src = FolderRoot + "Images/Blank.gif";
        }
    }
}
