/*-----------------------------------------------------------------------------
 * Visual Product Options - Javascript Functions
 *-----------------------------------------------------------------------------
 * ems_visual_options_custom.js
 *-----------------------------------------------------------------------------
 * Author:   Estelle Winterflood
 * Email:    cubecart@expandingbrain.com
 * Store:    http://cubecart.expandingbrain.com
 *
 * Date:     July 11, 2008
 * For CubeCart Version:  3.0.x
 *-----------------------------------------------------------------------------
 * SOFTWARE LICENSE AGREEMENT:
 * You must own a valid license for this modification to use it on your
 * CubeCart™ store. Licenses for this modification can be purchased from
 * Estelle Winterflood using the URL above. One license permits you to install
 * this modification on a single CubeCart installation only. This non-exclusive
 * license grants you certain rights to use the modification and is not an
 * agreement for sale of the modification or any portion of it. The
 * modification and accompanied documentation may not be sublicensed, sold,
 * leased, rented, lent, or given away to another person or entity. This
 * modification and accompanied documentation is the intellectual property of
 * Estelle Winterflood.
 *-----------------------------------------------------------------------------
 * DISCLAIMER:
 * The modification is provided on an "AS IS" basis, without warranty of
 * any kind, including without limitation the warranties of merchantability,
 * fitness for a particular purpose and non-infringement. The entire risk
 * as to the quality and performance of the Software is borne by you.
 * Should the modification prove defective, you and not the author assume 
 * the entire cost of any service and repair. 
 *-----------------------------------------------------------------------------
 */

function vpo_gv(o){var a = [];for(var i=0; i<o.length; ++i) {for(var j=0; o[i] && j<o[i].length; ++j) {if (o[i][j].options)a.push(o[i][j].options[o[i][j].selectedIndex].value);else if (o[i][j].checked)a.push(o[i][j].value);else if (o[i][j].length) {for(var k=0; o[i][j] && k<o[i][j].length; ++k) {if (o[i][j][k].options)a.push(o[i][j][k].options[o[i][j][k].selectedIndex].value);else if (o[i][j][k].checked)a.push(o[i][j][k].value);}}}}return a;}

function determine_selected_assign_id(radio_element_name) {
	var e = [];
	e[0] = document.getElementsByName(radio_element_name);
	var selected_assign_id = vpo_gv(e);
	return selected_assign_id;
}

function select_radio(radio_element_id) {
	var e = document.getElementById(radio_element_id);
	e.checked=true; 
}

function apply_class_to_div(div_element_id, class_name) {
	var d = document.getElementById(div_element_id);
	d.className = class_name;
}

function vo_radio_onmousedown(option_id, class_name_unselected) {
	var prev_assign_id = determine_selected_assign_id('productOptions_'+option_id);
	apply_class_to_div('po_div_'+prev_assign_id, class_name_unselected);
}
function vo_radio_onclick(assign_id, class_name_selected) {
	apply_class_to_div('po_div_'+assign_id, class_name_selected);
}
function vo_link_onclick(option_id, assign_id, class_name_unselected, class_name_selected) {
	var prev_assign_id = determine_selected_assign_id('productOptions_'+option_id);
	apply_class_to_div('po_div_'+prev_assign_id, class_name_unselected);
	apply_class_to_div('po_div_'+assign_id, class_name_selected);
	select_radio('po_'+assign_id);
}

/* CUSTOMIZATIONS */
function gi(o){var a = [];for(var i=0; i<o.length; ++i) {for(var j=0; o[i] && j<o[i].length; ++j) {if (o[i][j].options)a.push(o[i][j].options[o[i][j].selectedIndex].id);else if (o[i][j].checked)a.push(o[i][j].id);else if (o[i][j].length) {for(var k=0; o[i][j] && k<o[i][j].length; ++k) {if (o[i][j][k].options)a.push(o[i][j][k].options[o[i][j][k].selectedIndex].id);else if (o[i][j][k].checked)a.push(o[i][j][k].id);}}}}return a;}

function vo_switch_text(option_id, text_id) {
	vo_switch(option_id, text_id, 'text');
}
function vo_switch_image(option_id, image_id) {
	vo_switch(option_id, image_id, 'image');
}

function vo_switch(option_id, dest_id, type) {
    var dest = document.getElementById(dest_id);
    var done = false;
    var els_array = Array();
    els_array[0] = document.getElementsByName('productOptions_'+option_id);
    els_array[1] = document.getElementsByName('productOptions_'+option_id+'[]');
    var bla = 'bla';
    if (dest && (els_array[0][0] || els_array[1][0])) {
        var ids = gi(els_array);
        if (ids && ids.length == 1) {
            var arr = ids[0].split(':');
            if (arr && arr[1] && arr[1].length > 0) {
                if (type == 'image') {
                    //dest.src = 'images/uploads/' + arr[1];
                    dest.src = arr[1];
                    done = true;
                } else if (type == 'text') {
                    dest.innerHTML = arr[1];
                    done = true;
                }
            }
        } else if (ids && ids.length > 1 && type == 'text') {
            var checkboxes = Array();
            for (var i=0; i<ids.length; i++) {
                var arr = ids[i].split(':');
                if (arr && arr[1] && arr[1].length > 0) {
                    checkboxes.push(arr[1]);
                }
            }
            dest.innerHTML = checkboxes.join(', ');
            done = true;
        }
	}
    if (!done) {
        if (type == 'image') {
            dest.src = 'images/general/px.gif';
        } else if (type == 'text') {
            dest.innerHTML = '';
        }
    }
}

function vo_setSelection(element_name, option_value) {
    if (option_value == '')
        return;
    elems = document.getElementsByName(element_name);
    for (var i=0; elems && i<elems.length; i++) {
        var el = elems[i];
        if (el && el.length && el.options) {
            for (var j=0; j < el.length; j++) {
                if (el[j].value == option_value) {
                    el[j].selected = true;
                }
            }
        }
    }
}

