﻿	function doGetNavigation(lng){
		if (!lng){
			lng = "eng";
		}
		navigation = makeRequest("inc/navigation.xml", "createNavigation", "xmlfunc");
	}
	function isNumber(a) {
		return ! isNaN (a-0);
	}

	function safeFileNameSS(t){
		return safeFileName(t, 'successStory');
	}
	function safeFileName(t, article){
		if (!article){
			article = 'news';
		}
		t = t.toLowerCase();
		t = t.replace(/ë/g, "e");
		t = t.replace(/ç/g, "c");
		t = t.replace(/ć/g, "c");
		t = t.replace(/č/g, "c");
		t = t.replace(/š/g, "s");
		t = t.replace(/ž/g, "z");
		t = t.replace(/đ/g, "d");
		
		t = t.replace(/\-/g, "");
		t = t.replace(/[^a-z0-9 ]/g, " ");
		while (t.match(/  /)){
			t = t.replace(/  /,' ');
		}
		t = t.trim();
		t = t.replace(/ /g, '_', t);
		
		if (t.length > 80){
			t = t.substr(0, 80);
			var pos = t.lastIndexOf("_");
			t = t.substr(0,pos);
		}
		t = article + '_' + t + ".html";
		return t;
	}

	
	function titleToUrl(title){	
		title = title.replace(/&acute;/g, " ");
		title = title.toLowerCase();
		title = title.replace(/[^a-z]/g, " ");
		title = title.trim();
		title = title.replace(/ /g, "_");
		title = title.replace(/__/g, "_");
		title = title.replace(/__/g, "_");
		return title;
	}
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g,"");
	}	
	function createNavigation(xml){
		xml = removeWhiteSpace(xml);
		siteMapMenu = new Array();
		
		loc = location.href;
		loc = loc.split("/");
		loc = loc[loc.length - 1];
		loc = loc.split("?");
		loc = loc[0];
		var currentMainMenuId;
		var breadCrump;
		for (i = 0; i < xml.childNodes[0].childNodes.length; i++){
			submenu = [];
			row = xml.childNodes[0].childNodes[i];
			siteMapMenu[i] = new Array();
			document.getElementById('menuHolder').innerHTML += "<div class='mainMenu' id='menuId_" + i + "'><a href='" + row.childNodes[1].childNodes[0].nodeValue + "'>" + row.childNodes[0].childNodes[0].nodeValue + "</a></div>";
			if (loc == row.childNodes[1].childNodes[0].nodeValue){
				currentMainMenuId = i;
				breadCrump = "<a class='linkBreadCrumpActive' href='" + row.childNodes[1].childNodes[0].nodeValue + "'>" + row.childNodes[0].childNodes[0].nodeValue + "</a>";
			}
			for (j = 0; j < row.childNodes[2].childNodes.length; j++){
				if (!currentMainMenuId){
					if (loc == row.childNodes[2].childNodes[j].childNodes[1].childNodes[0].nodeValue){
						currentMainMenuId = i;
						breadCrump = "<a class='linkBreadCrump' href='" + row.childNodes[1].childNodes[0].nodeValue + "'>" + row.childNodes[0].childNodes[0].nodeValue + "</a>";
						breadCrump = breadCrump + " > <a class='linkBreadCrumpActive' href='" + row.childNodes[2].childNodes[j].childNodes[1].childNodes[0].nodeValue + "'>" + row.childNodes[2].childNodes[j].childNodes[0].childNodes[0].nodeValue + "</a>";
					}
				}
				submenu.push([row.childNodes[2].childNodes[j].childNodes[0].childNodes[0].nodeValue, row.childNodes[2].childNodes[j].childNodes[1].childNodes[0].nodeValue, '']);
			}
			siteMapMenu[i].push(row.childNodes[0].childNodes[0].nodeValue, row.childNodes[1].childNodes[0].nodeValue, submenu);
			makeMenu("menuId_" + i, submenu);
		}
		if (currentMainMenuId && document.getElementById("navLevelTwo")){
			createNavigationLevel2(currentMainMenuId, xml);
		} else {
			loc = location.href.split("/");
			loc = loc[loc.length - 1];
			loc = loc.split(".");
			loc = loc[0];
			if (loc == "archive"){
				createNavigationLevel2(2, xml);		
			}
		}

		initMenu();
		createSiteMap(siteMapMenu);
		updatePositions();
		if (document.getElementById('breadCrumpArea')){
			if (breadCrump){
				document.getElementById('breadCrumpArea').innerHTML = " Ju jeni këtu: " + breadCrump;
			} else {
				document.getElementById('breadCrumpArea').innerHTML = " &nbsp; ";
			}
		}
	}
	
	function createSiteMap(siteMapArray){
		siteMapCnt = "<div><div style='width:950px; height:25px; background-image:url(img/system/footer_bg_main.jpg); text-align:left;'><img src='img/system/siteMap_label_alb.gif' alt='USAID KOSOVO / Site Map' /></div>";
		for (var i=1; i<siteMapArray.length; i++){
			siteMapCnt += "<div style='padding-left:15px;'><div style='float:left; padding-top:10px; text-align:left; width:155px; display:table;'>";
				siteMapCnt += "<div style='height:20px;'><a href='"+siteMapArray[i][1]+"' class='siteMap'><strong>"+siteMapArray[i][0]+"</strong></a></div>";
				siteMapCnt += "<div'>";
					siteMapCnt += "<ul style='list-style-type:none; margin:0px; padding:0px;'>";
					if (siteMapArray[i][2] != ""){
						for (var j=0; j<siteMapArray[i][2].length; j++){
							siteMapCnt += "<li style='margin-left:0px; height:18px;'><a href='"+siteMapArray[i][2][j][1]+"' class='siteMap'>"+siteMapArray[i][2][j][0]+"</a></li>";
						}
					}
					siteMapCnt += "</ul>";
				siteMapCnt += "</div'>";
			siteMapCnt += "</div></div>";
		}
		siteMapCnt += "<div>";
		document.getElementById('USAIDsiteMap').innerHTML = siteMapCnt;
	}	
	function doGetNavigationLevel2(){
	}
	
	function createNavigationLevel2(idm, xml){
		levelId = parseInt(idm);
		xml = removeWhiteSpace(xml);
		row = xml.childNodes[0].childNodes[levelId].childNodes[2];

		navLevel2 = "<ul class='navSecGroup'>";
		for (k = 0; k < row.childNodes.length; k++){
			navLevel2 += "<li class='navSecElement' onMouseOver=\"this.style.backgroundColor='#d3355a'\" onMouseOut=\"this.style.backgroundColor='#c2113a'\" id='subMenuId_" + k + "'><a class='navSec' href='" + row.childNodes[k].childNodes[1].childNodes[0].nodeValue + "'>" + row.childNodes[k].childNodes[0].childNodes[0].nodeValue + "</a></li>";
		}
		navLevel2 += "</ul>";
		document.getElementById('navLevelTwo').innerHTML = navLevel2;
		document.getElementById('navLevelTwoParent').innerHTML = xml.childNodes[0].childNodes[levelId].childNodes[0].childNodes[0].nodeValue;
	}	


	function removeWhiteSpace(xml){
		for (var i = xml.childNodes.length -1; i >= 0; i--){
			if (xml.childNodes[i].nodeType == 3) {
				if (xml.childNodes[i].nodeValue.match(/^\s*$/)){
					xml.removeChild(xml.childNodes[i]);
				}
			} else {
				xml.replaceChild(removeWhiteSpace(xml.childNodes[i]),xml.childNodes[i]);
			}
		}
		return xml;
	}
	
	function makeRequest(url, returnFunction, type, postParams) {
		var http_request = false;
		if (window.XMLHttpRequest) {
			http_request = new XMLHttpRequest();
				if (http_request.overrideMimeType) {
					http_request.overrideMimeType('text/xml');
				}
			} 
			if (window.ActiveXObject) { 
				try {
					http_request = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						http_request = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {}
				}
			}
			if (!http_request) {
				alert('Giving up :( Cannot create an XMLHTTP instance');
				return false;
			}
			http_request.onreadystatechange = function() {
				if (http_request.readyState == 4) { 
					if (http_request.status == 200 || http_request.status == 0) {
						if (returnFunction){
							if (type == "textfunc"){
								eval(returnFunction + '(http_request.responseText)');
							} else if (type == "xmlfunc"){
								eval(returnFunction + '(http_request.responseXML)');
							} else if (type == "textid"){
								gel(returnFunction).innerHTML = http_request.responseText;
							} else {
								eval(returnFunction + '(http_request.responseXML)');
							}
						} else {
						}
					} else {
					}
				}
			};
		if (postParams){
			http_request.open('POST', url, true);
			http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http_request.setRequestHeader("Content-length", postParams.length);
			http_request.setRequestHeader("Connection", "close");
			http_request.send(postParams);
		} else {
			http_request.open('GET', url, true);
			http_request.send(null);
		}
	}

	function doSubmitSearch(){
		var searchTerm = document.getElementById('q').value;
		if (searchTerm != ""){
			window.location = "search.html?siteurl=www.usaid.gov/kosovo/eng/&cx=014799971538997001187:ibvb0oc9ahs&cof=FORID:10&ie=UTF-8&q="+searchTerm+"&sa=GO";
			return false;
		} else {
			alert('Please type the keyword to search!');
			return false;			
		}
	}
