/*

        Authors: Ferenc Radius, Nuey San Waldman

*/

$(document).ready(function () {

// ________________________________________________________________________________________________
//                                                                                    G E N E R I C  
    /*
    if (jQuery.browser.msie) {
                // Global search tweaks
        $('q').prepend('&ldquo;').append('&rdquo;');
        $('#search #infobank_search_results').css('font-style', 'normal');
    }
    */
    
    $('a[href^="http://"]').attr('target', '_blank'); // Always open external sites in a new window 

    $('a[href $= ".pdf"]').attr('target', '_blank'); // Always open pdf files in a new window

    //$('li#bookmark_link a').bookmarks(); // initialize bookmarks plugin

    $('#product_new div.component_feature div img').attr('style', 'display:block;');

    $('body.v_product p.contact_dealers, body.v_news p.contact_dealers, body.v_profile p.contact_dealers, body.v_technical p.contact_dealers').appendTo('div.page:last');

    $('#content > #infobank_container').prependTo('#content');

// ________________________________________________________________________________________________
//                                                                                            F A Q

    $('#faq_container li div').hide().addClass('hidden');
    $('#faq_container h2').click(function () {
		var totalScrolled = $(window).scrollTop();
		var openFaqHeight = $('#faq_container li h2.active').length > 0 ? $('#faq_container li h2.active').next('div').height() : 0;
		var openFaqOffset = $('#faq_container li h2.active').length > 0 ? $('#faq_container li h2.active').parent().offset().top : 0;
        var curFaqOffset = $(this).parent().offset().top;
		$('#faq_container li div').slideUp(500).removeClass('visible').addClass('hidden');
       	$('#faq_container li h2').removeClass('active');
        var dejaVu = $(this).next('div');
        if (dejaVu.is(':hidden')) {
			if (openFaqHeight > 0 && openFaqOffset < curFaqOffset)
$('html,body').scrollTo( (totalScrolled - openFaqHeight - 36) + 'px', 500 );
//$('html,body').animate({ scrollTop: (totalScrolled - (openFaqHeight + 36))}, 500);
            dejaVu.slideDown(500).removeClass('hidden').addClass('visible');
			$(this).addClass('active');
        }
        return false;
    });
    
    $('a#taf_tac').click(function(e){
			e.preventDefault();
			window.open('/files/taf/terms_and_conditions.html','_blank','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=640,left=0,top=0');
			return false;
		});
});

//___________________________________________________________________________________________________

function reloadImage(imageId){
    // calling and displaying a new security image (captcha) for a given img element id
    document.getElementById(imageId).src = document.getElementById(imageId).src + '#';
}
function validate_email(email){
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);
}
function validate_form(){
    var error = false;
    if (!DWRUtil.getValue("email_address").length ||
    !validate_email(DWRUtil.getValue("email_address"))) {
        $('#label_email_address').css('color', 'red');
        error = true;
    } else {
        $('#label_email_address').css('color', '');
    }
    if (!DWRUtil.getValue("your_email_address").length ||
    !validate_email(DWRUtil.getValue("your_email_address"))) {
        $('#label_your_email_address').css('color', 'red');
        error = true;
    } else {
        $('#label_your_email_address').css('color', '');
    }
    if (!DWRUtil.getValue("confirmation").length) {
        $('#label_confirmation').css('color', 'red');
        error = true;
    } else {
        $('#label_confirmation').css('color', '');
    }
    if (!DWRUtil.getValue("your_name").length) {
        $('#label_your_name').css('color', 'red');
        error = true;
    } else {
        $('#label_your_name').css('color', '');
    }
    if (DWRUtil.getValue("personal_message").length > 1000) {
        $('#label_personal_message').css('color', 'red');
        error = true;
    } else {
        $('#label_personal_message').css('color', '');
    }
    if (error) {
        return false;
    }
    var data = [DWRUtil.getValue("email_address"), DWRUtil.getValue("toName"), DWRUtil.getValue("message"), DWRUtil.getValue("your_email_address"), DWRUtil.getValue("confirmation"), DWRUtil.getValue("article_id")];
    CaptchaValidator.validate(data, validateCalllback);
    return false;
}

function validateCalllback(accepted){
    if (!accepted.length) {
        reloadImage('captcha_image');
        alert("Your e-mail was sent successfully");
        $('input#email_address, input#confirmation').attr('value', '');
				$('input#email_address').focus();
				$('input#confirmation').attr('value', '');
        /*
        $('<div class="dialog_container" id="message_success"><p><strong>Your e-mail was sent successfully</strong></p><a id="taf_resend" href="#"><img src="./files/gfx/taf/taf_button_resend.png" alt="Send to another friend" title="" /></a></div>').appendTo('li#taf_tab.active');
        $('#taf_resend').click(function(){
            $('input#email_address, input#confirmation').attr('value', '');
            $('input#email_address').focus();
            $('input#confirmation').attr('value', '');
            $('#message_success').fadeTo(200, 0).remove();
            return false;
        });
        */
    } else {
        $('#label_confirmation').css('color', 'red');
    }
}

