jQuery.validator.addMethod("defaultInvalid", function(value, element) {
		return value != '';
	}, "");
var $j=jQuery.noConflict();
var slideSpeed = 500;
var captchaState;																//variable that is set by the ajax captcha call
var doSubmit = false;															//cancels submit until form and captcha are valid

$j(document).ready(function(){
	$j('#previousEmployer').hide();				
	$j('input[name=employment_information-years_at_company]').click(function(){
		$j('#previousEmployer').toggle();
		});
	$j('#caPreviousEmployer').hide();				
	$j('input[name=ca_employment_information-years_at_company]').click(function(){
		$j('#caPreviousEmployer').toggle();
		});
							
	$j('input.phone').mask("(999) 999-9999").blur( function(){					//setup input masks, uses maskedinput plugin
		$j(this).valid();
	});
	$j('input.zipcode').mask("99999").blur( function(){
		$j(this).valid();
	});
	$j('input.social').mask("999-99-9999").blur( function(){
		$j(this).valid();
	});
							
	$j('body').ajaxComplete(function() {										//function that runs after every ajax call
		if( captchaState=="1" ){
			if( $j('#validateForm form').valid() ){
				doSubmit = true;												//at this point the captcha and form are valid, so re-do submit
				$j('#validateForm form').submit();
			}
		}
	});
	
	$j('#validateForm form').validate({											//call to validate plugin
		onfocusout: function(element) {											//validate on blur
   		this.element(element);
  		} 				  
	});										
	$j('#validateForm form').submit(function () {
		if( doSubmit == false ){												//cancel submit and call captcha checker
			captcha();
			return false;
		}
		else																	//all is good, do submit
			return true;
	});
	
	if( $j('#jointOwner0').length > 0 && $j('#jointOwner1').length > 0 && $j('#jointOwner2').length > 0 )
		jointShowHide();														//Setup show/hide for join applications
	if( $j('#sectionD').length > 0 )
		loanShowHide();															//setup show/hide for loan applications

	
	if( $j(".DOB").length > 0 )
		$j(".DOB").datepicker({minDate: '-100y' , maxDate: '-18Y', changeMonth: true, changeYear: true, yearRange: '1900:2100'});	//uses datepicker plugin, date of birth selector, must be 18years
	if( $j(".futureDate").length > 0 )
		$j(".futureDate").datepicker({maxDate: '+1Y', numberOfMonths: [1, 3]});	//uses datepicker plugin, allows for selection of future dates
		
	//when residental addr = mailing addr -> mailing addr section goes away
	$j("#addressSwitch").click( function(){ 
		$j("#address_information-mailing").slideToggle(slideSpeed) });

	$j("#caSwitch").click( function(){ 
		$j("#caAddInfo").slideToggle(slideSpeed) });
});

function jointShowHide(){
	$j('#jointApplicant1, #jointApplicant2').hide();
	$j('#jointOwner0').click( function(){
		jointRequiredClass("jointApplicant1", "remove");
		jointRequiredClass("jointApplicant2", "remove");
		$j('#jointApplicant1, #jointApplicant2').slideUp(slideSpeed);
	});
	$j('#jointOwner1').click( function(){
		jointRequiredClass("jointApplicant1", "add");
		jointRequiredClass("jointApplicant2", "remove");
		$j('#jointApplicant1').slideDown(slideSpeed);
		$j('#jointApplicant2').slideUp(slideSpeed);
	});
	$j('#jointOwner2').click( function(){
		jointRequiredClass("jointApplicant1", "add");
		jointRequiredClass("jointApplicant2", "add");
		$j('#jointApplicant1, #jointApplicant2').slideDown(slideSpeed);
	});
}

function jointRequiredClass(theId, action){
	$j('#' + theId + ' input.jointRequired, #' + theId + ' select.jointRequired').each( function(){
		if( action == 'remove' )
			$j(this).removeClass('required');
		else if( action == "add" && !$j(this).hasClass('required') )
			$j(this).addClass('required');
	});
}

function captcha(){
	var url="/blockbuilder/Captcha/validate/Tal/";
	var captchaID="";
	var captchaCode="";
	//var temp;
	if( $j('#roi-captchaID').length > 0 ){
		captchaID=$j('#roi-captchaID').attr('value');
	}
	if( $j('#roi-captchaCode').length > 0 ){
		captchaCode=$j("#roi-captchaCode").attr('value');
	}
	
	$j.get(url, {"roi-captchaID":captchaID,"roi-captchaCode":captchaCode}, function(isValid){
		if(isValid=="1"){
			$j('#roi-captchaCode').removeClass('error').parent().next().removeClass('error');
			//return "1";
		}
		else{
			$j('#roi-captchaCode').addClass('error').attr('value', '').parent().next().addClass('error');
		}
		captchaState = isValid;
	}, "text");
}

function loanShowHide(){
	$j('#sectionD fieldset').hide();
	$j('#sectionD .radioWlabel').each( function(){
		$j(this).find('input:eq(0)').click( function(){
			if( $j(this).hasClass('selected') ){
				$j(this).removeClass('selected').parent().next().slideUp();
				$j('.open input').removeClass('required');
				$j(this).parent().next().removeClass('open');
			}
			else{
				$j(this).addClass('selected').parent().next().slideDown();
				$j(this).parent().next().addClass('open');
				$j('.open input').not('.radio input').addClass('required');
			}
		});
	});
	$j('#sectionD .radioWlabel').each( function(){
		$j(this).find('input:eq(1)').click( function(){
			if( $j(this).hasClass('selected') ){
				$j(this).removeClass('selected').parent().next().next().slideUp();
				$j('.open input').removeClass('required');
				$j(this).parent().next().next().removeClass('open');
			}
			else{
				$j(this).addClass('selected').parent().next().next().slideDown();
				$j(this).parent().next().next().addClass('open');
				$j('.open input').not('.radio input').addClass('required');
			}
		});
	});
}
