﻿gMasterPrefix = 'ctl00_cphMain_';

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;
}

function P(photoName, filename)
{
    filename = "i/" + filename;
    photo = $(photoName);
    photo.alt = filename;
    photo.src = filename;
    photo.style.display = "block";
}

function B(PriceDDL, IndependentPriceDDLValues, OptionDDL)
{
    hide = true;
    PriceDDL = $(PriceDDL);
    spanOption = $("span" + OptionDDL);
    OptionDDL = $$(OptionDDL, PriceDDL.parentNode.parentNode, "select");
    OptionDDL = OptionDDL[0];
    
    for(counter = 0; counter < IndependentPriceDDLValues.length; counter++)
    {
        if (PriceDDL.options[PriceDDL.selectedIndex].value == IndependentPriceDDLValues[counter])
            hide = false;
    }
    
    if (hide)
    {
        OptionDDL.style.display = "none";
        spanOption.style.display = "none";
    }
    else
    {
        OptionDDL.style.display = "block";
        spanOption.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 --%>
function A(PriceDDL, IndependentPriceDDLValues, OptionDDL, DependentOptionDDLValues)
{   
    add = false;
    counter = 0;
    counter2 = 0;
    PriceDDL = $(PriceDDL);
    OptionDDL = $$(OptionDDL, PriceDDL.parentNode.parentNode, "select");
    OptionDDL = OptionDDL[0];
    
    // comparing values to see if we should add or delete an option
    for(counter = 0; counter < IndependentPriceDDLValues.length; counter++)
    {
        if (PriceDDL.options[PriceDDL.selectedIndex].value == 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);
            }
        }
    }
}

function Tally(ProductID, HiddenDivID, feature)
{   
    if (feature == null) feature = "";
    
    product = $('PD' + ProductID + feature);
    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) + "=" + detail.value + ";";
        }
    }
    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);
    $(gMasterPrefix + '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;
}

//Original Name: getElementsByPartialID.  Returns an ARRAY
function $$(searchID,node,tag) {
    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++;
		}
	}
	return idElements;
}

function LoadProductPictures(categoryName)
{   
    if ($(categoryName) != null)
    {
        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)
{
	$(gMasterPrefix + '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';
    
    $(gMasterPrefix + 'hfCartStep').value = step;
    $('CartStep' + step).style.display = 'block';
}
// Highlight 
function H(id)
{   title = $(id);
    globalbgcolor = title.style.backgroundColor;
    title.style.backgroundColor = '#ae2f56'; //'#970f1a';
    title.style.color = '#fffac9';
}
// UnHighlight 
function UH(id)
{   title = $(id);
    title.style.color = '#000000';
    title.style.backgroundColor = globalbgcolor;
}
// HighlightSmall
function HS(id)
{   title = $(id);
    title.style.backgroundColor = '#ae2f56';
    title.style.color = "#fffac9";
}
// UnHighlightSmall
function UHS(id)
{   title = $(id);
    title.style.color = '#000000';
    title.style.backgroundColor = '#abae79';
}

var gCurrentHighlighted, gCurrentTitle, gCurrentSmallTitle;

function SH(id, title, smalltitle) {

	// hiding the previously shown block
	if (gCurrentHighlighted != null)
	{
		var tempCurrentHightlighted = gCurrentHighlighted;
		var tempCurrentTitle = gCurrentTitle;
		var tempCurrentSmallTitle = gCurrentSmallTitle;
		
		gCurrentHighlighted = null;
		gCurrentTitle = null;
		gCurrentSmallTitle = null;

		SH(tempCurrentHightlighted, tempCurrentTitle, tempCurrentSmallTitle);
	}
	gCurrentHighlighted = id;
	gCurrentTitle = title;
	gCurrentSmallTitle = 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;
}
