
function getElement(id) {
	return document.getElementById ? document.getElementById(id) : document.all[id];
}

function setVisibility(id, visible) {
	getElement(id).style.display = visible ? "block" : "none";
}

function setVisibilityAndFocus(id, visible) {
	var field = getElement(id);
	field.style.display = visible ? "block" : "none";
	if (visible && field.focus) field.focus();
}

function toggle(id) {
	var field = document.getElementById(id);
	var show = field.style.display=="none";
	field.style.display = show ? "block" : "none"; 
}

function trackDigits(textInput) {
	if (/[^0-9]/.test(textInput.value)) {
		textInput.value = textInput.value.replace(/[^0-9]*/g, '');
	}
}

function limitDigis(textInput) {
	if (window.event) {
		var key = document.layers ? window.event.which : window.event.keyCode;
		if (key < 48 || key > 57) { // is not digit 
			window.event.returnValue = false;
			return false;
		}
	}
	return true;
}

function trackTextLength(textInput, maxLength) {
  if (textInput.value.length > maxLength) {
    textInput.value = textInput.value.substring(0, maxLength);
  }
}
	
function limitTextLength(textInput, maxLength) {
  
  var result = true;
  if (textInput.value.length > maxLength) {
  	result = false;
  }
  
  if (window.event) {
  	window.event.returnValue = result;
  }
  
  return result;
}

function submitFormOnReturnKeyPress(form, event) {
	if ((document.layers ? event.which : event.keyCode)==13) {
		form.submit();
	}
}

function popup(link, name, w, h, options) {
	getPopup(link, name, w, h, options);
}


function setSimpleCookie(id, value) {
	var oneYearFromNow = new Date();
	oneYearFromNow.setTime(oneYearFromNow.getTime() + 365 * 24 * 60 * 60 * 1000);
	setCookie(id, value, oneYearFromNow);							
}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function gotoPage(link) {
	window.location.href = link;
}

function displayBigImage() {
	var div = document.getElementById('bigImageDiv');
	div.style.top=getScrollY() + 90;
	Effect.Grow('bigImageDiv');
}

function idisplayPresentation(title, contentUrl){
	window.parent.displayPresentation(title, contentUrl);
}

var lastTop=0;
var lastHeight=0;
var lastWidth=0;

function displayPresentation(title, contentUrl, notAnimation, w, h){
	if(!w){
		w = getWidth()-60;
	}
	if(!h){
		h = getHeight()-60;
	} 
	var top = (getHeight() - h)/2;
	var left = (getWidth() - w)/2;
	
	var contentFrame = getElement('contentPresentation');
	contentFrame.src = '';
	var div = getElement('displayPresentation');
	div.style.display = "none";
	var titleTd = getElement('namePresentation');
	titleTd.innerHTML = "<p><b>" + title+ "</b></p>";
	div.style.left = left;
	lastTop = top + getScrollY();
	div.style.top = lastTop;
	lastWidth = w;
	div.style.width = lastWidth;
	lastHeight = h ;
	div.style.height = lastHeight;
	isMaxim = false;
	contentFrame.src = contentUrl;
	if(notAnimation){
		div.style.display = "";
	}else{
		Effect.Grow('displayPresentation');
	}
}

var isMaxim = false;

function closePresentation(){
	getElement('contentPresentation').src=''; 
	new Effect.Shrink('displayPresentation');
}

function maxminPresentation(){
	var contentFrame = getElement('contentPresentation');
	var url = contentFrame.src;
	if(url.indexOf("children")>-1){
		contentFrame.src = '';
		contentFrame.src = url;
	}
	var div = getElement('displayPresentation');
	if(isMaxim){
		div.style.left = 30;
		div.style.top = lastTop;
		div.style.width = lastWidth;
		div.style.height = lastHeight;
		isMaxim = false;
	}else{
		div.style.left = 0;
		div.style.top = getScrollY();
		div.style.width = getWidth();
		div.style.height = getHeight();
		isMaxim = true;
	}
}

function getWidth(){
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
	return myWidth;
}

function getHeight(){
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getScrollY(){
	var scrollY=0;
	if( typeof(window.pageYOffset)=='number'){
		scrollY = window.pageYOffset;
	}else if(document.body && document.body.scrollTop){
		scrollY = document.body.scrollTop
	}else if(document.documentElement && document.documentElement.scrollTop){
		scrollY = document.documentElement.scrollTop;
	}
	return scrollY;
}

function resize100Height(f){
	f.style.height=800-parseInt(f.offsetTop);
}

function openTreeNode(id){
	if(id=='' || id=='-1') return;
	var f = window.parent.frames['tree'];
	if(f){
		f.openNode(id);
	}
}

function redirect(cursId, chapter, menu){
	if(window.parent.document.getElementById('search')==null){
		var url = "index.htm?action=viewTree";
		if(chapter!=null){
			url =url+ "&chapter=" +chapter;
		}
		if(menu!=null){
			url =url+ "&menu=" +menu;
		}
		
		window.location= url;
	}
}

function redirectOlimp(olimpId, menu){
	if(window.parent.document.getElementById('search')==null){
		var url = "index.htm?action=listOlympiad";
		if(olimpId!=null){
			url =url+ "&catId=" +olimpId;
		}
		if(menu!=null){
			url =url+ "&menu=" +menu;
		}
		
		window.location= url;
	}
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }


var alreadyDisplayed = false;
var countryVar = "";
var dontClose = false;
function displayCountries(lastUrl){
	if(!alreadyDisplayed){
		var div = getElement('countryDiv');
		Effect.BlindDown('countryDiv');
		alreadyDisplayed = true;
	}else{
		if(countryVar.indexOf("group_")==0){
			toggle(countryVar);
			dontClose = true;
		}else{
			alreadyDisplayed = false;
			var div = getElement('countryDiv');
			div.style.display='none';
			if(countryVar.length>1){
				if(lastUrl.indexOf("&country=")>-1){
					lastUrl = lastUrl.substring(0, lastUrl.indexOf("&country="));
				}
				if(lastUrl.indexOf("?country=")>-1){
					lastUrl = lastUrl.substring(0, lastUrl.indexOf("?country="));
				}
				if(lastUrl.indexOf("?")>-1){
					window.location= lastUrl + "&country=" + countryVar;
				}else{
					window.location= lastUrl + "?country=" + countryVar;
				}
			}
		}
		
	}
}

function closeCombos(){
	if(dontClose) {
		dontClose = false;
		return;
	}
	var div = getElement('countryDiv');
	if(div!=null && div.style.display!='none'){
		div.style.display='none';
		alreadyDisplayed = false;
	}
}

function deleteClass(id){
	window.location = "/classes.htm?action=deleteClass&id=" + id;
}

function alertMenu(){
	changeColor();
}

function changeColor(){
	if(elems){
		for(var i=0; i<elems.length; i++){
			var el = getElement(elems[i]);
			if(el){
				if(el.color=='#ff0000'){
					el.color="#ffffff";
				}else{
					el.color="#ff0000";
				}
			}
		}
		var m = "changeColor()";
		setTimeout(m, 500);
	}
}

function submitLogin(event) {
	if((document.layers ? event.which : event.keyCode)==13) {
		getElement("loginForm").submit();
	}
}

function changeNbExercise(){
	var nbExSel = getElement('nbcId');
	var sel = getElement('cId');
	var o = sel.options[sel.selectedIndex];
	var nb = parseInt(o.getAttribute('nb'));
	for(var i=0; i<nbExSel.options.length; i++){
		nbExSel.options[0] = null;
	}
	
	for(var i=1; i<11; i++){
		nbExSel.options[i-1] = new Option(nb*i, nb*i);
	}
}

function redimensionTable(tableId){
	var t = getElement(tableId);
	if(t!=null){
		var w = getWidth();
		t.style.height = w*(64/80);
	}
}