$('html').addClass("js");

$(document).ready(function() {

	//external links excluding any elements which have child elements
	var h = window.location.host.toLowerCase();
    $('a[href^="http://"]').not("[href*='"+h+"']").each(function(){
		$(this).children().addClass("childExternal");
		if (!$(this).children().hasClass("childExternal") && !$(this).parent().is(":header")){
			if (!$(this).hasClass("relatedLink")){
				$(this).addClass('external').append("<span title='This is an external link'>&nbsp;</span>");
			}
		}
		$(this).children().removeClass("childExternal");
    })

    //table row striping
    $("table tbody").each(function() {
        $(this).find("tr:odd").addClass('even');
        $(this).find("tr:even").addClass('odd');
    })

    //document list striping
    $(".documents ul.library").each(function() {
        $(this).find("li:odd").addClass('even');
        $(this).find("li:even").addClass('odd');
    })

    //toggle readyreckoner TEMP
    $("#iReadyReckoner").toggleResult();
    $("#iReadyReckonerAlt").toggleResult();

    //help
    $("a.help").helpToggle();

    //JSON validation
    if ($(".validate").length) {
        $(".validate").think_validation({
            formId: "form",
            urlString: window.location.href, //live data
            mainMessageId: "iError"
        });
    }

    //Polls labels
    $('.pollResults').find("dd").each(function() {
        var ddWidth = parseInt($(this).css("width"));
        if (ddWidth >= 20) {
            $(this).css("color", "#fff");
        }
    })


    //JSON self diagnosis
    if ($("#iQuestions").length) {
        $("#iQuestions").diagnosis();
    }


    //Homepage ajax tabs
    if ($("#iTabs").length) {
        $("#iTabs").ajaxTabs();
    }

	if ($("#iPostcodeLookup").length){
		$("#iPostcodeLookup").postcodeLookup();
	}
    

    //Replace Postcode text on focus
    $("#UserPreferences1_PostCodeText").replaceText();

    //news section tabs
    $(".tabs").newsTabs();


    $("body").addClass("enhance");

    //add enhance stylesheet
    var root = $('script[@src*="_assets/"]:eq(0)').attr('src').split('_assets')[0];
    $('head link:eq(0)').after('<link rel="stylesheet" type="text/css" media="screen" href="' + root + '_assets/css/enhance.css" />');

    //lightbox
    if ($(".lightbox").length) {
        $(".lightbox").lightbox();
    }

    //big links
    if (typeof (redDotMode) == 'undefined') {
        $("#iQuicklinks .quicklink").hover(function() {
            $(this).addClass("hover");
        }, function() {
            $(this).removeClass("hover");
        })
    .bigLink()
    }

    //toggle help
    if (typeof (redDotMode) == 'undefined') {
        $(".faqs dl").toggleList();
    }


    //rounded corners
    $("div.help, .sign-in, .message, #iBody, .filtered-list, .quote, #iQuicklinks .related, .highlightB, .group, .tariff-list, .highlight, .area-activity, .parish, .case-studies, .case-study, .contact, .task-list, .latest-list, .package-summary,.sub-pages").roundedCorners(true);
    $("#iPrefHelp .information, .type-summary .summary").roundedCorners(false);
    $("#iNavL2 h2").roundedCorners(true, 1);
    $("#iPreferences .footer, .frmFooter").roundedCorners(true, 'blbr');
    $(".your-area-map .postcode-search").roundedCorners(false, 'tltr');
    $(".area-list .active").roundedCorners(true, 3);
    $(".search-wrapper").roundedCorners(false, 'tlbl');

    
    //upload overlay
    if ($("#uploadProgress").length) {
        $("#uploadProgress").overlay();
    }
    
    //date picker

    //household - apply for reduced sewerage charge
    $('#SelfDiagnosis_ucDateFormCompleted_dateSelectorDiv').dateHelper(
	{
	    linkId: 'SelfDiagnosis_ucDateFormCompleted_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#SelfDiagnosis_ucDateFormCompleted_iSelect_dd',
	    selectMthId: '#SelfDiagnosis_ucDateFormCompleted_iSelect_mm',
	    selectYrId: '#SelfDiagnosis_ucDateFormCompleted_iSelect_yyyy'
	});

    //household - update address details
    $('#UpdateAddressDetails_ucDateStoppedWater_dateSelectorDiv').dateHelper(
	{
	    linkId: 'UpdateAddressDetails_ucDateStoppedWater_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#UpdateAddressDetails_ucDateStoppedWater_iSelect_dd',
	    selectMthId: '#UpdateAddressDetails_ucDateStoppedWater_iSelect_mm',
	    selectYrId: '#UpdateAddressDetails_ucDateStoppedWater_iSelect_yyyy'
	});

    $('#UpdateAddressDetails_ucTenancyDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'UpdateAddressDetails_ucTenancyDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#UpdateAddressDetails_ucTenancyDate_iSelect_dd',
	    selectMthId: '#UpdateAddressDetails_ucTenancyDate_iSelect_mm',
	    selectYrId: '#UpdateAddressDetails_ucTenancyDate_iSelect_yyyy'
	});

    $('#UpdateAddressDetails_ucDateStartUsingWater_dateSelectorDiv').dateHelper(
	{
	    linkId: 'UpdateAddressDetails_ucDateStartUsingWater_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#UpdateAddressDetails_ucDateStartUsingWater_iSelect_dd',
	    selectMthId: '#UpdateAddressDetails_ucDateStartUsingWater_iSelect_mm',
	    selectYrId: '#UpdateAddressDetails_ucDateStartUsingWater_iSelect_yyyy'
	});

    //household - provide a meter reading
    $('#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl0_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl0_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl0_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl0_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl0_ucReading_ucDate_iSelect_yyyy'
	});

	$('#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl1_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl1_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl1_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl1_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl1_ucReading_ucDate_iSelect_yyyy'
	});

	$('#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl2_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl2_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl2_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl2_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl2_ucReading_ucDate_iSelect_yyyy'
	});

	$('#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl3_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl3_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl3_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl3_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl3_ucReading_ucDate_iSelect_yyyy'
	});

	$('#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl4_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl4_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl4_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl4_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ProvideAMeterReading_AddWaterMeterReadings1_lvReadings_ctrl4_ucReading_ucDate_iSelect_yyyy'
	});

    //household - apply for a meter
    $('#ApplyForAMeter_ucMovingDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ApplyForAMeter_ucMovingDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ApplyForAMeter_ucMovingDate_iSelect_dd',
	    selectMthId: '#ApplyForAMeter_ucMovingDate_iSelect_mm',
	    selectYrId: '#ApplyForAMeter_ucMovingDate_iSelect_yyyy'
	});

    //household - leakage allowance
    $('#LeakageAllowance_ucDateOfLeak_dateSelectorDiv').dateHelper(
	{
	    linkId: 'LeakageAllowance_ucDateOfLeak_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#LeakageAllowance_ucDateOfLeak_iSelect_dd',
	    selectMthId: '#LeakageAllowance_ucDateOfLeak_iSelect_mm',
	    selectYrId: '#LeakageAllowance_ucDateOfLeak_iSelect_yyyy'
	});

    $('#LeakageAllowance_ucDateLeakRepaired_dateSelectorDiv').dateHelper(
	{
	    linkId: 'LeakageAllowance_ucDateLeakRepaired_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#LeakageAllowance_ucDateLeakRepaired_iSelect_dd',
	    selectMthId: '#LeakageAllowance_ucDateLeakRepaired_iSelect_mm',
	    selectYrId: '#LeakageAllowance_ucDateLeakRepaired_iSelect_yyyy'
	});

    $('#LeakageAllowance_ucMeterReading1_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'LeakageAllowance_ucMeterReading1_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#LeakageAllowance_ucMeterReading1_ucDate_iSelect_dd',
	    selectMthId: '#LeakageAllowance_ucMeterReading1_ucDate_iSelect_mm',
	    selectYrId: '#LeakageAllowance_ucMeterReading1_ucDate_iSelect_yyyy'
	});

    $('#LeakageAllowance_ucMeterReading2_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'LeakageAllowance_ucMeterReading2_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#LeakageAllowance_ucMeterReading2_ucDate_iSelect_dd',
	    selectMthId: '#LeakageAllowance_ucMeterReading2_ucDate_iSelect_mm',
	    selectYrId: '#LeakageAllowance_ucMeterReading2_ucDate_iSelect_yyyy'
	});

    $('#LeakageAllowance_ucDateCompleted_dateSelectorDiv').dateHelper(
	{
	    linkId: 'LeakageAllowance_ucDateCompleted_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#LeakageAllowance_ucDateCompleted_iSelect_dd',
	    selectMthId: '#LeakageAllowance_ucDateCompleted_iSelect_mm',
	    selectYrId: '#LeakageAllowance_ucDateCompleted_iSelect_yyyy'
	});

    //household - apply for watercare
    $('#WaterCare_ucDateFormCompleted_dateSelectorDiv').dateHelper(
	{
	    linkId: 'WaterCare_ucDateFormCompleted_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#WaterCare_ucDateFormCompleted_iSelect_dd',
	    selectMthId: '#WaterCare_ucDateFormCompleted_iSelect_mm',
	    selectYrId: '#WaterCare_ucDateFormCompleted_iSelect_yyyy'
	});

    //business - update premises details
    $('#UpdatePremisesDetails_ucPreviousDateStopped_dateSelectorDiv').dateHelper(
	{
	    linkId: 'UpdatePremisesDetails_ucPreviousDateStopped_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#UpdatePremisesDetails_ucPreviousDateStopped_iSelect_dd',
	    selectMthId: '#UpdatePremisesDetails_ucPreviousDateStopped_iSelect_mm',
	    selectYrId: '#UpdatePremisesDetails_ucPreviousDateStopped_iSelect_yyyy'
	});

    $('#UpdatePremisesDetails_ucDateTenancyStart_dateSelectorDiv').dateHelper(
	{
	    linkId: 'UpdatePremisesDetails_ucDateTenancyStart_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#UpdatePremisesDetails_ucDateTenancyStart_iSelect_dd',
	    selectMthId: '#UpdatePremisesDetails_ucDateTenancyStart_iSelect_mm',
	    selectYrId: '#UpdatePremisesDetails_ucDateTenancyStart_iSelect_yyyy'
	});

    $('#UpdatePremisesDetails_ucDateStartUsingWater_dateSelectorDiv').dateHelper(
	{
	    linkId: 'UpdatePremisesDetails_ucDateStartUsingWater_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#UpdatePremisesDetails_ucDateStartUsingWater_iSelect_dd',
	    selectMthId: '#UpdatePremisesDetails_ucDateStartUsingWater_iSelect_mm',
	    selectYrId: '#UpdatePremisesDetails_ucDateStartUsingWater_iSelect_yyyy'
	});

	//business - provide a meter reading
	$('#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl0_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl0_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl0_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl0_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl0_ucReading_ucDate_iSelect_yyyy'
	});

    $('#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl1_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl1_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl1_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl1_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl1_ucReading_ucDate_iSelect_yyyy'
	});

    $('#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl2_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl2_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl2_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl2_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl2_ucReading_ucDate_iSelect_yyyy'
	});

    $('#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl3_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl3_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl3_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl3_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl3_ucReading_ucDate_iSelect_yyyy'
	});

    $('#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl4_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl4_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl4_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl4_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ProvideAMeterReadingBusiness_AddWaterMeterReadings1_lvReadings_ctrl4_ucReading_ucDate_iSelect_yyyy'
	});

    //developers - planning your development
    $('#PreDevelopment_ucSiteStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucSiteStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucSiteStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucSiteStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucSiteStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucFirstOccupancyDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucFirstOccupancyDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucFirstOccupancyDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucFirstOccupancyDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucFirstOccupancyDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl0_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl0_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl0_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl0_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl0_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl1_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl1_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl1_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl1_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl1_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl2_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl2_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl2_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl2_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl2_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl3_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl3_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl3_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl3_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl3_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl4_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl4_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl4_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl4_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl4_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl5_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl5_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl5_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl5_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl5_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl6_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl6_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl6_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl6_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl6_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl7_ateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl7_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl7_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl7_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl7_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl8_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl8_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl8_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl8_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl8_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl9_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl9_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl9_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl9_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl9_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl10_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl10_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl10_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl10_ucStartDate_iSelect_dd',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl10_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl11_ucStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl11_ucStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl11_ucStartDate_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl11_ucStartDate_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucConstructionPeriods_lvPeriods_ctrl11_ucStartDate_iSelect_yyyy'
	});

    $('#PreDevelopment_ucDateFormCompleted_dateSelectorDiv').dateHelper(
	{
	    linkId: 'PreDevelopment_ucDateFormCompleted_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#PreDevelopment_ucDateFormCompleted_iSelect_dd',
	    selectMthId: '#PreDevelopment_ucDateFormCompleted_iSelect_mm',
	    selectYrId: '#PreDevelopment_ucDateFormCompleted_iSelect_yyyy'
	});

    //developers - commencement of service connections
    $('#CommencementOfServiceConnections_ucLayingStartDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'CommencementOfServiceConnections_ucLayingStartDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#CommencementOfServiceConnections_ucLayingStartDate_iSelect_dd',
	    selectMthId: '#CommencementOfServiceConnections_ucLayingStartDate_iSelect_mm',
	    selectYrId: '#CommencementOfServiceConnections_ucLayingStartDate_iSelect_yyyy'
	});

    //developers - completed service connection
    $('#CompleteServiceConnections_ucPlots_lsvPlots_ctrl0_ucDateMeterInstalled_dateSelectorDiv').dateHelper(
	{
	    linkId: 'CompleteServiceConnections_ucPlots_lsvPlots_ctrl0_ucDateMeterInstalled_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl0_ucDateMeterInstalled_iSelect_dd',
	    selectMthId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl0_ucDateMeterInstalled_iSelect_mm',
	    selectYrId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl0_ucDateMeterInstalled_iSelect_yyyy'
	});

    $('#CompleteServiceConnections_ucPlots_lsvPlots_ctrl1_ucDateMeterInstalled_dateSelectorDiv').dateHelper(
	{
	    linkId: 'CompleteServiceConnections_ucPlots_lsvPlots_ctrl1_ucDateMeterInstalled_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl1_ucDateMeterInstalled_iSelect_dd',
	    selectMthId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl1_ucDateMeterInstalled_iSelect_mm',
	    selectYrId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl1_ucDateMeterInstalled_iSelect_yyyy'
	});

    $('#CompleteServiceConnections_ucPlots_lsvPlots_ctrl2_ucDateMeterInstalled_dateSelectorDiv').dateHelper(
	{
	    linkId: 'CompleteServiceConnections_ucPlots_lsvPlots_ctrl2_ucDateMeterInstalled_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl2_ucDateMeterInstalled_iSelect_dd',
	    selectMthId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl2_ucDateMeterInstalled_iSelect_mm',
	    selectYrId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl2_ucDateMeterInstalled_iSelect_yyyy'
	});

    $('#CompleteServiceConnections_ucPlots_lsvPlots_ctrl3_ucDateMeterInstalled_dateSelectorDiv').dateHelper(
	{
	    linkId: 'CompleteServiceConnections_ucPlots_lsvPlots_ctrl3_ucDateMeterInstalled_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl3_ucDateMeterInstalled_iSelect_dd',
	    selectMthId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl3_ucDateMeterInstalled_iSelect_mm',
	    selectYrId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl3_ucDateMeterInstalled_iSelect_yyyy'
	});

    $('#CompleteServiceConnections_ucPlots_lsvPlots_ctrl4_ucDateMeterInstalled_dateSelectorDiv').dateHelper(
	{
	    linkId: 'CompleteServiceConnections_ucPlots_lsvPlots_ctrl4_ucDateMeterInstalled_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl4_ucDateMeterInstalled_iSelect_dd',
	    selectMthId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl4_ucDateMeterInstalled_iSelect_mm',
	    selectYrId: '#CompleteServiceConnections_ucPlots_lsvPlots_ctrl4_ucDateMeterInstalled_iSelect_yyyy'
	});

    //developers - join the approved plumber/contractor scheme
    $('#ClassroomBooking_ctl00_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ClassroomBooking_ctl00_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ClassroomBooking_ctl00_ucDate_iSelect_dd',
	    selectMthId: '#ClassroomBooking_ctl00_ucDate_iSelect_mm',
	    selectYrId: '#ClassroomBooking_ctl00_ucDate_iSelect_yyyy'
	});


    //test code - TODO remove
    $('#ctl00_ctl00_iPHContent_Content_ucReading_ucAddMeterReadings_lvReadings_ctrl0_ucReading_ucDate_dateSelectorDiv').dateHelper(
	{
	    linkId: 'ctl00_ctl00_iPHContent_Content_ucReading_ucAddMeterReadings_lvReadings_ctrl0_ucReading_ucDate_dateSelectorDivDatehelper',  // dynamic a tag, can be any unique id
	    selectDayId: '#ctl00_ctl00_iPHContent_Content_ucReading_ucAddMeterReadings_lvReadings_ctrl0_ucReading_ucDate_iSelect_dd',
	    selectMthId: '#ctl00_ctl00_iPHContent_Content_ucReading_ucAddMeterReadings_lvReadings_ctrl0_ucReading_ucDate_iSelect_mm',
	    selectYrId: '#ctl00_ctl00_iPHContent_Content_ucReading_ucAddMeterReadings_lvReadings_ctrl0_ucReading_ucDate_iSelect_yyyy'
	});
    //add remove
    $(".hazards").addRemove("hazard", "hazard");
    $(".plots").addRemove("plot", "plot");
    $(".meter-readings").addRemove("meter reading", "meter-reading");
    $(".children").addRemove("child", "child");
    $(".buildPeriods").addRemove("build period", "buildPeriod");
    //radio tabs
    $(".frmConditions").radioTabs();
    //show/hide option
    $(".frmOptionCheck").setOptions();
    //form panels
    $(".form").accordian();
    //show/hide option
    $(".requiredoptions").setOptions();
    // Modal popup - Only use when a postback happens. Need something different for AJAX calls
    $(".longRunning").modalPopup();
    // Apply For WaterCare Bills To Nominee options
    $(".billsToNominee").billsToNominee();
});

// Function that displays / hides options when a checkbox is clicked
$.fn.setOptions = function() {
    // Loop through each element looking for checkboxes
    $(this).each(function(optionIndex) {
        // check if the element is a checkbox
        $(this).find(":checkbox").each(function(optionIndex) {
	        if ($(this).is(":checked")){
		        $(this).parents('.frmOption').find('.subOption').removeClass("js-hide")
	        }
	        else{
		        $(this).parents('.frmOption').find('.subOption').addClass("js-hide")
	        }
           
            // Hook into the click event for each checkbox
            $(this).click(function() {
                $(this).parents('.frmOption').find('.subOption').toggleClass("js-hide");
            })
        })
    })
}


/**
jQuery extension - replace input box text when on focus
*/
$.fn.replaceText = function()
{
	var text = $(this).val()
	$(this).focus(function(){
		$(this).addClass("focused");
		$(this).removeClass("active");
		if($(this).val() == text) $(this).val("");
	});
	$(this).blur(function(){
		$(this).removeClass("focused");
		// removed so js does not repopulate input box
		/*if($(this).val() == "") {
			$(this).val(text);
		} else {
			$(this).addClass("active");
		};*/
	});
}


/**
jQuery extension - tabbing for news categories
*/
$.fn.newsTabs = function()
{

 var parent = $(this);
$(this).addClass('js-tabs');
$(this).find('.nav-jump').append('<span class="tl"></span><span class="tr"></span>');
$(this).find('.nav li:eq(0)').addClass('here');
$(this).find('.panel:gt(0)').hide();
$(this).find('.nav a').click(function (){
	parent.find(".nav li").removeClass('here');
	$(this).parent('li').addClass('here');
	parent.find(".panel").hide();
	$($(this).attr('href')).show();
	return false;
});
}

/**
jQuery extension - hide show of help panels
*/
$.fn.helpToggle = function(arg)
{
    //$(this).prev(".information").addClass("js-hide");
    $(this).click(function(e)
    {
	    
        e.preventDefault();
        //$(this).prev(".information").toggleClass("js-hide");
        $(this).prev(".information").toggle();
    })
}

/**
jQuery extension - add remove of sections
*/
$.fn.addRemove = function(arg, arg2) {
    var stringName = arg;
    var className = arg2;
    var topLevel = $(this);

    $(this).append("<div class='add'><a href='#'>Add another " + stringName + "</a></div>");

    var elemLength = $("." + className).length;
    var defaultElementOpen = 1;
    if (className == 'child') {
        var elemOpen = 3;
        defaultElementOpen = 3;
    }
    else {
        var elemOpen = 1;
    }

    // Find each child element (will be inside the ItemLayout of the ListView)
    $("." + className).each(function(i) {
        // Add the remove element link, need to render this to get the top of the element looking correct
        $(this).prepend("<div class='remove'></div>");
        if (i >= defaultElementOpen) {
            // Insert the Remove link
            $(this).find("div.remove:first").append("<a href='#'>Remove " + stringName + "</a>");
        }
        // Get the hidden field
        var hiddenfield = $(this).find(":input[type='hidden']");
        if (hiddenfield.attr("value") == '1') {
            if (!$(this).hasClass("postBack")) {
                if (i >= elemOpen) {
                    hiddenfield.attr("value", "0");
                    $(this).addClass("js-hide");
                }
                else {
                    if (i >= defaultElementOpen) {
                        elemOpen += 1;
                    }
                    if (elemOpen == elemLength) {
                        topLevel.find("div.add").addClass("js-hide");
                    }
                }
            }
            else {
                // Page postback, just load don't need to change anything yet
            }
        }
        else {
            $(this).addClass("js-hide");
        }
    })

    topLevel.find("div.add a").click(function(e) {
        e.preventDefault();
        elemOpen += 1;
        // Find the first hidden element and show it again
        var elementToShow = topLevel.find("fieldset." + className + ".js-hide:first");
        elementToShow.removeClass("js-hide");
        elementToShow.find(":input[type='hidden']:first").attr("value", "1");
        // Check if we have to hide the add now        
        if (elemOpen == elemLength) {
            topLevel.find("div.add").addClass("js-hide");
        }
    });

    $("." + className).find("div.remove>a").click(function(e) {
        e.preventDefault();
        elemOpen += -1;
        var fieldsetParent = $(this).parents("fieldset." + className + ":first");

        // Hide the element and clear it out
        fieldsetParent.addClass("js-hide");
        fieldsetParent.find(".frmElement input").attr("value", "");
        //reset date selectors to todays date on removal
        var today = new Date();
        fieldsetParent.find("select.dd").attr("value", today.getDate());
        fieldsetParent.find("select.mm").attr("value", today.getMonth() + 1);
        fieldsetParent.find("select.yyyy").attr("value", today.getFullYear());
        // Set the hiddenfield value to 0
        fieldsetParent.find(":input[type='hidden']").val("0");
        // If the error class was set remove it
        fieldsetParent.find(".error").removeClass("error");
        fieldsetParent.find(".inline.errorMessage").remove();

        if (elemOpen < elemLength) {
            // Show that we can add a new element
            topLevel.find("div.add").removeClass("js-hide");
        }
    });
}

/**
jQuery extension - adds rounded corners to elements

@param [noIe] boolean selector to determine whether to set corners on ie6
@param [bottomOnly] boolean selector to only add the bottom two corners
*/
$.fn.roundedCorners = function(/**Bool*/noIe, /**int*/corners)
{
    var ie6 = $.browser.msie && $.browser.version < 7;
    if (ie6 && noIe)
    {
        $(this).each(function()
        {
            var pb, pt, pl, pr;
            ($(this).css("padding-bottom") == "auto") ? pb = "1px" : pb = (parseInt(pb) + 1) + "px";
            ($(this).css("padding-top") == "auto") ? pt = "1px" : pt = (parseInt(pt) + 1) + "px";
            ($(this).css("padding-left") == "auto") ? pl = "1px" : pl = (parseInt(pl) + 1) + "px";
            ($(this).css("padding-right") == "auto") ? pr = "1px" : pr = (parseInt(pr) + 1) + "px";

            var padding = "padding: " + pt + " " + pr + " " + pb + " " + pl + ";";

            if (corners == 1)
            {
                $(this).prepend('<span class="tl"></span>');
            }
            else if (corners == 'blbr')
            {
                $(this).css("padding", "0");
                $(this).wrapInner('<div class="iebr" style="float:left;z-index:10;position:relative;margin:0 -1px -1px 0"><div class="iebl" style="float:left;padding:3px 10px 6px 7px;margin:0 0 0 -1px;z-index:9;position:relative;"></div></div>');
            }
            else if (corners == 'tltr')
            {
                $(this).css("padding", "0 0 0 1px");
                $(this).wrapInner('<div class="tl" style="margin:-1px 0 0 -1px;z-index:10;border:none;padding:0;float:left;width:auto;height:auto;top:0;left:0;background-position:0 0;"><div class="tr" style="margin:0 -1px 0 0;padding:0;border:none;float:left;width:auto;height:auto;top:0;left:0;background-position:100% 0;"></div></div>');
            }
            else if (corners == 3)
            {
                $(this).css("padding", "0 0 0 1px");
                $(this).wrapInner('<div class="tl" style="margin:-1px 0 0 -1px;z-index:10;border:none;padding:0;float:left;width:auto;height:auto;top:0;left:0;background-position:0 0;"><div class="tr" style="margin:0 -1px 0 0;padding:0;border:none;float:left;width:auto;height:auto;top:0;left:0;background-position:100% 0;"><div class="br" style="margin:0 0 -1px 0;z-index:10;border:none;padding:10px 6px 10px 9px;float:left;width:136px;height:auto;top:0;left:0;background-position:100% 100%;"></div></div></div>');
            }
            else
            {
                $(this).wrapInner("<div class='ietr' style='zoom:1;z-index:999;position:relative;margin-right:-1px;margin-top:-1px;'><div class='ietl' style='zoom:1;z-index:999;position:relative;margin-left:-1px;'><div class='iebr' style='zoom:1;z-index:999;position:relative;margin-bottom:-1px;'><div class='iebl' style='z-index:998;position:relative;zoom:1;height:1%;" + padding + "'></div></div></div></div>");
                $(this).css({ "padding-top": "0",
                    "padding-bottom": "0",
                    "padding-left": "0",
                    "padding-right": "0"
                });
            }
        })

    }
    if (!ie6)
    {
        if (corners == 'blbr')
        {
            $(this).append('<span class="bl"></span><span class="br"></span>');
        }
        else if (corners == 'tltr')
        {
            $(this).append('<span class="tl"></span><span class="tr"></span>');
        }
        else if (corners == 'tlbl')
        {
            $(this).append('<span class="tl"></span><span class="bl"></span>');
        }
        else if (corners == 3)
        {
            $(this).append('<span class="tl"></span><span class="tr"></span><span class="br"></span>');
        }
        else if (corners == 1)
        {
            $(this).prepend('<span class="tl"></span>');
        }
        else
        {
            $(this).append('<span class="tl"></span><span class="tr"></span><span class="bl"></span><span class="br"></span>');
        }

    }
}




/**
jQuery extension - tabbing with radio buttons
*/
$.fn.radioTabs = function()
{
	$(".frmConOption").addClass("tabOption js-hide");
	var siblingChecked;
    $(this).each(function(i){
        $(this).find(":radio,:checkbox").each(function(i)
        {

            //check if option is shown
            ($(this).is(":checked")) ? $(this).parents("li").addClass("selected") : $(this).parents("li").removeClass("selected");

            if($(this).is(":checked")){
                $(this).parents(".frmConditional").find(".tabOption").each(function(i){
                    if($(this).hasClass(".opt" + i)){
                        $(this).removeClass("js-hide");
                    }
                })
            }
            
            
            if ($(this).is(":checked")){
		        $(this).parents('.frmConditions').siblings(".tabOption").each(function()
                {
                    ($(this).hasClass(".opt" + i)) ? $(this).removeClass("js-hide") : $(this).addClass("js-hide");
                    if ($(this).hasClass(".opt" + i)){
	                    $(this).parents(".frmConditional").children('.frmConditions').find(".frmGroupPair li").addClass("selected");
                    }
                })
                
                $(this).parents(".frmConditional").find(":radio,:checkbox").each(function(){
	                if (!$(this).is(":checked")){
		                $(this).parents("li").removeClass("selected");
		               
                		if ($(this).is(":checkbox")){
	                		$(this).parents('.frmConditions').siblings(".tabOption").each(function()
                			{
	                			$(this).addClass("js-hide");
                			})
                		}
	                }
                })
	        }
            $(this).click(function()
            {
                $(this).parents('.frmConditions').siblings(".tabOption").each(function()
                {
                    ($(this).hasClass(".opt" + i)) ? $(this).removeClass("js-hide") : $(this).addClass("js-hide");
                    if ($(this).hasClass(".opt" + i)){
	                    $(this).parents(".frmConditional").children('.frmConditions').find(".frmGroupPair li").addClass("selected");
                    }
                })
                
                $(this).parents(".frmConditional").find(":radio,:checkbox").each(function(){
	                if (!$(this).is(":checked")){
		                $(this).parents("li").removeClass("selected");
		               
                		if ($(this).is(":checkbox")){
	                		$(this).parents('.frmConditions').siblings(".tabOption").each(function()
                			{
	                			$(this).addClass("js-hide");
                			})
                		}
	                }
                })
            })
            
            
            
        })
    })
}

/**
jQuery extension - Add accordian functionallity to a group of form items
*/
$.fn.accordian = function()
{
    $(this).find(".frmSection .container:not(:first)").each(function()
    {
        if ($(this).find(".error").length == 0)
        {
            $(this).addClass("js-hide");
        }
    });
		
		$(this).find("h2:not(:first)").each(function()
		{
			if($(this).parents(".frmSection").find(".error").length == 0)
			{
				$(this).addClass("js-closed");
				$(this).wrapInner("<a class='js-toggle js-closed' href='#'></a>");
			}
			else
			{
				$(this).wrapInner("<a class='js-toggle' href='#'></a>");
			}
		});
		
		$(this).find("h2:first").wrapInner("<a class='js-toggle' href='#'></a>");
		
    /*$(this).find("h2:not(:first)").wrapInner("<a class='js-toggle js-closed' href='#'></a>");*/
		
		
    $(this).find(".footer").html("<a class='js-toggle-next' href='#'>open next section</a>");

    $(".js-toggle").click(function(e)
    {
        e.preventDefault();
        if ($(this).parents(".frmSection").find(".container").hasClass("js-hide") == true)
        {
            $(this).parents(".frmSection").find(".container").removeClass("js-hide");
						$(this).removeClass("js-closed").parent('h2').removeClass("js-closed");
            var pos = $(this).parents(".frmSection").position();
            $('html, body').animate({ scrollTop: pos.top + 50 }, 500);
        }
        else
        {
            $(this).parents(".frmSection").find(".container").addClass("js-hide");
						$(this).addClass("js-closed").parent('h2').addClass("js-closed");
        }

    });

    $(".js-toggle-next").click(function(e)
    {
        e.preventDefault();
        $(this).parents(".frmSection").next().find(".container").removeClass("js-hide");
        var pos = $(this).parents(".frmSection").next().position();
        $('html, body').animate({ scrollTop: pos.top + 50 }, 500);
    })
}

/**
jQuery extension - Add hide show functionallity to a data list of q and a's
*/
$.fn.toggleList = function()
{
    $(this).find("dd").addClass("js-hide").end()
    .find("dt").addClass("js-closed").wrapInner("<a href='#'></a>")
    .find("a").click(function(e)
    {
        e.preventDefault();
        $(this).parent("dt").toggleClass("js-closed").next("dd").toggleClass("js-hide");
    });
}


/**
jQuery extension - resize overlay in ie6 to make it span whole screen
*/
$.fn.overlay = function()
{

	var width = $(window).width();
	var xpos = 0 - $(this).x();
	var ypos = 0 -$(this).y();
	var height = $(window).height();
	var pageHeight = document.body.clientHeight;  
	if (pageHeight > height) {  
		height = pageHeight;  
		}  
	$("#uploadProgress").css({width: width, height: height, top:ypos, left:xpos}); 
	
	$('#iBody').find(".tl,.tr,.bl,.br").each(function(){
		$(this).css("z-index","-1");
	})
	$('#iPage').css("z-index","-1");
}



$.fn.x = function(n) {
	var result = null;
	this.each(function() {
	var o = this;
	if (n === undefined) {
	var x = 0;
	if (o.offsetParent) {
	while (o.offsetParent) {
	x += o.offsetLeft;
	o = o.offsetParent;
	}
	}
	if (result === null) {
	result = x;
	} else {
	result = Math.min(result, x);
	}
	} else {
	o.style.left = n + 'px';
	}
	});
	return result;
};

$.fn.y = function(n) {
	var result = null;
	this.each(function() {
	var o = this;
	if (n === undefined) {
	var y = 0;
	if (o.offsetParent) {
	while (o.offsetParent) {
	y += o.offsetTop;
	o = o.offsetParent;
	}
	}
	if (result === null) {
	result = y;
	} else {
	result = Math.min(result, y);
	}
	} else {
	o.style.top = n + 'px';
	}
	});
	return result;
};


/**
jQuery extension - Create a large clickable area from a hyperlink, when clicked the hyperlink click event is invoked.
	
@param [selector] An optional jQuery selector string pointing the link you wish to 'make big', if ommitted it will use the first hyperlink within the element
*/
$.fn.bigLink = function(/**String*/selector)
{
    //var link = selector ? $(selector) : this.find('a[@href]:eq(0)');

    this.click(function()
    {
	    var link = $(this).find('a[@href]:eq(0)');
        // the first link within the supplied element
        var prevent_default = false;

        // check through all click events to for preventDefault command
        var all_events = link.data('events');
        var click_events;

        if (all_events && all_events.click)
        {
            click_events = all_events.click;

            for (var k in click_events)
            {
                if (/\.preventDefault\(\)/.test(click_events[k].toString()))
                {
                    prevent_default = true;
                }
            }
        }

        // trigger all jquery assigned events
        link.triggerHandler('click');

        // if preventDefault has never been assigned to this link then it is safe to do a standard redirect
        if (!prevent_default)
        {
            document.location = link.attr('href');
        }
    })
	.css('cursor', 'pointer');
}

/**
jQuery plugin extension which adds inline date selection
*/
jQuery.fn.dateHelper = function(attr) {
    if (this.length == 0) return this;

    var containingId = "#" + this.attr("id");

    //add link
    $(containingId + " .frmElement:last").after('<a id="' + attr.linkId + '" href="#" class="dp-applied dateSelector">Select date</a>');

    $('#' + attr.linkId).click(function(e) { e.preventDefault(); });

    //get the start end dates	
    var endDd = $(attr.selectDayId + ' option:last').val();
    var endMm = $(attr.selectMthId + ' option:last').val();
    var endYyyy = $(attr.selectYrId + ' option:last').val();
    var endDate = endDd + "/" + endMm + "/" + endYyyy;
    var startDd = $(attr.selectDayId + ' option:first').val();
    if (startDd.length === 1) startDd = "0" + startDd;
    var startMm = $(attr.selectMthId + ' option:first').val();
    if (startMm.length === 1) startMm = "0" + startMm;
    var startYyyy = $(attr.selectYrId + ' option:first').val();
    var startDate = startDd + "/" + startMm + "/" + startYyyy;

    $('#' + attr.linkId)
		.datePicker(
    // associate the link with a date picker
			{
			createButton: false,
			startDate: startDate,
			endDate: endDate


}
		).bind(
    // when the link is clicked display the date picker
			'click',
			function() {
			    //updateSelects($(this).dpGetSelected()[0]);
			    updateSelects($(attr.selectMthId).val() + '/' + $(attr.selectDayId).val() + '/' + $(attr.selectYrId).val());
			    $(this).dpDisplay();
			    return false;
			}
		).bind(
    // when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state) {
			    updateSelects(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected) {
			    //updateSelects(selected[0]);
			}
		);

    var updateSelects = function(selectedDate) {
        var count = 0;
        selectedDate = new Date(selectedDate);
        var todayDay = today.getDate();
        var todayMonth = today.getMonth();
        var todayYear = today.getFullYear();
        var d = selectedDate.getDate();
        var m = selectedDate.getMonth();
        var y = selectedDate.getFullYear();
        var dateValid;
		
        if ($(attr.selectYrId).hasClass("preventRetro")) {
            var errorMsg = "<div style='clear:left;padding:10px 0;' class='inline errorMessage'><strong>The selected date cannot be in the past</strong></div>";
            if (d < todayDay) {
                if (m <= todayMonth) {
                    if (y <= todayYear) {
                        $(containingId).addClass("error").append(errorMsg);
                    }
                    else {
                        dateValid = true;
                    }
                }
                else {
                    dateValid = true;
                }
            }
            else {
				if (m >= todayMonth){
					if(y >= todayYear){
						dateValid= true;
					}
					else{
						$(containingId).addClass("error").append(errorMsg);
					}
				}
				else{
					if (y <= todayYear){
						$(containingId).addClass("error").append(errorMsg);
					}
					else{
						dateValid = true;
					}
				}
        	}
           
        }

        if ($(attr.selectYrId).hasClass("preventFuture")) {
            var errorMsg = "<div style='clear:left;padding:10px 0;' class='inline errorMessage'><strong>The selected date cannot be in the future</strong></div>";
            if (d > todayDay) {
                if (m >= todayMonth) {
                    if (y >= todayYear) {
                        $(containingId).addClass("error").append(errorMsg);
                    }
                    else {
                        dateValid = true;
                    }
                }
                else {
                    dateValid = true;
                }
            }
            else {
				if (m <= todayMonth){
					if(y <= todayYear){
						dateValid= true;
					}
					else{
						$(containingId).addClass("error").append(errorMsg);
					}
				}
				else{
					if (y >= todayYear){
						$(containingId).addClass("error").append(errorMsg);
					}
					else{
						dateValid = true;
					}
				}
        	}
        }
        
        if (dateValid == true) {
            $(containingId).removeClass("error")
            if ($(containingId).find(".errorMessage")) {
                $(containingId).find(".errorMessage").remove();
            }
            ($(attr.selectDayId)[0]).selectedIndex = d - 1;
            ($(attr.selectMthId)[0]).selectedIndex = m;
            ($(attr.selectYrId)[0]).selectedIndex = y - startYyyy;
        }

        $(attr.selectDayId + ", " + attr.selectMthId + ", " + attr.selectYrId).trigger("change");
    }
    // listen for when the selects are changed and update the picker
    $(attr.selectDayId + ", " + attr.selectMthId + ", " + attr.selectYrId)
		.bind(
			'change',
			function() {
			    var d = new Date(
							$(attr.selectYrId).val(),
							$(attr.selectMthId).val() - 1,
							$(attr.selectDayId).val()
						);
			    $('#' + attr.linkId).dpSetSelected(d.asString());
			}
		);

    // default the position of the selects to today

    if (!$(this).children(".frmElementGroup").hasClass("noDefault")) {
        var today = new Date();

        ($(attr.selectDayId)[0]).selectedIndex = today.getDate() - 1;
        ($(attr.selectMthId)[0]).selectedIndex = today.getMonth();
        ($(attr.selectYrId)[0]).selectedIndex = today.getFullYear() - 2005;


        // and update the datePicker to reflect it...
        $(attr.selectDayId + ", " + attr.selectMthId + ", " + attr.selectYrId).trigger("change")
        $(attr.selectDayId).trigger('change');
    }
}


/**
jQuery extension - ready reckoner
*/
$.fn.toggleResult = function()
{
    var help = $(this).find("div.information");
		var result = $(this).find("div.result").addClass("js-hide");
    $(this).find(":submit").click(function()
    {
			help.hide();
			isOpen = result.hasClass("js-open");
		 	if (isOpen){result.fadeOut();result.toggleClass("js-open");}
      result.fadeIn();
			result.toggleClass("js-open");
      return false;
    })
}

// Only use when a postback happens. Need something different for AJAX calls
$.fn.modalPopup = function() {
$(this).find(":input[type='submit']").each(function() {
    $(this).click(function() {
        $("#uploadProgress").removeClass("js-hide");
    });
    });
}

// Enables or disables the Nominee section based on the status of the 2 checkboxes
$.fn.billsToNominee = function() {
    var checkbox = $(this).find(":input[type='checkbox']:first");
    var section = $(".frmSection.billsToNomineeSection.frmConOption.opt0");
    section.find("h2>a:first").html("Nominee");
    section.find("div.nomineeHelp").addClass("js-hide");
    if (checkbox.is(":checked")) { section.removeClass("js-hide"); }
    checkbox.click(function() {
        var Option = "opt0";
        // Need to know which checkbox was clicked as there are 2 that affect the section
        if ($(this).parent().hasClass("opt0")) { Option = "opt1"; }
        var OtherChecked = $(".billsToNominee." + Option).find(":input[type='checkbox']:first").is(":checked");
        $(this).is(":checked") || OtherChecked ? section.removeClass("js-hide") : section.addClass("js-hide");
    })
}
