function ForceToBeTopWindow()
{
	if (window.top != window)
		window.top.location.href = window.location.href;
}
function RadioRepeater(itemIndex, repeaterCount, repeaterName, radioName)
{	
	itemIndex++; 
	var il = radioName.length - repeaterName.length;
	var suffix = radioName.substr(repeaterName.length, il);
	suffix = suffix.replace(itemIndex, '%1');
	 
	for (var i = 1; i <= repeaterCount; i++) {
		if (i == itemIndex)
			continue;
		var name = repeaterName + suffix.replace('%1', i);
		var el = FindElement(name);
		if (el != null)
			el.checked = false;
	}
}


function RadioRepeaterEx(hidName,radioName)
{	
	var elHid = FindElement(hidName);
	var radArray;
	
	if(elHid != null)
	{
		radArray = elHid.value.split("|");
	} 
	var i;
	for (i in radArray)
	{
		if (radArray[i] == radioName)
			continue;
		var el = FindElement(radArray[i]);
		if (el != null)
			el.checked = false;
	}
}


function HandleEnterKey(buttonID)  {    
	if (event.keyCode == 13) {    
		var elButton = eval("document.forms[0]." + buttonID);		
		if (elButton != null) {
			event.cancelBubble = true;
			event.returnValue = false;
			elButton.click();
		}
    }
}
 
function TipRollOver(rowIndex, tipIndex)
{
	if (typeof(g_rowImage) != "undefined") {
		var elRowImage = eval(g_rowImage[rowIndex]);
		if (elRowImage != null) 
			elRowImage.src = g_tipUrl[tipIndex];	
		onText(g_tipText[tipIndex]);
	}
	return true;
}

function TipClear(rowIndex) {
	return TipRollOver(rowIndex, 0);
}
 
function LowerFrameLoad(strURL, strParams) {
	LowerFrameLoadFull(strURL + strParams);
}

function LowerFrameLoadFull(strFullUrl) {
	var elBottomFrame = GetFrame("bottom");

	if (elBottomFrame != null)
		elBottomFrame.document.location.href = strFullUrl;
}

function HRefWindowOpener() {	
    window.open(BuildHRefClickHandler(HRefWindowOpener.arguments), null, "width=540,height=480,scrollbars=yes,resizable=yes");
}

function HRefOpenRMPriceCalcWindow() {	
	var url = BuildHRefClickHandler(HRefOpenRMPriceCalcWindow.arguments);
	OpenChildWindow(url, "PriceCalc", "width=760,height=560,top=50,left=20,alwaysRaised=yes,status=yes,resizable=yes,scrollbars=yes,toolbar=0,directories=0,menubar=0,location=0,copyhistory=0");
}

function HRefOpenRFPriceCalcWindow() {	
	var url = BuildHRefClickHandler(HRefOpenRFPriceCalcWindow.arguments);
	OpenChildWindow(url, "PriceCalc", "width=490,height=500,top=50,left=20,alwaysRaised=yes,status=yes,resizable=yes,scrollbars=yes,toolbar=0,directories=0,menubar=0,location=0,copyhistory=0");
}

function HRefClickHandler() {	
    document.location.href = BuildHRefClickHandler(HRefClickHandler.arguments);
}

function ParentHRefClickHandler() {		
    parent.location.href = BuildHRefClickHandler(ParentHRefClickHandler.arguments);
}

function FrameHRefClickHandler() {			
	var href = BuildHRefClickHandler(FrameHRefClickHandler.arguments);	
	var elBottomFrame = GetFrame("bottom");
	if (elBottomFrame != null) 
		elBottomFrame.document.location.href = href;
}

function BuildHRefClickHandler(argv) {          
	var argc = argv.length;
	var linkArray = eval("g_link" + argv[0]);
	var href = linkArray[0];
	var delim = ""          

	if (href.indexOf("?") == -1 && argc > 1) 
		href = href + "?";
	else
		delim = "&"

	for (var i = 1; i < argc; i++) {
		href = href + delim + linkArray[i] + "=" + argv[i];         
		delim = "&";
	}
	return href;
}

function HandleImageSearch(txtID, rmID, rfID, typeID, brand, usePrefsID, controlSubmitButtonID, rfsubsID)  {

	
	if (controlSubmitButtonID != null && controlSubmitButtonID.length > 0)
		if(!ValidateFromElement(controlSubmitButtonID)) return;
	else 
		if (!Page_ClientValidate()) return;
	
	var el = eval("document.forms[0]." + txtID);		
	var query = "&txtSearch=" + escape(el.value);
	var allRoyaltyFree = false;
	var allLicensed = false;
		
	el = (rmID == "") ? null : eval("document.forms[0]." + rmID);		
	if (el != null && el.checked)
		allLicensed = true;
	
	el = (rfID == "") ? null : eval("document.forms[0]." + rfID);		
	if (el != null && el.checked)
		allRoyaltyFree = true;
		
	el = (usePrefsID == "") ? null : eval("document.forms[0]." +usePrefsID);		
	if (el != null && el.checked)
		query += "&chkUsePrefs=on";

	el = (typeID == "") ? null : eval("document.forms[0]." + typeID);		
	if (el != null)
		query += "&selImageType=" + el.value;

	el = (rfsubsID == "") ? null : eval("document.forms[0]." + rfsubsID);		
	if (el != null && el.checked)
		{
		query += "&chkMySubsOnly=on";
		allLicensed = false;
		allRoyaltyFree = true;
		}
		
	if (allLicensed)
		query += "&chkLicensed=on";

	if (allRoyaltyFree)
		query += "&chkRoyaltyFree=on";

	if (brand != -1)
		query += "&brandID=" + brand;
				
	document.location.href = g_imageSearchHRef + query;
	return;
}

// called directly in a url, not from a form.
function HandleSubscriptionImageSearch(controlSubmitButtonID, txtID, rfsubSKU, newToSubscriptionOnly, rfsubUpsellMode)
{
	if (controlSubmitButtonID != null && controlSubmitButtonID.length > 0)
		if(!ValidateFromElement(controlSubmitButtonID)) return;
	else 
		if (!Page_ClientValidate()) return;

	var query = "";
	
	var el;
	el = (txtID == "") ? null : eval("document.forms[0]." + txtID);		
	if (el != null && el.value != "")  // if validation allows blank, map to key that will get all images
		query += "&txtSearch=" + escape(el.value);
	else
		query += "&txtSearch=vocabulary";
		
	query += "&chkRoyaltyFree=on";

	if (rfsubSKU == "")
		query += "&chkMySubsOnly=on";
	else 
		query += "&rfsubSKU=" + rfsubSKU;
		
	if (newToSubscriptionOnly)
		query += "&chkNewSubImagesOnly=on";
		
	if (rfsubUpsellMode)
		query += "&chkRFSubUpsellMode=on";

	document.location.href = g_imageSearchHRef + query;
	return;
}

function HandleValueImageSearch(txtID, valueID, rfID, typeID, brand, usePrefsID)  {

	if (!Page_ClientValidate())
		return;
		
	var el = eval("document.forms[0]." + txtID);		
	var query = "&txtSearch=" + escape(el.value);
		
	el = (valueID == "") ? null : eval("document.forms[0]." + valueID);		
	if (el != null && el.checked)
		query += "&chkRFValued=on";
	else
	{
		el = (rfID == "") ? null : eval("document.forms[0]." + rfID);		
		if (el != null && el.checked)
			query += "&chkRoyaltyFree=on";
	}	
	el = (usePrefsID == "") ? null : eval("document.forms[0]." +usePrefsID);		
	if (el != null && el.checked)
		query += "&chkUsePrefs=on";

	el = (typeID == "") ? null : eval("document.forms[0]." + typeID);		
	if (el != null)
		query += "&selImageType=" + el.value;

	if (brand != -1)
		query += "&brandID=" + brand;
				
	document.location.href = g_imageSearchHRef + query;
	return;
}

function HandlePDISearch(txtID, redID, greenID, blueID, typeID)  {
	if (!Page_ClientValidate())
		return;
		
	var el = eval("document.forms[0]." + txtID);		
	var query = "&txtSearch=" + escape(el.value);
		
	el = eval("document.forms[0]." + redID);		
	if (el != null && el.checked)
		query += "&chkPdiRed=on";
	
	el = eval("document.forms[0]." + greenID);		
	if (el != null && el.checked)
		query += "&chkPdiGreen=on";

	el = eval("document.forms[0]." + blueID);		
	if (el != null && el.checked)
		query += "&chkPdiBlue=on";

	el = eval("document.forms[0]." + typeID);		
	if (el != null)
		query += "&selImageType=" + el.value;
		
	document.location.href = g_imageSearchHRef + query;
	return;
}

function HandleStockbyteSearch(txtID, platinumID, goldID, typeID)  {
	if (!Page_ClientValidate())
		return;
		
	var el = eval("document.forms[0]." + txtID);		
	var query = "&txtSearch=" + escape(el.value);
		
	el = eval("document.forms[0]." + platinumID);		
	if (el != null && el.checked)
		query += "&chkSBPlatinum=on";
	
	el = eval("document.forms[0]." + goldID);		
	if (el != null && el.checked)
		query += "&chkSBGold=on";

	el = eval("document.forms[0]." + typeID);		
	if (el != null)
		query += "&selImageType=" + el.value;
		
	document.location.href = g_imageSearchHRef + query;
	return;
}

function HandleStockdiscSearch(txtID, premiumID, classicID, typeID)  {
	if (!Page_ClientValidate())
		return;
		
	var el = eval("document.forms[0]." + txtID);		
	var query = "&txtSearch=" + escape(el.value);
		
	el = eval("document.forms[0]." + premiumID);		
	if (el != null && el.checked)
		query += "&chkSDPremium=on";
	
	el = eval("document.forms[0]." + classicID);		
	if (el != null && el.checked)
		query += "&chkSDClassic=on";

	el = eval("document.forms[0]." + typeID);		
	if (el != null)
		query += "&selImageType=" + el.value;
		
	document.location.href = g_imageSearchHRef + query;
	return;
}
 
function ValidateCheckboxSelection(errorMsg)
{
	if (g_ValidationError) return false;
	
	var found = false;
    var checkboxArray = eval("g_checkboxes");
	for (var i = 0; i < checkboxArray.length; i++) {
		var el = eval("document.forms[0]." + checkboxArray[i]);		
		if (el) {
			if (el.checked) {
				found = true;
				break;
			}
		}
	}	
	if (!found) {
		if (errorMsg != null && errorMsg.length > 0)
			alert(errorMsg);
		return false;
	}	
	return true;
}

function ValidateListboxSelection(listboxID, errorMsg)
{
	if (g_ValidationError) return false;
	
	var found = false;
	var elList = eval("document.forms[0]." + listboxID);		
	if (elList != null) {	
		for (var i = 0; i < elList.length; i++) {
			if (elList.options[i].selected) {
				found = true;
				break;
			}
		}
		if (!found) {
			if (errorMsg != null && errorMsg.length > 0)
				alert(errorMsg);
			return false;
		}	
	}
	return true;
}

function CheckList(elCheckBox, elSelectList)
{
	var elBox = FindElement(elCheckBox);
	var elList = FindElement(elSelectList);

	for (var i = 0; i < elList.length; i++)
	{
		elList.options[i].selected = elBox.checked;
	}
}
function CheckRFList(elCheckBox1, elCheckBox2, elSelectList)
{
	var elBox1 = FindElement(elCheckBox1);
	var elBox2 = FindElement(elCheckBox2);
	var elList = FindElement(elSelectList);

	if (elBox1.checked) elBox2.checked = false;
	for (var i = 0; i < elList.length; i++)
	{
		elList.options[i].selected = elBox1.checked;
	}
	
}
function CheckValueList(elCheckBox1, elCheckBox2, elSelectList, strValueBrands)
{
	var elBox1 = FindElement(elCheckBox1);
	var elBox2 = FindElement(elCheckBox2);
	var elList = FindElement(elSelectList);
	var match = false;
	var brandList;

	if (elBox2.checked)
	{
		elBox1.checked = false;
		CheckList(elCheckBox1, elSelectList);
	}
	if (strValueBrands != null && strValueBrands.length > 0)
	{
		brandList = strValueBrands.split(",");
		for (var j = 0; j < brandList.length; j++)
		{
			for (var i = 0; i < elList.length; i++)
			{
				if (elList.options[i].value == brandList[j])
				{
					elList.options[i].selected = elBox2.checked;
					break;
				}
			}
		}
	}
	
}

function ManageCheckBox(elCheckBox, elSelectList)
{ 
	var elBox = FindElement(elCheckBox);
	var elList = FindElement(elSelectList);

	if (elBox != null && elList != null)
		for (var i = 0; i < elList.options.length; i++)
			if (!elList.options[i].selected)
			{
				elBox.checked = false;
				return;
			}
	elBox.checked = true; 
}
function SyncRFandValueCheckbox(elCheckBox1, elCheckBox2)
{ 
	var elBox1 = FindElement(elCheckBox1);
	var elBox2 = FindElement(elCheckBox2);
	if (elBox1 != null && elBox2 != null)
	{
		if(elBox1.checked && elBox2.checked)
		{
				elBox2.checked = false;
		}
	}
}
function ManageRFCheckBoxs(elCheckBox1, elCheckBox2, elSelectList, strValueBrands)
{ 
	var elBox1 = FindElement(elCheckBox1);
	var elBox2 = FindElement(elCheckBox2);
	var elList = FindElement(elSelectList);
	var brandList = strValueBrands.split(",");

	elBox1.checked = true;
	if (elBox1 != null && elList != null)
	{
		for (var i = 0; i < elList.options.length; i++)
		{
			if (!elList.options[i].selected)
			{
				elBox1.checked = false;
				break;
			}
		}
	}
	elBox2.checked = false;
	var shouldCheck = true;
	var selectedCount = 0;
	if (elBox2 != null && brandList != null && elList != null)
	{
		
		for (var j = 0; j < elList.options.length; j++)
		{
			if (elList.options[j].selected)
			{
				//check if the selected item is in the value collection list
				var isInList = false;
				for (var i = 0; i < brandList.length; i++)
				{
					if (elList.options[j].value == brandList[i])
					{
						isInList = true;
						selectedCount++;
						break;
					}
				}
				if (!isInList)
				{
					shouldCheck = false;
					break;
				}	
			}
		}
		
		if (shouldCheck && selectedCount == brandList.length)
			elBox2.checked = true;
	}
	
}


var g_childWindow = null;
function openHelpWindow(helpURL, winName)
{
	if (helpURL.indexOf("http") != -1)
	{
		if (typeof(winName) != "undefined")
			helpURL += winName;
		
		winName = "helpWin";
	}
	OpenChildWindow(helpURL, winName, "width=500,height=360,top=50,left=280,alwaysRaised=yes,toolbar=0,directories=0,menubar=0,status=1,resizable=yes,location=0,scrollbars=1,copyhistory=0");
}

function GetParentIfExists(wnd)
{
	var wndOpener = wnd.parent;
	if (!wndOpener) wndOpener = wnd;
	return wndOpener;
}

function IsSafari()
{
	return g_BrowserName == "AppleWebKit";
}

function CloseWorksCorrectly()
{
	if (g_JS_isWin) return true;
	// else mac
	return !(g_JS_isIE || IsSafari()); // netscape works ok, but IE and Safari not so much
}

// like OpenChildWindow, but does not replace itself
function OpenPopupWindow(childURL, childName, childFeatures)
{
	var wndOpener = GetParentIfExists(window);
	var wndChild;
	wndChild = wndOpener.open(childURL, childName, childFeatures);
	if (g_JS_isWin || !g_JS_isIE)
		wndChild.focus();
}

function OpenChildWindow(childURL, childName, childFeatures){ 

	if (CloseWorksCorrectly()) CloseChildWindow();
	if ((!g_JS_isWin) && (g_JS_isIE)){
		g_childWindow = window.open(childURL, childName, childFeatures);
	} else {	
		if (!FindChildWindow(childName)) {
			g_childWindow = window.open(childURL, childName, childFeatures);
		} else {
			if (document.location.protocol == "http:")
				g_childWindow.document.location.href=childURL;
		}
		g_childWindow.focus();
	}
}
 
function CloseChildWindow(){
	if (g_childWindow != null){
		if (!g_childWindow.closed)
			g_childWindow.close();
		g_childWindow = null;
	}
}

function FindChildWindow(childName){
	if ((g_childWindow != null) && (g_childWindow.closed != true)){
		g_childWindow = window.open("", childName);
		return true;
	}
	else
		return false;
}

function SetFocus(controlId)
{
	var vld = FindElement(controlId);
	if (vld) {
		vld.focus();
		if (vld.type == "text")
			vld.select();
	}
}

function ClearText(controlId)
{
	var ctl = FindElement(controlId);
	if (ctl && (ctl.type == "text"))
		ctl.value = "";
}

function SelectDropDownList(controlId, index)
{
	var ctl = FindElement(controlId);
	if (ctl)
		ctl.selectedIndex = index;
}

function imageSwap(theImage, theSrc){
	if (document.images)
		document.images[theImage].src = theSrc;
}

function frontdoor_imageClick(imageID) {
	var url = g_imageDetailHRef.replace("%1", imageID);
	document.location.href = url;
} 

function frontdoor_imageDetail(imageID) {
	var url = g_imageDetailHRef.replace("%1", imageID);
	document.location.href = url;
}
 
function frontdoor_imageSearch(imageIDs) {
	document.location.href = g_imageSearchHRef + "&txtSearch=" + imageIDs;
}

function frontdoor_CDDetail(productCode)
{
	var newurl = g_CDHRef.replace('%1', productCode);
	window.location.href = newurl;
}

function frontdoor_CDSearch(productCode)
{
	var newurl = g_CDSearchHRef.replace('%1', productCode);
	window.location.href = newurl;
}

function frontdoor_CatalogDetail(productCode)
{
	var newurl = g_catalogSearchHRef.replace('%1', productCode);
	window.location.href = newurl;
}

function frontdoor_clipDetail(clipId, brandId)
{
	var newurl = g_clipDetailHRef.replace('%1', clipId);
	if (brandId != null)
		newurl = newurl.replace('=-1', '='+brandId);
	window.location.href = newurl;
}

function frontdoor_clipSearch(clipIds)
{
	var newurl = g_clipSearchHRef.replace('%1', clipIds);
	window.location.href = newurl;
}


// utils for refreshing openers
function GetFrameFromWindow(wnd, frameName)
{	
	var i;
	for (i = 0; i < wnd.frames.length; i++)
	{
		if (wnd.frames[i].name == frameName)
		{
			return wnd.frames[i];
		}
	}
	return null;
}

function WindowHasOpener(wnd)
{
    return wnd.opener && !wnd.opener.closed;
}
 
function WindowHrefContainsPage(wnd, page)
{
    try
    {
        if (!page || page.length == 0)
            return true; // if no page param, return true; 
		return wnd.location.href.indexOf(page) != -1;  // this could also be wnd.document.location.href...
    }
    catch (ex)  // we'll get here if the window is on another domain.
    {
        return false;
	}
}    
 
function RefreshOpener(hrefForFrame, pageForFrame, pageForFullPage)
{
    try
    {
		if (WindowHasOpener(window)) // does our window have an opener?
        {
			// get top level window of opener
			var wndToRefresh = GetParentIfExists(window.opener);
            if (WindowHrefContainsPage(wndToRefresh, pageForFullPage)) //only refesh opener if window is on a page we expect
            {
                wndToRefresh.document.location.href = wndToRefresh.document.location.href;
            }
            else if (WindowHrefContainsPage(wndToRefresh, pageForFrame)) //only refesh opening frame if window is on a page we expect    
            {
                var frmBottom = GetFrameFromWindow(wndToRefresh, "bottom"); // "bottom" could be param too 
                if (frmBottom)
                    frmBottom.location.href = hrefForFrame;
            }
            // else didnt match either, so do nothing
        }
    }
    catch (ex)
    {
    }
}

function ManageCheckboxes(chkAll, strBoxArray)
{
	var elAll = FindElement(chkAll);
	var arrBoxes = strBoxArray.split(",");
	var elBox;
	if (elAll.checked)
	{
		for(i=0;i<arrBoxes.length;i++)
		{
			elBox = FindElement(arrBoxes[i]);
			elBox.checked = true;
		}
			
	}
	else
	{
		for(i=0;i<arrBoxes.length;i++)
		{
			elBox = FindElement(arrBoxes[i]);
			elBox.checked = false;
		}
	}
}

function ManageAllCheckbox(chkAll, strBoxArray)
{
	var elAll = FindElement(chkAll);
	var arrBoxes = strBoxArray.split(",");
	var elBox;
	
	for(i=0;i<arrBoxes.length;i++)
	{
		elBox = FindElement(arrBoxes[i]);
		if (!elBox.checked)
		{
			elAll.checked = elBox.checked;
			return;
		}
	}
	elAll.checked = elBox.checked;
}

function FindAllElements(elementId)			// uses partial id
{
	var arElements = new Array();	
	var frm = document.forms[0];
	for (var i = 0; i < frm.elements.length; i++)   {
		if (frm.elements[i].id.indexOf(elementId) != -1) {
			arElements[arElements.length] = frm.elements[i];
		}
	}
	return arElements;
}

function LaunchProfileWindow(url)
{
	OpenPopupWindow(url,'registerpop',"width=800,height=600,top=50,left=50,alwaysRaised=yes,toolbar=0,directories=0,menubar=0,status=1,resizable=yes,location=0,scrollbars=1,copyhistory=0");
}
