// Checks if a field is empty, and if so, resets the input's content to the default value
function checkField( obj, value )
{
	// Match any amount of white spaces
	if( obj.value == "" )
	{
		obj.value = value;
	}
	
	return false;
}

// Clears an input's content
function clearField( obj, value )
{
	if( obj.value == value )
	{
		obj.value = "";
	}
	
	return false;
}

function checkForm(form, caz)
{
	switch (caz)
	{
		case "personalizare":
			cant = form.cant.value;
			if (form.produs.value == 'ce') ce = true; else ce = false;
			corect = true;
			for (i=0;i<cant;i++)
			{
				if (ce)
				{
					if (obiect('numedest'+i).value == '') 
					{
						obiect('numedest'+i).style.backgroundColor = '#F9E5E5';
						corect = false;
					}
					else
					{
						obiect('numedest'+i).style.backgroundColor = '#FFFFFF';
					}
					
					if (obiect('email1dest'+i).value == '' || obiect('email2dest'+i).value == '' || obiect('email1dest'+i).value != obiect('email2dest'+i).value || !validEmail(obiect('email1dest'+i).value)) 
					{
						obiect('email1dest'+i).style.backgroundColor = '#F9E5E5'; 
						obiect('email2dest'+i).style.backgroundColor = '#F9E5E5'; 
						corect = false;
					}
					else
					{
						obiect('email1dest'+i).style.backgroundColor = '#FFFFFF'; 
						obiect('email2dest'+i).style.backgroundColor = '#FFFFFF'; 
					}
				}
				if (obiect('titlu'+i).value == '' || obiect('titlu'+i).length > 50) 
				{
					obiect('titlu'+i).style.backgroundColor = '#F9E5E5'; 
					corect = false;
				}
				else
				{
					obiect('titlu'+i).style.backgroundColor = '#FFFFFF'; 
				}
				
				if (obiect('mesaj'+i).value == '' || obiect('mesaj'+i).length > 300) 
				{
					obiect('mesaj'+i).style.backgroundColor = '#F9E5E5'; 
					corect = false;
				}
				else
				{
					obiect('mesaj'+i).style.backgroundColor = '#FFFFFF'; 
				}
				
				if (obiect('incheiere'+i).value == '' || obiect('incheiere'+i).length > 50) 
				{
					obiect('incheiere'+i).style.backgroundColor = '#F9E5E5'; 
					corect = false;
				}
				else
				{
					obiect('incheiere'+i).style.backgroundColor = '#FFFFFF'; 
				}
			}
			if (corect) return true;
			else
			{
				alert('Va rugam sa completati corect toate campurile marcate!');
				return false;
			}
			break;
	}
}

function confirmDelete(caz, link)
{
	switch (caz)
	{
		case 'produs':
			var sterge = confirm('Sunteti sigur ca vreti sa renuntati la acest produs?');
			if (sterge) window.location = link;
			break;
		case 'stergePersonalizare':
			var sterge = confirm('Personalizarea certificatelor VA FI STEARSA si veti fi transferat in cosul de cumparaturi.\r\nContinuati?');
			if (sterge) window.location = link;
			break;
		case 'anuleazaPersonalizare':
			var sterge = confirm('Personalizarea certificatelor nu va fi afectata si veti fi transferat in cosul de cumparaturi.\r\nContinuati?');
			if (sterge) window.location = link;
			break;
	}
}

function closeWindow(id)
{
	if (document.getElementById(id)) document.getElementById(id).style.display = 'none';
}

function viewBox(bid, iid)
{
	if (iid.checked) document.getElementById(bid).style.display = 'block';
	else  document.getElementById(bid).style.display = 'none';
}

function showHide(divID) 
{
	current = obiect(divID).style.display;
	if (current == 'none') obiect(divID).style.display = 'block';
	else obiect(divID).style.display = 'none';
}

function changePage(val, slug)
{
	pag = val.value;
	if (pag != 'sel')
	{
		window.location = mainWebsiteURL+slug+'/'+pag+'/';
	}
	else alert('Va rugam sa alegeti o pagina!');
}

function viewCid(sel)
{
	if (sel.value == 'comanda')
	{
		if (obiect('trCid')) obiect('trCid').style.display = 'table-row';
		if (obiect('trTr')) obiect('trTr').style.display = 'none';
	}
	else if (sel.value == 'transfer')
	{
		if (obiect('trCid')) obiect('trCid').style.display = 'none';
		if (obiect('trTr')) obiect('trTr').style.display = 'table-row';
	}
	else 
	{
		if (obiect('trCid')) obiect('trCid').style.display = 'none';
		if (obiect('trTr')) obiect('trTr').style.display = 'none';
	}
}

function obiect(id)
{
	return document.getElementById(id);
}

function validEmail(str)
{
	var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	if (str.search(validRegExp) == -1) return false;
	else return true;
}

function changeTab(activeID)
{
	for (i=1;i<=4;i++)
	{
		if (i!=activeID && obiect('t'+i) && obiect('t'+i+'Link')) 
		{
			obiect('t'+i).style.display='none';
			obiect('t'+i+'Link').className='';
		}
	}
	if (obiect('t'+activeID) && obiect('t'+activeID+'Link')) 
	{
		obiect('t'+activeID).style.display='block';
		obiect('t'+activeID+'Link').className='active';
	}
}

function show(divID) 
{
	if (obiect(divID)) obiect(divID).style.display = 'block';
}

function hide(divID) 
{
	if (obiect(divID)) obiect(divID).style.display = 'none';
}

function updateTaxa(caz, cos)
{
	ce = obiect('mpcom').checked;
	judet = obiect('judet').value;
	ljudet = obiect('ljudet').value;
	livrare = ((caz == 0) ? obiect('livrare').checked : obiect('noua').checked);
	mptransfer = obiect('mptransfer').checked;
	if (ce)
	{
		obiect('taxaTransport').innerHTML = '0';
		obiect('totalComanda').innerHTML = cos+' Lei';
	}
	else
	{
		if (livrare)
		{
			if (ljudet == 10)
			{
				obiect('taxaTransport').innerHTML = 15+' Lei';
				obiect('totalComanda').innerHTML = cos+15+' Lei';
			}
			else if (ljudet != 0)
			{
				obiect('taxaTransport').innerHTML = 19+' Lei';
				obiect('totalComanda').innerHTML = cos+19+' Lei';
			}
			else
			{
				obiect('taxaTransport').innerHTML = '';
				obiect('totalComanda').innerHTML = '';
			}
		}
		else if (judet == 10)
		{
			obiect('taxaTransport').innerHTML = 15+' Lei';
			obiect('totalComanda').innerHTML = cos+15+' Lei';
		}
		else if (judet != 0)
		{
			obiect('taxaTransport').innerHTML = 19+' Lei';
			obiect('totalComanda').innerHTML = cos+19+' Lei';
		}
		else
		{
			obiect('taxaTransport').innerHTML = '';
			obiect('totalComanda').innerHTML = '';
		}
	}
	if (mptransfer) obiect('transferBancar').style.display = 'block'; else obiect('transferBancar').style.display = 'none';
}