$(document).ready(function() {


    $("#defaultframework_txtSearch").keyup(function(event) {
        if (event.keyCode == 13) {
            $("#defaultframework_cmdSearch").click();
        }
    });



    //photogallery image switch
    $('.multi-col a').click(function() {
        var $imgSrc = $(this).children().attr("src").replace("sml", "lrg");
        $('img.large-photo').attr("src", $imgSrc);
        return false;
    });

    //product detail tabs
    $('#tabs-nav ul li a').click(function() {
        //remove active class from link & content
        $('#tabs-nav ul li').removeClass('active');
        $('#tabs-body div').removeClass('active');
        //add active class from link & content
        $(this).parent().addClass('active');
        var currentTab = $(this).attr('href');
        $('#' + currentTab).addClass('active');
        return false;
    });

    convertThickboxLinks();

    //hide answer content
    $('div.answer > div').hide();

    $('div.answer > h3').click(function() {

        var $nextDiv = $(this).next();
        var $visibleSiblings = $nextDiv.siblings('div:visible');

        if ($visibleSiblings.length) {
            $visibleSiblings.slideUp('fast', function() {
                $nextDiv.slideToggle('fast');
            });
        }
        else {
            $nextDiv.slideToggle('fast');
        }

        if ($(this).hasClass('open')) {
            $('h3.open').removeClass('open');
        }
        else {
            $('h3.open').removeClass('open');
            $(this).addClass('open');
        }

        return false;
    });

    $('div.answer-item > a.close').click(function() {
        var $openDiv = $(this).parent().parent();
        $openDiv.slideUp('fast');
        $('h3.open').removeClass('open');
        return false;
    });

    if ($('#enter-address').length != 0 && $('.lstPAFAddress :selected').text() == '') {
        $('#enter-address').hide();
        $('.input-address').bind('click keypress', function(e) {
            switch (e.keyCode) {
                case 9:
                case 16:
                case 18:
                case 17:
                    return;
                default:
                    $('#enter-address').fadeIn('slow');
                    $('#select-address').hide();
                    return false
            }
        });
    }

    if ($('#enter-billing-address').length != 0 && $('.lstPAFBillingAddress :selected').text() == '') {
        $('#choose-billing-address').hide();
        $('#enter-billing-address').hide();



        $('.registered-no').bind('click', function() {
            //if (this.checked == true) {
            $('#choose-billing-address').fadeIn('slow');
            // }
        });

        $('.input-billing-address').bind('click keypress', function(e) {
            switch (e.keyCode) {
                case 9:
                case 16:
                case 18:
                case 17:
                    return;
                default:
                    $('#enter-billing-address').fadeIn('slow');
                    $('#select-billing-address').hide();
                    return false
            }
        });
    }

    //If the registered-no is checked (i.e. we did a post back) then display the choose-billing-address
    if ($('.registered-no input').is(':checked') == true) {
        $('#choose-billing-address').fadeIn('show');
    }



    if ($('input.required').length != 0) {
        var currentAmount = 0;
        requiredFields(currentAmount);
    }

    //Added by GD
    $('.validated').change(function() {
        ValidateFieldLength($(this));
    });

    $('.validated').blur(function() {
        ValidateFieldLength($(this));
    });

    $('.optionalvalidated').change(function() {
        ValidateOptionalFieldLength($(this));
    });

    $('.optionalvalidated').blur(function() {
        ValidateOptionalFieldLength($(this));
    });

    $('.validatedconfirmemail').change(function() {
        ValidateConfirmEmail($(this), $('.validatedemail:first'));
    });

    $('.validatedconfirmemail').blur(function() {
        ValidateConfirmEmail($(this), $('.validatedemail:first'));
    });

    $('.validatedemail').change(function() {
        ValidateEmail($(this));
    });

    $('.validatedemail').blur(function() {
        ValidateEmail($(this));
    });
});

window.onload=init;

function init(){
printLink();
}


function requiredFields(c) {
	var element= $('input.required');
	var amountRequired= element.length;
	var currentAmount = c;
	var button= $('#TB_ajaxContent input.submit, input.confirm');
	var src= button.attr('src');
	
	/*
	var newSrc= src.split('.');
	newSrc= newSrc[0]+ '-disabled' + '.' + newSrc[1];
	*/
	
	var srcLength= src.length;
	var srcStart= src.substring(0,srcLength-4);
	var srcEnd= src.substring(srcLength-4, srcLength);
	var newSrc= srcStart + '-disabled' + srcEnd;

	button.attr('src', newSrc);

	button.bind('click', function() {
		return false;
	});
	
	button.addClass('disabled');

	element.bind('click', function() {

	    if (this.checked == true) {
	        currentAmount++;
	        if (currentAmount == amountRequired) {
	            button.attr('src', src);
	            button.removeClass('disabled');

	            button.removeAttr('disabled');

	            button.unbind('click');
	        }
	    }
	    else {
	        currentAmount--;
	        button.attr('src', newSrc);
	        button.addClass('disabled');

	    }
	    //alert(currentAmount);
	});
	
}

function printLink(){
var linkEl

	if(document.getElementById("print")){
	
		linkEl = document.createElement("a");
		linkEl.setAttribute("href","javascript:self.print();");
		linkEl.appendChild(document.createTextNode("Print page"));
		document.getElementById("print").appendChild(linkEl);
		document.getElementById("print").removeChild(document.getElementById("print").firstChild);

	}
}


if(document.images){
ProductsRoll = new Image();
ProductsRoll.src = "/images/nav-1-selected.gif";
ProductsOff = new Image();
ProductsOff.src = "/images/nav-1.gif";

CustomerRoll = new Image();
CustomerRoll.src = "/images/nav-2-selected.gif";
CustomerOff = new Image();
CustomerOff.src = "/images/nav-2.gif";

PartsRoll = new Image();
PartsRoll.src = "/images/nav-3-selected.gif";
PartsOff = new Image();
PartsOff.src = "/images/nav-3.gif";

TradeRoll = new Image();
TradeRoll.src = "/images/nav-4-selected.gif";
TradeOff = new Image();
TradeOff.src = "/images/nav-4.gif";

}

function imgRoll(n){
	if(document.getElementById(n+"Link")){document.getElementById(n+"Link").src = eval(n+"Roll").src}
}

function imgOff(n){
	if(document.getElementById(n+"Link")){document.getElementById(n+"Link").src = eval(n+"Off").src}
}


function updateImg(l){
var thumb = l.firstChild.src;
var start = thumb.lastIndexOf("/");
start++;
var end = thumb.lastIndexOf(".");
var url = "/images/"+thumb.substring(start,end)+"-main.jpg"
document.getElementById("productImg").src=url;
}



function chkForm(fo){
	if(fo.place.value==""){
		alert("Please enter a place name or postcode")
		return false;
	}
}

function convertThickboxLinks() {
    $('a.thickbox').each(function() {
        // For non-JavaScript users, thickbox links point to full pages, with the site navigation etc.
        // When JavaScript is enabled, change the links to point to pages just containing the content, minus site navigation etc., so that they look sensible within thickbox.
        if (this.href.indexOf('.aspx', 0) > -1) {
            this.href = this.href.replace('.aspx', '_ajax.aspx');
        }
        else if (this.href.indexOf('.html', 0) > -1) {
            this.href = this.href.replace('.html', '_ajax.html');
        }
        else {
            this.href = this.href.replace('?', '_ajax.ashx?');
        }
    });

    $('input.thickbox').each(function() {
        if (this.alt.indexOf('.aspx', 0) > -1) {
            this.alt = this.alt.replace('.aspx', '_ajax.aspx');
        }
        else if (this.alt.indexOf('.html', 0) > -1) {
            this.alt = this.alt.replace('.html', '_ajax.html');
        }
        else {
            this.alt = this.alt.replace('?', '_ajax.ashx?');
        }
    });
}



function CheckRequiredUserDetails() {

    var PassedValidation = true;
    $('.validated').each(function() {
        if (!ValidateFieldLength($(this))) {
            PassedValidation = false;
        }
    });

    $('.optionalvalidated').each(function() {
        if (!ValidateOptionalFieldLength($(this))) {
            PassedValidation = false;
        }
    });

    $('.validatedemail').each(function() {
        if (!ValidateEmail($(this))) {
            PassedValidation = false;
        }
    });


    $('.validatedconfirmemail').each(function() {
        if (!ValidateConfirmEmail($(this), $('.validatedemail:first'))) {
            PassedValidation = false;
        }
    });

    return PassedValidation;
};

function ValidateOptionalFieldLength(Field) {
    if (Field.val().toString().length < parseInt(Field.attr('minlength')) &&
            ($('.registered-no input').is(':checked'))) {
        //The entered text is too short

        //Make the input box invalid (have an orange border)
        Field.parents('.optionalvalidatedfield').addClass('invalid');

        Field.parents('.optionalvalidatedfield').children('.error').show();
        Field.parents('.optionalvalidatedfield').find('.errormessage').text('A value is required');
        return false;
    }
    else {
        //The entered text is just right

        //Make this input box valid (not invalid)
        Field.parents('.optionalvalidatedfield').removeClass('invalid');

        Field.parents('.optionalvalidatedfield').children('.error').hide();
        return true;
    }
};

function ValidateFieldLength(Field) {

    if (Field.val().toString().length < parseInt(Field.attr('minlength'))) {
        //The entered text is too short

        //Make the input box invalid (have an orange border)
        Field.parents('.validatedfield').addClass('invalid');

        Field.parents('.validatedfield').children('.error').show();
        Field.parents('.validatedfield').find('.errormessage').text('A value is required');
        return false;
    }
    else {
        //The entered text is just right

        //Make this input box valid (not invalid)
        Field.parents('.validatedfield').removeClass('invalid');

        Field.parents('.validatedfield').children('.error').hide();
        return true;
    }
};

function ValidateEmail(Field) {

    var filter = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i

    if (!filter.test(Field.val())) {
        //The entered text isn't an email address

        //Make the input box invalid (have an orange border)
        Field.parents('.validatedfield').addClass('invalid');

        Field.parents('.validatedfield').children('.error').show();
        Field.parents('.validatedfield').find('.errormessage').text('Must be a valid email');
        return false;
    }
    else {
        //The entered text is just right

        //Make this input box valid (not invalid)
        Field.parents('.validatedfield').removeClass('invalid');

        Field.parents('.validatedfield').children('.error').hide();
        return true;
    }
};

function ValidateConfirmEmail(Field, FieldToMatch) {
    if (Field.val().toString() != FieldToMatch.val().toString()) {
        //The entered text doesn't match

        //Make the input box invalid (have an orange border)
        Field.parents('.validatedfield').addClass('invalid');

        Field.parents('.validatedfield').children('.error').show();
        Field.parents('.validatedfield').find('.errormessage').text('Emails do not match');
        return false;
    }
    else {
        //The entered text is just right

        //Make this input box valid (not invalid)
        Field.parents('.validatedfield').removeClass('invalid');

        Field.parents('.validatedfield').children('.error').hide();
        return true;
    }
};