function showChargebackQueue(link) {
	
	resetLinks('menu' , link); 
	$('div1').update(); 
	$('divSelectors').update(); 
	$('searchJobs').hide(); 
	$('divJobSearch').hide();
	if($('divWoSearch')) $('divWoSearch').remove();

	var parms = 'mode=showChargebackQueue';
	new Ajax.Updater ('div1', 'processChargeback.php', {
		onLoading: showSpinner('div1'),
		asynchronous:true, 
		evalScripts:true, 
		method: 'post', 
		parameters: parms,
		onComplete: function() { }
	});	
	return false;
}

function launchSearch(cbId, jobNumber) {
	// alert("cbId:" + cbId + " | jobNumber:" + jobNumber);
	$j('#modalBox').load('processWo.php', { 'mode': 'search_r2w_chargeback_workorders', 'jobNumber': jobNumber, 'cbId': cbId }, function(response, status, xhr) {
		// alert(status + " - " + xhr.status + " " + xhr.statusText);
		if(status=="error") {
			// error stuff
			$j('#modalBox').html('');
			alert('There was an error trying to retrieve the form. Please try again. If this issues persists, please contact an administrator.');
			return false;
		}
		if(status=="success") {
			// alert(response);
			$j('#modalBox').dialog('open');
		}
	});
}

function show_r2w_chargeBacks(link) {
	resetLinks('menu' , link); 
	
	$('div1').update(); 
	$('divSelectors').update(); 
	$('searchJobs').hide(); 
	$('divJobSearch').hide();
	if($('divWoSearch')) $('divWoSearch').remove();

	var parms = 'mode=show_r2w_chargeBacks';
	new Ajax.Updater ('div1', 'processWo.php', {
		onLoading: showSpinner('div1'),
		asynchronous:true, 
		evalScripts:true, 
		method: 'post', 
		parameters: parms,
		onComplete: function() { }
	});	
	return false;
}

function accept_r2w_chargeBack(cbId) {
	var parms = 'mode=accept_r2w_chargeBack&cbId=' + cbId + '&cbComments=' + $j('#cbComments' + cbId).val() + '&cbAmount=' + $j('#cbAmount' + cbId).val();
	alert(parms);
	new Ajax.Updater ('chargeBackMessage' + cbId, 'processWo.php', {
		onLoading: showSpinner('chargeBackMessage' + cbId),
		asynchronous:true, 
		evalScripts:true, 
		method: 'post', 
		parameters: parms,
		onComplete: function() { 
					
		}
	});	
	return false;
}

function decline_r2w_chargeBack(cbId) {

	if(!confirm('decline this chargeback?')) {  return false; }
	var parms = 'mode=decline_r2w_chargeBack&cbId=' + cbId;

	new Ajax.Updater ('chargeBackMessage' + cbId, 'processWo.php', {
		asynchronous:true, 
		evalScripts:true, 
		method: 'post', 
		parameters: parms,
		onComplete: function() { 
			$('chargeBack' + cbId).hide();
		}
	});	
	return false;

}

function search_r2w_chargeback_workOrder(cbId, jobNumber) {

	if($('divWoSearch')) {  $('divWoSearch').remove(); }
		
	divWoSearch = document.createElement('div');
	divWoSearch.id = 'divWoSearch';
	divWoSearch.style.position = 'absolute';
	divWoSearch.style.top =  (mouse.y) + 'px'; 
	divWoSearch.style.left =  (mouse.x) + 'px';

	divWoSearch.style.width = '310px'; 
	//myElement.style.height = '600px'; 
	divWoSearch.style.marginBottom = '50px'; 
	divWoSearch.style.border = "1px solid #6B6C5A"; 
	divWoSearch.style.backgroundColor = "#ffffff"; 
	divWoSearch.style.zIndex = 999;

	document.body.appendChild(divWoSearch);

	new Ajax.Updater (divWoSearch, 'processWo.php', {
		onLoading: showSpinner(divWoSearch),
		asynchronous:true, 
		evalScripts:true, 
		method: 'post', 
		parameters: { mode: 'search_r2w_chargeback_workorders', jobNumber: jobNumber, cbId: cbId  },
		onComplete: function() { 
				
		}
	});	
}

function attach_r2w_chargeBack_workorder(cbId, workOrderId, jobNumber) {
	$j.get('processWo.php', { 'mode': 'attach_r2w_chargeBack_workorder', 'workOrderId': workOrderId, 'cbId': cbId, 'cbComments': $j('#cbComments' + cbId).val(), 'cbAmount':  $j('#cbAmount' + cbId).val() }, function(d, s, x) {
		if(s=="error") {
			// error stuff
			// $j('#modalBox').html('');
			alert('There was an error trying to retrieve the form. Please try again. If this issues persists, please contact an administrator.');
		}
		if(s=="success") {
			// alert(d.companyId);
			$j('#modalBox').dialog('close');
			$j('#acceptButton' + cbId).show();
			$j('#declineButton' + cbId).show();

			var link = document.createElement('a');
			link.setAttribute('href', 'wo.php?workOrderId=' + workOrderId);
			link.target = '_new';
			link.innerHTML = d.jobNumber;

			$('jobNumberLink' + cbId).update();
			$('jobNumberLink' + cbId).appendChild(link);
			
			$('techNumber' + cbId).update(d.companyId);
			$('orderNumber' + cbId).update(d.orderNumber);
			$('woType' + cbId).update(d.woType);
		}
	}, 'json');
	return false;																																																					// new Ajax.Updater (modalBox, 'processWo.php', {
// 		asynchronous:true, 
//		evalScripts:true, 
//		method: 'post', 
//		parameters: { mode: 'attach_r2w_chargeBack_workorder', workOrderId: workOrderId, cbId: cbId, cbComments: $j('#cbComments' + cbId).val(), cbAmount:  $j('#cbAmount' + cbId).val() },
//		onComplete: function(d) { }
//	});	
}

function getHoldingJobSearch(cbId) {
	if($j('#cbHoldingSearchForm').length > 0) { job = $j('#searchFor').val(); } else { job = '' };
	$j('#modalBox').load('processChargeback.php', { 'mode': 'getHoldingJobSearch', 'cbId': cbId, 'jobNumber': job }, function(d, s, x) {
		// alert(status + " - " + xhr.status + " " + xhr.statusText);
		if(s=="error") {
			// error stuff
			$j('#modalBox').html('');
			alert('There was an error trying to retrieve the form. Please try again. If this issues persists, please contact an administrator.');
			return false;
		}
		if(s=="success") {
			// alert(response);
			$j('#modalBox').dialog('open');
		}
	});
	return false;
}

function getR2WJobSearch(cbId, type) {
	if($j('#cbR2WSearchForm').length > 0) { job = $j('#searchFor').val(); } else { job = '' };
	$j('#modalBox').load('processChargeback.php', { 'mode': 'getR2WJobSearch', 'cbId': cbId, 'jobNumber': job, 'searchType': type }, function(d, s, x) {
		// alert(status + " - " + xhr.status + " " + xhr.statusText);
		if(s=="error") {
			// error stuff
			$j('#modalBox').html('');
			alert('There was an error trying to retrieve the form. Please try again. If this issues persists, please contact an administrator.');
			return false;
		}
		if(s=="success") {
			// alert(response);
			$j('#modalBox').dialog('open');
		}
	});
	return false;
}

function attachJobToHoldingCB(id) {
	var woId = $j('#attachJobHoldForm input[name=woId]:checked').val();
	$j.get('processChargeback.php', { 'cbId': id, 'woId': woId, 'mode': 'attachJobToHoldingCB'}, function(d, s, x) {
		// alert(status + " - " + xhr.status + " " + xhr.statusText);
		if(s=="error") {
			// error stuff
			alert('There was an error attaching the job. Please try again. If this issues persists, please contact an administrator.');
		}
		if(s=="success") {
			$j('#modalBox').html('');
			$j('#modalBox').dialog('close');
			// update the jobNumber with a link to the job, update any data, and 
			$j('#holdRow' + id + ' td:nth-child(2)').html('<a onclick="viewWorkorder(' + woId + ')">'+ d.jobNumber +'</a>');
			$j('#holdRow' + id + ' td:nth-child(4)').html(d.address);
			$j('#holdRow' + id + ' td:nth-child(10)').html('<a onclick="getAcceptChargebackHoldingForm(' + woId + ');">Accept</a> | <a onclick="getRejectChargebackHoldingForm(' + woId + ');">Reject</a>');
			$j('#holdRow' + id).effect('highlight', 3000);
		}
	}, 'json');
	return false;
}

function getAcceptChargebackHoldingForm(cbId) {
	$j('#modalBox').load('processChargeback.php', { 'mode': 'getAcceptChargebackHoldingForm', 'cbId': cbId }, function(d, s, x) {
		// alert(status + " - " + xhr.status + " " + xhr.statusText);
		if(s=="error") {
			// error stuff
			$j('#modalBox').html('');
			alert('There was an error trying to retrieve the form. Please try again. If this issues persists, please contact an administrator.');
			return false;
		}
		if(s=="success") {
			// alert(response);
			$j('#modalBox').dialog('open');
		}
	});
	return false;
}

function getRejectChargebackHoldingForm(cbId) {
	$j('#modalBox').load('processChargeback.php', { 'mode': 'getRejectChargebackHoldingForm', 'cbId': cbId }, function(d, s, x) {
		// alert(status + " - " + xhr.status + " " + xhr.statusText);
		if(s=="error") {
			// error stuff
			$j('#modalBox').html('');
			alert('There was an error trying to retrieve the form. Please try again. If this issues persists, please contact an administrator.');
			return false;
		}
		if(s=="success") {
			// alert(response);
			$j('#modalBox').dialog('open');
		}
	});
	return false;
}

function acceptChargebackHolding() {
	$j('#cbHoldAcceptButton').attr('disabled','disabled');
	id = $j('#cbHoldAcceptForm input[name=cbId]').val();
	frm = $j('#cbHoldAcceptForm').serialize();
	// alert(frm);
	$j.get('processChargeback.php', frm, function(d, s, x) {
		if(s=="error") {
			alert('There was an error accepting the chargeback. Please try again. If this issues persists, please contact an administrator.');
			$j('#cbHoldAcceptButton').attr('disabled','');
			return false;
		}
		$j('#modalBox').dialog('close');
		$j('#holdRow' + id).html('<td colspan="13" style="text-align:center; font-weight:bold">Chargeback acceptance has been processed!</td>');
		$j('#holdRow' + id + ' td').effect('highlight', 3000).delay(2000).fadeOut(3000, function() { $j(this).parent().remove();});
	}, 'json');
	return false;
}

function rejectChargebackHolding() {
	$j('#cbHoldRejectButton').attr('disabled','disabled');
	id = $j('#cbHoldRejectForm input[name=cbId]').val();
	frm = $j('#cbHoldRejectForm').serialize();
	// alert(frm);
	$j.get('processChargeback.php', frm, function(d, s, x) {
		if(s=="error") {
			alert('There was an error rejecting the chargeback. Please try again. If this issues persists, please contact an administrator.');
			$j('#cbHoldAcceptButton').attr('disabled','');
			return false;
		}
		// alert($j('#holdRow' + id).html());
		$j('#modalBox').dialog('close');
		$j('#holdRow' + id).html('<td colspan="13" style="text-align:center; font-weight:bold">Chargeback rejection has been processed!</td>');
		$j('#holdRow' + id + ' td').effect('highlight', 3000).delay(2000).fadeOut(3000, function() { $j(this).parent().remove();});
	}, 'json');
	return false;
}

function attachJobToR2WCB(id) {
	var woId = $j('#attachJobR2WForm input[name=woId]:checked').val();
	$j.get('processChargeback.php', { 'cbId': id, 'woId': woId, 'mode': 'attachJobToR2WCB'}, function(d, s, x) {
		// alert(status + " - " + xhr.status + " " + xhr.statusText);
		if(s=="error") {
			// error stuff
			alert('There was an error attaching the job. Please try again. If this issues persists, please contact an administrator.');
		}
		if(s=="success") {
			$j('#modalBox').html('');
			$j('#modalBox').dialog('close');
			$j('#r2wRow' + id + ' td:nth-child(2)').html('<a onclick="viewWorkorder(' + woId + ')">'+ d.jobNumber +'</a>');
			$j('#r2wRow' + id + ' td:nth-child(11)').html('<a onclick="getChargebackR2WEdit(' + id + ');">Edit</a> | <a onclick="acceptChargebackR2W(' + id + ');">Accept</a> | <a onclick="rejectChargebackR2W(' + id + ');">Reject</a>');
			$j('#r2wRow' + id).effect('highlight', 3000);
		}
	}, 'json');
	return false;
}

function getChargebackR2WEdit(cbId) {
	$j('#modalBox').load('processChargeback.php', { 'mode': 'getChargebackR2WEdit', 'cbId': cbId }, function(d, s, x) {
		// alert(status + " - " + xhr.status + " " + xhr.statusText);
		if(s=="error") {
			// error stuff
			$j('#modalBox').html('');
			alert('There was an error trying to retrieve the form. Please try again. If this issues persists, please contact an administrator.');
			return false;
		}
		if(s=="success") {
			// alert(response);
			$j('#modalBox').dialog('open');
		}
	});
	return false;
}

function editChargebackR2W() {
	id = $j('#cbR2WEditForm input[name=cbId]').val();
	frm = $j('#cbR2WEditForm').serialize();
	// alert(frm);
	$j.get('processChargeback.php', frm, function(d, s, x) {
		if(s=="error") {
			alert('There was an error editing the chargeback. Please try again. If this issues persists, please contact an administrator.');
		}
		if(s=="success") {
			if(d.cbComments.length > 20) {
				comment = d.cbComments.substr(0, 17) + '...';
			} else {
				comment = d.cbComments;
			}
			$j('#modalBox').dialog('close');
			$j('#r2wRow' + id + ' td:nth-child(8)').html(d.techCBAmount);
			$j('#r2wRow' + id + ' td:nth-child(9)').html(comment);
			$j('#r2wRow' + id).effect('highlight', 3000);
		}
	}, 'json');
	return false;
}

function acceptChargebackR2W(id) {
	if(confirm("Are you sure you want to accept this chargeback?")) {
		$j.get('processChargeback.php', { 'cbId': id, 'mode': 'acceptChargebackR2W' }, function(d, s, x) {
			if(s=="error") {
				alert('There was an error accepting the chargeback. Please try again. If this issues persists, please contact an administrator.');
			}
			if(s=="success") {
				// alert("successful!");
				if(d.accepted==1) {
					$j('#r2wRow' + id).html('<td colspan="11" style="text-align:center; font-weight:bold">Chargeback acceptance has been processed!</td>');
					$j('#r2wRow' + id + ' td').effect('highlight', 3000).delay(2000).fadeOut(3000, function() { $j(this).parent().remove();});
				} else {
					alert("there was an error:" + d.error.error + "/nsql:" + d.error.sql);
				}
			}
		}, 'json');
	}
}

function rejectChargebackR2W(id) {
	if(confirm("Are you sure you want to reject this chargeback?")) {
		$j.get('processChargeback.php', { 'cbId': id, 'mode': 'rejectChargebackR2W' }, function(d, s, x) {
			if(s=="error") {
				alert('There was an error rejecting the chargeback. Please try again. If this issues persists, please contact an administrator.');
			}
			if(s=="success") {
				// alert("successful!");
				if(d.rejected==1) {
					$j('#r2wRow' + id).html('<td colspan="11" style="text-align:center; font-weight:bold">Chargeback rejection has been processed!</td>');
					$j('#r2wRow' + id + ' td').effect('highlight', 3000).delay(2000).fadeOut(3000, function() { $j(this).parent().remove();});
				} else {
					alert("there was an error:" + d.error.error + "\nsql:" + d.error.sql);
				}
			}
		}, 'json');
	}
}

function submitCBChallenge(id) {
	if(confirm('Are you sure you want to challenge this chargeback?')) {
		$j.get('processChargeback.php', { 'cbId':id, 'mode':'submitCBChallenge' }, function(d,s,x) {
			if(s=="success") {
				if(d.failed==1) {
					alert('The request did not process correctly. If the problem persists, please inform a supervisor. Error:' + d.error.error);
				} else {
					$j('#cbRow' + id + ' td:nth-child(9)').html('Pending');
					$j('#cbRow' + id).effect('highlight', 3000);
				}
			}
		}, 'json');
	}
}

function agreeCB(id) {
	if(confirm('Are you sure you want to agree to this chargeback?')) {
		$j.get('processChargeback.php', { 'cbId':id, 'mode':'agreeCB' }, function(d,s,x) {
			if(s=="success") {
				if(d.failed==1) {
					alert('The request did not process correctly. If the problem persists, please inform a supervisor. Error:' + d.error.error);
				} else {
					$j('#cbRow' + id + ' td:nth-child(9)').html('Agreed');
					$j('#cbRow' + id).effect('highlight', 3000);
				}
			}
		}, 'json');
	}
}

function acceptChallenge(id) {
	if(confirm('By accepting, you are agreeing that this chargeback should NOT be charged. Are you sure you want to accept this challenge?')) {
		$j.get('processChargeback.php', { 'cbId':id, 'mode':'acceptChallenge' }, function(d,s,x) {
			// alert(d.sql);
			// alert($j('#noteRow' + id + ' td:nth-child(10)').html());
			if(s=="success") {
				if(d.failed==1) {
					alert('The request did not process correctly. If the problem persists, please contact an administrator. Error:' + d.error.error);
				} else {
					$j('#noteRow' + id + ' td:nth-child(10)').html('Challenge accepted');
					$j('#noteRow' + id).effect('highlight', 3000);
				}
			}
		}, 'json');
	}
}

function declineChallenge(id) {
	if(confirm('By declining, you are charging the contractor with this chargeback. Are you sure you want to decline this challenge?')) {
		$j.get('processChargeback.php', { 'cbId':id, 'mode':'declineChallenge' }, function(d,s,x) {
			// alert(d.sql);
			if(s=="success") {
				if(d.failed==1) {
					alert('The request did not process correctly. If the problem persists, please contact an administrator. Error:' + d.error.error);
				} else {
					alert($j('#noteRow' + id + ' td:nth-child(10)').html());
					$j('#noteRow' + id + ' td:nth-child(10)').html('Challenge declined');
					$j('#noteRow' + id).effect('highlight', 3000);
				}
			}
		}, 'json');
	}
}
