// Confirm action
function confirmAction(message,action) {
	if (confirm(message)) {
		document.location = action;
	}
}

// Do action
function doAction(theform,theaction) {
	strTemp = eval('document.' + theform);
	strTemp.action = theaction;
	strTemp.submit();
}

// Verify characters. Only numbers, alphabetic characters, dots, minus sign and return are allowed.
function verifyCharactersForNames() {
	if (!((window.event.keyCode>=97 && window.event.keyCode<=122) || (window.event.keyCode>=65 && window.event.keyCode<=90) || window.event.keyCode==42 || window.event.keyCode==45 || window.event.keyCode==8 || window.event.keyCode==127 || window.event.keyCode==13 || (window.event.keyCode>=48 && window.event.keyCode<=57)))	{
		window.event.keyCode=0;	
	}
}

// Open popup window
function openWin(location,windowname,props) {
	window.open(location,windowname,props);
}

// Popup for glossary reference
function popupjump(refid)	{
	reference = document.getElementById("ref" + refid).value;
	if(refid != 'new')	{
		openWin('/engine/popGlossaryList.asp?id=' + reference + '&ref=' + refid + '#' + reference + '','GlossaryPop','width=500,height=350,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
	}
	else	{
		openWin('/engine/popGlossaryList.asp','GlossaryPop','width=500,height=350,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
	}
}

// Initialize current window (ref event)
function initializeWindow()	{
	if (self.init) self.init();
}

// Context menu
function showChapterContext(ID,event) {
	document.frmChapterDetail.ID.value = ID;
	
	var menuobj = document.getElementById('chapterContext');
	var rightedge = document.body.clientWidth-event.clientX;
	var bottomedge = document.body.clientHeight-event.clientY;
	
	menuobj.innerHTML = menuobj.innerHTML.replace(/\[IDX\]/g,ID);
	
	if (rightedge < menuobj.offsetWidth)	{
		menuobj.style.left = document.body.scrollLeft+event.clientX-menuobj.offsetWidth;
	}
	else	{
		menuobj.style.left = document.body.scrollLeft+event.clientX;
	}
	if (bottomedge < menuobj.offsetHeight)	{
		menuobj.style.top = (document.body.scrollTop+event.clientY)-menuobj.offsetHeight;
		menuobj.style.visibility = "visible";
	}
	else	{
		menuobj.style.top = document.body.scrollTop+event.clientY;
		menuobj.style.visibility = "visible";
	}
	return false;
}

// Hide context menu
function hideChapterContext(e) {
	if (!e) e = window.event;
	document.getElementById('chapterContext').style.visibility = "hidden";
}

// Disable browser context menu
function disableContext(e) {
	if (!e) e = window.event;
	return false;
}

// Enable form dropdown
function enableDropdown(fieldname,defaultval)	{
	document.getElementById(fieldname).disabled = false;
	document.getElementById(fieldname).value = defaultval;
}

// Disable form dropdown
function disableDropdown(fieldname)	{
	document.getElementById(fieldname).disabled = true;
	document.getElementById(fieldname).value = "0";
}

// Generate random string
function randomString(length,fieldname) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = length;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	document.getElementById(fieldname).value = randomstring;
	alert("Het random gegenereerde wachtwoord is: " + randomstring);
}

// Toggle visibility
function toggleVisibility(fld)	{
	var cnt = document.getElementById(fld);
	var img = document.getElementById(fld + 'img');
	if (cnt.style.display == 'block') {
		cnt.style.display = 'none';
		img.src = '/imgs/common/plus.gif';
	}
	else	{
		cnt.style.display = 'block';
		img.src = '/imgs/common/minus.gif';
	}
}

// Open all
function openAll()	{
	var cnt1 = document.getElementById("pgq");
	var img1 = document.getElementById("pgqimg");
	
	var cnt2 = document.getElementById("chq");
	var img2 = document.getElementById("chqimg");
	
	var cnt3 = document.getElementById("pgqo");
	var img3 = document.getElementById("pgqoimg");
	
	var cnt4 = document.getElementById("chqo");
	var img4 = document.getElementById("chqoimg");
	
	cnt1.style.display = 'block';
	img1.src = '/imgs/common/minus.gif';
	
	cnt2.style.display = 'block';
	img2.src = '/imgs/common/minus.gif';
	
	cnt3.style.display = 'block';
	img3.src = '/imgs/common/minus.gif';
	
	cnt4.style.display = 'block';
	img4.src = '/imgs/common/minus.gif';
}

// Select checkboxes randomly
function uncheckAll() {
	for (i = 0; i < arr.length; i++) {
		document.getElementById(arr[i]).checked = false ;
	}
}
function checkRandom() {
	msgIndex = 0;
	uncheckAll();
	rndAmount = document.getElementById('selectRandom').value;
	if (eval(rndAmount) > eval(arr.length)) {
		rndAmount = arr.length
	}
	if (rndAmount != "") {
		do {
			rndNumber = Math.floor(Math.random()*arr.length);
			if (document.getElementById(arr[rndNumber]).checked == false) {
				document.getElementById(arr[rndNumber]).checked = true;
				msgIndex++;
			}
		}
		while (msgIndex != rndAmount)
	}
}

// Redirect dropdown
function dropRedirect(form,url) { 
var index = form.select.selectedIndex;
if (form.select.options[index].value != "0")	{
	location.href = url + form.select.options[index].value;
	}
}

// Checks if value is alphanumeric
function letternumber(e)
{
var key;
var keychar;
if (window.event)
 key = window.event.keyCode;
else if (e)
 key = e.which;
else
 return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
// control keys
if ((key==null) || (key==0) || (key==8) || 
 (key==9) || (key==13) || (key==27) )
 return true;
// alphas and numbers
else if ((("abcdefghijklmnopqrstuvwxyz0123456789").indexOf(keychar) > -1))
 return true;
else
 return false;
}

// Move items between select boxes
function moveSelect(from,to) {
fromList = document.getElementById(from);
toList = document.getElementById(to);

  if (toList.options.length > 0 && toList.options[0].value == 'temp')
  {
    toList.options.length = 0;
  }
  var sel = false;
  for (i=0;i<fromList.options.length;i++)
  {
    var current = fromList.options[i];
    if (current.selected)
    {
      sel = true;
      if (current.value == 'temp')
      {
        alert ('You cannot move this text!');
        return;
      }
      txt = current.text;
      val = current.value;
      toList.options[toList.length] = new Option(txt,val);
      fromList.options[i] = null;
      i--;
    }
  }
  if (!sel) alert ('You haven\'t selected any options!');
}

//Select all list items
function allSelect(fld)
{
  List = document.getElementById(fld);
  if (List.length && List.options[0].value == 'temp') return;
  for (i=0;i<List.length;i++)
  {
     List.options[i].selected = true;
  }
}

//Check required form fields
function checkForm(frm) { 
  if(!checkRequired(frm)) { 
	  	document.getElementById("frmerror").style.display = 'block';
		return false; 
  } 
  else { 
    document.forms[0].submit();   
  } 
} 

function checkRequired(frmData) { 
  var bFail; 
  bFail = false;  
  for(iElement = 0; iElement < frmData.elements.length; iElement++) { 
  	if(frmData.elements[iElement].disabled == true)	{
		frmData.elements[iElement].disabled = false;
	}
    if(frmData.elements[iElement].className == 'frmrequired' || frmData.elements[iElement].className == 'frmrequirederror') { 
      if(frmData.elements[iElement].value == '') { 
        bFail = true; 
        frmData.elements[iElement].className = 'frmrequirederror'; 
      } else { 
        frmData.elements[iElement].className = 'frmrequired'; 
      } 
    } 
  } 
  return !bFail; 
} 

function isDate(mm,dd,yyyy) {
   var d = new Date(mm + "/" + dd + "/" + yyyy);
   return d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy;
}

