var no_basket_p = 1;
var return_url = '';
var tabs_array = new Array();
//tabs_array[0]='features_list';
tabs_array[0]='accessories_list';
tabs_array[1]='substitution_list';
tabs_array[2]='defective_products';
tabs_array[3]='offers';
tabs_array[4]='files';
tabs_array[5]='reviews';
tabs_array[6]='opinions';
tabs_array[7]='compatibility';

/* JAVASCRIPT PROCS FOR STORES*/
function toggle_visibility(element_id) {
    Effect.toggle(element_id,'appear',{duration:0});
}

function show_element(element_id) {
    element = document.getElementById(element_id);
    element.style.display = 'inline-block';
}

function hide_element(element_id) {
    element = document.getElementById(element_id);
    element.style.display = 'none';
}

function switch_visibility(element_id,default_value) {

    element = document.getElementById(element_id);
    if (element) {
	if (element.style.display == '') {
	    element.style.display = default_value;
	}
	if (element.style.display == 'none') {
	    element.style.display = 'inline-block';
	} else {
	    element.style.display = 'none';
	}
    }
}

/* SET A PRODUCT TO DELETE IN THE ORDER CONFIRMATION PROCESS */
var localization_strings = {
    OrderProductDelete:{
	'en_US':'Delete this article from basket',
	'es_ES':'Eliminar este art\u00edculo de la cesta'
    },
    OrderProductRecover:{
	'en_US':'Recover this article',
	'es_ES':'Recuperar este art\u00edculo'
    }
};

function switch_order_product(product_id,image,defective_type,type) {
    var widget = document.getElementById('qty_'+type+'_'+product_id+'_'+defective_type);
    var row = document.getElementById('row_'+type+'_'+product_id+'_'+defective_type);
    if (row.className!='') {
	if (widget != null) {
	    widget.disabled = '';
	}
	row.className='';
	image.src='/resources/product-store/images/trash.png';
	image.title=localization_strings['OrderProductDelete']['es_ES'];
	image.alt=localization_strings['OrderProductDelete']['es_ES'];
    } else {
	if (widget != null) {
	    widget.disabled = 'disabled';
	}
	row.className='removed-row';
	image.src='/resources/product-store/images/undo.png';
	image.title=localization_strings['OrderProductRecover']['es_ES'];
	image.alt=localization_strings['OrderProductRecover']['es_ES'];
    }
}

/* Show offers and products popups */
function substitution_product_popup_content(subst_product,product_qty,switch_product,switch_defective,ret_url) {
    display_product=subst_product;
    default_qty = product_qty;
    if (ret_url) {
	return_url = ret_url;
    } 

    new Ajax.Request('/product-store/lib/product-view',{asynchronous:true,method:'post',parameters:'product_id='+display_product+'&complete_p=1&no_basket_p='+no_basket_p+'&switch_product='+switch_product+'&switch_defective='+switch_defective+'&return_url='+return_url+'&show_extra=0&default_qty='+default_qty+'&',onSuccess: function(transport) {show_popup(transport.responseText,'product-enhanced-popup');}}); 
}

function offer_popup_content(offer_id) {
    show_offer_id = offer_id;
    new Ajax.Request('/product-store/lib/offer-products',{asynchronous:true,method:'post',parameters:'admin_p=0&offer_id='+show_offer_id,onSuccess: function(transport) {show_popup(transport.responseText,'offer-popup');}}); 
}

function show_popup(content,encap_id) {
    popup_content='<div id="'+encap_id+'">'+content+'</div>';
    return overlib(popup_content,STICKY,CLOSETEXT,'<img src="/resources/eliminar.gif" width="15px" border="0">',CAPTION,'&nbsp;',CLOSECLICK,BGCOLOR,'#000000',FGCOLOR,'#ffffff',WIDTH,900);
}

/* Manage product view tabs */
function switch_product_tabs (product_id,view_tab) {
    var i=0;
    var j=0;
    for (i = 0;i<tabs_array.length;i++) {
	var element = document.getElementById(tabs_array[i]+'_'+product_id);
	if (element != null) {
	    element.style.display='none';
	}
    }    
    var lis = document.getElementById('product-tabs-menu_'+product_id).getElementsByTagName('li');
    for (j=0;j < lis.length;j++) {
	lis[j].id='';
    }
    var anchors = document.getElementById('product-tabs-menu_'+product_id).getElementsByTagName('a');
    for (j=0;j < anchors.length;j++) {
	anchors[j].id='';
    }	
    
    //toggle_visibility(view_tab);
    switch_visibility(view_tab+'_'+product_id,'none');

    //document.getElementById(view_tab+'_tab').id='product-view-selected';
    var lis = document.getElementById(view_tab+'_tab_'+product_id).getElementsByTagName('li');
    for (j=0;j < lis.length;j++) {
	lis[j].id='product-view-selected';
    }
    var anchors = document.getElementById(view_tab+'_tab_'+product_id).getElementsByTagName('a');
    for (j=0;j < anchors.length;j++) {
	anchors[j].id='product-view-selected';
    }	    
}

/* MANAGE LOCATOR TYPES TAB */
function switch_locator_tabs(product_id,category_name) {
    var element = document.getElementById('locator-result');
    var divs = document.getElementById('locator-result').getElementsByTagName('div');
    var i=0;
    var pattern=/product-accessories-tab/;
    for (i=0;i < divs.length; i++) {
	if (divs[i].className == 'buscador-accessories-list') {
	    divs[i].style.display='none';
	} else if (pattern.test(divs[i].id)) {
   	    divs[i].className = 'buscador-tab product-tabs-menu';
	}
    }
    if (category_name != '') {
	var selected_div = document.getElementById('product-accessories-tab-'+product_id+'-'+category_name);
	if (selected_div) {
	    selected_div.className = 'buscador-tab product-tabs-menu-selected';
	}
    }
}

function update_disabled_form_data(element_id_one,element_id_two,check_element) {
    if(check_element.checked) {
	element_id_one.value=element_id_two.value;
    }
}

/* DISABLE FORM ELEMENTS ON CLICK ON NEW PROFESSIONALS REGISTER */
function switch_register_form_fields(value,element,substitution_element) {
    var admin_fields = new Array();
    var contact_fields = new Array();
    var i=0;

    contact_fields[0]=substitution_element+'_first_names';
    contact_fields[1]=substitution_element+'_last_name';
    contact_fields[2]=substitution_element+'_email';

    if (element == 'admin') {
	admin_fields[0]='admin_first_names';
	admin_fields[1]='admin_last_name';
	admin_fields[2]='admin_email';
	admin_fields[3]='admin_position';
	contact_fields[3]=substitution_element+'_position';

    } else {
	admin_fields[0]='filling_first_names';
	admin_fields[1]='filling_last_name';
	admin_fields[2]='filling_email';
    }
    for (i=0;i < admin_fields.length; i++) {
	if (!document.getElementById(admin_fields[i]).disabled && value) {
	    document.getElementById(admin_fields[i]).disabled = 'true';
	    document.getElementById(admin_fields[i]).value=document.getElementById(contact_fields[i]).value;
	} else if (document.getElementById(admin_fields[i]).disabled && !value) {
	    document.getElementById(admin_fields[i]).disabled = '';
	}
    }
}

/* SELECT LEFT MENU ITEM */
function select_left_menu_item(element_id,class_name) {
    var element = document.getElementById(element_id);
    element.setAttribute("class",class_name);
    element.setAttribute("className",class_name);
}

/* CHECK AVAILABILITY */
function availability_popup(display_product) {
    new Ajax.Request('/product-store/lib/availability-check',{asynchronous:true,method:'post',parameters:'product_id='+display_product,onSuccess: function(transport) {show_popup(transport.responseText,'product-enhanced-popup');}});
}

function compatibility_brand_filter(brand) {
    jQuery(".brand-compatible").parent().parent().hide();
    jQuery("."+brand).parent().parent().show();
}

function toggle_feature_elements(product_id) {
    jQuery("#all-product-features-"+product_id).find(".extra-features").toggle();
    if (jQuery("#view-features-more-"+product_id).html() == "+") {
	jQuery("#view-features-more-"+product_id).html("-");
    } else {
	jQuery("#view-features-more-"+product_id).html("+");
    }
}
