var local=
    {
        "T[1]":"Имя",
        "T[2]":"E-Mail",
        "T[3]":"Тема",
        "T[4]":"Сообщение"
    };
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
function confdel(url,cnf) {    
	if(!cnf) {
		cnf="Вы действительно хотите удалить этот элемент? Это действие не может быть отменено позднее!";
	}
	if (confirm(cnf)) {
		location.href=url;
	}
}
function dateChanged(calendar) {
  // Beware that this function is called even if the end-user only
  // changed the month/year.  In order to determine if a date was
  // clicked you can use the dateClicked property of the calendar:
  if (calendar.dateClicked||calendar.TodayClicked) {
    var y = calendar.date.getFullYear();
    var m = calendar.date.getMonth()+1;     // integer, 0..11
    var d = calendar.date.getDate();      // integer, 1..31
    // redirect...
    window.location = "news-y-" + y + "-m-" + m + "-n-" + d + ".html";
  }
}

function getelementbyid(myid) {
   if (isNS4){
        objElement = document.layers[myid];
     }else if (isIE4) {
        objElement = document.all[myid];
     }else if (isIE5 || isNS6) {
             objElement = document.getElementById(myid);
     }
return(objElement);
} 

function xlshow(id)
 {
  el=getelementbyid(id);
  if(el) {
    el.style.display='block';
  }
 }
function xlshowhide(id)
 {
  el=getelementbyid(id);
  if(typeof el == undefined) { return ;}
  if((el.style.display=='block')||(el.style.display=='')) {
    xlhide(id);
  }
  else {
    xlshow(id);
  }
 }
function xlhide(id)
 {
  el=getelementbyid(id);
  if(typeof el == undefined) { return ;}
  if(el) {
    el.style.display='none';
  }
}       

function keyup_team(inp) {
	str="";
	orig_len=inp.value.length;
	for (i=0;i<orig_len;i++) {
		if(((inp.value.charAt(i)>='a')&&(inp.value.charAt(i)<='z'))||((inp.value.charAt(i)>='A')&&(inp.value.charAt(i)<='Z'))||((inp.value.charAt(i)>='0')&&(inp.value.charAt(i)<='9'))||(inp.value.charAt(i)==' ')||(inp.value.charAt(i)=='_')||(inp.value.charAt(i)=='-')) {
			str+=inp.value.charAt(i);
		}
	}
	if(orig_len!=str.length) {
		inp.value=str;
	}
}

 function validateEmailForm(form,x,zerrdiv) {
   zerrdiv.innerHTML="";
 	 emlchk=emailCheck(form.elements[1].value);
 	 if(emlchk!='') {
 	 	emlchk='<img src="images/errdiv.gif" alt="Error"> '+emlchk+'<br>';
 	 }
   var gut=emlchk=='';
   var e=emlchk;
   for (var i = 0;i<form.elements.length;i++) {
     y=form.elements[i];
     if((y.value.length==0)&&(in_array(y.name,x))) {
       gut=false;
       e+='<img src="images/errdiv.gif" alt="Error"> Не заполнено обязательное поле "'+local[y.name]+'"<br>';
       y.style.background="#FFA0A0";
     }
     else {
       y.style.background="#FFFFFF";
     }
   }
   if(!gut) {
     zerrdiv.style.padding='5px';
     zerrdiv.style.border='1px #d8d885 solid';
     zerrdiv.innerHTML=e;
     window.scroll(0,150);
   }
   return gut && captcha_gut;
  }

 function in_array(the_needle, the_haystack){
        var the_hay = the_haystack.toString();
        if(the_hay == ''){
            return false;
        }
        var matched=0;
        for(i=0;i<the_haystack.length;i++) {
          if('T['+the_haystack[i]+']'==the_needle) {
            matched=1;
            break;
          }
        }
        return matched;
    }

function CheckAll(theForm, cName) 
 {
  for (i=0,n=theForm.elements.length;i<n;i++)
   {
    if (theForm.elements[i].name.indexOf(cName) !=-1)
     {theForm.elements[i].checked = true;}
//    alert(theForm.elements[i].) 
   } 
}

function CheckNone(theForm, cName) {
for (i=0,n=theForm.elements.length;i<n;i++)
if (theForm.elements[i].name.indexOf(cName) !=-1)
theForm.elements[i].checked = false;
}

function UncheckAllButMe(theForm, cName,Me) {
for (i=0,n=theForm.elements.length;i<n;i++)
if ((theForm.elements[i].name.indexOf(cName) !=-1)&&(theForm.elements[i]!=Me))
theForm.elements[i].checked = false;
}

function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|eu|asia)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

return "Неверный адрес E-Mail (проверьте @ и точки)";
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
return "Имя пользователя в E-Mail содержит некорректные символы";
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
return "Доменное имя в E-Mail содержит некорректные символы";
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

return "Имя пользователя в E-Mail содержит некорректные символы";
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
return "Destination IP address is invalid!";
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
return "Доменное имя в E-Mail ошибочно";
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
return "Адрес E-Mail должен содерждать корректный домен или двухбуквенный код страны.";
}

// Make sure there's a host name preceding the domain.

if (len<2) {
return "В адресе E-Mail отсутствует домен!";
}

// If we've gotten this far, everything's valid!
return '';
}

function toggleMenu(mmenu, itemId, level) {
	var tablNode=$($(mmenu).parentNode.parentNode.parentNode);
	tablNode.toggleClassName("active");
	if($('submenu'+itemId)) {
		if(!$('submenu'+itemId).hasClassName('loaded')) {
			// preload
			$('submenu'+itemId).show();
			$('submenu'+itemId).innerHTML="<div class='mc'><br><br><img src='images/loading.gif' alt='Loading' /><br><br></div>";
//			Effect.SlideDown("submenu"+itemId, {duration: 1});
			new Ajax.Updater('submenu'+itemId, 'ajaxmenu.php', {'parameters': {'itemId': itemId, 'level': level}, 'method': 'get', onComplete: function() {
				// finally toggle it
				$('submenu'+itemId).addClassName('loaded')
				Effect.SlideDown("submenu"+itemId, {duration: 1});
			}})
		}
		else {
			if(tablNode.hasClassName('active')) {
				Effect.SlideDown("submenu"+itemId, {duration: 1});
			}
			else {
				Effect.SlideUp("submenu"+itemId, {duration: 1});
			}
		}
	}
	return false;
}
