 /*
	searchForm_div.js
	Javascript for subscriber form
	Alpha Release: $Id: searchForm_class.js 115026 2010-08-31 11:07:12Z fibbe03 $
	Copyright 2007 Realworks B.V., AMSTERDAM, NL
	All Rights Reserved
*/

// ------------------------------------------------------------------------------------------------------------------------------

// select the option with text <selectedOptionText> of a select input element
// selectInputElement: Id of select element
// selectedOptionText: text of option that has to be selected

function setMenuSelectedValue(selectInputelement,selectedOptionText){
	if(!selectInputelement || !selectedOptionText){return false;}
	var selEl=document.getElementById(selectInputelement);
	for(var i=0; i<selEl.options.length; i++){
		if(selEl.options[i].value == selectedOptionText){
			selEl.selectedIndex=i;
			break;
		}
	}
}

// ------------------------------------------------------------------------------------------------------------------------------

//let the value of form select element 'master' determine the value of text input element 'slave'
//INPUT
// master: reference to select element that determines slave 
// aSlaveValues: possible values for slave

function setInputElementValue(oMaster,sSlaveName,aSlaveValues){
	if(!oMaster){return false;}
	var slaveVal=aSlaveValues[oMaster.selectedIndex];
	document.getElementsByName(sSlaveName)[0].value = slaveVal;
}

// ------------------------------------------------------------------------------------------------------------------------------

function hiliteInputElementBorder(textInput){
	if(!textInput){return;}
	textInput.className="alert";
}

// ------------------------------------------------------------------------------------------------------------------------------

function unHiliteInputElementBorder(textInput){
	if(!textInput){return;}
	textInput.className="";
}

// ------------------------------------------------------------------------------------------------------------------------------

function showElement(elementId) {
	var el = document.getElementById(elementId);
	if(el){
		el.style.display = '';
	}
}

// ------------------------------------------------------------------------------------------------------------------------------

function hideElement(elementId) {
	var el = document.getElementById(elementId);
	if(el){
		el.style.display = 'none';
	}
}
// ------------------------------------------------------------------------------------------------------------------------------

	function SaleRentPrice(value){
		if (value == 1)
			{
			hideRentPrice();
			showSalePrice();
			}
		if (value == 2)
			{
			showRentPrice();
			hideSalePrice();
			}
		if (value== 0)
			{
			showRentPrice();
			showSalePrice();
			}

	}
// ------------------------------------------------------------------------------------------------------------------------------

	function hideRentPrice(){

		document.getElementById("inputrentPriceFrom").style.display="none";
		document.getElementById("inputrentPriceTo").style.display="none";

		
		var selRentFrom = document.getElementById('rentPriceFrom');
		var selRentTo = document.getElementById('rentPriceTo');
		selRentFrom.selectedIndex = 0;
		selRentTo.selectedIndex = 18;
		 
		
	}

// ------------------------------------------------------------------------------------------------------------------------------

	function showRentPrice(){
		document.getElementById("inputrentPriceFrom").style.display="block";
		document.getElementById("inputrentPriceTo").style.display="block";
		

	}

// ------------------------------------------------------------------------------------------------------------------------------

	function hideSalePrice(){
 
		document.getElementById('inputsalePriceFrom').style.display='none';
		document.getElementById('inputsalePriceTo').style.display='none';
	 
		var selSaleFrom = document.getElementById('salePriceFrom');
		var selSaleTo = document.getElementById('salePriceTo');
		selSaleFrom.selectedIndex = 0;
		selSaleTo.selectedIndex = 28;

	}

// ------------------------------------------------------------------------------------------------------------------------------

	function showSalePrice(){
		document.getElementById('inputsalePriceFrom').style.display='block';
		document.getElementById('inputsalePriceTo').style.display='block';
		
	
		
	}

// ------------------------------------------------------------------------------------------------------------------------------

// reset the value of an inputelement

function resetInputelementsValue(aDivIds){
	//loop over array with divs
	for(i=0;i<aDivIds.length;i++){
		try{
			//create reference to table row
			var oDiv = document.getElementById(aDivIds[i])
	
			//create array with references to selects in row 
			aSelects = oDiv.getElementsByTagName('select');
	
			//create array with references to other inputelements in row 
			aInputs= oDiv.getElementsByTagName('input');
	
			//reset selects
			for(j=0;j<aSelects.length;j++){
				aSelects[j].selectedIndex=0;
			}
			//reset other inputelements
			for(j=0;j<aInputs.length;j++){
				if(aInputs[j].type.indexOf('text')!=-1){
					aInputs[j].value="";
				}
				else{
					if(aInputs[j].type.indexOf('checkbox')!=-1){
						aInputs[j].checked=false;
					}
				}
			}
		}
		catch(e){}
	}
}

// ------------------------------------------------------------------------------------------------------------------------------

// toon een table row afhankelijk van selectedOption en
// deselecteer de checkboxes die verborgen worden

function hideElements(selectedValue){ 
	 if(!window.currentSelected){
	 	currentSelected = 5;
	 }

	// show all table rows
	for(var i=0; i<aDivIds.length;i++){
		showElement(aDivIds[i]);
	}

	// hide selected table rows
	for(var i=0; i<aDivIdsToHide[selectedValue].length;i++){
		hideElement(aDivIdsToHide[selectedValue][i]);
	}
	resetInputelementsValue(aDivIdsToHide[selectedValue]);
	checkAllBySoortOg(selectedValue);
}

// Alle checkbox aanvinken van Soort woning, Type woning Kenmerken en Appartement
// afhankelijk van wat er gekozen is.
function checkAllBySoortOg(selectedValue) {
	var WOONHUIS_APPARTEMENT = 0;
	var WOONHUIS = 1;
	var APPARTEMENT = 2;
	var BOUWGROND = 3;
	var GARAGEBOX = 4;
	var NONE = 5;
	
	var divsToCheck = new Array();
	
	divsToCheck[WOONHUIS_APPARTEMENT]	= ['typeOfProperty', 'typeOfResidence', 'kenmerkenOfResidence', 'typeOfApartment'];
	divsToCheck[WOONHUIS]				= ['typeOfProperty', 'typeOfResidence', 'kenmerkenOfResidence'];
	divsToCheck[APPARTEMENT]			= ['typeOfApartment'];
	divsToCheck[BOUWGROND]				= ['kenmerkenOfResidence'];
	divsToCheck[GARAGEBOX]				= ['kenmerkenOfResidence'];
	divsToCheck[NONE]					= [];
	
	var checkIds = divsToCheck[selectedValue];
	
	for(var i=0; i < checkIds.length; i++ ) {
		checkAllInDiv(checkIds[i])
	}
}

// Als het vinkboxje "Allen" aangeinkt wordt
function checkAllInDiv(divId) {
	var oDiv = document.getElementById(divId);
	
	if(oDiv == null) {
		return; // div bestaat niet, door een instelling van de makelaar
	}
	aInputs= oDiv.getElementsByTagName('input');
	
	//Check alle divs
	for(j=0;j<aInputs.length;j++){
		// Triviant, maar je weet maar nooit
		if(aInputs[j].type.indexOf('checkbox')!=-1){
			aInputs[j].checked=true;
		}
	}
}

// Als het vinkboxje "Allen" uitgevinkt wordt
function uncheckAllCheckbox(divId) {
	var oDiv = document.getElementById(divId);
	aInputs= oDiv.getElementsByTagName('input');
		
	//Check alle divs
	for(j=0;j<aInputs.length;j++){
		// Triviant, maar je weet maar nooit
		if(aInputs[j].type.indexOf('checkbox')!=-1){
			aInputs[j].checked=false;
		}
	}
}

// Voor checkboxes voor Soort Woning, Soort Appartement, etc.
function checkboxClicked(checkbox, divId) {
	
	// De bijbehorende 'Allen' checkbox
	var allenId = divId + "_All";
	var oAllen = document.getElementById(allenId);
	
	var endsWith = checkbox.name.substring(checkbox.name.length - 3);
	
	if (endsWith == 'All') {
		if (checkbox.checked) {
			checkAllInDiv(divId);
			return;
		} else {
			uncheckAllCheckbox(divId);
			return;
		}
	}
	
	if (!checkbox.checked) {
		oAllen.checked = false; 
	}
	
	// Als alle checkboxes in de div zijn aangelikt, dan ook de 'Allen' checkbox 
	// aanvinken
	var oDiv = document.getElementById(divId);
	var aInputs= oDiv.getElementsByTagName('input');
	var checkAll = true;
	for(j=0;j<aInputs.length;j++){
		var endsWithTemp = aInputs[j].name.substring(aInputs[j].name.length - 3);
		if (endsWithTemp == 'All') {
			continue;
		}
		if (!aInputs[j].checked) {
			checkAll = false;
		}
	}
	if (checkAll) {
		oAllen.checked = true;		
	}
}

// ------------------------------------------------------------------------------------------------------------------------------

function processForm(oThisForm){
	result = false;
	formValid = checkForm(oThisForm);

	if(formValid){
		if(mySelectAreas.selectAll){
			mySelectAreas.selectAll('selected');
		}
		if(mySelectCities.selectAll){
			mySelectCities.selectAll('selected');
		}
		result = true;
	}

	return result;
}

// ------------------------------------------------------------------------------------------------------------------------------

//show all cities belonging to a province in the selectElementID multi select box

function showCities(selectedProvince,selectElementID){
	if(!selectedProvince)return;
	var sel=document.getElementById(selectElementID);
	for(i=0;i<citiesAndProvincesJS.length;i++){
		if(citiesAndProvincesJS[i][0]==selectedProvince){
			var cityCount=citiesAndProvincesJS[i].length-1;
			for(i=0;i<cityCount;i++){
			}
		}
	}
}

// ------------------------------------------------------------------------------------------------------------------------------

//initialiseer pagina...

window.onload = function(){
	var f = document.getElementById('inschrijfformulier');
	if (!f) {
		return;
	}
	if(typeof CitySelection == "function"){
		mySelectCities = CitySelection('inschrijfformulier','cities','selectedCities','province');	
	}
	 else{
		mySelectCities = new Object();
		mySelectCities.exists = function(){return false;}
	 }

	if(typeof AreaSelection == "function"){
		// don't remove the new keyword - it will overwrite 'mySelectCities'
		mySelectAreas = new AreaSelection('inschrijfformulier','areas','selectedAreas','#owner#','#department#');
	}
	 else{
		mySelectAreas = new Object();
		mySelectAreas.exists = function(){return false;}
	 }

	// ids of all rows
	aDivIds = new Array();
	aDivIds.push('inputnewConstruction','inputpropertySurfaceFrom','inputlivingspaceFrom','inputnumberOfRooms','inputconstructionYearFrom','inputconstructionYearTo');
	aDivIds.push('inputtypeOfProperty','inputtypeOfResidence','inputresidenceFeature','typeOfProperty','typeOfApartment','typeOfResidence', 'kenmerkenOfResidence');

	// ids of rows to hide depending on value of select 'Soort OG' by value of the select
	aDivIdsToHide = new Array();
	// checkboxes in deze rows verbergen bij keuze voor woonhuis/appartement
	aDivIdsToHide[0] = [];
	// Maak keuze
	
	// checkboxes in deze rows verbergen bij keuze voor woonhuis
	aDivIdsToHide[1] = new Array();
	aDivIdsToHide[1] = ['typeOfApartment'];

	// checkboxes in deze rows verbergen bij keuze voor appartement
	aDivIdsToHide[2] = new Array();
	aDivIdsToHide[2].push('typeOfResidence','typeOfProperty', 'kenmerkenOfResidence', 'inputresidenceFeature','inputpropertySurfaceFrom');

	// checkboxes in deze rows verbergen bij keuze voor bouwgrond
	aDivIdsToHide[3] = new Array();
	aDivIdsToHide[3].push('typeOfProperty','typeOfApartment','typeOfResidence','inputtypeOfApartment', 'kenmerkenOfResidence');
	aDivIdsToHide[3].push('inputnewConstruction','inputlivingspaceFrom','inputnumberOfRooms','inputconstructionYearFrom','inputconstructionYearTo');

	// checkboxes in deze rows verbergen bij keuze voor garage
	aDivIdsToHide[4] = new Array();
	aDivIdsToHide[4].push('typeOfProperty','typeOfApartment','typeOfResidence','inputtypeOfApartment');
	aDivIdsToHide[4].push('inputnewConstruction','inputpropertySurfaceFrom','inputlivingspaceFrom','inputnumberOfRooms');
	aDivIdsToHide[4].push('inputconstructionYearFrom','inputconstructionYearTo', 'kenmerkenOfResidence');
	
	// checkboxes in deze rows verbergen bij keuze voor Maak keuze
	aDivIdsToHide[5] = new Array();
	var soort = soortOG;

	var SOORT_OG_APPARTEMENT = 0;
	var SOORT_OG_BOUWGROUND = 1;
	var SOORT_OG_GARAGEBOX = 2;
	var SOORT_OG_WOONHUIS = 3;
	var SOORT_OG_WOONHUIS_APPARTEMENT = 4;

	if(soort.charAt(SOORT_OG_APPARTEMENT) == "0" && soort.charAt(SOORT_OG_WOONHUIS) == "0" && soort.charAt(SOORT_OG_WOONHUIS_APPARTEMENT) == "0") {
		aDivIdsToHide[5].push('inputnewConstruction','inputlivingspaceFrom','inputnumberOfRooms');
		if (soort.charAt(SOORT_OG_WOONHUIS) == "0" && soort.charAt(SOORT_OG_WOONHUIS_APPARTEMENT) == "0") {
			aDivIdsToHide[5].push('inputpropertySurfaceFrom');
		}
	}

	aDivIdsToHide[5].push('inputconstructionYearFrom', 'inputconstructionYearTo');	
	aDivIdsToHide[5].push('typeOfProperty','typeOfApartment','typeOfResidence', 'kenmerkenOfResidence', 'inputtypeOfApartment');

	setInputElementValue(document.getElementsByName('subscriberGender')[0],'subscriberTitle',['De Heer','Mevr','']);
	hideElements(5);
	
	// Wanneer slechts een enkele province in de selectbox, deze automatisch selecteren
	if (keys(citiesAndProvincesJS).length == 1) {
		var oSel = document.getElementById("province");
		oSel.selectedIndex = 1;
		mySelectCities.searchCities();
		$("#inputprovince").hide();				// Selectbox hiden
		$("#inputuitleg ol>li:first").hide();	// De "Kies een province" label hiden
	}
}


/**
 * De indexes op een "associative" array zijn stiekem attributen op het array 
 * object, waardoor assocArray.length altijd 0 terug geeft. Daarom deze methode.
 */
function keys(obj) {
	var ret = [];
	for (key in obj) {
		ret.push(key);
	} 
	return ret;
}


