// JavaScript Document
function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function ReadForm (theForm) 
{
	var elems = theForm.elements;
	var str = 'Form : ' + theForm.name + '\n';
	
	fieldTotal=document.getElementById('frmTotal');
	fieldTotalPdv=document.getElementById('frmTotalPdv');
	fieldTotall=document.getElementById('frmTotall');
	
	total=0;
	arrMounts=new Array();
	for (var ix=0; ix < elems.length; ix++) 
	{
		var elem = elems[ix]
		if(ix%2!=0)
		{		
			//total=parseInt(total) + parseInt(elem.value);
			total=total + parseFloat(elem.value);
			str +='\n' + parseFloat(elem.value);
		}
	}
	total=parseFloat(total);
		
	
	
	total_pdv=total*0.25;
	
	fulltotal=total+total_pdv;
	total=number_format(total, 2, ",", "." );
	total_pdv=number_format(total_pdv, 2, ",", "." );
	fulltotal=number_format(fulltotal, 2, ",", "." );	
	
	fieldTotal.innerHTML = total;
	fieldTotalPdv.innerHTML = total_pdv;
	fieldTotall.innerHTML = fulltotal;
}

function changeItem(folderpath, obj, objmount, id, frmmount)
{
	var defQuant=-1;
	
 	curobj=document.getElementById(obj);	
	var code=curobj.value.charCodeAt(0);
	
	if(curobj.value.length<1)
	{
		curobj.value=1;
	}
	if(code<49 || code>57)
	{
		curobj.value=1;
	}
	curval=parseInt(curobj.value, 10);
	
	curmount=document.getElementById(objmount);
	curfrmmount=document.getElementById(frmmount);
	retForm(id);
}


function increaseItem(folderpath, obj, objmount, id, frmmount)
{
 	var minval=1;
	var defQuant=-1;
	curobj=document.getElementById(obj);
	curval=parseInt(curobj.value, 10);
	
	curmount=document.getElementById(objmount);
	curfrmmount=document.getElementById(frmmount);
	
	if(curval>=minval)
	{
		curval++;
	}
	if(curval<minval)
	{
		//return;
	}
	
	retForm(id);
}

function decreaseItem(folderpath, obj, objmount, id, frmmount)
{
 	var minval=1;
	curobj=document.getElementById(obj);
	curmount=document.getElementById(objmount);
	curval=parseInt(curobj.value, 10);
	curfrmmount=document.getElementById(frmmount);
	if(curval>=minval)
	{
		curval--;
	}
	if(curval<minval)
	{
		return;
	}	
	retForm(id);
}

// executed automatically when a message is received from the server
function retForm(z) 
{
	
	// update the client display using the data received from the server
	
	 curobj.value=curval;
	 
	 mountclear=curval*z;
	 mount1=number_format(mountclear, 2, ",", "." );
	 
	 curmount.innerHTML = mount1;
	
	 curfrmmount.value = mountclear;

	
	 ReadForm(document.forms['frmShopBasket']);
}
