


function getSkuById(skuId){
	for (var i=0; i<skus.length; i++) {
		if (skus[i].getId()==skuId) {
			return skus[i];
		}
	}
	return null;
}

function getSoftwareByTypeAndDeliveryMethod(softwareType,deliveryMethod,language){
	if(softwareType==null || deliveryMethod==null)
		return null;

	var tmpTarget;

	for (var i=0; i<skus.length; i++) {
		if (softwareType==skus[i].getSoftwareType().toUpperCase()
			&& deliveryMethod==skus[i].getDeliveryMethod().toUpperCase()) {
		
			tmpTarget=skus[i];
			
			var languages = skus[i].getLanguageCodes(); 
			for (var j=0; j<languages.length; j++) {
				if (language==languages[j]) return skus[i];
			}
			
			// software type and delivery method matched, but language not,  return the last one found
			return tmpTarget;
		}
	}
	
	return null;
	
}


function switchSkuPrice_software(skuId){
	var sku=getSkuById(skuId);
	if(sku){
		var lstPrFormatted=sku.formattedListPrice;
		var salePrFormatted=sku.formattedSalePrice;
		
		currentSku=sku;
		
		if(sku.languageCodes == 'jp'){
			if(sku.onSale){
				document.getElementById("lstPrBrick").style.display="block";
				document.getElementById("lstPrBrick").innerHTML=sku.listPriceParts[1]+ '<span class="jpCurrencySymbol">&nbsp;'+sku.listPriceParts[0]+'</span>';
				document.getElementById("offrPrBrick").innerHTML=sku.salesPriceParts[1]+ '<span class="jpCurrencySymbol">&nbsp;'+sku.salesPriceParts[0]+'</span>';
				
				document.getElementById("offrPrBrick").style.color="#009CFF";
			}else{
				document.getElementById("lstPrBrick").style.display="none";
				document.getElementById("offrPrBrick").innerHTML=sku.listPriceParts[1]+ '<span class="jpCurrencySymbol">&nbsp;'+sku.listPriceParts[0]+'</span>';
				document.getElementById("offrPrBrick").style.color="#494D56";
				$("#offrPrBrick span.jpCurrencySymbol").css("color","#494D56");
			}
		}else{
			if(sku.onSale){
				document.getElementById("lstPrBrick").style.display="block";
				document.getElementById("lstPrBrick").innerHTML=lstPrFormatted;
				document.getElementById("offrPrBrick").innerHTML=salePrFormatted;
				
				document.getElementById("offrPrBrick").style.color="#009CFF";
			}else{
				document.getElementById("offrPrBrick").innerHTML=lstPrFormatted;
				document.getElementById("offrPrBrick").style.color="#494D56";
				document.getElementById("lstPrBrick").style.display="none";
			}
		}
		updateVatLabel_v2();
	}	
}

function switchSkuById_software(skuId){
	var sku=getSkuById(skuId);

	if(sku){
		if(sku.deliveryMethod.toUpperCase()=="PHYSICAL"){
			if(document.getElementById('physicalSkuOpt')){
				document.getElementById('physicalSkuOpt').value=skuId;
				document.getElementById('physicalSkuOpt').checked=true;
			}	
		}else{
			if(document.getElementById('downloadSkuOpt')){
				document.getElementById('downloadSkuOpt').value=skuId;
				document.getElementById('downloadSkuOpt').checked=true;
			}	
		}
		switchSkuPrice_software(skuId);
	}
}

function iniDeliveryMethodRadio(softwareType){
	if(!softwareType)
		softwareType="FULL";
		
	var downloadSku=getSoftwareByTypeAndDeliveryMethod(softwareType.toUpperCase(),"DOWNLOAD",storeLangCode);
	var physicalSku=getSoftwareByTypeAndDeliveryMethod(softwareType.toUpperCase(),"PHYSICAL",storeLangCode);
	
	var dwnSkuExists=false;
	var phySkuExists=false;
	var singularSkuDM=false;  //only single delivery method exists ?
	
	if(downloadSku)
		dwnSkuExists=true;
	if(physicalSku)
		phySkuExists=true;
	
	if(dwnSkuExists==false && phySkuExists==false){
		// no available sku found
		return;
	}
	
	if(dwnSkuExists==false || phySkuExists==false)
		singularSkuDM=true;
	
	if(dwnSkuExists){
		if(document.getElementById('downloadSkuOpt')){
			document.getElementById('downloadSkuOpt').value=downloadSku.getId();
			document.getElementById('downloadSkuOpt').checked=true;
			if(singularSkuDM){
				document.getElementById('downloadSkuOpt').style.display="none";	
				document.getElementById('dwnInstLnkMulDM').style.display="none";
				document.getElementById('dwnInstLnkSinDM').style.display="block";
			}
		}	
	}else{
		if(document.getElementById('downloadSkuOptRow'))
			document.getElementById('downloadSkuOptRow').style.display="none";
	}

	if(phySkuExists){
		if(document.getElementById('physicalSkuOpt')){
			document.getElementById('physicalSkuOpt').value=physicalSku.getId();
			if(singularSkuDM){
				document.getElementById('physicalSkuOpt').style.display="none";	
				document.getElementById('spPcyLnkMulDM').style.display="none";
				document.getElementById('spPcyLnkSinDM').style.display="block";
			}
		}	
	}else{
		if(document.getElementById('physicalSkuOptRow'))
			document.getElementById('physicalSkuOptRow').style.display="none";
	}

	if(dwnSkuExists){
		switchSkuById_software(downloadSku.getId());
	}else{
		if(document.getElementById('physicalSkuOpt'))
			document.getElementById('physicalSkuOpt').checked=true;
		switchSkuById_software(physicalSku.getId());
	}
}

function initDeliveryMethodBtnIPM(softwareType){
	if(!softwareType)
		softwareType="FULL";
		
	var downloadSku=getSoftwareByTypeAndDeliveryMethod(softwareType.toUpperCase(),"DOWNLOAD",storeLangCode);
	var physicalSku=getSoftwareByTypeAndDeliveryMethod(softwareType.toUpperCase(),"PHYSICAL",storeLangCode);
	
	var dwnSkuExists=false;
	var phySkuExists=false;
	var singularSkuDM=false;  //only single delivery method exists ?
	
	if(downloadSku)
		dwnSkuExists=true;
	if(physicalSku)
		phySkuExists=true;
	
	if(dwnSkuExists==false && phySkuExists==false){
		// no available sku found
		return;
	}
	
	if(dwnSkuExists==false || phySkuExists==false)
		singularSkuDM=true;
	
	if(dwnSkuExists){ //if there is download sku
		if(document.getElementById('downloadSkuOpt')){
			document.getElementById('downloadSkuOpt').value=downloadSku.getId();
			document.getElementById('downloadSkuOpt').checked=true;
			document.getElementById('downloadSkuOpt').style.display="none";	
		}	
	}else{
		if(document.getElementById('activateNowBtnDiv'))
			document.getElementById('activateNowBtnDiv').style.display="none";
		if(document.getElementById('activateNowLnkSinDM'))
			document.getElementById('activateNowLnkSinDM').style.display="none";
	}

	if(phySkuExists){
		if(document.getElementById('physicalSkuOpt')){
			document.getElementById('physicalSkuOpt').value=physicalSku.getId();
			document.getElementById('physicalSkuOpt').style.display="none";	
		}	
	}else{
		if(document.getElementById('buyBoxBtnDiv'))
			document.getElementById('buyBoxBtnDiv').style.display="none";
		if(document.getElementById('spPcyLnkSinDM'))
			document.getElementById('spPcyLnkSinDM').style.display="none";
	}

	if(dwnSkuExists){
		switchSkuById_software(downloadSku.getId());
	}else{
		if(document.getElementById('physicalSkuOpt'))
			document.getElementById('physicalSkuOpt').checked=true;
		switchSkuById_software(physicalSku.getId());
	}
}
function switchSkuById_hardware(skuId){
	var sku=getSkuById(skuId);

	if(sku){
		document.getElementById("iAddToCartSkuId").value=skuId;
		switchSkuPrice_hardware(skuId);
	}
}

function switchSkuPrice_hardware(skuId){
	// the price tags are the same the software, reuse software's method
	switchSkuPrice_software(skuId);
}

function switchSkuById_license(skuId){
	var sku=getSkuById(skuId);

	if(sku){
		switchSkuPrice_license(sku);
	}
}

function switchSkuPrice_license(sku){
 	var priceWasElement = document.getElementById('productPriceWas');
	var priceOfferElement = document.getElementById('productPriceOffer');
	var unitProductPriceWas = document.getElementById('unitProductPriceWas');
	var unitProductPriceOffer = document.getElementById('unitProductPriceOffer');
	var prdPrcCurCde = document.getElementById('prdPrcCurCde');
	
	if (!sku) {
		if(priceWasElement && priceOfferElement){
			priceWasElement.style.display="none";
			priceOfferElement.style.display="none";	
		}

		if(unitProductPriceWas && unitProductPriceOffer){
			unitProductPriceWas.style.display="none";
			unitProductPriceOffer.style.display="none";
		} 
		return;
	} 
	
	currentSku=sku;
	
	if (!sku.isOnSale()) {
		// no discount
		if(priceWasElement && priceOfferElement){
			priceWasElement.style.display="none";
			priceOfferElement.style.display="block";
			priceOfferElement.innerHTML = sku.getFormattedListPrice();
	 		priceOfferElement.className="lcnOfrPrc";
	 		prdPrcCurCde.className="lcnCurCde";
		}

		
		if(unitProductPriceWas && unitProductPriceOffer){
			unitProductPriceWas.style.display="none";
			unitProductPriceOffer.style.display="block";
			unitProductPriceOffer.innerHTML = sku.getFormattedListPrice();
			unitProductPriceOffer.className="lcnUnitOfrPrc";
		}
 		
	} else {
		// discounted
		if(priceWasElement && priceOfferElement){
			priceWasElement.style.display="block";
			priceWasElement.innerHTML = sku.getFormattedListPrice();

	 		priceOfferElement.style.display="block";
	 		priceOfferElement.innerHTML = sku.getFormattedSalePrice();
	 		priceOfferElement.className="lcnOfrPrcPromo";	
	 		prdPrcCurCde.className="lcnCurCdePromo";
		}

		
		if(unitProductPriceWas && unitProductPriceOffer){
			unitProductPriceWas.style.display="block";
			unitProductPriceWas.innerHTML = sku.getFormattedListPrice();
			unitProductPriceOffer.style.display="block";
			unitProductPriceOffer.innerHTML = sku.getFormattedSalePrice();
			unitProductPriceOffer.className="lcnUnitOfrPrcPromo";
		}
		
		//double check
 		if(sku.getFormattedListPrice() == sku.getFormattedSalePrice()){
 			if(priceWasElement && priceOfferElement){
 				priceWasElement.style.display="none";
 				priceOfferElement.className="lcnOfrPrc";
 		 		prdPrcCurCde.className="lcnCurCde";
 			}

			
			if(unitProductPriceWas){
				unitProductPriceWas.style.display="none";
				unitProductPriceOffer.className="lcnUnitOfrPrc";
			}
 		}
	}
	updateDisplayName_v2();
	updateVatLabel_v2();
}

function updateDisplayName_v2(){
	var displayNameElement = document.getElementById('productDisplayName');
	
	if (!currentSku) {
		displayNameElement.style.display="none";
		return;
	} 
	
	displayNameElement.innerHTML = currentSku.getDisplayName();
}

function updateVatLabel_v2(){
	var vatLabelElement = document.getElementById('vatLabel');
	
	if (!currentSku) {
		if(vatLabelElement)
			vatLabelElement.style.display="none";
		return;
	} 
	
	if(vatLabelElement){
		if(currentSku.getVatLabelText()==""){
			vatLabelElement.innerHTML = "";
		}		
		else{
			vatLabelElement.innerHTML = currentSku.getVatLabelText();
		}	
	}	
}

function changeLicenseQuantity_v2(pId, quantity, maxQuantity,hasUpgradeSku,currentOrderId) {
	 
	if (!intValidate.validate(1,9999,document.getElementById("quantityErrorMessage"))) return;
	 
	if(parseInt(quantity) > parseInt(maxQuantity)){
		document.getElementById("softwareRowOverLimit").style.display = "";
		document.getElementById("lcPrcTalTr").style.display = "none";
		document.getElementById("unitPriceDiv").style.display = "none";
		document.getElementById("multiplySign").style.display = "none";
		document.getElementById("addToCartDisableBtn").style.display = "";
		document.getElementById("addToCartBtn").style.display = "none";
		document.getElementById("volumePricingLinkDiv").style.display = "none";
	}else{ 
 
		document.getElementById("softwareRowOverLimit").style.display = "none";
		document.getElementById("lcPrcTalTr").style.display = "";
		document.getElementById("unitPriceDiv").style.display = "";
		document.getElementById("multiplySign").style.display = "";
		document.getElementById("addToCartBtn").style.display = "";
		document.getElementById("addToCartDisableBtn").style.display = "none";
		document.getElementById("volumePricingLinkDiv").style.display = "";
		
		var queryStr="?skuId="+currentSku.getId()+"&pId="+pId+"&quantity="+quantity+"&currentOrderId="+currentOrderId;
  		getJson("../ajax/volumeLicensePrice.jsp"+queryStr,updateVolumeLicense,error);	
	}

}
 
function purchaseItem_license(){
	document.getElementById("iAddToCartSkuId").value=currentSku.getId();
 	if(document.getElementById("iAddToCartQuantity") && document.getElementById("volumeLicenseQuantity_quantity")){
 		document.getElementById("iAddToCartQuantity").value = document.getElementById("volumeLicenseQuantity_quantity").value;
 		if (!intValidate.validate(1,9999,document.getElementById("quantityErrorMessage"))) return;	  
	}

 }

function populateSupportProductPrices(){
	var curSkuId;
	var sku;
	
	for (var i=0; i<skus.length; i++) {
		curSkuId=skus[i].getId();
		sku=skus[i];
		
		currentSku=sku;
		
		if(document.getElementById("lstPrBrick_"+curSkuId)){
			var listPrice=sku.formattedListPrice;
			var salePrice=sku.formattedSalePrice;
			
			if(sku.onSale){
				document.getElementById("lstPrBrick_"+curSkuId).style.display="inline";
				document.getElementById("lstPrBrick_"+curSkuId).innerHTML=listPrice;
				document.getElementById("offrPrBrick_"+curSkuId).innerHTML=salePrice;
				document.getElementById("offrPrBrick_"+curSkuId).style.color="#009CFF";
			}else{
				document.getElementById("offrPrBrick_"+curSkuId).innerHTML=listPrice;
				document.getElementById("offrPrBrick_"+curSkuId).style.color="#494D56";
				document.getElementById("lstPrBrick_"+curSkuId).style.display="none";
			}		
		}
	}
	updateVatLabel_v2();
}

function switchPrdPgeTab(anchorName){

	if(anchorName){
		var pwrRvwAnk=$("ul.tabset li").filter(function(index){
			return $("a",this).attr("href")==anchorName;
		}).get(0);
		
		if(pwrRvwAnk){
			$("div.tab-content").hide();
			$("ul.tabset li").removeClass();
			
			$("ul.tabset li").filter(function(index){
				return $("a",this).attr("href")==anchorName;
			}).attr("class","active");
			
			$("div#"+anchorName).show();		
		}	
	}	
}
