function changeSum(num)
{
	if (num==0) num = selected;
	selected = num;
    obj = document.getElementById('sum');
    if(obj==null) return;
	try{
		if (num >= condition)
		{
			document.getElementById('promotion').disabled = false;
		}
		else
		{
			document.getElementById('promotion').disabled = true;
			document.getElementById('promotion').checked = false;
		}
		if (document.getElementById('promotion').checked)
		{
			if(promotion_price == undefined) promotion_price=document.getElementById('promotion').value;
			promo = promotion_price + " " + currencyText + " + ";
			promo_price = promotion_price;
		}
		else
		{
			promo = '';
			promo_price = 0;
		}
	}
	catch(e)
	{
		promo = '';
		promo_price = 0;
	}
	//obj.value = " " + window["price_" + num] + " " + currencyText + " + " + promo + shippingCost + " " + currencyText + " = " + (shippingCost + window["price_" + num] + promo_price) + " " + currencyText;
	var disObj=document.getElementById('discount');
	
		
			var discount=0;
			if(disObj!=null){
				if(disObj.value>0) discount=disObj.value;
			}
			//obj.value = " (" + window["price_" + num] + " " + currencyText + " + " + promo + ") - "+ disObj.value +"%" +shippingCost + " " + currencyText
			var shipping=shippingCost;
			
			if(typeof(window["shipping_" + num])!="undefined") shipping=window["shipping_" + num];
			
			var paymentType=document.getElementsByName('paymentType');
			if(typeof(paymentType)!="undefined"){
				var payment='';
				for(var i=0;i<paymentType.length;i++ )
				{
					if(paymentType[i].checked==true){
					 	payment=paymentType[i].value;
					 	break;
					}
				}
			}
			if(typeof(shippingMassCollectCost)!="undefined" && payment=='MassCollect') shipping=shippingMassCollectCost;
			
			
			
			
			obj.value='';
			if(promo_price>0 && discount>0) obj.value += " (";
			
			obj.value += window["price_" + num] + " " + currencyText;
			
			if(promo_price>0) obj.value += " + "+promo_price+ " " + currencyText
			if(promo_price>0 && discount>0) obj.value += ")";
			
			if(discount>0) obj.value+=" - "+ disObj.value +"%"
			if(shipping!=0)	obj.value+=" +" +shipping + " " + currencyText
			
			
			var subtotal=window["price_" + num] + promo_price;
			var suma= subtotal;
			if(discount>0)	suma= Math.round((subtotal-(subtotal*disObj.value/100))*100)/100;
			obj.value+= " = " + (suma + shipping) + " " + currencyText;
			//console.log(obj.value);
			
		
		
		
	var ident=document.getElementById("numItems_" + num);
    if(ident!=null)    ident.checked = true;
}


if (typeof(jQuery)!="undefined") { 
	$(document).ready(function(){
		$('#discount_code').bind('change',function(){
		    var v=$(this).val();
		    $('#discount_code_txt').html('');
		    $('#discount_code').removeClass('error');
		    $('#discount_code').removeClass('ok');
		    if(v!=''){
		    	$('#discount_code_txt').html('<img src="includes/ajax_loader_spinner.gif"/>');
		    	
			    $.ajax({
			       type: "POST",
			       //url: "form.php",
			       dataType:'json',
			       data: {'action':'validateDiscount','discount_code':v},
			       success: function(msg){
			           $('#discount').val(msg.discount);
			           
			           var tmp=document.getElementById('numItems');
			           if(tmp!=null)
			        	   changeSum(tmp.value);
			           else
			           changeSum($('input[name="numItems"]:checked').val());
			           if(msg.discount>0){
			        	   $('#discount_code_txt').html('');
			        	   $('#discount_code').removeClass('error');
			        	   $('#discount_code').addClass('ok');
			           }else{
			        	   $('#discount_code_txt').html('Invalid');
			        	   $('#discount_code').addClass('error');
			        	   $('#discount_code').removeClass('ok');
			           }
			       }
			    });
		    }
		});
		if(document.getElementById('state')!=null){
			 $('#state optgroup').hide();
			 $('#state optgroup').attr('disabled',true);
			 $('#state optgroup[label='+$('#country').val()+']').show();
			 $('#state optgroup[label='+$('#country').val()+']').attr('disabled',false);
			 
			$('#country').bind('change',function(){
			    var country=$(this).val();
			    $('#state optgroup').hide();
			    $('#state optgroup').attr('disabled',true);
			    $('#state optgroup[label='+country+']').show();
			    $('#state optgroup[label='+country+']').attr('disabled',false);
			})
		}

		
		
	});
	
	
	
}
