﻿/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//												New Code
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

gFiredOnce = false;

// Calculate Price
function cP(productID)
{
	var productDiv = $('div' + productID);
	var priceDDL = $('PD' + productID);
	var qtyDDL = $('QD' + productID);
	var individualPriceDisplay = $('UT' + productID);
	var totalPriceDisplay = $('ST' + productID);
	
	var ddlList = $$('', productDiv, 'select');

	var currentDDL;
	var currentPrice;
	var currentQuantity;
	var totalPrice = +ReturnDDLPrice(priceDDL);

	var length = ddlList.length;
	for (index = 0; index < length; index++)
	{
		currentDDL = ddlList[index];
		if (currentDDL != priceDDL && currentDDL != qtyDDL && !IsQtyDDL(currentDDL))			// if we're not on the price DDL or overall product DDL
		{
			currentPrice = ReturnDDLPrice(currentDDL);
			currentQuantity = ReturnSelectedValue($(currentDDL.id + "Q"));
			if (currentQuantity == null) currentQuantity = 1;
			totalPrice += currentPrice * currentQuantity;
		}
	}

	individualPriceDisplay.innerHTML = totalPrice.toFixed(2);
	
	totalPrice = totalPrice * ReturnSelectedValue(qtyDDL);
	totalPriceDisplay.innerHTML = totalPrice.toFixed(2);
}

function IsQtyDDL(ddl)
{
	return (ddl.id[ddl.id.length - 1] == "Q");
}


// extracts the price from a value of the selected item of a DDL
function ReturnDDLPrice(ddl)
{
	var value = ReturnSelectedValue(ddl);
	var newValue = value.split(/\//);

	if (newValue.length > 1)
		return newValue[1];
	else return 0;
}

// extracts the ID from a value of the selected item of a DDL
function ReturnDDLID(ddl)
{
	var value = ReturnSelectedValue(ddl);
	var newValue = value.split(/\//);

	//if (newValue.length > 1)
		return newValue[0];
	//else return newValue;
}

function ReturnSelectedValue(ddl)
{
	if (ddl == null) return 0;
	return ddl.options[ddl.selectedIndex].value
}




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//											End New Code
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function SIFocus(txtobject)
{
	if (txtobject.value == txtobject.defaultValue)
		txtobject.value = '';
	txtobject.className = 'optionDDL';
}

function SIBlur(txtobject)
{
	if (txtobject.value == '')
	{
		txtobject.value = txtobject.defaultValue;
		txtobject.className = 'optionDDL greyedout';
	}
}

function Move(txt, next)
{
    next = $$(next, txt.parentNode ,"input");
    
    if (txt.value.length == txt.maxLength)
        next[0].focus();
}

function IsNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
}

// loads images as necessary
function P(photoName, filename)
{
    filename = "i/" + filename;
    photo = $(photoName);
    photo.alt = filename;
    photo.src = filename;
    photo.style.display = "block";
}

// hides/shows dependent drop downs
function B(PriceDDL, IndependentPriceDDLValues, name)
{
	var hide = true;
    var divOptionAndDDL = $(name);

    for (counter = 0; counter < IndependentPriceDDLValues.length; counter++)
    {
    	if (ReturnDDLID(PriceDDL) == IndependentPriceDDLValues[counter])
            hide = false;
    }

    if (hide)
    	divOptionAndDDL.style.display = "none";
    else
    	divOptionAndDDL.style.display = "block";
}


function CharLimit(pnum, charLimit, feature)
{
    if (feature == null) feature = "";
    textarea = $("txtMessage" + pnum + feature);
    
    textarea.setAttribute('maxlength', charLimit);
    
    MsgKeyUp("txtMessage" + pnum + feature, pnum, feature);
}

function MsgKeyUp(id, pnum, feature)
{
    textarea = $(id);
    limit = textarea.getAttribute('maxlength');
    
    if (textarea.value.length > limit)
        textarea.value = textarea.value.substring(0, limit);
        
    if (pnum != null)
    {
        if (feature == null) feature = "";
        $("charsleft" + pnum + feature).innerHTML = limit - textarea.value.length;
    }
}

//PriceDDL                      -- the PartialID of the independent PriceDDL calling the function (may not be necessary)
//IndependentPriceDDLValues     -- the possible selected values that would trigger action in PriceDDL
//OptionDDL                     -- the PartialID of the Dependent DDL of the option
//DependentOptionDDLValues      -- the values and labels(text) that will be added or deleted from the dependent DDL --%>
//		handles dependent options
function A(PriceDDL, IndependentPriceDDLValues, OptionDDL, DependentOptionDDLValues)
{
    add = false;
    counter = 0;
    counter2 = 0;
    PriceDDL = $(PriceDDL);
    OptionDDL = $$(OptionDDL, PriceDDL.parentNode.parentNode, "select")[0];

    //todo: may encounter problems with dependent drop downs that contain price
    
    // comparing values to see if we should add or delete an option
    for(counter = 0; counter < IndependentPriceDDLValues.length; counter++)
    {
        if (ReturnDDLID(PriceDDL) == IndependentPriceDDLValues[counter])
            add = true;
    }
        
    // if we're adding, search and see if the value is already there
    if(add)
    {
        for(counter = 0; counter < OptionDDL.options.length; counter++)
        {
            for(counter2 = 0; counter2 < DependentOptionDDLValues.length; counter2++)
            {
                if (OptionDDL.options[counter].value == DependentOptionDDLValues[counter2][0])
                    add = false;
            }
        }
        
        //if the value needed to be added, and it wasn't there (it still needs to be added)
        if (add)
        {
            for(counter = 0; counter < DependentOptionDDLValues.length; counter++)
            {
                option = document.createElement("OPTION");
                option.value = DependentOptionDDLValues[counter][0];
                option.text = DependentOptionDDLValues[counter][1];
                
                OptionDDL.options.add(option);
            }
        }
    }
        
    else //remove the option(s)
    {
        for(counter = 0; counter < OptionDDL.options.length; counter++)
        {
            for(counter2 = 0; counter2 < DependentOptionDDLValues.length; counter2++)
            {
                if (OptionDDL.options[counter].value == DependentOptionDDLValues[counter2][0])
                    OptionDDL.remove(counter);
            }
        }
    }
}

// compiles info to send to the server
function Tally(ProductID, HiddenDivID, feature)
{
    if (feature == null) feature = "";

    product = $('div' + feature + ProductID);
    info = $(HiddenDivID);
    info.value = "";
    
    for (i=0; i<product.getElementsByTagName("select").length; i++)
    {
        if (product.getElementsByTagName("select")[i].style.display != "none")
        {
            detail = product.getElementsByTagName("select")[i];
            info.value = info.value + detail.id.slice(detail.id.lastIndexOf('_') + 1) + "=" + ReturnDDLID(detail) + ";";
        }
    }
    if (product.getElementsByTagName("textarea").length > 0)
    {
        for (i=0; i<product.getElementsByTagName("textarea").length; i++)
        {
            detail = product.getElementsByTagName("textarea")[i];
            if (detail.value != detail.defaultValue)
            {
				info.value = info.value + 
				detail.id.slice(detail.id.lastIndexOf('_') + 1).replace(/[0-9]+[A-Za-z]+/ , '') + "=" + 
				encodeURI(detail.value.replace(/[;\=]/g, '')) + ";";
            }
        }
    }

    info.value = info.value.substr(0, info.value.length - 1);   //removing trailing ";"  
    
    //alert(info.value);
    $('ctl00_cphBody_hfCartStep').value = 1;
}

//function PathToNode(id)
//{
//    temp = PathToNodeHelper(id);
//    return temp.substr(1, temp.length - 1);
//}
//function PathToNodeHelper(id)
//{
//    if (id.parentNode != null)
//        return PathToNodeHelper(id.parentNode) + '.' + id.parentNode.nodeName;
//    else
//        return "";
//}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


var gLast$$SearchParameters;
var gLast$$SearchResults;

//searchID:		Partial id of element to find
//node:			where to search
//tag:			what element types to search (ex. 'div')
//Original Name: getElementsByPartialID.  Returns an ARRAY
function $$(searchID, node, tag)
{
	if (gLast$$SearchParameters != null &&
		gLast$$SearchParameters[0] == searchID && gLast$$SearchParameters[1] == node && gLast$$SearchParameters[2] == tag)		// return previously cached results
	{
		return gLast$$SearchResults;
	}

	if (node != null && node.id != null && node.id != '')	// only cache named node searches
		gLast$$SearchParameters = [searchID, node, tag].slice();

    var idElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	// The RegExp ^ begin, \\w anychr, $ end
	var pattern = new RegExp('(^|\\w)'+searchID+'(\\w|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].id) ) {
			idElements[j] = els[i];
			j++;
		}
	}

	if (node != null && node.id != null && node.id != '')	// only cache named node searches
		gLast$$SearchResults = idElements;
	
	return idElements;
}

function LoadProductPictures(categoryName)
{   
    if ($(categoryName))
    {
        currentCat = $(categoryName);
        for (i=0; i<currentCat.getElementsByTagName("img").length; i++)
        {
            currentImage = currentCat.getElementsByTagName("img")[i];
            if (currentImage.alt != "Add to Cart" && currentImage.alt != "Sorry, No items at this time.")//only exceptions are in the if
                currentImage.src = currentImage.alt;
        }
    }
}
		 
function PrepCartForPostBack(step)
{
	$('ctl00_cphBody_hfCartStep').value = step;
}

function CartView(step)
{
    if ($('CartStep1') != null) $('CartStep1').style.display = 'none';
    if ($('CartStep2') != null) $('CartStep2').style.display = 'none';
    if ($('CartStep3') != null) $('CartStep3').style.display = 'none';
    
    $('ctl00_cphBody_hfCartStep').value = step;
    $('CartStep' + step).style.display = 'block';
   }

// Highlight 
function H(id)
{   title = $(id);
    globalbgcolor = title.style.backgroundColor;
    title.style.backgroundColor = '#970f1a';
    title.style.color = '#FFFFFF';
}
// UnHighlight 
function UH(id)
{   title = $(id);
    title.style.color = '#000000';
    title.style.backgroundColor = globalbgcolor;
}
// HighlightSmall
function HS(id)
{   title = $(id);
    title.style.backgroundColor = '#970f1a';
    title.style.color = "#FFFFFF";
}
// UnHighlightSmall
function UHS(id)
{   title = $(id);
    title.style.color = '#000000';
    title.style.backgroundColor = '#DDDDDD';
}

// Show/Hide product
function SH(id, title, smalltitle)
{
	product = $(id);
    title = $(title);
    smalltitle = $(smalltitle);
    if (product.style.display == 'none' || product.style.display == '')
    {   product.style.display = 'block';
        title.style.display = 'none';
        smalltitle.style.display = 'block';
    }
    else
    {   product.style.display = 'none';
        title.style.display = 'block';
        smalltitle.style.display = 'none';
    }
}

function toggleservings(lstcontrol, nmax, rprcontrol, rprcontrol2, uprcontrol, txtcontrol, targetqty, targetprice )
{   // Indexes start at zero, uprcontrol and txtcontrol we set, rprcontrol and rprcontrol2 are sources
    // This assumes your option / price numbers start at 1 and have no breaks as they increase
    // Otherwise, we cannot reference the controls by index (no direct relationship between controls index and option / price number
    $(txtcontrol).innerHTML = $(rprcontrol + '_ctl0' + ($(lstcontrol).options[$(lstcontrol).selectedIndex].value -1) + '_sstext').value;
    $(uprcontrol).innerHTML = $(rprcontrol2 + '_ctl0' + ($(lstcontrol).options[$(lstcontrol).selectedIndex].value -1) + '_ssunitprice').value;
    var tempamount = $(targetqty).options[$(targetqty).selectedIndex].value;

    if( tempamount > 0 )
    {   $(targetprice).innerHTML = '$' + (($(targetqty).options[$(targetqty).selectedIndex].value *
        ($(rprcontrol2 + '_ctl0' + ($(lstcontrol).options[$(lstcontrol).selectedIndex].value -1) + '_ssunitprice').value).slice(1)).toFixed(2));
    }
    else { $(targetprice).innerHTML = '$0.00';
    }
}

function toggleoptions(lstcontrol, nmax, rprcontrol, txtcontrol)
{   // Indexes start at zero
    $(txtcontrol).innerHTML = $(rprcontrol + '_ctl0' + ($(lstcontrol).options[$(lstcontrol).selectedIndex].value -1) + '_ssoption').value;
}