function explode( delimiter, string, limit ) {  
    // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.    
    //   
    // version: 810.114  
    // discuss at: http://phpjs.org/functions/explode  
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
    // +     improved by: kenneth  
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
    // +     improved by: d3x  
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
    // *     example 1: explode(' ', 'Kevin van Zonneveld');  
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}  
    // *     example 2: explode('=', 'a=bc=d', 2);  
    // *     returns 2: ['a', 'bc=d']  
   
    var emptyArray = { 0: '' };  
      
    // third argument is not required  
    if ( arguments.length < 2  
        || typeof arguments[0] == 'undefined'  
        || typeof arguments[1] == 'undefined' )  
    {  
        return null;  
    }  
   
    if ( delimiter === ''  
        || delimiter === false  
        || delimiter === null )  
    {  
        return false;  
    }  
   
    if ( typeof delimiter == 'function'  
        || typeof delimiter == 'object'  
        || typeof string == 'function'  
        || typeof string == 'object' )  
    {  
        return emptyArray;  
    }  
   
    if ( delimiter === true ) {  
        delimiter = '1';  
    }  
      
    if (!limit) {  
        return string.toString().split(delimiter.toString());  
    } else {  
        // support for limit argument  
        var splitted = string.toString().split(delimiter.toString());  
        var partA = splitted.splice(0, limit - 1);  
        var partB = splitted.join(delimiter.toString());  
        partA.push(partB);  
        return partA;  
    }  
}  


function setNewPrice(form, atr) {

	var price = document.getElementById('mainproductprice').innerHTML;
	price = explode(' ', price);
	var waluta = price[1];
	price = Number(price[0]);
		
	
	var opt = form.getElementsByTagName('select');
	for(var i = 0; i < opt.length; i++){
//FILIP4YETI było 		if (opt[i].options[opt[i].selectedIndex].label != '') {
	//testowalem && opt[i].options[opt[i].selectedIndex].label != opt[i].options[opt[i].selectedIndex].text ale wywalilem
		if (opt[i].options[opt[i].selectedIndex].label != '' ) {
			elprice = explode(' ', opt[i].options[opt[i].selectedIndex].label);
			//FILIP4YETI dodalem warunek
			if(!isNaN(Number(elprice[0])))
				price = price + Number(elprice[0]);

		}
					
	}
	
	document.getElementById('productprice').innerHTML = price + ' ' + waluta;
}

/*CHECKBOX*/
function initRichCheckbox() { 
    var els = document.getElementsByTagName('input');
    for(var i=0; i<els.length; i++) {
       if (els[i].type=='checkbox'){
        new RichCheckbox(els[i]); 
       } 
    }
} 
   
function RichCheckbox(org) {
    this.state=false;
    this.orgcheckbox=org;
    this.checkboxDiv;
    this.initialize();
} 
RichCheckbox.prototype.initialize = function () {
    
    this.checkboxDiv = document.createElement('div');
    if(this.orgcheckbox.checked==true) {
        this.checkboxDiv.className='richCheckboxOn';
    }
    else {
        this.checkboxDiv.className='richCheckboxOff';
    }
    this.orgcheckbox.parentNode.insertBefore(this.checkboxDiv, this.orgcheckbox);
    var self=this;
    this.checkboxDiv.onclick = function() { 
        self.togglebox();
    }
    this.orgcheckbox.style.display='none';

}
RichCheckbox.prototype.togglebox = function(num) {
    if(this.orgcheckbox.checked==true) {
        this.orgcheckbox.checked=false;
        this.checkboxDiv.className='richCheckboxOff';
    }
    else {
        this.orgcheckbox.checked=true;
        this.checkboxDiv.className='richCheckboxOn';
    }
}

/*RADIO*/
function initRichRadio() { 
    names=new Array();
    var els = document.getElementsByTagName('input');
    for(var i=0; i<els.length; i++) {
        if (els[i].type=='radio'){
            addnew=true;
            for(j=0; j<names.length; j++) {
                if(names[j]==els[i].name) {
                    addnew=false;
                }
            }
            if(addnew) {
                names[names.length]=els[i].name;
                new RichRadio(els[i].name);
            }
        }
    }
}   
function RichRadio(radioname) {
    this.radioname=radioname;
    this.options=new Array();
    this.initialize();
} 
RichRadio.prototype.initialize = function () {
    
    var els = document.getElementsByTagName('input');
    for(var i=0; i<els.length; i++) {
        if (els[i].type=='radio'&&els[i].name==this.radioname){
            this.options[this.options.length]=Array(els[i],null);
        }
    }
    for(var i=0; i<this.options.length; i++) {
        
        this.options[i][1]=document.createElement('div');
        this.options[i][1].optionid=i;
        if(this.options[i][0].checked==true) {
            this.options[i][1].className='richRadio_on';
        }
        else {
            this.options[i][1].className='richRadio_off';
        }
        var self=this;
        this.options[i][1].onclick=function() { self.toggleOptions(this.optionid); }
        this.options[i][0].parentNode.insertBefore(this.options[i][1], this.options[i][0]);
        this.options[i][0].style.display='none';
    }
}
RichRadio.prototype.toggleOptions = function(newoption) {
    for(i=0; i<this.options.length; i++) {
        this.options[i][0].checked=false;
        this.options[i][1].className='richRadio_off';
    }
    this.options[newoption][0].checked=true;
    this.options[newoption][1].className='richRadio_on';
}
/*RICHSELECT*/
function initRichSelect() { 
    var els = document.getElementsByTagName('select');
    for(var i=0; i<els.length; i++) {
        new RichSelect(els[i]); 
    }
}
    
   
function RichSelect(org) {
    this.options=new Array();
    this.optcount=0;
    this.curoption=0;
    this.orgselect=org;
    this.initialize();
    
    this.containerDiv;
    this.mainDiv;
    this.optionsDiv;
    this.expanded=false;
    this.classextra='';
}

RichSelect.prototype.initialize = function () {
    this.id="richSelect_"+this.orgselect.name;
    this.classextra=this.orgselect.className;
    for(var i=0; i<this.orgselect.childNodes.length; i++) {
        if(this.orgselect.childNodes[i].nodeName=='OPTION') {
            this.options[this.optcount]=Array(this.orgselect.childNodes[i].value, this.orgselect.childNodes[i].innerHTML, null, this.orgselect.childNodes[i]);
            if(this.orgselect.childNodes[i].selected==true) {
                this.curoption=this.optcount;
            }
            this.optcount++;
        }
    }
    this.render();

    //this.orgselect.parentNode.insertBefore(myElement, this.orgselect);
}

kl='';

RichSelect.prototype.render = function () {
    //tempw=this.orgselect.offsetWidth;
    this.orgselect.style.display='none';
    this.containerDiv=document.createElement('div');
    this.containerDiv.className='richSelect'+' '+this.classextra;
    //this.containerDiv.style.width=tempw+'px';
    this.mainDiv=document.createElement('div');
    this.mainDiv.innerHTML=this.options[this.curoption][1];
    this.mainDiv.className='richSelectMain';
    /*if(this.classextra=='smallSelect') {
        this.mainDiv.style.width=tempw-19+'px';
    }
    else {
        this.mainDiv.style.width=tempw-30+'px';
    }*/
    var self=this;
    this.mainDiv.onclick=function() { self.toggleOptions(); }
    this.mainDiv.onchange=function() { self.toggleOptions(); }
    this.mainDiv.onmouseover=function() { self.mainOver(); }
    this.mainDiv.onmouseout=function() { self.mainOut(); }
    this.containerDiv.appendChild(this.mainDiv);
    
    this.optionsDiv=document.createElement('div');
    this.optionsDiv.className='richSelectOptions';
    this.optionsDiv.style.display='none';
    this.optionsDiv.style.overflow='auto';
    if(this.options.length>8) {
 //przenioslem    this.optionsDiv.style.overflow='auto';
 //poza if-a - powyzej FILIP4YETI 
		this.optionsDiv.style.height=25*8+'px';
        //this.optionsDiv.style.width=tempw+20+'px';
        //prawyborder=false;
    }
    //else { prawyborder=true; this.optionsDiv.style.width=tempw+2+'px';}
    for(var i=0; i<this.options.length; i++) {
        this.options[i][2]=document.createElement('div');
        //this.options[i][2].style.width=(this.optionsDiv.offsetWidth-10)+'px';
//FILIP4YETI NIE
		if (this.options[i][1].indexOf('-10%') >= 0){
			kl=' promo10';
			this.options[i][1]=this.options[i][1].replace('-10%', '<span style="color:orange;">-10%</span>');
		}else if(this.options[i][1].indexOf('-20%') >= 0){
			kl=' promo20';
			this.options[i][1]=this.options[i][1].replace('-20%', '<span style="color:red;">-20%</span>');
		}else
			kl='';

        this.options[i][2].innerHTML=this.options[i][1];			
			
        if(i==this.curoption) {
            this.options[i][2].className='richSelectOption richSelectOptionCurrent' + kl;
        }
        else {
            this.options[i][2].className='richSelectOption' + kl;
        }
        this.options[i][2].optionid=i;
        if(this.options[i][3].disabled==false) {
            this.options[i][2].onmouseover=function() { self.optionOver(this.optionid);}
            this.options[i][2].onmouseout=function() { self.optionOut(this.optionid);}
            this.options[i][2].onclick=function() { self.optionClick(this.optionid);}
        }
        //if(!prawyborder) {
		//	this.options[i][2].style.borderRightWidth='0px';
        //}
        this.optionsDiv.appendChild(this.options[i][2]);
        
    }
    
    this.containerDiv.appendChild(this.optionsDiv);
    this.orgselect.parentNode.insertBefore(this.containerDiv, this.orgselect); 
    
}
RichSelect.prototype.toggleOptions = function () {
    if(this.expanded) {
        this.optionsDiv.style.display='none';
        this.expanded=false;
        this.mainDiv.className='richSelectMain';
    }
    else {
        this.expanded=true;
        this.optionsDiv.style.display='block';
        this.mainDiv.className='richSelectMain richSelectMainExpand';
    }
}
RichSelect.prototype.mainOver = function () {
    if(!this.expanded) { this.mainDiv.className='richSelectMain  richSelectMainOver'; }
}
RichSelect.prototype.mainOut = function () {
    if(!this.expanded) { this.mainDiv.className='richSelectMain'; }
}
RichSelect.prototype.optionOver = function (optover) {
//FILIP4YETI NIE
		if (this.options[optover][1].indexOf('-10%') >= 0){
			kl=' promo10';
//			this.options[optover][1]=this.options[optover][1].replace('-10%', '');
		}else if (this.options[optover][1].indexOf('-20%') >= 0){
			kl=' promo20';
//			this.options[optover][1]=this.options[optover][1].replace('-20%', '');
		}else
			kl='';

    if(this.curoption==optover) {
        this.options[optover][2].className='richSelectOption richSelectOptionCurrent richSelectOptionOver' + kl;
    }
    else {
        this.options[optover][2].className='richSelectOption richSelectOptionOver' + kl;    
    }
}
RichSelect.prototype.optionOut= function (optout) {
//FILIP4YETI NIE
		if (this.options[optout][1].indexOf('-10%') >= 0){
			kl=' promo10';
//			this.options[optout][1]=this.options[optout][1].replace('-10%', '');
		}else if (this.options[optout][1].indexOf('-20%') >= 0){
			kl=' promo20';
//			this.options[optout][1]=this.options[optout][1].replace('-20%', '');
		}else
			kl='';

		if(this.curoption==optout) {
        this.options[optout][2].className='richSelectOption richSelectOptionCurrent' +kl;
    }
    else {
        this.options[optout][2].className='richSelectOption' +kl;
    }
}
RichSelect.prototype.optionClick = function (optclick) {
    this.curoption=optclick;
    this.rerender();
    this.toggleOptions();
    this.update();
    if(this.orgselect.onchange) { this.orgselect.onchange(); }
}
RichSelect.prototype.rerender = function () {
    this.mainDiv.innerHTML=this.options[this.curoption][1];

    for(var i=0; i<this.options.length; i++) {
//FILIP4YETI NIE
		if (this.options[i][1].indexOf('-10%') >= 0){
			kl=' promo10';
//			this.options[i][1]=this.options[i][1].replace('-10%', '');
		}else if (this.options[i][1].indexOf('-20%') >= 0){
			kl=' promo20';
//			this.options[i][1]=this.options[i][1].replace('-20%', '');
		}else
			kl='';

	if(i==this.curoption) {
            this.options[i][2].className='richSelectOption richSelectOptionCurrent' + kl ;
        }
        else {
            this.options[i][2].className='richSelectOption' + kl;
        }
    }   
}
RichSelect.prototype.update = function () {
    for(var i=0; i<this.options; i++) {
        this.options[i][3].selected=false;
    }
    this.options[this.curoption][3].selected=true;
}

function initRichForms() { /*initRichRadio();*/ initRichCheckbox(); initRichSelect(); }
    


if (window.addEventListener) {
	window.addEventListener("load",initRichForms,false);
} else if (window.attachEvent) {
	window.attachEvent("onload",initRichForms);
} else {
	window.onload = function() {initRichForms();}
}
