function ReplaceR(STRING)
{
    STRING = STRING.replace("||","|");
    STRING = LReplace(STRING);
    return RReplace(STRING);
}

function RReplace(STRING)
{
    while(STRING.charAt((STRING.length -1))=="|")
    {
        STRING = STRING.substring(0,STRING.length-1);
    }
    return STRING;
}

function LReplace(STRING)
{
    while(STRING.charAt(0)=="|")
    {
        STRING = STRING.replace(STRING.charAt(0),"");
    }
    return STRING;
}

//Trim function
function Trim(stringval)
{
	var len;
	len = stringval.length;
	if(len>0)
	{
		while(stringval.substring(0,1) == " ")
		{ //trim left
		    stringval = stringval.substring(1, len);
		    len = stringval.length;
		}
		while(stringval.substring(len-1, len) == " ")
		{ //trim right
		    stringval = stringval.substring(0, len-1);
		    len = stringval.length;
		}
	}
	return stringval;
}

function fnSplit(string)
{
	var temp = new Array();
	temp = string.split('|');
	return temp.length;
}

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function char(s)
{
	if (!isInteger(s))
	{
		return false;
	}
	return true;
}

//Phonenumber Validation
function phone(s)
{
	/*if (!isInteger(s))
	{
		return false;
	}
	if (s.length <10)
	{
		return false;
	}
	if (s.length >13)
	{
		return false;
	}*/
	return true;
}

//Phonenumber Validation
function phonecheck(s)
{
	var invalidChars = '_@\'\\ ",;:?!()[]\{\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	for (i=0; i<invalidChars.length; i++)
	{
	    if(s.indexOf(invalidChars.charAt(i),0) > -1)
	    {
		    return false
	    }
	}
	if (s.length <10)
	{
		return false;
	}
	if (s.charAt(0) != "0")
	{
	    return false;
	}

	return true;
}


//Maximum function
function max(txt,n)
{
	l = txt.length;
	if(l>n)
	{
		return true;
	}
return false;
}

//Minimum function
function min(txt,n)
{
	if( txt.length < n)
	{
		return true; 
	}
	return false;
}

//Email validation 
function echeck(str)
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1)
	{
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
	    return false
	}

    if (str.indexOf(at,(lat+1))!=-1)
    {
        return false
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
    {
        return false
    }

    if (str.indexOf(dot,(lat+2))==-1)
    {
        return false
    }

    if (str.indexOf(" ")!=-1)
    {
        return false
    }
    
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) 
	{
	   if (str.indexOf(invalidChars.charAt(i),0) > -1) 
	   {
		  return false;
	   }
	}
	
	if (str.indexOf('..',0) != -1) 
	{
		return false;
	}
	
	var invalidChar = '-_@.\/\'\\ ";:?!()[]\{\}^|1234567890';
	if(invalidChar.indexOf(str.charAt(0),0)>-1)
	{
		return false
	}
	return true;
}
	
function fnNameExist(string,str)
{
	var temp = new Array();
	temp = str.split('|');
	cnt = temp.length;
	 for (i=0;i<cnt;i++)
	 {
		//alert(string);
		//alert(temp[i]);
	 	if (Trim(string.toUpperCase())==Trim(temp[i].toUpperCase()))
		{
			return "exist";
		}						
	 }
	 return "true";
}

function gridmousover(id)
{
	document.getElementById("tr"+id).style.backgroundColor="#E6E6E6";
}

function gridmouseout(id,clr)
{
	document.getElementById("tr"+id).style.backgroundColor=clr;
}

function greymouseover(id)
{
	document.getElementById("tr"+id).style.backgroundColor="#F0F0F0";
}

// combo value selecte maximum three coding Starts
function fnPlus(Id)
{
	id = document.getElementById("hid"+Id);
	oprid = document.getElementById("opr"+Id);
	cnt=fnSplit(id.value);
	
	if (cnt=="3")
	{
		alert("Je kunt er maximaal 3 selecteren");
		oprid.value="0";
		return false;
	}
	else
	{
		oprid.value=1;
	}
}

function fnSub(Id)
{
	id = document.getElementById("hid"+Id);
	oprid = document.getElementById("opr"+Id);

	if (id.value == "0")
	{
		alert("Je hebt geen keuze gemaakt");
		oprid.value="1";
		return false;
	}
			
	cnt=fnSplit(id.value);
	if (cnt=="0")
	{
		alert("Je hebt geen keuze gemaakt");
		oprid.value="1";
		return false;
	}
	else
	{
		oprid.value=2;
	}
}

function fnSelect(Id,cmbId)
{
	id = document.getElementById("hid"+Id);
	oprid = document.getElementById("opr"+Id);
	cmb_Id = document.getElementById(cmbId);
	inst = document.getElementById(Id);

	if (Trim(oprid.value)=="0")
	{
		alert("Klik op de + of de - om keuze toe te voegen of te verwijderen");
		cmb_Id.value="0";
		return false;
	}	
	else if (Trim(oprid.value)=="1" && Trim(cmb_Id.value)!="0")
	{
		if (id.value=="0")
		{
			id.value=cmb_Id.value;
		}
		else
		{
			chkavlble=fnNameExist(cmb_Id.value,id.value);
			if (chkavlble == "true")
			{
				id.value=id.value + "|" + cmb_Id.value;			
			}			
			else
			{
				alert("Bestaat al");
				return false;
			}
		}
		oprid.value="0";
		if (id.value!="0")
		{
		    inst.innerHTML=id.value.toUpperCase().replace(/\|/g,"<br>");
		}
		else
		{
		    inst.innerHTML="&nbsp;"
		}
		cmb_Id.value="0";				
		
		return false;			
	}
	else if (Trim(oprid.value)=="2" && Trim(cmb_Id.value)!="0")
	{
		if (id.value=="0" || Trim(id.value)=="")
		{
			alert("Naam bestaat niet");
			return false;
		}
		else
		{
			chkavlble=fnNameExist(cmb_Id.value,id.value);
			
			if (chkavlble == "true")
			{
				alert("Naam bestaat niet");
				return false;		
			}			
			else
			{			
				id.value=id.value.toUpperCase().replace(Trim(cmb_Id.value.toUpperCase()),"");				
				id.value=ReplaceR(id.value);				
				if (Trim(id.value)=="")
				{
					id.value="0";
					oprid.value="1";
				}
				else
				{
					oprid.value="0";
				}
			}
		}
		
		if (id.value!="0")
		{
		    inst.innerHTML=id.value.replace(/\|/g,"<br>");
		}
		else
		{
		    inst.innerHTML="&nbsp;"
		    //inst.innerHTML=inst.innerHTML.replace("<br>","");
		}
		cmb_Id.value="0";	
		return false;			
	}
}

function instReccmb(Id)
{
	id = document.getElementById("hid"+Id);
	inst = document.getElementById(Id);
	inst.innerHTML=id.value.toUpperCase().replace(/\|/g,"<br>");
}

//Date validation

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function yesterdayCheck()
{
    var dateTime = new Date();
    var tempY = 0;
    var tempM = 0;
    var tempD = 0;
    if(dateTime.getDate() == 1)
    {
        if(dateTime.getMonth() == 0)
        {
            tempY = dateTime.getFullYear()-1;
            tempM = 12;
            tempD = 31;
            //alert(tempY+'-'+tempM+'-'+tempD);
            return tempY+'-'+tempM+'-'+tempD;
        }
        else
        {
            tempY = dateTime.getFullYear();
            tempM = dateTime.getMonth();
            if(tempM == 1)
            {
                tempD = daysInFebruary(dateTime.getYear());
                //alert(tempY+'-'+tempM+'-'+tempD);
                return tempY+'-'+tempM+'-'+tempD;
            }
            else if(tempM == 4 || tempM == 6 || tempM == 9 || tempM == 11)
            {
                tempD = 30;
                //alert(tempY+'-'+tempM+'-'+tempD);
                return tempY+'-'+tempM+'-'+tempD;
            }
            else
            {
                tempD = 31;
                //alert(tempY+'-'+tempM+'-'+tempD);
                return tempY+'-'+tempM+'-'+tempD;
            }
        }
    }
    else
    {
        tempY = dateTime.getFullYear();
        tempM = dateTime.getMonth()+1;
        tempD = dateTime.getDate()-1;
        //alert(tempY+'-'+tempM+'-'+tempD);
        return tempY+'-'+tempM+'-'+tempD;
    }
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) 
{
    for (var i = 1; i <= n; i++) 
    {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) 
		{
		    this[i] = 30
		}
		if (i==2) 
		{
		    this[i] = 29
		}
    } 
    return this
}

function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1)
	{
		alert("Datum heeft het volgende formaat : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12)
	{
		alert("Voer een correcte maand in")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		alert("Voer een correcte dag in")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		alert("Voer een correct 4 cijferig jaar in tussen "+minYear+" en "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		alert("Voer een correcte datum in")
		return false
	}
return true
}

function compareTheDates(dtStr1, dtStr2)
{
    var frmtemp = dtStr1.split("/");
    var totemp = dtStr2.split("/");
    if (parseInt(frmtemp[2]+""+frmtemp[1]+""+frmtemp[0]) > parseInt(totemp[2]+""+totemp[1]+""+totemp[0])) {
        return false;
    }
    return true
}

function isChar(s)
{
	var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9")))
        { 
			return false;
	    }
    }
    // All characters are numbers.
    return true;
}

//function isChar(s){
	//var i;
	//for(i=0;i<s.length;i++){
	//		var c = s.charAt(i);
	//		if(!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))						
		//	return false;}
	//	return true;
	//}

// combo value selecte maximum three coding Ends

function getParam(paramName){
	//returns the value of the requested querystring parameter
	var docLoc = document.location.href;
	var qString = '';
	var params = new Array();
	
	if(docLoc.indexOf("?") != -1){
		qString = document.location.href.split("?");
		qString = qString[1];
	}
		
	if(qString.indexOf("&") != -1){
		params = qString.split("&");
	}
	else if(qString.indexOf("=") != -1){
		params[params.length] = qString;	
	}
	
	for(i=0; i<params.length; i++){
		param = params[i].split("=");
		if(param[0] == paramName){
			return param[1];	
		}
	}
	
	return '';
}

/**
*   Class: TimeDropDown 
*   writes and populates a dropdown with time values
**/
var TimeDropDown = function(objId){

    var priv = {
        times       : Array(),
        startTime   : '08:00',
        endTime     : '20:00',
        interval    : 30, //interval in minutes, no
        obj         : document.getElementById(objId),
        attributes  : Array(),
        debug       : false,
        
        time        : function(value, text){
            this.value = value;
            this.text = text;
        },
        
        attribute   : function(name, value){
            this.name = name;
            this.value = value;
        },
        
        populateTimes : function(){
            priv.writeDebug(priv.startTime);
            thisStartTime = priv.startTime;
            if(thisStartTime.substr(0,1) == '0'){
                thisStartTime = thisStartTime.substr(1);
            }
            thisEndTime = priv.endTime;
            if(thisEndTime.substr(0,1) == '0'){
                thisEndTime = thisEndTime.substr(1);
            }
            for(var i=parseInt(thisStartTime); i<parseInt(thisEndTime); i++){
                hours   = i;
                hourStr = (i<10 ? ("0"+i) : i);
                if(priv.interval > 0){
                    for(var j=0; j<(60/priv.interval); j++){
                        mins    = j*priv.interval;
                        minStr  = (mins<10 ? ("0"+mins) : mins);
                        priv.writeDebug(parseInt(thisStartTime.replace(':', '')) + "\n" + parseInt(hours + '' + minStr));
                        
                        if(parseInt(thisStartTime.replace(':', '')) <= parseInt(hours + '' + minStr)){
                            priv.times[priv.times.length] = new priv.time(hourStr + '' + minStr, hourStr + ':' + minStr);
                        }
                    }
                }
                else {
                     minStr  = "00";
                     priv.times[priv.times.length] = new priv.time(hourStr + '' + minStr, hourStr + ':' + minStr);
                }
            }
            priv.writeDebug(priv.times);
        },
        
        clearOptions    : function(){
            priv.times.length = 0;
            priv.obj.innerHTML = '';
            priv.writeDebug(priv.obj.innerHTML);
        },
        
        writeDebug : function(msg){
            if(priv.debug && console && console.log){
                console.log(msg);
            }
        }
    };
    
    return {
        setInterval : function(interval){
            if(Math.round(interval / 60) != interval / 60){
                priv.writeDebug('Warning: TimeDropDown interval cannot be divided by 60');
            }
            priv.interval = interval % 60; //make sure it is a modulo of 60
        },
        
        setStartTime    : function(newStartTime){
            priv.startTime = newStartTime;
        },
        
        setEndTime    : function(newEndTime){
            priv.endTime = newEndTime;
        },
        
        addAttribute    : function(name, value) {
            priv.attributes[priv.attributes.length] = new attribute(name, value);
        },
        
        write       : function(){
            //populate the array
            priv.clearOptions();
            priv.populateTimes();
                  
            for(var i=0; i<priv.attributes.length; i++){
                eval('selectObj.' + priv.attributes[i].name + ' = "' + priv.attributes[i].value + '";')
            }
                       
            for(var i=0; i<priv.times.length; i++){
                var tmpOption = document.createElement("OPTION");
                tmpOption.value = priv.times[i].value;
                tmpOption.text = priv.times[i].text;
                if(tmpOption.text == priv.startTime)
                    tmpOption.selected = true;
                
                try {
                    //for IE:
                    priv.obj.add(tmpOption);
                }
                catch(err) {
                    //for others
                    priv.obj.appendChild(tmpOption);
                }
            }
        }
    };    
}

function URLEncode(urlToEncode)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = urlToEncode;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}
function URLDecode(urlToDecode)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = urlToDecode;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
}

