$(document).ready(function(){
    DD_roundies.addRule('.rc_05a', '5px', true);
    DD_roundies.addRule('.rc_05t', '5px 5px 0px 0px', true);
    DD_roundies.addRule('.rc_05b', '0px 0px 5px 5px', true);
    DD_roundies.addRule('.tab_box', '0px 5px 5px 5px', true);
    
    if (!$.browser.msie) {
        DD_roundies.addRule('.rc_05a_faq', '5px', true);
        DD_roundies.addRule('.rc_05b_faq', '0px 0px 5px 5px', true);
    }
    
    $(".box.no_top").css("border-top", "none");
    
    $("#tabs_02").css("display", "none");
    $("#tabs_03").css("display", "none");
    
    $("#fcform2").submit(function(){
        if ($('#email').val().length >= 1) {
            checkField('email', 'EMAIL', 'Email');
        }
        checked = $('input[name=preferred_contact]:checked').val();
        if (checked !== undefined) {
            if ((checked == 'email')) {
                if (($('#email').val().length < 1)) {
                    $('#msgarea').html("Email cannot be empty").show();
                    document.getElementById('email').focus();
                    return false;
                }
                checkField('email', 'EMAIL', 'Email');
            }
            else 
                if ((checked == 'sms')) {
                    if (($('#mobile').val().length < 1)) {
                        $('#msgarea').html("Mobile number is not a valid number").show();
                        document.getElementById('mobile').focus();
                        return false;
                    }
                    checkField('mobile', 'ALPHANUMSPACE', 'Mobile number');
                }
                else 
                    if ((checked == 'textphone')) {
                        if (($('#textphone').val().length < 1)) {
                            $('#msgarea').html("Please ensure you provided a valid textphone number").show();
                            document.getElementById('textphone').focus();
                            return false;
                        }
                        checkField('phone', 'ALPHANUMSPACE', 'Telephone');
                    }
                    else 
                        if ((checked == 'voice')) {
                            if (($('#phone').val().length < 1)) {
                                $('#msgarea').html("Please ensure you provided a valid telephone number").show();
                                document.getElementById('phone').focus();
                                return false;
                            }
                            checkField('phone', 'ALPHANUMSPACE', 'Telephone');
                        }
        }
        
        return validate.check();
    });
    
    if ($("#fcform2").is(':visible')) {
        required.add('message', 'NOT_EMPTY');
        required.add('verification', 'NUMERIC');
    }
    
    $("#register").submit(function(){
        var re_0 = new RegExp(/^[0]{1}\d{10}$/);
		var re_447 = new RegExp(/^[4]{2}[7]{1}\d{9}$/);
		
		if ((document.register.msn.value.match(re_447) != null) || (document.register.msn.value.match(re_0) != null)) {
			return true;
        }   
		
        $("#msgarea").html('The mobile number should contain 11 digits starting with "0" or 12 digits starting with "447"').show();
		return false;
    });
    
    $("#verification").keyup(function(e){
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            $("#msgarea").html("Digits Only").show();
            return false;
        }
        
        var text = $(this).val();
        var textlength = text.length;
        if (textlength > 4) {
            $(this).val(text.substr(0, 4));
            return false;
        }
        else {
            return true;
        }
    });
    
    // BSL videos
    $("a.media").click(function(){
        $(this).parent().find('a').each(function(){
            $(this).removeClass('playing');
        })
        $(this).toggleClass('playing');
        
        attrs = $(this).attr('rel').split('#');
        var so = new SWFObject('http://emergencysms.org.uk/flash/player.swf', 'mpl', '318', '300', '9');
        so.addVariable('start', '100');
        so.addVariable('file', 'http://emergencysms.org.uk/flash/videos/' + attrs[0]);
        so.addVariable('title', attrs[1]);
        so.addVariable('duration', attrs[2]);
        so.addVariable('respectduration', 'true');
        so.addVariable('backcolor', '0x000000');
        so.addVariable('stretching', 'exactfit');
        so.addVariable('frontcolor', '0xEEEEEE');
        so.addVariable('lightcolor', '0xFFFFFF');
        so.addVariable('skin', 'http://emergencysms.org.uk/flash/skins/dangdang.swf');
        so.addVariable("autostart", "true");
        
        so.addParam('allowscriptaccess', 'always');
        so.addParam('allowfullscreen', 'true');
        so.addParam('seamlesstabbing', 'true');
        
        so.write('player');
        
        return false;
    });
});

function checkField(input, type, title){
    if (validate.checkit(input, type)) {
        // validated okay
    }
    else {
        $('#msgarea').html(title + " is not valid").show();
        document.getElementById(input).focus();
        return false;
    }
}

var player;
function playerReady(obj){
    player = document.getElementById(obj['id']);
};

// Delay Plugin for jQuery
// - http://www.evanbot.com
// - 2008 Evan Byrne
jQuery.fn.delay = function(time, func){
    return this.each(function(){
        setTimeout(func, time);
    });
};

// for mini popup browser windows
function popUp(URL, width, height){
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=0,width=" + width + ",height=" + height + "');");
}

