//////////////////////////////////
//////---GLOBAL VARIABLES---//////
//////////////////////////////////

//CHANGE VARIABLES UNDER THIS STRING

//Info page content
var infoENG = '<p><h2>«IGIT» LTD</h2><font size="5"> Dairy factory</p><p>Address RA, Shirak Region, Azatan village<br>Tel. (+374) 312 6-00-06<br>Fax (+374) 312 3-55-71<br>E-mail <a href="mailto:support@igitkat.am"><u>support@igitkat.am</u></a></p></font>';
var infoRUS = '<p><h2>«ИГИТ» ООО</h2><font size="5"> Завод молочных продуктов</p><p>Адресс РА, регион Ширак, село Азатан<br>Тел. (+374) 312 6-00-06<br>Факс (+374) 312 3-55-71<br>Эл-почта <a href="mailto:support@igitkat.am"><u>support@igitkat.am</u></a></p></font>';
var infoARM = '<p><h2>«ԻԳԻԹ» ՍՊԸ </h2><font size="5"> Կաթնամթերքի գործարան</p><p>Հասցե ՀՀ, Շիրակի մարզ, գյուղ Ազատան<br>Հեռ. (+374) 312 6-00-06<br>Ֆաքս (+374) 312 3-55-71<br>Է-փոստ <a href="mailto:support@igitkat.am"><u>support@igitkat.am</u></a></p></font>';


//History page content
var historyENG = 'English historvalody';
var historyRUS = 'Russian history';
var historyARM = 'Armenian history';

// DO NOT change variables under this string
var pagenum = 1;
var flashReady = false;
var docURL = location.href.substring(location.href.lastIndexOf('/')+1);	  //path to your document URL


///////////////////////////////////////////////////////
//////---GLOBAL FUNCTIONS FOR ALMOST ALL PAGES---//////
///////////////////////////////////////////////////////

//Banner
function initBanner() {
    window.flashObj = (navigator.appName.indexOf ("Microsoft") !=-1)?window["banner"]:window.document["banner"];
}
function conn()
{
	flashReady = true;
	if(document.cookie == 'lang=arm')
	{
		flashObj.startArm();
	}
	else if(document.cookie == 'lang=rus')
	{
		flashObj.startRus();
	}
	else
	{
		flashObj.startEng();
	}
}


//Cookie
function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}


//Defult language
if(document.cookie != 'lang=eng' && document.cookie != 'lang=rus')
{
	document.cookie='lang=arm';
}



//Special script to handle xml in Firefox
function removeWhitespace(xml)
{
	var loopIndex;
	for (loopIndex = 0; loopIndex < xml.childNodes.length;
	loopIndex++) {
	var currentNode = xml.childNodes[loopIndex];
	if (currentNode.nodeType == 1) {
	removeWhitespace(currentNode);
	}
	if (((/^\s+$/.test(currentNode.nodeValue))) &&
	(currentNode.nodeType == 3)) {
	xml.removeChild(xml.childNodes[loopIndex--]);
	}
	}
}


//////////////////////////////////////////
//////---SCRIPT FOR PRODUCTS PAGE---//////
//////////////////////////////////////////



// Script for handling returned products XML
function showProducts(xmldoc,page)
{
	
	var docNode, pageNode, productNode, i, imageNode, nameNode, attr1, attr2, attr3, attr4;
	pagenum = page;
	docNode = xmldoc.documentElement;
	pageNode = docNode.firstChild;
	for(i=1;i<page;i++)
	{
		pageNode = pageNode.nextSibling;
	}
	productNode = pageNode.firstChild;
	for(i=1;i<=3;i++)
	{
		imageNode = productNode.firstChild;
		nameNode = imageNode.nextSibling;
		attr1 = nameNode.nextSibling;
		attr2 = attr1.nextSibling;
		attr3 = attr2.nextSibling;
		attr4 = attr3.nextSibling;
		document.getElementById('name' + i).innerHTML = nameNode.firstChild.nodeValue; 
		document.getElementById('a' + i).href = 'products/' + imageNode.firstChild.nodeValue + '.jpg';
		document.getElementById('pic' + i).src = 'products/tumbs/' + imageNode.firstChild.nodeValue + '.png';
		document.getElementById('attr1' + i).innerHTML = attr1.attributes.getNamedItem("name").value; 
		document.getElementById('val1' + i).innerHTML = attr1.firstChild.nodeValue; 
		document.getElementById('attr2' + i).innerHTML = attr2.attributes.getNamedItem("name").value;
		document.getElementById('val2' + i).innerHTML = attr2.firstChild.nodeValue; 
		document.getElementById('attr3' + i).innerHTML = attr3.attributes.getNamedItem("name").value;
		document.getElementById('val3' + i).innerHTML = attr3.firstChild.nodeValue; 
		document.getElementById('attr4' + i).innerHTML = attr4.attributes.getNamedItem("name").value;
		document.getElementById('val4' + i).innerHTML = attr4.firstChild.nodeValue; 
		productNode = productNode.nextSibling;
	}
	for(k=1;k<=9;k++)
		{
			document.getElementById('page' + k).innerHTML = k;
		}
		document.getElementById('page' + page).innerHTML = '<u>' + page + '</u>';
}


// Products XML get function
function showPage(page)
{
	{
	var xmlhttp = false;
	var mozillaFlag = false;
	if(window.XMLHttpRequest)
	{
		mozillaFlag = true;
		xmlhttp = new XMLHttpRequest();
	}
	else if(window.ActiveXRequest)
	{
		xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
	}
	else if(!xmlhttp)
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(!xmlhttp)
	{
		alert("Sorry, your browser is too old, please update it");
	}
	if(document.cookie == 'lang=eng')
	{
		document.getElementById('pages').innerHTML = 'Pages';
		var url = "xml/products_eng.xml";
	}
	else if(document.cookie == 'lang=rus')
	{
		document.getElementById('pages').innerHTML = 'Страницы';
		var url = "xml/products_rus.xml";
	}
	else
	{
		document.getElementById('pages').innerHTML = 'Էջեր';
		var url = "xml/products_arm.xml";
	}
	if(xmlhttp)
	{
		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{
			var xmldocument = xmlhttp.responseXML;
			if(mozillaFlag){
				removeWhitespace(xmldocument);
			}
			showProducts(xmldocument,page);
			delete xmlhttp;
			xmlhttp = null;
			}
		}
		xmlhttp.send(null);
	}
}
}





//////////////////////////////////
//////---SCRIPTS FOR MENU---//////
//////////////////////////////////

//Script for showing pictures in menu and changing menu element colors

function change(obj_id, status)
{
	if(status == '1')
	{
		document.getElementById('img' + obj_id).src = 'buttons/p' + obj_id + status + '.png';
		document.getElementById('p' + obj_id).color = '#00FF00';
	}
	else
	{
		document.getElementById('img' + obj_id).src = 'buttons/p00.png';
		document.getElementById('p' + obj_id).color = '#005E00';
	}
	
}

// Script for getting menu elements from XML
function sendDate(lang)
{
	var xmlhttp = false;
	var mozillaFlag = false;
	if(window.XMLHttpRequest)
	{
		mozillaFlag = true;
		xmlhttp = new XMLHttpRequest();
	}
	else if(window.ActiveXRequest)
	{
		xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
	}
	else if(!xmlhttp)
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(!xmlhttp)
	{
		alert("Sorry, your browser is too old, please update it");
	}
	var url = "xml/menu.xml";
	if(xmlhttp)
	{
		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{
			var xmldocument = xmlhttp.responseXML;
			if(mozillaFlag){
				removeWhitespace(xmldocument);
			}
			display(xmldocument,lang);
			delete xmlhttp;
			xmlhttp = null;
			}
		}
		xmlhttp.send(null);
	}
}



//Script for handling menu XML
function display(xmldoc,lang)
{
	var docNode, menuNode, menu1, menu2, menu3, menu4, menu5;
	docNode = xmldoc.documentElement;
	if(lang == 'arm')
	{		
		menuNode = docNode.firstChild;
		document.cookie="lang=arm";
		document.getElementById('lang1').href = "javascript:sendDate('rus');";
		document.getElementById('lang1').innerHTML = '<img src="pics/rus.png" alt="" width="36" height="23" border="0"><br>Русский';
		document.getElementById('lang2').href = "javascript:sendDate('eng');";
		document.getElementById('lang2').innerHTML = '<img src="pics/eng.png" alt="" width="36" height="23" border="0"><br>English';
	}
	else if(lang == 'rus')
	{
		menuNode = docNode.firstChild.nextSibling;
		document.cookie="lang=rus";
		document.getElementById('lang1').href = "javascript:sendDate('arm');";
		document.getElementById('lang1').innerHTML = '<img src="pics/arm.png" alt="" width="36" height="23" border="0"><br>Հայերեն';
		document.getElementById('lang2').href = "javascript:sendDate('eng');";
		document.getElementById('lang2').innerHTML = '<img src="pics/eng.png" alt="" width="36" height="23" border="0"><br>English';
	}
	else
	{
		document.cookie="lang=eng";
		menuNode = docNode.lastChild;
		document.getElementById('lang1').href = "javascript:sendDate('rus');";
		document.getElementById('lang1').innerHTML = '<img src="pics/rus.png" alt="" width="36" height="23" border="0"><br>Русский';
		document.getElementById('lang2').href = "javascript:sendDate('arm');";
		document.getElementById('lang2').innerHTML = '<img src="pics/arm.png" alt="" width="36" height="23" border="0"><br>Հայերեն';
	}
	if(flashReady == true)
	{
		conn();
	}
	menu1 = menuNode.firstChild;
	menu2 = menu1.nextSibling;
	menu4 = menu2.nextSibling;
	menu5 = menu4.nextSibling;
	document.getElementById('p0').innerHTML = menu1.firstChild.nodeValue;
	document.getElementById('p1').innerHTML = menu2.firstChild.nodeValue;
	document.getElementById('p3').innerHTML = menu4.firstChild.nodeValue;
	document.getElementById('p4').innerHTML = menu5.firstChild.nodeValue;
	if(docURL == 'products.html')
	{
		showPage(pagenum);
	}
	else if(docURL == 'contact.html')
	{
		showInfo(document.cookie);
	}
	else if(docURL == 'history.html')
	{
		showHistory(document.cookie);
	}
	else if(docURL == 'pics.html')
	{
		getPics(pagenum);
	}
}





///////////////////////////////////////
//////---SCRIPTS FOR INFO PAGE---//////
///////////////////////////////////////

// Language changing script

function showInfo(lang)
{
	if(lang == 'lang=eng')
	{
		document.getElementById('info').innerHTML = infoENG;
	}
	else if(lang == 'lang=rus')
	{
		document.getElementById('info').innerHTML = infoRUS;
	}
	else
	{
		document.getElementById('info').innerHTML = infoARM;
	}
}

//////////////////////////////////////////
//////---SCRIPTS FOR HISTORY PAGE---//////
//////////////////////////////////////////

// Language changing script

function showHistory(lang)
{
	if(lang == 'lang=eng')
	{
		document.getElementById('history').innerHTML = historyENG;
	}
	else if(lang == 'lang=rus')
	{
		document.getElementById('history').innerHTML = historyRUS;
	}
	else
	{
		document.getElementById('history').innerHTML = historyARM;
	}
}




/////////////////////////////////////////
/////---SCRIPTS FOR PICTURES PAGE---/////
/////////////////////////////////////////



//Script for getting pictures XML

function getPics(page)
{
	pagenum = page;
	if(document.cookie == 'lang=eng')
	{
		document.getElementById('pages').innerHTML = 'Pages';
	}
	else if(document.cookie == 'lang=rus')
	{
		document.getElementById('pages').innerHTML = 'Страницы';
	}
	else
	{
		document.getElementById('pages').innerHTML = 'Էջեր';
	}
	for(i=1;i<=6;i++)
	{
		pic = ((page-1)*6)+i
		document.getElementById('pic' + i).src = 'photoes/tumbs/' + pic + '.png';
		document.getElementById('a' + i).href = 'photoes/' + pic + '.jpg';
		for(k=1;k<=4;k++)
		{
			document.getElementById('page' + k).innerHTML = k;
		}
		document.getElementById('page' + page).innerHTML = '<u>' + page + '</u>';
	}
}

