$(document).ready(function(){
/*	DoenAjax_buildinput_for('mainpic','record_id','mainpagepicform','1');*/
	$('#sendlogin').unbind()
		.click(function(){ DoenAjaxLogin() })

	$('#tx_menu').unbind()
		.click(function(){ 
				DoAjx_fetchtxform()
				return false		
		})
		.addClass('clickable')
	$('#txdiv').html('')
	$('#txformdiv').html('')
	$('#prtf_menu').unbind()
		.click(function(){ 
				DoAjx_fetchprtfform()
				return false		
		})
		.addClass('clickable')
		
		
});

function putstripes() {
		$(".stripeMe tr").mouseover(function(){
				 				$(this).addClass("over");})
							.mouseout(function() {
								$(this).removeClass("over");});
		$(".stripeMe tr:even").addClass("alt");

		
		$('#addrec').click(function(){addrec_tableadmin(localtable,'record_id',gotolocation)})

	 	$('.stripeMe').tableSorter().sortStop(function(){restripe()})
	
}

function DoAjx_fetchprtfform() {
		$.ajax({
   			type: "POST",
   			url: "ajq_getprtfform.php",
/*			data: poststr,*/
			dataType: "json",
   			success: function(jr){
				$('#txformdiv').html(jr.respz.html)
				$('#txdiv').html('')
				$('#portfoliodiv').html('')

				$('#investortxinput').unbind()
					.change(function() {
//						DoAjx_gettx_investorid($(this).val(),'txdiv')
						DoAjx_getportfolio($(this).val(),'portfoliodiv')
						$('#gettx').show()
						$('#topdf').show()
						$('#tx_add').remove()
					})
				$('#gettx').unbind()
					.click(function() {
						$('#portfoliodiv').after('<div id="tx_add">+</div>')
						DoAjx_gettx_investorid($('#investortxinput').val(),'tx_add')
						$(this).hide()
   					})
					.hide()

				$('#topdf').unbind()
					.click(function() {
						window.location='portfolios.htm'
//						DoAjx_porttopdf($('#investortxinput').val(),'tx_add')
   					})
					.hide()
			}
 		});
}

function DoAjx_fetchtxform() {
		$.ajax({
   			type: "POST",
   			url: "ajq_getbuytxform.php",
/*			data: poststr,*/
			dataType: "json",
   			success: function(jr){
				$('#txformdiv').html(jr.respz.html)
				$('#txhistory').html('')
				$('#portfoliodiv').html('')
				$('.transactionheader').remove()
				$('.portfolioheader').remove()
				$('#exectx').unbind()
					.click(function() {
						DoAjx_posttx()
					})
				$('#producttxinput').unbind()
					.change(function() {
						DoAjx_getproductprice($(this).val(),'pricelookup') //val and targetdiv
					})
				$('#investortxinput').unbind()
					.change(function() {
						DoAjx_gettx_investorid($(this).val(),'txdiv')
					})
				setupcalendar()
   			}
 		});
}

function validate_single(obj) {
	if ( ($(obj).val()=='') || ($(obj).val()=='empty')) {
		$(obj).addClass('failvalidate')
		return false
	}
	else {
		$(obj).removeClass('failvalidate')
		return true
	}
}

function DoAjx_posttx() {
//	alert($('#overwrite').attr("checked"))
		var overwrite=""
		if ($('#overwrite').attr("checked")) overwrite='&overwrite=true'
		

	if ( (validate_single($('#investortxinput'))) &&  (validate_single($('#txtypetxinput'))) 	&&  (validate_single($('#txqty'))) &&  (validate_single($('#producttxinput')))  &&  (validate_single($('#price'))) &&  (validate_single($('#date'))) 
	) {

		var poststr="investor_id="+$('#investortxinput').val()+"&txtype="+$('#txtypetxinput').val()+"&unitcount="+$('#txqty').val()+"&product_id="+$('#producttxinput').val()+"&currency="+$('#pricecurrency').html()+"&price="+$('#price').val()+"&datetime="+$('#date').val()+overwrite
	
		$.ajax({
   			type: "POST",
   			url: "ajq_posttx.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('#rightpaneldiv').append('<div>'+jr.respz.html+'</div>')
				if (jr.respz.newrec_price.indexOf('Overwrite')!=-1) alert('Overwrite?')
				else DoAjx_gettx_investorid($('#investortxinput').val(),'txdiv')
   			}
 		});
	}
}

function DoAjx_getproductprice(record_id,targetdiv) { //val and targetdiv
	var poststr="record_id="+record_id
		$.ajax({
   			type: "POST",
   			url: "ajq_getproductprice.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('#'+targetdiv).html(jr.respz.html)
   			}
 		});
}

function DoAjx_gettx_investorid(investor_id,targetdiv) { //val and targetdiv
	var poststr="investor_id="+investor_id
		$.ajax({
   			type: "POST",
   			url: "ajq_gettx.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('.transactionheader').remove()
				$('#txhistory').remove()
				$('#'+targetdiv).html(jr.respz.html)
				DoAjx_getportfolio(investor_id,'portfoliodiv')
   			}
 		});
}

function DoAjx_getportfolio(investor_id,targetdiv) { //val and targetdiv
	var poststr="investor_id="+investor_id+"&user=session&htmlpdf=html"
		$.ajax({
   			type: "POST",
   			url: "ajq_getprtf.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('.portfolioheader').remove()
				$('#portfoliodiv').html(jr.respz.ptfolhtml)
				putstripes()
   			}
 		});
}

function DoAjx_getclientportfolio(htmlpdf) { //val and targetdiv
// users need to provide username and password ==> user => investor
// admin need to provide investor id=====================> investor
	var username=$('#username').val()
	var password=$('#password').val()
	var investor_id=$('#investortxinput').val()
	var todate=$('#todate').val()
	var poststr="username="+username+"&password="+password+"&todate="+todate+"&investor_id="+investor_id+"&htmlpdf="+htmlpdf+"&user=session"
		$.ajax({
   			type: "POST",
   			url: "ajq_getprtf.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
			  $('#puthere').html(jr.respz.html)
				if (htmlpdf=="html") scrollTo(0,200)
				
   			}
 		});
}

function putclientportfcontrols() {
	
		$.ajax({
   			type: "POST",
   			url: "ajq_getclntptfctrl.php",
/*			data: poststr,*/
			dataType: "json",
   			success: function(jr){
				$('#centerpanel').html(jr.respz.html)
				setupcalendar()
				$('#fetch').unbind()
				 .click(function(){ DoAjx_getclientportfolio('html') })
				$('#submittopdf').unbind()
				 .click(function(){ 
					$('#topdf').val('yes')
					$('#portform').submit()
					$('#submittopdf').after('<img src="tiny_red.gif">')
						window.setTimeout('waitforpdf()',20000)
					 })

				$('#investortxinput').unbind()
					.change(function() {
						$('#username').val($(this).val())
					})


   			}
 		});
}

function waitforpdf(){
		$('#submittopdf').next().remove()
}

function setupcalendar() {
				$('.calendarButton')
					.calendar({autoPopUp: 'both', 
							buttonImageOnly: true, 
							buttonImage: 'calendar.gif', 
							buttonText: 'Calendar'})
				popUpCal.minDate = new Date(1990, 1 - 1, 26);
				popUpCal.maxDate = new Date(2020, 1 - 1, 26);
				popUpCal.dateFormat = 'YMD-';
				$('.calinput').attr('readonly','true')

}

function helptext(forwhat) {
	
	$("#helpdisplay").html('<img src=rpspinbig.gif').show()
	var tourl
	if (forwhat=="fielddescriptions") tourl="ajq_fielddesc.php"
	else tourl="ajq_helptext.php"

		$.ajax({
   			type: "POST",
   			url: tourl,
 			data: "topic="+forwhat,
			dataType: "json",
   			success: function(jr){
				logthis ("fetched field desc")
				$('#helpdisplay').html(jr.respz.html)
				.prepend('<div id="closehelp" style="float:right"><img src="close.png" alt="close"></div>')
				.show()
				$('#closehelp').unbind().click(function(){$('#helpdisplay').html('')})
					.addClass('clickable')
   			}
 		});
}