/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setCheckboxes(the_form, do_check)
{
    var elts      = document.forms[the_form].elements['idarray[]'];
    var elts_cnt  = elts.length;
    var amount_element = document.getElementById('check_amount');
    
    if (amount_element && !isNaN(amount_element.value)) {
    	amount = Math.abs(parseInt(amount_element.value));
    	elts_cnt = Math.min(amount, elts_cnt);
    }

    for (var i = 0; i < elts_cnt; i++) {
        elts[i].checked = do_check;
    } // end for

    return true;
} // end of the 'setCheckboxes()' function


/**
 * popup window open function
 *
 * @param   url      url to open in popup window
 */
function open_window(url) { 
	mywin = window.open(url,'win','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=350,height=350');
}

/**
 * preview window open function
 *
 * @param   url      url to open in popup window
 */
function open_preview_sized(url,width,height) {
	width = Math.min(screen.availWidth, width);
	height = Math.min(screen.availHeight, height);
	mywin = window.open(url,'preview','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+width+',height='+height);
}

/**
 *
 */
function open_preview(url) {
	open_preview_sized(url, 830, 620);
}

/**
 * @param   url      url to open in popup window
 */
function confirm_delete(url) {
	if(confirm("Klicken Sie auf OK um den Eintrag definitiv zu entfernen.") == true)
		top.location.href=url;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function clearTextFields(the_form) {
	var myform = document.forms[the_form];
	for(var i=0;i<myform.length;++i) {
		if (myform.elements[i].type == 'text' || myform.elements[i].type == 'textarea')
			myform.elements[i].value = "";
	}
}

function disableSubmitButton() {
	document.getElementById("submit").disabled = true;
	return true;
}

function toggleVisibility(e, id2, id, type, dnames) {

	var x, y;
	var NNtype,IEtype,W3Ctype;
	
	if (type == 'visible') {
		NNtype = 'show';
		NNtypeO = 'hidden';

		IEtype = 'visible';
		IEtypeO = 'hidden';

		W3Ctype = 'visible';
		W3CtypeO = 'hidden';
	} else {
		NNtype = 'hidden';
		NNtypeO = 'hidden';
		IEtype = 'hidden';
		IEtypeO = 'hidden';
		W3Ctype = 'hidden';
		W3CtypeO = 'hidden';
	}

	// if ie
	if (document.all) {
		document.getElementById(id).style.visibility = IEtype;
		//eval("document.all." + id + ".style.visibility = \"" + IEtype + "\"");
		
		for(i=0; i<dnames.length && type == 'visible'; i++) {
			if (dnames[i] != id) {
				document.getElementById(dnames[i]).style.visibility = IEtypeO;
				//document.all.dnames[i].style.visibility = IEtypeO;
				//eval("document.all." + dnames[i] + ".style.visibility = \"" + IEtypeO + "\"");
			}
		}
		
		x = window.event.x;
		y = window.event.y - 15;
		//	alert(y + " : " + id2);
	//	document.all[id2].style.pixelTop = y;
		return true;
	}
	
	// if ns 4
	if (document.layers) {
		document.layers[id].visibility = NNtype;

		for(i=0; i<dnames.length && type == 'visible'; i++) {
			if (dnames[i] != id)
				document.layers[dnames[i]].visibility = NNtypeO;
		}

		x = e.pageX;
		y = e.pageY - 18;
		//alert(y + " : " + id2);
	//	document.layers[id2].top = y;
		return true;
	}

	// if ns 6
	if (document.getElementById?1:0 && parseInt(navigator.appVersion) >= 5) {
		eval("document.getElementById(id).style.visibility = \"" + W3Ctype + "\"");
		
		for(i=0; i<dnames.length; i++) {
			if (dnames[i] != id)
				eval("document.getElementById(dnames[i]).style.visibility = \"" + W3CtypeO + "\"");
		}

		x = e.pageX;
		y = e.pageY - 10;
		//alert(y + " : " + id2);
	//	document.getElementById(id2).style.top = y;
		return true;
	}
}

function calcCharLeft(target) {
	var maxLength = 300;
   
   	if (target.value.length > maxLength)
    {
      target.value = target.value.substring(0,maxLength);
      charleft = 0;
    }
    else
    {
      charleft = maxLength - target.value.length;
    }
    document.newMsg.msg_len.value = charleft;
 }

function checkChar() {
    var maxLength = 138;
    if (document.newMsg.text.value.length > maxLength)
    {
      document.newMsg.text.value = target.value.substring(0,maxLength);
      charleft = 0;
    }
    else
    {
      charleft = maxLength - document.newMsg.text.value.length;
    }
    document.newMsg.msg_len.value = charleft;
    setTimeout("checkChar()",200);
}

function setDisabled(objectID,value) {
	document.getElementById(objectID).disabled = value;
}

function setFocus(){
	if(document.getElementById("field0"))
		document.getElementById("field0").focus();
}

function validate(fields,myform) {
	for(i=0; i<fields.length; i++)
	{
		var myname = "form["+fields[i]+"]";
		
		if (!document.forms[myform]) {
			alert("Form '" + myform + "' not found.");
			return false;			
		}
		
		if (!document.forms[myform].elements[myname]) {
			alert("Element '" + myname + "' not found.");
			return false;
		}
		
		if (document.forms[myform].elements[myname] && document.forms[myform].elements[myname].value == "") {
			alert("Bitte alle markierten Felder ausfuellen!\nVeuillez remplir toutes les cases marquees!");
			document.forms[myform].elements[myname].focus();
			return false;
		} 
	}
	document.forms[myform].submit();
	return true;
}
 
 //<form name='newMsg'>
 //<textarea rows='8' cols='44' name='text' class='input' onChange='calcCharLeft(this)' onFocus='calcCharLeft(this)' onKeyUp='calcCharLeft(this)' onKeyDown='calcCharLeft(this)' onKeyPress='calcCharLeft(this)'></textarea>
 //<input value='300' size='3' name='msg_len' readonly class='input'>
