// -----------------------------------------------------------------------------------
//
//	BBToolbox
//	(c) 2009 BitBee Solutions GmbH
//
// -----------------------------------------------------------------------------------
/*

    Table of Contents
    -----------------
	Functions:
	-------------
	NewDivContent
	OldDivContent
*/
// -----------------------------------------------------------------------------------

// Global javascript variables
var oldContent = '';					// Generally holds the active thumnbail matrix (full HTML code in contentcenter div)
var arrPicPaths = new Array();	// Names of the full size pictures which are in the current subcategory
var imageDirectory = '';				// Directory of the current subcategory (path)
var intCurrPic = 0;					// Index pointing the currently showed full size picture (Index points to arrPicPaths)


// Called by PHP when a new subcategory has been selected.
function FillPicPaths(picPaths, imageDir)
{
	arrPicPaths = picPaths.split(";");
	imageDirectory = imageDir;
}

//
//  Fills a div with the passed in content.
//
function NewDivContent(divName, divContent) {
	oldContent = document.getElementById(divName).innerHTML;
	document.getElementById(divName).innerHTML=divContent;
}

// Restore the thumbnail matrix
function SwitchBackOldContent(divName)
{
	document.getElementById(divName).innerHTML=oldContent;
}

// Called when a user clicks a thumbnail
function ShowPictInDiv(divName, picPath, picIndex)
{
	if (FileExists(picPath))
	{
		// Set the index of the picture, the user has clicked. This is the index which points a picture in the javascript array arrPicPaths.
		intCurrPic = picIndex;
		
		ShowPictInDivInternal(divName, picPath, true);
	}
	return false;
}

// Called by ShowPictInDiv and internally as soon as a user clicks backward or forward.
function ShowPictInDivInternal(divName, picPath, storeOldContent)
{
	var strTemp;
	strTemp = '';
	//var documentRoot = document.location.href.slice(0, document.location.href.lastIndexOf('/'));
	if (FileExists(picPath))
	{
		if (storeOldContent)
			oldContent = document.getElementById(divName).innerHTML;
		//document.getElementById(divName).innerHTML='<img onmouseout="SwitchBackOldContent(' + "'" + divName + "'" + ');" src="' + picPath + '"/>' ;
		strTemp = '<div id="bigpic"><img src="' + picPath + '"/></div>';
		strTemp += '<div id="navibox">'
		if (intCurrPic > 0) // Only show back button, if there are pix available
			strTemp += '<a href="" onclick="LastPic(); return false;"><img class="navigation" src="images/back.gif"/></a>';
		else
			strTemp += '<a href="" onclick="LastPic(); return false;"><img class="navigationfaded" src="images/back.gif"/></a>';
		strTemp += '<a href="" onclick="SwitchBackOldContent(' + "'" + divName + "'" + '); return false;"><img class="navigation" src="images/nwclose.gif"/></a>';
		if (intCurrPic < arrPicPaths.length - 1)
			strTemp += '<a href="" onclick="NextPic(); return false;"><img class="navigation" src="images/forward.gif"/></a>'
		else 
			strTemp += '<a href="" onclick="NextPic(); return false;"><img class="navigationfaded" src="images/forward.gif"/></a>'
		strTemp += '</div>';
		document.getElementById(divName).innerHTML = strTemp;
		
	}
	return false;
}

// Called when a user clicks the last picture button
function LastPic()
{
	if (intCurrPic > 0)
		intCurrPic -= 1;
		
	ShowPictInDivInternal('dcentercontent', imageDirectory + arrPicPaths[intCurrPic], false);
}

// Called when a user clicks the next picture button
function NextPic()
{
	if (intCurrPic < arrPicPaths.length - 1)
		intCurrPic += 1;
		
	ShowPictInDivInternal('dcentercontent', imageDirectory + arrPicPaths[intCurrPic], false);
}

// Checks, if a file exists.
function FileExists(strURL)
{
    oHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");    
	oHttp.open("HEAD", strURL, false);
	oHttp.send(null);
	return (oHttp.status==404) ? false : true;
}

// Check if a form field is provided with a value
function DoSubmitContact()
{
	var errMsg = "";
	if (document.getElementById('name').value == '')
	{
		errMsg += 'The field "name" must not be empty.<br />';
	}
	if (document.getElementById('email').value == '')
	{
		errMsg += 'The field "email" must not be empty.<br />';
	}
	if (document.getElementById('msg').value == '')
	{
		errMsg += 'The field "message" must not be empty.<br />';
	}

	if (errMsg != "")
	{
		document.getElementById("frmErr").innerHTML = errMsg;
		return false;
	} else {
		return true;
	}
}
