/************************************
* open_scherm                       *
************************************/
var imageLoading = false;

function open_test_scherm(link) {
	var address = link
	var op_tool  = 0
	var op_loc_box  = 0
	var op_dir  = 0
	var op_stat  = 0
	var op_menu  = 0
	var op_scroll  =1
	var op_resize  = 1

	var op_wid  = 789
	var op_heigh = 700
 
	var option = "toolbar=" + op_tool + ",location=" + op_loc_box + ",directories=" 
			+ op_dir + ",status=" + op_stat + ",menubar=" + op_menu + ",scrollbars="  
			+ op_scroll + ",resizable="  + op_resize + ",width=" + op_wid + ",height=" 
			+ op_heigh;

	var new_win1 = window.open(address, "NewWindow1", option);
	var new_win2 = window.open(address, "NewWindow2", option);
	var new_win3 = window.open(address, "NewWindow3", option);
}

function open_scherm(link) {
	var address = link
	var op_tool  = 0
	var op_loc_box  = 0
	var op_dir  = 0
	var op_stat  = 0
	var op_menu  = 0
	var op_scroll  =1
	var op_resize  = 1

	var op_wid  = 400
	var op_heigh = 10
 
	var option = "toolbar=" + op_tool + ",location=" + op_loc_box + ",directories=" 
			+ op_dir + ",status=" + op_stat + ",menubar=" + op_menu + ",scrollbars="  
			+ op_scroll + ",resizable="  + op_resize + ",width=" + op_wid + ",height=" 
			+ op_heigh;
 
	var new_win = window.open(address, "NewWindow", option);
	if (window.focus && new_win != null)
		new_win.focus();
} 

/************************************
* NextPage                          *
************************************/

function Init() {
	// You can add/delete initial functions here

	for (var i = 0; i < document.survey.length; i++) {
		if (document.survey[i].name.indexOf('Q') == 0) {
			document.survey[i].onchange = "CheckNav();";
			alert(document.survey[i].onchange);
		}
	}
}

/************************************
* NextPage                          *
************************************/

function NextPage() {
	document.survey.doit.value='NextPage';
	document.survey.onsubmit = new Function("return true;");
	document.survey.submit();
}
function PCSearch(link){
	if ((document.survey.QSTQ5.value.length>0) && (document.survey.QSTQ4.value.length>0))
	{
		open_scherm(link+"?postcode="+document.survey.QSTQ5.value+"&hn="+document.survey.QSTQ4.value );
	}
	

}
/************************************
* PreviousPage                      *
************************************/

function PreviousPage() {
	document.survey.doit.value='PreviousPage';
	document.survey.onsubmit = new Function("return true;");
	document.survey.submit();
}
function GotoPage(page) {
	document.survey.doit.value='GotoPage';
	document.survey.gotopage.value=page;
	document.survey.onsubmit = new Function("return true;");
	document.survey.submit();
}

/************************************
* FirstEmpty                        *
************************************/

function FirstEmpty() {
	document.survey.doit.value='FirstEmpty';
	document.survey.onsubmit = new Function("return true;");
	document.survey.submit();
}

/************************************
* Ready                             *
************************************/

function Ready() {
	document.survey.doit.value='Ready';
	document.survey.onsubmit = new Function("return true;");
	document.survey.submit();
}


/************************************
* Login                             *
************************************/


function Login() {
	document.survey.doit.value='login';
	document.survey.action='login.asp';
	document.survey.submit();
}

/************************************
* CheckLength                       *
************************************/

function CheckLength(element, max) {
	val = element.value;
	if (val.length > max)
	{
		element.value = val.substring(0, max - 1);
		element.focus();
	}
}

/************************************
* CheckMinMax                       *
************************************/

function CheckMinMax(element, min, max, step) {
	val = element.value;
	if (val.length == 0 || step == 0 || min == max)
		return;
	
	val = stripChars(val);
	element.value = val;
	if (val > max) {
		alert('Waarde te hoog');
		element.focus();
		return false;
	}
	if (val < min) {
		alert('Waarde te laag');
		element.focus();
		return false;
	}
	if (val % step != 0)
	{
		alert('Ongeldige waarde, heeft geen stapgrootte van ' + step);
		element.focus();
		return false;
	}
	return true;
}

/************************************
* stripChars                        *
************************************/

function stripChars(s) {
	var i;
  var returnString = "";
	var integer = "0123456789";

	for (i = 0; i < s.length; i++) {   
		var c = s.charAt(i);
		if(integer.indexOf(c) != -1)
			returnString += c;
	}
	return returnString;
}
/**************************************************************************************************
* NAME: MarkTheBox
* Description:
* Als de gebruiker bij een OpenEnum of OpenMulti in het open antwoord veld klikt, wordt het 
* bijbehorende vakje aangekruist, mits er nog geen ander antwoord gegeven is.
*
* Subroutines Called:
*
* Returns:
*
* Globals:
* document.survey
*
* Tested by:					Date:
*
*	PARAMETERLIST
* Name:								Type:			Description:
*	element							string		Het radio of checkbox HTML element
* openanswer					string		De waarde van het openanswer radio of checkbox element
*
* Assumptions and Limitations:
*
* Exception Processing:
*
* Notes:
*
* REVISION HISTORY
* Version:			Date:					By:									Description:
*
**************************************************************************************************/
function MarkTheBox(element, openanswer)
{
	var question = eval("document.survey." + element);
	for (i = 0; i < question.length; i++) {
		if (question[i].checked && (question[i].type == 'radio' || question[i].type == 'select-single'))
			return;
	}

	for (i = 0; i < question.length; i++) {
		if (question[i].value == openanswer) {
			question[i].checked = true;
			return;
		}
	}
}
/************************************
* CheckTheBox                       *
************************************/

function CheckTheBox(questionnumber, openanswer)
{
	var question = eval("document.survey.Q" + questionnumber);
	for (i = 0; i < question.length; i++) {
		if (question[i].checked)
			return;
	}

	for (i = 0; i < question.length; i++) {
		if (question[i].value == openanswer) {
			question[i].checked = true;
			return;
		}
	}
}

/************************************
* SetAnswer                         *
************************************/

function SetAnswer(questionnumber, answer)
{
	var question = eval("document.survey." + questionnumber);
	if (questionnumber.indexOf("_open") != -1) {
		question.value = answer;
		return;
	}

	if (question.length > 0) {
		if (question.type == "select-one" || question.type == "select-multiple") {
			for (i = 0; i < question.length; i++) {
				if (inArray(answer, question[i].value)) {
					question.selectedIndex = i;
				}
			}
		} 
		else {
			for (i = 0; i < question.length; i++) {
				if (inArray(answer, question[i].value)) {
					question[i].checked = true;
				}
			}
		}
	}
	else if (question.type == "text" || question.type =="textarea"|| question.type == "hidden")
		question.value = answer;
	
		
}

/************************************
* CheckDisabler                     *
************************************/

function CheckDisabler(item, clicked_item, disabler) {
	var element = eval('document.survey.' + item);

	var disable_rest = false;

	if (inArray(clicked_item.value, disabler)) {
		for (i = 0; i < element.length; i++) {
			if (!inArray(disabler, element[i].value)) {
				element[i].checked = false;
			}
		}
	}
	else {
		for (i = 0; i < element.length; i++) {
			if (inArray(disabler, element[i].value)) {
				element[i].checked = false;
			}
		}
	}
}

/************************************
* CheckMax                          *
************************************/

function CheckMax(questionnumber, amount, element)
{
	var aantal = 0;
	var question = eval("document.survey.Q" + questionnumber);
	for (i = 0; i < question.length; i++) {
		if (question[i].checked)
			aantal++;
	}

	if ( aantal > amount )
	{
		element.checked = false;
		return false;
	}
	return true;
}

/************************************
* isArray                           *
************************************/

function isArray(obj) {
	if (obj.constructor.toString().indexOf("Array") == -1)
		return false;
	else
		return true;
}

/************************************
* inArray                           *
************************************/

function inArray(obj, item) {
	if (isArray(obj)) {
		for (i = 0; i < obj.length; i++) {
			if (obj[i] == item) {
				return true;
			}
		}
	} else if (obj == item) {
		return true;
	}
	return false;
}

/************************************
* SetCookie                         *
************************************/

function SetCookie(name, value) {
	document.cookie = name + "=" + escape( value );
}

/************************************
* CheckDate                         *
************************************/

var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function CheckDate(element)
{
	x = element.value;
	if (x == "")
		return false;
	tag = ( x.indexOf("/") != -1 ) ? "/" : "-";
	var pos = x.indexOf(tag);
	var day = parseInt(x.substr(0, pos));
	x = x.substr(pos + 1);
	pos = x.indexOf(tag);
	var month = parseInt(x.substr(0, pos));
	x = x.substr(pos + 1);
	var year = parseInt(x);

	if (!day || !month || !year) {
		alert("Deze datum is ongeldig");
		element.value = "";
		element.focus();
		return false;
	}

	if (year / 4 == parseInt(year / 4))
		monthLength[1] = 29;

	if (day > monthLength[month - 1]) {
		alert("Deze datum is ongeldig");
		element.value = "";
		element.focus();
		return false;
	}

	return true;
}

/************************************
* CheckEmail                        *
************************************/

function CheckEmail(element) {
	x = element.value;
	if ( x != "" )
	{
		var strinValid = / (&)|(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|[*']/; 
		var strVaild = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; 
		if (!(!strinValid.test(x) && strVaild.test(x)))
		{
			alert( x + ' is geen geldig email adres');
			element.value = "";
			element.clear();
			return false; 
		}
		return true;
	}
	return false;
}

/************************************
* Disable                           *
************************************/

function Disable(name) {
	var element = eval("document.survey." + name);
	element.disabled = true;
}

/************************************
* Enable                            *
************************************/

function Enable(name) {
	var element = eval("document.survey." + name);
	element.disabled = false;
}

/************************************
* CheckNav                          *
************************************/

function CheckNav() {
	alert("CheckNav");
	return false;
}

/************************************
* Checkvolgorde						*
************************************/

function CheckVolgorde(sourceSelect,i, aantal)
{
	for (t=33;t<=aantal+32; t++)
	{
		
		if (sourceSelect.value != "")
		{
			if (sourceSelect.value == eval("document.survey.Q"+t+".value"))
			{
				
				if (i != t )
				{
					sourceSelect.value= 0;
				}
			}
		}	
	}
}
/************************************
* Postcode controle (AN)			*
************************************/

function CheckNlPostal(postcode){
	//Postcode controle
	var postc=new String(postcode.value); //.substring(0,4)); 
	var reg = new RegExp("[0-9]{4} ?[a-zA-Z]{2}$");
	if (reg.test(postc)){
		return true; 
	}
	else{
		alert("Je postcode is onjuist ingevoerd");
		postcode.value="";
		return false;
	}
}

function flip(naam,source){
    plaatje = eval(naam); 
	plaatje.src= source;

//	document.naam

}

function ExchangeCredits(rewardnumber){
	//if	(confirm("Weet je zeker dat je punten wilt inwisselen?")){
		document.rewards.handle.value=rewardnumber;
		//alert(rewardnumber);
		document.rewards.submit();
	//}

}



function hover(id,bg){
	document.getElementById(id).style.background = bg; // = bg;
}

function showLargeImage(image, name){
	var POPUP_SIZE	  = 400;
	var POPUP_PADDING = 10;

	$("#closepopupbutton").bind("click", function() {
		$("#imagepopup").get(0).style.display = "none";
	});

	var newImage	= new Image();
	newImage.src	= image.src;
	newImage.onload = function() {
		// Reset the load function, for the stupid animated gifs
		newImage.onload = function() {};

		var body			= $("body");
		var windowSize		= getWindowSize();
		var windowScroll	= getScrollXY();
		var popupContainer	= $("#imagepopup").get(0);
		var rewardnamelabel = $("#rewardnamelabel").get(0);
		var imageContainer  = $("#imagecontainer").get(0);
		var popupImage		= $("#popupimage").get(0);
		var scaling			= 1.0;
		var newWidth		= newImage.width;
		var newHeight		= newImage.height;

		popupContainer.style.width	= POPUP_SIZE;
		popupContainer.style.height	= POPUP_SIZE;
		popupContainer.style.top	= windowScroll.y + ((windowSize.height / 2) - (POPUP_SIZE / 2));
		popupContainer.style.left	= windowScroll.x + ((windowSize.width / 2)  - (POPUP_SIZE / 2));
		
		// Scale on the x axis
		if(newWidth > POPUP_SIZE - (POPUP_PADDING*2)){
			scaling = (POPUP_SIZE - (POPUP_PADDING*2)) / newWidth;
			
			newWidth  = newWidth * scaling;
			newHeight = newHeight * scaling;
		}

		// Scale on the y axis
		if(newHeight > POPUP_SIZE - (POPUP_PADDING*2)){
			scaling = (POPUP_SIZE - (POPUP_PADDING*2)) / newHeight;
			
			newWidth  = newWidth  * scaling;
			newHeight = newHeight * scaling;
		}

		popupImage.src			= image.src;
		popupImage.style.width	= newWidth;
		popupImage.style.height = newHeight;

		imagecontainer.style.width		= newWidth;
		imagecontainer.style.height		= newHeight;
		imagecontainer.style.marginLeft	= -(newWidth / 2);
		imagecontainer.style.marginTop  = -(newHeight / 2);

		rewardnamelabel.innerHTML	 = name;
		popupContainer.style.display = "block";
		
	};
}

function getWindowSize(){
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  return {width: myWidth, height: myHeight};
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return {x: scrOfX, y: scrOfY};
}

function loadStatGraph(){
	if(imageLoading)
		return;

	imageLoading = true;
	var listElement  = $("#yearlist").get(0);
	var imageElement = $("#statsimage").get(0);
	var button		 = $("#generatestatsbutton").get(0);
	var loadLabel	 = $("#loadlabel");
	var imageUrl	 = "admin.asp?view=generalstats&action=graph&year=" + listElement.options[listElement.selectedIndex].value + "&r=" + Math.floor(Math.random() * 1000);

	button.disabled = true;
	loadLabel.show();

	var newImage = new Image();

	newImage.onload = function(){
		newImage.onload = function() {};
		imageElement.src = newImage.src;

		imageLoading = false;
		button.disabled = false;
		loadLabel.hide();
	}

	newImage.src = imageUrl;
}
function SubmitPassword(){
	document.survey.onsubmit = new Function("return true;");
	document.survey.submit();
}