var globalAjaxDestination = "/ajax/";

//var globalAjaxRequestObject = this.createRequestObject();

var globalAjaxQueue = new Array();

var pageIsLoaded = false;

function nl2br(myString) {
  var regX = /\n/gi ;

  s = new String(myString);
  s = s.replace(regX, "<br /> \n");
  return s;
}


function checkPageIsLoaded(silent){
  if(!pageIsLoaded && !silent) alert('De pagina is nog niet helemaal geladen.\nAls de pagina volledig geladen is, is deze functie beschikbaar.');
  return pageIsLoaded;
}


function createRequestObject(){
  var requestObject;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer"){
    requestObject = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    requestObject = new XMLHttpRequest();
  }
  return requestObject;
}

function sendAjax(requestObject,doAlert,doFunc,ignoreEncoding){
	var globalAjaxRequestObject = this.createRequestObject();
	//fix quotes and some other illegal chars in request
	var myRequestObject = requestObject;
	//if(!ignoreEncoding && !myRequestObject.ignoreEncoding){
		//myRequestObject = iterate(myRequestObject,xmlEncode);
	//	jsonRequestString = ( JSON.encode(myRequestObject) );
	//} else {
		//added unescape to fix double encoding
	//	jsonRequestString = (JSON.encode(myRequestObject));
	//}
    
    var jsonRequestString = JSON.encode(myRequestObject);

	globalAjaxRequestObject.open('post', globalAjaxDestination);
	globalAjaxRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	var sendData = "jsonrequest="+encodeURIComponent(jsonRequestString);
	
	globalAjaxRequestObject.send(sendData);
	
	globalAjaxRequestObject.onreadystatechange = function () {
		if(globalAjaxRequestObject.readyState == 4){
			if(globalAjaxRequestObject.status==200){
                var response = eval('(' + globalAjaxRequestObject.responseText + ')');
				
				if(doAlert == 'message'){
					alert(response["message"]);
				}else if(doAlert){
					alert('Status ok\nResponse:\n'+ globalAjaxRequestObject.responseText);
				}
				if (typeof doFunc === 'function') {
					//var response = eval('(' + globalAjaxRequestObject.responseText + ')');
					//response = iterate(response, xmlDecode);
					doFunc(response);
				} else if (doFunc) {
					var txt = globalAjaxRequestObject.responseText;
					eval(doFunc + '('+txt+');');
					//doFunc(response);
				}
			}else{
                /*
                Naast de 'normale' fouten (4xx, 5xx) zijn er ook non-standard status codes (IE):                 
                    12002 - Server timeout
                    12029,12030, 12031 - dropped connections (either web server or DB server)
                    12152 - Connection closed by server.
                    13030 - StatusText properties are unavailable, and a query attempt throws an exception
                */
				alert('Uw verzoek kon niet verwerkt worden, uw browser kan geen verbinding krijgen met de website.\nHerlaad de pagina en probeer het a.u.b. opnieuw.\n\nFout bij ajax gegevensverwerking ' + requestObject.action + ',' + globalAjaxRequestObject.status + '\nDetails:\n' + globalAjaxRequestObject.responseText);
			}
		}
	};
}

function ajaxQueue() {
	if (globalAjaxQueue.length) {
		var requestObject = globalAjaxQueue.splice(0,1);
		sendAjax(requestObject[0],false,'ajaxQueue');
	}
}

function fillCheck(el,text){
    if(el.value==''){
      el.value = text;
    }else if(el.value==text){
      el.value = '';
    }
}

function fillCheckColored(el,text,colorFilled,colorEmpty){
    if(el.value==''){
      el.value = text;
	  el.style.color = colorEmpty;
    }else if(el.value==text){
      el.value = '';
	  el.style.color = colorFilled;
    }else{
		el.style.color = colorFilled;
	}
}

function saveSingleProjectFileValue(input,key,defaultvalue,projectFileId,callBackFunction){
  // use input field or direct value
  value = '';
  if(input.parentNode){
    value = input.value;
  }else{
    value = input;
  }

  if(value=='' && defaultvalue!=''){
    alert(key + ' is een verplicht veld.');
    //input.value = defaultvalue;
	input.value = input.defaultValue;
  }else{
    requestObject = {
      action: "saveSingleProjectFileValue",
      data: {
        projectFileId: projectFileId,
        key: key,
        value: value
      }
    };
    sendAjax(requestObject,false,callBackFunction);
	input.defaultValue = value;
  }
}

function saveSingleInvitationValue(input,key,allowEmpty,invitationId,callBackFunction){
  // use input field or direct value
  value = '';
  if(input.parentNode){
    value = input.value;
  }else{
    value = input;
  }

  if(value==='' && !allowEmpty){
    alert(key + ' is een verplicht veld.');
    input.value = defaultvalue;
  }else{
    requestObject = {
      action: "saveSingleInvitationValue",
      data: {
        invitationId: invitationId,
        key: key,
        value: value
      }
    };
    sendAjax(requestObject,false,callBackFunction);
  }
}

function saveSingleInvitationPhaseValue(input,key,allowEmpty,invitationPhaseId,callBackFunction){
  // use input field or direct value
  value = '';
  if(input.parentNode){
    value = input.value;
  }else{
    value = input;
  }

  if(value==='' && !allowEmpty){
    alert(key + ' is een verplicht veld.');
    input.value = defaultvalue;
  }else{
    requestObject = {
      action: "saveSingleInvitationPhaseValue",
      data: {
        invitationPhaseId: invitationPhaseId,
        key: key,
        value: value
      }
    };
    sendAjax(requestObject,false,callBackFunction);
  }
}

function saveMultipleProjectFilesValue(input,key,defaultvalue,projectFileIds,callBackFunction){

  // use input field or direct value
  value = '';
  if(input.parentNode){
    value = input.value;
  }else{
    value = input;
  }

  //if(value=='' && defaultvalue!=''){
  //  alert(key + ' is een verplicht veld.');
  //  input.value = defaultvalue;
  //}else{
    requestObject = {
      action: "saveMultipleProjectFilesValue",
      data: {
        projectFileIds: projectFileIds,
        key: key,
        value: value,
        defaultvalue: defaultvalue
      }
    };
    sendAjax(requestObject,false,callBackFunction);
  }



function saveMultipleInvitationsValue(input,key,defaultvalue,invitationIds,callBackFunction){
	// use input field or direct value
	value = '';
	if(input.parentNode){
	  value = input.value;
	}else{
	  value = input;
	}
	//if(value=='' && defaultvalue!==''){
	//  alert(key + ' is een verplicht veld.');
	//  input.value = defaultvalue;
	//}else{
    requestObject = {
		action: "saveMultipleInvitationsValue",
		data: {
			invitationIds: invitationIds,
			key: key,
			value: value,
			defaultvalue: defaultvalue
		}
    };
	sendAjax(requestObject,false,callBackFunction);
}



function saveSingleProjectCategoryValue(input,key,allowEmpty,categoryId){
  if(input.value=='' && defaultvalue!=''){
    alert(key + ' is een verplicht veld.');
    //input.value = defaultvalue;
	input.value = input.defaultValue;
  }else{
	var inputValue = input.value;
    requestObject = {
      action: "saveSingleProjectCategoryValue",
      data: {
        categoryId: categoryId,
        key: key,
        value: input.value
      }
    };
    
    sendAjax(requestObject,false);
	input.defaultValue = input.value;
  }
}

function saveSingleMessageGroupValue(input,key,allowEmpty,messageGroupId){
  
  
  if(input.value=='' && !allowEmpty){
    alert(key + ' is een verplicht veld.');
    //input.value = defaultvalue;
	input.value = input.defaultValue;
  }else{
    requestObject = {
      action: "saveSingleMessageGroupValue",
      data: {
        messageGroupId: messageGroupId,
        key: key,
        value: input.value
      }
    };
    sendAjax(requestObject,false);
	input.defaultValue = input.value;
  }
}


function removeProjectDocument(documentId,callBackFunction){
  requestObject = {
    action: "removeProjectDocument",
    data: {
      id: documentId
    }
  };
  
  sendAjax(requestObject,false,callBackFunction);
}

function removeProject(projectId,callBackFunction,remote){
	requestObject = {
		action: "removeProject",
		data: {
			id: projectId,
			remote: remote
		}
	};
	sendAjax(requestObject,false,callBackFunction);
}

function saveCustomSelectSingleCompanyValue(value,fixedValue,name){

    requestObject = {
      action: "saveSingleCompanyValue",
      data: {
        key: fixedValue,
        value: value
      }
    };
    
    sendAjax(requestObject,false,'processUpdateMissingPercentages');

}

function saveSingleSpecialistValue(input, key, allowzero, specId){
  value = input.value;
  
  if(!allowzero && value.replace(/\s/g,"") == ""){
    alert('Invoer verplicht! Lege waarde is niet opgeslagen'); 
  }else{
    input.style.border = '1px solid #00ff00';
    
    requestObject = {
      action: "saveSingleCompanyValue",
      data: {
        specId: specId,
        key: key,
        value: value
      }
    };
    sendAjax(requestObject,false);

  }
}
function checkProjectSize(input, key, allowzero, specId, formId){
  var value = input.value;
  if(!allowzero && value.replace(/\s/g,"") == ""){
    alert('Invoer verplicht! Lege waarde is niet opgeslagen'); 
  }else{
	//check selectedIndex
	var selMin = document.getElementById('projectSizeMin');
	var selMax = document.getElementById('projectSizeMax');
	//am i min and >
	if(key == 'projectSizeMin' && selMin.selectedIndex >= selMax.selectedIndex+2){
		selMax.selectedIndex = (selMin.selectedIndex-1);
		fireMyEvent(selMax,'change');
	} else if (key == 'projectSizeMax' && selMax.selectedIndex <= selMin.selectedIndex-2){
		selMin.selectedIndex = (selMax.selectedIndex+1);
		fireMyEvent(selMin, 'change');
	}
	//set option of other to + 1
	//am i max and <
	//set option of other to -1
    input.style.border = '1px solid #00ff00';
    
    requestObject = {
      action: "saveSingleCompanyValue",
      data: {
        specId: specId,
        key: key,
        value: value
      }
    };
    sendAjax(requestObject,false);

  }
}
function fixProjectSize(input, key, allowzero, specId, formId){
	var value = input.value;
  
	//check selectedIndex
	var selMin = document.getElementById('projectSizeMin');
	var selMax = document.getElementById('projectSizeMax');
	//am i min and >
	if(key == 'projectSizeMin' && selMin.selectedIndex >= selMax.selectedIndex+2){
		selMax.selectedIndex = (selMin.selectedIndex-1);
	} else if (key == 'projectSizeMax' && selMax.selectedIndex <= selMin.selectedIndex-2){
		selMin.selectedIndex = (selMax.selectedIndex+1);
	}
	//set option of other to + 1
	//am i max and <
	//set option of other to -1
}
function saveSingleCompanyValue(input, key, allowzero, updateMissingCounters){
	//debugger;
	value = input.value;
  //  alert(updateMissingCounters);
	if(updateMissingCounters=='' || updateMissingCounters===true){
		process = true;      
	} else {
		process = false;
	  
	}                 
  
	if(!allowzero && value.replace(/\s/g,"") == ""){
		alert('Invoer verplicht! Lege waarde is niet opgeslagen'); 
	}else{
		input.style.border = '1px solid #00ff00';
		
		requestObject = {
			action: "saveSingleCompanyValue",
			data: {
				key: key,
				value: value
			}
		};
	  if(process==true){    
			sendAjax(requestObject,false,'processUpdateMissingPercentages');
	  } else {
			sendAjax(requestObject,false);
	  }
	}
}

function saveSingleProjectContactCheckbox(input, key, projectReadId, projectId, userId, type, updateMissingCounters){
  value = input.checked;  
//  alert(updateMissingCounters);
  if(updateMissingCounters==''){
    process = true;      
  } else {
    process = false;
    
  }                 
  input.style.border = '1px solid #00ff00';
    
  requestObject = {
    action: "saveSingleProjectContactValue",      
    data: {
        projectReadId: projectReadId,
        projectId: projectId,
        userId: userId,
        type: type,
        key: key,
        value: value
      }
    };
  if(process==true){    
    sendAjax(requestObject,false,'processUpdateMissingPercentages');
  } else {
    sendAjax(requestObject,false);
  }  
}

function saveContactProjectAccess(input,key,projectId){
	value = 0;
	if(input.checked){
		value = 1;
	}
	requestObject = {
	  action: "saveContactProjectAccess",      
	  data: {
		  projectId: projectId,
		  key: key,
		  value: value
		}
	  };
	  sendAjax(requestObject,false,false);
}

function saveSingleCompanyContactCheckbox(input, key, idContact, updateMissingCounters){
  //value = input.checked;
  
  if(input.checked=='1'){
    value = input.value;    
  } else {
    value = '';
  }
  
  
//  alert(updateMissingCounters);
  if(updateMissingCounters==''){
    process = true;      
  } else {
    process = false;
    
  }                 
  input.style.border = '1px solid #00ff00';
    
  requestObject = {
    action: "saveSingleCompanyContactValue",      
    data: {
        idContact: idContact,
        key: key,
        value: value
      }
    };
  if(process==true){    
    sendAjax(requestObject,false,'processUpdateMissingPercentages');
  } else {
    sendAjax(requestObject,false);
  }  
}

function saveSingleCompanyLogoValue(input, key, idLogo, allowzero, updateMissingCounters){
  value = input.value;
//  alert(updateMissingCounters);
  if(updateMissingCounters==''){
    process = true;      
  } else {
    process = false;
    
  }                 

  if(!allowzero && value.replace(/\s/g,"") == ""){
    alert('Invoer verplicht! Lege waarde is niet opgeslagen'); 
  }else{        
    input.style.border = '1px solid #00ff00';
    
    requestObject = {
      action: "saveSingleCompanyLogoValue",      
      data: {
        idLogo: idLogo,
        key: key,
        value: value
      }
    };
    if(process==true){    
      sendAjax(requestObject,false,'processUpdateMissingPercentages');
    } else {
      sendAjax(requestObject,false);
    }
  }
}

function saveSingleSpecialistAddressValue(input, key, allowzero, specId){
  value = input.value;

  if(!allowzero && value.replace(/\s/g,"") == ""){
    alert('Invoer verplicht! Lege waarde is niet opgeslagen'); 
  }else{
    input.style.border = '1px solid #00ff00';
    requestObject = {
      action: "saveSingleCompanyAddressValue",
      data: {
        specId: specId,
        key: key,
        value: value
      }
    };
     
    sendAjax(requestObject,false,false);
  }
}

function saveSingleCompanyAddressValue(input, key, allowzero, addressType){
  value = input.value;
  if(!addressType) addressType = 'post';
  if(!allowzero && value.replace(/\s/g,"") == ""){
    alert('Invoer verplicht! Lege waarde is niet opgeslagen'); 
  }else{
    input.style.border = '1px solid #00ff00';

    requestObject = {
      action: "saveSingleCompanyAddressValue",
      data: {
		posttype: addressType,
        key: key,
        value: value
      }
    };
     
    sendAjax(requestObject,false,false);
  }
}
function saveSingleProjectValue(input, key, allowzero){

	value = input.value;
	if(input.type=='checkbox' && !input.checked){
		value = '';
	}

  if(!allowzero && value.replace(/\s/g,"") == ""){
    alert('Invoer verplicht! Lege waarde is niet opgeslagen'); 
  }else{
    input.style.border = '1px solid #00ff00';

    requestObject = {
      action: "saveSingleProjectValue",
      data: {
        key: key,
        value: value
      }
    };
    
    sendAjax(requestObject,false,false);
  }
}
function saveSingleOtherProjectValue(input, key, allowzero, projectId){
  value = input.value;

  if(!allowzero && value.replace(/\s/g,"") == ""){
    alert('Invoer verplicht! Lege waarde is niet opgeslagen'); 
  }else{
    input.style.border = '1px solid #00ff00';
    
    requestObject = {
      action: "saveSingleOtherProjectValue",
      data: {
        projectid: projectId,
        key: key,
        value: value
      }
    };
    
    sendAjax(requestObject,false,false);
  }
}

function updateMissingCounters(){
  requestObject = {
    action: "getMissingCounters",
    data: {
      nodata: 'null'
    }
  };
  
  sendAjax(requestObject,false,'processUpdateMissingPercentages');
};

function processUpdateMissingCounters(respons){
  if(respons.data[1]>0){
    baseMissing.innerHTML = ' ('+ respons.data[1] +')';
  }else{
    baseMissing.innerHTML = ' ';
  }
  if(respons.data[2]>0){
    contactMissing.innerHTML = ' ('+ respons.data[2] +')';
  }else{
    contactMissing.innerHTML = ' ';
  }
  if(respons.data[3]>0){
    stabuMissing.innerHTML = ' ('+ respons.data[3] +')';
  }else{
    stabuMissing.innerHTML = ' ';
  }
  if(respons.data[4]>0){
    cetificateMissing.innerHTML = ' ('+ respons.data[4] +')';
  }else{
    cetificateMissing.innerHTML = ' ';
  }
  if(respons.data[5]>=0){
    totalMissing.innerHTML = ' ('+ respons.data[5] +')';
  }else{
    totalMissing.innerHTML = ' ';
  }
}
function processUpdateMissingPercentages(respons){
	//debugger;
	if(respons.data['basePercentage']>0){
		baseMissing.innerHTML = ' ('+ respons.data['basePercentage'] +'%)';
	}else{
		baseMissing.innerHTML = ' ';
	}
	if(respons.data['contactPercentage']>0){
		contactMissing.innerHTML = ' ('+ respons.data['contactPercentage'] +'%)';
	}else{
		contactMissing.innerHTML = ' ';
	}
	if(respons.data['stabuPercentage']>0){
		stabuMissing.innerHTML = ' ('+ respons.data['stabuPercentage'] +'%)';
	}else{
		stabuMissing.innerHTML = ' ';
	}
	if(respons.data[4]>0){
		//certificateMissing.innerHTML = ' ('+ respons.data['certificatePercentage'] +'%)';
	}else{
		//certificateMissing.innerHTML = ' ';
	}
	if(respons.data['completedPercentage']>=0){
		totalMissing.innerHTML =  '(' + respons.data['completedPercentage'] +'%)';
	}else{
		totalMissing.innerHTML = ' ';
	}
}

function setSelectByValue(selectEl,optionValue){
	for (i=0; i<selectEl.options.length; i++) {
		if (selectEl.options[i].value == optionValue) {
			selectEl.selectedIndex = i;
		}
	}
}

function addOptionToSelect(selectEl,optionValue,optionName){

  var newOpt = new Option(optionName, optionValue);
  selectEl.options[selectEl.length] = newOpt;
  
}


function checkFilled(input, key, nullvalue, noPercent){
	
  value = input.value;
  elState = document.getElementById(key + '_state');    
  if(value==nullvalue && noPercent){
    elState.innerHTML = ' (!)';
  }else if(value==nullvalue){
    elState.innerHTML = ' (10%)';
  }else{
    elState.innerHTML = '';
  }  
}
function checkCustomSelectFilled(elId, key, nullvalue, errorText){

  valueEl = document.getElementById(elId);
  value = valueEl.innerHTML; 
  elState = document.getElementById(key + '_state');

  if(value==nullvalue){
	if (errorText){
	  elState.innerHTML = errorText;
	} else {
	  elState.innerHTML = ' (!)';	  
	}
  }else{
    elState.innerHTML = '';
  }
}

function removeCompanyContact(contactId){
  requestObject = {
    action: "removeCompanyContact",
    data: {
      id: contactId
    }
  };
  
  sendAjax(requestObject,false,'updateMissingCounters');
}
function removeSpecialistContact(contactId){
  requestObject = {
    action: "removeCompanyContact",
    data: {
      id: contactId
    }
  };
  
  sendAjax(requestObject,false,'postRemoveSpecialistContact');
}
function postRemoveSpecialistContact(obj){
	if(obj.error!=0){
		alert(obj.message);
	}else{
		rowEl = document.getElementById('contact_' + obj.data.id);
		rowEl.style.display = 'none';    
	}
}



function setResponseIndication(value,fixvalue){
  requestObject = {
    action: "setResponseIndication",
    data: {
      invitationid: fixvalue,
      value: value
    }
  };

  sendAjax(requestObject,false,false);
}
function setResponseIndicationPhase(value,fixvalue){
  requestObject = {
    action: "setResponseIndicationPhase",
    data: {
      invitationphaseid: fixvalue,
      value: value
    }
  };

  sendAjax(requestObject,false,false);
}


function in_array(string, array){ 
  for (var i = 0; i < array.length; i++) { 
    if(array[i] == string){ 
      return true; 
    } 
  } 
  return false; 
} 

function updateLogo(id, placement, origWidth, origHeight){
  var fullWidth = 700;  
  var container = document.getElementById('container_'+id);
  var image = document.getElementById('img_'+ id);

  if(placement=='center'){
    container.align = 'center';
    if(image.width==fullWidth){
      image.width = (image.height / origHeight) * origWidth;
    }
  }
  else if(placement=='right'){
    container.align = 'right';
    if(image.width==fullWidth){
      image.width = (image.height / origHeight) * origWidth;
    }

  }
  else if(placement=='justify'){
    container.align= 'center';    
    image.width = fullWidth;
  } else {
    container.align = 'left';
    if(image.width==fullWidth){
      image.width = (image.height / origHeight) * origWidth;
    }
  }
  if(image.width=='0'){
    image.width= (4/3) * image.height;
  }
}


function getX( oElement ){
  var iReturnValue = 0;
  while( oElement != null ) {
    iReturnValue += oElement.offsetLeft;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}
function getY( oElement ){
  var iReturnValue = 0;
  while( oElement != null ) {
    iReturnValue += oElement.offsetTop;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}

  function getPost(obj) {
    var pos = {'x':0,'y':0};
    if(obj.offsetParent) {
      while(1) {
        pos.x += obj.offsetLeft;
        pos.y += obj.offsetTop;
        if(!obj.offsetParent) {
          break;
        }
        obj = obj.offsetParent;
         }
     } else if(obj.x) {
         pos.x += obj.x;
         pos.y += obj.y;
     }
     return pos;
   }
   
function getValuesFromMultipleSelect(ob){
	var selected = new Array();
	for (var i = 0; i < ob.options.length; i++)
		if (ob.options[ i ].selected){
			selected.push(ob.options[ i ].value);
		}
	return selected
}

function reloadPage(){
	document.location = document.location;
}
//function to fire a specific event on a dom object, for "onclick" you pass 'click' as an argument
function fireMyEvent(element,event){
    if (document.createEventObject && !document.createEvent){
        // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt);
    }
    else{
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}
function editContact(contactId){
	var contactsForm = document.forms['1contact'];
	contactId = contactId;
	showPopUp('ContactEdit');
	requestObject = {
	  action: "getContact",
	  data: {
		id: contactId
	  }
	};
	sendAjax(requestObject,false,'fillContactForm');
}
function fillContactForm(obj){
	var contactForm = document.forms["1contact"];
	contactForm['contactid'].value = obj.data.id;
	contactForm['sex'].value = obj.data.sex;
	contactForm['jobTitle'].value = obj.data.jobTitle;
	contactForm['initials'].value = obj.data.initials;
	contactForm['infix'].value = obj.data.infix;
	contactForm['name'].value = obj.data.name;
	contactForm['phone'].value = obj.data.phone;
	//contactForm['mobile'].value = obj.data.mobile;
	//contactForm['fax'].value = obj.data.fax;
	contactForm['email'].value = obj.data.email;
		if(obj.data.favorite == 1){
        contactForm.favorite.checked = true;
    }
   
                 
	if(typeof(obj.data.login)!= 'undefined' && contactForm['login']){
		contactForm['login'].value = obj.data.login;
		contactForm['password'].value = obj.data.password;
	}         
	fireMyEvent(contactForm['sex'],'click');
}

//functie om input te fixen/encoden voordat ze verzonden wordt via een ajax request.
function xmlEncode(input){
    return input;
		var replace_with	=	'';
		if(typeof(input) == 'undefined')	{
			return;
		}
		input	=	input.toString();
		// The 'g' in the first argument is used to tell the function 'replace' 
		// that all occurences (g = global)
		// of the character in between slashes have to be replaced.
		//, the i stands for case insensitive
		replace_with		=	'&amp;';
		input 				= 	input.replace(/&/g,		replace_with);
		replace_with		=	'&lt;';
		input 				= 	input.replace(/</g,		replace_with);
		replace_with		=	'&gt;';
		input 				= 	input.replace(/>/g,		replace_with);
		replace_with		=	'';
		input 				= 	input.replace(/'/g,		replace_with);
		replace_with		=	'';
		input 				= 	input.replace(/"/g,		replace_with);
		replace_with		=	'&#043;';
		input 				= 	input.replace(/\+/g,	replace_with);
		replace_with		=	'';
		input 				= 	input.replace(/\\/g,	replace_with);
		replace_with		=	'&sect;';
		input 				= 	input.replace(/§/g,		replace_with);
		replace_with		=	'&ouml;';
		input 				= 	input.replace(/ö/gi,	replace_with);
		replace_with		=	'&auml;';
		input 				= 	input.replace(/ä/gi,	replace_with);
		replace_with		=	'&euml;';
		input 				= 	input.replace(/ë/gi,	replace_with);
		replace_with		=	'&iuml;';
		input 				= 	input.replace(/ï/gi,	replace_with);
		replace_with		=	'&uuml;';
		input 				= 	input.replace(/ü/gi,	replace_with);
		replace_with		=	'&eacute;';
		input 				= 	input.replace(/é/gi,	replace_with);
		replace_with		=	'&egrave;';
		input 				= 	input.replace(/è/gi,	replace_with);
		replace_with		=	'&agrave;';
		input 				= 	input.replace(/à/g,		replace_with);
		input = encodeURIComponent(input);
		return input.toString();
	}
//functie om input te fixen/encoden voordat ze verzonden wordt via een ajax request.
function xmlDecode(input, decodeQuotes){
    return input;
	var replace_with	=	'';
	if(typeof(input) == 'undefined')	{
		return;
	}
	input	=	input.toString();
	input = decodeURIComponent(input);
	//alert(input);
	// The 'g' in the first argument is used to tell the function 'replace' 
	// that all occurences (g = global)
	// of the character in between slashes have to be replaced.
	//, the i stands for case insensitive
	
	replace_with		=	'ö';
	input 				= 	input.replace(/&ouml;/gi,	replace_with);
	replace_with		=	'à';
	input 				= 	input.replace(/&agrave;/gi,	replace_with);
	replace_with		=	'\'';
	input 				= 	input.replace(/&#039;/gi,	replace_with);
	replace_with		=	'ä';
	input 				= 	input.replace(/&auml;/gi,	replace_with);
	replace_with		=	'ë';
	input 				= 	input.replace(/&euml;/gi,	replace_with);
	replace_with		=	'ï';
	input 				= 	input.replace(/&iuml;/gi,	replace_with);
	replace_with		=	'ü';
	input 				= 	input.replace(/&uuml;/gi,	replace_with);
	replace_with		=	'é';
	input 				= 	input.replace(/&eacute;/gi,	replace_with);
	replace_with		=	'è';
	input 				= 	input.replace(/&egrave;/gi,	replace_with);
	replace_with		=	'&';
	input 				= 	input.replace(/&amp;/g,	replace_with);
	replace_with		=	'<';
	input 				= 	input.replace(/&lt;/g,	replace_with);
	replace_with		=	'>';
	input 				= 	input.replace(/&gt;/g,	replace_with);
	replace_with		=	'\+';
	input 				= 	input.replace(/&#043;/g,	replace_with);
	replace_with		=	'§';
	input 				= 	input.replace(/&sect;/g,	replace_with);
	if (decodeQuotes)	{
	  replace_with		=	'"';
	  input 				= 	input.replace(/&quot;/gi,	replace_with);
	}
	//alert(input.toString());
	return input.toString();
	
}
function iterate(obj, func){
	for(var x in obj){
		switch (typeof(obj[x])) {
			case "object":
				obj[x] = iterate.apply(this,Array(obj[x],func));
				break;
			case "array":
				obj[x] = iterate.apply(this,Array(obj[x],func));
				break;
			case "string":
				obj[x] = func.call(this,obj[x]);
				break;
			default:
				void(0);
		}
	}
	return obj;
}

function is_numeric(val){
   return (input - 0) == input && input.length > 0;
}

function saveDeadlineDate(deadlineDate, invitationPhaseId){

  requestObject = {
      action: "saveDeadlineDate",
      data: {
        deadlineDate: deadlineDate,
        invitationPhaseId: invitationPhaseId
      }
    };
    sendAjax(requestObject,false,false);
}

function serialize(_obj){
   // Let Gecko browsers do this the easy way
   //if (typeof _obj.toSource !== 'undefined' && typeof _obj.callee === 'undefined')
   //{
   //   return _obj.toSource();
   //}
   // Other browsers must do it the hard way
   switch (typeof _obj)
   {
      // numbers, booleans, and functions are trivial:
      // just return the object itself since its default .toString()
      // gives us exactly what we want
      case 'number':
      case 'boolean':
      case 'function':
         return _obj;
         break;

      // for JSON format, strings need to be wrapped in quotes
      case 'string':
         return '"' + _obj + '"';
         break;

      case 'object':
         var str;
         if (_obj.constructor === Array || typeof _obj.callee !== 'undefined')
         {
            str = '[';
            var i, len = _obj.length;
            for (i = 0; i < len-1; i++) { str += serialize(_obj[i]) + ','; }
            str += serialize(_obj[i]) + ']';
         }
         else
         {
            str = '{';
            var key;
            for (key in _obj) { str += '"' + key + '":' + serialize(_obj[key]) + ','; }
            str = str.replace(/\,$/, '') + '}';
         }
         return str;
         break;

      default:
         return 'UNKNOWN';
         break;
   }
   
}
function addslashes(str) {
		return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
   }
   
   
   function showInfoblock(id){
		el = document.getElementById(id);
		if(el.style.display=='block'){
			el.style.display = 'none';	
		}else{
			el.style.display = 'block';	
		}
		
	}
	function sendSpecialistLoginData(companyId){
		requestObject = {
		action: "sendSpecialistLoginData",
		data: {
		  companyId: companyId
		}
	  };
	  sendAjax(requestObject,'message',false);
	}
	
	function stopBubbles(e) {
		var event = e || window.event;
		
		if (event.stopPropagation) {
			event.stopPropagation();
		} else {
			event.cancelBubble = true;
		} 
	}
