// JavaScript Document

// shoppingCart - Natufit 

var deleteText = "Löschen";
var shopCheckboxId = "PID";
var shopDivId = "ic_";
var shopCheckboxPrefix = "";
var conditionsDivId = "conditions_";
var shopURL = "/Default.aspx?TabId=100";
var productPrice = 3.90;
var priceSecondProduct = 1;
	

function quantity(operator) 
{
	if (operator == "add")
	{
		operatorValue = 1;
	}
	else if  (operator == "remove")
	{
		operatorValue = -1;
	}
	
  var subject1 = document.getElementById("PID1");
  var subject2 = document.getElementById("PID2");
  
  //subject 1 must always be equal to subject 2; therefore only calculating with value 1
  var newQty = parseFloat(subject1.value) + operatorValue;
  
  if (newQty == 1 || newQty == 2)
  {
        subject1.value = newQty.toString();
        subject2.value = newQty.toString();
        document.getElementById("PID1_Quantity").innerHTML = newQty;
        document.getElementById("PID2_Quantity").innerHTML = newQty;
        document.getElementById("PID1_ConditionsQuantity").innerHTML = newQty;        
        document.getElementById("PID2_ConditionsQuantity").innerHTML = newQty;
  } 
  
  // Calculate again
  calculateTotal();
}

function calculateTotal() 
{	
    if (document.getElementById("natufitWinkelwagen")) {
	var shoppingCart = document.getElementById("natufitWinkelwagen");
	var shoppingCartTotal = document.getElementById("natufitWinkelwagenTotaal");
	var tempCount = 0; // reset 
	var tempConditionsCount = 0; // reset
	var shopCheckboxPrefix = "";
	var productQuantity = "1"; // reset
	var nrOfProducts = 0; // reset
	var productPriceTotalConditions // reset
	var condtionsDiscountSecondProduct = 0.5; 

		for (j=0; j<2; j++) 
		{		
			//set quantity after postform
			document.getElementById(shopCheckboxId + (j+1) + "_Quantity").innerHTML = document.getElementById(shopCheckboxId + (j+1)).value;
			document.getElementById(shopCheckboxId + (j+1) + "_ConditionsQuantity").innerHTML = document.getElementById(shopCheckboxId + (j+1)).value;
            //get quantity and convert tot int
			productQuantity = parseFloat(document.getElementById(shopCheckboxId + (j+1) + "_Quantity").innerHTML);
			productPriceConditions = parseFloat(document.getElementById(shopCheckboxId + (j+1) + "_Price").value);
			
			//second product 50% discount
			if (productQuantity == 2)
			{
			    productPriceTotal = roundPrice(productPrice + priceSecondProduct);
			    productPriceTotalConditions = roundPrice(productPriceConditions + (productPriceConditions * condtionsDiscountSecondProduct));
			   document.getElementById("cartDisabler").style.top = "0px"; 
			   document.getElementById("rbQuantity2").checked = true; 
			}
			else
			{
			    productPriceTotal = productPrice;
			    productPriceTotalConditions = productPriceConditions; 
			   document.getElementById("cartDisabler").style.top = "23px";  
			   document.getElementById("rbQuantity1").checked = true; 
			}

			
	        document.getElementById("cartDisabler").style.display = "block"; 
			document.getElementById(shopCheckboxId + (j+1) + "_ShowPrice").innerHTML =  showPrice(productPriceTotal);
			document.getElementById(shopCheckboxId + (j+1) + "_ConditionsPrice").innerHTML =  showPrice(productPriceTotalConditions);
						
			if (parseFloat(document.getElementById(shopCheckboxId + (j+1)).value) >= 1) 
			{ 	
				document.getElementById(conditionsDivId + shopCheckboxId + (j+1)).style.display = "block";
				tempCount = productPriceTotal;
				nrOfProducts = nrOfProducts + productQuantity;
				
				//set price for conditions
			    tempConditionsCount += productPriceTotalConditions
			} 
			
			else 
			{
				document.getElementById(conditionsDivId + shopCheckboxId + (j+1)).style.display = "block";
			}
			
		}

    	
	    if(document.getElementById("dnn_WebShopForm_tb_Forderung").value == "1")
	    {
    	    var conditionsDiscountMachtiging = 0.05;
	    }
	    else
	    {
	        var conditionsDiscountMachtiging = 0;
	    }
	    
        document.getElementById("TotalPrice").value = showPrice(tempCount);
        
        
        // set values conditions
        
	    document.getElementById("conditions_subtotal").innerHTML = showPrice(tempConditionsCount);
	    document.getElementById("conditions_korting").innerHTML = showPrice(tempConditionsCount*conditionsDiscountMachtiging);
  	    document.getElementById("conditions_korting_percentage").innerHTML = "-&nbsp;&nbsp;<a href='/OverlayKortingsstructuur.aspx' onclick='icLightBox(this.href); return false' title='Mehr Informationen zum Rabatt'><img src='/portals/0/Assets/iconInfo.gif' border='0'></a>";
	    document.getElementById("conditions_total").innerHTML = showPrice(roundPrice(tempConditionsCount) - roundPrice(tempConditionsCount*conditionsDiscountMachtiging)) ; 

    }
}

function roundPrice(priceInt)
{
    return Math.round(priceInt*100)/100;
}


function showPrice(priceInt)
{
    // convert to price
    var strPrice = roundPrice(priceInt);
    strPrice = strPrice.toString().replace(".", ",");	
    
    if (strPrice.length - strPrice.indexOf(",") == 2)
    {
	    if (strPrice.indexOf(",") > 0)
	    {
		    strPrice += "0";
	    }
    }
    
    if (strPrice.indexOf(",") < 0)
    {
	    strPrice += ",00";
    }
    
    return unescape("&euro; ") + strPrice;
}


function getArgsURL() {
    // get information from URL
    var args = new Object();
    var query = location.search.substring(1);     // Get query string
    
   //search in querystring for Omega AND Multi definition. Minimal one of both is required 
    if (query.indexOf("PID1=")==-1)
    {   
         query+="&PID1=1";
    }
    if (query.indexOf("PID2=")==-1)
    {   
         query+="&PID2=1";
    }
  
    var pairs = query.split("&");                 // Split at ampersand
    search_array = query.split("&");
    
    for (i=0; i < pairs.length; i++)
    {
        var pos = pairs[i].indexOf('=');          // Look for "name=value"
        if (pos == -1) continue;                  // If not found, skip
        var argname = pairs[i].substring(0,pos);  // Extract the name
        var value = pairs[i].substring(pos+1);    // Extract the value        
    
        // In Germany only multi and omega (PID2 en PID1) > therefore check so no other products can be added
        if (argname == "PID1" || argname == "PID2")
        {
            args[argname] = unescape(value);          // Store as a decoded value	

            if (argname.indexOf(shopCheckboxId) != -1) 
            {
                if (args[argname] == "")
				    {
					    document.getElementById("PID1").value = "1";    // set the value into checkbox
					    document.getElementById("PID2").value = "1";    // set the value into checkbox
				    }
				    else
				    {
				 	    document.getElementById("PID1").value = args[argname];
					    document.getElementById("PID2").value = args[argname];
				    }
    				
                document.getElementById("PID1").checked = true; // set checkbox to checked
               document.getElementById("PID2").checked = true; // set checkbox to checked 
            }
        }
    }
}


function getArgs()
{
    if (document.getElementById("dnn_WebShopForm_tbIsPosted")) 
    {
         // this is 'stap 2'
        if (document.getElementById("dnn_WebShopForm_tbIsPosted").value == "0") 
        {
            getArgsURL();
        } 
    } else {    
        // not 'stap 2'
        getArgsURL();
    }
}


function shoppingCartLinkPaymentMethod(pm) 
{	
	
	var linkPaymentMethodQueryString = "";
	var arrayCheckboxes = document.getElementsByTagName("input");
	if (arrayCheckboxes.length != 0) {
		for (i=0; i < arrayCheckboxes.length; i++)
		{
			if(arrayCheckboxes[i].type == "checkbox") 
			{
			    if (arrayCheckboxes[i].id.indexOf(shopCheckboxId) != -1) 
			    {
				    if(arrayCheckboxes[i].checked == true)
				    {				       
				        // substract prefix from checkbox id's 
					    linkPaymentMethodQueryString += "&" + arrayCheckboxes[i].id.substring(shopCheckboxPrefix.length) + "=" + arrayCheckboxes[i].value;	
				    }
				}				
			}
		}
	}
	
	if(document.getElementById("dnn_WebShopForm_hf_BatchID").value != "")
	{
		linkPaymentMethodQueryString += "&batchid=" + document.getElementById("dnn_WebShopForm_hf_BatchID").value;
	}
	
	top.document.location.href = "/Probepaket.aspx?forderung=" + pm + linkPaymentMethodQueryString;
}

function submitFormAddFunction() 
{	
	// toevoegen knop op pagina 'toevoegen'
	var linkAddQueryString = "";
	var arrayCheckboxes = document.getElementsByTagName("input");
	if (arrayCheckboxes.length != 0) 
	{
		for (i=0; i < arrayCheckboxes.length; i++)
		{
			if(arrayCheckboxes[i].type == "checkbox") 
			{
				
				var parentSubject = top.document.getElementById(window.parent.shopCheckboxPrefix + arrayCheckboxes[i].id);
				
				if(arrayCheckboxes[i].checked == true)
				{
				    if (arrayCheckboxes[i].id.indexOf(shopCheckboxId) != -1) 
					 {
						parentSubject.checked = true;
						if (parentSubject.value == "0")
						{
							parentSubject.value = "1";
						}
					}
				} 
				else 
				{
				    if (arrayCheckboxes[i].id.indexOf(shopCheckboxId) != -1) 
					 {
					    parentSubject.checked = false;
						 parentSubject.value = "0";
					}
				}
			}
		}
	}
	
	icLightBox("hide");
	window.parent.calculateTotal();
}


function submitFormFunction() 
{	
	// check checkboxes stap 1 + maak querysting + alerts
	var shopQueryString = "";
	var tmpCheckedCount = 0;
	var arrayCheckboxes = document.getElementsByTagName("input");
	if (arrayCheckboxes.length != 0) {
		for (i=0; i < arrayCheckboxes.length; i++)
		{
			if(arrayCheckboxes[i].type == "checkbox") 
			{
			    if (arrayCheckboxes[i].id.indexOf(shopCheckboxId) != -1) 
			    {
				    if(arrayCheckboxes[i].checked == true)
				    {   
				        tmpCheckedCount ++;
				        if (parseFloat(arrayCheckboxes[i].value) == 0) 
				        {					   
					        tmpValue = 1;
					    } else {
					        tmpValue = arrayCheckboxes[i].value;
					    }
					    
					    shopQueryString += (shopQueryString == "") ? ("&" + arrayCheckboxes[i].name + "=" + tmpValue) : ("&" + arrayCheckboxes[i].name + "=" + tmpValue);
					    
				    }
				}
			}
		}
	}
	if (tmpCheckedCount == 0) {
		alert(headingText0);
	} else {
		top.document.location.href = shopURL + shopQueryString;
	}
}


function setHeadingText() {
	// set headingtext 
	var tmpCheckedCount = 0;
	headingText = document.getElementById("instructionText");
	nextButton = document.getElementById("nextBtn");
	nextButton2 = document.getElementById("nextBtn2");
	var arrayCheckboxes = document.getElementsByTagName("input");
	if (arrayCheckboxes.length != 0) {
		for (i=0; i < arrayCheckboxes.length; i++)
		{
			if(arrayCheckboxes[i].type == "checkbox") 
			{
			    if (arrayCheckboxes[i].id.indexOf(shopCheckboxId) != -1) 
			    {
				    var parent1 = arrayCheckboxes[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
			        var parent2 = arrayCheckboxes[i].parentNode;			        
				    if(arrayCheckboxes[i].checked == true)
				    {
				        parent1.style.zoom = "1";
			            parent2.style.zoom = "1";
				        parent1.style.backgroundColor = "#FFFFFF";
			            parent2.style.backgroundColor = "#F0F0EF";
			            parent1.style.filter = "alpha(opacity=40)";
                        parent1.style.opacity = "0.4";
                        parent2.style.filter = "alpha(opacity=40)";
                        parent2.style.opacity = "";
				    	tmpCheckedCount++;
				    } else {
				        parent1.style.backgroundColor = "";
			            parent2.style.backgroundColor = "";
				        parent1.style.filter = "";
				        parent1.style.opacity = "";
				        parent2.style.filter = "";
				        parent2.style.opacity = "";
				    }
				}
			}
		}
	}	
	if (tmpCheckedCount == 0) {
		nextButton.className = "nextBtnDisabled";
		nextButton2.className = "nextBtnDisabled";
		headingText.innerHTML = headingText0;
	} else {
		nextButton.className = "nextBtn";
		nextButton2.className = "nextBtn";
		headingText.innerHTML = headingTextMore;
	}
}


// Add initFunction to window.onload
if (window.addEventListener) 
{
    window.addEventListener("load", initFunction, false);
} 
else if (window.attachEvent) 
{
	window.attachEvent("onload", initFunction);
}
