// check for DOM
var supportDOM = (document.getElementsByTagName && document.createElement);
// all other functions

function returnLayer(_id) {
  if (document.getElementById) {
    thisLayer = document.getElementById(_id);
  } else if (document.all) {
    thisLayer = document.all[_id];
  } else if (document.layers) {
    thisLayer = document.layers[_id];
  } else {
    thisLayer = false;
  }
  return thisLayer;
}
function toggleInnerTbl(_id) {
	_target = returnLayer('btable_'+_id);
	if (_target) {
		_image = returnLayer('bimage_'+_id);
		if (_target.style.display == "none") {
			_target.style.display = "";
			_action = "collapse";
			_image.src = _image.src.replace(/expand/g, _action);
			_image.title = 'Cacher';
		} else {
			_target.style.display = "none"
			_action = "expand";
			_image.src = _image.src.replace(/collapse/g, _action);
			_image.title = 'Montrer';
		}
	}
	return false;
}
function validateFields() {
	validForm = true;
	firstError = null;
	var x = document.forms[0].elements;	
	for (var i=0; i<x.length; i++) {
		objname = x[i].name;
		objtype = x[i].nodeName.toLowerCase();
		if ((objtype == 'input' || objtype == 'select' || objtype == 'textarea') && objname.indexOf('req_') != -1 ) {
			if (x[i].value == "") {
				writeError(x[i]);
			}
		}
	}	
	if (firstError) {
		alert("Invalid data!\n Please fill in all required fields");
		firstError.focus();
	}
	if (!validForm) {
		return false;
	}
}

function writeError(obj) {
	validForm = false;
	if (obj.hasError) return;
	if (supportDOM) {
		obj.style.borderColor = 'red';
			/*
		obj.className += ' error';
		obj.parentNode.className += ' error';
		obj.onchange = removeError;
			*/
		obj.hasError = true;
	} else {
		obj.hasError = true;
	}
	if (!firstError)
		firstError = obj;
}
function removeError() {
	this.className = this.className.substring(0, this.className.lastIndexOf(' '));
	this.parentNode.className = this.parentNode.className.substring(0, this.parentNode.className.lastIndexOf(' '));
	this.hasError = null;
	this.onchange = null;
}

function mailFormAction() {
	document.getelementById('mailform').action = 'interface.php?action=manage&sec=mail';
	document.getelementById('mailform').submit;
}



//define custom in_array function (in js it deoes not exist)
Array.prototype.in_array = function ( obj ) {
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ ) {
		if ( this[x] == obj ) return true;
	}
	return false;
}

//myValue accepts the text string to add
function insertText(myFieldName, myValue) {
	//underline, bold, italic
	var exceptions = Array("u", "i", "b");
	var myField = document.getElementById(myFieldName);
	//IE support
	if (document.selection) {
		myField.focus();
		
		//in effect we are creating a text range with zero
		//length at the cursor location and replacing it
		//with myValue
		sel = document.selection.createRange();						
		if(exceptions.in_array(myValue)){			
			sel.text = "[" + myValue + "]" + sel.text + "[/" + myValue + "]";
		}		
		else{
			sel.text = myValue;
		}
	}
	
	//Mozilla/Firefox/Netscape 7+ support
	else if (myField.selectionStart || myField.selectionStart == '0') {
	
		//Here we get the start and end points of the
		//selection. Then we create substrings up to the
		//start of the selection and from the end point
		//of the selection to the end of the field value.
		//Then we concatenate the first substring, myValue,
		//and the second substring to get the new value.
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		if(exceptions.in_array(myValue)){			
			myField.value = myField.value.substring(0, startPos) + "[" + myValue + "]" + myField.value.substring(startPos, endPos) + "[/" + myValue + "]" + myField.value.substring(endPos, myField.value.length);
		}
		else{
			myField.value = myField.value.substring(0, startPos) + " " + myValue + " " + myField.value.substring(endPos, myField.value.length);
		}
	
	} else {
		myField.value += myValue;
	}
}
