function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}
var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;
function isEmpty(s){
	
   return ((s == null) || (s.length == 0))
}
function isWhitespace (s){
   var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}
function isLetter (c){  
	 return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}
function isDigit (c){   
	return ((c >= "0") && (c <= "9"))
}
function StripLeadingZero (s){
	var i;
    var sreturn = '';
    var LeadingZero = true;
    for (i = 0; i < s.length; i++)
    {   var c = s.charAt(i);
        if (LeadingZero){
	    if (c != "0") { 
	    LeadingZero = false;
	    sreturn = sreturn + c;
        }
    }
	else
        sreturn = sreturn + c;
    }
    return sreturn;
}
function isInteger (s){
	var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (i==0 && ("-+ ").indexOf(c)!=-1)
        {
	        //if 1st character is '-' or '+' or ' ' then this is ok
    	}
    	else
    	{
        	if (!isDigit(c)) return false;
    	}
    }
    return true;
}
function isSignedInteger (s){
	if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);
    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;
        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}
function isNonnegativeInteger (s){
	var secondArg = defaultEmptyOK;
    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];
    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}
function isFloat (s){
	var i;
    var seenDecimalPoint = false;
    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);
    if (s == decimalPointDelimiter) return false;
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }
    return true;
}
function isYear (s){
	if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}
function isIntegerInRange (s, a, b){
	if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);
    if (!isInteger(s, false)) return false;
    var t = StripLeadingZero (s);
    var num = parseInt (t);
    return ((num >= a) && (num <= b));
}
function isMonth (s){
    if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}
function isDay (s){
    if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}
function daysInFebruary (year){   
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}
function isDate (year, month, day){  
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;
    var intYear = parseInt(year, 10);
    var intMonth = parseInt(month, 10);
    var intDay = parseInt(day, 10);
    if (intDay > daysInMonth[intMonth]) return false; 
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
    return true;
}
function promptEntry (s){
   window.status = pEntryPrompt + s
}

function warnEmpty (theField, s)
{
	var rv=true;
	try
	{
	    theField.focus();
	    alert(mPrefix + mSuffix);
    	rv = false;
    }
    catch(err)
    {
	    //setting focus failed, probably not visible; continue validation on other fields
    }
    return rv;
}

function warnInvalid (theField, s){
    theField.focus()
    if(theField.select != null) theField.select()
    alert(s)
    return false
}
function warnToLarge (theField, s){
    theField.focus()
    alert(sToLarge)
    return false;
}
function checkString (theField, s, emptyOK){
	var lngMaxLenghtDS;
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK ==true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) return warnEmpty (theField, s);
    lngMaxLenghtDS = theField.getAttribute("MAXLENGTHDS");
    if (lngMaxLenghtDS != null)
    {
		if ((lngMaxLenghtDS + "") == "") {
			return true;
		}
		if (theField.value.length > lngMaxLenghtDS)
		{
			return warnToLarge(theField, s);
		}
    }
    return true;
}
function checkNumber (theField, emptyOK){
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    if (checkNumber.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isInteger(theField.value, false)) 
       return warnInvalid (theField, iNumber);
    else return true;
}
function checkNumberWithExtends (theField,intMin,intMax, emptyOK){
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    var iMin = sGreater1 + intMin + sGreater2;
	var iMax = sSmaller1 + intMax + sSmaller2;
	if (checkNumberWithExtends.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isInteger(theField.value, false)) return warnInvalid (theField, iNumber);
    if(!isEmpty(intMin))
    {
		if(theField.value < intMin) return warnInvalid (theField, iMin);
	}
    if(!isEmpty(intMax))
    {
		if(theField.value > intMax) return warnInvalid (theField, iMax);
	}
    return true;
}
function checkCurrency (theField, emptyOK){
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    if (checkCurrency.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isFloat((theField.value).replace(',','.'), false)) 
    {
       return warnInvalid (theField, iCurrency);
    }
    if (!checkCurrencyForThreeDecimalsAfter(theField))
    {
		return warnInvalid (theField, iCurrency);
	}
    return true;
}
function checkCurrencyWithExtends (theField,intMin,intMax, emptyOK){
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    var iMin = sGreater1 + intMin + sGreater2;
	var iMax = sSmaller1 + intMax + sSmaller2;		
	if (checkCurrencyWithExtends.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isFloat((theField.value).replace(',','.'), false)) return warnInvalid (theField, iCurrency);    
    if(!isEmpty(intMin))
    {
		if( parseFloat((theField.value).replace(',','.')) < parseFloat((intMin.toString()).replace(',','.')) ) return warnInvalid (theField, iMin);
	}
    if(!isEmpty(intMax))
    {
		if( parseFloat((theField.value).replace(',','.')) > parseFloat((intMax.toString()).replace(',','.')) ) return warnInvalid (theField, iMax);
	}
    if (!checkCurrencyForThreeDecimalsAfter(theField))
    {
		return warnInvalid (theField, iCurrency);
	}
    return true;
}
function checkCurrencyForThreeDecimalsAfter(theField)
{
	// disables all dots filled in
	if (( theField.value.indexOf(".") ) > -1)
	{
		return false;
	}
	else
	{
		if (( theField.value.indexOf(",") ) > -1)
		{
			var lngPos = theField.value.indexOf(",");
			if (lngPos < (theField.value.length - 3))
			{
				return false;		
			}			
		}		
	}
	return true;	
}
function checkYear (theField, emptyOK)
{
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    if (checkYear.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isYear(theField.value, false)) 
       return warnInvalid (theField, iYear);
    else return true;
}
function checkMonth (theField, emptyOK){
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    if (checkMonth.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isMonth(theField.value, false)) 
       return warnInvalid (theField, iMonth);
    else return true;
}
function checkDay (theField, emptyOK){
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    if (checkDay.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isDay(theField.value, false)) 
       return warnInvalid (theField, iDay);
    else return true;
}
function checkDate (yearField, monthField, dayField, labelString, OKtoOmitDay){
    if (yearField == null) return true;
    if (checkDate.arguments.length == 4) OKtoOmitDay = false;
    if (OKtoOmitDay==false)
    {
	    var dateElmBaseNm = yearField.name.substr(0, yearField.name.indexOf("Year"));
	    var elm = DIVCalendarGetDocElm(dateElmBaseNm + "DateField");
	    if (elm==null)
	    {
		    //do nothing; this is an old dateformat
	    }
	    else
	    {
		    //Is new datefield; that means 'mandatory' variable must be defined.
		    var mnd = 0;
		    try
		    {
			    mnd = eval("calMandatory_" + dateElmBaseNm);
		    }
		    catch(err)
		    {
		    }
		    if (mnd==1 && yearField.value=="1800")	//in xsl also only checked on year...
		    {
			    return warnEmpty(elm, "");
		    }
	    }
    }
    if (!isYear(yearField.value)) return warnInvalid (yearField, iYear);
    if (!isMonth(monthField.value)) return warnInvalid (monthField, iMonth);
    if ( (OKtoOmitDay == true) && isEmpty(dayField.value) ) return true;
    else if (!isDay(dayField.value)) 
       return warnInvalid (dayField, iDay);
    if (isDate (yearField.value, monthField.value, dayField.value))
       return true;
    alert (iDatePrefix + iDateSuffix)
    return false
}
function checkDateString(dateField) {
	return true;
}
function checkMultiSelectFilled(elmSelect) {
	if (elmSelect == null) return true;
	if (elmSelect.selectedIndex==-1) {
		elmSelect.focus()
		alert(mPrefix + "\nA minimum of one item must be selected." + mSuffix)
		return false;
	} else {
		return true;
	}
}
function reformatZIPCode (p,s)
{   
	return p + s.toUpperCase();
}

function checkZIPCode (theField, emptyOK)
{
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {	
		var SuffixZIP = stripCharsNotInBag(theField.value, ZIPCodeDelimiters);
		var PrefixZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters);

		if (!isZIPCode(PrefixZIP,SuffixZIP, false)) 
			return warnInvalid (theField, iZIPCode);
		else 
		{
			theField.value = reformatZIPCode(PrefixZIP,SuffixZIP);
			return true;
		}
    }
}

function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        
        if (bag.indexOf(c) < 0) 
        {
			if (c != " ")
			{ 
				returnString += c;
			}
        }
    }
    return returnString;
}

function isZIPCode (p,s)
{  
	if (isEmpty(p)) 
       if (isZIPCode.arguments.length == 2) return defaultEmptyOK;
       else return (isZIPCode.arguments[2] == true);
   return (((digitsInZIPCodePrefix == p.length) && (isIntegerInRange(p,1000,9999))) && (s.length == digitsInZIPCodeSuffix));
}

function checkLicensePlate (theField, emptyOK)
	{
		var rv = false;
		if (theField == null) return true;
		if (theField.type == "hidden") return true;
	    if (checkLicensePlate.arguments.length == 1) emptyOK = defaultEmptyOK;
	    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
	    else
	    {	
			var normalized = stripCharsInBag(theField.value,"-");
			if (normalized.length == 6)
			{
				var part1 = getPart(normalized,0,2);
				var part2 = getPart(normalized,2,4);
				var part3 = getPart(normalized,4,6);
	 			
	 			if (isLincensePlate(part1,part2,part3, false) || checkRDWSideCode7tot10(theField,iLicensePlate)) {
		 			theField.value = reformatLicensePlate(part1,part2,part3);
					rv = true;
				} 
			}
	    }
	    if (rv==false)
	    {
		    warnInvalid(theField,iLicensePlate);
	    }
	    return rv;
	}

function checkRDWSideCode7tot10 (theField, emptyOK) {
	check = 1;
	if (theField == null) return true;
	if (theField.type == "hidden")  { return true; }
    if (checkLicensePlate.arguments.length == 1) { emptyOK = defaultEmptyOK; }
    if ((emptyOK == true) && (isEmpty(theField.value))) { return true; }
    else
    {	
		var normalized = stripCharsInBag(theField.value,"-");
		if (normalized.length == 6) {
			//regexSideCode1tot6 = "^([0-9]{2}|[a-z]{2})([0-9]{2}|[a-z]{2})([0-9]{2}|[a-z]{2})$";
			regexSideCode7 = "^[0-9]{2}[a-z]{3}[0-9]{1}$";
			regexSideCode8 = "^[a-z]{2}[0-9]{3}[a-z]{1}$";
			regexSideCode9 = "^[0-9]{1}[a-z]{3}[0-9]{2}$";
			regexSideCode10 = "^[a-z]{1}[0-9]{3}[a-z]{2}$";
			re = new RegExp(regexSideCode7 + "|" + regexSideCode8 + "|" + regexSideCode9 + "|" + regexSideCode10,"i");
			var result = normalized.match(re);
			if(result){
				return true;
			} else {
				return warnInvalid (theField, iLicensePlate);
			} 	
		} else { 
			return false;
		}
	}
}

function getPart (s,r1,r2)
{   var i;
    var returnString = "";

    for (i = r1; i < r2; i++)
    {   
        var c = s.charAt(i);
		returnString += c;
    }
    return returnString;
}

function isLincensePlate (s1,s2,s3)
{ 
	if (!isInteger(s1,false)) 
	{
		if (!isAlphabetic(s1,false)) return false;
	}
	if (!isInteger(s2,false)) 
	{
		if (!isAlphabetic(s2,false)) return false;
	}
	if (!isInteger(s3,false)) 
	{
		if (!isAlphabetic(s3,false)) return false;
	}

    return true;
}

function reformatLicensePlate (s1,s2,s3)
{   
	return s1.toUpperCase() +  s2.toUpperCase() +  s3.toUpperCase();
}

function isAlphabetic (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isLetter(c))
        return false;
    }

    return true;
}

function checkChassis (theField, emptyOK)
{
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    if (checkChassis.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {	var s = theField.value;
		if (s.length == 4)
		{ 
 			if (!isInteger(s, false)) 
				return warnInvalid (theField, iChassis);
			else 
			{
				return true;
			}
		}
		else
		{
			return warnInvalid (theField, iChassis);
		}
    }
}

function checkEmailAddress(theField, emptyOK)
{
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    if (checkEmailAddress.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {	
		if (!isEmailAddress(theField.value, false)) 
			return warnInvalid(theField, iEmailAddress);
		else 
		{
			theField.value = reformatEmailAddress(theField.value);
			return true;
		}
    }
}

function isEmailAddress(p,s)
{  
	var rv = false;
	if (p!="")
	{
		var posAtSign = p.indexOf("@");
		var posLastDot = p.lastIndexOf(".");
		var posFirst = 0;
		var posLast = p.length;
		//minimal emailaddress looks like a@bb.cc
		if (posAtSign>=(posFirst+1) && posLastDot>(posAtSign+2) && posLast>(posLastDot+2))
		{
			rv = true;
		}
	}
	return rv;
}

function reformatEmailAddress(p)
{
	var rv = p.toLowerCase();
	return rv;
}

function checkSofi (theField, emptyOK)
{
	if (theField == null) return true;
	if (theField.type == "hidden") return true;
    if (checkSofi.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value)))
    {
	    return true;
	}
    else
    {
	    var s = theField.value;
		if (s.length == 9)
		{ 
 			if (!isInteger(s, false)) 
				return warnInvalid (theField, iSofi);
			else 
			{
				if (isSofi(s))
				{
					return true;
				}
				else
				{
					return warnInvalid (theField, iSofi);
				}
			}
		}
		else
		{
			return warnInvalid (theField, iSofi);
		}
    }
}

function isSofi(p)
{
	var rv = false;
	var sofinr = "" + p;
	if (sofinr!="")
	{
		if (isInteger(sofinr))
		{
			//now the real check
			var mp = -1;
			var tsum = 0;
			var char;
			for (var j=sofinr.length-1;j>=0; --j)
			{
				char = sofinr.substr(j, 1);
				tsum += mp*parseInt(char, 10);
				if (mp==-1)
				{
					mp = 2;
				}
				else 
				{
					++mp;
				}
			}
			//deelbaar door 11 en niet 9*0...
			if (((tsum % 11) == 0) && (tsum!=0)) 
			{
				rv = true;
			}
		}
	}
	return rv;
}
