// JScript File

//========================================================================
//Functions used for helpfader
//========================================================================
function ShowHelp(obj, strHelp) {
	document.all.divHelpText.innerHTML = "<TABLE CELLSPACING=0 CELLPADDING=0><TR><TD VALIGN=TOP><IMG SRC='/images/help.gif'></TD><TD><TABLE ID=tblFader CLASS='Lokaalreservering' STYLE='WIDTH:150px;BORDER-RIGHT: black thin solid; BORDER-TOP: black thin solid; BORDER-LEFT: black thin solid; COLOR: black;    BORDER-BOTTOM: black thin solid;' CELLSPACING=0 CELLPADDING=3 BORDER=0><TR><TD><FONT COLOR=White><B><U>Aanvullende informatie</U><BR>" + strHelp + "</B></FONT></TD></TR></TABLE></TD></TR></TABLE>";
	document.all.divHelpText.style.pixelTop = obj.offsetTop;
	document.all.divHelpText.style.pixelLeft = obj.offsetLeft + obj.offsetWidth + 10;
	document.all.divHelpText.style.visibility = "visible";
	BgFade(0xff,0xff,0xff, 0x00,0x00,0xff, 20);
}

function HideHelp(obj) {
	document.all.divHelpText.style.visibility = "hidden";
}

function BgFade(red1, grn1, blu1, red2, grn2, blu2, steps) {
	sred = red1; sgrn = grn1; sblu = blu1;
	ered = red2; egrn = grn2; eblu = blu2;
	inc = steps;
	step = 0;
	RunFader();
}

function RunFader() {
	var epct = step/inc;
	var spct = 1 - epct;

	document.all.tblFader.bgColor = Math.floor(sred * spct + ered * epct) * 256 * 256 + Math.floor(sgrn * spct + egrn * epct) * 256 + Math.floor(sblu * spct + eblu * epct);

	if (step < inc) {
		setTimeout('RunFader()', 50);
	}
		step++;
}
//========================================================================

function IsValidDate(strDate){
var strDate;
var strDateArray;
var strDay = "";
var strMonth = "";
var strYear = "";
var intday;
var intMonth;
var intYear;
var booFound = false;
var strSeparatorArray = new Array("-","/",".");
var intElementNr;
var err = 0;

	if (strDate.length < 1)
		return true;

	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}

	if (booFound == false) {
		return false
		/*if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
	   }*/
	}

	if (strYear.length == 2)
		strYear = '20' + strYear;

	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}

	intMonth = parseInt(strMonth, 10);

	intYear = parseInt(strYear, 10);

	if (intYear < 1900 || intYear > 3000) {
		err = 4;
		return false;
	}

	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}

	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}

	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}

	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}
return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00'
              : ( (mnt*10 == Math.floor(mnt*10)) ?
                       mnt + '0' : mnt);
}

function CheckDate(source) {

	var strDate="";
	var intLen=0;
	var currentdate=new Date();

	strDate = source.value;
	intLen = source.value.length;

	if (intLen != 0) {
		if (intLen <= 5)
			strDate += "-" + currentdate.getYear()
		if (strDate.indexOf("-", 3) != -1 && strDate.substring(strDate.indexOf("-", 3) + 1).length == 2) {
			if (strDate.substring(strDate.indexOf("-", 3) + 1) < 30)
				strDate = strDate.substring(0, intLen - 2) + "20" + strDate.substring(strDate.indexOf("-", 3) + 1);
			else
				strDate = strDate.substring(0, intLen - 2) + "19" + strDate.substring(strDate.indexOf("-", 3) + 1);
		}
	}

	source.value = strDate;
}

//Remove leading blanks from our string.
function LTrim(str) {
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

//Remove trailing blanks from our string.
function RTrim(str)	{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

//Remove trailing and leading blanks from our string.
function Trim(str) {
   return RTrim(LTrim(str));
}

//Use this function to show a popup with additional information
function OpenPopupInfo(lngPopupID) {
	window.open('/popupinfo.asp?info=' + lngPopupID, 'test', 'toolbar=no,height=250,width=250,directories=no,status=no,scrollbars=no,resizable=no,menubar=no');
}

//Called when the user clicks on the ASC sort button
function ChangeSort(strSort, strSortUI) {
	document.frmOverzicht.Sort.value = strSort;
	document.frmOverzicht.SortUI.value = strSortUI;
	document.frmOverzicht.submit();
}

//Called when the user clicks on the DESC sort button
function ChangeSortDesc(strSort, strSortUI) {
	document.frmOverzicht.Sort.value = strSort + ' DESC';
	document.frmOverzicht.SortUI.value = strSortUI;
	document.frmOverzicht.submit();
}

//Called when the user clicks the 'Afmelden' button
function AbandonSession() {
	window.top.document.location = "/index.asp"
}

//The next two functions are used for highlighting in tables
var rowWithMouse = null;

function rowRollover(myId, isInRow) {
  // myId is our own integer id, not the DOM id
  // isInRow is 1 for onmouseover, 0 for onmouseout
  var row = document.getElementById('ROW' + myId);
  rowWithMouse = (isInRow) ? row : null;
  rowUpdateBg(row);
}

function rowUpdateBg(row) {
   row.style.backgroundColor = (row == rowWithMouse) ? gcHighlight_Selected : gcHighlight_NotSelected;
   row.style.color = (row == rowWithMouse) ? gcHighlight_SelectedTxt : gcHighlight_NotSelectedTxt;
}

//Code below is for setting the browser's statusbar
function hidestatus(){
	window.status='Powered and Hosted by Twice Services'
	return true
}
if (document.layers)
	document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT)
document.onmouseover = hidestatus;
document.onmouseout = hidestatus;

function IsValidEmail (emailStr) {

	if (emailStr == "")
		return true;

	/* 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 false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid
	if (user.match(userPat)==null) {
		// user is not valid
		return false
	}

	/* 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 false
			}
		}
		return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false
	}

	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding
	   the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 ||
		domArr[domArr.length-1].length>3) {
	   // the address must end in a two letter or three letter word.
	   return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}

function checkPrice(price)
{
	var pattern

	pattern = /^([0-9].+),([0-9]{1,2})$|\d[0-9]$|^([0-9].+).([0-9]{1,2})$|^([0-9])$/

	var matchArray=price.match(pattern)
	if (matchArray==null)
	{
		var matchArray=price.match(pattern)
		if (matchArray==null)
			return false;
	}
	else
		return true;

}

function checkPercentage(percentage)
{
	var pattern

	pattern = /^([0-9]+),([0-9]+)$|^([0-9]+)$/

	var matchArray=percentage.match(pattern)
	if (matchArray==null)
	{
		var matchArray=percentage.match(pattern)
		if (matchArray==null)
			return false;
	}
	else
		return true;

}

function checkDecimal(decValue)
{
	var pattern = /^[0-9]+[,|.][0-9]+$|^[0-9]+$/

	var matchArray=decValue.match(pattern)
	if (matchArray==null)
	{
		var matchArray=decValue.match(pattern)
		if (matchArray==null)
			return false;
	}
	else
		return true;

}

function IsNumeric(strValue)
{
	var intGetal
	var blnGetal
	blnGetal = true
	if(strValue != "" )
	{
		for(i = 0; i < strValue.length; i++)
		{
			intGetal = parseInt(strValue.charAt(i), 10)
			if(intGetal >= 0 && intGetal <= 9)
			{
			}
			else
			{
				alert("U moet een getal invoeren om het maximaal aantal cursisten aan te geven.");
				return false;
			}
		}
	}
	return true;
}

function VergelijkTijden(ElementNummer, NaamStartUur, NaamStartMinuut, NaamEindUur, NaamEindMinuut, blnSoortVerplicht) {
	var element1;
	var element2;
	var element3;
	var element4;
	var startUren;
	var eindUren;

	element1 = NaamStartUur + ElementNummer;
	element2 = NaamStartMinuut + ElementNummer;
	element3 = NaamEindUur + ElementNummer;
	element4 = NaamEindMinuut + ElementNummer;

	startUren = parseInt(document.all.item(element1).value);
	eindUren = parseInt(document.all.item(element3).value);
	startMinuten = parseInt(document.all.item(element2).value);
	eindMinuten = parseInt(document.all.item(element4).value);

	if (startUren == 0 && startMinuten == 0 && eindUren == 0 && eindMinuten == 0) {
		if (blnSoortVerplicht == true)
			return false
		else
			return true
	}
	if (startUren > eindUren || (startUren == eindUren && startMinuten > eindMinuten))
		return false;
	else if (startUren > 0 && startUren == eindUren && startMinuten == eindMinuten)
		return false;
	else
		return true;

}

function ForceInputFieldPrefix(strStartWidth, objInputField)
{
	var string;
	string = objInputField.value.toLowerCase();
	string = string.substring(0,7);
	if(string != strStartWidth)
	{
		objInputField.value = strStartWidth + objInputField.value
	}

}

function SetInputSelection(obj) {

	var range = obj.createTextRange();
	range.collapse(true);
	range.moveEnd('character', obj.value.length);
	range.moveStart('character', 0);
	range.select();

}

function ResetConfirmLogout() {
	window.clearTimeout(timeoutID);

	var popup = document.getElementById('popup_confirminactive');
	document.body.removeChild(popup);
	var overlay = document.getElementById('overlay');
	document.body.removeChild(overlay);

	window.setTimeout('ConfirmLogout()', ResetConfirmLogoutMinutes * 60000);
}

function ShowLoggedOut() {
	var popup_inner = document.getElementById('popup_inner');
	popup_inner.innerHTML = logout_msg;
}

var timeoutID;

function ConfirmLogout() {
	var now = new Date();
	var hour = now.getHours();
	var minute = now.getMinutes() + LogoutAfterConfirmMinutes;
	var seconds = now.getSeconds();

	if (parseInt(minute, 10) >= 60) {
		hour = parseInt(hour, 10) + 1;
		minute = parseInt(minute, 10) - 60;
	}
	if (minute < 10) {  minute = '0' + minute; }
	if (seconds < 10) { seconds = '0' + seconds; }

	var msg = 'U bent ' + ResetConfirmLogoutMinutes + ' minuten inactief, u wordt uitgelogd om ' + hour + ':' + minute + ':' + seconds + '.<br /><br />Klik op annuleren om ingelogd te blijven.<br /><br />';
	msg += '<input type="button" name="continue" value="Annuleren" id="cancellogout" onclick="ResetConfirmLogout();" />';

	var overlay = document.createElement('DIV');
	overlay.id = 'overlay';
	var popup = document.createElement('DIV');
	popup.id = 'popup_confirminactive';
	var popup_inner = document.createElement('DIV');
	popup_inner.id = 'popup_inner';
	popup_inner.innerHTML = msg;
	popup.appendChild(popup_inner);

	document.body.appendChild(overlay);
	document.body.appendChild(popup);

	timeoutID = window.setTimeout('ShowLoggedOut()', LogoutAfterConfirmMinutes * 60000);
}

