/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
if(typeof Product=='undefined') {
    var Product = {};
}
/**************************** BUNDLE PRODUCT **************************/
Product.Bundle = Class.create();
Product.Bundle.prototype = {
    initialize: function(config){
        this.config = config;
        this.reloadPrice();
    },
    
    checkoptions: function (type,textbox,checkbox,textcount,SelectionId)
    {
    	if(type=='check')
    	{
	    	if(document.getElementById(checkbox).checked)
	    	{
		    	document.getElementById("td_"+SelectionId).className="dipping_selected";
	       		document.getElementById("td_price_"+SelectionId).className="dipping_price";	            		
	       		document.getElementById("td_chk_"+SelectionId).style.backgroundColor="#a50034";
		    	document.getElementById(textcount).value=document.getElementById(textbox.id).value;
		    }
		    else
		    { 
		    	document.getElementById(textbox.id).value="";
		    	document.getElementById(textcount).value=document.getElementById(textbox.id).value;
		    	document.getElementById("td_"+SelectionId).className="dipping";
	       		document.getElementById("td_price_"+SelectionId).className="dipping_not_price";	            		
	       		document.getElementById("td_chk_"+SelectionId).style.backgroundColor="#dc86a1";
		    }
	    }
	    else
	    {
		    var oHidden = document.getElementsByName("hiddenText");
	    	var count=0;
	    	var totalCount=0;
			for(i=0;i<oHidden.length;i++)
			{
				count=oHidden[i].value;
			}
	    	var oCount = document.getElementsByName("counttext");
	        for(i=0;i<oCount.length;i++)
			{
				
				if(!isNaN(oCount[i].value) && oCount[i].value.length>0)
				{
					totalCount=parseInt(totalCount)+parseInt(oCount[i].value);
				}
			}
			if(isNaN(document.getElementById(textbox.id).value) || (document.getElementById(textbox.id).value).length<=0)
			{
				document.getElementById("createcount").innerHTML=count-totalCount;
				document.getElementById(checkbox).checked=false;
				document.getElementById("td_"+SelectionId).className="dipping";
	        	document.getElementById("td_price_"+SelectionId).className="dipping_not_price";	            		
	        	document.getElementById("td_chk_"+SelectionId).style.backgroundColor="#dc86a1";
			}
			else
			{
				if(parseInt(document.getElementById("createcount").innerHTML)>0)
		    	{
			    	if(parseInt(totalCount)>=parseInt(document.getElementById("createcount").innerHTML))
			    	{
			    		//document.getElementById(textbox.id).value=document.getElementById("createcount").innerHTML;
			    		
			    	}
			    	document.getElementById("td_"+SelectionId).className="dipping_selected";
	           		document.getElementById("td_price_"+SelectionId).className="dipping_price";	            		
	           		document.getElementById("td_chk_"+SelectionId).style.backgroundColor="#a50034";
			    	document.getElementById(textcount).value=document.getElementById(textbox.id).value;
			    	document.getElementById(checkbox).checked=true;
			    	document.getElementById("createcount").innerHTML=(parseInt(count)-parseInt(totalCount));
			    	if(parseInt(document.getElementById("createcount").innerHTML)<0)
			    	{
			    		document.getElementById(textbox.id).value=parseInt(document.getElementById(textbox.id).value)+parseInt(document.getElementById("createcount").innerHTML);
			    		document.getElementById("createcount").innerHTML=0;
			    	}
			    }
			    else
			    { 
			    	document.getElementById(textbox.id).value="";
			    	document.getElementById(textcount).value=document.getElementById(textbox.id).value;
			    	document.getElementById("td_"+SelectionId).className="dipping";
	           		document.getElementById("td_price_"+SelectionId).className="dipping_not_price";	            		
	           		document.getElementById("td_chk_"+SelectionId).style.backgroundColor="#dc86a1";
			    	document.getElementById(checkbox).checked=false;
			    	document.getElementById("createcount").innerHTML=(parseInt(count)-parseInt(totalCount));
			    	if(parseInt(document.getElementById("createcount").innerHTML)<0)
			    	{
			    		document.getElementById("createcount").innerHTML=0;
			    	}
			    }
		    }
	    }
	    
    },
    
    changeSelection: function(selection,productid,position,imageneeded,id,skus){   
    	var oarr = new Array();
        oarr = document.getElementsByTagName( "*" );
    	try
    	{
    		
	    	skus=skus.split(",");
	    	
	    	for(var i=0; i<skus.length;i++)
	    	{
	    		
	    		document.getElementById("tr_"+skus[i]).style.display="block";	
	    		for(j=0;j<oarr.length;j++)
				{
					if(oarr[j].type=="radio")
					{
						if(document.getElementById("tr_"+skus[i]).innerHTML.indexOf(oarr[j].id)>0)
						{
							document.getElementById(oarr[j].id).checked=true;
							i=200;
							break;
						}
					}
				}    		
	    	}
	    	var oTR = document.getElementsByTagName("tr");
			//alert(oTR.length);				
	    	for(var j=0;j<oTR.length;j++)
	    	{
	    		if((oTR[j].id).indexOf("tr_GE")>=0)
	    		{
	    			document.getElementById(oTR[j].id).style.display="none";
	    		}
	    		//document.getElementById(oTR[j].id).style.display="none";
	    		//document.getElementById(oTR[j].id).style.visibility = "hidden";
	    	}
	    	for(var i=0; i<skus.length;i++)
	    	{
	    		document.getElementById("tr_"+skus[i]).style.display="inline";							
	    			    		
	    	}
	    	
    	}
    	catch(ex){}
        parts = selection.id.split('-'); 
       	
        if (this.config['options'][parts[2]].isMulti) {
            selected = new Array();
            if (selection.tagName == 'SELECT') { 
                for (var i = 0; i < selection.options.length; i++) { 
                    if (selection.options[i].selected && selection.options[i].value != '') {                    
                        selected.push(selection.options[i].value);
                    }
                }
            } else if (selection.tagName == 'INPUT') { 
                selector = parts[0]+'-'+parts[1]+'-'+parts[2];
                selections = $$('.'+selector);
                var oHidden = document.getElementsByName("hiddenText");
               
                for(i=0;i<oHidden.length;i++)
				{
					document.getElementById("createcount").innerHTML=oHidden[i].value;
				}
               
                for (var i = 0; i < selections.length; i++) {
                    if (selections[i].checked && selections[i].value != '') {
                        selected.push(selections[i].value);
                    }
                }
            }
            this.config.selected[parts[2]] = selected;
        } else {   
        	document.getElementById('hd_'+parts[2]).value=productid;
            if (selection.value != '') {
                this.config.selected[parts[2]] = new Array(selection.value);
            } else {
                this.config.selected[parts[2]] = new Array();
            }
            
			var oRadio = document.getElementsByName("radiodipping");
			var oPricing = document.getElementsByName("hdPrice"); 	
          	var j=0;
          	
            if(position==0)
            { 
            	if(imageneeded==1)
            	{
            		try
	            	{
		            	for(i=0;i<oRadio.length;i++)
						{
							var tdidparts1=oRadio[i].id.split('=');	
							if(tdidparts1[1]==0)
							{
								tdidparts1[1]="";
							}			
	           				if(tdidparts1.length==3)
							{
								if(tdidparts1[0]==parts[2])
								{ 
									if(!isNaN(tdidparts1[1]) && tdidparts1[1].length>0)
									{	
										if(id==tdidparts1[2])
										{
											document.getElementById("td_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_selected";
						            		document.getElementById("td_price_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_price";	            		
						            		document.getElementById("td_radio_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).style.backgroundColor="#a50034";
										}
										else
										{
											document.getElementById("td_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping";
						            		document.getElementById("td_price_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_not_price";          		
						            		document.getElementById("td_radio_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).style.backgroundColor="#dc86a1";
										}											
									}
									else if(imageneeded==1)
									{
										if(id==tdidparts1[2])
										{
											document.getElementById("td_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_selected";
						            		document.getElementById("td_price_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_price";	            		
						            		document.getElementById("td_radio_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).style.backgroundColor="#a50034";
										}
										else
										{
											document.getElementById("td_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping";
						            		document.getElementById("td_price_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_not_price";          		
						            		document.getElementById("td_radio_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).style.backgroundColor="#dc86a1";
										}
									}
								}
	           					
	           				}
					    }
	            	}
	            	catch(ex)
	            	{
	            		
	            	}
            	}
            	else
            	{
	            	try
	            	{
	            		document.getElementById("createcount").innerHTML=productid;
	            	}
	            	catch(ex)
	            	{
	            	}         	
	            	try
	            	{
	            		for(i=0;i<oRadio.length;i++)
						{
							var tdidparts1=oRadio[i].id.split('=');						
	           				if(tdidparts1.length==3)
							{
	           					if(!isNaN(tdidparts1[1]) && tdidparts1[1].length>0)
								{			
								  					
									if(tdidparts1[1]==productid)
									{
										document.getElementById("bundle-option-"+tdidparts1[0]+"-"+tdidparts1[1]).checked=true;
										if(j==0)
										{
											
											document.getElementById("td_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_selected";
						            		document.getElementById("td_price_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_price";	            		
						            		document.getElementById("td_radio_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).style.backgroundColor="#a50034";
						            		j=1;
						            	}
						            	else
						            	{
						            		document.getElementById("td_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping";
						            		document.getElementById("td_price_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_not_price";          		
						            		document.getElementById("td_radio_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).style.backgroundColor="#dc86a1";
						            	}				            		
									}	
														
								}
	           				}
					    }
	            		
	            		tds = document.getElementsByTagName('td');
						for (var i = 0; i < tds.length; i++) 
						{
							var tdidparts=tds[i].id.split('#');
							if(tdidparts.length==2)
							{	
								if(tdidparts[0]!=0)
								{
									if(!isNaN(tdidparts[0]) && tdidparts[0].length>0)
									{				
										if(tdidparts[0]==productid)
										{
											document.getElementById(tds[i].id).style.display="";									
										}
										else
										{
											document.getElementById(tds[i].id).style.display="none";
										}
									}
								}				
								
							} 
						}        		
	            		
	            	}
	            	catch(ex)
	            	{
	            		
	            	}          	
           		}
            }            
            else if( imageneeded==1)
            { 	
            
            	
            
            	try
            	{
	            	for(i=0;i<oRadio.length;i++)
					{
						var tdidparts1=oRadio[i].id.split('=');						
           				if(tdidparts1.length==3)
						{
							if(tdidparts1[0]==parts[2])
							{ 
								if(!isNaN(tdidparts1[1]) && tdidparts1[1].length>0)
								{	
									if(id==tdidparts1[2])
									{
										document.getElementById("td_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_selected";
					            		document.getElementById("td_price_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_price";	            		
					            		document.getElementById("td_radio_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).style.backgroundColor="#a50034";
									}
									else
									{
										document.getElementById("td_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping";
					            		document.getElementById("td_price_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_not_price";          		
					            		document.getElementById("td_radio_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).style.backgroundColor="#dc86a1";
									}											
								}
								else if(imageneeded==1)
								{
									if(id==tdidparts1[2])
									{
										document.getElementById("td_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_selected";
					            		document.getElementById("td_price_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_price";	            		
					            		document.getElementById("td_radio_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).style.backgroundColor="#a50034";
									}
									else
									{
										document.getElementById("td_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping";
					            		document.getElementById("td_price_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).className="dipping_not_price";          		
					            		document.getElementById("td_radio_"+tdidparts1[2]+"_"+tdidparts1[1]+"_"+tdidparts1[0]).style.backgroundColor="#dc86a1";
									}
								}
							}
           					
           				}
				    }
            	}
            	catch(ex)
            	{
            		
            	}
            	   	
            }
            this.populateQty(parts[2], selection.value);
        }
        this.reloadPrice();

    },

    reloadPrice: function() {
        var calculatedPrice = 0;
        var dispositionPrice = 0;
        for (var option in this.config.selected) {
            if (this.config.options[option]) {
                for (var i=0; i < this.config.selected[option].length; i++) {
                    var prices = this.selectionPrice(option, this.config.selected[option][i]);
                    calculatedPrice += Number(prices[0]);
                    dispositionPrice += Number(prices[1]);
                }
            }
        }

        if (this.config.specialPrice) {
            var newPrice = (calculatedPrice*this.config.specialPrice)/100;
            calculatedPrice = Math.min(newPrice, calculatedPrice);
        }

        optionsPrice.changePrice('bundle', calculatedPrice);
        optionsPrice.changePrice('nontaxable', dispositionPrice);
        optionsPrice.reload();

        return calculatedPrice;
    },

    selectionPrice: function(optionId, selectionId) {
        if (selectionId == '' || selectionId == 'none') {
            return 0;
        }

        if (this.config.options[optionId].selections[selectionId].customQty == 1 && !this.config['options'][optionId].isMulti) {
            if ($('bundle-option-' + optionId + '-qty-input')) {
                qty = $('bundle-option-' + optionId + '-qty-input').value;
            } else {
                qty = 1;
            }
        } else {
            qty = this.config.options[optionId].selections[selectionId].qty;
        }

        if (this.config.priceType == '0') {
            price = this.config.options[optionId].selections[selectionId].price;
            tierPrice = this.config.options[optionId].selections[selectionId].tierPrice;

            for (var i=0; i < tierPrice.length; i++) {
                if (Number(tierPrice[i].price_qty) <= qty && Number(tierPrice[i].price) <= price) {
                    price = tierPrice[i].price;
                }
            }
        } else {
            selection = this.config.options[optionId].selections[selectionId];
            if (selection.priceType == '0') {
                price = selection.priceValue;
            } else {
                price = (this.config.basePrice*selection.priceValue)/100;
            }
        }
        //price += this.config.options[optionId].selections[selectionId].plusDisposition;
        //price -= this.config.options[optionId].selections[selectionId].minusDisposition;
        //return price*qty;
        var disposition = this.config.options[optionId].selections[selectionId].plusDisposition +
            this.config.options[optionId].selections[selectionId].minusDisposition;

        var result = new Array(price*qty, disposition*qty);
        return result;
    },

    populateQty: function(optionId, selectionId){
        if (selectionId == '' || selectionId == 'none') {
            this.showQtyInput(optionId, '0', false);
            return;
        }
        if (this.config.options[optionId].selections[selectionId].customQty == 1) {
            this.showQtyInput(optionId, this.config.options[optionId].selections[selectionId].qty, true);
        } else {
            this.showQtyInput(optionId, this.config.options[optionId].selections[selectionId].qty, false);
        }
    },

    showQtyInput: function(optionId, value, canEdit) {
        elem = $('bundle-option-' + optionId + '-qty-input');
        elem.value = value;
        elem.disabled = !canEdit;
        if (canEdit) {
            elem.removeClassName('qty-disabled');
        } else {
            elem.addClassName('qty-disabled');
        }
    },

    changeOptionQty: function (element, event) {
        var checkQty = true;
        if (typeof(event) != 'undefined') {
            if (event.keyCode == 8 || event.keyCode == 46) {
                checkQty = false;
            }
        }
        if (checkQty && (Number(element.value) == 0 || isNaN(Number(element.value)))) {
            element.value = 1;
        }
        parts = element.id.split('-');
        optionId = parts[2];
        if (!this.config['options'][optionId].isMulti) {
            selectionId = this.config.selected[optionId][0];
            /*document.getElementById('bundle-option-2-qty-input').value=element.value*1;
            alert(document.getElementById('hd1-dozen').value);
            alert(document.getElementById('hd1-2-dozen').value);
            alert(document.getElementById('hd2-dozen').value);
            this.config.options[2].selections[5].qty=element.value*1;
            this.config.options[2].selections[4].qty=element.value*1;
            this.config.options[2].selections[6].qty=element.value*1;*/
            this.config.options[optionId].selections[selectionId].qty = element.value*1;
            this.reloadPrice();
        }
    },

    validationCallback: function (elmId, result){
        if (typeof elmId == 'undefined') {
            return;
        }
        var container = $(elmId).up('ul.options-list');
        if (typeof container != 'undefined') {
            if (result == 'failed') {
                container.removeClassName('validation-passed');
                container.addClassName('validation-failed');
            } else {
                container.removeClassName('validation-failed');
                container.addClassName('validation-passed');
            }
        }
    }
}