function initPage(shwprc) {
    showMyPrice = shwprc;
    my_productID = '';
    my_categoryID = '';
    myOptions = new Array();
}

var divHandler = {
    div: dojo.byId("editDiv"),
    show: function(productID){
        hideFooter();
        ajaxCall(productID);
    },
    showOption: function(productID,sku_modifier) {
        showPop('editDiv');
        hideFooter();
        ajaxOptionCall(productID,sku_modifier);
    },
    hide: function(){
        toggleOptions('block');
        hidePop('editDiv');
        showFooter();
        //hideEdit();
    }
};

function togglePrice() {
	if(!showMyPrice) {
		if(!confirm("Are You Sure you want to show prices?\n\nNote: Do Not use the browser 'Back' button when using the Show/Hide Price option.")) {
			return;
		}
	}
	dojo.io.bind(
		{
		url: '/toggleShowPrice.cfm?date=' + Date() + '&showprice=' + IsPriceShow(),
		handler: doPrice,
		mimetype: "text/javascript"
	}
	);
}


function addToFavorites() {
    var sku_modifier = document.getElementById('edit_option_valueID');
    var basesku = document.getElementById('basesku').value;
    if(sku_modifier != null) {
        sku_modifier = sku_modifier.value;
    }
    dojo.io.bind(
    {
                   url: '/ax_commerce/ajax_add_to_favorites.cfm?categoryID=' + my_categoryID + '&productID=' + my_productID + '&sku_modifier=' + sku_modifier + '&basesku=' + basesku,
                   handler: favoriteAdded,
                   mimetype: "text/plain"
    }
    );
    return false;
}
function addToCart() {
    var quantity = dojo.byId('edit_quantity').value;
    var option_field = dojo.byId('edit_option_valueID');
    if(option_field != null) {
        var option_valueID = option_field.value;
        var name = option_field.name;
        if(option_valueID == '') {
            alert('Option is required');
            return false;
        }
    } else {
        var option_valueID = '';
        var name = '';
    }
    var special_instructions = dojo.byId('edit_special_instructions');
    if(special_instructions == null) {
        special_instructions = '';
    } else {
        special_instructions = escape(special_instructions.value);
    }
    var urlstring = 'productID=' + my_productID + '&option_valueID=' + option_valueID + '&option=' + name + '&quantity=' + quantity + '&special_instructions=' + special_instructions;

    dojo.io.bind(
        {
            url: '/ax_commerce/ajax_add_to_cart.cfm?' + urlstring,
            load: function(type, data, evt){
                   updateRecentlyAddedToCart(data);
                   divHandler.hide();
            },
            mimetype: "text/plain"
        }
    );
    return true;
}

selectedITems = "";
function quickViewAddToCart()
{	
	var selectedItems = getSelectedItems($$("#quickViewCart input[type='text']"));
	
	if(!$('add_items'))
	{
		showPop('adding_items');
	}
	else
	{
		selectedITems = $('add_items').innerHTML;
		$('add_items').innerHTML = $('adding_items').innerHTML;
	}

	dojo.io.bind(
	{
		url: '/ax_commerce/ajax_add_multiple_to_cart.cfm',
		method : "post",
		content : {"items" : Object.toJSON(selectedItems) },
		load: function(type, data, evt){
				updateRecentlyAddedToCart(data);
				divHandler.hide();
				if($('adding_items'))$('adding_items').hide();
				if($('add_items'))$('add_items').hide();
				window.scroll(0,0);
				$('add_items').innerHTML = selectedITems;
				
			},
			mimetype: "text/plain"
		}
	);
	
	return true;
}

function getSelectedItems(inputFields)
{
	var SelectedItems = [];
	var SelectedItem = {};
	
	$(inputFields).each(function(inputField)	
	{
		var productAttributes = inputField.name.split("_");
		var currentAttributeIndex = "";
		SelectedItem = {};	
	
		if(inputField.value + 0 > 1)
		{
			$(productAttributes).each(function(attribute, index){
				if(index % 2 == 0) currentAttributeIndex = attribute;
				SelectedItem[currentAttributeIndex] = attribute;
			});
			
			SelectedItem["quantity"] = inputField.value;
			SelectedItem["specialInstruction"] = $("special_instructions_" + SelectedItem["productID"] + "_" + SelectedItem["optionValueID"]).value;
			SelectedItems.push(SelectedItem);
			inputField.value = "";
			$("instr_text_" + SelectedItem["productID"] + "_" +  SelectedItem["optionValueID"]).hide();
			$("instr_img_" + SelectedItem["productID"] + "_" +  SelectedItem["optionValueID"]).show();
		}		
	}
	);
	
	return SelectedItems;
}
// tracks pdf clicks in analytics

function trackClick(element) {
	var url = 'unknown_pdf_from_' + location.href;
	if (element != null) {
		if(element.href != null) {
			url = element.href;
		}
	}

	return pageTracker._trackPageview(url);
}

function isPDF(href) {
	var last = href.length;
	return href.substring(last - 4, last).toLowerCase() == '.pdf';
}

function trackPDFLinks(scriptName) {
	$$('a').each(
		function(e) {
			if (isPDF(e.href)) {
				Event.observe(e, 'click', function(event) {
					trackClick(this);
				});
			}
		}
	);
}

