$(document).ready(function(){
	$('#joinmailbutton, #sendjoin').unbind()
		.click(function(evt){ 
					$('#joinmaildiv').remove()
					getjoinmailform(this,evt.pageX,evt.pageY)
						})
					.addClass('clickable') 
})


function getjoinmailform(obj,xpos,ypos) {
	var poststr="record_id="+$(obj).attr('record_id')+"&category="+$(obj).attr('category')
		$.ajax({
   			type: "POST",
   			url: "ajq_joinmailform.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
//				$('#'+jr.respz.targetdiv).html(jr.respz.html)
	 			$(obj).after(jr.respz.html)
				$('.closebox').unbind().click(
						function(){$('#joinmaildiv').remove()
				})
				$('#joinmaildiv').css('top',ypos-50)

				$('#join_submitbtn').unbind()
					.click(function(){
							submitjoinmailform( $(this).parents('form'),'join_feedback')
							return false
							})
				var mailfrom=$('#subscribe').val()
				$('#mymail').val(mailfrom).unbind()
					.focus(function(){
						if ($(this).val()=='Your email') $(this).val('')
						})
   			}
 		});
}


function validatejoinmail(formobj) {
	var notok=0
	var s

	$(formobj).find("input").each(function(j) {
			var entry=$(this).val()
			if ( entry=="") {
					notok++;
					s+=notok+":"+$(this).attr('name')+"=>"+entry+"\n"
					$(this).addClass('failvalidate')
			}
			else $(this).removeClass('failvalidate')
			
		})
	if (notok>0) alert('Please complete the fields shaded red')
	return notok

}


function submitjoinmailform(formobj,targetdiv) {
 	var poststr=$('#mailtofriend').formSerialize()+"&targetdiv=join_feedback"
 	//TODO does ajq_saveform.php need a table var?
 	if (validatejoinmail(formobj)==0) {  //counts no of erroneous fields
		$('#join_feedback').html('<img src=tiny_red.gif>')
		$.ajax({
   			type: "POST",
   			url: "ajq_joinmail.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('#'+jr.respz.targetdiv).html(jr.respz.feedback)
   			}
 		});
 	}
}
