/** 
 *         Form validator
 *        (c) 2007 NEGESO
 *    Changed by Rostislav Brizgunov
 *
 * In all forms which should be validated
 * please add onSubmit="return validate(this)"
 * all forms with class="contact". Please
 * insert required="true" attribute-value pair
 * into such validated objects:
 * - required text field;
 * - fieldset, in which at least one checkbox
 *   should be checked.
 * 
 * 
 * Example:
 * <form class="contact">
 *     <input type="text" required="true">
 *     <fieldset required="true">
 *         <input type="checkbox">
 *         <input type="checkbox">
 *     </fieldset>
 *     <input type="submit" onclick="if (!validate(this.form)) return false;">
 * </form>
 */

function doShow(form) {
    form.style.display = "block";
} 

//=====================================Menu Function=============================================
var menuTime;   // Timer identifier used to hide submenus
var hideSub = null;    // 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

//This function add some test data to title (convenient for testing)
function addTitle(text)
{
  if (document.title.length > 55) {
    document.title = '';
  }                                 
  document.title += ' ' + text + ' ';
  
}


/* check if obj is menu item or its contents (oblect if true, null if false) */
function isMenuItem(obj) {
    while (obj.nodeType==1 && obj.tagName.toLowerCase() != "ul") {
        if (obj.tagName.toLowerCase() == "li") return obj;
        obj = (obj.parentElement) ? obj.parentElement : obj.parentNode;
    }
    return false;
}

/* 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 (re.test(cn)) return false;
    if (re1.test(cn)) return false;
    if ((re2.test(cn)) && ((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;
	if(obj.parentNode.id=='site_menu') {
		var classN = obj.parentNode.className.replace(/menu /,"");
		if (classN=='menu') classN = 'bg_fon0';
		lastulli(obj,classN);
	}
}
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")) {
          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")) 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")) return null;
  doubleparentul = ((parentul.parentElement) ? parentul.parentElement.parentElement : parentul.parentNode.parentNode);
  return doubleparentul;
}

function handleMouseOver(e) {
    var el = (e) ? e.target : event.srcElement;
    var menuitem = isMenuItem(el);
    if (menuitem) {
  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);
  if (hideSub != null)
  {
    vHideParentUL = parentUL(hideSub);
    vHideParentLI = parentLI(hideSub);
    vHideDoubleParentUL = doubleParentUL(hideSub);
  }

  if (menuitem == hideSub)
  {
    clearTimeout(menuTime);
    addStyle(menuitem, "over");
    hideSub = menuitem;
  }
  else if (hideSub == null)
  {
    addStyle(menuitem, "sub over");
  }

  else if (vParentUL == vHideParentUL)
  {
    clearTimeout(menuTime);
    closetype = 'current';
    removeStyle(hideSub, "sub");
    addStyle(menuitem, "sub over");
    hideSub = menuitem;
  }
  else if ((vParentLI != null) && (vParentLI == hideSub))
  {
    clearTimeout(menuTime);
    addStyle(menuitem, "sub over");
    hideSub = menuitem;
  }
  else if ((vHideParentLI != null) && (vHideParentLI == menuitem))
  {
    clearTimeout(menuTime);
    closetype = 'current';
    removeStyle(hideSub, "sub");
    addStyle(menuitem, "over");
    hideSub = menuitem;
  }
  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;
    var menuitem = isMenuItem(el);
    if (menuitem) {
        removeStyle(menuitem, "over");
        hideSub = menuitem;
		closetype = 'all';
        menuTime = setTimeout('removeStyle(hideSub, "sub")', 200);
    }
}

function handleClick(e) {
    var el = (e) ? e.target : event.srcElement;
    var menuitem = isMenuItem(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);
      }
    }
  }
}

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 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)) {
                var menuItems = menus[i].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, imgTitle)
{
	if (typeof(imgTitle)=="undefined" || imgTitle==null)
		imgTitle = "";
	imgWidth += 12;
	imgHeight += 15;
	var myt = "newWin = window.open(\"\", \"\", \"height="+ imgHeight + ", width=" + imgWidth + ",resizable=yes\")";
	eval(myt);
	newWin.document.write("<html>");
	newWin.document.write("<head>");
	if (imgTitle!="") {
		newWin.document.write("<title>");
		newWin.document.write(imgTitle);
		newWin.document.write("</title>");
	}
	newWin.document.write("</head>");
	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.write("</html>");
	newWin.document.close();
}

function loadFlash(swfName, swfWidth, swfHeight)
{
  var myt = "newWin = window.open(\"\", \"\", \"height="+ swfHeight + ", width=" + swfWidth + ",resizable=no\")";
  eval(myt);
  newWin.document.write("<body style=\"padding: 0px; margin: 0px;\">");
  newWin.document.write("<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\" WIDTH=\"" + swfWidth + "\" HEIGHT=\""+ swfHeight + "\" id=\"animation\">");
  newWin.document.write("<PARAM NAME=\"movie\" VALUE=\"media/" + swfName + "\">");
  newWin.document.write("<PARAM NAME=\"quality\" VALUE=\"high\">");
  newWin.document.write("<PARAM NAME=\"bgcolor\" VALUE=\"#FFFFFF\">");
  newWin.document.write("<EMBED src=\"media/" + swfName + "\" quality=\"high\" bgcolor=\"#FFFFFF\" WIDTH=\"" + swfWidth + "\" HEIGHT=\""+ swfHeight + "\" NAME=\"animation\" ALIGN=\"\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">");
  newWin.document.write("</EMBED>");
  newWin.document.write("</OBJECT>");
  newWin.document.write("</body>");
  newWin.document.close();
}


function menuInit(){
	try{document.getElementById('site_menu').onmouseover = handleMouseOver;}
	catch(err){alert('over: no menu');};
	
	try{document.getElementById('site_menu').onmouseout = handleMouseOut;}
	catch(err){alert('out: no menu');};
	
	try{document.getElementById('site_menu').onclick = handleClick;}
	catch(err){alert('click: no menu');};

	if (document.getElementById('site_menu1')){
		try{document.getElementById('site_menu1').onmouseover = handleMouseOver;}
		catch(err){alert('over: no menu');};
	
		try{document.getElementById('site_menu1').onmouseout = handleMouseOut;}
		catch(err){alert('out: no menu');};
	
		try{document.getElementById('site_menu1').onclick = handleClick;}
		catch(err){alert('click: no menu');};
	}
}

function lastulli(obj,classN){
	var obj_ul= obj.getElementsByTagName('ul');
//	alert(classN);
	if(obj_ul[0]){ 
		browserType();
		browserVersion();
		if (( (browserVersion() == "5.0")||(browserVersion() == "5.5")||(browserVersion() == "6.0") ) & (browserType() == "MSIE")){
			var ListLi = obj_ul[0].childNodes;
			var i = 0;
			while(i < ListLi.length) {
				ListLi[i].style.margin="0px;";
				i++;
			}
		}
		obj_ul[0].className=classN;
		numClass = obj_ul[0].className.replace(/bg_fon/,"");
//		alert(numClass);
		obj_li=obj_ul[0].lastChild;
		obj_li.style.backgroundImage="url('images/bg-li-last"+numClass+".gif')";
		obj_li.style.backgroundPosition="bottom left";
		obj_li.style.backgroundRepeat="no-repeat";
		obj_li.style.width="165px";
		obj_li.style.margin="0px";
		obj_li.style.padding="6px 21px 6px 21px;";
	}
}


String.prototype.contains = function (pattern) {
	if (this.indexOf(pattern) != -1) return true;
	else return false;
}

function browserType() {
	var agent = navigator.userAgent;
	if (agent.contains("MSIE")) return "MSIE";	
	else return "Unknown";
}

function browserVersion() {
	var agent = navigator.userAgent;
	if (agent.contains("MSIE")) return agent.substring(agent.indexOf("MSIE") + 5, agent.indexOf("MSIE") + 8);	
	else return "Unknown";
}