// show hide mainDet content
function show(divID)
{
	if (document.getElementById(divID))	document.getElementById(divID).style.display='block';
}
function showi(divID)
{
	if (document.getElementById(divID))	document.getElementById(divID).style.display='inline';
}
function hide(divID)
{
	if (document.getElementById(divID))	document.getElementById(divID).style.display='none';
}
function showHide(divID) 
{
	document.getElementById(divID).style.display == 'none' ? document.getElementById(divID).style.display = 'block' : document.getElementById(divID).style.display = 'none';
}

// 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 createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function reloadWindow()
{
	window.location=window.location;
	
	return;
}

function obiect(id)
{
	if (document.getElementById(id)) return document.getElementById(id); else return false;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function markFields(campuri)
{
	var vector = new Array;
	vector = campuri.split("%");
	for (i=0;i<vector.length;i++)
	if (obiect(vector[i]))
	{
		obiect(vector[i]).style.backgroundColor = '#F8E6E4';
		obiect(vector[i]).style.color = '#b93939';
	}
}

function closeWindow(id)
{
	if (document.getElementById(id)) document.getElementById(id).style.display = 'none';
}

function viewBox(iid)
{
	if (iid.checked) document.getElementById('altaFactura').style.display = 'block';
	else document.getElementById('altaFactura').style.display = 'none';
}

function confirma(caz,mes,link,stoc)
{
	switch (caz)
	{
		case 'sterge':
			var sterge = confirm(decodeURIComponent(mes.replace(/\+/g,  " ")));
			if (sterge) window.location = link;
			break;
		case 'adauga':
			if (stoc != null && stoc != 0)
			{
				window.location = link;
			}
			else alert(decodeURIComponent(mes.replace(/\+/g,  " ")));
			break;
	}
}

function orderDetail(oid,clas)
{
	tr = document.getElementById(oid);
	if (tr.style.display == 'none') 
	{
		var browser = navigator.appName;
		view = ((browser == 'Netscape') ? 'table-row' : 'block')
		tr.style.display = view;
	}
	else
	{
		tr.style.display = 'none';
	}
}

function stripPath(loc)
{
	return loc.replace(/^(((.+)\/)+)?(([\w\-_]+\.)+\w{3})$/, '$4');
}

function updatePret(pret)
{
	var pret = parseFloat(pret);
	var variante = getElementsByClass('selectVarianta',null,'select');
	for (i=0; i<variante.length; i++)
	{
		if (typeof(optiuni[variante[i].value]) != 'undefined') pret += parseFloat(optiuni[variante[i].value]);
	}
	obiect('pretProdus').innerHTML = addCommas(pret);
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1))
	{
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function swapPic(thumb)
{
	newSrc = stripPath(thumb.src);
	oldSrc = stripPath(obiect("smallPic").src);
	jQuery.noConflict();
	jQuery(thumb).attr("src",mainWebsiteURL+'custom_images/produse/thumb/'+oldSrc);
	jQuery("#smallPic").attr("src",mainWebsiteURL+'custom_images/produse/det/'+newSrc);
	jQuery("#bigPic").attr("href",mainWebsiteURL+'custom_images/produse/pop/'+newSrc);
	Shadowbox.setup();
}

$().ready(function(){
	$("div.produs.last_item").after('<div class="clear"></div>');
});
