
function doShow(form) {
    form.style.display = "block";
}

//=====================================Menu Function=============================================
var menuTime;    // Timer identifier used to hide submenus
var hideSub;    // Menu item, for which submenu should be hidden
var closetype; //If 'all' then all menus must be closed. Else only 1 menu will be closed
var currentVisible = true;


var showTest = false;

function setTest() {
	if (showTest) {
		showTest = false;
	} else {
		showTest = true;
	}
	
}
//This function add some test data to title (convenient for testing)
function addTitle(text)
{
 	if (document.title.length > 55) {
 		document.title = '';
 	}                                	
	document.title += ' ' + text + ' ';
	
}

//This function add some test data to title (convenient for testing)
function addTestText(text)
{
	if (showTest) { 
		el = document.getElementById("FrontArticle");
		el.innerHTML = el.innerHTML + text + "=== ";
	}
	
	return true;
	
}


/* check if obj is menu item or its contents (oblect if true, null if false) */
function isMenuItem(obj) {
	if (!obj) {
		return false;
	}
	
	//Is needed for Safari - because in Safari text in <a> is also mouse overed but it does not have
	//tagName - therefore this function do not work in some cases
	if (!obj.tagName) {
		obj = (obj.parentElement) ? obj.parentElement : obj.parentNode;
	}
    while (obj && (obj != document) && (obj.tagName.toLowerCase() != "ul")) {
        if (obj.tagName.toLowerCase() == "li") return obj;
        obj = (obj.parentElement) ? obj.parentElement : obj.parentNode;
    }
    return false;
}

function isShouldOpen(obj) {
    while (obj && (obj != document)) {
    	if (obj.getAttribute('eltype')) {
        	if (obj.getAttribute('eltype').toLowerCase() == "open") return obj;
        }
        obj = (obj.parentElement) ? obj.parentElement : obj.parentNode;
    }
    return false;
}

function changeSrc(src, operation) {
	if (operation == 'open') {
		return src.replace(new RegExp("_open."), "_close.");
	} else {
		return src.replace(new RegExp("_close."), "_open.");
	}
}




function clearCurrent() {
	currentVisible = false;
	var currentEl = document.getElementById("current");
	if (currentEl) {
	    currentEl.className = "";
	}
}

/* handling multiple styles applied to an element */
function addStyle(obj, class_name) {
    var cn = obj.className;
    var re = new RegExp("(^| )" + class_name + "($| )");
    var re1 = new RegExp("(^| )"  + "*cust" + class_name + "($| )");
    var re2 = new RegExp("cust");
    if (cn.search(re) != -1) return false;
    if (cn.search(re1) != -1) return false;
    if ((cn.search(re2) != -1) && ((class_name == "sub") || (class_name == "sub over"))) {
		cn = cn.replace(re2, "cust" + class_name);
	} else {
	    if (cn != "" && cn.charAt(cn.length - 1) != " ") cn += " ";
	    cn += class_name;
	}
    obj.className = cn;
}
function removeStyle(obj, class_name) {

    if (!obj) return;
    var re = new RegExp(" " + class_name + " ");
    var re1 = new RegExp("(^" + class_name + " )|( " + class_name + "$)|(^"
                           + class_name + "$)");
    var re2 = new RegExp("cust" + class_name + "($| )");
    if ( closetype == 'all' )
    {
	    while (obj && (obj != document) && (obj.className != "menu") && (obj.className != "menu1") && (obj.className != "menu2")) {
        	if (obj.tagName.toLowerCase() == "li")
		{
		    var cn = obj.className;
		    cn = cn.replace(re, " ");
		    cn = cn.replace(re1, "");
		    cn = cn.replace(re2, "cust");
		    obj.className = cn;
		}

	        obj = (obj.parentElement) ? obj.parentElement : obj.parentNode;
	    }
	hideSub = null;
    }
    else
    {
	    var cn = obj.className;
	    cn = cn.replace(re, " ");
	    cn = cn.replace(re1, "");
	    cn = cn.replace(re2, "cust");
	    obj.className = cn;
    }
}

function parentUL(obj) {
	parentul = ((obj.parentElement) ? obj.parentElement : obj.parentNode);
	return parentul;
}

function parentLI(obj) {
	parentul = ((obj.parentElement) ? obj.parentElement : obj.parentNode);
	if ((parentul.className == "menu") || (parentul.className == "menu1") || (parentul.className == "menu2")) return null;
	parentli = ((parentul.parentElement) ? parentul.parentElement : parentul.parentNode);
	return parentli;
}

function doubleParentUL(obj) {
	parentul = ((obj.parentElement) ? obj.parentElement : obj.parentNode);
	if ((parentul.className == "menu") || (parentul.className == "menu1") || (parentul.className == "menu2")) return null;
	doubleparentul = ((parentul.parentElement) ? parentul.parentElement.parentElement : parentul.parentNode.parentNode);
	return doubleparentul;
}



function handleMouseOver(e) {
    var el = (e) ? e.target : event.srcElement;
    /*addTestText(" over:" + el.tagName + " ");
    if (!el.tagName) {
    	addTestText(" UNDEFINED:" + el.parentNode.tagName + " ");
    }*/
    //addTestText(" " + el.tagName + ":" + el.className + " ");
    var menuitem = isMenuItem(el);
    if (menuitem) {
    	
    //Removing id="current" from menu item which was current before this over (so previous mouse overed)
    //(I think this is not needed anymore - the same effect can be reached if you'll just place
    //.over style after .current style)
    if (currentVisible) {
    	clearCurrent();
    }
    
    //All needed for future work elements variables
	var vParentUL = null;
	var vParentLI = null;
	var vDoubleParentUL = null;
	var vHideParentUL = null;
	var vHideParentLI = null;
	var vHideDoubleParentUL = null;

	vParentUL = parentUL(menuitem);
	vParentLI = parentLI(menuitem);
	vDoubleParentUL = doubleParentUL(menuitem);
	
	//hideSub is menu item which was
	if (hideSub != null)
	{
		vHideParentUL = parentUL(hideSub);
		vHideParentLI = parentLI(hideSub);
		vHideDoubleParentUL = doubleParentUL(hideSub);
	}

	//If mouse were moved to the same menu element (so mouse out and mouse over is for same menu element - for example mouse out from a and mouse over to li)
	if (menuitem == hideSub)
	{
		clearTimeout(menuTime);
		addStyle(menuitem, "over");
		hideSub = menuitem;
	}
	//If there were no overed menu
	else if (hideSub == null)
	{
		addStyle(menuitem, "sub over");
	}
	//if Out and Over menu items are childs of the same UL
	else if (vParentUL == vHideParentUL)
	{
		clearTimeout(menuTime);
		closetype = 'current';
		removeStyle(hideSub, "sub");
		addStyle(menuitem, "sub over");
		hideSub = menuitem;
	}
	//If out menu item is parent of over menu item
	else if ((vParentLI != null) && (vParentLI == hideSub))
	{
		clearTimeout(menuTime);
		addStyle(menuitem, "sub over");
		hideSub = menuitem;
	}
	//If mouse out menu item was child of mouse overed menu item
	else if ((vHideParentLI != null) && (vHideParentLI == menuitem))
	{
		clearTimeout(menuTime);
		closetype = 'current';
		removeStyle(hideSub, "sub");
		addStyle(menuitem, "over");
		hideSub = menuitem;
	}
	//If out item is second level item and mouse is moved to some first level item
	else if ((vHideDoubleParentUL != null) && (vHideDoubleParentUL == vParentUL))
	{
		clearTimeout(menuTime);
		closetype = 'current';
		removeStyle(hideSub, "sub");
		removeStyle(vHideParentLI, "sub");
		addStyle(menuitem, "sub over");
		hideSub = menuitem;
	}
	else
	{
		clearTimeout(menuTime);
		closetype = 'all';
		removeStyle(hideSub, "sub");
	}	


    }
}

function handleMouseOut(e) {
    var el = (e) ? e.target : event.srcElement;

    /*addTestText(" out:" + el.tagName + " ");
    if (!el.tagName) {
    	addTestText(" UNDEFINED:" + el.parentNode.tagName + " ");
    }*/
    var toel = (e) ? e.relatedTarget : event.toElement;
    var menuitem = isMenuItem(el);

    if (menuitem && (menuitem != isMenuItem(toel))) {

        removeStyle(menuitem, "over");
        hideSub = menuitem;
		closetype = 'all';
        menuTime = setTimeout('removeStyle(hideSub, "sub")', 200);
    }
}

var flag;

function handleClick(e) {

    var el = (e) ? e.target : event.srcElement;
    var menuitem = isMenuItem(el);
    var shouldOpen = isShouldOpen(el);
    
   /* if (menuitem && el.tagName.toLowerCase() != "a") {
		neededA = menuitem.getElementsByTagName("a");
		if ( neededA.length != 0 ) {
			neededLink = neededA[0].getAttribute('href',2);
			neededLocation = neededA[0].getAttribute('target',2);
			if (neededLocation && neededLocation == "_blank")
			{
				window.open(neededLink,"","");
			} else {
				location.assign(neededLink);
			}
		}
	}
	*/
	
	if (shouldOpen) {                                      
		if (shouldOpen.getAttribute('actionvalue') == 'closed') {
			shouldOpen.nextSibling.style['display'] = 'block';
			shouldOpen.setAttribute('actionvalue', 'opened');
			if(flag) location.href=flag;
			shouldOpen.firstChild.firstChild.src = changeSrc(shouldOpen.firstChild.firstChild.src, 'open');
		} else {
			shouldOpen.nextSibling.style['display'] = 'none';
			shouldOpen.setAttribute('actionvalue', 'closed');
			shouldOpen.firstChild.firstChild.src = changeSrc(shouldOpen.firstChild.firstChild.src, 'close');
		}
	}
}

function hclick(title) {
	
	flag='#'+title;
	/*location.href='#'+title;*/
}

function handleLoad() {
   
    
    // This overcomes event-handling bug in menuitems in Mozilla
    if (navigator.appName.indexOf('Netscape') != -1) {
        var re = new RegExp("(^| )menu($| )");
        var re1 = new RegExp("(^| )menu1($| )");
        var re2 = new RegExp("(^| )menu2($| )");
        var menus = document.getElementsByTagName("ul");
        var mozMenuItem;
        
        for (var i = 0; i < menus.length; i++) {
            if ((menus[i].className.search(re) != -1) || (menus[i].className.search(re1) != -1) || (menus[i].className.search(re2) != -1)) {
                var menuItems = document.getElementsByTagName("li");
                for (var k = 0; k < menuItems.length; k++) {
                    mozMenuItem = document.createElement("div");
                    mozMenuItem.className = "moz";
                    menuItems[k].appendChild(mozMenuItem);
                }
            }
        }
    }
    
	//Adding validator to forms
	var forms = document.getElementsByTagName("FORM");
        for (var i = 0; i < forms.length; i++) {
            if (forms[i].className.indexOf("contact") != -1) {
		doShow(forms[i]);
            }
        }

}



function dPopup(url){
    newPopup = window.open(
        url,
        "",
        "height=300, width=400, menubar=no, resizable=yes, " +
        "scrollbars=no, status=no, titlebar=yes, toolbar=no, " +
        "left=" + (screen.availWidth/5*2) + ", " +
        "top=" + (screen.availHeight/5*1)
        );
}

function errorTrap() {
    return true;
}

function loadImage(imgName, imgWidth, imgHeight)
{
	imgWidth += 12;
	imgHeight += 15;
	var myt = "newWin = window.open(\"\", \"\", \"height="+ imgHeight + ", width=" + imgWidth + ",resizable=yes\")";
	eval(myt);
	newWin.document.write("<body style=\"padding: 0px; margin: 0px;\">");
	newWin.document.write("<table>");
	newWin.document.write("<tr>");
	newWin.document.write("<td style=\"background-color: #FFFFFF; padding: 5px;\">");
	newWin.document.write("<img src=\"" + imgName + "\" border=\"0\">");
	newWin.document.write("</td>");
	newWin.document.write("</tr>");
	newWin.document.write("</table>");
	newWin.document.write("</body>");
	newWin.document.close();
}


document.onmouseover = handleMouseOver;
document.onmouseout = handleMouseOut;
document.onclick = handleClick;
window.onload = handleLoad;
//window.onerror=errorTrap;
