// Trim whitespaces - used for 'search'
function trim(s) {
	return s.replace(/^\s+|\s+$/g, "");
}
	/*
		* Unlike Autocompleter.Local you can now pass along arrays in an array.
		* The output will be <ul><li id=''></li></ul> instead of <ul><li></li></ul>
		* It now has the same functionality as the basic autocompleter with ajax request
		*/	
if(typeof Autocompleter != 'undefined'){
	Autocompleter.Local2 = Class.create(Autocompleter.Base, {
		initialize: function(element, update, array, options) {
			this.baseInitialize(element, update, options);
			this.options.array = array;
		},
	
		getUpdatedChoices: function() {
			this.updateChoices(this.options.selector(this));
		},
	
		setOptions: function(options) {
			this.options = Object.extend({
				choices: 150,
				partialSearch: true,
				partialChars: 1,
				ignoreCase: true,
				fullSearch: false,
				selector: function(instance) {
					var ret       = []; // Beginning matches
					var partial   = []; // Inside matches
					var entry     = instance.getToken();
					var count     = 0;
			
					var first_letter;
					if(entry.length >= 1){
						first_letter = entry.substring(0,1).toLowerCase();
					}
	
					try{
						instance.options.array = eval("cities_"+first_letter);
					}
					catch(err){
						instance.options.array = eval("cities_special");	
					}
					
					for (var i = 0; i < instance.options.array.length; i++) { 
						if(instance.options.array[i] !== undefined){
							var elem = instance.options.array[i];
			
							var foundPos = instance.options.ignoreCase ? 
							elem[0].toLowerCase().indexOf(entry.toLowerCase()) : 
							elem[0].indexOf(entry);
			
							while (foundPos != -1) {
			
							if (foundPos == 0 && elem[0].length != entry.length) { 
								ret.push("<li id='"+elem[1]+"'><strong>" + elem[0].substr(0, entry.length) + "</strong>" + 
								elem[0].substr(entry.length) + "</li>");
								break;
							} 
							else if (entry.length >= instance.options.partialChars && instance.options.partialSearch && foundPos != -1) {
								if (instance.options.fullSearch || /\s/.test(elem[0].substr(foundPos-1,1))) {
									partial.push("<li id='"+elem[1]+"'>" + elem[0].substr(0, foundPos) + "<strong>" +
									elem[0].substr(foundPos, entry.length) + "</strong>" + elem[0].substr(
									foundPos + entry.length) + "</li>");
									break;
								}
							}
							foundPos = instance.options.ignoreCase ? 
							elem[0].toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : 
							elem[0].indexOf(entry, foundPos + 1);						
						}
					}
				}
				if (partial.length)
					ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
					return "<ul>" + ret.join('') + "</ul>";
				}
			}, options || { });
		}
	});
}

function isTeLang(pTextArea, pMaxLengte)
{
    if (pTextArea)
    {
        if (pTextArea.value && pTextArea.value.length > pMaxLengte)
        {
            pTextArea.style.background = '#ff8080';
            return true;
        }
        else
        {
            pTextArea.style.background = '#ffffff';
            return false;
        }
    }
    else {

        if (window.console) {
            
            window.console.log("object not found: " + pTextArea);
        }
    }
}

function ismaxlength(obj)
{
    var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length > mlength)
        obj.value = obj.value.substring(0, mlength)
}


function isLeeg(pField)
{
    if (pField.value == '')
    {
        pField.style.background = '#ff8080';
        return true;
    }
    else
    {
        pField.style.background = 'white';
        return false;
    }
}

function isLeegId(pField, pRed)
{
    if (pField.value == "" || pField.value == "-1")
    {
        /*if(document.getElementById("pZaalNieuwId")){
            pRed.style.background = "white";
            return false;
        } */
        pRed.style.background = "#ff8080";
        return true;
    }
    else
    {
        pRed.style.background = "white";
        return false;
    }
}

function goedeSelectie(pSelect)
{
    if (pSelect != null)
    {
        if (pSelect.options.length > 1 && pSelect.options[pSelect.selectedIndex].value < 0)
        {
            pSelect.style.background = '#ff8080';
            return false;
        }
        else
        {
            pSelect.style.background = 'white';
            return true;
        }
    }
}

function isNumericField(pField)
{
    if (isNaN(pField.value))
    {
        pField.style.background = '#ff8080';
        return false;
    }
    else
    {
        pField.style.background = 'white';
        return true;
    }
}

function isDecimalNumber(pValue)
{
    if (isNaN(pValue.value))
    {
        pValue.style.background = '#ff8080';
        return false;
    }
    else
    {
        if (pValue.value == Math.round(pValue.value))
        {
            return false;
        }
        else
        {
            pValue.style.background = '#ff8080';
            return true;
        }
    }
}

function isNegatiefNummer(pValue)
{
    if (isNaN(pValue.value))
    {
        pValue.style.background = '#ff8080';
        return false;
    }
    else
    {
        if (pValue.value < 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

function waardeVanRadioButton(pField)
{
    for (i = 0; i < pField.length; i++)
    {
        if (pField[i].checked)
        {
            return pField[i].value;
        }
    }
    return '';
}

function isDateVoorDate(pFieldVan, pFieldTot)
{
    // indien beide velden leeg, skip deze functie en geef true terug
    if (pFieldVan == null || pFieldTot == null) {
        return true;
    }

    var datumVan = pFieldVan.value;
    var datumTot = pFieldTot.value;
    var datumPat = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
    var matchArrayVan = datumVan.match(datumPat);
    var matchArrayTot = datumTot.match(datumPat);
    var dateVan = new Date();
    dateVan.setFullYear(matchArrayVan[3], matchArrayVan[2] - 1, matchArrayVan[1]);
    var dateTot = new Date();
    dateTot.setFullYear(matchArrayTot[3], matchArrayTot[2] - 1, matchArrayTot[1]);
    if (dateTot >= dateVan) {
        pFieldVan.style.background = 'white';
        pFieldTot.style.background = 'white';
        return true;
    }
    else {
        pFieldVan.style.background = '#ff8080';
        pFieldTot.style.background = '#ff8080';
        return false;
    }
}

function isCorrectDate(pField)
{
    var datum = pField.value;
    var datumPat = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
    var matchArray = datum.match(datumPat);

    var ok = true;

    if (matchArray != null)
    {
        day = matchArray[1];
        month = matchArray[2];
        year = matchArray[3];

        if (month < 0 || month > 12)
        {
            ok = false;
        }

        if (day > daysInMonth(month, year))
        {
            ok = false;
        }

        if (year < 1900 || year > 2100)
        {
            ok = false;
        }
    }
    else
    {
        ok = false;
    }

    if (!ok)
    {
        pField.style.background = '#ff8080';
        return false;
    }
    else
    {
        pField.style.background = 'white';
        return true;
    }
}

function daysInMonth(month, year)
{
    if (month == 2)
    {
        return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
    }
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    {
        return 31;
    }
    return 30;
}

function CheckEmail(pField)
{
    var rejectedDomain = new Array();
    var index = 0;
    rejectedDomain[index++] = 'rocketmail';
    rejectedDomain[index++] = 'zdnetmail';

    var rejected = false;
    var testresults = true;
    var str = pField.value;
    var filter = /^.+@.+\..{2,3}$/;
    if (filter.test(str))
    {
        var tempstring = str.split('@');
        tempstring = tempstring[1].split('.');
        for (i = 0; i < rejectedDomain.length; i++)
        {
            if (tempstring[0] == rejectedDomain[i])
                rejected = true;
        }
        if (rejected)
        {
            var message = 'Gelieve een meer officieel adres in te geven!\n';
            message += 'De volgende webhosts zijn verboden:\n';
            for (i = 0; i < rejectedDomain.length; i++)
            {
                message += '\t' + rejectedDomain[i] + '\n';
            }
            testresults = false;
        }
    }
    else
    {
        testresults = false;
    }
    return testresults;
}

function popUpUrl(pUrlField, pFoutBoodschap)
{
    var http = 'http://';
    url = pUrlField.value;

    if (url == '')
    {
        window.alert(pFoutBoodschap);
    }
    else
    {
        if (url.substring(0, http.length) != http)
        {
            url = http + url;
        }
        window.open(url);
    }
}

function minstens1Ingevuld(pField)
{
    for (i = 0; i < pField.length; i++)
    {
        if (pField[i].checked)
        {
            return true;
        }
    }
    return false;
}

function addEuroSign(pField)
{
    if (pField.selectionStart)
    {
        pField.value = pField.value.substring(0, pField.selectionStart) + '\u20ac' + pField.value.substring(pField.selectionStart)
    }
    else if (pField.createTextRange && pField.caretPos)
    {
        var vCaretPos = pField.caretPos;
        vCaretPos.text = vCaretPos.text.charAt(vCaretPos.text.length - 1) == ' ' ? '\u20ac' + ' ' : '\u20ac';
    }
    else
    {
        pField.value = pField.value + '\u20ac';
    }
}

function bewaarCursor(pField)
{
    if (pField.createTextRange)
    {
        pField.caretPos = document.selection.createRange().duplicate();
    }
}

function telKarakters(pTag, pWaarde, pLabel)
{
    // Strip out all line breaks.
    var strSingleLineText = pWaarde.replace(new RegExp("\\n", "g"), '..');

    document.getElementById(pTag).innerHTML = pLabel + ': ' + strSingleLineText.length;
}

function addLoadEvent(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function addUnLoadEvent(func)
{
    var oldunonload = window.onunload;
    if (typeof window.onunload != 'function') {
        window.onunload = func;
    }
    else {
        window.onunload = function() {
            if (oldonunload) {
                oldonunload();
            }
            func();
        }
    }
}
function checkFileType(field) {
    if (field!=null) {

      var ext = field.value;

      if (ext!=null && ext.length > 0) {

          ext = ext.substring(ext.length-3,ext.length);
          ext = ext.toLowerCase();
          return ext == 'jpg' || ext == 'gif';
      }
   }
   return true;
}
function showBig(id){
	$(id+'_small').hide();
	$(id+'_big').appear();
}

function showSmall(id){
	$(id+'_big').hide();
	$(id+'_small').appear();
}
function setPPrijs()
{

    var pPrijs = document.getElementById("free");
    if (pPrijs.checked)
    {

        document.getElementById("price").value = 0;
        document.getElementById("price").readOnly = true;
    }
    else
    {

        document.getElementById("price").value = "";
        document.getElementById("price").readOnly = false;
    }
}
