String.prototype.wordWrap = function(m, b, c){
    var i, j, s, r = this.split("\n");
    if(m > 0) for(i in r){
        for(s = r[i], r[i] = ""; s.length > m;
            j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length
            || m,
            r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
        );
        r[i] += s;
    }
    return r.join("\n");
};


// Function javascript que llama el flash de la cabecera. Contacto y mapa harcodeado en flash
	function changeSec(where) {
		if(where === 'contacto') 
			//document.location.replace('#');
			document.location.replace('#');
		else if(where === 'mapa')
			document.location.replace('/mapadelsitio/colorin');
		
		//document.location.replace(where);
	}
	
	function productoBuscaChange( url ) {
		 
		IntExt = $('superficie').options[$('superficie').selectedIndex].value;
		Superf = $('product_tipo_superficie').options[$('product_tipo_superficie').selectedIndex].value;
		
		new Ajax.Request(url + 'es/ajaxsearch/Producto/'+IntExt+'/'+Superf,
		  	{
			    method:'get',
			    onSuccess: function(transport){
			      var response = transport.responseText || "---";
			      $('comboAjax').innerHTML = response ;
			    },
			    onFailure: function(){ alert('Request Error...'); aux.display.display = '';}
		  	}
		  );						
		
	}
	
	function getVentaName(foovar,path){
			
		new Ajax.Request(path+'ajaxname/'+ foovar, {   
				method:'post',
			    parameters:{letter: $('product_name').value},
			    onSuccess:function(transport) {
			      var response = transport.responseText;
			      $('pintureria').innerHTML = response;
			    },
			    onFailure: function(){ alert('Request Error...'); aux.display = ''; }
		  	}
		  );	
	}

	
// Muestra y esconde párrafos, divs, etc.

	document.write('<style type="text/css">div.hide{display: none;}</style>');
	function MostrarOcultar(capa) {
		var aux = $(capa).style;
		aux.display = aux.display? "":"block";
	}
	
	function getInfo(capa, entity, category, id, path, url, imagePath)
	{
		var aux = $(capa).style;
		aux.display = aux.display? "":"block";
		
		if(aux.display == "block"){
			
			new Ajax.Request(path + 'ajaxInfo/'+entity+'/'+category+'/'+id,
			  {
			    method:'get',
			    onSuccess: function(transport){
			      var response = transport.responseText || "{error:'no text'}";
			      try{
			      
			      	var json = response.evalJSON();
			      	
			      	addInformation(json, capa, entity, url, imagePath);
			      	
			      }catch(e){
					alert('List Error'+e) }
			    },
			    onFailure: function(){ alert('Request Error...'); aux.display.display = '';}
			  });
		}else{
			$('loading').style.display = '';
			$('prod_info').style.display = 'none';
		}
	}
	
	function addInformation(json, capa, entity, url, imagePath){
		
		if(entity == 'Producto'){
		
			$('loading').style.display = 'none';
			$('prod_info').style.display = '';
			
			if(json.imagenGrande) {
				$('prod_img').src = imagePath + json.imagenGrande;
				
			} else {
				$('prod_img').src = _siteURL+'project/images/frontend/error_small.gif';
			}
			
			$('prod_img').width="96";
			$('prod_img').height="120";
			
			$('prod_img').alt = json.nombre;
			
			$('prod_img').onError = function() {this.src = _siteURL+'project/images/frontend/error_small.gif';};
			$('prod_img').onerror = function() {this.src = _siteURL+'project/images/frontend/error_small.gif';};
			
			$('prod_title').innerHTML = json.nombre.wordWrap(25, '-\n<br />', true);
			$('prod_small_desc').innerHTML = json.descripcionCorta;
			$('prod_line').innerHTML = 'Línea ' +json.descripcion_TL;
			$('codigo').innerHTML = 'Codigo ' + json.codigo;
			$('prod_long_desc').innerHTML = json.descripcionLarga;
			$('prod_url').href = url + '/' + json.idProducto;
			
		}
		
	}
	
	function searchByPositionAndTS(capa, path){
		intExtValue = 0;
		
		Prod = $('producto_asociado_2')[$('producto_asociado_2').selectedIndex].value;
		
		if (Prod != 0) {
		
			capa.href = _siteURL+'es/detalle/Producto/' + Prod;
			return;
		}
		
		intExt = document.getElementById('superficie');
		intExtValue = intExt.options[intExt.selectedIndex].value;
		
		TSvalue = $('product_tipo_superficie').selectedIndex;		
		
		TSvalue = $('product_tipo_superficie')[TSvalue].value

		capa.href = path + '/ByPandTS/' + intExtValue + '/' + TSvalue;
		
	}
	function searchByName(capa, path){
		str = $('product_name').value;
		capa.href = path + '/byName/' + escape(str) ;
		
	}
	function searchByLinea(capa, path){
		LineaValue = $('product_tipo_linea').selectedIndex;
		LineaValue = $('product_tipo_linea')[LineaValue].value;
		capa.href = path + '/byLinea/' + LineaValue;
		
	}
	function searchByProvCity(capa, path){
		Prov = $('provincia').selectedIndex;		
		Prov = $('provincia')[Prov].value
		
		City = $('ciudades').selectedIndex;		
		City = $('ciudades')[City].value

		capa.href = path + '/ByLocation/' + Prov + '/' + City;
		
	}
	
	var ActualCity = 0;
	function getCiudades(provincia, capa, entity, path){
	
		var aux = provincia[provincia.selectedIndex].value;
		
		if(ActualCity != aux && aux > 0){
		ActualCity = aux;
		
		new Ajax.Request(path + 'ajaxInfo/'+entity+'/provincia/'+ActualCity,
		  {
		    method:'get',
		    onSuccess: function(transport){
		      var response = transport.responseText || "{error:'no text'}";
			  
		      try{
		        // aparentemente si tiene \n genera un error en json, quitarlos
			// response = str_replace("\n", "", response);
			// alert(response); 
		      	var json = response.evalJSON();
		 
		      	var elOptNew = document.createElement('option');
      		  	elOptNew.text = ' -- ';
      		 	elOptNew.value = 0; 
      			
      		 	$(capa).disabled = '';
      		 	$(capa).options.length = 0;
      		 	try {
			 	  $(capa).add(elOptNew, null); // standards compliant; doesn't work in IE
			 	}
			  	catch(ex) {
			  	  $(capa).add(elOptNew); // IE only
			 	}
			 	
		      	for(i=0 ; i < json.length ; i++){
		      	
		      		  var elOptNew = document.createElement('option');
		      		  elOptNew.text = json[i].descripcion;
		      		  elOptNew.value = json[i].IdCiudad;
		      		  
		      		  try {
					    $(capa).add(elOptNew, null); // standards compliant; doesn't work in IE
					  }
					  catch(ex) {
					    $(capa).add(elOptNew); // IE only
					  }
		      	}
		      	
		      }catch(e){ alert('List Error'+e) }
		    },
		    onFailure: function(){ alert('Request Error...'); aux.display.display = '';}
		  });
		} else if(aux == 0){
			var elOptNew = document.createElement('option');
     		  	elOptNew.text = ' -- ';
     		 	elOptNew.value = 0; 
     			
     		 	$(capa).disabled = 'disabled';
     		 	$(capa).options.length = 0;
     		 	try {
		 	   $(capa).add(elOptNew, null); // standards compliant; doesn't work in IE
			 	}
			  	catch(ex) {
			  	  $(capa).add(elOptNew); // IE only
			 	}
		}
	}
	function cambiarImagenes(){
		new PeriodicalExecuter(function(pe) {
				if(!$('rtImg') || !$('rtImg').src ) {
					return;
				}
				
				  $('rtImg').src = path+arrImgsName[actualImg];			  
				  actualImg += 1;
				  
				  if(actualImg >= arrImgsName.length)
			  			actualImg= 0;
			}, 5);

	}
	
	
	function openP(url){
		var mYwindow = window.open(url, "PrintPopUp", "width=700,scrollbars=yes,menubar=no");
	}



