$(function() {

	$('.mcview_showform').click(function(e) {
	
		e.preventDefault();
		$('.mcview_form').show();
		$('html, body').animate({
			scrollTop: $('.mcview_tarea').position().top + 'px'
		}, 1000);
	
	});
	
	$('.mcview_showform').live('click', function(e) {
	
		e.preventDefault();
		$('.mcview_form').show();
		$('html, body').animate({
			scrollTop: ($('.mcview_tarea').position().top + 10) + 'px'
		}, 1000);
	
	});
	
	$('.mcview_closelink').live('click', function(e) {
	
		e.preventDefault();
		
		if($('.mcview_clink').attr('rel') == 'no')
			alert('Sorry, but this lead is either waiting for a response from us or is already closed.');
		else {
			if($('.mcview_clink').attr('rel') == 'admin') {
				var delLead = confirm("Are you sure you want to close this lead? A notice will not be sent to the client.");
			} else {
				var delLead = confirm("Are you sure you want to close this lead? A notice will be sent to the client.");
			}
		
			if(delLead == true) {
			
				var msgid = $('#mcview_respform input[name=messageid]').val();
				
				$.ajax({
					url: 'leadtracker.php?action=closeLead',
					type: 'POST',
					data: 'mid='+msgid,
					success: function(r) {
						
						if(r == 'ERROR') {
							alert('The lead could not be closed at this time. Please check the status and try again later.');
						} else {
							window.location = "leadtracker.php";
						}
						
					}			
				});
			
			}
		}
		
	});
	
	$('#mcview_respform').submit(function(e) {
	
		e.preventDefault();
		if($('.mcview_tarea').val() != '') {
			var formdata = $(this).serialize();
			
			$.ajax({
				url: 'leadtracker.php?action=respondLeadProc',
				type: 'POST',
				data: formdata,
				success: function(r) {
					var html = $('#lm_mainbodycont', r).html();
					$('#lm_mainbodycont').html(html);	
					 $('html, body').animate({
						 scrollTop: $('.mcview_resptable').position().top + 'px'
					 }, 1000);
				}			
			});
		}
	});
	
	$('#noteform_sbmt').live('click', function(e) {
	
		e.preventDefault();
		$('.notes_updated').hide();
		$('.notes_updating').show('slow');
		var notedata = $('.leadnotes').serialize();
			
		$.ajax({
			url: 'leadtracker.php?action=updNotes',
			type: 'GET',
			data: notedata,
			success: function(r) {
				var notes = $('.sidemenucont_notes_ajax', r).html();
				$('.sidemenucont_notes').html(notes);	
				$('.notes_updating').hide();
				$('.notes_updated').show('slow');
			}			
		});
		
	});
	
	$('#googleapi_addaccount').submit(function(e){
		e.preventDefault();
		var purl = $(this).attr('action');
		var pdata = $(this).serialize();
		$.ajax({
			url: purl,
			type: 'post',
			data: pdata,
			success: function(r){
				if('SUCCESS' == r) {
					$.ajax({
						url: 'runreports_googleapiusage.php',
						type: 'get',
						success: function(d) {
							$('#googleadwords_apiclient').html($('#googleadwords_apiclient', d).html());
							$('#successshow_googleapi').fadeIn(250);
							$('#errorshow_googleapi').hide();
						}
					});
				} else {
					$('#successshow_googleapi').hide();
					$('#errorshow_googleapi').fadeIn(250);
				}
			}
		});
	});
	
	var parent;
	
	$('.temp_addnew').click(function() {
	
		parent = $(this).parent().parent();
		
		var pclass = parent.parent().parent().attr('class');
		var textareatype = 'template';
		if(pclass == 'customercomment') {
			textareatype = 'customercomment';
		}
		
		parent.before('<tr><td align="left" valign="top" colspan="2">' +
		'<input class="temp_status_num" value="0" name="active[]" type="hidden" />' +
									'<textarea noresize class="template_box" name="' + textareatype + '[]"></textarea>' +
									'<img src="images/remove.png" /><span class="temp_del" style="font-size: 12px; cursor: pointer;">Delete</span>' +
									'<br />' +
									'<div class="temp_status"><img src="images/alert.png" /><span class="temp_inact" style="font-size: 12px; cursor: pointer;">Inactive</span></div>' +
								'</td></tr>');
	
	});
	
	$('.temp_del').live('click', function() {
	
		parent = $(this).parent().parent();
		parent.remove();
	
	});
	
	var gparent;
	
	$('.temp_act').live('click', function() {
	
		parent = $(this).parent()
		parent.html('<img src="images/alert.png" /><span class="temp_inact" style="font-size: 14px; cursor: pointer;">Inactive</span>');
		gparent = parent.parent();
		$('.temp_status_num', gparent).val(0);
	
	});
	
	$('.temp_inact').live('click', function() {
	
		parent = $(this).parent()
		parent.html('<img src="images/check.png" /><span class="temp_act" style="font-size: 14px; cursor: pointer;">Active</span>');
		gparent = parent.parent();
		$('.temp_status_num', gparent).val(1);
	
	});
	
	$('.leadstatus').live('mouseenter', function(){
	
		parent = $(this).parent();
		$('.ls_popup', parent).show();
	
	});
	
	$('.leadstatus').live('mouseleave', function(){
	
		parent = $(this).parent();
		$('.ls_popup', parent).hide();
	
	});	
	
	var currTemp;
	
	$('.lead_temp_select').change(function() {
	
		currTemp = $(this).val();
		$('.mcview_tarea').val(currTemp);
	
	});
	
	
	
	$('.predefined_messages').change(function(){
		var areaselect = $(this).attr('ref');
		$('#' + areaselect).html($(this).val());
	});
	
	
	$('.orderlink').click(function(){
		var boxref = $(this).attr('ref');
		$('#'+boxref+' .ipbtn').trigger('click');
	});
	
	
	$('.ipbtn').click(function(e){
		e.preventDefault();
		var ipaddr = $(this).attr('ref');
		var box = $(this).parent();
		var city = $(this).attr('city');
		var state = $(this).attr('state');
		var country = $(this).attr('country');
		
		$.ajax({
			type: 'post',
			url: 'getipinfo.php',
			data: 'ipaddr='+ipaddr+'&city='+city+'&state='+state+'&country='+country,
			success: function(r){
				box.html(r);
			}
		});
	});
	
	
	

});

function pruneLead() {
	var msgid;
	$('.leadid').each(function() {
		msgid = $(this).attr('rel');
		$.ajax({
		url: 'leadtracker.php?action=closeLead',
		type: 'POST',
		data: 'mid='+msgid,
		success: function(r) {
			}
		});
	});
}
