/**************************************************
 * Shipping estimation functions.
 */

function getCartItemQty() {
	var cartItemQtyArray = new Array();
	var index = 0;
	var cartItemId = "cartItems[INDEX].quantity";
	var curCartItemNode = document.getElementById(cartItemId.replace("INDEX", index));
	while (curCartItemNode != null) {
		cartItemQtyArray[cartItemQtyArray.length] = curCartItemNode.value;
		index ++;
		curCartItemNode = document.getElementById(cartItemId.replace("INDEX", index));
	}
	//alert(DWRUtil.toDescriptiveString(cartItemQtyArray, 2));
	return cartItemQtyArray;
}

function showDeliveryOptions(shoppingCart) {
	shoppingCartAjaxController.calculateForSelectedShippingServiceLevel(shoppingCart.selectedShippingServiceLevel.uidPk , updateCartSummary);   		
}

function updateCartSummary (shoppingCart) {
	var totalDiv=document.getElementById("cartTotalValue");
	totalDiv.innerHTML= '<strong>'+ shoppingCart.totalMoney.moneyValueAndSymbol+ '</strong>';
	updateShippingCost(shoppingCart);
}


var block = false;

function deleteCartItem(itemUid, isCertificate) {
	var cartItemQtyArray = getCartItemQty();
	if (!block) {
		if (itemUid) {
			block = true;
			DWREngine.beginBatch();
			shoppingCartAjaxController.deleteCartItem(itemUid, isCertificate, deleteCartItemCallback);		
			DWREngine.endBatch({verb:"GET", ordered:true});		
			document.getElementById('I' + itemUid).style.display = 'none';
	    } else {
	    	alert(specifyShippingStr);
	    }
    }
}

function estimateShippingByRegion(shippingRegionUidPk, shippingRegionName) {
	var cartItemQtyArray = getCartItemQty();
	if (shippingRegionUidPk) {
    	DWREngine.beginBatch();
    	document.getElementById("shippingOptions").style.display = "none";
		shoppingCartAjaxController.estimateShippingByShippingRegion(shippingRegionUidPk, cartItemQtyArray, estimateShippingCallBack);
		
		if (shippingRegionName != null) {
			updateEstimateCountry(shippingRegionName);
		}
		DWREngine.endBatch({verb:"GET", ordered:true});
    } else {
    	alert(specifyShippingStr);
    }
}

function estimateShippingCallBack(shoppingCart) {
	if (shoppingCart.selectedShippingServiceLevel) {
    	selectedShippingServiceLevelId = shoppingCart.selectedShippingServiceLevel.uidPk;
    	if (shoppingCart.shippingServiceLevelList.length > 0) {
        	showDeliveryOptions(shoppingCart);
        	updateCartSummary(shoppingCart);        	
    	}
    } else {
    	// alert(noDeliveryOptionStr);
    	updateCartSummary(shoppingCart);    	
    }
}

function deleteCartItemCallback(shoppingCart) {
	    
    	if (shoppingCart.selectedShippingServiceLevel) {
	    	selectedShippingServiceLevelId = shoppingCart.selectedShippingServiceLevel.uidPk;
	    	if (shoppingCart.shippingServiceLevelList.length > 0) {
	        	showDeliveryOptions(shoppingCart);	
	    	}
    	} 
    	
	    var totalDiv=document.getElementById("cartTotalValue");
		totalDiv.innerHTML= '<strong>'+ shoppingCart.totalMoney.moneyValueAndSymbol+ '</strong>';

	   	document.getElementById("shippingOptions").style.display = 'none';
    	if (shoppingCart.shoppingCartItemsCount == 0) {
    		document.getElementById('tinyCart').style.display = 'none';
    	} else if (shoppingCart.shippingRegion) {
    		estimateShippingByRegion(shoppingCart.shippingRegion.uidPk, null);
    	}
    	block = false;
    	
}

function updateEstimateCountry(countryStr) {
	var country = document.getElementById("country");
	if (country) {
		country.innerHTML = countryStr;
	} else {
		var parent = document.getElementById("shippingRegion");
		country = document.createElement('a');
		country.setAttribute('id','country');
		country.setAttribute('name','country');
		country.setAttribute('href','#');
		country.setAttribute('id','country');
		country.onclick=function(){setShippingStyle();}
		//country.onmouseover=function(){onElMouseover(this.id);}
		//country.onmouseout=function(){onElMouseout(this.id);}
		country.innerHTML = countryStr;
		var cal = document.getElementById("calc");
		parent.insertBefore(country, cal);
		parent.removeChild(cal);
	}
}

function setShippingStyle()
{
	var shippingOptions = document.getElementById('shippingOptions');
	shippingOptions.style.display='block';
	shippingOptions.style.background='url(../grafik/versand_open.gif) 180px 6px no-repeat #E9EDD3';	
}

function onElMouseover(id) {
	document.getElementById(id).style.backgroundColor ='#E4E7CB';
	document.getElementById('versandneu').style.backgroundColor ='#E4E7CB';
	var shippingCostRow = document.getElementById('shippingCostRow');
	if (shippingCostRow) {
		shippingCostRow.style.backgroundColor ='#E4E7CB';
	}
}
function onElMouseout(id) {
	document.getElementById(id).style.backgroundColor = '#E9EDD3';
	document.getElementById('versandneu').style.backgroundColor ='#E9EDD3';
	var shippingCostRow = document.getElementById('shippingCostRow');
	if (shippingCostRow) {
		shippingCostRow.style.backgroundColor ='#E9EDD3';
	}
}

function updateShippingCost(shoppingCart) {
	var shippingCost = document.getElementById("shippingCost");
	var language = getLang();
	
	if (shippingCost) {
		if (shoppingCart.shippingCost.amount > 0) { 
	   		shippingCost.innerHTML = shoppingCart.shippingCost.moneyValueAndSymbol;
	   	} else {	   	
	   		var freeShippingHidden = document.getElementById('freeShippingHidden');
	   		shippingCost.innerHTML = freeShippingHidden.value;	   		
	   	}
    } else {
		var tbody = document.getElementById("cartTable");
		var lastRow = tbody.rows.length-2;
		if (lastRow <=0) {
			lastRow = 1;
		}
		var shippingCostRow = tbody.insertRow(lastRow);
       	shippingCostRow.setAttribute('id','shippingCostRow');
       	shippingCostRow.appendChild(document.createElement('td'));
       	shippingCostRow.insertCell(0);
       	//shippingCostRow.onmouseover=function(){onElMouseover(this.id);}
		//shippingCostRow.onmouseout=function(){onElMouseout(this.id);}
       	var productTd = shippingCostRow.insertCell(1);//document.createElement('td');
       	productTd.setAttribute('class','produkt');
       	productTd.innerHTML = '<strong>' + document.getElementById('tinycart.forwardingExpense.text').value + '</strong>';
              	
       	shippingCost = shippingCostRow.insertCell(2);//document.createElement('td');
       	shippingCost.setAttribute('id','shippingCost');
       	shippingCost.setAttribute('class','preis');
       	shippingCost.innerHTML = shoppingCart.shippingCost.moneyValueAndSymbol;
       
       	var infoTd = shippingCostRow.insertCell(3); document.createElement('td');
       	infoTd.setAttribute('class','info');
       	/*var aLink = document.createElement('a');
       	aLink.setAttribute('href','#',0);
       	if (language == "DE") {
       		//tinycart.forwardingExpense
       		aLink.setAttribute('title','Versand',0);
       	} else {
       		aLink.setAttribute('title','Shipping',0);
       	}   	
       	
       	infoTd.appendChild(aLink);*/
       	
		}
}

function getLang() {
	var lang;
	if (typeof navigator.userLanguage !="undefined") {
		lang = navigator.userLanguage.toUpperCase();
	} else if (typeof navigator.language !="undefined") {
		lang = navigator.language.toUpperCase();
	}
	if (lang == null || lang.substring(0,2) == "DE") {
		lang = "DE";
	}
	return lang;
}