JSU = {

error_messages : {},
neinputs_count : 4,
pdetails_loaded : {},
orders_loaded : false,
account_type : '',
order_id_offset : 0,

add_neinput : function() {
	if(this.neinputs_count < 1) this.neinputs_count = 1;
	this.neinputs_count = this.neinputs_count + 1;
	$('#add_more_tr').before('<tr>'+$('#ne_tr_0').html().replace(/\[0\]/g, '['+(this.neinputs_count - 1)+']')+'</tr>');	
},

check_reg : function() {
	var _ermsg = [];
	if(!($('#ts_email_input').val().match(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/))) _ermsg.push(JSU.error_messages['email_invalid']);
	if(!($('#ts_pwd_input').val().match(/^\w+$/))) _ermsg.push(JSU.error_messages['password_invalid']);
	if($('#ts_pwd_input').val() != $('#pwd_repeat_input').val()) _ermsg.push(JSU.error_messages['password_wrong_repeat']);
	if($('#ts_name_input').val().length < 1) _ermsg.push(JSU.error_messages['name_required']);
	//if($('#ts_company_name_input').val().length < 1) _ermsg.push(JSU.error_messages['company_name_required']);
	if(_ermsg.length < 1) {
		$.post('/_aj', {act : 'check_reg_e', em : $('#ts_email_input').val()}, function(d) {
			if(d.success) {
				document.reg_form.submit();
			} else {
				_ermsg.push(JSU.error_messages['email_exists']);
				JSU.reg_error(_ermsg);
			}			
		}, 'json');				
		return;
	}
	this.reg_error(_ermsg);	
},

reg_error : function(_ermsg) {
	$('#ermsg').html(_ermsg.join('<br />'));
	$('#ermsg').show();
	$.scrollTo($('#ermsg'));		
},

remind_password : function() {
$('#rpbutton').attr('disabled', true);
$('#rp2_div').hide();
$('#rp1_div').show();
$.post('/_aj', {act : 'remind_password', urp : $('#urp_input').val()}, function() {
	$('#rp1_div').hide();
	$('#rp2_div').show();
	$('#rpbutton').attr('disabled', false);
	}, 'json');
},

remove_partner : function(tp, c) {
	$('#partners_'+tp+'_div_'+c).remove();
	$('#partners_'+tp+'_tr_'+c).remove();
	alert('When done click SAVE button to finalize changes');
	return false;
},

pdetails : function(uid, sfx, nr) {
	if(!sfx) sfx = '';
	if(!nr) nr = 0;
	$('#pdetails_div_'+uid+sfx).toggle();
	if(!this.pdetails_loaded[uid+sfx]) {
		$('#pdetails_div_'+uid+sfx).html('<img src="/images/wait.gif" border="0" />');
		$.post('/_aj', {act : 'det', uid : uid, nr : nr}, function(d) {
			if(d.data) {
				$('#pdetails_div_'+uid+sfx).html(d.data);							
				JSU.pdetails_loaded[uid+sfx] = true;
			}
		}, 'json');		
	}
	return false;	
},

check_order : function() {
	var _ermsg = [];
	if($('#ts_email_input').length > 0 && !($('#ts_email_input').val().match(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/))) _ermsg.push(JSU.error_messages['email_invalid']);
	if($('#ts_pwd_input').length > 0 && !($('#ts_pwd_input').val().match(/^\w+$/))) _ermsg.push(JSU.error_messages['password_invalid']);
	if($('#ts_pwd_input').length > 0 && $('#ts_pwd_input').val() != $('#pwd_repeat_input').val()) _ermsg.push(JSU.error_messages['password_wrong_repeat']);
	
	var _reqs = ['name', 'company_name', 'company_address', 'city', 'state', 'zip', 'phone_number', 'property_address', 'property_city', 'property_county', 'property_state', 'property_zip', 'property_type', 'property_occupancy_status', 'property_loan_type', 'property_loan_purpose', 'property_number_of_units', 'misc_borrower_name', 'misc_borrower_phone'];
	
	$.each(_reqs, function() {
		if($('#ts_'+this+'_input').val().length < 1) _ermsg.push(JSU.error_messages[this+'_required']);
	});
	
	if(_ermsg.length < 1) {
		if($('#ts_email_input').length > 0) {
		$.post('/_aj', {act : 'check_reg_e', em : $('#ts_email_input').val()}, function(d) {
			if(d.success) {
				document.reg_form.submit();
			} else {
				_ermsg.push(JSU.error_messages['email_exists']);
				JSU.reg_error(_ermsg);
			}			
		}, 'json');
		} else {
			document.reg_form.submit();
		}
		return;
	}
	this.reg_error(_ermsg);	
},

order_use_same : function() {
	var _ar = ['name', 'phone', 'alt_phone', 'email'];
	if($('#_use_same_checkbox').attr('checked')) {
		$('#ts_misc_use_same_information_as_borrower_input').val('1');
		$.each(_ar, function() {$('#ts_misc_contact_'+this+'_input').attr('disabled', true);});
	} else {
		$('#ts_misc_use_same_information_as_borrower_input').val('0');
		$.each(_ar, function() {$('#ts_misc_contact_'+this+'_input').attr('disabled', false);});
	}
},

show_orders : function() {
	$('#orders_div').toggle();
	if(this.orders_loaded) return;
	$('#orders_wait_div').show();
	$.post('/_aj', {act : 'show_orders'}, function(d) {
		JSU.orders_loaded = true;
		if(!d.success) {
			$('#orders_content_div').html(JSU.error_messages[JSU.account_type+'_no_orders']);
		} else {
			$('#orders_content_div').html('<table border="1" cellpadding="2" cellspacing="0" id="orders_table" class="tablesorter"><thead><tr><th>Property</th><th>Added</th><th>status <br />changed</th><th>status</th><th>fee</th></tr></thead><tbody id="orders_table_body"></tbody></table>');
			var _html = [];
			$.each(d.data, function() {				
				if(this.current_status == 'Ordered') {
					var _status = 'Pending';
				} else if(this.current_status == 'Assigned') {
					var _status = 'Processing';
				} else {
					var _status = this.current_status;
				}				
				var _h = '<tr><td><a href="/showOrder.php?id='+(parseInt(this.id)+JSU.order_id_offset)+'" target="_blank">'+this.property_address+', '+this.property_city+', '+this.property_state+'</a></td><td>'+this.date_added+'</td><td>'+this.current_status_date+'</td><td><div style="text-align:center;"><strong>'+_status+'</strong></div>';
				if(this.partner_id) {
					_h += '<br />'+this.partner_type+' <a href="#" onclick="javascript:return JSU.pdetails('+this.partner_id+',';
					_h += "'_"+this.id+"'";
					_h += ', 1);">'+this.partner_name+'</a> <a href="mailto:'+this.partner_email+'">'+this.partner_email+'</a>';
					_h += '<div id="pdetails_div_'+this.partner_id+'_'+this.id+'" style="display:none;text-align:left;border:1px solid #DED;padding:3px;"></div>';
				}				
				_h += '</td><td>';
				if(this._show_payment) {
					_h += '<b>$'+this.appraisal_fee+'</b> '+this.amo_fee+'<br />'+this.payment_status;
				} else {
					_h += '<div style="display:none;">0</div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				}
				_h += '</td></tr>';
				_html.push(_h);
			});
			$('#orders_table_body').append(_html.join(''));
			$('#orders_table').tablesorter();
		}
		$('#orders_wait_div').hide();
		$('#orders_content_div').show();
	}, 'json');
}

}