var number_of_surfaces=0;
var current_index=0;
var finisaje=false;

function addSurface() 
{
	var table = document.getElementById('tabel_calcul');
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	row.id = 'surface'+current_index;
	var cell1 = row.insertCell(0);
	var element1 = document.createElement("p");
	element1.align="left";
	if (number_of_surfaces)
		element1.innerHTML = 'Suprafata :';
	else
		element1.innerHTML = 'Suprafata* :';
	cell1.appendChild(element1);		
	var cell2 = row.insertCell(1);
	
	var element2 = document.createElement("input");
	element2.type="text";
	element2.size="9"
	element2.name="surface"+current_index+"_width";
	element2.id="surface"+current_index+"_width";
	element2.setAttribute('value',"0");
	cell2.appendChild(element2);
	cell2.innerHTML+='&nbsp;m';
	

	var cell3 = row.insertCell(2);
	var element3 = document.createElement("p");
	element3.innerHTML = "X";
	element3.align="center";
	element3.style.textAlign='center';
	cell3.appendChild(element3);
	
	var cell4 = row.insertCell(3);
	var element4 = document.createElement("input");			
	element4.type="text";			
	element4.size="9"
	element4.name="surface"+current_index+"_height";
	element4.id="surface"+current_index+"_height";			
	cell4.appendChild(element4);
	element4.setAttribute('value',"0");
	cell4.innerHTML+='&nbsp;m';
	
	var cell5 = row.insertCell(4);
	var element5 = document.createElement("img");	
	element5.style.border = 'none';
	element5.style.background = 'none';
	element5.style.cursor = 'pointer';
	element5.style.outline = 'none';
	element5.style.display='block';
	element5.style.float='right';
	element5.style.width = '12px';
	element5.style.height = '12px';
	element5.style.marginTop='2px';
	element5.style.padding='0px';
	element5.align = "right";
	var suffix='';
	if (ie6)
		suffix = '-ie';
	if (number_of_surfaces)
	{
		element5.src = 'images/delete-row'+suffix+'.png';
		element5.alt='Sterge suprafata';
		element5.title='Sterge suprafata';
	}
	else
	{
		element5.src = 'images/add-row'+suffix+'.png';
		element5.alt='Adauga suprafata';
		element5.title = 'Adauga suprafata';
	}
	element5.align="right";
	element5.id="delete_"+current_index;
	cell5.appendChild(element5);
	if (number_of_surfaces)
	{	
		element5.onclick= function (e){ removeSurface(element5.id.substr(7)); } ;
	}
	else
	{				
		element5.onclick = function (e) {addSurface(); };		
	}	
	number_of_surfaces++;
	current_index++;

}

function removeSurface(id) 
{
	try 
	{
		var table = document.getElementById('tabel_calcul');
		tr = document.getElementById('surface'+id);
		while (tr.hasChildNodes()) 
		{
			tr.removeChild( tr.lastChild );
		}
		tr.parentNode.removeChild( tr );
		number_of_surfaces--;
		
	}catch(e) {
		alert(e);
	}
}

function doMath()
{
	var suprafata = 0;
	var wtf;//width text field
	var htf;//height text field
	var aux=0;
	for (i=0;i<current_index;i++)
	{
		wtf = document.getElementById("surface"+i+"_width");
		if (wtf==undefined || wtf.value=='')
			continue;
		htf = document.getElementById("surface"+i+"_height");
		if (htf==undefined || htf.value=='')
			continue;
		aux = parseInt(wtf.value) * parseInt(htf.value);
		if (isNaN(aux) || aux<0)
			aux=0;
		suprafata+=aux;
	}
	if (finisaje)
		suprafata = Math.round(Number(105/100)*suprafata);
	document.getElementById('rezultat_calcul').value = suprafata;
}
		
function finishes(elm)
{
	if (elm.checked)
	{
		finisaje=true;
		if (!isNaN(document.getElementById('rezultat_calcul').value) && document.getElementById('rezultat_calcul').value!=0)//deci tre sa facem un update
		{
			val = parseInt(document.getElementById('rezultat_calcul').value);
			val = Math.round(105/100 * val);
			document.getElementById('rezultat_calcul').value = val;
		}
	}
	else
	{
		finisaje=false;
		if (!isNaN(document.getElementById('rezultat_calcul').value) && document.getElementById('rezultat_calcul').value!=0)//deci tre sa facem un update
		{
			val = parseInt(document.getElementById('rezultat_calcul').value);
			val = Math.round(Number(100/105)*val);
			document.getElementById('rezultat_calcul').value = val;
		}
	}
}

//----------------------------------------------- Contact page ------------------------------------------------//

function doAwayDate(obj)
{
	if (obj.value=='ZZ.LL.AAAA')
		obj.value='';
}
function validate(form)
{
	if (form=='cerere')
	{
		if (!document.getElementById(form+'_nume').value.length)
		{alert('Campul nume trebuie completat');return false;}
		
		if (!document.getElementById(form+'_prenume').value.length)
		{alert('Campul prenume trebuie completat');return false;}
		
		if (!document.getElementById(form+'_email').value.length)
		{alert('Campul e-mail trebuie completat');return false;}
		
		if (!document.getElementById(form+'_info').value.length)
		{alert('Trebuie sa specificati despre ce doriti informatii');return false;}
	
	}
	else
	{
		if (!document.getElementById(form+'_nume').value.length)
		{alert('Campul nume trebuie completat');return false;}
		
		if (!document.getElementById(form+'_prenume').value.length)
		{alert('Campul prenume trebuie completat');return false;}
		
		if (!document.getElementById(form+'_email').value.length)
		{alert('Campul e-mail trebuie completat');return false;}
		
		if (!document.getElementById(form+'_adresa').value.length)
		{alert('Campul adresa trebuie completat');return false;}
		
		if (!document.getElementById(form+'_cod_postal').value.length)
		{alert('Campul cod postal trebuie completat');return false;}
		
		if (!document.getElementById(form+'_livrare').value.length)
		{alert('Data livrarii trebuie completata');return false;}
		
		if (!document.getElementById(form+'_montare').value.length)
		{alert('Data montarii trebuie completata');return false;}
		
		if (!parseInt(document.getElementById(form+'_suprafata').value))
		{alert('Suprafata necesara trebuie specificata');return false;}
	}
	
	return true;
}

