/* IE mist de indexOf methode voor een array */
if(!Array.indexOf)
{
    Array.prototype.indexOf = function(obj)
    {
        for(var i=0; i < this.length; i++)
        {
            if(this[i]===obj)
            {
                return i;
            }
      }
      return -1;
   }
}

var browserName = navigator.appName; 
var browserVer = parseInt(navigator.appVersion); 
var showBlock = '';

if (browserName == "Microsoft Internet Explorer") 
{
    showBlock = 'block';
}
else
{ 
    showBlock = 'table-row';
}


document.write("<style type='text/css'>\n");

//Search im-/export
document.write("#cms_cm80__form__country_row {display:none;}\n");

//Search kennispartner
document.write("#cms_cm129__form__country_row {display:none;}\n"); 

//Search ledenoverzicht
document.write("#cms_cm244__form__country_row {display:none;}\n"); 

//bewerken im/export
document.write("#cms_cm79__form__country-import_row {display:none;}\n");
document.write("#cms_cm79__form__country-export_row {display:none;}\n");

//aanmelden im/export 
document.write("#cms_cm64__form__country-import_row {display:none;}\n"); 
document.write("#cms_cm64__form__country-export_row {display:none;}\n"); 

//aanmelden partner
document.write("#cms_cm121__form__country_row {display:none;}\n"); 

//bewerken partner
document.write("#cms_cm156__form__country_row {display:none;}\n"); 

document.write("</style>\n");

//AUTO Complete (search)
function bindAutoComplete(countries) 
{
    function log(message) 
    {            
        var link = '[<a href="javascript:" onClick="deleteSearchTerm(\'' + message.split(' ').join('') + '\')">X</a>]';
        var hiddenText = $('input[name$="[form][country][]"]').attr('value');
        
        if (hiddenText == '' || hiddenText == null)
        {
            $('input[name$="[form][country][]"]').attr('value', message);
        }
        else
        {
            $('input[name$="[form][country][]"]').attr('value', hiddenText + ',' + message);
        }
        
        $('#results').append('<div id="' + message.split(' ').join('') + '">' + message + '&nbsp;' + link + '</div>');

        if ($('#searchResults').is(':hidden'))
        {
            $('#searchResults').attr('style', 'display:block');
        }
    }
    
    var cache = {};
    
    if (countries.length > 0)
    {
        for (var iCountry = 0; iCountry < countries.length; iCountry++)
        {
            log(countries[iCountry]);
        }
    }
    
    $("#searchInput").autocomplete(
    {
        source: function(request, response) 
        {            
            request.select = $('input[name$="[form][country][]"]').attr('value');
            
            if (cache.term == request.term && cache.content) 
            {
                response(cache.content);
            }
            if (new RegExp(cache.term).test(request.term) && cache.content && cache.content.length < 13) 
            {
                var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                
                response($.grep(cache.content, function(value) 
                {
                    return matcher.test(value.value)
                }));
            }
            $.ajax(
            {
                url: "/module/countries/search.php",
                dataType: "json",
                data: request,
                success: function(data) 
                {                        
                    cache.term = request.term;
                    cache.content = data;
                    response(data);
                }
            });
        },
        select: function(event, ui) 
        {
            if (ui.item)
            {                    
                log(ui.item.value, cache.count);
                ui.item.value = '';
            }
        }
    });
}

function deleteSearchTerm(searchTerm)
{
    if (searchTerm)
    {
        $('#' + searchTerm).remove();
        
        var hiddenText = $('input[name$="[form][country][]"]').attr('value');

        if (hiddenText != null && hiddenText != '')
        {
            var hiddenArray = hiddenText.split(',');            
            var hiddenKey = hiddenArray.indexOf(searchTerm);          

            hiddenArray.splice(hiddenKey, 1);
            
            $('input[name$="[form][country][]"]').attr('value', hiddenArray.join());
        }
    }
}
//Einde AUTO Complete

Countries =
{		
	selectAllCountries : function()
	{
		$.ajax({ type: 'POST',
					data: "action=get&type=allCountries",
					dataType: "json",
					url: '/module/countries/countries.php',
					success: function(data)
					{
						Countries.callCountry(data, true);
					}
					});
	},	
	
	deselectAllCountries : function()
	{
		$.ajax({ type: 'POST',
					data: "action=get&type=allCountries",
					dataType: "json",
					url: '/module/countries/countries.php',
					success: function(data)
					{
						Countries.callCountry(data, false);
					}
					});
	},	
	selectCountries : function(region)
	{
		$.ajax({ type: 'POST',
					data: "action=get&type=countriesByRegion&data=" + region,
					dataType: "json",
					url: '/module/countries/countries.php',
					success: function(data)
					{
						Countries.callCountry(data, true);
					}
					});
	},	
	deselectCountries : function(region)
	{
		$.ajax({ type: 'POST',
					data: "action=get&type=countriesByRegion&data=" + region,
					dataType: "json",
					url: '/module/countries/countries.php',
					success: function(data)
					{
						Countries.callCountry(data, false);
					}
					});
	},
	callCountry : function(countries, select)
	{		
		if (select === true)
		{			
			for (var iCountry = 0; iCountry < countries.length; iCountry++)
			{
				$('#form_country_' + countries[iCountry].ID).attr('checked', true);
			}
		}
		else
		{
			for (var iCountry = 0; iCountry < countries.length; iCountry++)
			{
				$('#form_country_' + countries[iCountry].ID).attr('checked', false);
			}
		}
	}
}

function generateCountryDom(id, cms, type, countries)
{		
	$.post('/module/countries/countries.php', {action:'html',type:'country',cms: cms,cttype: type, countries: countries}, function(txt)
	{
		$(id).html(txt);
		$(id).attr("style","display:block");
        
		$(".countrieslist").hide();
	});
}

function generateCountrySearchDom(id, cms, type, countries)
{		
	$.post('/module/countries/countries.php', {action:'html',type:'search',cms: cms,type: type}, function(txt, cms)
	{
		$(id).html(txt);
		$(id).attr("style","display:" + showBlock); //even checken met IE
        
        bindAutoComplete(countries);
	});
}

function toggleRegion(region)
{	
	$("#regioncountries" + region).slideToggle("slow");
    
    if ($("#region-toggle" + region).html() == "Regio Uitklappen")
    {
        $("#region-toggle" + region).html("Regio Inklappen");
    }
    else
    {
        $("#region-toggle" + region).html("Regio Uitklappen");
    }
}

function pageSection(cms)
{
	var total = $('input[name$="cms[cm64][total]"]').val();
	$('#cms_cm64__form__pagina-separator_title > span').append(' van ' + total);
	$('#cms_cm64__form__pagina1-separator_title > span').append(' van ' + total);
	$('#cms_cm64__form__pagina2-separator_title > span').append(' van ' + total);
	$('#cms_cm64__form__pagina3-separator_title > span').append(' van ' + total);
	
	total = $('input[name$="cms[cm79][total]"]').val();
	$('#cms_cm79__form__pagina-separator_title > span').append(' van ' + total);
	$('#cms_cm79__form__pagina1-separator_title > span').append(' van ' + total);
	$('#cms_cm79__form__pagina2-separator_title > span').append(' van ' + total);
	$('#cms_cm79__form__pagina3-separator_title > span').append(' van ' + total);
	
	total = $('input[name$="cms[cm121][total]"]').val();
	$('#cms_cm121__form__pagina-separator_title > span').append(' van ' + total);
	$('#cms_cm121__form__pagina1-separator_title > span').append(' van ' + total);
	$('#cms_cm121__form__pagina2-separator_title > span').append(' van ' + total);
	$('#cms_cm121__form__pagina3-separator_title > span').append(' van ' + total);
	
	total = $('input[name$="cms[cm156][total]"]').val();
	$('#cms_cm156__form__pagina-separator_title > span').append(' van ' + total);
	$('#cms_cm156__form__pagina1-separator_title > span').append(' van ' + total);
	$('#cms_cm156__form__pagina2-separator_title > span').append(' van ' + total);
	$('#cms_cm156__form__pagina3-separator_title > span').append(' van ' + total);
}

function changeProfile()
{
    var vragenProfile = $('#vragenprofiel').html();
    var editProfileButtons = $('#editProfileLinkRow').html();
    
    if (vragenProfile != null && editProfileButtons != null)
    {
        $('#vragenprofiel').html('<tr>' + editProfileButtons + '</tr>');
        $('#editProfileLinkRow').html(vragenProfile);
    }
}

$(document).ready(function()
{	
	pageSection();
    changeProfile();
	
    /* ===== BIND CHANGE FUNCTION ===== */
	$("#cms_cm64__form__position").change(function() 
    { 
		if ($("#cms_cm64__form__position").val() == 4)
		{
			$('#cms_cm64__form__otherposition').removeAttr("disabled");
			$('#cms_cm64__form__otherposition').removeClass("disabled");
		}
		else
		{
			$('#cms_cm64__form__otherposition').attr("disabled", true);
			$('#cms_cm64__form__otherposition').addClass("disabled");	
		}
    });
    $("#cms_cm79__form__position").change(function() 
    { 
		if ($("#cms_cm79__form__position").val() == 4)
		{
			$('#cms_cm79__form__otherposition').removeAttr("disabled");
			$('#cms_cm79__form__otherposition').removeClass("disabled");
		}
		else
		{
			$('#cms_cm79__form__otherposition').attr("disabled", true);
			$('#cms_cm79__form__otherposition').addClass("disabled");	
		}
    });
	
	$("#cms_cm64__form__branche").change(function() 
    {
		if ($("#cms_cm64__form__branche").val() == 16)
		{
			$('#cms_cm64__form__otherbranche').removeAttr("disabled");
			$('#cms_cm64__form__otherbranche').removeClass("disabled");
		}
		else
		{
			$('#cms_cm64__form__otherbranche').attr("disabled", true);
			$('#cms_cm64__form__otherbranche').addClass("disabled");	
		}
    });
    $("#cms_cm79__form__branche").change(function() 
    {
		if ($("#cms_cm79__form__branche").val() == 16)
		{
			$('#cms_cm79__form__otherbranche').removeAttr("disabled");
			$('#cms_cm79__form__otherbranche').removeClass("disabled");
		}
		else
		{
			$('#cms_cm79__form__otherbranche').attr("disabled", true);
			$('#cms_cm79__form__otherbranche').addClass("disabled");	
		}
    });
    
    $("#cms_cm64__form__interest_8").change(function() 
    {
		if ($("#cms_cm64__form__interest_8").attr('checked') == true)
		{
			$('#cms_cm64__form__otherinterest').removeAttr("disabled");
			$('#cms_cm64__form__otherinterest').removeClass("disabled");
		}
		else
		{
			$('#cms_cm64__form__otherinterest').attr("disabled", true);
			$('#cms_cm64__form__otherinterest').addClass("disabled");	
		}
    });
    
    $("#cms_cm79__form__interest_8").change(function() 
    {
		if ($("#cms_cm79__form__interest_8").attr('checked') == true)
		{
			$('#cms_cm79__form__otherinterest').removeAttr("disabled");
			$('#cms_cm79__form__otherinterest').removeClass("disabled");
		}
		else
		{
			$('#cms_cm79__form__otherinterest').attr("disabled", true);
			$('#cms_cm79__form__otherinterest').addClass("disabled");	
		}
    });
	
    /* ======= SEARCH ========= */
    if($('#cms_cm80__form__country_row').length > 0)
	{
		var selCountries = new Array();
		
		$("input[name*='cms[cm80][form][country]']").each(function(input) 
		{            
            if (this.checked == true)
			{                
                selCountries[selCountries.length] = this.value;
			}
		});		

        generateCountrySearchDom('#cms_cm80__form__country_row', '80', 'search', selCountries);
	}
    if($('#cms_cm129__form__country_row').length > 0)
	{
		var selCountries = new Array();
		
		$("input[name*='cms[cm129][form][country]']").each(function(input) 
		{
			if (this.checked == true)
			{
				selCountries[selCountries.length] = this.value;
			}
		});	
        
        generateCountrySearchDom('#cms_cm129__form__country_row', '129', 'search', selCountries);
	}
    if($('#cms_cm244__form__country_Afghanistan').length > 0)
	{
		var selCountries = new Array();
		
		$("input[name*='cms[cm244][form][country]']").each(function(input) 
		{
			if (this.checked == true)
			{
				selCountries[selCountries.length] = this.value;
			}
		});	
        
        generateCountrySearchDom('#cms_cm244__form__country_row', '244', 'search', selCountries);
	}
    
	/* ======= COUNTRY ========= */
	if($('#cms_cm121__form__country_row').length > 0)
	{
		var selCountries = new Array();
		
		$("input[name*='cms[cm121][form][country]']").each(function(input) 
		{
			if (this.checked == true)
			{
				selCountries[selCountries.length] = this.value;
			}
		});
		
		selCountries = selCountries.join();		
        
        generateCountryDom('#cms_cm121__form__country_row', '121', 'country', selCountries);
	}
	else if ($('#cms_cm64__form__country-import_row').length > 0)
	{
		var selCountries = new Array();
		
		$("input[name*='cms[cm64][form][country-import]']").each(function(input) 
		{
			if (this.checked == true)
			{
				selCountries[selCountries.length] = this.value;
			}
		});
		
		selCountries = selCountries.join();		
        
        generateCountryDom('#cms_cm64__form__country-import_row', '64', 'country-import', selCountries);
	}
    else if ($('#cms_cm64__form__country-export_row').length > 0)
    {
        var selCountries = new Array();
		
		$("input[name*='cms[cm64][form][country-export]']").each(function(input) 
		{
			if (this.checked == true)
			{
				selCountries[selCountries.length] = this.value;
			}
		});
		
		selCountries = selCountries.join();		
        
        generateCountryDom('#cms_cm64__form__country-export_row', '64', 'country-export', selCountries);
    }
	else if ($('#cms_cm79__form__country-import_row').length > 0)
	{
		var selCountries = new Array();
		
		$("input[name*='cms[cm79][form][country-import]']").each(function(input) 
		{
			if (this.checked == true)
			{
				selCountries[selCountries.length] = this.value;
			}
		});
		
		selCountries = selCountries.join();		
        
        generateCountryDom('#cms_cm79__form__country-import_row', '79', 'country-import', selCountries);
	}
    else if ($('#cms_cm79__form__country-export_row').length > 0)
    {
        var selCountries = new Array();
		
		$("input[name*='cms[cm79][form][country-export]']").each(function(input) 
		{
			if (this.checked == true)
			{
				selCountries[selCountries.length] = this.value;
			}
		});
		
		selCountries = selCountries.join();		
        
        generateCountryDom('#cms_cm79__form__country-export_row', '79', 'country-export', selCountries);
    }
    else if ($('#cms_cm156__form__country_row').length > 0)
    {
        var selCountries = new Array();
		
		$("input[name*='cms[cm156][form][country]']").each(function(input) 
		{
			if (this.checked == true)
			{
				selCountries[selCountries.length] = this.value;
			}
		});
		
		selCountries = selCountries.join();		

        generateCountryDom('#cms_cm156__form__country_row', '156', 'country', selCountries);
    }
	
	if($('#cms_cm64__form__otherposition').length > 0)
	{		
		$('#cms_cm64__form__otherposition').attr("disabled", true);
		$('#cms_cm64__form__otherposition').addClass("disabled");		
	}
    else if($('#cms_cm79__form__otherposition').length > 0)
	{		
		$('#cms_cm79__form__otherposition').attr("disabled", true);
		$('#cms_cm79__form__otherposition').addClass("disabled");
	}
	
	if($('#cms_cm64__form__otherbranche').length > 0)
	{		
		$('#cms_cm64__form__otherbranche').attr("disabled", true);
		$('#cms_cm64__form__otherbranche').addClass("disabled");
	}
    else if($('#cms_cm79__form__otherbranche').length > 0)
	{		
		$('#cms_cm79__form__otherbranche').attr("disabled", true);
		$('#cms_cm79__form__otherbranche').addClass("disabled");
	}
    
    if($('#cms_cm64__form__otherinterest').length > 0)
	{		
		$('#cms_cm64__form__otherinterest').attr("disabled", true);
		$('#cms_cm64__form__otherinterest').addClass("disabled");
	}
    else if($('#cms_cm79__form__otherinterest').length > 0)
	{		
		$('#cms_cm79__form__otherinterest').attr("disabled", true);
		$('#cms_cm79__form__otherinterest').addClass("disabled");
	}
});


