function callBackAddObs(res){

}

function add_obs(){
	obs = getValue('e_obs');
	sendAjax('ajax.ecommerce.php', 'action=addObs&e_obs='+obs, 'callBackAddObs');
}

function callBackValidateFormRegistration(res){
	if (res != 0) {
		if (res == 2){
			setValue('h_do_registration', 2);
			submitForm('f_registration');
		}
		else{
			infoObj = eval('(' + res + ')');
			flag = infoObj[0] == 1 && infoObj[1] == 1;
			if (infoObj[0] == 0){
				setDisplay('error_msg', '');
				setClass('tr_e_username', 'f_textfield f_error');
			}
			if (infoObj[1] == 0){
				setDisplay('error_msg', '');
				setClass('tr_e_email', 'f_textfield f_error');
			}
			if (flag){
				setValue('h_do_registration', 1);
				submitForm('f_registration');
			}
		}
	}
}

function callBackValidateEditMyUser(res){
	var flag = res == 1;
	if (flag) {
		setValue('h_data_edit_my_user', 1);
		submitForm('f_data_edit_my_user');
	}
	else{
		setDisplay('error_msg', '');
		setClass('tr_e_username', 'f_textfield f_error');
	}
}

function validateEditMyUser(){
	f = getElement('f_data_edit_my_user');
	if (validateForm(f)){
		username = getValue('e_username');
		h_username = getValue('h_e_username');
		sendAjax('ajax.registration.php', 'action=validateEditMyUser&h_username='+h_username+'&username='+username, 'callBackValidateEditMyUser');
	}
}

function callBackValidateFormEditPersonalInfo(res){
	if (res != 0) {
		infoObj = eval('(' + res + ')');
		flag = infoObj[0] == 1 && infoObj[1] == 1;
		if (infoObj[0] == 0){
			setDisplay('error_msg', '');
			setClass('tr_e_username', 'f_textfield f_error');
		}
		if (infoObj[1] == 0){
			setDisplay('error_msg', '');
			setClass('tr_e_email', 'f_textfield f_error');
		}
		if (flag){
			setValue('h_do_edit_personal_info', 1);
			submitForm('f_registration');
		}
	}

}

function validateFormEditPersonalInfo(){
f = getElement('f_registration');
	if (validateForm(f)){
		email = getValue('e_email');
		username = getValue('e_username');
		h_email = getValue('h_e_email');
		h_username = getValue('h_e_username');
		sendAjax('ajax.registration.php', 'action=validateEditPersonalInfo&h_email='+h_email+'&email='+email+'&h_username='+h_username+'&username='+username, 'callBackValidateFormEditPersonalInfo');
	}
}


function validateFormRegistration(){
	f = getElement('f_registration');
	if (validateForm(f)){
		email = getValue('e_email');
		username = getValue('e_username');
		nif = getValue('e_nif');
		sendAjax('ajax.registration.php', 'action=validateRegistration&email='+email+'&username='+username+'&nif='+nif, 'callBackValidateFormRegistration');
	}
	else{
	}
}

function callBackGetStateName(res){
	if (res != 0){
		infoObj = eval('(' + res + ')');
		setValue('e_state',infoObj[1]);
		setValue('h_e_state',infoObj[0]);
	}
	else{
		setValue('e_state','');
		setValue('h_e_state','');
	}
}

function callBackGetStateDeliveryName(res){
	if (res != 0) {
		infoObj = eval('(' + res + ')');
		setValue('e_state_delivery', infoObj[1]);
		setValue('h_e_state_delivery', infoObj[0]);
	}
	else {
		setValue('e_state_delivery', '');
		setValue('h_e_state_delivery', '');
	}
}

function callBackGetStateCompanyName(res){
	if (res != 0) {
		infoObj = eval('(' + res + ')');
		setValue('e_state_company', infoObj[1]);
		setValue('h_e_state_company', infoObj[0]);
	}
	else {
		setValue('e_state_company', '');
		setValue('h_e_state_company', '');
	}
}

function callBackGetMembersZip(res){
	res = URLDecode(res);
	selectElem = getElement('s_member_zip');
	loadSelectWithJsonObject("s_member_zip", res);
	if (selectElem.length == 0) {
		elemZip = getElement('s_member_zip');
		elemZip.disabled = true;
		setDisplay("tr_s_member_zip", 'none');
		setDisplay("hint_member_all", '');
		elemStates = getElement('s_state_member');
		//elemStates.disabled = false;
		setDisplay("tr_s_state_member", 'none');
	}
	else {
		elem1 = getElement('s_state_member');
		elem1.disabled = true;
		elem2 = getElement('s_member_all');
		elem2.disabled = true;
		elem3 = getElement('s_member_zip');
		elem3.disabled = false;
		setDisplay("hint_member_all", 'none');
//		setDisplay('tr_s_member_all', 'true');
		setDisplay('tr_s_state_member', 'none');
		setDisplay("tr_s_member_zip", 'none');
	}
}

function callBackGetMembersState(res){
	res = URLDecode(res);
	selectElem = getElement('s_member_all');
	loadSelectWithJsonObject("s_member_all", res);
	if (selectElem.length == 0) {
		selectElem.disabled = false;
		setDisplay("tr_s_state_member", '');
		setDisplay("tr_s_member_all", '');
		loadSelectWithJsonObject("s_member_all", res);
		alert('No hemos ubicado ningún Socio en su provincia, \npor favor seleccione uno de la lista');
//		location.reload();
	}
	else {
		selectElem.disabled = false;
		setDisplay("tr_s_member_all", '');
		setDisplay("tr_s_state_member", 'none');
	}
	selectElem1 = getElement('s_member_zip');
	selectElem.value = selectElem1.value;
}

function getMembersState(){
	zip = getValue('e_zip');
	state = getValue('h_e_state');
	sendAjax('ajax.registration.php', 'action=getMembers_by_state&state='+state+'&zip='+zip, 'callBackGetMembersState');
}

function callBackGetMembersStateVacio(res){
	res = URLDecode(res);
	selectElem = getElement('s_member_all');
	loadSelectWithJsonObject("s_member_all", res);
}

function getMembersStateVacio(){
	state = getValue('s_state_member');
	sendAjax('ajax.registration.php', 'action=getMembers_by_state&state='+state, 'callBackGetMembersStateVacio');
}

function callBackGetMembersStateForce(res){
	res = URLDecode(res);
	selectElem = getElement('select_s_member_zip');
	loadSelectWithJsonObject("select_s_member_zip", res);
	if (selectElem.length == 0) {
		selectElem.disabled = false;
		setDisplay("tr_s_member_all", '');
		//location.reload();
	}
	else {
		//selectElem.disabled = false;
		//setDisplay("tr_s_member_all", '');
	}
}

function getMembersForce(){
	state = getValue('s_state_member');
	sendAjax('ajax.registration.php', 'action=getMembers_by_state&state='+state, 'callBackGetMembersStateForce');

}



function callBackGetAddFavorites(res){
	if (res > 0) {
		setDisplay("favorite_added_"+res, "");
		setDisplay("favorite_to_add_"+res, "none");
	}
}

function add_to_favorites(id_product){
	sendAjax('ajax.ecommerce.php', 'action=addfovorites&id_product='+id_product, 'callBackGetAddFavorites');
}

function callBackDeleteFavorito(res){
	if (res != 0){
		setDisplay("li_favorito_"+res, "none");
		total = parseInt(getValue('h_total_favorites')) - 1;
		setInnerHtml('span_total_favoritos_1', total);
		setInnerHtml('span_total_favoritos_2', total);
		setInnerHtml('span_total_favoritos_3', total);
	}
	location.reload(true);
}

function delete_favorito(id_product){
	sendAjax('ajax.ecommerce.php', 'action=deleteFavorito&id_product='+id_product,'callBackDeleteFavorito');
}

function callBackAddToCart(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		setDisplay("already_purchased", "");
		setInnerHtml('sh_cart_status_subtotal', infoObj[1]);
		setInnerHtml('sh_cart_status_cant_prod', infoObj[0]);
		setValue('h_sh_cart_status_cant_prod', infoObj[0]);
	}
}

function addToCart(id_product){
	valor = getValue('suggest_product');
	id_variant = getValue('variant_select');
	if (valor.length > 0 ){
		sendAjax('ajax.ecommerce.php', 'action=addToCart&id_product='+id_product+'&qty='+valor+'&id_variant='+id_variant,'callBackAddToCart');
	}
}

function addToCartDirect(codoe, cantidad){
	//alert(idproduct);
	//alert(cantidad);
	formato="000000";
	valor = document.getElementById("codoe").value;
	valor = formato.substring(0,formato.length-valor.length)+valor;
	
	document.getElementById("codoe").value = valor;
		
	if(codoe.length > 0 && cantidad.length > 0) {
	
		//cantidad = calculateMod(cantidad,6);
	
		sendAjax('ajax.ecommerce.php', 'action=addToCartDirect&codoe='+valor+'&qty='+cantidad,'callBackAddToCartDirect');
	}else{
		alert('Por favor introduzca el código de producto y la cantidad solicitada');
	}
	
	
}

function callBackAddToCartDirect(res){
	if (res != 0) {
		//infoObj = eval('('+res+')');
		//setDisplay("already_purchased", "");
		//setInnerHtml('sh_cart_status_subtotal', infoObj[1]);
		//setInnerHtml('sh_cart_status_cant_prod', infoObj[0]);
		//setValue('h_sh_cart_status_cant_prod', infoObj[0]);
		//alert("ok");
		window.location = "mi_cesta.php";
	} else {
		//alert("ko");
		setInnerHtml('already_purchased', "<b>El código solicitado: "+document.getElementById("codoe").value+" no est&aacute; disponible.</b> Utilice el b&uacute;scador o p&oacute;ngase en contacto con su tienda habitual.");
		setDisplay("already_purchased", ""); //Modifica la propiedad display del div.
		document.getElementById("codoe").focus();
	}
}

function muestraProductoRapido(codoe) {
	formato="000000";
	valor = document.getElementById("codoe").value;
	valor = formato.substring(0,formato.length-valor.length)+valor;
	
	document.getElementById("codoe").value = valor;
		
	if(codoe.length > 0) {
	
		//cantidad = calculateMod(cantidad,6);
	
		sendAjax('ajax.ecommerce.php', 'action=muestraProductoRapido&codoe='+valor,'callBackmuestraProductoRapido');
	}else{
		alert('Por favor introduzca el código de producto.');
	}
}

function callBackmuestraProductoRapido(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		//setDisplay("already_purchased", "");
		//setInnerHtml('sh_cart_status_subtotal', infoObj[1]);
		//setInnerHtml('sh_cart_status_cant_prod', infoObj[0]);
		//setValue('h_sh_cart_status_cant_prod', infoObj[0]);
		//alert("ok");
		$nombre = infoObj[1];
		$multiplo = infoObj[2];
		setInnerHtml('already_purchased', "<b>"+$nombre+".</b><br>Este producto se vende en lotes de "+$multiplo+" uds:");
		setDisplay("already_purchased", ""); //Modifica la propiedad display del div.
		document.getElementById("cantidad").focus();
		//window.location = "mi_cesta.php";
	} else {
		//alert("ko");
		setInnerHtml('already_purchased', "<b>El código solicitado: "+document.getElementById("codoe").value+" no est&aacute; disponible.</b> Utilice el b&uacute;scador o p&oacute;ngase en contacto con su tienda habitual.");
		setDisplay("already_purchased", ""); //Modifica la propiedad display del div.
		document.getElementById("codoe").focus();
	}
}
function callBackAddToCartSpecial(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		setDisplay("already_purchased", "");
		setInnerHtml('sh_cart_status_subtotal', infoObj[1]);
		setInnerHtml('sh_cart_status_cant_prod', infoObj[0]);
		setValue('h_sh_cart_status_cant_prod', infoObj[0]);
	}
}

function addToCartSpecial(id_special){
	valor = getValue('suggest_special');
	if (valor.length > 0 ){
		sendAjax('ajax.ecommerce.php', 'action=addToCartSpecial&id_special='+id_special+'&qty='+valor,'callBackAddToCartSpecial');
	}
}

function callBackIsInCart(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		if (infoObj[0] == 1) {
			setDisplay("already_purchased", "block;");
		}
		else {
			setDisplay("already_purchased", "none");
		}
		setInnerHtml('unit_price', infoObj[1]);
	}
	setValue('suggest_product', '');
	setInnerHtml('subtotal', infoObj[2]);
	setInnerHtml('sh_cart_status_subtotal', infoObj[2]);
}
function isInCart(id_product){
	id_variant = getValue('variant_select');
	setInnerHtml('unit_price', '<input type="button" value="" id="" class="reloading" disabled="disabled" title="Actualizando..." />');
	setInnerHtml('subtotal', '<input type="button" value="" id="" class="reloading" disabled="disabled" title="Actualizando..." />');
	sendAjax('ajax.ecommerce.php', 'action=isInCart&id_product='+id_product+'&id_variant='+id_variant,'callBackIsInCart');
}

function callBackDelFromCart(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		setInnerHtml('total_price', infoObj[1]);
		setInnerHtml('sh_cart_status_subtotal', infoObj[1]);
		total = parseInt(getValue('h_sh_cart_status_cant_prod')) - 1;
		setInnerHtml('sh_cart_status_cant_prod', total);
		setValue('h_sh_cart_status_cant_prod', total);
		elem = getElement('ico_checkout');
		if (total == 0){
			elem.disabled=true;
		}
		else{
			elem.disabled=false;
		}
	}
}

function callBackDelFromCartchanged(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		setInnerHtml('total_price', infoObj[1]);
		setInnerHtml('sh_cart_status_subtotal', infoObj[1]);
		total = parseInt(getValue('h_sh_cart_status_cant_prod')) - 1;
		setInnerHtml('sh_cart_status_cant_prod', total);
		setValue('h_sh_cart_status_cant_prod', total);
		elem = getElement('ico_checkout');
		if (total == 0){
			elem.disabled=true;
		}
		else{
			elem.disabled=false;
		}
		MM_goToURL('top', 'mi_cesta.php');
		return document.MM_returnValue;
	}
}

function delFromCartchanged(id_product, id_variant){
	setInnerHtml('total_price', '<input type="button" value="" id="" class="reloading" disabled="disabled" title="Actualizando..." />');
	setDisplay('tr_product_'+id_product+'_'+id_variant, 'none');
	sendAjax('ajax.ecommerce.php', 'action=delFromCart&id_product='+id_product+'&id_variant='+id_variant,'callBackDelFromCartchanged');

}
function delFromCartSpecialchanged(id_special, id_variant){
	setInnerHtml('total_price', '<input type="button" value="" id="" class="reloading" disabled="disabled" title="Actualizando..." />');
	setDisplay('tr_product_'+id_special+'_'+id_variant, 'none');
	sendAjax('ajax.ecommerce.php', 'action=delFromCartSpecial&id_special='+id_special, 'callBackDelFromCartSpecialchanged');

}

function delFromCart(id_product, id_variant){
	setInnerHtml('total_price', '<input type="button" value="" id="" class="reloading" disabled="disabled" title="Actualizando..." />');
	setDisplay('tr_product_'+id_product+'_'+id_variant, 'none');
	sendAjax('ajax.ecommerce.php', 'action=delFromCart&id_product='+id_product+'&id_variant='+id_variant,'callBackDelFromCart');
}

function callBackDelFromCartSpecialchanged(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		setInnerHtml('total_price', infoObj[1]);
		setInnerHtml('sh_cart_status_subtotal', infoObj[1]);
		total = parseInt(getValue('h_sh_cart_status_cant_prod')) - 1;
		setInnerHtml('sh_cart_status_cant_prod', total);
		setValue('h_sh_cart_status_cant_prod', total);
		MM_goToURL('top', 'mi_cesta.php');
		return document.MM_returnValue;
	}
}

function callBackDelFromCartSpecial(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		setInnerHtml('total_price', infoObj[1]);
		setInnerHtml('sh_cart_status_subtotal', infoObj[1]);
		total = parseInt(getValue('h_sh_cart_status_cant_prod')) - 1;
		setInnerHtml('sh_cart_status_cant_prod', total);
		setValue('h_sh_cart_status_cant_prod', total);
	}
}

function delFromCartSpecial(id_special, id_variant){
	setInnerHtml('total_price', '<input type="button" value="" id="" class="reloading" disabled="disabled" title="Actualizando..." />');
	setDisplay('tr_product_'+id_special+'_'+id_variant, 'none');
	sendAjax('ajax.ecommerce.php', 'action=delFromCartSpecial&id_special='+id_special, 'callBackDelFromCartSpecial');
}

function callBackDelFromSavedOrder(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		setInnerHtml('total_price', infoObj[1]);
	}
}

function delFromSavedOrder(id_product, id_variant, idOrder){
	setInnerHtml('total_price', '<input type="button" value="" id="" class="reloading" disabled="disabled" title="Actualizando..." />');
	setDisplay('tr_product_'+id_product+'_'+id_variant, 'none');
	sendAjax('ajax.ecommerce.php', 'action=delFromSavedOrder&id_product='+id_product+'&id_variant='+id_variant+'&id='+idOrder,'callBackDelFromSavedOrder');
}

function callBackLoadSavedOrderToCart(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		setInnerHtml('sh_cart_status_subtotal', infoObj[1]);
		setInnerHtml('sh_cart_status_cant_prod', infoObj[0]);
	}
}

function loadSavedOrderToCart(idOrder){
	sendAjax('ajax.ecommerce.php', 'action=loadSavedOrderToCart','callBackLoadSavedOrderToCart');
}

function loadSavedOrderToCartFromDB(idOrder){
	sendAjax('ajax.ecommerce.php', 'action=loadSavedOrderToCartFromDB&id='+idOrder,'callBackLoadSavedOrderToCart');
}

function callBackdeleteSavedOrder(res){
}

function deleteSavedOrder(idOrder){
	setDisplay('tr_order_'+idOrder, 'none');
	sendAjax('ajax.ecommerce.php', 'action=deleteSavedOrder&idOrder='+idOrder,'callBackdeleteSavedOrder');
}

function callBackAddToSavedOrder(res){
	if (res != 0) {
		infoObj = eval('('+res+')');
		idOrder = infoObj[0];
		setInnerHtml('s_cant_prod_'+idOrder, infoObj[2]);
		setInnerHtml('s_cant_unid_'+idOrder, infoObj[1]);
		setInnerHtml('s_total_price_'+idOrder, infoObj[3]);
		setDisplay('a_is_in_pedido_'+idOrder, 'none');
		setDisplay('sh_a_is_in_pedido_'+idOrder, '');
		setClass('sh_a_is_in_pedido_'+idOrder, 'ico ico_already_in_this_list');
		setClass('a_is_in_pedido_'+idOrder, 'ico ico_already_in_this_list');
	}
}

function addToSavedOrder(idOrder, id_product,id_variant){
	sendAjax('ajax.ecommerce.php', 'action=addToSavedOrder&idOrder='+idOrder+'&id_product='+id_product+'&id_variant='+id_variant, 'callBackAddToSavedOrder')
}

function LimpiaCombo(combo) {
	while(combo.length > 0) {
		combo.remove(combo.length-1);
	}
}

function LlenarCombo(json, combo) {
	//Primer valor
	combo.options[0] = new Option('Todos',0);
	
	for (var i=0;i<json.length;i++) {
		combo.options[combo.length] = new Option(json[i].nombre, json[i].id);
	}
}

function CambiaCombo(origen,destino) {
	destino = document.getElementById(destino);
	//Limpiamos combo
	LimpiaCombo(destino);
	//Verificamos que hay algo seleccionado en el origen
	if(origen.options[origen.selectedIndex].value !='') {
		//Llamada a ajax.
		$.ajax({
			type: 'get',
			dataType: 'json',
			url: 'ajax.ecommerce.php?action=bcmod',
			data: {im: origen.options[origen.selectedIndex].value},
			success: function(json) { LlenarCombo(json,destino);}
		});
	}
	
}

