function encode_utf8( s )
{
  return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s )
{
  return decodeURIComponent( escape( s ) );
}



/*

                   Ajax Gold JavaScript Library
            ** No warranty is expressed or implied. **

  The Ajax Gold JavaScript Library executes in a web browser and allows 
  you to fetch data from the server behind the scenes, using JavaScript 
  without having the browser reload the current page (which would cause 
  the screen to flicker and reset while waiting for the page to be 
  loaded from the server), which is what Ajax is all about. This 
  library has been designed to be thread-safe.

  To use this JavaScript library in your own web pages, place 
  ajaxgold.js in the same directory as your web pages and use this 
  line in the <head> section of your pages:

  <script type = "text/javascript" src = "ajaxgold.js"></script>

  This library supports these functions for using Ajax (most commonly 
  used: getDataReturnText and getDataReturnXml):

  getDataReturnText(url, callback) 
    ** Uses the GET method to get text from the server. **
    Gets text from url, calls function named callback with that text.
    Use when you just want to get data from an URL, or can easily   
    encode the data you want to pass to the server in an URL, such as 
    "http://localhost/script.php?a=1&b=2&c=hello+there".
    Example: getDataReturnText("http://localhost/data.txt", doWork); 
    Here, the URL is a string, and doWork is a function in your own 
    script.

  getDataReturnXml(url, callback) 
    ** Uses the GET method to get XML from the server. **
    Gets XML from url, calls function named callback with that XML.
    Use when you just want to get data from an URL, or can easily   
    encode the data you want to pass to the server in an URL, such as 
    "http://localhost/script.php?a=1&b=2&c=hello+there".
    Example: getDataReturnXml("http://localhost/data.txt", doWork); 
    Here, the URL is a string, and doWork is a function in your 
    own script. You can recover XML elements from the XML object 
    passed to your callback function using JavaScript methods like 
    getElementsByTagName.

  postDataReturnText(url, data, callback) 
    ** Uses the POST method to send data to server, gets text back. **
    Posts data to url, calls function callback with the returned text.
    Uses the POST method, use this when you have more text data to send 
    to the server than can be easily encoded into an URL.
    Example: postDataReturnText("http://localhost/data.php", 
      "parameter=5", doWork); 
    Here, the URL is a string, the data sent to the server 
    ("parameter=5") is a string, and doWork is a function in 
    your own script.

  postDataReturnXml(url, data, callback) 
    ** Uses the POST method to send data to server, gets XML back. **
    Posts data to url, calls function callback with the returned XML.
    Uses the POST method, use this when you have more text data to send 
    to the server than can be easily encoded into an URL.
    Example: postDataReturnXml("http://localhost/data.php", 
      "parameter=5", doWork); 
    Here, the URL is a string, the data sent to the server 
    ("parameter=5") is a string, and doWork is a function in 
    your own script. You can recover XML elements from the XML object 
    passed to your callback function using JavaScript methods like 
    getElementsByTagName.

  Bear in mind that the URL you want to fetch data from has to be in
  the same domain as your web page that uses Ajax Gold methods or, as
  with any Ajax application, you'll get a security warning. If you want
  to fetch data from another domain, have your server-side program do 
  the fetching and send the fetched data back to your Ajax application.

*/



function getDataReturnText(url, callback)
{ 

  var XMLHttpRequestObject = false; 

  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new 
     ActiveXObject("Microsoft.XMLHTTP");
  }

  if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open("GET", url); 

    XMLHttpRequestObject.onreadystatechange = function() 
    { 
      if (XMLHttpRequestObject.readyState == 4 && 
        XMLHttpRequestObject.status == 200) { 
          callback(XMLHttpRequestObject.responseText); 
          delete XMLHttpRequestObject;
          XMLHttpRequestObject = null;
      } 
    } 

    XMLHttpRequestObject.send(null); 
  }
}

function getDataReturnXml(url, callback)
{ 
  var XMLHttpRequestObject = false; 

  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new 
     ActiveXObject("Microsoft.XMLHTTP");
  }

  if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open("GET", url); 

    XMLHttpRequestObject.onreadystatechange = function() 
    { 
      if (XMLHttpRequestObject.readyState == 4 && 
        XMLHttpRequestObject.status == 200) { 
          callback(XMLHttpRequestObject.responseXML); 
          delete XMLHttpRequestObject;
          XMLHttpRequestObject = null;
      } 
    } 

    XMLHttpRequestObject.send(null); 
  }
}

function postDataReturnText(url, data, callback)
{ 



  var XMLHttpRequestObject = false; 

  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new 
     ActiveXObject("Microsoft.XMLHTTP");
  }

  if(XMLHttpRequestObject) {

    XMLHttpRequestObject.open("POST", url); 

    XMLHttpRequestObject.setRequestHeader('Content-Type', 
      'application/x-www-form-urlencoded'); 

    XMLHttpRequestObject.onreadystatechange = function() 
    { 
      if (XMLHttpRequestObject.readyState == 4 && 
        XMLHttpRequestObject.status == 200) {
          callback(XMLHttpRequestObject.responseText); 
          delete XMLHttpRequestObject;
          XMLHttpRequestObject = null;
      } 
    }

    XMLHttpRequestObject.send(data); 
  }
}

function postDataReturnXml(url, data, callback)
{ 



  var XMLHttpRequestObject = false; 

  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new 
     ActiveXObject("Microsoft.XMLHTTP");
  }

  if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open("POST", url); 
    XMLHttpRequestObject.setRequestHeader('Content-Type', 
      'application/x-www-form-urlencoded'); 

    XMLHttpRequestObject.onreadystatechange = function() 
    { 
      if (XMLHttpRequestObject.readyState == 4 && 
        XMLHttpRequestObject.status == 200) {
          callback(XMLHttpRequestObject.responseXML); 
          delete XMLHttpRequestObject;
          XMLHttpRequestObject = null;
      } 
    }

    XMLHttpRequestObject.send(data); 
  }

}

var hostName2;

function initDomain(event,dom){
    window.hostName2=dom;
    //alert(hostName2);//alerts the right string!

    return;
}


var indexFile = "index.php";

//var theUrl=window.hostName2+indexFile+"?class=User&action=Ajax&action2=";
//var twUrl=window.hostName2+indexFile+"?class=Teamweb&action=Ajax&action2=";
//var proUrl=theUrl + "province";
//var munUrl=theUrl + "municipality";
//var citUrl=theUrl + "city";
//var cluUrl=theUrl + "club";
//var sugUrl=theUrl + "suggestName";
//var sugTURL=twUrl + "suggesteamwebName";

function checkCellphoneExists(event){


    var theUrl = window.hostName2+indexFile+"?class=User&action=Ajax&action2=";
    var cellUrl = theUrl + "checkCellphone";
    var elt = document.getElementById('cellphone');

    if(elt.value!=""){
            postDataReturnText(cellUrl, 'input='+elt.value, cellCallback);
    }
}

function cellCallback(text){
    var targElt = document.getElementById('cellphone');
    var arr=text.split('|');

    if(arr[0] == 'true'){
        targElt.value='';
		document.getElementById('cell_exist').innerHTML=arr[1];
    }
	else
		document.getElementById('cell_exist').innerHTML = '';
}

function resetCellphone(msg){
	var res = confirm(msg);
	if(res){ //Only action on ok result
		//Disable services
		var theUrl = window.hostName2+indexFile+"?class=User&action=Ajax&action2=DisableSms";
		postDataReturnText(theUrl, '', resetCellCallback);
	}
}

function resetCellCallback(text){
	if(text == ''){ //Reset done, so enable fields
		//Enable field
		var cp = document.forms.profile.elements['cellphone'];
		cp.value = '';
		cp.disabled = false;
		
		var rc = document.getElementById('reset_cell');
		rc.innerHTML = '';
	}
}

function checkEmailExists(event){


    var theUrl = window.hostName2+indexFile+"?class=User&action=Ajax&action2=";
    var ceUrl = theUrl + "checkEmail";
    var elt = document.getElementById('email_');

    if(elt.value!=""){
            postDataReturnText(ceUrl, 'input='+elt.value, ceCallback);
    }
}

function ceCallback(text){
    var targElt = document.getElementById('email_');
    var arr=text.split('|');

    document.getElementById('email_targ').innerHTML=arr[1];
    if(arr[0] == 'true'){
        targElt.value='';
    }
}

function suggestTeamwebHostname(event,n, id){
       var twUrl=window.hostName2+indexFile+"?class=Teamweb&action=Ajax&action2=";
       var sugTURL=twUrl + "suggesteamwebName";
       var elt = document.getElementById(n);
       if(elt.value != ""){
            postDataReturnText(sugTURL, 'input_tw='+elt.value+"&twid="+id, sthCallback);
       }

}

function sthCallback(text){

    var targElt = document.getElementById('hostnamesug');
    var arr = text.split('|');
//alert(targElt);
    targElt.innerHTML = arr[1];
    document.getElementById('hostname').value=arr[0];

}
function requestUsernameSuggestion(event, elt_id){
    var elt = document.getElementById(elt_id);
    var theUrl=window.hostName2+indexFile+"?class=User&action=Ajax&action2=";
    var sugUrl=theUrl + "suggestName";
    if(elt.value != ""){
        postDataReturnText(sugUrl, 'input='+elt.value, rusCallback);
    }
}

function rusCallback(text){

    var targElt = document.getElementById('suggestion');
    var arr=text.split('|');

    targElt.innerHTML = arr[1];
    document.getElementById('username_').value=arr[0];

}


function initProfileForm(){
	//disable the province, municipality and city select element

	province = document.getElementById("province");
	mun = document.getElementById("municipality");
	city = document.getElementById("city");

	if(city.value==0){
		mun.disabled=true;
		city.disabled=true;
	}
}

function dummyRequest(url, data){
	if (window.XMLHttpRequest) {
    	XMLHttpRequestObject = new XMLHttpRequest();
  	} 
	else if (window.ActiveXObject) {
    	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  	}

  	if(XMLHttpRequestObject) {
    	XMLHttpRequestObject.open("POST", url); 
    	XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
   		XMLHttpRequestObject.send(data); 
  	}	
}

//target = the id of the select element the values should be in
function getProvinceList(event) {
		var theUrl=window.hostName2+indexFile+"?class=User&action=Ajax&action2=";
        var proUrl=theUrl + "province";

	countryElt = document.getElementById('country');

	//grab list from server using ajax
	dummyRequest(proUrl, 'country='+countryElt.value);
	postDataReturnText(proUrl, 'country='+countryElt.value, getProvinceListCallback);
	
	//update the municipality select element

}

function getProvinceListCallback(text) {
            elt=document.getElementById('province');
            var arr = text.split(',');
    
            //clean current list
            while(elt.length>0){
                    elt.remove(elt.length-1);
            }
    
			//make a choice entry
			if ( document.getElementById('make_choice') != undefined ) {
				var x = document.createElement('option');
				x.text= document.getElementById('make_choice').value+'  ';
				x.value=0;
				try {
                            elt.add(x,null); // standards compliant
				}
				catch(ex){
                            elt.add(x); // IE only
				}

			}

            for(var i=0;i<arr.length;i++){
                    var tmp = arr[i].split('=>');
                    var y=document.createElement('option');
                    y.text=decode_utf8(tmp[1])+" ";		
                    y.value=tmp[0];
    
            try {
                            elt.add(y,null); // standards compliant
                    }
                    catch(ex) {
                            elt.add(y); // IE only
                    }
            }
            
                    getMunicipalityList(null);
        
}

function getMunicipalityList(event) {
        var theUrl=window.hostName2+indexFile+"?class=User&action=Ajax&action2=";   
        var munUrl=theUrl + "municipality";
	proElt = document.getElementById('province');

	//grab list from server using ajax
	dummyRequest(munUrl, 'province='+proElt.value);
	postDataReturnText(munUrl, 'province='+proElt.value, getMunicipalityListCallback);


}
function getMunicipalityListCallback(text) {

	elt = document.getElementById('municipality');
	elt.disabled=false;
	var arr = text.split(',');

	//clean current list
	while(elt.length>0){
		elt.remove(elt.length-1);
	}

		//make a choice entry
		if ( document.getElementById('make_choice') != undefined ) {
			var x = document.createElement('option');
			x.text= document.getElementById('make_choice').value+'  ';
			x.value=0;
			try {
						elt.add(x,null); // standards compliant
			}
			catch(ex){
						elt.add(x); // IE only
			}
	
		}
	
    if(arr[0] != 'none'){	
		for(var i=0;i<arr.length;i++){
			var tmp = arr[i].split('=>');
			var y=document.createElement('option');
			y.text=decode_utf8(tmp[1])+" ";		
			y.value=tmp[0];
	
			try {
				elt.add(y,null); // standards compliant
			}
			catch(ex) {
				elt.add(y); // IE only
			}
		}
	}
	else {
		elt.disabled = true;
	}
	getCityList(null);

}

function getCityList(event){
        var theUrl=window.hostName2+indexFile+"?class=User&action=Ajax&action2=";       
        var citUrl=theUrl + "city";

	munElt = document.getElementById('municipality');


	//grab list from server using ajax
	dummyRequest(citUrl, 'municipality='+munElt.value);
	postDataReturnText(citUrl, 'municipality='+munElt.value, getCityListCallback);


}
function getCityListCallback(text){
	elt = document.getElementById('city');
	var arr = text.split(',');
	elt.disabled=false;
	//clean current list
	while(elt.length>0){
		elt.remove(elt.length-1);
	}


	//make a choice entry
	if ( document.getElementById('make_choice') != undefined ) {
		var x = document.createElement('option');
		x.text= document.getElementById('make_choice').value+'  ';
		x.value=0;
		try {
					elt.add(x,null); // standards compliant
		}
		catch(ex){
					elt.add(x); // IE only
		}

	}


	if(arr[0] != 'none'){	
		for(var i=0;i<arr.length;i++){
			var tmp = arr[i].split('=>');
			var y=document.createElement('option');
			y.text=decode_utf8(tmp[1])+" ";		
			y.value=tmp[0];
	
			try {
				elt.add(y,null); // standards compliant
			}
			catch(ex) {
				elt.add(y); // IE only
			}
		}	
	}
	else {
		elt.disabled = true;
	}
	//alert(document.getElementById('club'));
	if(document.getElementById('club') != null) {
		getClubList(null);
	}
}

function getClubList(event){
        var theUrl=window.hostName2+indexFile+"?class=User&action=Ajax&action2=";
        var cluUrl=theUrl + "club";
	citElt = document.getElementById('city');
	//grab list from server using ajax
	dummyRequest(cluUrl, 'city='+citElt.value);
	postDataReturnText(cluUrl, 'city='+citElt.value, getClubListCallback);


}
function getClubListCallback(text){
	elt = document.getElementById('club');
	var arr = text.split(',');
	//clean current list
	while(elt.length>0){
		elt.remove(elt.length-1);
	}

	if(arr[0]=="error"){
			var y=document.createElement('option');
			y.text=tmp[1];		


			try {
				elt.add(y,null); // standards compliant
			}
			catch(ex) {
				elt.add(y); // IE only
			}
			elt.disabled=true;
	}
	else {

                var n = document.createElement('option');
                n.text='';
                n.value='';

                try{
	                elt.add(n,null); // standards compliant
                }
                catch(ex) {
        		elt.add(n); // IE only
                }
		for(var i=0;i<arr.length;i++){
			var tmp = arr[i].split('=>');
			var y=document.createElement('option');
			y.text=decode_utf8(tmp[1])+" ";		
			y.value=tmp[0];
			
			try {
				elt.add(y,null); // standards compliant
			}
			catch(ex) {
				elt.add(y); // IE only
			}
		}
	}
}

var lastSelectElementId=0;
var lastSelectedSport;//index
var idToRemove;



function initSportNumber(event, number){
     lastSelectElementId=number;

}

//addSportForm(event, 'clubtable', 'clubform');" 
function addSportForm(event, tablename, formname){

        nr_sports = document.getElementById('number_sports');
      
        var lastSelectElementId =nr_sports.value;
//alert(lastSelectElementId);
        var i = parseInt(lastSelectElementId);
        i = i+1;
		nr_sports.value = i;	

	var lastEltId="sport_"+lastSelectElementId;
//alert(lastSelectElementId);

	var found = false;
/*	while(!found){
		lastEltId="sport_"+lastSelectElementId;
		var obj = document.getElementById(lastEltId);
		if(obj) {//See if object exists
			found = true;
		}
		else {
			lastSelectElementId++;
		}
	}
*/

	
	idToRemove++;
	
	//get last selected item, so that it is removed from this list.
	var lastSelElt = document.getElementById(formname).elements[lastEltId];
	
	var ind = lastSelElt.selectedIndex;
	
	//create new select element
	lastSelectElementId++;
	var selElt = document.createElement('select');
	selElt.id="sport_"+lastSelectElementId;
	selElt.name="sport_"+lastSelectElementId;

        var optElt = document.createElement('option');
        optElt.text = 'Maak een keuze...';
        optElt.value= ' ';

        try {
                selElt.add(optElt,null); // standards compliant
        }
        catch(ex) {
                selElt.add(optElt); // IE only
        }

	for(var i=0;i<lastSelElt.length;i++) {
		if (i != ind) {
			var optElt = document.createElement('option');
			optElt.text = lastSelElt.options[i].text;
			optElt.value= lastSelElt.options[i].value;
	
			try {
				selElt.add(optElt,null); // standards compliant
			}
			catch(ex) {
				selElt.add(optElt); // IE only
		}
        }
	}
	
	//get clubtable
	var ix;
	if(tablename == "clubtable"){
		ix=1;
	}
	else {
		ix=2;
	}

	var tableElt = document.getElementById(tablename);

	var tableRow = tableElt.insertRow(tableElt.rows.length-ix);	
	
	var tableCell = tableRow.insertCell(0);
	var tableCell2 = tableRow.insertCell(1);
	var tableCell3 = tableRow.insertCell(2);	
	//append to table
    if(tablename == "clubtable")
		tableCell.appendChild(selElt);
	else
		tableCell2.appendChild(selElt);

}

function removeSportForm(event,tablename){
	elt = document.getElementById('number_sports');
	idToRemove = elt.value;

	if(idToRemove>0){
		var s = "sport_"+idToRemove;

		var x = document.getElementById(s);

		x.parentNode.removeChild(x);
		alert(x.parentNode);

		elt.value = idToRemove-1;


	}

}


function selectPhotoPreview(event, album_id, photo_id){
	var pUrl=window.hostName2+indexFile+"?class=PhotoAlbum&action=Ajax&action2=";
	var phoUrl = pUrl + "selectPreview";

    postDataReturnText(phoUrl, 'photo_id='+photo_id+'&album_id='+album_id, phoCallback);
}

function phoCallback(text){
    

}

function enableDisableClubForm(event, b){
		var connected = false;
		var cc = document.forms.teamweb.elements['club_connected'];
		if(cc[0].checked){
			connected = true;
		}
			
		if(connected){
            document.getElementById('country').disabled=false;
            document.getElementById('province').disabled=false;
            document.getElementById('municipality').disabled=false;
            document.getElementById('city').disabled=false;
            document.getElementById('club').disabled=false;
            document.getElementById('club_add').style.visibility="visible";
        }
        else {

            document.getElementById('country').disabled=true;
            document.getElementById('province').disabled=true;
            document.getElementById('municipality').disabled=true;
            document.getElementById('city').disabled=true;
            document.getElementById('club').selectedIndex=0;
            document.getElementById('club').disabled=true;
            document.getElementById('club_add').style.visibility="hidden";
        }

        return;
}


var plaats='',straat='';

function checkpc(postcode,id) {
	postcode = postcode.replace(' ', '');
	if (postcode.length == 6) {
		postcode = postcode.toUpperCase();
		document.getElementById('postcode').value = postcode;

		var script = document.createElement('script'); 
		script.type = 'text/javascript'; 
		script.src = "http://www.postcodeboek.info/api/apipostcode.cgi?pc="+postcode+"&id="+id; 
		document.getElementsByTagName('head')[0].appendChild(script);  

	}
}


function invullen() {
	document.getElementById('straat').value = straat;
	document.getElementById('plaats').value = plaats;
}
