// JavaScript Document

/********************************************************************************
 *
 * Obiekt do wyswietlania, budowania i validacji wersji produktu
 *
 * @autor Łukasz Pior
 * @version 1.0
 * @date 2006-07-04
 *
 ********************************************************************************/

function Product( id, name, oProductVersionInfo )
{
	/********************************************************************************
	 ********************************************************************************
	 **
	 ** Properties
	 **
	 ********************************************************************************
	 ********************************************************************************/

	this.name = null;
	this.oVersionInfo = {};
	this.oVersionInfolength = 0;
	this.id = null;
	this.selects = '';

	/********************************************************************************
	 ********************************************************************************
	 **
	 ** Methods
	 **
	 ********************************************************************************
	 ********************************************************************************/

	/********************************************************************************
	 *
	 * Metoda ustawiająca dane selectów
	 *
	 * przykładowy oProductVersionInfo wygląda tak:
	 * { nazwa_1:['opt1'], nazwa_2:['opt_1','opt_2'], nazwa_3:['opt_1','opt_2','opt_3']}
	 *
	 ********************************************************************************/

	this.add_version_info = function( oProductVersionInfo ){
		this.oVersionInfo = oProductVersionInfo;
	}

	this.oVersionInfo_calculate_length = function(){
		for(var i in this.oVersionInfo)	{
			this.oVersionInfolength++;
		}
		this.oVersionInfolength--;
	}

	/********************************************************************************
	 *
	 * Metoda twoząca dla kazdego obiektu z oVersionInfo selecta i zwracająca 
	 * wszystkie
	 *
	 ********************************************************************************/

	this.create_selects = function(){
		this.selects = '';
		for(var i in this.oVersionInfo){
			if(typeof(this.oVersionInfo[i])=='object')
			{
				this.create_select(i);
			}
		} // for
		this.selects += '<input id="product_'+this.id+'_version" type="hidden" value="" />';
		return this.selects;
	}

	/********************************************************************************
	 *
	 * Metoda twoząca pojedynczego selecta
	 * dany select musi zawierać przynajmniej jedną opcje, 
	 * w przeciwnym przypadku nie zostanie stworzony
	 *
	 ********************************************************************************/

	this.create_select = function( type ){
		var values = this.oVersionInfo[type];
		if(values.length>0){
			this.selects += '<select class="version" onchange="'+this.name+'.change_version(\'product_'+this.id+'_'+type+'\',this.value)"><option value="-1">Wybierz '+type.toString()+'</'+'option>';
			for(var i=0; i<values.length; i++){
				if(values[i]!='undefined')
				{
					this.selects += '<option value="'+values[i]+'">'+values[i]+'</'+'option>';
				}
			} // for
			this.selects += '</'+'select>';
			this.selects += '<input id="product_'+this.id+'_'+type+'" type="hidden" value="" />';
		} // if
	}

	/********************************************************************************
	 *
	 * Metoda zmieniająca ukrytego inputa z parametrem versji
	 *
	 ********************************************************************************/

	this.change_version = function( what, v ){
		var w = document.getElementById(what);
		var u = document.getElementById(String('product_'+this.id+'_version')); // ukryty input z wersją
		w.value = v; // wartosc wybranej opcji selecta
		var val=''; // zerujemy wersje
		for(var i in this.oVersionInfo){
			if(typeof(this.oVersionInfo[i])=='object')
			{
				var s = document.getElementById(String('product_'+this.id+'_'+i+'')).value;
				val += s+',';
			}
		} // for
		val = val.substring(0,val.length-1); // taki pseudo trim
		u.value = String( val ); // podmieniamy wersję
	}

	/********************************************************************************
	 *
	 * Metoda sprawdzająca czy są wybrane wszystkie selecty, a dokladniej parsująca
	 * ukrytego inputa z versją
	 *
	 ********************************************************************************/

	this.validate = function(){
		var v = document.getElementById('product_'+this.id+'_version').value.split(',');
		for(var i in v){
			if(v[i]=='-1' || v[i]==''){
				alert('Przed dodaniem produktu do koszyka musisz wybrać wszystkie parametry produktu z rozwijalnego menu.');
				return false;
			} // if
		} // for
		return true;
	}

	/********************************************************************************
	 ********************************************************************************
	 **
	 ** Constructor
	 **
 	 ********************************************************************************
	 ********************************************************************************/

	this.id = id;
	this.name = name;
	this.oVersionInfo = oProductVersionInfo;
	this.oVersionInfo_calculate_length();
}
