keywordsclicked = 0;
UpdateUnsubclicked = 0;

function validateSearch(form) {
	if ((form.queryText.value == "") || (form.queryText.value.toLowerCase() == "search site")) {
		alert('Please enter a search word or phrase');
		return false;
	}
}

function keywordsDown(){
	if (keywordsclicked==0){
		keywordsclicked = 1;
		UpdateUnsubclicked = 0;
		toggleVisibility("most_popular_keywords_slide","show","visible","visible");
		toggleVisibility("update_unsubscribe_details","hidden","hidden","hidden");
		toggleVisibility("update_unsubscribe_button","hidden","hidden","hidden");
	} else {
		keywordsclicked = 0;
		toggleVisibility("most_popular_keywords_slide","hidden","hidden","hidden");
	}
}

if (document.images) 
{
	tabOnLeft = new Image(); tabOnLeft.src = "/shopping/images/tmpl/tab_images/left_tab_on.gif"; 
	tabOnRight = new Image(); tabOnRight.src = "/shopping/images/tmpl/tab_images/right_tab_on.gif"; 
	tabOffLeft = new Image(); tabOffLeft.src = "/shopping/images/tmpl/tab_images/left_tab_off.gif"; 
	tabOffRight = new Image(); tabOffRight.src = "/shopping/images/tmpl/tab_images/right_tab_off.gif"; 
}
function swapTab(whichImages, on)
{
	if (on)
	{
		document.images[whichImages+"_left"].src=tabOnLeft.src;
		document.images[whichImages+"_right"].src=tabOnRight.src;
	}
	else
	{
		document.images[whichImages+"_left"].src=tabOffLeft.src;
		document.images[whichImages+"_right"].src=tabOffRight.src;
	}
}

function toggleVisibility(id, NStype, IEtype, WC3type) {
	if (document.getElementById) {
		eval("document.getElementById(id).style.visibility = \"" + WC3type + "\"");
	}
	else if (document.layers) {
		document.layers[id].visibility = NStype;
	} else {
		if (document.all) {
			eval("document.all." + id + ".style.visibility = \"" + IEtype + "\"");
		}
	}
}

function toggleUpdateUnsub(){
	if (UpdateUnsubclicked==0){
		UpdateUnsubclicked = 1;
		keywordsclicked = 0;
		toggleVisibility("update_unsubscribe_details","show","visible","visible");
		toggleVisibility("update_unsubscribe_button","show","visible","visible");
		toggleVisibility("most_popular_keywords_slide","hidden","hidden","hidden");
	} else {
		UpdateUnsubclicked = 0;
		toggleVisibility("update_unsubscribe_details","hidden","hidden","hidden");
		toggleVisibility("update_unsubscribe_button","hidden","hidden","hidden");
	}
}

function getRadioValue(whichForm, whichFormField)
{
	valueSelected = false;
	if (whichForm)
	{
		if (whichFormField.length) 
		{
			for (i=0; i< whichFormField.length; i++)
			{
				if (whichFormField[i].checked) {
					valueSelected=true;
					return true;
				}
			}
		}
		
		//if radio array only has 1 element, then it will return "undefined"
		//for the length property, so you must check for a field value
		//to see if it has been checked.
		else if (null!=whichFormField.value && ""!=whichFormField.value)
		{
			if (whichFormField.checked)
			{
				return true;
			}
		}
	}
	return valueSelected;
}


function validateOptionsSelected(pSelect1, pSelect2)
{
	//select1 is driver value
	//select2 is filtered value
	var alertType =  "";
	if (pSelect1 && document.getElementById("select1") && !getRadioValue(document.SkuSelector, document.SkuSelector.driverValue)) {
		document.getElementById("select1Text").style.color="red";
		document.getElementById("select1Indicator").style.display="block";
		alertType += pSelect1; 
	} 
	else if (pSelect2 && document.getElementById("select2") && !getRadioValue(document.Skus, document.Skus.skuid)) {
		document.getElementById("select2Text").style.color="red";
		document.getElementById("select2Indicator").style.display="block";
		if (""!=alertType) { alertType += " and "; }
		alertType += pSelect2; 	
	}
	if (alertType)
	{
		document.getElementById("alertMessage").innerHTML = "Please select a " +alertType+ " before adding this item to your cart."; 
		document.getElementById("alertMessage").style.display = "block"; 
		return false; 
	}
}


// AJAX call to reload divs.
function reloadDiv (pUrl, pDivId) {
    var xmlhttp;
    // create the request
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    // callback to update specified div
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            // alert ("x updating: " + pDivId + "\nURL: "+pUrl+"\nstatus: " + xmlhttp.status);
            document.getElementById(pDivId).innerHTML = xmlhttp.responseText;
        }
    }
    // submit request
    xmlhttp.open("get", pUrl);
    xmlhttp.send(null);
}

function reloadSkuOptionsDiv (pUrl, pDivId, pSkusFormName, pSkusElementName,pProdId, pDriverValue, pDisplaySelectedProduct, pProductDetailsTab, pSelect2) {
    var skuId = getSelectedFormElementValue(pSkusFormName,pSkusElementName);
	var hidePurchase = false;
    if (document.getElementById("select2")) hidePurchase = true;
    var paramString = "?id="+pProdId+"&driverValue="+pDriverValue+"&displaySelectedProduct="+pDisplaySelectedProduct+"&productDetailsTab="+pProductDetailsTab;
    paramString = paramString+"&testSkuId=";
    if (skuId) {
       paramString = paramString+skuId;
    }
    var xmlhttp;
    // create the request
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    // callback to update specified div
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            //alert ("x updating: " + pDivId + "\nURL: "+pUrl+paramString+"\nstatus: " + xmlhttp.status);
            document.getElementById(pDivId).innerHTML = xmlhttp.responseText;

            skuId = getSelectedFormElementValue(pSkusFormName,pSkusElementName);
            if (!skuId) {
                //skuId = selectFirstFormElementValue(pSkusFormName,pSkusElementName);
            }
            if (!skuId) {
                skuId = getFormElementValue(pSkusFormName,"defaultSKU");
            }
            if (document.getElementById("select1Text"))
            {
				document.getElementById("select1Text").style.color="#666666";
				document.getElementById("select1Indicator").style.display="none";
            }
            reloadProductDivs(pProdId, skuId, pDriverValue, pDisplaySelectedProduct, pProductDetailsTab, hidePurchase, pSelect2);
        }
    }
    // submit request
    xmlhttp.open("get", getPageURLPrefix("/catalog/") + pUrl+paramString);
    xmlhttp.send(null);
}
function reloadProductDivs(pProdId, pSkuId, pDriverValue, pDisplaySelectedProduct, pProductDetailsTab, pDisableOnFirstDisplay, pSelect2) {
	if (!pSkuId)
	{
		pSkuId="";
	}
    var urlPrefix = getPageURLPrefix("/catalog/");
    var paramString = "?prodId="+pProdId+"&skuId="+pSkuId+"&driverValue="+pDriverValue+"&disableOnFirstDisplay="+pDisableOnFirstDisplay+'&select2Desc='+pSelect2;
    reloadDiv(urlPrefix+"catalog/productnamewrapper.jspf"+paramString+"&displaySelectedProduct="+pDisplaySelectedProduct,"product-name");
    reloadDiv(urlPrefix+"catalog/productpurchasewrapper.jspf"+paramString,"product-purchase");
    reloadDiv(urlPrefix+"catalog/productmainimagewrapper.jspf"+paramString,"product-image");
    reloadDiv(urlPrefix+"catalog/productimageswrapper.jspf"+paramString,"product-images");
    reloadDiv(urlPrefix+"catalog/productdetailstabwrapper.jspf"+paramString+"&productDetailsTab="+pProductDetailsTab,"product-details");

    if (document.getElementById("select2Text"))
    {
		document.getElementById("select2Text").style.color="#666666";
		document.getElementById("select2Indicator").style.display="none";
    }

}

function reloadProductDetailDiv(pProdId, pSkuId, pProductDetailsTab, pTabIndentifier) {
    var paramString = "?prodId="+pProdId+"&skuId="+pSkuId+"&productDetailsTab="+pProductDetailsTab+"&tabIndentifier="+pTabIndentifier;
    reloadDiv(getPageURLPrefix("/catalog/") + "catalog/productdetailstabwrapper.jspf"+paramString,"product-details");
}

function getFormElementValue(pFormName, pElementName) {
    var value;
    var form=document.getElementById(pFormName);
    if (!form) {
        return value;
    }
    var element = form.elements[pElementName];
    if (!element) {
        return value;
    }
    value = element.value
    return value;
}

function getSelectedFormElementValue(pFormName, pElementName) {
    var checkedValue;
    var form=document.getElementById(pFormName);
    if (!form) {
        return checkedValue;
    }
    var options = form.elements[pElementName];
    if ( !options ) {
        return checkedValue;
    }
    if (options.length) {
        for (i=0;  (!checkedValue) && (i<options.length); i++) {
            if (options[i].checked) {
                checkedValue = options[i].value;
            }
        }
    }
    else {
        if (options.checked) {
            checkedValue = options.value;
        }
    }
    return checkedValue;
}

function selectFirstFormElementValue(pFormName, pElementName) {
    var checkedValue;
    var form=document.getElementById(pFormName);
    if (!form) {
        return checkedValue;
    }
    var options = form.elements[pElementName];
    if ( !options ) {
        return checkedValue;
    }
    if (options.length) {
        for (i=0; (!checkedValue) && (i<options.length); i++) {
            if (!options[i].disabled) {
                options[i].checked = true;
                checkedValue = options[i].value;
            }
        }
    }
    else {
        if (!options.disabled) {
            options.checked = true;
            checkedValue = options.value;
        }
    }
    return checkedValue;
}

function getPageURLPrefix(pBefore) {
    var requestURL = location.href;
    var endIndex = requestURL.indexOf(pBefore);
    truncated = requestURL.substring(0, endIndex+1);
    return truncated;
}


