

$(document).ready(function(){
    $("#busqueda-standard").click(function(){
		document.getElementById("advancedSearch").value = "false";
		$("#busqueda-standard-cont").slideDown(500);
		$("#busqueda-avanzada-cont").slideUp(250);

       
		$("#toolbar-advanced").css("background-color", "#e4e4e4");
    });
    
    $("#busqueda-avanzada").click(function(){
		busquedaavanzada_click();
    });
});


function busquedaavanzada_click() {
		
		if ((document.frmSearch.categoriaValue.value=='') || (checkselected(document.frmSearch.stype)!='categoria')) {
			document.getElementById('alertavanzada').style.display = 'block';
			mytime = setTimeout("ocultaalert()", 10000); 
		} else {
			if (tieneToolbar()) {
				document.getElementById("advancedSearch").value = "true";
				document.getElementById('alertavanzada').style.display = 'none';
				$("#busqueda-avanzada-cont").slideDown(500);
				$("#busqueda-standard-cont").slideUp(250);
				$("#toolbar-advanced").css("background-color", "#e2cecf");
				cargaToolbarAdvanced();
			} else {
				alert('Esta categoría no dispone de búsqueda avanzada');
			}
			
			
		}

}

function checktoolbars() {
	if (gup('advancedSearch')=='true') {
		busquedaavanzada_click();
	}
}

function tieneToolbar() {
	var c = document.getElementById("categoriaValue").value;
	if ((c=='382') || (c=='261')) {
		return false;
	} else {
		return true;
	}
}

function cargaToolbarAdvanced() {
	var c = document.getElementById("categoriaValue").value;
	var prms = "";
	switch (c) {
		case '100':
			prms = prms + getParam("ancho");
			prms = prms + getParam("alto");
			prms = prms + getParam("diametro");
			prms = prms + getParam("tecnicaimpresionValue");
			prms = prms + getParam("tipocantoValue");
			prms = prms + getParam("tipoadhesivoValue");
			prms = prms + getParam("coloretiquetaValue");
			prms = prms + getParam("tamanohojaValue");
			prms = prms + getParam("formaetiquetaValue");
			prms = prms + getParam("hojaspackValue");
			prms = prms + getParam("palabraValue");
			$("#toolbar-advanced").load("producto/search_etiquetas.aspx?" + prms);
			break;
		case '200':
			prms = prms + getParam("tipoproductoValue");
			prms = prms + getParam("ancho");
			prms = prms + getParam("alto");
			prms = prms + getParam("diametro");
			prms = prms + getParam("tecnicaimpresionValue");
			prms = prms + getParam("tipoadhesivoValue");
			prms = prms + getParam("hojaspackValue");
			prms = prms + getParam("palabraValue");
			$("#toolbar-advanced").load("producto/search_multimedia.aspx?" + prms);
			break;
		case '300':
			prms = prms + getParam("medidastarjetaValue");
			prms = prms + getParam("tecnicaimpresionValue");
			prms = prms + getParam("tipotarjetaValue");
			prms = prms + getParam("colortarjetaValue");
			prms = prms + getParam("hojaspackValue");
			prms = prms + getParam("palabraValue");
			$("#toolbar-advanced").load("producto/search_tarjetas.aspx?" + prms);
			break;
		case '400':
			prms = prms + getParam("tamanohojaValue");
			prms = prms + getParam("tecnicaimpresionValue");
			prms = prms + getParam("tipopapelValue");
			prms = prms + getParam("hojaspackValue");
			prms = prms + getParam("palabraValue");
			$("#toolbar-advanced").load("producto/search_papeles.aspx?" + prms);
			break;
		case '500':
			prms = prms + getParam("tamanohojaValue");
			prms = prms + getParam("tecnicaimpresionValue");
			prms = prms + getParam("tipoproductoValue");
			prms = prms + getParam("palabraValue");
			$("#toolbar-advanced").load("producto/search_hobbyline.aspx?" + prms);
			break;
		case '600':
			prms = prms + getParam("tipoproductoValue");
			prms = prms + getParam("palabraValue");
			$("#toolbar-advanced").load("producto/search_oficinacomercio.aspx?" + prms);
			break;
		case '700':
			prms = prms + getParam("tipoproductoValue");
			prms = prms + getParam("tipoadhesivogometValue");
			prms = prms + getParam("colorgometValue");
			prms = prms + getParam("packgometsValue");
			prms = prms + getParam("formagometValue");
			prms = prms + getParam("palabraValue");
			$("#toolbar-advanced").load("producto/search_escuelahogar.aspx?" + prms);
			break;
		case '800':
			alert('toolbar no disponible');
			//$("#toolbar-advanced").load("producto/search_softwarekits.htm");
			break;
		case '900':
			prms = prms + getParam("tecnicaimpresionValue");
			prms = prms + getParam("numerotransparenciasValue");
			prms = prms + getParam("palabraValue");
			$("#toolbar-advanced").load("producto/search_transparencias.aspx?" + prms);
			break;
		case '950':
			prms = prms + getParam("ancho");
			prms = prms + getParam("alto");
			prms = prms + getParam("diametro");
			prms = prms + getParam("tecnicaimpresionValue");
			prms = prms + getParam("tipocantoValue");
			prms = prms + getParam("tipoadhesivoValue");
			prms = prms + getParam("coloretiquetaValue");
			prms = prms + getParam("tipoetiquetaValue");
			prms = prms + getParam("tipopackValue");
			prms = prms + getParam("formaetiquetaValue");
			prms = prms + getParam("palabraValue");
			$("#toolbar-advanced").load("producto/search_otrasetiquetas.aspx?" + prms);
			break;
		case '261':
			alert('toolbar no disponible');
			//$("#toolbar-advanced").load("producto/search_etiquetasimpresorasmatriciales.htm");
			break;
		default:
			alert('toolbar no disponible');
	}
	
}

function getParam(paramname) {
	if (gup(paramname)!='') {
		return paramname + "="+gup(paramname)+"&";
	} else {
		return "";
	}
}



function search() {
	if(checkselected(document.frmSearch.stype)=='referencia') {
		var codigo = document.frmSearch.referenciaValue.value;
		while (codigo.length<5) {
			codigo = '0'+codigo;
		}
		document.frmSearch.referenciaValue.value = codigo;
		myBaseHREF('producto/ficha_producto.aspx?referencia='+document.frmSearch.referenciaValue.value+'&stype=referencia&referenciaValue='+document.frmSearch.referenciaValue.value);
	} else {
		if((checkselected(document.frmSearch.stype)=='losmasvendidos') || (checkselected(document.frmSearch.stype)=='novedades')) {
			document.frmSearch.action='producto/busqueda-de-productos-listado-global.aspx';
		}
		if(checkselected(document.frmSearch.stype)=='pclave') {
			document.frmSearch.action='producto/busqueda-de-productos-listado-global.aspx';
		}
		
		document.frmSearch.submit();
	}
}

function searchRef() {
	if (IsNumeric(document.getElementById('q').value)) {
		var codigo = document.getElementById('q').value;
		while (codigo.length<5) {
			codigo = '0'+codigo;
		}
		document.getElementById('q').value = codigo;
		
	myBaseHREF('producto/ficha_producto.aspx?referencia='+document.getElementById('q').value+'&stype=referencia&referenciaValue='+document.getElementById('q').value+'&q='+document.getElementById('q').value);
	
	} else {
	myBaseHREF('producto/busqueda-de-productos-listado-global.aspx?advancedSearch=&referenciaValue=&categoriaValue=&subcategoriaValue=&stype=pclave&palabraclave='+document.getElementById('q').value);
	
	}
	//
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function checkselected(radio) {
for (i=0; i<radio.length; i++) {
    if (radio[i].checked) { return radio[i].value; }
}
}

function markSearchType(id) {
	for(var i = 0; i < document.frmSearch.stype.length; i++) {
		document.frmSearch.stype[i].checked = false;
	}

	document.frmSearch.stype[id].checked=true;
}

function loadCategoriaValue(fam_id) {
    $.ajax({
        type : 'get',
        url : 'service/getSubFamiliesQueryString.aspx?fam_id='+fam_id,
        success : function(response){
            var response = response || "no response text";
		    //alert("Success! \n\n" + response);  
			fillSelect('subcategoriaValue',response);
			
        }
    });

}

function fillSelect(idSelect,data) {
	removeSelectOptions(idSelect);
	addOption('subcategoriaValue','','...');
	if (data!='0') {
		parejas=data.split(';');
		for(var i = 0; i < parejas.length; i++) {
			parejas2 = parejas[i].split('=');
			addOption(idSelect,parejas2[0],camelCaps(parejas2[1],true));
		}
	}
}

function removeSelectOptions(idSelect) {
	while (document.getElementById(idSelect).length> 0) {
		document.getElementById(idSelect).remove(0);
	} 
}

function addOption(idSelect,id,val) {
  var elOptNew = document.createElement('option');
  elOptNew.text = val;
  elOptNew.value = id;
  var elSel = document.getElementById(idSelect);

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }

}


function camelCaps(str, theCase) {
  var tempArray = str.split(' ');

  // Make the first character of each word upper- or lowercase
  // depending on the value of theCase
  for (var i = 0; i < tempArray.length; i++) {
    if (theCase) {
      tempArray[i] = tempArray[i].charAt(0).toUpperCase() + tempArray[i].substring(1);
      }
    else {
      tempArray[i] = tempArray[i].charAt(0).toLowerCase() + tempArray[i].substring(1);
      }
    }
  return tempArray.join(' ');
  }
  
function ocultaalert() {
	document.getElementById('alertavanzada').style.display = 'none';
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function cargaresultadoAdvancedSearch() {
	document.frmSearch.action = 'producto/busqueda-de-productos-listado.aspx';
	document.frmSearch.submit();
}

function VeureNewsSized(f,tam) {
 if (f != '')
 var newWin = window.open(absHREF(f),'VeureNews','height=500,width='+tam+',scrollbars=yes,menubar=no,resizable=yes,top=30,left=40');
} 

function VeureNews(f) {
	VeureNewsSized(f,650);
} 

