//SETTING UP OUR POPUP  - 0 means disabled; 1 means enabled;
var popupQuoteStatus = 0;

$(document).ready(function() {
	$("li").live("click", function() {
		if ($(this).find(":checkbox").attr('checked')) {
			var lineVal = $(this).find("input.hiddenValue").val();
			$(this).find("input.txtValue").val(lineVal);
		} else {
			$(this).find("input.txtValue").val("");
		}
		var qT = 0;
        $("input.txtValue").each(function() {
            if ($(this).val()) {
                qT = parseFloat(qT + parseFloat($(this).val()));
            }
        });
        qT = qT.toFixed(2);
        if (qT == "0.00") {
            $("input.txtTotal").val("");
			$(".btnNext").hide();
        } else {
            $("input.txtTotal").val(qT);
			$(".btnNext").show();
        }
    });
     
    	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

	
	$(".btnNext").click(function() {
		$('.tabs .tab1').removeClass('active');
		$('.tabs .tab2').addClass('active');
		$('#tab1').hide();
		$('#tab2').show();
		return false;
	});
	
	$(".lnkTandC").click(function() {
		//centering with css
		centerQuotePopup("divTandC");
		//load popup
		loadQuotePopup("divTandC");
		return false;
	});
	
	$(".lnkCC").click(function() {
		//centering with css
		centerQuotePopup("divCreditCheck");
		//load popup
		loadQuotePopup("divCreditCheck");
		return false;
	});
	
	$(".divQuotePopupClose").click(function(){
		disableQuotePopup();
	});
	
});

function loadQuotePopup(divID){
	//loads popup only if it is disabled
	if(popupQuoteStatus==0){
		$("#backgroundPopup").css({
		"opacity": "0.7"
	});
	$("#backgroundPopup").fadeIn("slow");
	$("#"+divID).fadeIn("slow");
	popupQuoteStatus = 1;
	}
}
//disabling popup with jQuery magic!
function disableQuotePopup(){
	//disables popup only if it is enabled
	if(popupQuoteStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#divCreditCheck").fadeOut("slow");
		$("#divTandC").fadeOut("slow");
		popupQuoteStatus = 0;
	}
}

function centerQuotePopup(divID){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#"+divID).height();
	var popupWidth = $("#"+divID).width();
	//centering
	$("#"+divID).css({
		"position": "absolute",
		"top": (windowHeight/2-popupHeight/2)+$(document).scrollTop(),
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

function validate() 	{
	$("p.error").hide();
	var formCheck = true;
	var email_addr = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	//Validate first name
	if (!$("#firstName").val()) {
		formCheck = false;
		$("#firstName").next("p.error").show();
	}
	//Validate surname
	if (!$("#surname").val()) {
		formCheck = false;
		$("#surname").next("p.error").show();
	}
	//Validate gender
	if ( !$("#genderM").attr('checked') && !$("#genderF").attr('checked') ) {
		formCheck = false;
		$("#genderM").next("p.error").show();
	}
	//Validate phone
	if (!$("#phone").val()) {
		formCheck = false;
		$("#phone").next("p.error").show();
	}
	//Validate mobile
	if (!$("#mobile").val()) {
		formCheck = false;
		$("#mobile").next("p.error").show();
	}
	//Validate email address
	if (!$("#email").val() || !email_addr.test($("#email").val())) {
		formCheck = false;
		$("#email").next("p.error").show();
	}
	//Validate DOB Day
	if ( $("#dobDay").attr("selectedIndex") == 0 || $("#dobMonth").attr("selectedIndex") == 0 || $("#dobYear").attr("selectedIndex") == 0) {
		formCheck = false;
		$("#dobYear").next("p.error").show();
	}
	//Validate street number
	if (!$("#streetNo").val()) {
		formCheck = false;
		$("#streetNo").next("p.error").show();
	}
	//Validate street name
	if (!$("#street").val()) {
		formCheck = false;
		$("#street").next("p.error").show();
	}
	//Validate that previous street number is filled out based on how long at current address
	if ( $("#currAddrNo").attr('checked') && !$("#prevStreetNo").val()) {
		formCheck = false;
		$("#prevStreetNo").next("p.error").show();
	}
	//Validate that previous street name is filled out based on how long at current address
	if ( $("#currAddrNo").attr('checked') && !$("#prevStreet").val()) {
		formCheck = false;
		$("#prevStreet").next("p.error").show();
	}
	//Validate that previous town/city is filled out based on how long at current address
	if ( $("#currAddrNo").attr('checked') && !$("#prevCity").val()) {
		formCheck = false;
		$("#prevCity").next("p.error").show();
	}
	
	//Validate terms and conditions
	if (!$("#chkTandC").attr('checked')) {
		formCheck = false;
		$("p#TandCError").show();
	}
	
	//Validate credit check
	if (!$("#chkCreditCheck").attr('checked')) {
		formCheck = false;
		$("p#CreditCheckError").show();
	}
	
	if (formCheck) {
		return true;
	} else {
		return false;
	}
}