// JavaScript Document

function selectRadioBtnById(name, elementId){
	radioObj = document.getElementsByName(name);

	if(!radioObj)
		return;
	
	var radioLength = radioObj.length;

	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].id == elementId) {
			radioObj[i].checked = true;
		}
	}
	
}

function setAfmetingId(afmetingId){
	 $("#afmetingId").val(afmetingId); 
	 
	 changePrice();
}

function checkFirstRadioBtn(){
	// uncheck all
	$('.treatmentRadio').removeAttr("checked");
	
	var q = 0;
	var foundNumber = 0;
	$('.treatmentDiv').each(
		function (){
			q++;
			if($(this).css('display') == 'block' && foundNumber == 0){
				foundNumber = q;				
			}
		}
	);
	
	//alert(foundNumber);

	$('div#somediv input:radio:nth('+(foundNumber-1)+')').attr("checked","checked");
	
	changePrice();
}

function changePrice(){
	
	var materiaalValue = $("input[name=materiaal]:checked").val(); 
	var houtbehandelingValue = $("input[name=houtbehandeling]:checked").val();
	var cementBlocksValue = $("input[name=cementBlocks]:checked").val(); 
	var numberOfProducts = $("#numberOfProducts").val(); 
	var articleId = $("#articleId").val(); 
	// afmeting
	var afmetingId = $("#afmetingId").val(); 
	
	/*if(materiaalValue == 4){ // if staal isset
		$("#treatment_9").hide(); // hide Geslepen / geborsteld
		$("#treatment_10").hide(); // hide Gestraald
		
		if(houtbehandelingValue == 9 || houtbehandelingValue == 10){
			checkFirstRadioBtn();
			return ''; // dirty fix to exit function
		}
		
	}else{
		$("#treatment_9").show(); // show Geslepen / geborsteld
		$("#treatment_10").show(); // show Gestraald
	}
	
	if(materiaalValue == 6 || materiaalValue == 15){ // if rvs isset
		$("#treatment_11").hide(); // hide Verzinkt
		$("#treatment_12").hide(); // hide Gecoat
		
		if(houtbehandelingValue == 11 || houtbehandelingValue == 12){
			checkFirstRadioBtn();
			return ''; // dirty fix to exit function
		}
	}else{
		$("#treatment_11").show(); // show Verzinkt
		$("#treatment_12").show(); // show Gecoat
	}*/
	if(materiaalValue == 4){ // if staal isset
		$("#treatment_9").hide(); // hide Geslepen / geborsteld
		$("#treatment_10").hide(); // hide Gestraald
		
		$("#treatment_11").show(); // show Verzinkt
		$("#treatment_12").show(); // show Gecoat
		
		if(houtbehandelingValue == 9 || houtbehandelingValue == 10){
			checkFirstRadioBtn();
			return ''; // dirty fix to exit function
		}
		
	}else if(materiaalValue == 6 || materiaalValue == 15){ // if rvs isset
		$("#treatment_9").show(); // hide Geslepen / geborsteld
		$("#treatment_10").show(); // hide Gestraald
		
		$("#treatment_11").hide(); // hide Verzinkt
		$("#treatment_12").hide(); // hide Gecoat
				
		if(houtbehandelingValue == 11 || houtbehandelingValue == 12){
			checkFirstRadioBtn();
			return ''; // dirty fix to exit function
		}
	}
	
	//alert(houtbehandelingValue);
	
	//ajax it
	$.ajax({		
		type: "GET",
		url: pagesPath+"product.ajax.php",	
		data: "action=changePrice&materiaal="+materiaalValue+"&houtbehandeling="+houtbehandelingValue+"&cementBlocks="+cementBlocksValue+"&numberOfProducts="+numberOfProducts+"&articleId="+articleId+"&afmetingId="+afmetingId,		
		success: function(msg){
			//alert(msg);
			returnArray = msg.split('|||');

			var currentPrice = returnArray[0];
			
			if(cementBlocksValue == 'Y'){ // if cement block is yes, substract 48 for cement block
				newPrice = currentPrice.replace(",", ".");
				currentPrice = newPrice - 48;
			}

			if(currentPrice == '0,00' || currentPrice == '0'){ // if no price hide amount: show error
				$("#priceAmount").hide();
				//set text for price error
				$("#priceError").html('Er is geen prijs gevonden.<br> Neem contact op met <a href="mailto:info@stiels-straatmeubilair.nl">Stiels Straatmeubilair</a>');
				$("#priceError").show();
			}else{
				$("#priceAmount").show(); // show pricing fields
				$("#priceError").hide(); // hide error
				// set total price + set price per product (for the math)
				$("#price").html(returnArray[0]);
				$("#pricePerProduct").val(returnArray[1]);
			}
		}
	});
}

function addProductToCart(productName, fileName, link){
	var materiaalValue = $("input[name=materiaal]:checked").val(); 
	var ralColor = $("input[name=ral]:checked").val(); 
	var houtbehandelingValue = $("input[name=houtbehandeling]:checked").val();
	var cementBlocksValue = $("input[name=cementBlocks]:checked").val(); 
	var numberOfProducts = $("#numberOfProducts").val(); 
	var articleId = $("#articleId").val();
	var price = $("#pricePerProduct").val();
	
	// afmeting
	var afmetingId = $("#afmetingId").val();
	if( materiaal && houtbehandelingValue == 12 && typeof(ralColor) == "undefined" ){
		alert('Selecteer een kleur');
	}else{
		if(numberOfProducts > 0){
						
			var productCartName = productName;
			var option_1;
			var option_1_color;
			var option_2;
			var option_3;
			var afmetingName;
			
			if(materiaal){
				option_1 = $("#input_hidden_"+materiaalValue).val();
				
			}		
				
			if(typeof(houtbehandelingValue) != "undefined"){
				option_2 = $("#input_hidden_"+houtbehandelingValue).val();
				if(houtbehandelingValue == 12 && typeof(ralColor) != "undefined" ){
					option_1_color = ralColor;
				}
			}

			if(cementBlocksValue == 'Y'){
				option_3 = 'met betonblok';
			}
			
			if(typeof(afmetingId) != "undefined"){
				afmetingName = $("#afmetingValue_"+afmetingId).val();
			}
			
			addToCart(articleId+'_'+materiaalValue+'_'+ralColor+'_'+houtbehandelingValue+'_'+cementBlocksValue+'_'+afmetingId, productCartName, numberOfProducts, price, fileName, link, option_1, option_1_color, option_2, option_3, afmetingName);
		}else{
			alert('Geen geldig aantal ingevoerd');
		}
	}
	
	
}



function emptyValue(elm, tekst){
	if(document.getElementById(elm).value==tekst){
		document.getElementById(elm).value="";
	}
}
function returnValue(elm, tekst){
	if(document.getElementById(elm).value==""){
		document.getElementById(elm).value=tekst;
	}
}

function popUp(link , width , height){
	window.open(link, 'venster', 'width=' + width + ',height=' + height + ',scrollbars=yes,toolbar=no,location=no,status=no,resize=no');
}



function prodDetailNavRollover(img){
	document.getElementById('img_detail').style.backgroundImage = 'url('+img+')';
}

function prodNavRollover(node,name,img){
	document.getElementById('pn_'+node).innerHTML = name;
	document.getElementById('img_'+node).style.backgroundImage = 'url('+img+')';
}

function showProductnav(elm, linkId){
	try{
		// hide all other productnavs
		navs = getObjectsByClassname('product_nav');
		for(x=0;x<navs.length;x++){
			navs[x].style.display = 'none';
		}
		// show the productnav
		document.getElementById(elm).style.display = 'block';
		links = getObjectsByClassname('activeMenuLink');
		for(x=0;x<links.length;x++){
			links[x].className = '';
		}
		document.getElementById(linkId).className = 'activeMenuLink';
		productNameSifr();
	}catch(e){}
}

function expandAll(startDiv) {
	var divObj = document.getElementById(startDiv);
	if (divObj == "object") {
		for (var i=0;i<divObj.childNodes.length;i++) {
			if (divObj.childNodes[i].nodeName == "DIV" && divObj.childNodes[i].id.substr(0,6) == "folder") {
				toggleFolder(divObj.childNodes[i].id, document.getElementById("image" + divObj.childNodes[i].id.substr(6)));
				expandAll(divObj.childNodes[i].id);
			}
		}
	}
}

function closeAll(startDiv) {
	expandAll(startDiv);
	var divObj = document.getElementById(startDiv);
	for (var i=0;i<divObj.childNodes.length;i++) {
		if (divObj.childNodes[i].nodeName == "DIV" && divObj.childNodes[i].id.substr(0,6) == "folder") {
			toggleFolder(divObj.childNodes[i].id, document.getElementById("image" + divObj.childNodes[i].id.substr(6)));
			expandAll(divObj.childNodes[i].id);
		}
	}
}

function expandItem(theID, startDiv) {
	var divObj = document.getElementById(startDiv);
	var found = false;
	for (var i=0;i<divObj.childNodes.length;i++) {
		if (divObj.childNodes[i].nodeName == "DIV" && divObj.childNodes[i].id.substr(0,6) == "folder") {
			found = expandItem(theID, divObj.childNodes[i].id);
			if (divObj.childNodes[i].id == "folder" + theID) {
				// current element found
				toggleFolder(divObj.childNodes[i].id, document.getElementById("image" + divObj.childNodes[i].id.substr(6)));
				return true;
			}
			if (found) {
				toggleFolder(divObj.childNodes[i].id, document.getElementById("image" + divObj.childNodes[i].id.substr(6)));
				return true;
			}
		}
	}
	return found;
}

function toggleFolder(id, imageNode) {
	var folder = document.getElementById(id);
	var l = imageNode.src.length;
	if (imageNode.src.substring(l-21,l)=="tree_folderclosed.png" || imageNode.src.substring(l-19,l)=="tree_folderopen.png") {
		imageNode = imageNode.previousSibling;
		l = imageNode.src.length;
	}
	if (folder == null) {
	} 
	else if (folder.style.display == "block")  {
		if (imageNode != null) {
			imageNode.nextSibling.src = imgPath+"sitemap/tree_folderclosed.png";
			if (imageNode.src.substring(l-14,l) == "tree_mnode.png") {
				imageNode.src = imgPath+"sitemap/tree_pnode.png";
			}
			else if (imageNode.src.substring(l-18,l) == "tree_mlastnode.png") {
				imageNode.src = imgPath+"sitemap/tree_plastnode.png";
			}
		}
		folder.style.display = "none";
	} 
	else {
		if (imageNode != null) {
			imageNode.nextSibling.src = imgPath+"sitemap/tree_folderopen.png";
			if (imageNode.src.substring(l-14,l) == "tree_pnode.png") {
				imageNode.src = imgPath+"sitemap/tree_mnode.png";
			}
			else if (imageNode.src.substring(l-18,l) == "tree_plastnode.png") {
				imageNode.src = imgPath+"sitemap/tree_mlastnode.png";
			}
		}
		folder.style.display = "block";
	}
	//updateRowColor();
}


function getObjectsByClassname(clsName) {
	var retVal = new Array();
	var elements = document.getElementsByTagName("*");
	
	for(var i = 0;i < elements.length;i++) {
		if(elements[i].className.indexOf(" ") >= 0) {
			classes = elements[i].className.split(" ");
			for(var j = 0;j < classes.length;j++){
				if(classes[j] == clsName) {
					retVal.push(elements[i]);
				}
			}
		}
		else if(elements[i].className == clsName) {
			retVal.push(elements[i]);
		}
	}
	return retVal;
}


function RemoveClassName(objElement, strClass) {
	// if there is a class
	if ( objElement.className ) {
	
	   // the classes are just a space separated list, so first get the list
	   var arrList = objElement.className.split(' ');
	
	   // get uppercase class for comparison purposes
	   var strClassUpper = strClass.toUpperCase();
	
	   // find all instances and remove them
	   for ( var i = 0; i < arrList.length; i++ )
	      {
	
	      // if class found
	      if ( arrList[i].toUpperCase() == strClassUpper )
	         {
	
	         // remove array item
	         arrList.splice(i, 1);
	
	         // decrement loop counter as we have adjusted the array's contents
	         i--;
	
	         }
	
	      }
	
	   // assign modified class name attribute
	   objElement.className = arrList.join(' ');
	
	   }
	// if there was no class
	// there is nothing to remove
}

function AddClassName(objElement, strClass, blnMayAlreadyExist) {
	
	// if there is a class
	if ( objElement.className )
	   {
	
	   // the classes are just a space separated list, so first get the list
	   var arrList = objElement.className.split(' ');
	
	   // if the new class name may already exist in list
	   if ( blnMayAlreadyExist )
	      {
	
	      // get uppercase class for comparison purposes
	      var strClassUpper = strClass.toUpperCase();
	
	      // find all instances and remove them
	      for ( var i = 0; i < arrList.length; i++ )
	         {
	
	         // if class found
	         if ( arrList[i].toUpperCase() == strClassUpper )
	            {
	
	            // remove array item
	            arrList.splice(i, 1);
	
	            // decrement loop counter as we have adjusted the array's contents
	            i--;
	
	            }
	
	         }
	
	      }
	
	   // add the new class to end of list
	   arrList[arrList.length] = strClass;
	
	   // add the new class to beginning of list
	   //arrList.splice(0, 0, strClass);
	   
	   // assign modified class name attribute
	   objElement.className = arrList.join(' ');
	
	   }
	// if there was no class
	else
	   {
	
	   // assign modified class name attribute      
	   objElement.className = strClass;
	
	   }
}

