function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
    //suppress error
   }
  }
 }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
 else
  return false
}


function createShim() {
	if (window.createPopup && !window.XmlHttpRequest){ //if IE5.5 to IE6, create iframe for iframe shim technique
		document.write('<IFRAME id="iframePopup"  src="" style="display: none; left: 0; top: 0; z-index: 49; position: absolute; filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)" frameBorder="0" scrolling="no"></IFRAME>')
	}
}

function setShim(objName,frameName){ //display iframe shim function
if (typeof document.getElementById(frameName)!="undefined")
	{
	if (document.getElementById(objName).style.visibility=="visible")
		{
		document.getElementById(frameName).style.width=document.getElementById(objName).offsetWidth+"px";
		document.getElementById(frameName).style.height=document.getElementById(objName).offsetHeight+"px";
		document.getElementById(frameName).style.left=document.getElementById(objName).style.left;
		document.getElementById(frameName).style.top=document.getElementById(objName).style.top;
		}
	document.getElementById(frameName).style.display=(document.getElementById(objName).style.visibility=="visible")? "block" : "none";
	}
}

function unsetShim(objName,frameName){
if (typeof document.getElementById(frameName)!="undefined")
	{
	document.getElementById(frameName).style.display='none';
	}
}

createShim();

function fadeBody(state) 
	{
	if(state=='fadein') 
		{
		document.getElementById("bodyTag").className = "fade";
		} 
	else if(state=='fadeout')
		{
		document.getElementById("bodyTag").className = "none";
		}	
	}
	


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function buildPopup(popupDivName,params)
	{
	scroll(0,0);
	var mypostrequest=new ajaxRequest();
	mypostrequest.onreadystatechange=function()
		{
		if (mypostrequest.readyState==4)
			{
	  		if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1)
				{
				document.getElementById("popupContainer").style.display = "block";
				var popupName = document.getElementById("popupContainer");
				var arrayPageSize = getPageSize();
				popupName.style.top = "50px";
				popupName.style.left = ((arrayPageSize[0] / 2) - (popupName.offsetWidth / 2)) + "px";
				fadeBody('fadein');
				if (window.createPopup && !window.XmlHttpRequest)
					{ //if IE5.5 to IE6, create iframe for iframe shim technique
					//setShim(popupDivName,"iframePopup");
					}
				params = '?'+params;
				document.getElementById("popupFrame").src = "popups/"+popupDivName+".php"+params;//mypostrequest.responseText;

				//setTextArea(popupName)
	  			}
	  		else
				{
	   			alert("Fout bij afmelden")
				}
	 		}
		}
	
	mypostrequest.open("POST", "popups/"+popupDivName+".php", true)
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	if(params!=null) { mypostrequest.send(params); }
	else { mypostrequest.send(); }
	
	
	}
	
function includePage(source,target,params)
	{
	var mypostrequest=new ajaxRequest();
	mypostrequest.onreadystatechange=function()
		{
		if (mypostrequest.readyState==4)
			{
	  		if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1)
				{
				params = '?'+params;
				document.getElementById(target).innerHTML = mypostrequest.responseText;
				parent.mainFunc(document.body.scrollHeight);
				

	  			}
	  		else
				{
	   			alert("Fout bij afmelden")
				}
	 		}
		}
	
	mypostrequest.open("POST", "../includes/"+source+".php", true)
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	if(params!=null) { mypostrequest.send(params); }
	else { mypostrequest.send(); }
	
	
	}

	
function closePopup(reloadNeeded) {
	fadeBody('fadeout');
	document.getElementById("popupContainer").style.display = "none";
	document.getElementById("popupFrame").src = '';
	if (window.createPopup && !window.XmlHttpRequest){ //if IE5.5 to IE6, create iframe for iframe shim technique
		unsetShim("popupContainer","iframePopup");
	}
	if(reloadNeeded)
		{
		location.reload(true);
		}
	else
		{
		rebuildView();
		}
}

function setActive(tN,id)
	{
	var mypostrequest=new ajaxRequest();
	mypostrequest.onreadystatechange=function()
		{
		if (mypostrequest.readyState==4)
			{
	  		if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1)
				{
				rebuildView();
				
				//setTextArea(popupName)
	  			}
	  		else
				{
	   			alert("Fout bij afmelden")
				}
	 		}
		}
	params='tN='+tN+'&id='+id;
	mypostrequest.open("POST", "includes/processSetActive.php", true)
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	mypostrequest.send(params);
	
	
	}


function validateEmail(required,objectName) 
	{
	var str = document.getElementById(objectName).value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		{ 
		changeState("valid",objectName);
		return true;
		}
	else if(required==true || str!='')
		{
		changeState("invalid",objectName);
		return false;
		}
	else
		{
		changeState("invalid_noreq",objectName);
		return true;
		}
	}
	
function validateEmailDouble(required,objectName,objectNameCheck)
	{
	if(document.getElementById(objectName).value==document.getElementById(objectNameCheck).value && document.getElementById(objectName).value!='' && validateEmail(required,objectName))
		{
		changeState("valid",objectName);
		return true;	
		}
	else
		{
		changeState("invalid",objectName);
		return false;	
		}
	}

function validatePostcode(required, postcodeveld1, postcodeveld2) 
	{
	var str1 = document.getElementById(postcodeveld1).value;
	var str2 = document.getElementById(postcodeveld2).value;
	var filter1=/\d{4}$/i
	var filter2=/\D{2}$/i
	if (filter1.test(str1) && filter2.test(str2))
		{ 
		changeState("valid", postcodeveld1);
		return true;
		}
	else if(required==true || str1!='' || str2!='')
		{
		changeState("invalid", postcodeveld1);
		return false;
		}
	else
		{
		changeState("invalid_noreq", postcodeveld1);
		return true;
		}
	}

function validateBasic(required,objectName) 
	{
	var str = document.getElementById(objectName).value;
	if (str!='')
		{ 
		changeState("valid",objectName);
		return true;
		}
	else if(required==true)
		{
		changeState("invalid",objectName);
		return false;
		}
	else
		{
		changeState("invalid_noreq",objectName);
		return true;
		}
	}
	
function validateBasicCount(required, imgID, inputID, count) 
	{
	var str = document.getElementById(inputID).value;
	if (str!='' && str.length>=13)
		{ 
		changeState("valid", imgID);
		return true;
		}
	else if(required==true)
		{
		changeState("invalid", imgID);
		return false;
		}
	else
		{
		changeState("invalid_noreq", imgID);
		return true;
		}
	}
	
function validateBasicCountDate(required, imgID, inputID, count) 
	{
	var str = document.getElementById(inputID).value;
	if (str!='' && str.length==10 && (/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/.test(str)))
		{
		changeState("valid", imgID);
		return true;
		}
	else if(required==true)
		{
		changeState("invalid", imgID);
		return false;
		}
	else
		{
		changeState("invalid_noreq", imgID);
		return true;
		}
	}
	
function validateDual(required, imgID, inputID1, inputID2) 
	{
	var str1 = document.getElementById(inputID1).value;
	var str2 = document.getElementById(inputID2).value;
	if (str1!='' && str2!='')
		{ 
		changeState("valid", imgID);
		return true;
		}
	else if(required==true || str1!='' || str2!='')
		{
		changeState("invalid", imgID);
		return false;
		}
	else
		{
		changeState("invalid_noreq", imgID);
		return true;
		}
	}
	
function validateSelect(required, imgID, selectID) 
	{
	var str = document.getElementById(selectID).value;
	if (str!='' && str!='none')
		{ 
		changeState("valid", imgID);
		return true;
		}
	else if(required==true)
		{
		changeState("invalid", imgID);
		return false;
		}
	else
		{
		changeState("invalid_noreq", imgID);
		return true;
		}
	}
	
function validateRadioDep(required, formname, fieldname, dependance) 
	{
	var str = eval("document." + getElementById(formname) + "." + fieldname + ".value");
	if (getCheckedValue(eval("document." + getElementById(formname) + "." + fieldname))=="nee")
		{
		showHide(dependance,"hidden");
		changeState("valid", fieldname);
		}
	if (getCheckedValue(eval("document." + getElementById(formname) + "." + fieldname))=="ja")
		{
		showHide(dependance,"visible");
		changeState("valid", fieldname);
		}
	}
	
function validateRadio(required, formname, fieldname) 
	{
	var str = eval("document." + getElementById(formname) + "." + fieldname + ".value");
	if (getCheckedValue(eval("document." + getElementById(formname) + "." + fieldname))!="")
		{
		changeState("valid", fieldname);
		return true;
		}
	else if(required==true)
		{
		changeState("invalid", imgID);
		return false;
		}
	else
		{
		changeState("invalid_noreq", imgID);
		return true;
		}
	}

function getCheckedValue(radioObj) 
	{
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function changeState(state, objectName) 
	{
	imgObject = document.getElementById(objectName+'Img');
	if (state=="valid")
		{
		imgObject.src = 'http://www.ox2design.nl/pigeonacademy/assets/validation_valid.png';
		}
	else if (state=="invalid_noreq")
		{
		imgObject.src = 'http://www.ox2design.nl/pigeonacademy/assets/validation_invalidOptional.png';
		}
	else if (state=="invalid")
		{
		imgObject.src = 'http://www.ox2design.nl/pigeonacademy/assets/validation_invalidRequired.png';
		}
	}
	
function numOnly(o){
  if(/[^0-9-]/.test(o.value)){
    o.value=o.value.toUpperCase().replace(/([^0-9-])/g,"");
  }
}