//Helper functions
var dtCh	= "-";
var minYear	= 1900;
var maxYear	= 2100;

function trim(cString) {
	var i;
    var returnString 	= '';
    var bag				= ' ';
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < cString.length; i++){   
        var c = cString.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

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 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
}

//Controleer of een datum geldig is. (dd-mm-jjjj)
function checkDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=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){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
	
	return true
}

//Controleer of een datum groter is dan een 2e datum.
function checkDateLater(dtStr1, dtStr2) {
    //String splitten in dag-maand-jaar
    laDate1 = dtStr1.split("-");
    laDate2 = dtStr2.split("-");

    //Datum objecten maken
    laDate1 = new Date(laDate1[2], laDate1[1], laDate1[0]);
    laDate2 = new Date(laDate2[2], laDate2[1], laDate2[0]);

    if(laDate1 < laDate2) {
        return false;
    }
    
    return true;
}

//Controleer of een invoer een integer is.
function checkInteger(cStr) {
	return isInteger(cStr);
}

//Controleer of een invoer numeriek is.
function checkNumeric(cStr) {
	var strValidChars = "0123456789.-";
   	var strChar;
   	var blnResult = true;

   	if (cStr.length == 0) return false;

   	//  test strString consists of valid characters listed above
   	for (i = 0; i < cStr.length && blnResult == true; i++) {
      	strChar = cStr.charAt(i);
      	if (strValidChars.indexOf(strChar) == -1) {
         	blnResult = false;
        }
	}
	
   	return blnResult;
}

//Open een zoek form.
function openZoek(cObject, cZoekScherm) {
    window.open('index.php?cpage='+ cZoekScherm +'&nomenu=true&veld='+ cObject +'', 'Zoeken', 'status,width=600,height=260'); 
}

//Geef pk terug aan parent form object.
function geefPk(cVeld, cPk) {
    window.opener.eval('document.getElementById(\''+ cVeld +'\').value = \''+ cPk +'\';');
    window.opener.eval('try { document.getElementById(\''+ cVeld +'\').focus(); } catch(e) {}');
    window.opener.eval('try { document.getElementById(\''+ cVeld +'\').onchange(); } catch(e) {}');
    
    window.close();
}

//Ajax aanvraag voor controle invoer
function replaceAmp(cValue) {
    var lcValue = "";
    
    lcValue = cValue.replace('&', '');
    
    return lcValue;
}

function validate(cForm) {
    if(window.ActiveXObject) {      //IE5
        loXML = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        loXML = new XMLHttpRequest();
    }
 
    var lcElements = '';

    laElements = document.getElementById(cForm).getElementsByTagName('input');
    for(var i = 0; i < laElements.length; i++) {
        lcElements += laElements[i].id +'='+ replaceAmp(laElements[i].value) +'&';
    }
    
    laElements = document.getElementById(cForm).getElementsByTagName('select');
    for(var i = 0; i < laElements.length; i++) {
        lcElements += laElements[i].id +'='+ replaceAmp(laElements[i].value) +'&';
    }    

    laElements = document.getElementById(cForm).getElementsByTagName('textarea');
    for(var i = 0; i < laElements.length; i++) {
        lcElements += laElements[i].id +'='+ replaceAmp(laElements[i].value) +'&';
    }     

    try {   
        loXML.open('post', 'pages/othersavechecks.php?form='+ cForm, false);
        loXML.setRequestHeader('Content-Type',  'application/x-www-form-urlencoded');
        loXML.setRequestHeader('Cache-Control', 'no-cache');
        loXML.send(lcElements);
        
        lcResponse = loXML.responseText;
    } catch(cError) {
        alert("Error:"+ cError);
    }
    
    return lcResponse;
}

function changePage(cUrl) {
	//Pagina value opvragen.
	lnPage = document.getElementById('nPage').value;
	lnRecs = document.getElementById('nPPage').value;
		
	//Pagina verversen.
	location.href = cUrl + '&nPage=' + lnPage + '&nPPage=' + lnRecs;
}

function YesNo() {
	return window.confirm('Weet u zeker dat u alle wedstrijden verwerkt hebt?\nBij Ok kunt u geen wijzigingen meer doorvoeren.');
}
