function change_item_color(img,color) {
	settothisimg = "";
	if (img !="") {
		document.getElementById('item_image').src=img;	
		document.getElementById('item_image_zoom').src=img;	
		
	}	else {
		orgimg = document.getElementById('item_image').src;	
		newimg = orgimg.substr(0,orgimg.lastIndexOf('-')+1)+color.substr(0,3)+orgimg.substr(orgimg.lastIndexOf('.'),orgimg.length);
		
		document.getElementById('item_image').src=newimg;	
		document.getElementById('item_image_zoom').src=newimg;	
	}
	if (color!="") {				
		item_colorlist = document.getElementById('item_colorlist');			
		if (item_colorlist) {
			for (i=0; i <= item_colorlist.options.length - 1;i++) {			
				if (color == item_colorlist[i].value.substr(0,3)) {			
					colorname = item_colorlist[i].value.substr(4,item_colorlist[i].value.length);				
					item_colorlist.options[i].selected = true;				
				}			
			}
		}
	}
}
function updatefilter(groupname_sub) {
	var uri = document.location.href;
	if (uri.lastIndexOf('&sub=')==-1) {
		document.location.href = uri+"&sub="+groupname_sub;
	} else {
		document.location.href = uri.substring(0,uri.lastIndexOf('&sub=')+5)+groupname_sub;
	}
}
String.prototype.trim = function() {
// Strip leading and trailing white-space
return this.replace(/^\s*|\s*$/g, "");
}
function addToBasket() {
	$("#basket").css("background-image", "url("+startpath+"style/images/loading.gif)");
	
	var f= document.getElementById('shopform');
	var qty = f.item_quantity.value;
	var color = (f.item_colorlist) ? f.item_colorlist.value : "";
	var size = (f.item_sizelist) ? f.item_sizelist.value : "";
	var itemprice =  f.itemprice.value
	
	if (size.lastIndexOf('+')!=-1) {					
		var pristillagg = size.substring(size.lastIndexOf('+')+1,size.length)
		if (pristillagg != "") {
			pristillagg = pristillagg.substring(0,pristillagg.lastIndexOf(' '));
			pristillagg.trim();		
			itemprice = new Number(itemprice.trim())+new Number(pristillagg);
		}
	}	
	var itemid =  f.itemid.value;
	
	
	var timeclicked = new Date();
    timeclicked = Number(timeclicked);
    var dopost = 0;
    if (document.getElementById("addedtocartclicktime").value.length > 1) {
        if (Number(timeclicked) >  Number(document.getElementById("addedtocartclicktime").value)) {
            dopost = 1;
        }
        else {
        }
    }
    else {
        dopost = 1;
    }
    //set limit on click for 500ms
    document.getElementById("addedtocartclicktime").value = timeclicked + 500;    
    if (dopost == 1) {    	         	     	
    	   var quantity;
        if (qty > 0) {
            quantity = qty;            
        } else if (isNaN(qty)) {
            quantity = 1;                    
        } else if (f.item_quantity != null) {
            quantity = f.item_quantity.value;            
        } else {
            quantity = 1;
        }

        var jsonData = '{"itemid": "' + itemid + '","itemprice": "' + itemprice + '","itemquantity": "' + quantity + '","itemcolor": "' + color + '", "itemsize": "' + size + '"}';
								
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: startpath+"webshop/cart.php?action=addtocart",
            data: jsonData,
            dataType: "json",
            processData: false,
            success: function (data) {            	
            		var jsonresult = jQuery.parseJSON(data.success);                                
            		if (jsonresult==true) {
                	var jsonObj = jQuery.parseJSON(data.basketcontent);                                                
                	setTimeout('showCart();', 500);                  	
            		} else {
            			var errormsg = jQuery.parseJSON(data.errormsg);  
            			modalDialogWithText(errormsg);                                                              	
            		}
                
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                modalDialogWithText(textStatus);                
            }
        });
    }
    
}

function showCart() {	
  $.ajax({
      type: "GET",
      contentType: "application/json; charset=utf-8",
      url: startpath+"webshop/cart.php?action=showcart",      
      dataType: "json",
      processData: false,
      success: function (data) {            	
      		var jsonresult = jQuery.parseJSON(data.success);                                
      		if (jsonresult==true) {
          	var jsonObj = jQuery.parseJSON(data.basketcontent);                                                
						$("#basketcontent").html(jsonObj);
						
      		} else {
      			var errormsg = jQuery.parseJSON(data.errormsg);  
      			modalDialogWithText(errormsg);                                                              	
      		}

          
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
          modalDialogWithText(textStatus);                
      }
  });	

	$("#basket").css("background-image", "url("+startpath+"style/images/basket.png)"); 	            					
}
function updatebigcart(){
	var f = document.forms.bigcart;			
	f.submit()	
}
function updaterow(event) {	
	if (event.keyCode == 13) {
		var f = document.forms.bigcart;			
		f.submit()	
	}	
}
function addavisering() {
	var f = document.forms.order;
	if(f.aviseringCheckBox.checked==true) {
		document.getElementById('totprice').innerHTML = f.totalavisering.value;
	} else {
			document.getElementById('totprice').innerHTML = f.totaltaxprice.value;
	}
	document.getElementById('dhl1').style.display='';
	document.getElementById('dhl2').style.display='';
	f.customeravisering.focus();
	
}
function toorder() {
	var f = document.forms.bigcart;
	var errormsg = "";	
	if (f.totalprice.value == 0) {
		errormsg= "Varukorgen innehåller inga produkter";
		modalDialogWithText(errormsg);
		return;
	}
	f.submitaction.value='order';
	f.submit()	
}
function copyinvoiceaddress() {
	var f = document.forms.order;
	f.customerlevcompany.value=f.customercompany.value;
	f.customerlevaddress.value=f.customerinvoiceaddress.value;
	f.customerlevzip.value=f.customerinvoicezip.value;
	f.customerlevcity.value=f.customerinvoicecity.value;
}
function sendorder() {
	var f = document.forms.order;
	var errormsg = "";
	
	if (f.totalprice.value == 0) {
		errormsg= "Varukorgen innehåller inga produkter";
		modalDialogWithText(errormsg);
		return;
	}	
	if (f.customerlevcompany.value=="") { errormsg +="Leveransadress - Företagsnamn<br />";}
	if (f.customerlevaddress.value=="") { errormsg +="Leveransadress - Gatuadress<br />";}
	if (f.customerlevzip.value=="") { errormsg +="Leveransadress - Postnr<br />";}
	if (f.customerlevcity.value=="") { errormsg +="Leveransadress - Ort<br />";}
	
	
	if (errormsg == ""){
	if (f.acceptCheckBox.checked != true) {
		errormsg += "Accepetera villkoren för att gå vidare<br />";		
	}}
	
	if (f.tryckRadio[0].checked != true){
		if (f.tryckRadio[1].checked != true) {
			errormsg += "<br />Vill du beställa tryck?<br />";		
		}
	}
	
	if (errormsg != "") {	
		errormsg = "<b>Följande uppgifter saknas</b>:<br /><br />"+errormsg;	
		modalDialogWithText(errormsg);	
		return;
	}
	
	f.submit()	
}

	
