/* ---- Put your site specific functions in here --- */
function in_array(needle, haystack) {
	for(var i in haystack) {
		if(i == needle || haystack[i] == needle || in_array(needle, haystack[i])) return true;
	}
	return false;
}

function detailfoto (fotoFlip) {
	for (i = 1; i <= 10; i++) {
		if (document.getElementById('fotoklein_' + i)) {
			if (i == fotoFlip) {
				document.getElementById('fotoklein_' + i).className = 'select';
				document.getElementById('fotogroot_' + i).className = 'toon-img';
			}	else {
				document.getElementById('fotoklein_' + i).className = '';
				document.getElementById('fotogroot_' + i).className = 'verberg-img';
			}
		}
	}	
}

function getSubCategories(){
    Event.observe('category', 'change', function() {
        if ($F('category') != 0) {
	        var url = "ajax/getSubcategories.php";
	        var pars = "parent_id="+$F('category');
	        
	        new Ajax.Request(url, {method:'get', parameters:pars, onComplete:function(originalRequest){
	            var data = eval('('+originalRequest.responseText+')');
	            var error = data.error;
	            var subcategories = data.subcategories;
	            
	            if (error != ''){
	                //error
	                alert(error);
	            } else {
	                //clear the box wich cities can be chosen from
	                $('subcategory').options.length=0;
	                
	                //show the cities
	                for (i=0;i<subcategories.length;i++){
	                    subcategory = subcategories[i];
	                    var option = document.createElement('option');
	                    option.value = subcategory.id;
	                    option.innerHTML = subcategory.text;
	                    $('subcategory').appendChild(option);
	                }
	            }
	        }
	        });
	    } else {
	    	$('subcategory').options.length=0;
            var option = document.createElement('option');
            option.value = 0;
            option.innerHTML = '-- kies eerst een Rubriek --';
            $('subcategory').appendChild(option);
	    }
        return false;
    });
}

function m_option(value, text, select) {
    var option = document.createElement('option');
    option.value = value;
    option.innerHTML = text;
    if(select) option.setAttribute('selected','selected');
	return option;
}

function getInfo(parent_id, site_id) {
	var ajaxUrl = siteRoot+'includes/inc_ajax_getCities.php?parent_id='+parent_id+'&site_id='+site_id;
	new Ajax.Request(ajaxUrl, {
	  onSuccess: function(transport){
		var json = transport.responseText.evalJSON();
		var cities = json.cities;
		$A($('place').childNodes).each(function(element) { Element.remove( element ); });
		cities.each(function(city) { place.appendChild(m_option(city.value, city.text)); });
	  }
	});
}

function brochure_switch(clas){
    if (clas == 'type02'){
        //hide type01
        $$('div.type01').each(function(item){item.style.display = 'none'});
        
        //show type02
        $$('div.type02').each(function(item){item.style.display = 'block'});
        $$('fieldset.type02').each(function(item){item.style.display = 'block'});
        $$('h3.type02').each(function(item){item.style.display = 'block'});
    } else {
        //hide type02
        $$('div.type02').each(function(item){item.style.display = 'none'});
        $$('fieldset.type02').each(function(item){item.style.display = 'none'});
        $$('h3.type02').each(function(item){item.style.display = 'none'});
        
        //show type01
        $$('div.type01').each(function(item){item.style.display = 'block'});
    }
}

function calculateWeeks(firstdate,seconddate,inputfield) {
	var minutes = 1000*60;
	var hours = minutes*60;
	var days = hours*24;
	var weeks = days*7;
	
	var fd = new Date();
	fd.setFullYear(document.getElementById(firstdate).value, document.getElementById(firstdate+'-mm').value-1, document.getElementById(firstdate+'-dd').value);
	
	var sd = new Date();
	sd.setFullYear(document.getElementById(seconddate).value, document.getElementById(seconddate+'-mm').value-1, document.getElementById(seconddate+'-dd').value);
	
	var fd_stamp = fd.getTime();
	var sd_stamp = sd.getTime();
	if(fd_stamp > sd_stamp) {
		var numweeks = Math.ceil((fd_stamp - sd_stamp) / weeks);
	}
	else {
		var numweeks = Math.ceil((sd_stamp - fd_stamp) / weeks);
	}
	document.getElementById(inputfield).value = numweeks;
	document.getElementById(inputfield+'_').value = numweeks;
}

function calculateDays(firstdate,seconddate,inputfield) {
	var minutes = 1000*60;
	var hours = minutes*60;
	var days = hours*24;
	var fd = new Date(document.getElementById(firstdate+'-mm').value+' '+document.getElementById(firstdate+'-dd').value+' '+document.getElementById(firstdate).value);
	var sd = new Date(document.getElementById(seconddate+'-mm').value+' '+document.getElementById(seconddate+'-dd').value+' '+document.getElementById(seconddate).value);
	var fd_stamp = fd.getTime();
	var sd_stamp = sd.getTime();
	if(fd_stamp > sd_stamp) {
		var numdays = Math.ceil((fd_stamp - sd_stamp) / days)+1;
	}
	else {
		var numdays = Math.ceil((sd_stamp - fd_stamp) / days)+1;
	}
	document.getElementById(inputfield).value = numdays;
	document.getElementById(inputfield+'_').value = numdays;
}

function check_eerstgehoordIL(waarde) {
	var block = document.getElementById('gehoordil_andersnamelijk');
	if(waarde == "anders") block.style.display = "block";
	else block.style.display = "none";
}

function check_eerstgehoordAS(waarde) {
	var block = document.getElementById('gehoordas_andersnamelijk');
	if(waarde == "anders") block.style.display = "block";
	else block.style.display = "none";
}

function populateSelect(element_id, data, firstOption){
    //alert(element_id);
    var selected = $F(element_id);
    $(element_id).options.length=0;

    if (firstOption != ''){
        option = document.createElement('option');
        option.value = '';
        option.innerHTML = 'Kies een '+firstOption;
        
        $(element_id).appendChild(option);
        option = null;
    }

    for (var i=0;i<data.length;i++){
        var item = data[i];
        //alert(item['title']);
        var option = document.createElement('option');
        option.value = item['id'];
        option.innerHTML = item['title'];

        //if selected, select it again
        if (selected != null && selected != '' && selected == item['id']){
            option.selected = true;
        }
        
        $(element_id).appendChild(option);
        
        option = null;
        item = null;
        $(element_id).disabled = false;
    }
}

function getProgram1(changedElement) {
    if (changedElement == 'language_1'){
        //when a new language is selected clear all the other selects.
        $('courselevel_1').options.length = 0;
        $('country_1').options.length = 0;
        $('city_1').options.length = 0;
        //$('institute_1').options.length = 0;
        $('coursetype_1').options.length = 0;
        //$('accomodation_1').options.length = 0;
        $('accomodationtype_1').options.length = 0;
    }
    var url = 'ajax/quicksearch.php?type=program1&program_id='+$F('program_id');
    
    //'accomodation_1',
    var arrElements = new Array('language_1','courselevel_1', 'country_1', 'city_1', 'coursetype_1', 'accomodationtype_1'); // 'institute_1', 
    //get the already selected elements and add them to the url
    arrElements.each(function(item){
        //the result of the ajax call looks like this 290_chinees. So we have to extract the id
        if ($F(item) != null && $F(item) != ''){
	        arrItem = $F(item).split('_');
            url += '&'+item+'='+arrItem[0];
	    }
    });
        
    new Ajax.Request(url, {
            method:'get', 
            onSuccess: function(transport){
                var json = transport.responseText.evalJSON();
                //courselevels
                if (json.courselevel_1.length>0){
                    populateSelect('courselevel_1', json.courselevel_1, 'taalniveau')
                }
                
                //countries
                if (json.country_1.length>0){
                    populateSelect('country_1', json.country_1, 'land')
                }
                //cities
                if (json.city_1.length>0){
                    populateSelect('city_1', json.city_1, 'stad')
                }
                //institutes
                //if (json.institute_1.length>0){
                //    populateSelect('institute_1', json.institute_1, 'Instituut')
                //}
                //coursetypes
                if (json.coursetype_1.length>0){
                    populateSelect('coursetype_1', json.coursetype_1, 'cursustype')
                }
                //accomodations
                /*if (json.accomodation_1.length>0){
                    populateSelect('accomodation_1', json.accomodation_1, 'Accomodatie')
                }*/
                
                //accomodationtypes
                if (json.accomodationtype_1.length>0){
                    populateSelect('accomodationtype_1', json.accomodationtype_1, 'accommodatietype')
                }
            }
        });
        
        if ($F('language_1') != ''){
            $('buttons_program1').show();
        } else {
            $('buttons_program1').hide();
        }
}

function getProgram2() {
    var url = 'ajax/quicksearch.php?type=program2&program_id='+$F('program_id');

    arrElements = new Array('country_2', 'city_2', 'institute_2', 'accomodationtype_2'); // 'coursetype_2', 'accomodation_2' 
    //get the already selected elements and add them to the url
    arrElements.each(function(item){
        if ($F(item) != null && $F(item) != ''){
            arrItem = $F(item).split('_');
            url += '&'+item+'='+arrItem[0];
        }
    });

    new Ajax.Request(url, {
            method:'get', 
            onSuccess: function(transport){
                var json = transport.responseText.evalJSON();
                
                //countries
                if (json.country_2.length>0){
                    populateSelect('country_2', json.country_2, 'land')
                }
                //cities
                if (json.city_2.length>0){
                    populateSelect('city_2', json.city_2, 'plaats')
                }

                //institutes
                if (json.institute_2.length>0){
                    populateSelect('institute_2', json.institute_2, 'college')
                }
                //coursetypes
                //if (json.coursetype_2.length>0){
                //    populateSelect('coursetype_2', json.coursetype_2, 'Cursustype')
                //}
                
                //accomodations
                //if (json.accomodation_2.length>0){
                //    populateSelect('accomodation_2', json.accomodation_2, 'accommodatie')
                //}
                
                //accommodationtype
                if (json.accomodationtype_2.length>0){
                    populateSelect('accomodationtype_2', json.accomodationtype_2, 'accommodatietype')
                }
            }
        });
}

function switchQuickSearch(id){
    childs = $('form_quicksearch').childElements();
    childs.each(function(item){
        if (item.id != ''){
	        if (item.id == 'quicksearch_'+id){
	            item.show();
	        } else {
	            item.hide();
	        }
	    }
    });
    
    if (id==1){
        //fill language_1 with options
        element_id = 'language_1';        

        var url = 'ajax/quicksearch.php?type='+element_id+'&program_id='+$F('program_id');
        new Ajax.Request(url, {
            method:'get', 
            onSuccess: function(transport){
                var json = transport.responseText.evalJSON();
                populateSelect('language_1', json.data, 'taal')
            }
        });
    }
    
    if (id==2){
        getProgram2();
    }
    
    if (id==3){
        //fill language_1 with options
        element_id = 'country_3';        

        var url = 'ajax/quicksearch.php?type='+element_id+'&program_id='+$F('program_id');
        new Ajax.Request(url, {
            method:'get', 
            onSuccess: function(transport){
                var json = transport.responseText.evalJSON();
                populateSelect('country_3', json.data, 'land')
            }
        });
    }
}

function submitQuickSearchForm() {
    switch ($F('program_id')){
        case '1':
            var parameters = 'taalreizen';
            arrElements = new Array(new Array('language_1','taal'),
                                    new Array('country_1','land'), 
                                    new Array('city_1','plaats'),
                                    new Array('institute_1','instituut'), 
                                    new Array('coursetype_1','cursustype'),
                                    new Array('course_1','cursus'),
                                    new Array('accomodationtype_1','accommodatietype'),
                                    new Array('accomodation_1','accommodatie'), 
                                    new Array('courselevel_1','taalniveau')
                                    ); //'institute_1', 'accomodation_1',
            break;
        case '2':
            var parameters = 'college';
            arrElements = new Array( new Array('country_2','land'),
                                     new Array('city_2', 'plaats'), 
                                     new Array('institute_2', 'college'), 
                                     new Array('accomodationtype_2','accommodatietype'),
                                     new Array('accomodation_2','accommodatie'),
                                     new Array('study_2','studie')
                                     ); //'coursetype_2',
            break;
        case '3':
            var parameters = 'highschool';
            arrElements = new Array(new Array('country_3','land'));
            break;
        default :
            return false
            break;
    }

    
    arrElements.each(function(item){
        if ($(item[0]) && $F(item[0]) != null && $F(item[0]) != ''){
            arrItem = $F(item[0]).split('_');
            parameters += '/'+arrItem[1];
        } else {
            parameters += '/'+item[1];
        }
    });

    $('form_quicksearch').action += parameters + '/';
    return true;
}

function checkBirthDate(day_element, month_element, year_element){
	var birthYear = $F(year_element);
	var birthMonth = $F(month_element)-1;
	var birthDay = $F(day_element);
	
	//hides a fieldset if user is underaged
    var curDate = new Date();
	var d = curDate.getDate();
	var m = curDate.getMonth();
	var y = curDate.getFullYear();
	
	var age = y - birthYear;
	if (birthMonth > m) {
		// Nog niet jarig geweest
		age = age - 1;
	} else if (birthMonth == m) {
		if (birthDay > d) {
			// Nog niet jarig geweest
			age = age - 1;
		}
	}
	
    if (age >= 18){
        $('ouders_voogd_contact').descendants().each(function(s){
           if (s.tagName == 'H4'){
             s.innerHTML = 'Gegevens thuisblijvers:';
           }
           if (s.tagName == 'EM'){
             s.innerHTML = '';
           }
        });
    } else {
        //underaged
        $('ouders_voogd_contact').descendants().each(function(s){
           if (s.tagName == 'H4'){
             s.innerHTML = 'Gegevens ouder / voogd:';
           }
           if (s.tagName == 'LABEL' && s.innerHTML != 'Man' && s.innerHTML != 'Vrouw' && s.innerHTML != 'Telefoon \'s avonds:' && s.innerHTML != 'Telefoon mobiel:'){
             if (s.innerHTML.indexOf('<em>*</em>') == -1) {
				s.innerHTML = s.innerHTML+'<em>*</em>';
			 }
           }
        });
    }
}

function getAge(date, month, year) {

year = $F(year);
month = $F(month);
date = $F(date);

var birthday =new Date(year, month-1, date) //Month is 0-11 in JavaScript
today=new Date()

//Get 1 day in milliseconds
var one_day=1000*60*60*24

//Calculate difference btw the two dates, and convert to days
alert(Math.ceil(((today.getTime()-birthday.getTime())/(one_day)) /365) + " days!");

}


function showFlight(){
    if ($F('vlucht_heen') != null){
        $('heenvlucht').style.display = 'block';
        $('heenvlucht').descendants().each(function(s){
           if (s.tagName == 'SELECT'){
             s.disabled = '';
           }
        });
    } else {
        $('heenvlucht').style.display = 'none';
        $('heenvlucht').descendants().each(function(s){
           if (s.tagName == 'SELECT'){
             s.disabled = 'disabled';
           }
        });
    }
    
    if ($F('vlucht_terug') != null){
        $('terugvlucht').style.display = 'block';
        $('terugvlucht').descendants().each(function(s){
           if (s.tagName == 'SELECT'){
             s.disabled = '';
           }
        });
    } else {
        $('terugvlucht').style.display = 'none';
        $('terugvlucht').descendants().each(function(s){
           if (s.tagName == 'SELECT'){
             s.disabled = 'disabled';
           }
        });
    }
    
    
}

// Functionaliteit voor onder de button op het boekingsformulier om de gegevens van de contact gegevens over te nemen voor de thuisblijvers
function fillForm (form) {	
	document.getElementById('contact_voornaam').value = document.getElementById('voornamen').value;	
	document.getElementById('contact_achternaam').value = document.getElementById('achternaam').value;
	document.getElementById('contact_geslacht_man').checked = document.getElementById('geslacht_man').checked;
	document.getElementById('contact_geslacht_vrouw').checked = document.getElementById('geslacht_vrouw').checked;
	document.getElementById('contact_straatnaam').value = document.getElementById('straatnaam').value;
	document.getElementById('contact_huisnummer').value = document.getElementById('huisnummer').value;
	document.getElementById('contact_postcode').value = document.getElementById('postcode').value;
	document.getElementById('contact_woonplaats').value = document.getElementById('woonplaats').value;
	document.getElementById('contact_telefoon_overdag').value = document.getElementById('telefoon_overdag').value;
	document.getElementById('contact_telefoon_avond').value = document.getElementById('telefoon_avond').value;
	document.getElementById('contact_telefoon_mobiel').value = document.getElementById('telefoon_mobiel').value;
	document.getElementById('contact_email').value = document.getElementById('email').value;
}