/*
Strip whitespace from the beginning and end of a string
*/
function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}

/*
   Check if a string is in valid email format.
*/
function isEmail(str)
{
var regex = /^[-_.a-z0-9]+@(([-a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|b|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|m|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|h|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|l|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ninl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|j|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|g|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;

return regex.test(str);
}

function openPopup( command, attributes )
{	
	switch( command )
	{
		case "image":
			window.open( "/utils/images.php" + attributes, "imagePopup", "status=false,toolbar=false,resizable=false,width=800,height=600" );
		break;

		case "pdf":
			window.open( "/utils/pdf.php" + attributes, "pdfPopup", "status=false,toolbar=false,resizable=false,width=650,height=400" );
		break;
	}
}

function loadPage( destination, pageToLoad, action )
{
	var http_request;

	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();

		if (http_request.overrideMimeType)
		{
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	}else
		if (window.ActiveXObject)
		{ // IE
			try
			{
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}catch (e)
			{
				try
				{
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}catch (e) {}
			}
		}

	http_request.open('GET', pageToLoad, true);

	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 1)
		{
			// Cria o efeito de loading
			showLoading(true, document.getElementById( destination ));
		}

		if (http_request.readyState == 4)
		{
			if (http_request.status == 200)
			{
				// Remove o efeito de loading
				document.getElementById( destination ).style.textAlign = 'left';
				showLoading(false, document.getElementById( destination ));
				document.getElementById( destination ).innerHTML = http_request.responseText;
				
				if( action != "" )
					eval( action );
			} // if-status->200
		}
	}

	http_request.send(null);
}

function showLoading(opt, element)
{
	if (opt == true)
	{
		//element.style.textAlign = 'center';

		var img = document.createElement('img');
		img.setAttribute('src','/admin/images/loading.gif');
		img.setAttribute('id','loading');
		img.setAttribute('width','32px');
		img.style.marginTop = '10px';

		if (!document.getElementById('loading'))
		{
			element.innerHTML = "";
			element.insertBefore(img, element.firstChild);
		}
	}
	else 
		if (opt == false)
		{
			var imgLoading = document.getElementById('loading');
			if (imgLoading)
			{
				imgLoading.parentNode.removeChild(imgLoading);
			}  //imgloading
		} //elseif
} //loading

var http_request = false;

var component = "";

function makePOSTRequest( url, parameters, destination )
{
	component = destination;
	http_request = false;

	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
    
		if (http_request.overrideMimeType)
		{
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	}else
		if (window.ActiveXObject)
		{ // IE
			try
			{
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}catch (e)
			{
				try
				{
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}catch (e) {}
			}
		}

	if (!http_request)
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = showResult;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function showResult()
{
	if ( http_request.readyState == 4 )
		if( http_request.status == 200 )
		{
			document.getElementById( component ).innerHTML = http_request.responseText;
		}else
			alert('There was a problem with the request.');
}

function updateSeriesCB( artistId )
{
	makePOSTRequest( "/utils/scripts/getSeries.php", "artistId=" + artistId, "CBSeries");
}

function removeListRow( rowId, tableId, listId )
{
	var table 		= document.getElementById( tableId );
	var list	= document.getElementById( listId );

	if( list.value == rowId )
		list.value = "";
	else
		if( list.value.indexOf( "-" + rowId ) != -1 )
			list.value = list.value.replace( "-" + rowId, "");
		else
			list.value = list.value.replace( rowId + "-", "");

	for( var i = 0; i != table.rows.length; i++ )
	{
		if( table.rows[i].id == rowId )
		{
			table.deleteRow(i);
			break;
		}
	}

	if( table.rows.length == 0 )
	{
		table.innerHTML = "<tr><td colspan='6'>La liste d'oeuvres de cette exposition est vide</td></tr>";
	}
}

function moveListRow( element, listId, direction)
{
	var list	= document.getElementById( listId ).value.split("-");

	while ( element.parentNode && 'tr' != element.nodeName.toLowerCase() )
	{
		element = element.parentNode;
	}

	var table = element.parentNode;
	var i = element.rowIndex + direction;

	list.splice(element.rowIndex,1);
	list.splice(i,0, element.id);

	document.getElementById( listId ).value = list.join("-");

	if( i >= table.rows.length || i < 0 )
		return;

	table.removeChild( element );
	var nRow = table.insertRow( i );
	table.replaceChild(element, nRow);
}
