// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Sistem avansat de obtinere in timp real a datelor cu privire la marime/culoare/stoc/disponibilitate pentru un produs
// Autor: SC NABS GROUP SRL, Divizia de Ecommerce, Romania (http://www.la-moda.ro)
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------

	var bridgeurl="smarty_plugins/function.load_product_bridge.php"//locatia fisierului care actualizeaza automat datele din baza de date
	
	// --------------------------------------------------------------------------------------------------------------------
	// functie care face update fisierului ce contine datele din baza de date
	// --------------------------------------------------------------------------------------------------------------------
	function update_string() 
	{
		new stringobject_ajax()
	}
	// --------------------------------------------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------------------------------------------
	
	// --------------------------------------------------------------------------------------------------------------------
	// Functia care creeaza corect obiectul ajax (textul) in functie de browser
	// --------------------------------------------------------------------------------------------------------------------
	function createAjaxObj()
	{
		var httprequest=false
		if (window.XMLHttpRequest)
		{ 	// pentru Mozilla, Safari etc
			httprequest=new XMLHttpRequest()		
			if (httprequest.overrideMimeType)
				httprequest.overrideMimeType('text/xml')
		}
		else if (window.ActiveXObject)
		{ // pentru IE
			try 
			{
				httprequest=new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch (e)
			{
				try
				{
					httprequest=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e){}
			}
		}
		return httprequest
	}
	// --------------------------------------------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------------------------------------------
	
	
	// --------------------------------------------------------------------------------------------------------------------
	// Functia principala care creeaza sirul de caractere pentru popularea obiectelor select
	// --------------------------------------------------------------------------------------------------------------------
	function stringobject_ajax()
	{
		this.ajaxobj = createAjaxObj()
		this.getAjaxcontent()
	}
	// --------------------------------------------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------------------------------------------
	
	
	// --------------------------------------------------------------------------------------------------------------------
	// getAjaxcontent()- Deschide "bridge.php" si obtine datele prin GET asincron
	// --------------------------------------------------------------------------------------------------------------------
	stringobject_ajax.prototype.getAjaxcontent=function()
	{
		if (this.ajaxobj)
		{
			var instance=this
			var parameters="ProductID="+document.getElementById('product_id').value
			this.ajaxobj.onreadystatechange=function(){instance.initialize()}
			this.ajaxobj.open('GET', bridgeurl+"?"+parameters, true)
			this.ajaxobj.send(null)
		}
	}
	// --------------------------------------------------------------------------------------------------------------------
	// --------------------------------------------------------------------------------------------------------------------
	
	
	// --------------------------------------------------------------------------------------------------------------------
	// metoda de initializare - daca totul este OK atribuie variabilei m String valoarea obtinuta din deschiderea "bridge.php"
	// --------------------------------------------------------------------------------------------------------------------
	stringobject_ajax.prototype.initialize=function()
	{ 
		if (this.ajaxobj.readyState == 4)	//daca fisierul cerut este complet
		{ 	
			if (this.ajaxobj.status==200)	//daca cererea a fost executata cu succes
			{ 	
				mString = this.ajaxobj.responseText.replace(/^\s*|\s*$/g,'');
			}
		}
	}
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------


Array.prototype.unique = function () {
    var r = new Array();
    o:for(var i = 0, n = this.length; i < n; i++)
    {
        for(var x = 0, y = r.length; x < y; x++)
        {
                if(r[x]==this[i])
                {
                	continue o;
                }
        }
        r[r.length] = this[i];
    }
    return r;
}

// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// functie care se apeleaza cand se actioneaza obiectul select culoare
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function change_size_listbox(no_size,delivery_days) 
	{
		
		new stringobject_ajax()	
		var color_id = document.dropdowns.select_color.options[document.dropdowns.select_color.selectedIndex].value;//determina idul pentru culoarea selectata
		var product_id = document.getElementById('product_id').value//determina idul produsului
		var select_size = document.getElementById('select_size') 
		var select_qty = document.getElementById('select_qty')
	
		// --------------------------------------------------------------------------------------------------------------------
		// cod care genereaza vectorul care contine datele pentru popularea obiectelor din pagina de produs
		// --------------------------------------------------------------------------------------------------------------------
		y = mString.split("@");
		var all_sizes_Array=new Array();
		var mColorsID=new Array();
		for (i = 0; i < y.length-1; i++) 
		{
			all_sizes_Array[i] = new Array();
			x = y[i].split("#");
			all_sizes_Array[i][0] = x[0]; //size_id
			all_sizes_Array[i][1] = x[1]; //size_name
			all_sizes_Array[i][2] = x[2]; //color_id
			all_sizes_Array[i][3] = x[3]; //denumirea culorii
			all_sizes_Array[i][4] = x[4]; //produse in stoc
			all_sizes_Array[i][5] = x[5]; //produse in stoc la furnizor	
			all_sizes_Array[i][6] = x[6]; //produsul se poate sau nu face la comanda
			
			mColorsID[i] = x[2];
		};
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		var unique = mColorsID.unique();
		for (i = 0; i < unique.length; i++) 
			if(unique[i] == color_id)
				position = i;
		var main_color_id = document.getElementById('product_main_color_id').value
		m=0;
		if ( typeof( document.images.big_picture1 ) == "object" ) m=1;
		if ( typeof( document.images.big_picture2 ) == "object" ) m=2;
		if ( typeof( document.images.big_picture3 ) == "object" ) m=3;
		
		if (m == 1) 
		{
			document.images.big_picture1.src = "product_images/product_" + product_id + "/thumbs/t3-"+main_color_id+"-image"+product_id+"-2.jpg";
		}
		
		if (m == 2)
		{
			document.images.big_picture1.src = "product_images/product_" + product_id + "/thumbs/t3-"+main_color_id+"-image"+product_id+"-2.jpg";
			document.images.big_picture2.src = "product_images/product_" + product_id + "/thumbs/t3-"+main_color_id+"-image"+product_id+"-3.jpg";
		};
		
		if (m == 3)
		{
			document.images.big_picture1.src = "product_images/product_" + product_id + "/thumbs/t3-"+main_color_id+"-image"+product_id+"-2.jpg";
			document.images.big_picture2.src = "product_images/product_" + product_id + "/thumbs/t3-"+main_color_id+"-image"+product_id+"-3.jpg";
			document.images.big_picture3.src = "product_images/product_" + product_id + "/thumbs/t3-"+main_color_id+"-image"+product_id+"-4.jpg";
		};
		
		// daca nu este selectata valoarea "Selecteaza" atunci actualizeaza informatiile
		if(color_id != 0)
		{
			if(no_size == 0)
				select_size_value = document.getElementById('select_size').value
			else 
				var select_size_value = all_sizes_Array[0][0];
			
	
		// --------------------------------------------------------------------------------------------------------------------
		// cod care misca indicarotul culorii in "Mostra material"
		// --------------------------------------------------------------------------------------------------------------------
		document.getElementById("lmd_ProductColorsSwatchMarker").style.paddingLeft = 110 + position * 32;
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		
		
		// --------------------------------------------------------------------------------------------------------------------
		// cod care schimba poza si link-ul cand se actioneaza obiectul select culoare
		// --------------------------------------------------------------------------------------------------------------------
			//schimba poza si link-ul initial al produsului cu poza si link-ul unei culorii selectate		
			if(color_id > 0)
			{
				$img_url = "product_images/product_" + product_id + "/thumbs/t1-"+color_id+"-image"+product_id+"-1.jpg";
				$zoom_img_url = "product_images/product_" + product_id + "/xxl/"+color_id+"-image"+product_id+"-1.jpg";
				loadImage($img_url);
				loadZoomImage($zoom_img_url);
				document.getElementById('big_picture_link').href = "product_image_view.php?ProductID="+product_id+"&ColorID=" + color_id + "&ImageID=1";
				document.getElementById('big_picture_zoom').href = "product_image_view.php?ProductID="+product_id+"&ColorID=" + color_id + "&ImageID=1";
			}
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		
	
		// --------------------------------------------------------------------------------------------------------------------
		// cod care genereaza vectorul care contine datele cu zilele de livrare posibile
		// --------------------------------------------------------------------------------------------------------------------
			y = delivery_days.split("@");
			var delivery_days_Array=new Array();
			for (i = 0; i < y.length-1; i++) 
			{
				//daca i = 0->in stoc, i = 1->stoc la furnizor, i = 2->la comanda
				delivery_days_Array[i] = new Array();
				x = y[i].split("#");
				delivery_days_Array[i][0] = x[0]; //cate zile dureaza pana cand produsul ajunge in stoc
				delivery_days_Array[i][1] = x[1]; //cate zile dureaza LIVRAREA STANDARD
				delivery_days_Array[i][2] = x[2]; //cate zile dureaza LIVRAREA EXPRESS
			};
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		
		
		// --------------------------------------------------------------------------------------------------------------------
		// cod pentru popularea obiectului input text disponibilitate
		// --------------------------------------------------------------------------------------------------------------------
			//populeaza campul disponibilitatea a produsului cu disponibilitatea primei marimi din campul select marime		
			availability_text = ""
			qty=5
			for (i = all_sizes_Array.length-1; i >=0; i--) 
			{
				if(all_sizes_Array[i][2] == color_id)
				{			
					if(all_sizes_Array[i][0] == select_size_value)
					{
						//daca produsul este la comanda
						if (all_sizes_Array[i][6] != 0) 
						{
							//genereaza textul pentru disponibilitatea produsului
							availability_text = "<i>La Comanda: livrare standard in "+delivery_days_Array[2][1]+" zile lucratoare, expres in "+delivery_days_Array[2][2]+" zile lucratoare</i>";
							qty = 5;
							document.getElementById("delivery_days").value = delivery_days_Array[2][1]+"@"+delivery_days_Array[2][2];
						};
						
						//daca produsul este in stoc la furnizor
						if (all_sizes_Array[i][5] > 0) 
						{
							//genereaza textul pentru disponibilitatea produsului
							availability_text = "<i>In Stoc La Furnizor: livrare standard in "+delivery_days_Array[1][1]+" zile lucratoare, expres in "+delivery_days_Array[1][2]+" zile lucratoare</i>";
							qty = all_sizes_Array[i][5];
							document.getElementById("delivery_days").value = delivery_days_Array[1][1]+"@"+delivery_days_Array[1][2];
						};
						
						//daca produsul este in stoc
						if (all_sizes_Array[i][4] > 0) 
						{
							//genereaza textul pentru disponibilitatea produsului
							availability_text = "<i>In Stoc: livrare standard in "+delivery_days_Array[0][1]+" zile lucratoare, expres in "+delivery_days_Array[0][2]+" zile lucratoare</i>";
							qty = all_sizes_Array[i][4];
							document.getElementById("delivery_days").value = delivery_days_Array[0][1]+"@"+delivery_days_Array[0][2];
						};
					}
				};		
			};
			document.getElementById("availabilityText").innerHTML = availability_text;		
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		
		
		
		// --------------------------------------------------------------------------------------------------------------------	
		// cod pentru populeaza obiectul select cantitate
		// --------------------------------------------------------------------------------------------------------------------
			//sterge toate valorile obiectului select cantitate pentru popularea cu valori noi
			for(i=select_qty.length-1; i>=0; i--) select_qty.options[i] = null
			
			//populeaza obiectul select cantitate	
			select_qty.options[0] = new Option("0","0")
			if (qty>5) qty=5
			for (i = 0; i <= qty; i++) 
			{
				select_qty.options[i] = new Option(i, i)
			}		
			
			//selecteaza prima optiune pentru select cantitate
			select_qty.selectedIndex = 1
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
	
		// --------------------------------------------------------------------------------------------------------------------
		// cod pentru popularea obiectului select marime
		// --------------------------------------------------------------------------------------------------------------------
			//daca exista obiectul select marime populeaza-l cu datele corecte		
			if (no_size == 0)
			{			
				selected_size_exists = 0;
				for (i = 0; i < all_sizes_Array.length; i++) 
				{
					if (all_sizes_Array[i][2] == color_id) 
					{
						if(all_sizes_Array[i][0] == select_size_value) selected_size_exists = select_size_value
					}
				}
	
				//sterge toate valorile obiectului select marime pentru popularea cu valori noi			
				for(i=select_size.length-1; i>=0; i--) select_size.options[i] = null
				
				//populeaza obiectul select marime	
				select_size.options[0] = new Option("Selecteaza","0")
				var childIndex = 1
				for (i = 0; i < all_sizes_Array.length; i++) 
				{
					if (all_sizes_Array[i][2] == color_id) 
					{
						select_size.options[childIndex] = new Option(all_sizes_Array[i][1], all_sizes_Array[i][0])
						if( all_sizes_Array[i][0] == selected_size_exists) selected_size_exists = childIndex
						childIndex++
					}
				}		
				//selecteaza prima optiune pentru select marimi
				select_size.selectedIndex = selected_size_exists
					
			}
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		}
		else
		{
			reset_selection(0,2, no_size)
		}
	}
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------



// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// functie care se apeleaza cand se actioneaza obiectul select marime
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
	function change_color_listbox(one_color,delivery_days) 
	{
		new stringobject_ajax()	
		var size_id = document.dropdowns.select_size.options[document.dropdowns.select_size.selectedIndex].value;//determina idul pentru marimea selectata
		var product_id = document.getElementById('product_id').value//determina idul produsului
		var select_color = document.getElementById('select_color')
		var select_qty = document.getElementById('select_qty')	
		
		// daca nu este selectata valoarea "Selecteaza" atunci actualizeaza informatiile
		if(size_id != 0)
		{
		// --------------------------------------------------------------------------------------------------------------------
		// cod care genereaza vectorul care contine datele pentru popularea obiectelor din pagina de produs
		// --------------------------------------------------------------------------------------------------------------------
			y = mString.split("@");
			var all_sizes_Array=new Array();
			for (i = 0; i < y.length-1; i++) 
			{
				all_sizes_Array[i] = new Array();
				x = y[i].split("#");
				all_sizes_Array[i][0] = x[0]; //size_id
				all_sizes_Array[i][1] = x[1]; //size_name
				all_sizes_Array[i][2] = x[2]; //color_id
				all_sizes_Array[i][3] = x[3]; //denumirea culorii
				all_sizes_Array[i][4] = x[4]; //produse in stoc
				all_sizes_Array[i][5] = x[5]; //produse in stoc la furnizor	
				all_sizes_Array[i][6] = x[6]; //produsul se poate sau nu face la comanda
			};
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		
		// --------------------------------------------------------------------------------------------------------------------
		// cod care genereaza vectorul care contine datele cu zilele de livrare posibile
		// --------------------------------------------------------------------------------------------------------------------
			//creeaza ventorul pentru popularea obiectelor select in pagina de produs
			y = delivery_days.split("@");
			var delivery_days_Array=new Array();
			for (i = 0; i < y.length-1; i++) 
			{
				//daca i = 0->in stoc, i = 1->stoc la furnizor, i = 2->la comanda
				delivery_days_Array[i] = new Array();
				x = y[i].split("#");
				delivery_days_Array[i][0] = x[0]; //cate zile dureaza pana cand produsul ajunge in stoc
				delivery_days_Array[i][1] = x[1]; //cate zile dureaza LIVRAREA STANDARD
				delivery_days_Array[i][2] = x[2]; //cate zile dureaza LIVRAREA EXPRESS
			};
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
	
		
		// --------------------------------------------------------------------------------------------------------------------
		// cod pentru popularea obiectului input text disponibilitate
		// --------------------------------------------------------------------------------------------------------------------
			//populeaza campul disponibilitatea a produsului cu disponibilitatea primei marimi din campul select marime		
			availability_text = ""
			qty=5
			for (i = all_sizes_Array.length-1; i >=0; i--) 
			{
				if(all_sizes_Array[i][0] == size_id)
				{			
					if(all_sizes_Array[i][2] == select_color.value)
					{
						//daca produsul este la comanda
						if (all_sizes_Array[i][6] != 0) 
						{
							//genereaza textul pentru disponibilitatea produsului
							availability_text = "<i>La Comanda: livrare standard in "+delivery_days_Array[2][1]+" zile lucratoare, expres in "+delivery_days_Array[2][2]+" zile lucratoare</i>";
							qty = 5;
							document.getElementById("delivery_days").value = delivery_days_Array[2][1]+"@"+delivery_days_Array[2][2];
						};
						
						//daca produsul este in stoc la furnizor
						if (all_sizes_Array[i][5] > 0) 
						{
							//genereaza textul pentru disponibilitatea produsului
							availability_text = "<i>In Stoc La Furnizor: livrare standard in "+delivery_days_Array[1][1]+" zile lucratoare, expres in "+delivery_days_Array[1][2]+" zile lucratoare</i>";
							qty = all_sizes_Array[i][5];
							document.getElementById("delivery_days").value = delivery_days_Array[1][1]+"@"+delivery_days_Array[1][2];
						};
						
						//daca produsul este in stoc
						if (all_sizes_Array[i][4] > 0) 
						{
							//genereaza textul pentru disponibilitatea produsului
							availability_text = "<i>In Stoc: livrare standard in "+delivery_days_Array[0][1]+" zile lucratoare, expres in "+delivery_days_Array[0][2]+" zile lucratoare</i>";
							qty = all_sizes_Array[i][4];
							document.getElementById("delivery_days").value = delivery_days_Array[0][1]+"@"+delivery_days_Array[0][2];
						};
					}
				};		
			};
			document.getElementById("availabilityText").innerHTML = availability_text		
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		
		
		
		// --------------------------------------------------------------------------------------------------------------------	
		// cod pentru populeaza obiectul select cantitate
		// --------------------------------------------------------------------------------------------------------------------
			//sterge toate valorile obiectului select cantitate pentru popularea cu valori noi
			for(i=select_qty.length-1; i>=0; i--) select_qty.options[i] = null
			
			//populeaza obiectul select cantitate	
			select_qty.options[0] = new Option("0","0")
			if (qty>5) qty=5
			for (i = 0; i <= qty; i++) 
			{
				select_qty.options[i] = new Option(i, i)
			}		
			
			//selecteaza prima optiune pentru select cantitate
			select_qty.selectedIndex = 1
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		
		// --------------------------------------------------------------------------------------------------------------------
		// cod pentru popularea obiectului select marime
		// --------------------------------------------------------------------------------------------------------------------
			if (one_color > 1)
			{
				selected_color_exists = 0;
				for (i = 0; i < all_sizes_Array.length; i++) 
				{
					if (all_sizes_Array[i][0] == size_id) 
					{
						if(all_sizes_Array[i][2] == select_color.value) selected_color_exists = select_color.value
					}
				}
		
				//sterge toate valorile obiectului select marime pentru popularea cu valori noi			
				for(i=select_color.length-1; i>=0; i--) select_color.options[i] = null
					
				//populeaza obiectul select marime	
				select_color.options[0] = new Option("Selecteaza","0")
				var childIndex = 1
				for (i = 0; i < all_sizes_Array.length; i++) 
				{
					if (all_sizes_Array[i][0] == size_id) 
					{
						select_color.options[childIndex] = new Option(all_sizes_Array[i][3], all_sizes_Array[i][2])
						if( all_sizes_Array[i][2] == selected_color_exists) selected_color_exists = childIndex
						childIndex++
					}
				}		
				
				//selecteaza prima optiune pentru select marimi
				select_color.selectedIndex = selected_color_exists
			}				
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		}
		else
		{			
			reset_selection(0,one_color, 0)			
		}
	}
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------



// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// functie care se apeleaza cand se actioneaza obiectul select marime
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
function reset_selection(from_reset_button, one_color, no_size)
{
		document.getElementById("availabilityText").innerHTML = ""
		y = mString.split("@");
		t=0
		var selected_color_index = document.dropdowns.select_color.selectedIndex
		var selected_size_index = document.dropdowns.select_size.selectedIndex
		var select_color = document.getElementById('select_color')
		var select_size = document.getElementById('select_size')
		var select_qty = document.getElementById('select_qty')
		var colors=new Array();
		var colors_id=new Array();
		var sizes=new Array();
		var sizes_id=new Array();
		
		for (i = 0; i < y.length-1; i++) 
		{
			colors[i] = new Array();
			sizes[i] = new Array();
			x = y[i].split("#");
			
			colors_id[i] = x[2]; //color_id
			colors[i][0] = x[2]; //color_id
			colors[i][1] = x[3]; //denumirea culorii
			
			sizes_id[i] = x[0]; //size_id
			sizes[i][0] = x[0]; //size_id
			sizes[i][1] = x[1]; //denumirea marimii
			
		};

		if(one_color > 1)
		{
			//completeaza cu toate culorile si selecteaza valoarea 0 pentru marime					
			all_colors = eliminateDuplicates(colors_id)
			
			var m=new Array();		
			for (i = 0; i < all_colors.length; i++) 
			{
				m[i]=new Array();
				m[i][0]=all_colors[i];
				color_name=""
				for (j = 0; j < colors.length; j++) 
				{
					if	(all_colors[i]==colors[j][0]) color_name=colors[j][1]
				}
				m[i][1]=color_name
			}
			//sterge toate valorile obiectelor select culoare pentru popularea cu valori noi			
			for(i=select_color.length-1; i>=0; i--) select_color.options[i] = null					
			//populeaza obiectul select culoare cu toate culorile 
			select_color.options[0] = new Option("Selecteaza","0")
			c=1
			for (i = 0; i < m.length; i++) 
			{
				select_color.options[c] = new Option(m[i][1], m[i][0])
				c++					
			}
		}
		
		if(no_size == 0)
		{
			all_sizes = eliminateDuplicates(sizes_id)
			var n=new Array();
			for (i = 0; i < all_sizes.length; i++) 
			{
				n[i]=new Array();
				n[i][0]=all_sizes[i];
				size_name=""
				for (j = 0; j < sizes.length; j++) 
				{
					if	(all_sizes[i]==sizes[j][0]) size_name=sizes[j][1]
				}
				n[i][1]=size_name
			}		
			//sterge toate valorile obiectelor select marime pentru popularea cu valori noi			
			for(i=select_size.length-1; i>=0; i--) select_size.options[i] = null
			//populeaza obiectul select marime cu toate culorile 
			select_size.options[0] = new Option("Selecteaza","0")
			c=1
			for (i = 0; i < n.length; i++) 
			{
				select_size.options[c] = new Option(n[i][1], n[i][0])
				c++					
			}
		}
		
		
		if(from_reset_button==0)
		{
			document.dropdowns.select_color.selectedIndex = selected_color_index
			document.dropdowns.select_size.selectedIndex = selected_size_index
		}
		else 
		{
			document.dropdowns.select_color.selectedIndex=0
			document.dropdowns.select_size.selectedIndex=0
		}
		// --------------------------------------------------------------------------------------------------------------------	
		// cod pentru populeaza obiectul select cantitate
		// --------------------------------------------------------------------------------------------------------------------
			//sterge toate valorile obiectului select cantitate pentru popularea cu valori noi
			for(i=select_qty.length-1; i>=0; i--) select_qty.options[i] = null
			
			//populeaza obiectul select cantitate	
			select_qty.options[0] = new Option("0","0")
			for (i = 0; i <= 5; i++) 
			{
				select_qty.options[i] = new Option(i, i)
			}		
			
			//selecteaza prima optiune pentru select cantitate
			select_qty.selectedIndex = 1
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
	}
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------


// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// arata sau ascunde divul in care este mosta pentru blog
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
function toggle_blog_this_item()
{
	var div = document.getElementById("blog_this_item_div");
	var text = document.getElementById("displayText");
	if(div.style.display == "block")
	{
		div.style.display = "none";
		text.innerHTML = "vezi cum va arata";
	}
	else
	{
		div.style.display = "block";
		text.innerHTML = "ascunde";
	}
}
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------


function eliminateDuplicates(arr)
{
  var i,
      len=arr.length,
      out=[],
      obj={};

  for (i=0;i<len;i++) {
    obj[arr[i]]=0;
  }
  for (i in obj) {
    out.push(i);
  }
  return out;
}


// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
function change_pictures_from_colors(obj, color_id, product_id, n, parentObject, childObject, no_size, delivery_days, color_id1)
{
	// verifica cate poze secundare are produsul
	m=0;
	if ( typeof( document.images.big_picture1 ) == "object" ) m=1;
	if ( typeof( document.images.big_picture2 ) == "object" ) m=2;
	if ( typeof( document.images.big_picture3 ) == "object" ) m=3;
	
	if (m == 1) 
	{
		document.images.big_picture1.src = "product_images/product_" + product_id + "/thumbs/t3-"+color_id1+"-image"+product_id+"-2.jpg";
	}
	
	if (m == 2)
	{
		document.images.big_picture1.src = "product_images/product_" + product_id + "/thumbs/t3-"+color_id1+"-image"+product_id+"-2.jpg";
		document.images.big_picture2.src = "product_images/product_" + product_id + "/thumbs/t3-"+color_id1+"-image"+product_id+"-3.jpg";
	};
	
	if (m == 3)
	{
		document.images.big_picture1.src = "product_images/product_" + product_id + "/thumbs/t3-"+color_id1+"-image"+product_id+"-2.jpg";
		document.images.big_picture2.src = "product_images/product_" + product_id + "/thumbs/t3-"+color_id1+"-image"+product_id+"-3.jpg";
		document.images.big_picture3.src = "product_images/product_" + product_id + "/thumbs/t3-"+color_id1+"-image"+product_id+"-4.jpg";
	};
		
	img_url = "product_images/product_" + product_id + "/thumbs/t1-"+color_id+"-image"+product_id+"-1.jpg";
	zoom_img_url = "product_images/product_" + product_id + "/xxl/"+color_id+"-image"+product_id+"-1.jpg";
	loadImage(img_url)
	loadZoomImage(zoom_img_url)
	x = 132 + (n-1) * 37;
	//document.getElementById('lmd_ProductColorsSwatchMarker').style.paddingLeft = padding;
	document.getElementById('big_picture_link').href = "product_image_view.php?ProductID="+product_id+"&ColorID=" + color_id + "&ImageID=1";
	document.getElementById('big_picture_zoom').href = "product_image_view.php?ProductID="+product_id+"&ColorID=" + color_id + "&ImageID=1";
	document.getElementById("lmd_ProductColorsSwatchMarker").style.paddingLeft= x;
	
	
	if(document.getElementById('select_size').value!=0)
	{
		y = mString.split("@");
		t=0
		var select_color = document.getElementById('select_color')
		var colors=new Array();
		var colors_id=new Array();
		for (i = 0; i < y.length-1; i++) 
		{
			colors[i] = new Array();
			x = y[i].split("#");
			if(x[0]==document.getElementById('select_size').value & x[2]==color_id) t=1;
			colors_id[i] = x[2]; //color_id
			colors[i][0] = x[2]; //color_id
			colors[i][1] = x[3]; //denumirea culorii
			
		};
		
		if(t==0) 
		{			
			//completeaza cu toate culorile si zelecteaza valoarea 0 pentru marime					
			all_colors = eliminateDuplicates(colors_id)
			var m=new Array();
			for (i = 0; i < all_colors.length; i++) 
			{
				m[i]=new Array();
				m[i][0]=all_colors[i];
				color_name=""
				for (j = 0; j < colors.length; j++) 
				{
					if	(all_colors[i]==colors[j][0]) color_name=colors[j][1]
				}
				m[i][1]=color_name
				
			}
			
			//sterge toate valorile obiectului select marime pentru popularea cu valori noi			
			for(i=select_color.length-1; i>=0; i--) select_color.options[i] = null
					
			//populeaza obiectul select culoare cu toate culorile 
			select_color.options[0] = new Option("Selecteaza","0")
			c=1
			for (i = 0; i < m.length; i++) 
			{
				select_color.options[c] = new Option(m[i][1], m[i][0])
				c++
					
			}		
		}
	}
	document.dropdowns.select_color.selectedIndex=n;
	var select_qty = document.getElementById('select_qty')
	// --------------------------------------------------------------------------------------------------------------------	
		// cod pentru populeaza obiectul select cantitate
		// --------------------------------------------------------------------------------------------------------------------
			//sterge toate valorile obiectului select cantitate pentru popularea cu valori noi
			for(i=select_qty.length-1; i>=0; i--) select_qty.options[i] = null
			
			//populeaza obiectul select cantitate	
			select_qty.options[0] = new Option("0","0")
			for (i = 0; i <= 5; i++) 
			{
				select_qty.options[i] = new Option(i, i)
			}		
			
			//selecteaza prima optiune pentru select cantitate
			select_qty.selectedIndex = 1
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
	change_size_listbox(no_size, delivery_days)
}
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------


function loadImage(image_src)
{
	document.getElementById('big_pic').innerHTML = "<img src='images/loading.gif' border='0' style='padding-top:185px;' />";
	var img = new Image();
 
	img.onerror = function (){
		document.getElementById('big_pic').innerHTML = "<p class='Down' style='color:#999999; padding-top:130px;'>Imaginea cautata nu a fost gasita.<br /> Va rugam incercati mai tarziu. <br /><img src='images/no_image.gif' border='0' /></p>";
	}
	img.onload = function (){
		document.getElementById('big_pic').innerHTML = "<img src='"+image_src+"' border='0' />";
	}

	img.src = image_src; 
	document.getElementById('product_main_image_src').value = image_src;				
};
function loadZoomImage(image_src)
{
	document.getElementById('zoom_big_image').href = image_src;
};

function CanSubmit(productID,no_size)
{	
	var sel_index = document.dropdowns.select_color.options[document.dropdowns.select_color.selectedIndex].value;	 
 	var sel_index_name = document.dropdowns.select_color.options[document.dropdowns.select_color.selectedIndex].text;
	var sel_qty = document.dropdowns.select_qty.options[document.dropdowns.select_qty.selectedIndex].value;
	if(no_size == 0)
	{
		 var sel_subindex = document.dropdowns.select_size.options[document.dropdowns.select_size.selectedIndex].value;
		 var sel_subindex_name = document.dropdowns.select_size.options[document.dropdowns.select_size.selectedIndex].text;	
	}
	else 
	{
		var sel_subindex = "1";
		var sel_subindex_name = "-";
	};
	
	if (sel_index == 0 || sel_subindex == 0 || sel_qty == 0) 
	{
		if (sel_index == 0 & sel_subindex != 0)
		{
			alert("Nu ai selectat culoarea!");
			return false; 
		};
							
		if (sel_index != 0 & sel_subindex == 0) 
		{
			alert("Nu ai selectat marimea!");
			return false; 
		};	
					
		if (sel_index == 0 & sel_subindex == 0) 
		{
			alert("Nu ai selectat culoarea si marimea!");
			return false; 
		};
		
		if (sel_qty == 0) 
		{
			alert("Nu ai selectat cantitatea!");
			return false; 
		};
	};
}


function change_pic(obj, product_id, color_id, n)
{
	y = mString.split("@");
		var all_sizes_Array=new Array();
		var mColorsID=new Array();
		for (i = 0; i < y.length-1; i++) 
		{
			all_sizes_Array[i] = new Array();
			x = y[i].split("#");
			all_sizes_Array[i][0] = x[0]; //size_id
			all_sizes_Array[i][1] = x[1]; //size_name
			all_sizes_Array[i][2] = x[2]; //color_id
			all_sizes_Array[i][3] = x[3]; //denumirea culorii
			all_sizes_Array[i][4] = x[4]; //produse in stoc
			all_sizes_Array[i][5] = x[5]; //produse in stoc la furnizor	
			all_sizes_Array[i][6] = x[6]; //produsul se poate sau nu face la comanda
			
			mColorsID[i] = x[2];
		};
		// --------------------------------------------------------------------------------------------------------------------
		// --------------------------------------------------------------------------------------------------------------------
		var unique = mColorsID.unique();
		for (i = 0; i < unique.length; i++) 
			if(unique[i] == color_id)
				position = i;
	document.getElementById("lmd_ProductColorsSwatchMarker").style.paddingLeft = 110 + position * 32;
	document.dropdowns.select_color.selectedIndex = 0;
	
	
	x = document.getElementById('product_main_image_src').value; 
	y = x.split("-");
	z = y[3];
	t = z.split(".");
	n1 = t[0];
	if (n == 1){x = document.images.big_picture1.src;};
	if (n == 2){x = document.images.big_picture2.src;};
	if (n == 3){x = document.images.big_picture3.src;};
	y = x.split("-");
	z = y[4];
	t = z.split(".")
	n2 = t[0];

	img_url = "product_images/product_" + product_id + "/thumbs/t1-"+color_id+"-image"+product_id+"-"+n2+".jpg";
	zoom_img_url = "product_images/product_" + product_id + "/xxl/"+color_id+"-image"+product_id+"-"+n2+".jpg";
	loadImage(img_url)
	loadZoomImage(zoom_img_url)
	if (n == 1)
	{
		document.images.big_picture1.src = "product_images/product_" + product_id + "/thumbs/t3-"+color_id+"-image"+product_id+"-"+n1+".jpg"; 
		document.getElementById('big_picture_link').href = "product_image_view.php?ProductID="+product_id+"&ColorID=" + color_id + "&ImageID=2";
		document.getElementById('big_picture_zoom').href = "product_image_view.php?ProductID="+product_id+"&ColorID=" + color_id + "&ImageID=2";

	};
	if (n == 2)
	{
		document.images.big_picture2.src = "product_images/product_" + product_id + "/thumbs/t3-"+color_id+"-image"+product_id+"-"+n1+".jpg";
		document.getElementById('big_picture_link').href = "product_image_view.php?ProductID="+product_id+"&ColorID=" + color_id + "&ImageID=3";
		document.getElementById('big_picture_zoom').href = "product_image_view.php?ProductID="+product_id+"&ColorID=" + color_id + "&ImageID=3";
	};
	if (n == 3)
	{
		document.images.big_picture3.src = "product_images/product_" + product_id + "/thumbs/t3-"+color_id+"-image"+product_id+"-"+n1+".jpg";
		document.getElementById('big_picture_link').href = "product_image_view.php?ProductID="+product_id+"&ColorID=" + color_id + "&ImageID=4";
		document.getElementById('big_picture_zoom').href = "product_image_view.php?ProductID="+product_id+"&ColorID=" + color_id + "&ImageID=4";
	};
}


// deschide o fereastra noua
function openwin(where,width,height,scrollbars)
{
	window.open(where,"TempWindow","width="+width+",height="+height+",resizable=0,status=0,location=0,scrollbars="+scrollbars)
}

// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// functii care se creeaza obiectul de prezentare a mostrei
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
var offsetxpoint=-90 //Customize x offset of tooltip
var offsetypoint=-160 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

function ietruebody()
{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(product_id, color_id, name, single_color)
{
	if (ns6||ie)
	{
		if (typeof thewidth!="undefined") tipobj.style.width="180px"
		if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
		var startStr = '<table width="180" ><tr><td align="center" width="100%"><img src="';
		var midStr = '" border="1" style="border-color:#CCCCCC; dispaly:block;"></td></tr><tr><td valign="top">';
		var endStr = '</td></tr><tr><td style="line-height:10px;"><span style="font-family: Arial; font-size:11px; color: #000000;">'+ name +'</span>';
		endStr = endStr +'</td></tr></table>';
		thetext = 'product_images/product_'+product_id+'/colors_demo/xxl-'+color_id+'.jpg'
		tipobj.innerHTML=startStr + thetext + midStr + endStr;
		enabletip=true
		return false
	}
}

function positiontip(e)
{
	if (enabletip)
	{
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20
		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<tipobj.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
		else if (curX<leftedge)
		tipobj.style.left="5px"
		else
		//position the horizontal position of the menu where the mouse is positioned
		tipobj.style.left=curX+offsetxpoint+"px"

		//same concept with the vertical position
		if (bottomedge<tipobj.offsetHeight)
		tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
		else
		tipobj.style.top=curY+offsetypoint+"px"
		tipobj.style.visibility="visible"
	}
}

function hideddrivetip()
{
	if (ns6||ie)
	{
		enabletip=false
		tipobj.style.visibility="hidden"
		tipobj.style.left="-1000px"
		tipobj.style.backgroundColor=''
		tipobj.style.width=''
	}
}
document.onmousemove=positiontip
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------