$(document).ready(function(){

        //nieuwsbrief popin
    $(".nieuwsbriefpopin").fancybox({
        'width'				: '40%',
        'height'			: '90%',
        'autoScale'			: false,
        'transitionIn'		: 'none',
        'transitionOut'		: 'none',
        'type'				: 'iframe',
        'href'              : 'http://informaxion.communicationmaster.nl/2/optin.php'
    });
        
    /*-----jquery drom down select-------*/
    $('#lang').jqDropDown({
        direction: 'down',
        optionChanged: function(){
            lang = $('#lang option:selected').val();
            
            if(lang == 2) 
            {
                document.location.href = 'http://www.fax-services.com/';
            }
            else
            {
                document.location.href = 'http://www.faxservice.nl/';
            }
        },
        defaultStyle: false,
        containerName: 'theContainer',
        toggleBtnName: 'awesomeToggleClass',
        optionListName: 'thisListIsRocking',
        effect:'fade'
    });
    
    $('#contactwantto').jqDropDown({
        direction: 'down',
        /*optionChanged: function(){
            $('#contact_form').submit();
        },*/
        defaultStyle: false,
        containerName: 'theContainer',
        toggleBtnName: 'awesomeToggleClass',
        optionListName: 'thisListIsRocking',
        effect:'fade'
    });
    /*-----------------wizard proefaccount formmulier stap1----------------*/
    function getFaxnumbers(urlAction, areacode){
        countrycode = getSelectedOption($("#ajax_land a.awesomeToggleClass").text(), '#ajax_land select option');
        $.post(
            urlAction,
            {areacode: areacode, countrycode: countrycode},
             function(faxenumbers){
                radiobuttons = "";
                radiobuttons += "<tr>";
                var c = 0; // Our counter
                var n = 3; // Each Nth iteration would be a new table row
                for (x in faxenumbers)
                {
                    if (c % n == 0 && c != 0) {
                       radiobuttons += '</tr><tr>';
                    }
                    c++;
                    var notFormatedFaxNumber = faxenumbers[x].replace(/[^\d.]/g, "");

                    radiobuttons += "<td>";
                    radiobuttons += "<label class='cursor-pointer faxnumers' for='"+notFormatedFaxNumber+"'>";
                    radiobuttons += "<input id='"+notFormatedFaxNumber+"' type='radio' name='faxno' value='"+notFormatedFaxNumber+"'/> " + faxenumbers[x] + "</label><td>";
                  }
                radiobuttons += "</tr>";
                $('table#faxnumbers').html(radiobuttons);
            }
        );
    }

    $('#wizard-land').jqDropDown({
        direction: 'down',
        optionChanged: function(){
            //post to get area codes
            $('table#faxnumbers tbody').remove();
            $.post(
                '/wizard/areacodes/format/json',
                {countryId: $("select#wizard-land").val()},
                 function(aAreacodes){
                     selectOptions = "";
                     selectLi = "";
                     for (var index in aAreacodes)
                    {
                        selectOptions += "<option value='"+index+"'>" +"0"+index+ " (" + aAreacodes[index]+ ")" + "</option>";
                        selectLi += "<li class='ddOption'><a rel='"+index+"' href='#' class=''>"+"0"+index+ " (" + aAreacodes[index]+")"+"</a></li>";
                    }
                    for (firstIndex in aAreacodes) break;
                    $('#wizard_form_step1 select#wizard-netnummer').html(selectOptions);
                    $('#ajax_netnummers ul').html(selectLi);
                    $('#ajax_netnummers a.awesomeToggleClass').text(+"0"+firstIndex+ " (" + aAreacodes[firstIndex]+")");
                }
            );
            ////////////
            var urlAction = '/wizard/faxnumbersbyareacode/format/json';
            getFaxnumbers(urlAction, $("select#wizard-netnummer").val());

            ///////////
            //post to get cities
            $.post(
                '/wizard/cities/format/json',
                {countryId: $("select#wizard-land").val()},
                 function(aCities){
                     _selectOptions = "";
                     _selectLi = "";
                     for (var index in aCities)
                    {
                        _selectOptions += "<option value='"+aCities[index]+"'>"+index+ " ("+"0"+aCities[index]+")"+"</option>";
                        _selectLi += "<li class='ddOption'><a rel='"+index+ "("+index+")' href='#' class=''>"+index+ " ("+"0"+aCities[index]+")"+"</a></li>";
                    }
                    for (firstIndex in aCities) break;
                    $('#wizard_form_step1 select#wizard-plaats').html(_selectOptions);
                    $('#ajax_plaats ul').html(_selectLi);
                    $('#ajax_plaats a.awesomeToggleClass').text(firstIndex+ " (" +"0"+ aCities[firstIndex]+")" );
                }
            );
            //event.preventDefault();
        },
        defaultStyle: false,
        containerName: 'theContainer',
        toggleBtnName: 'awesomeToggleClass',
        optionListName: 'thisListIsRocking',
        effect:'fade'
    });

    /**
     * OptionText = text van option element
     * selectObj  = select object
     */
    function getSelectedOption(OptionText, selectObj){
        var value = 0;
         $(selectObj).each(function(){
            if($(this).text() ==  OptionText){
                value = $(this).val();
                 return false;
            }
         });
        return value;
    }
    //post to get faxnumbers when we clik on an areacode
    $('#wizard-netnummer').jqDropDown({
        direction: 'down',
        optionChanged: function(){
            var urlAction = '/wizard/faxnumbersbyareacode/format/json';
            getFaxnumbers(urlAction, $("select#wizard-netnummer").val());
        },
        defaultStyle: false,
        containerName: 'theContainer',
        toggleBtnName: 'awesomeToggleClass',
        optionListName: 'thisListIsRocking',
        effect:'fade'
    });

    //post to get faxnumbers when we clik on a cityname
    $('#wizard-plaats').jqDropDown({
        direction: 'down',
        optionChanged: function(){
            areaCode = getSelectedOption($("#ajax_plaats a.awesomeToggleClass").text(), 'select#wizard-plaats option');
            var urlAction = '/wizard/faxnumbersbycity/format/json';
            getFaxnumbers(urlAction, areaCode);
        },
        defaultStyle: false,
        containerName: 'theContainer',
        toggleBtnName: 'awesomeToggleClass',
        optionListName: 'thisListIsRocking',
        effect:'fade'
    });

    /*-----------------end of wizard proefaccount formmulier stap1 script----------------*/

    /*change opacity of logos in the footer */
    $('.imgOpa').each(function() {
        $(this).hover(
            function() {
                $(this).stop().animate({
                    opacity: 1.0
                }, 8);
            },
            function() {
                $(this).stop().animate({
                    opacity: 0.3
                }, 800);
            })
    });

    /**
     *validatie van contact formulier
     *(niet het quick formulier aan de rechter kant maar het uitgebreide formulier)
     */
  	$("#contact_form").validate({
		rules: {
			name: "required",
			description: "required",
			emailadres: {
				required: true,
				email: true
			},
 			phone: "required"
		},
		messages: {
			description: "U heeft geen vraag/opmerking gesteld.",
			name: "U heeft uw naam niet ingevuld.",
			emailadres: "U heeft uw e-mailadres niet ingevuld.",
            phone: "U heeft uw telefoonnummer niet ingevuld."
		}
	});

    /**
     *validatie van snel contact formulier aan de rechterkant
      */
  	$("#quickContactForm").validate({
		rules: {
			name: "required",
			message: "required",
 			phone: "required"
		},
 		messages: {
			message: "",
			name: "",
            phone: ""
		}
	});

    /*--voor faq om block texten in en uitklappen--*/
    $('#toggle-view li').click(function () {

        var text = $(this).children('p');

        if (text.is(':hidden')) {
            text.slideDown('200');
            $(this).children('span').html("<img src='/images/slideup_icon.png' />");
            $(this).css('backgroundColor', '#e8f0f4');
            $(this).children("ul li h3").css('color', '#0092cf');
        } else {
            text.slideUp('200');
            $(this).children('span').html("<img src='/images/slidedown_icon.png' />");
            $(this).css('backgroundColor', '#fff');
            $(this).children("ul li h3").css('color', '#455560');
         }

    });

    //posten van het formulier op pagin stap2
    $('a.btn-wizard').click(function() {
        $('#wizard_form_step2').submit();
        return false;
    });

    // tab stap 2 uitzetten op stap2.phtml tpl
    $("#step2").removeAttr('href');
    $("#step2").css('borderBottom','none');

    $('a.btn-wizard').click(function() {
      $('#wizard_form_step1').submit();
      return false;
    });
    
    /* workaround for the referenties list. because I did not feel like rebuilding the css*/
    $('table.referenties tbody tr').hover(
            function(){
                $('td.partner_logo img.grayscale',this).hide();
                $('td.partner_logo img.color',this).show();
            },
            function(){
                $('td.partner_logo img.color',this).hide();
                $('td.partner_logo img.grayscale',this).show();
            }
        );
    
    
   //Form validator for question form
	$('form#question-form').validate({
		errorClass:'error'
		//submitHandler: function(form) {
		//	form.submit();
		//}
	});

    /*======wizard stap 2=======*/
    $("form#wizard_form_step2 #company").keyup(function(){

        var temp = $("form#wizard_form_step2 #company").val();
        var inputVal = temp.replace(/[^a-zA-Z]+/g, '');
        $("form#wizard_form_step2 #fax_account").val(inputVal);
    });

    $("form#wizard_form_step2 #lastname").keyup(function(){
        if($("form#wizard_form_step2 #company").val().length < 1){
            var temp = $("form#wizard_form_step2 #lastname").val();
            var inputVal = temp.replace(/[^a-zA-Z]+/g, '');
            $("form#wizard_form_step2 #fax_account").val(inputVal);
        }
    });


    $("form#wizard_form_step2 #fax_account").keyup(function(){
        var temp = $("form#wizard_form_step2 #fax_account").val();
        var inputVal = temp.replace(/[^a-zA-Z]+/g, '');
        $("form#wizard_form_step2 #fax_account").val(inputVal);
    });

    $('div#bt-content a#sluiten-account-beschrijving').click(function(){
        alert('dfgdf');
    });

});

