
var cardcount = 0;


var isie = (document.all) ? true : false;

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	
	if ((version >= 5.5 && version < 7.0) && (document.body.filters)) 
	{
		for(var i=0; i<document.images.length; i++)
		{
			var img = document.images[i]
			var imgName = img.src.toUpperCase()
			
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			{

				img.style.display = 'block';
				
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<div " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></div>" 
				img.outerHTML = strNewHTML
				
				i = i-1
			}
		}
	}
	
	for (var i=0; i<document.images.length; i++)
	{
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		{
			img.style.display = 'block';
		}
	}
}

function setClassName (object, name)
{
	var names 	= object.className.split(" ");
	var classes = "";
	
	if (names.length > 0 && name == "")
	{
		classes = names[0];
	}
	
	if (name != "")
	{
		classes = names[0] + " " + names[0] + "_active";
	}
	
	object.className = classes;
}

function replaceField(field)
{ 
    var password 	= document.createElement("input"); 
    
    password.type 	= "password"; 
    password.name 	= field.name; 
    password.id		= field.name; 
    
    field.parentNode.replaceChild(password, field); 
}


function showHideElement(source, target)
{	
	if(document.getElementById(source).checked == 1)
	{
		document.getElementById(target).style.display = 'block';
	}
	else
	{
		document.getElementById(target).style.display = 'none';
	}
}

function changeQuantity(objectinstanceid, count, udcsizeitemid, udccoloritemid)
{
	new Ajax.Request('/_ctrl/shop/cart/add/'+objectinstanceid+'/'+count+'/'+ udcsizeitemid +'/'+ udccoloritemid +'', {
			onSuccess: function(transport) 
			{
				document.location.href='/_ctrl/shop/cart/list';
			}
		});
}

function SwapImage(element)
{
	this.active = element;
}

	SwapImage.prototype.swap = function(element, id, src)
	{
		if (this.active.id != element.id)
		{
			element.className = 'active';
			
			document.getElementById(id).src = src;
			
			this.active.className = '';
			this.active = element;
		}
	}


/*
Event.observe(window, 'load', function() 
{
    // when an element with the id of cart exists and a key has occured, the calculatenewPrice will be triggered.
	if($('cart') != null)
    {
    	Event.observe('cart', 'keyup', calculateNewPriceQuantity);
    }
}
);

function calculateNewPriceQuantity(event)
{
    var input = event.element();
    
    var updateknop = $('updatecartbtn');
    
    updateknop.value    = 'Updaten...';
    updateknop.disabled = true;
      
    var url = '/_ctrl/shop/cart/update/req/ajax';
   
    var AllOptions = 
    {
        method: 'get',
        parameters: { quantity: input.value, encodedid: input.id }, 
        onSuccess: function(oXHR, oJson)
        {
            updateknop.value = "Update winkelwagen";
            updateknop.disabled = false;
            
            updateCart(oXHR, oJson, input);
        },
        onFailure: function(oXHR, oJson)
        {
            alert('error helaas geen succes');
        }
    }   
    var myAjax = new Ajax.Request(url, AllOptions);
    
}

// when ajax request is succesfull, the retrieved data from the Json object needs to be placed inside the DOM structure
// this is where the updateCart function fits in.
function updateCart(oXHR, oJson, input)
{
    var cartobject = oXHR.responseText.evalJSON();
     
	var formatedid = input.id.split('_');
	var subtotalprice = $('subtot_'+formatedid[1]);
	var totalprice    = $('totalprice');
     	     
    subtotalprice.innerHTML    = '&euro; '+cartobject.subtotalprice;
    totalprice.innerHTML       = '<b>&euro; '+cartobject.totalprice+'</b>';
    input.value                = cartobject.quantity;  
     
    document.getElementById('quantity').innerHTML		= cartobject.quantity; 
    document.getElementById('totalamount').innerHTML	= '&euro; '+cartobject.totalprice+''; 
    
     if(cartobject.errorkey != null)
     {
        alert(cartobject.errormessage);
     }
     
     var totalamount = cartobject.totalprice.replace(',', '.') * 100;
     
   	 // todo: Ergens een Jason - config object aanmaken en hier aanroepen
   	 // Nu betekent 2500 de verzendkosten. Als het bedrag hoger dan 25 euro is, wordt de regel verzendkosten in de tabel gedisabled.
   	 // 265 is verzendkosten
   	 
   	 if (totalamount > 2500)
   	 {
     	document.getElementById('tr_1a').style.display = 'none';
     	document.getElementById('sendcostmessage').innerHTML = '(geen verzendkosten!)';
   	 }
   	 else
   	 {
   	 	
   	 	if (isie)
   	 	{
   	 		document.getElementById('tr_1a').style.display = 'block';
   	 	}
   	 	else
   	 	{
   	 		document.getElementById('tr_1a').style.display = 'table-row';
   	 	}
   	 	
   	 	document.getElementById('sendcostmessage').innerHTML = '(inclusief verzendkosten)';
   	 	
   	 	totalamount	= ((totalamount + 200)/100).toFixed(2);
   	 	totalamount = totalamount.toString().replace('.',',');
   	 	totalprice.innerHTML       = '<b>&euro; '+totalamount + '</b>';
   	 	document.getElementById('totalamount').innerHTML	= '&euro; '+totalamount; 
   	 }
   	 
     
}
*/
function updateCart(cartcollection)
{
	 var totalamount = cartcollection.jsonobj.totalcleanprice;
	 
	 // Als het bedrag hoger dan 25 euro is, wordt de regel verzendkosten in de tabel gedisabled.
	 
   	 if (cartcollection.jsonobj.shippingcosts == 0)
   	 {
     	document.getElementById('tr_1a').style.display = 'none';
     	document.getElementById('sendcostmessage').innerHTML = '(geen verzendkosten!)';
   	 }
   	 else
   	 {
   		shippingamount = (cartcollection.jsonobj.shippingcosts/100).toFixed(2);
   		shippingamount = shippingamount.toString().replace('.',',');
   		document.getElementById('shippingamount').innerHTML = '€ ' + shippingamount;
   		document.getElementById('shippingtotalamount').innerHTML = '€ ' + shippingamount;
   		
   	 	if (isie)
   	 	{
   	 		document.getElementById('tr_1a').style.display = 'block';
   	 	}
   	 	else
   	 	{
   	 		document.getElementById('tr_1a').style.display = 'table-row';
   	 	}
   	 	
   	 	document.getElementById('sendcostmessage').innerHTML = '(inclusief verzendkosten)';
   	 }
}

window.onload = function ()
{
	// png's voor IE6 aanpassen
	correctPNG();
	
}

counter = 0;
function SelectFile(fileids, profileid, direction)
{
	files = fileids.split(',');
	filelength = (files.length -1)
	
	// counter bepalen
	if 		(	direction == 'left')	{	counter = counter - 1;	}
	else	{	counter = counter + 1;	}
	if 		(	counter < 0)			{ counter = filelength;}
	if 		(	counter > filelength)	{ counter = 0;}
	
	// fileid ophalen
	new Ajax.Request('/ajax.php?type=encode&file_id='+ files[counter] +'&profile_id='+ profileid, {
		onSuccess: function(transport) 
		{
			document.getElementById('popup_image_file').src = '/image/'+transport.responseText;
		}
	});
}

function loadPopupInfo(id, articlenumber, format, type, price, description, path, image, controllerkey, fileids)
{
	
	var splitprice = price.split(',');
	var newprice = '<span class="price_left">' + splitprice[0] + '</span>,<span class="price_right">' + splitprice[1] + '</span>';
	files = fileids.split(',');
	filelength = (files.length -1)
	
	// Bij kaarten halen we de eerste afbeelding voor de popup er handmatig af.
	if (filelength > 1 && controllerkey == 'card')
	{
		fileids = fileids.slice(4);
		
	}	
	
	document.getElementById('popup_articlenumber').innerHTML = articlenumber;
	document.getElementById('popup_price').innerHTML = newprice;
	document.getElementById('popup_description').innerHTML = description;
	document.getElementById('popup_link').href = path + '/_ctrl/shop/cart/add/'+id+'/1';
	document.getElementById('popup_image_file').src = image;
	

	if (filelength > 1)
	{
		document.getElementById('img_prev').style.visibility = 'visible';
		document.getElementById('img_next').style.visibility = 'visible';
		document.getElementById('img_prev').href = 'javascript:SelectFile("'+fileids+'", "25", "left")';
		document.getElementById('img_next').href = 'javascript:SelectFile("'+fileids+'", "25", "right")';
	}
	else
	{
		document.getElementById('img_prev').style.visibility = 'hidden';
		document.getElementById('img_next').style.visibility = 'hidden';
	}
	
	
	
	if (controllerkey == 'card')
	{
		document.getElementById('popup_format').innerHTML = format;
		//document.getElementById('popup_type').innerHTML = type;
	}
	else if (controllerkey == 'canvas')
	{
		document.getElementById('popup_format').innerHTML = format;
		//document.getElementById('popup_type').innerHTML = type;
		document.getElementById('popup_binnenzijderow').style.display = 'none';
		document.getElementById('popup_enveloprow').style.display = 'none';
	}
	else
	{
		document.getElementById('popup_formatrow').style.display = 'none';
		document.getElementById('popup_typerow').style.display = 'none';
		document.getElementById('popup_binnenzijderow').style.display = 'none';
		document.getElementById('popup_enveloprow').style.display = 'none';
	}
	
	if (controllerkey == 'calender')
	{
		document.getElementById('popup_format').innerHTML = format;
		document.getElementById('popup_formatrow').style.display = 'table-cell';
	}

}

function formHandler ()
{
	
}

	formHandler.prototype.Send = function (formname, method, action)
	{
		document.forms[formname].method = method;
		document.forms[formname].action = action;
		document.forms[formname].submit();
	}
	
	var formHandler = new formHandler();
	

// Debug venster voor het uitlezen van een JS Object

function var_dump(obj, recursive)

{           

    //var recursive = recursive||true;

    var s = var_dump_noprint(obj, 0, recursive);

    s = s.replace(/</g, '&lt;');

    s = s.replace(/>/g, '&gt;');

    debugwin = window.open('', 'debugwin', 'left=20,top=20,width=400,height=800,toolbar=0,location=0,status=0,scrollbars=1,resizable=1');

    debugwin.document.write('<html><body style="cursor:pointer;" onClick="self.close()"><pre>' + s + '</pre></body></html>');

    debugwin.document.close();

}

 

/**

* voor debuggen, wordt gebruikt door var_dump hierboven

* ook los te gebruiken: alert(var_dump_noprint(obj));

*/

function var_dump_noprint(obj, indent_level, recursive)

{

    var LINEFEED = '\n';      

    var INDENT   = '\t';

    var indent_level = indent_level||0;

    var indent_str = '';

    var j;

    for(j=0; j<indent_level; j++) {

                indent_str += INDENT;

    }

    var s = '';

    if(typeof obj == 'undefined' || obj==null) {

                s += 'undefined' + LINEFEED;

    } else if(typeof obj == 'array' || typeof obj == 'object') {

                s += typeof obj + ' (' + LINEFEED;

                if(recursive || indent_level==0) {

                           var i;

                           for(i in obj) {

                                       s += indent_str + INDENT + '[' + i + '] => ' /*+ INDENT*/ + var_dump_noprint(obj[i], (indent_level+1), recursive);

                           }

                }

                s += indent_str + ')' + LINEFEED;

    } else if(typeof obj == 'function') {

                s += 'function' + LINEFEED;

    } else {

                s += '(' + typeof obj + ') ' /*+ INDENT*/ + obj + LINEFEED;

    }

    return s;

}

function addCard(nr, totalcard)
{
	
	nr = Number(cardcount);
	
	var html = 
		Builder.node
			('tr', {},
					[
					 
					 Builder.node ('td', {style:'width:140px'}, 
							 [
							  	
							  ]
					 ),
					 Builder.node ('td', {style:'width:115px'}, 
							 [
							  	Builder.node('input', { id: 'artcode_'+nr, className:'artcode', type: 'text', name: 'artcode[]'})
							  ]
					 ),
					 Builder.node ('td', {style:'width:45px'}, 
							 [
							  	Builder.node('input', {id: 'cardcount_'+nr ,className:'aantal', type: 'text', name: 'cardcount[]'})
							  ]
					 ),
					 Builder.node ('td', {}, 
							 [
							  
							  ]
					 ),
					 
					 ]
			);
	
	var desc = $('cardcol').descendants();
	
	desc[0].appendChild(html);
	
	$('artcode_'+nr).className = 'artcode';
	$('cardcount_'+nr).className = 'aantal';
	
	cardcount++;
}
Event.observe(window, 'load', function() 
{
	nav();
});
//Hier worden de navigatie items aangeroepen
function nav()
{
	var Navigators 		= new Array();
	newNavigator = new NavigatorCollection();
	newNavigator.getNavigator({
		'id'				:	"topmenu",
		'submenu'			:	"downblind",
		'activatesubmenu'	:	"hover",
		'effect'			:	"fade",
		'activeopen'		:	false
	});
}
