function makeRequest(locationURL, parameters, onComplete, doSynchronous) {// Safari & Chrome
    var http_request = false;
    try {http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
        try {http_request= new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            http_request = new XMLHttpRequest();
        }
    }
 
    if (!http_request) {
      alert("Ваша система не поддерживает работу с XML-данными. Чтобы просмотреть наше портфолио, необходимо произвести обновление браузера до самой новой версии.");
      return false;
    }
 
	http_request.open('GET', locationURL + (parameters ? ("?" + parameters) : ""), !doSynchronous);
	http_request.send(null);                        
    return http_request;
}


function loadImagesDB(fileName) {
	dbImages = null;
 
	if (window.ActiveXObject) { // IE
		dbImages = new ActiveXObject("Microsoft.XMLDOM");
	} else 
	if (document.implementation && document.implementation.createDocument) { // Firefox, Opera
		dbImages = document.implementation.createDocument("","",null);
		if (!dbImages.load) { // Safari & Chrome
			var request = makeRequest(fileName, null, null, true);
			return request.responseXML;}
	} else {
		alert("Ваша система не поддерживает работу с XML-данными. Чтобы просмотреть наше портфолио, необходимо произвести обновление браузера до самой новой версии.");
	}

	dbImages.async = false;
	try {
		dbImages.load(fileName);
	} catch(e) {
		alert("Извините, произошла ошибка при загрузке файла данных: " + fileName);
	}
	return(dbImages);
}


function scoreItemsOverall(dbName){
	itemsCount = 0;
	allItems = loadImagesDB(dbName);
	
	with (allItems.childNodes.item(0).childNodes)
		for (i = 0; i < length; i++)
			if (item(i).nodeName != "#text") itemsCount++;
	
	return (itemsCount);
}


function scoreItemsByKey(dbName, dbKey, dbKeyValue){
	itemsCount = 0;
	allItems = loadImagesDB(dbName);
	
	with (allItems.childNodes.item(0).childNodes)
		for (i = 0; i < length; i++)
			if (item(i).nodeName != "#text")
				if (item(i).getAttribute(dbKey) == dbKeyValue) itemsCount++;
	
	return (itemsCount);
}


function scoreCategories(dbName){
	catList = [];
	catCount = [];
	isExist = false;
	allItems = loadImagesDB(dbName);
	
	with (allItems.childNodes.item(0).childNodes) {
		for (i = 0; i < length; i++){
			if (item(i).nodeName != "#text") {
				isExist = false;
				for (k = 0; k < catList.length; k++) if (catList[k] == item(i).getAttribute("category")) isExist = true;
				if (!isExist) catList[catList.length] = item(i).getAttribute("category");
			}
		}

		for (k = 0; k < catList.length; k++) catCount[k] = 0;
		for (i = 0; i < length; i++){
			if (item(i).nodeName != "#text") {
				for (k = 0; k < catList.length; k++) if (catList[k] == item(i).getAttribute("category")) catCount[k] += 1;
				isExist = false;
			}
		}
	}
	
	totalCount = 0;
	for (k = 0; k < catList.length; k++) {
		document.getElementById(catList[k]).innerHTML = "(" + catCount[k] + ")";
		totalCount += catCount[k];
	}
	document.getElementById("total").innerHTML = "(" + totalCount + ")";
}


function parseDataBase(dbName, imgsPath, keyName, keyValue) {
	arrList = [];
	itemsCount = 0;
	imagesDB = loadImagesDB(dbName);
	htmlData = "<table width='600px' cellspacing='0' cellpadding='0'>";
	
	with (imagesDB.childNodes.item(0).childNodes) {
		if (keyName == null) {
			for (i = 0; i < length; i++){
				if (item(i).nodeName != "#text") {
					var arrItem = [];
					arrItem[0] = item(i).getAttribute("client");
					arrItem[1] = item(i).getAttribute("work");
					arrItem[2] = item(i).getAttribute("year");
					arrItem[3] = item(i).getAttribute("thumb");
					arrList[itemsCount] = arrItem;
					itemsCount++;
				}
			}
		} else {
			for (i = 0; i < length; i++){
				if ((item(i).nodeName != "#text")&&(item(i).getAttribute(keyName) == keyValue)) {
					var arrItem = [];
					arrItem[0] = item(i).getAttribute("client");
					arrItem[1] = item(i).getAttribute("work");
					arrItem[2] = item(i).getAttribute("year");
					arrItem[3] = item(i).getAttribute("thumb");
					arrList[itemsCount] = arrItem;
					itemsCount++;
				}
			}
		}
	}

	rowsCount = Math.ceil(itemsCount/3);
	readItemNo = 0;

	for (i=0; i<rowsCount; i++) {
		htmlData += "<tr>";
		for (j=1; j<=3; j++) {
			htmlData += "<td height='225px' align='center' valign='middle' id='flashcell" + readItemNo +"'></td>";
			readItemNo++;}
		htmlData += "</tr>";
	}
	if (rowsCount <= 3) for (i=1; i <= ((rowsCount-4)*-1); i++) htmlData += "<tr><td height='225px'></td><td height='225px'></td><td height='225px'></td></tr>";
	
	htmlData += "</table>";
	
	document.getElementById("imgGrid").innerHTML = htmlData;
	
	function insFlash (clientName, workName, yDate, imgName, id) {
		var flashItem = new SWFObject("flash/import.swf", "flash"+id, "150", "200", "8");

		flashItem.addParam("wmode", "opaque");
		flashItem.addParam("bgcolor", "#404040");
		flashItem.addParam("quality", "high");
		flashItem.addParam("menu", "false");
		flashItem.addParam("allowScriptAccess", "always");
		flashItem.addVariable("varClient", clientName);
		flashItem.addVariable("varWorks", workName);
		flashItem.addVariable("varDate", yDate);
		flashItem.addVariable("varImage", imgsPath + imgName);
		flashItem.write("flashcell"+id);
	}
	
	for (i=0; i<itemsCount; i++) insFlash(arrList[i][0], arrList[i][1], arrList[i][2], arrList[i][3], i);
}

function getRandomImage(dbName, imgsPath, targetElement, targetChapter) {
	imagesDB = loadImagesDB(dbName);
	with (imagesDB.childNodes.item(0).childNodes) {
		randomIndex = Math.floor(Math.random() * (length-1));
		if (item(randomIndex).nodeName != "#text")
			imgName = item(randomIndex).getAttribute("thumb");
		else imgName = item(randomIndex+1).getAttribute("thumb");
	}
	
	
	var flashItem = new SWFObject("flash/import_main.swf", targetElement, "150", "200", "8");

	flashItem.addParam("wmode", "opaque");
	flashItem.addParam("bgcolor", "#404040");
	flashItem.addParam("quality", "high");
	flashItem.addParam("menu", "false");
	flashItem.addParam("allowScriptAccess", "always");
	flashItem.addVariable("varImage", imgsPath + imgName);
	flashItem.addVariable("varChapter", targetChapter);
	flashItem.write(targetElement);
}