$(document).ready(function() {
    $(document).pngFix(); // PNG fix for IE6
    openInNewWindow(); // Open links in new window
    $('body').addClass('domenabled');
    $("#footer ul li:first-child").css("border-left", "none");

    /* Slideshow */
    $('#contact .callme').fancyslideshow({
        slideshowNavigation: false,
        keyNavigation: false,
        animationSpeed: 500
    });

    /* Contact selection */
    $('#contact #ContactBlock5, #contact #ContactBlock6, #contact #ContactBlock7, #contact #ContactBlock37').hide();
    $('#contact select').change(function() {
        var selectVal = $('#contact select').attr('value');
        if ($('#contact #ContactBlock' + selectVal + ':visible')) {
            $('#contact #ContactBlock5, #contact #ContactBlock6, #contact #ContactBlock7, #contact #ContactBlock37').hide();
            $('#contact #ContactBlock' + selectVal).fadeIn(1000);
        }
    });

    /* Contact display correct person */
    if ($('#navigation li.active a.import').length == 1) {
        $('#contact #ContactBlock5').show();
    }
    if ($('#navigation li.active a.lease').length == 1) {
        $('#contact #ContactBlock6').show();
    }
    if ($('#navigation li.active a.insurance').length == 1) {
        $('#contact #ContactBlock7').show();
    }
    if ($('#navigation li.active a.mkb').length == 1) {
        $('#contact #ContactBlock37').show();
    }

    // Faq page animations
    $(".faq .link-list li p").hide();
    $(".faq .link-list li a").toggle(function() {
        $(this).next("p").fadeIn("slow");
    }, function() {
        $(this).next("p").slideUp("slow");
    });

    // Disable tab
    $('fieldset:not(:first) input, fieldset:not(:first) a').attr('disabled', 'disabled');
    // Validate phone
    $('.phonecheck input.text').after('<span class="message"></span>');

    $('.phonecheck input.text').bind("keydown", function() {
        $('.phonecheck span.message').html('<img src="../include/images/icons/ajax-loader.gif" height="16" width="16" />');
        CheckPhone(GetInputObject('_contactPhone'));
    });
});

function openInNewWindow() {
	$("a[rel='blank']").each(function() {
		$(this).attr({
			href: "javascript:window.open('" + $(this).attr("href") + "');void(0);", 
			title: "Link opent in een nieuw venster"
		});
	});
}

/* Form validation */
function ValidatePhoneMeFormStep1() {
    var checkInfo = CheckRequired('_contactInfoDropDown');
    var checkRadio = CheckRequiredRadio('_contactTitleRadioList');
    var checkSurname = CheckRequired('_contactSurnameTextBox');

    if (checkInfo &&
        checkRadio &&
        checkSurname) {
        return true;
    }
    return false;
}

function GetInputObject(id) {
    var elem;
    var objs = $(':input');
    for (i = 0; i < objs.length; i++) {
        if (objs[i].id.indexOf(id) > 0) {
            elem = objs[i];
            break;
        }
    }
    return elem;
}

function CheckRequired(id) {
    var element = GetInputObject(id);
    $('#' + element.id).val($('#' + element.id).val().replace('<','').replace('>',''));
    if ($('#' + element.id).val().length > 0) {
        SetErrorClass(element.id, true);
        return true;
    }
    else {
        SetErrorClass(element.id, false);
        return false;
    }
}

function CheckRequiredRadio(id) {
    var element = GetInputObject(id);
    var groupName = element.id.replace('_0', '').replace(/_/g, '$').replace('$$', '$_');
    if ($('input:radio[name=' + groupName + ']:checked').val() != undefined) {
        SetErrorClass('contactradiogroup', true);
        return true;
    }
    else {
        SetErrorClass('contactradiogroup', false);
        return false;
    }
}

function CheckPhone(source, args) {
	if (source.controltovalidate) {
		var element = document.getElementById(source.controltovalidate);
	} else {
		var element = GetInputObject(source);
    }
    element.value = element.value.replace(/ /g, '');
	
	if (/((^06((\s{0,1})|(\-{0,1}))[0-9]{8}$)|(^0[0-9]{2}(\s{0,1}|\-{0,1})[0-9]{7}$)|(^0[0-9]{3}(\s{0,1}|\-{0,1})[0-9]{6}$))/i.test(element.value)) {
		if (args) {
			args.IsValid = true;
		}
		$('.phonecheck span.message').html('<img src="../include/images/icons/correct.gif" height="16" width="16" />');
	}
	else {
		if (args) {
			args.IsValid = false;
		}
		$('.phonecheck span.message').html('<img src="../include/images/icons/error.gif" height="16" width="16" />');
	}
	if (args) {
		SetErrorClass(source.controltovalidate, args.IsValid);
    }

}

function CheckRequiredVal(source, args) {
    $('#' + source.controltovalidate).val($('#' + source.controltovalidate).val().replace(/</g, '').replace(/>/g, ''));
	
    if (args.Value.length > 0) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }
    SetErrorClass(source.controltovalidate, args.IsValid);
}

function SetErrorClass(elemId, isValid) {
	var element = document.getElementById(elemId);
	if (isValid) {
		element.className = element.className.replace(' error', '');
		if (element.className == 'error') {
			element.className = '';
		}
	}
	else {
		element.className = element.className.replace(' error', '') + ' error';
	}
}