var window_obj, cal_open;
function ccmbDisplayCalendar(){
	popupWindow('index.php?page=popup_calendar&layout=cal&show_header=n&dtformat=mm/dd/yyyy&formname=quickform&fieldname=date_mortgage_required','winCalendar','toolbar=no,menu=no,resizable=yes,width=250,height=260');

	var cal_open = setInterval(function() {
	try {
		if (window_obj.closed) {
		 clearTimeout(cal_open );
		 $('#date_mortgage_required_blockui').val($('#date_mortgage_required').val()); 
		}
	} catch(e){};
 }, 200);

}

$().ready(function() {
  var currentpage='home';
	if (window.location.search){
		var rx = /page=([^&#]+)/
		if (match = rx.exec(window.location.search)){
				currentpage=match[1];
		}
	}

	//track submit form
	$('#step2_submit').click(function(){
		var val;
		val =$('#mortgage_yesno').val();
		if (! /^(Yes|No)$/.test(val)){
			alert('Please specify if you currently have a mortgage.');
			$('#mortgage_yesno').focus();
			return false;
		}
		
		if (val=='Yes'){
			val =$('#current_mortgage_institution').val();
			if (val=='Please Select'){
				alert('Please select the institution your mortgage is with.');
				$('#current_mortgage_institution').focus();
				return false;
			}
		}
		
		//validate date
		val = $('#date_mortgage_required_blockui').val();
		var failed=(val=='');
		if (failed){
		   alert('Please enter the date when the mortgage is required.');
		   $('#date_mortgage_required_blockui').focus();
		   return false;
		 }
		 
		if (!/^\d{1,2}[\/-]\d{1,2}[\/-]\d{4}$/.test(val) || /Invalid|NaN/.test(new Date(val))){
			failed=true;
		}
		if (!failed){			
			//validate that the first number is 1-12, second is 1-31
			var x = val.split(/[\/-]/); for (i=0;i<=2;i++) {x[i]=parseInt(x[i].replace(/^0+/,''))};
			var y = new Date(val);
			if (y.getDate()!=x[1] || (y.getMonth()+1)!=x[0] || y.getFullYear()!=x[2]) failed=true;

		}
		if (failed){
		   alert('The date you entered is invalid. Please enter the date as MM/DD/YYYY.');
		   $('#date_mortgage_required_blockui').focus();
		   return false;
		}								
		
		
		//validate amount
		val = $('#mortgage_amount').val();
		val = jQuery.trim(val);
		if (val==''){
			alert('Please enter the approximate mortgage amount required.');
			 $('#mortgage_amount').focus();
			return false;
		}
		if (/[^0-9\s\.\,]/.test(val)){
			alert('Please enter only numbers in the Approximate Mortgage Amount Required field.');
			$('#mortgage_amount').focus();
			return false;
		}
		$('form[name="quickform"]').submit();
	});
	$('#step2_cancel').click(function(){	
		$('#udm').css('visibility','visible');
		$.unblockUI();
	});

	$('#mortgage_amount').change(function(){
		var val = $(this).val();
		val = jQuery.trim(val);
		$(this).val(val);
		$('#h_mortgage_amount').val(val);
	});
	
	$('#mortgage_yesno').change(function(){
		var val = $(this).val();
		if (val=='Yes'){
			$('#mortgage_institution_row').removeClass('_disabled');
			$('#mortgage_institution_row').find('td').removeClass('_disabled');
			$('#current_mortgage_institution').attr('disabled','');
		} else {
			$('#mortgage_institution_row').addClass('_disabled');
			$('#mortgage_institution_row').find('td').addClass('_disabled');
			$('#current_mortgage_institution').attr('disabled','disabled');
		}
		if (! /^(Yes|No)$/.test(val)) val='';
		$('#h_mortgage_yesno').val(val);
		if (val!='Yes'){
			$('#h_current_mortgage_institution').val('');
		} else {
			$('#current_mortgage_institution').change();
		}
	});
	
	$('#current_mortgage_institution').change(function(){
		var val = $(this).val();
		var h_val = val=='Please Select'?'N/A':val;
		$('#h_current_mortgage_institution').val(h_val);
	});
	
	$('#date_mortgage_required_blockui').change(function(){
		var val = $(this).val();
		var h_val = val=='Please Select'?'N/A':val;
		$('#date_mortgage_required').val($('#date_mortgage_required_blockui').val());
	});	
	
	$('.submitform_button').click(function(){
		var result = quickformValidations($('form[name="quickform"]').get(0));				
		if (result){
			$('#date_mortgage_required_blockui').val($('#date_mortgage_required').val());
			$('#mortgage_yesno').change();
			$('#udm').css('visibility','hidden');
			$.blockUI({
				message: $('#step2_div'),
				centerY: false,
				css:{ 'width':'600px', 
					 'padding': '20px',
					 top: '10px',
					 'text-align': 'left','cursor':'default','background-color':'#FEFFE9',
					 'font-family':'Arial,Book Antiqua,Georgia,Times,New Roman,Times',
					 'font-size': '13px',
					 }		
			});
		}
	});
	$('form[name="quickform"]').attr('onsubmit', '');
	$('form[name="quickform"]').bind('submit', function (e){		
		//alert('Form submitted');
		$.get('index.php',{page:'$$logclick',action:'option3'});
		try{
			if ($(this).attr('name')!='MaintForm' && pageTracker!==undefined) {
				pageTracker._trackEvent('Button', 'Option 3 Submit', currentpage);
			}
		}
		catch(err) {}
	});

});

