var dvice = {

    // change the sizes when a colour is changed
    changeColour: function(select_elem, stock_data)
    {

        // check for size select
        var size_elem = document.getElementById('product_size');

        // return if there isn't a size select box
        if (!size_elem || size_elem.tagName.toLowerCase() != "select") {
        	return false;
        }

        // get selected colour
        var cl_id = select_elem.options[select_elem.selectedIndex].id;

        // if there is size data for the colour add it
        if (stock_data[cl_id]) {
        	size_elem.options.length = 0;
        	c = 0;
        	for (guid in stock_data[cl_id]) {
        		size_elem.options[c] = new Option(stock_data[cl_id][guid], guid);
        		c++;
        	}
        } else {
        	// TODO: could this ever happen?
        	// alert('doh');
        }
    },

    switchCurrency: function(a_elem)
    {

    	// get min elements
    	var local_min = document.getElementById('local_currency_min');
    	var us_min = document.getElementById('us_currency_min');

        // get max elements (won't exist sometimes)
        var local_max = document.getElementById('local_currency_max');
        var us_max = document.getElementById('us_currency_max');

        // get switch elements
        var local_a = document.getElementById('local_switch');
        var us_a = document.getElementById('us_switch');

        // bail out if there is no minimum
        if (!local_min) {
        	return false;
        }

        // determine if we're showing local or us
        if (local_min.style.display == '') {

        	// showing local, switch to us
        	local_min.style.display = 'none';
        	us_min.style.display = '';

            // switch maximums if they exist
        	if (local_max) {
	            local_max.style.display = 'none';
	            us_max.style.display = '';
        	}

        	local_a.style.display = 'none';
        	us_a.style.display = '';


        } else {

            // showing local, switch to us
            local_min.style.display = '';
            us_min.style.display = 'none';

            // switch maximums if they exist
            if (local_max) {
                local_max.style.display = '';
                us_max.style.display = 'none';
            }

            local_a.style.display = '';
            us_a.style.display = 'none';

        }

    },

    popUp: function(URL) {
        day = new Date();
        id = day.getTime();
        return eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=500,height=350');");
    },


    toggleProductReview: function() {

        var div_elem = document.getElementById('product-review-form');

        if (div_elem) {

        	if (div_elem.style.display == '') {
        		div_elem.style.display = 'none';
        	} else {
        		div_elem.style.display = '';
        	}
        }
    },


    addToCart: function() {

    	var product_colour = document.getElementById('product_colour');
    	var product_colour_id;

    	var product_size = document.getElementById('product_size');
    	var product_size_id;

    	var product_id_elem = document.getElementById('product_id');

    	var redirect_page_elem = document.getElementById('redirect_page');

        if (product_colour && product_size && product_id_elem && redirect_page_elem) {

            // get id
            product_id = product_id_elem.value;

            // get redirect page
            redirect_page = redirect_page_elem.value;

            // get value
            if (product_colour.tagName == "SELECT") {
                product_colour_id = product_colour.options[product_colour.selectedIndex].value;
            } else {
            	product_colour_id = product_colour.value;
            }

            // get size
            if (product_size.tagName == "SELECT") {
                product_size_id = product_size.options[product_size.selectedIndex].value;
            } else {
                product_size_id = product_size.value;
            }

            //console.log("colour: " + product_colour_id);
            //console.log("size: " + product_size_id);
            //console.log("id: " + product_id);

            if (product_size_id.length == 32) {
                ps_guid = product_size_id;
            } else if (product_colour_id.length == 32){
            	ps_guid = product_colour_id;
            } else {
            	alert('no guid');
            	return;
            }

            window.location = "/add-to-cart?cart_opt=add&cart_obj_guid=" + ps_guid + "&r=" + escape(redirect_page);

        } else {
        	alert('no colour or size or product_id');
        }

    },

    updateItem: function(prod_guid) {

        var product_qty_elem = document.getElementById('product_qty_' + prod_guid);

        var redirect_page_elem = document.getElementById('redirect_page');

        if (product_qty_elem && redirect_page_elem) {

            // get redirect page
            redirect_page = redirect_page_elem.value;

            // get qty
            var product_qty = product_qty_elem.options[product_qty_elem.selectedIndex].value;

            // redirect
            window.location = "/add-to-cart?cart_opt=update&cart_obj_guid=" + escape(prod_guid) + "&cart_quantity=" + product_qty + "&r=" + escape(redirect_page);

        } else {
        	alert('no colour or size or product_id');
        }

    }

}

function trackMailListSubmit(type) {
    if((typeof pageTracker)!="undefined") {
        pageTracker._trackPageview("/mailing-list/" + type + "/");
    } else {
        //alert(type);
    }
}

function openVideo(title, guid) {
    tb_show("D.vice TV - " + title,'/dvice-tv/video/' + guid + '?height=410&width=480', "video")
    
    return false;
}
function openFlash(title, guid, w, h) {
    h=h+25;
    w=w+20;
    
    tb_show("D.vice TV - " + title,'/dvice-tv/flash-viewer/' + guid + '?height=' + h + '&width=' + w, "flash")
    
    return false;
}

var partySent = false;

function dviceDisableFormResending() {    
    if(partySent) {
        return false;
    }
    
    s3bt = document.getElementById('step3_button');
    s3bt.disabled = true;
    
    partySent = true;
}