$(document).ready(function(){ 
		$('.contact').unbind()   // findtenant
			.click(function() { 
				doAjx_fetchcontactform(this)
                return false })
		

		$('.smsbtn').unbind()
			.click(function() { 
//				var tenant=$(this).parents('.fnclass').html()
				if (confirm('Are you sure you want to send an SMS to this tenant')) doAjx_getsmstenantform(this)
                return false })

// for contact form painted by main script, NOT ajax
		$('#mf_submit').unbind()
			.click(function(){
					submitcontacttenant( $(this).parents('form'),'mf_feedback')
					return false
					})

		$('.closebox')
			.click(
				function(){
					$('#contactowner_form').remove()
					if ($.browser.msie ) {
						$('select').show()
					}
				}
			)
		.addClass('clickable')


})

function doAjx_fetchcontactform(obj) {

		//$(obj).href()
		var poststr="href="+$(obj).attr('href')
		$.ajax({
   			type: "POST",
//   			url: "ajq_getmailownerform.php",
   			url: "ajq_getmailtenantform.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('#resultarea').append(jr.respz.html)
				if ($.browser.msie ) {
					$('select').hide()
				}
				$('#mf_submit').unbind()
					.click(function(){
							submitcontacttenant( $(this).parents('form'),'mf_feedback')
							return false
							})

				$('.closebox')
					.click(
						function(){
							$('#contactowner_form').remove()
							if ($.browser.msie ) {
								$('select').show()
							}
						}
					)
				.addClass('clickable')

			window.scrollTo(0,0)


   			}
 		});
}

function submitcontacttenant(formobj,targetdiv) {
 	var poststr=$(formobj).formSerialize()+"&targetdiv=mf_feedback"
 	//TODO does ajq_saveform.php need a table var?
 	if (validateform(formobj)==0) {  //counts no of erroneous fields
 		
		$('#'+targetdiv).html('<img id="spinner" src=rpspinbig.gif>')
		$.ajax({
   			type: "POST",
   			url: "ajq_mailcontacttenant.php",  // which calls mail_lltotenant ($_POST);
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('#'+jr.respz.targetdiv).appendTo(jr.respz.html)
				$('#mf_submit').hide().next('.closebox').children('img').attr('src','buttons/closesend.png')
				
				window.setTimeout('killspinner()',2000)
   			}
 		});
 	}
}

function doAjx_getsmstenantform (obj) {
		$(obj).addClass('sendingsms')
		var poststr="href="+$(obj).attr('href')

		$.ajax({
   			type: "POST",
   			url: "ajq_getsmstenantform.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('#resultarea').append(jr.respz.html)
				$('#mf_submit').unbind()
					.click(function(){
							doAjx_sendsms( $(this).parents('form'),'mf_feedback')
//							submitcontacttenant( $(this).parents('form'),'mf_feedback')
							return false
							})
				$('.closebox').unbind()
					.click(
						function(){
							$('#contactowner_form').remove()
							$('.sendingsms').removeClass('sendingsms')
						}
					)
				.addClass('clickable')

				window.scrollTo(0,0)

				$('#username').bind('keypress', function(event) {
						var code=event.charCode || event.keyCode;
						if(code == 13) {
							$('#submit1').click() 
						}
				})

				var msgcharavail=jr.respz.msgcharavail
				$('#mailtofriend').find('textarea[@name=notetofriend]')
					.bind('keypress', function(event) {
						var nota = $(this).val()
						var tel = nota.length
						$('#cntchars').html(tel)
						if (tel>msgcharavail) {
							$('#cntchars').addClass('errormsg')
						}
						else $('#cntchars').removeClass('errormsg')
					})
				
   			}
 		});

}

function doAjx_sendsms(obj) {

		//$(obj).href()
//		var poststr="href="+$(obj).attr('href')
		var nota = $('#mailtofriend').find('textarea[@name=notetofriend]').val()
		var tel = nota.length
		if (tel>160) {
			alert("SMS may be max 160 characters.\nThis message:"+tel)
			return
		}

		$('#mf_submit').remove()  // the send button

		var poststr=$('#mailtofriend').formSerialize()

		$.ajax({
   			type: "POST",
   			url: "ajq_smsfindtenant.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				if (jr.respz.smsstatus=='ok') {
					alert('SMS sent')
					$('.sendingsms').remove()
				}
				else {
					$('.sendingsms').removeClass('sendingsms')
					alert('SMS could not be sent')
				}

				$('#contactowner_form').remove()	
   			}
 		});
}

function whoviewedmylisting_putform(obj,record_id,xpos,ypos,step) {  
	//called ia from jquery_rentsa DoenAjax_showmaxi

	$(obj).after('<img id="spinner" src=rpspinbig.gif>')
	$('.whoviewedholder').remove()

	var poststr="record_id="+record_id+"&step="+step
		$.ajax({
   			type: "POST",
   			url: "ajq_getwhoviewedform.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('#spinner').remove()
	 			$(obj).after(jr.respz.html)

				$('.navstrips').find('a').unbind()
				 .click(function() { 
						var hrefstring=$(this).attr('href')
						var mx=hrefstring.split('=');
						whoviewedmylisting_putform(obj,record_id,xpos,ypos,mx[1])
						$('.whoviewedholder').remove()
						return false // cancel a-tag
					}) 

				if ($.browser.msie ) {
					$('#chartarea').hide()
					$('#footer').hide()
				}
// 				window.scrollBy(0,200)

				$('#closeboxwhoviewed').click(
					function(){
						$('#whoviewedholder').remove()
						if ($.browser.msie ) {
							$('#chartarea').show()
							$('#footer').show()
						}
				})


				$('.mailtobtn').unbind()
					.click(function() { 
						doAjx_fetchcontactform(this)					
						return false })


				$('.smsbtn').unbind()
					.click(function() { 
		//				var tenant=$(this).parents('.fnclass').html()
						if (confirm('Are you sure you want to send an SMS to this tenant')) doAjx_getsmstenantform(this)
						return false })

				$('.stripeMe').tablesorter({ dateFormat:'yyyy/mm/dd',  headers:{ 1: {sorter: "text"}} })
				.addClass('tablesorter')


				$('#gotologin').unbind().click(function() { 
						$('.whoviewedholder').remove()
						DoenAjaxLoginbox('expanded') 
					})
				.addClass('clickable')


   			}
 		});
}
