var request = {};
request.ID = 'Imp';
request.Fn = 'GetImportFCLPortToPortRate';
request.lastFn = 'GetImportFCLPortToPortRate';
request.params = new Array;
request.data = '?';
request.ready = false;
request.responceData = '';
request.bookingKey = '';
request.paramsSaved = new Array;
request.story = new Array;

request.setFunction = function(FNname) {
	this.lastFn = this.Fn;
	this.Fn = FNname;
};

request.setReady = function(r) {
	this.ready = r;
};

request.getParams = function() {
	try {
		var fn = 'this.' + this.Fn + '();';
		this.params = new Array;
		this.data = '?';
		eval(fn);
		this.parseParams();
	} catch (e) {
		this.ready = false;
	}
};

request.parseParams = function() {
	this.setReady(true);
	if (!empty(this.params['pieces'])) {
		var s = '';
		$.each(this.params['pieces'], function(key, arr) {
			var i = 0;
			for (k in arr) {
				if (empty(arr[k]) || !isNumber(arr[k])) {
					request.setReady(false);
				}
				i++;
				s += k + '=' + arr[k] + ((i == 6) ? ';' : ':');
			}
		});
		this.params['pieces'] = s;
	}

	if (!empty(this.params['lineItems'])) {
		this.setReady(false);
		var s = '';
		$.each(this.params['lineItems'], function(key, arr) {
			var i = 0;
			if (arr) {
				if (!empty(arr['Quantity']) && isNumber(arr['Quantity'])
						&& !empty(arr['Weight']) && isNumber(arr['Weight'])) {
					request.setReady(true);
					for (k in arr) {
						i++;
						s += k + '=' + arr[k] + ((i == 9) ? ';' : ':');
					}
				}
			}
		});
		this.params['lineItems'] = s;
	}

	if (!empty(this.params['bookingLineItems'])) {
		this.setReady(false);
		var s = '';
		$.each(this.params['bookingLineItems'], function(key, arr) {
			var i = 0;
			if (arr) {
				request.setReady(true);
				for (k in arr) {
					i++;
					s += k + '=' + arr[k] + ((i == 4) ? ';' : ':');
				}

			}
		});
		this.params['bookingLineItems'] = s;
	}

	for (k in this.params) {
		if (k == 'residentialPickup' || k == 'liftGatePickup') {
			this.data += k + '=' + this.params[k] + '&';
		} else {
			if (empty(this.params[k])) {
				this.setReady(false);
			}
			this.data += k + '=' + this.params[k] + '&';
		}
	}
	this.story[this.Fn] = this.data;
};

request.GetExportFCLDoorToPortRate = function() {
	this.params['originCity'] = $('#' + this.ID + 'DestinationCitySL').val();
	this.params['originState'] = $('#' + this.ID + 'DestinationState').val();
	this.params['destinationPort'] = $('#' + this.ID + 'DestinationPortSL')
			.val();
	this.type = 'FCL';

};
request.GetExportFCLPortToPortRate = function() {
	this.type = 'FCL';
	this.params['origin'] = $('#' + this.ID + 'OriginCity').val();
	this.params['destination'] = $('#' + this.ID + 'DestinationPortSL').val();
};
request.GetExportLCLDoorToPortRate = function() {
	this.type = 'LCL';
	this.params['originZipCode'] = $('#' + this.ID + 'OriginCity').val();
	this.params['destinationPort'] = $('#' + this.ID + 'DestinationPortSL')
			.val();
	this.params['insuranceAmount'] = $('#txtInsur').val();
	this.params['residentialPickup'] = (($('#chkRes').attr('checked')) ? 1 : 0);
	this.params['liftGatePickup'] = (($('#chkLift').attr('checked')) ? 1 : 0);
	this.parseTable();
};
request.GetExportLCLPortToPortRate = function() {
	request.GetImportLCLPortToPortRate();
};
request.GetImportFCLPortToDoorRate = function() {
	this.type = 'FCL';
	this.params['originPort'] = $('#' + this.ID + 'OriginCity').val();
	this.params['destinationCity'] = $('#' + this.ID + 'DestinationCitySL')
			.val();
	this.params['destinationState'] = $('#' + this.ID + 'DestinationState')
			.val();
};
request.GetImportFCLPortToPortRate = function() {
	this.type = 'FCL';
	this.params['origin'] = $('#' + this.ID + 'OriginCity').val();
	this.params['destination'] = $('#' + this.ID + 'DestinationPortSL').val();
};
request.GetImportLCLPortToPortRate = function() {
	this.type = 'LCL';
	this.params['origin'] = $('#' + this.ID + 'OriginCity').val();
	this.params['destination'] = $('#' + this.ID + 'DestinationPortSL').val();
	this.params['pieces'] = new Array();
	this.parseTable();
};

request.GetLTLRate = function() {
	var originData = $('#LTLOriginCityState').val().split(',');
	var destinationData = $('#LTLDestinationCityState').val().split(',');

	this.type = 'LTL';
	this.params['pickupDate'] = $('#txtPickupDate').val();

	this.params['originZip'] = $('#LTLOriginZip').val();
	this.params['originCity'] = originData[0];
	this.params['originState'] = originData[1];

	this.params['destinationZip'] = $('#LTLDestinationZip').val();
	this.params['destinationCity'] = destinationData[0];
	this.params['destinationState'] = destinationData[1];
	this.parseItemInformation();
	this.parseServices();
	this.paramsSaved = this.params;
};

request.CreateLTLBooking = function() {
	this.params['bookingKey'] = this.bookingKey;
	this.params['customerType'] = $('#rdblClientType input:checked').val();
	this.params['shipmentDate'] = $('#txtPickupDate').val();
	this.params['readyTime'] = $('#ddlRHour').val() + ':'
			+ $('#ddlRMinute').val() + ' ' + $('#ddlRAMPM').val();
	this.params['closeTime'] = $('#ddlCHour').val() + ':'
			+ $('#ddlCMinute').val() + ' ' + $('#ddlCAMPM').val();
	;
	this.params['bolSendTo'] = $('#rdblBill input:checked').val();
	this.params['poNumber'] = $('#txtPONumber').val();
	this.params['comments'] = $('#txtComment').val();

	var pickupLocation = new Array();
	pickupLocation['Name'] = $('#txtPName').val();
	pickupLocation['Email'] = $('#txtPEmail').val();
	pickupLocation['Company'] = $('#txtPCompany').val();
	pickupLocation['Phone'] = $('#txtPPhone').val();
	pickupLocation['Address1'] = $('#txtPAddress1').val();
	pickupLocation['Address2'] = $('#txtPAddress2').val();
	pickupLocation['City'] = $('#txtPCity').val();
	pickupLocation['State'] = $('#txtPST').val();
	pickupLocation['Zip'] = $('#txtPZip').val();
	this.params['pickupLocation'] = this.JoinArr(pickupLocation);

	var destinationLocation = new Array();
	destinationLocation['Name'] = $('#txtDName').val();
	destinationLocation['Email'] = $('#txtDEmail').val();
	destinationLocation['Company'] = $('#txtDCompany').val();
	destinationLocation['Phone'] = $('#txtDPhone').val();
	destinationLocation['Address1'] = $('#txtDAddress1').val();
	destinationLocation['Address2'] = $('#txtDAddress2').val();
	destinationLocation['City'] = $('#txtDCity').val();
	destinationLocation['State'] = $('#txtDST').val();
	destinationLocation['Zip'] = $('#txtDZip').val();
	this.params['destinationLocation'] = this.JoinArr(destinationLocation);

	this.params['bookingLineItems'] = new Array();
	n = $('#tblShipmentDetails tr').length - 1;
	for (i = 0; i < n; i++) {
		this.params['bookingLineItems'][i] = new Array();
		this.params['bookingLineItems'][i]['Tag'] = '1';
		this.params['bookingLineItems'][i]['NMFC'] = $('#txtNMFC' + i).val();
		this.params['bookingLineItems'][i]['NumberOfPallet'] = $(
				'#txtPallet' + i).val();
		this.params['bookingLineItems'][i]['Description'] = $('#txtDesc' + i)
				.val();
	}
};

request.SendOcean = function(){
	this.params['customerType'] = $('#rdblClientType input:checked').val();
	this.params['shipmentDate'] = $('#txtPickupDate').val();
	this.params['readyTime'] = $('#ddlRHour').val() + ':'
			+ $('#ddlRMinute').val() + ' ' + $('#ddlRAMPM').val();
	this.params['closeTime'] = $('#ddlCHour').val() + ':'
			+ $('#ddlCMinute').val() + ' ' + $('#ddlCAMPM').val();
	;
	this.params['bolSendTo'] = $('#rdblBill input:checked').val();
	this.params['poNumber'] = $('#txtPONumber').val();
	this.params['comments'] = $('#txtComment').val();

	var pickupLocation = new Array();
	pickupLocation['Name'] = $('#txtPName').val();
	pickupLocation['Email'] = $('#txtPEmail').val();
	pickupLocation['Company'] = $('#txtPCompany').val();
	pickupLocation['Phone'] = $('#txtPPhone').val();
	pickupLocation['Address1'] = $('#txtPAddress1').val();
	pickupLocation['Address2'] = $('#txtPAddress2').val();
	pickupLocation['City'] = $('#txtPCity').val();
	pickupLocation['State'] = $('#txtPST').val();
	pickupLocation['Zip'] = $('#txtPZip').val();
	this.params['pickupLocation'] = this.JoinArr(pickupLocation);

	var destinationLocation = new Array();
	destinationLocation['Name'] = $('#txtDName').val();
	destinationLocation['Email'] = $('#txtDEmail').val();
	destinationLocation['Company'] = $('#txtDCompany').val();
	destinationLocation['Phone'] = $('#txtDPhone').val();
	destinationLocation['Address1'] = $('#txtDAddress1').val();
	destinationLocation['Address2'] = $('#txtDAddress2').val();
	destinationLocation['City'] = $('#txtDCity').val();
	destinationLocation['State'] = $('#txtDST').val();
	destinationLocation['Zip'] = $('#txtDZip').val();
	this.params['destinationLocation'] = this.JoinArr(destinationLocation);
	this.params['lastFnParams'] = this.story[this.lastFn].replace('&', ':').replace('?', '')+'lastFn='+this.lastFn;
	if(this.story['OceanInfo']) this.params['OceanInfo'] = this.story['OceanInfo'];
};

request.GetFCLExportDoorToPortOriginStates = function() {
};

request.GetFCLExportDoorToPortOriginCities = function() {
	this.params['originState'] = $('#' + this.ID + 'DestinationState').val();
};

request.GetFCLExportDoorToPortDestinations = function() {
	this.params['originState'] = $('#' + this.ID + 'DestinationState').val();
	this.params['originCity'] = $('#' + this.ID + 'DestinationCitySL').val();
};

request.GetFCLExportPortToPortOrigins = function() {
};

request.GetFCLExportPortToPortDestinations = function() {
	this.params['originCity'] = $('#' + this.ID + 'OriginCity').val();
};

request.GetFCLImportOrigins = function() {
};

request.GetFCLImportPortToPortDestinations = function() {
	this.params['originCity'] = $('#' + this.ID + 'OriginCity').val();
};

request.GetFCLImportPortToDoorDestinationStates = function() {
	this.params['originCity'] = $('#' + this.ID + 'OriginCity').val();
};

request.GetFCLImportPortToDoorDestinationCities = function() {
	this.params['originCity'] = $('#' + this.ID + 'OriginCity').val();
	this.params['destinationState'] = $('#' + this.ID + 'DestinationState')
			.val();
};

request.GetLCLImportOrigins = function() {
};

request.GetLCLImportPortToPortDestinations = function() {
	this.params['originCity'] = $('#' + this.ID + 'OriginCity').val();
};

request.GetLCLExportDoorToPortDestinations = function() {
};

request.GetLCLExportPortToPortOrigins = function() {
};

request.GetLCLExportPortToPortDestinations = function() {
	this.params['originCity'] = $('#' + this.ID + 'OriginCity').val();
};

request.parseTable = function() {
	var rowCount = $('#' + this.ID + 'UserTable tr').length - 2;
	for ( var rowNumber = 1; rowNumber <= rowCount; rowNumber++) {
		this.params['pieces'] = new Array;
		this.params['pieces'][rowNumber] = new Array;
		this.params['pieces'][rowNumber]['Quantity'] = $(
				'#' + this.ID + 'UserTable :input[@name="lcl_' + rowNumber
						+ '-2"]').val();
		this.params['pieces'][rowNumber]['Weight'] = $(
				'#' + this.ID + 'UserTable :input[@name="lcl_' + rowNumber
						+ '-1"]').val();
		this.params['pieces'][rowNumber]['Height'] = $(
				'#' + this.ID + 'UserTable :input[@name="lcl_' + rowNumber
						+ '-5"]').val();
		this.params['pieces'][rowNumber]['Width'] = $(
				'#' + this.ID + 'UserTable :input[@name="lcl_' + rowNumber
						+ '-4"]').val();
		this.params['pieces'][rowNumber]['Length'] = $(
				'#' + this.ID + 'UserTable :input[@name="lcl_' + rowNumber
						+ '-3"]').val();
		this.params['pieces'][rowNumber]['FreightClass'] = $(
				'#' + this.ID + 'UserTable td[class="lcl ' + this.ID + '-lcl'
						+ rowNumber + '-0"] > *').val();
	}
};

request.parseServices = function() {
	this.params['services'] = 'false';
	arr = $('#ServicesTable input[type=checkbox]:checked');
	if (arr.length > 0) {
		var services = new Array;
		$.each(arr, function(key, obj) {
			servicesPP[key] = obj.id;
			services[key] = obj.value;
		});
		this.params['services'] = services.join(':');
	}
};

request.JoinArr = function(arr) {
	string = '';
	for (k in arr) {
		string += k + ':' + arr[k] + ';';
	}
	return string;
};

request.parseItemInformation = function() {
	this.params['lineItems'] = new Array;
	for ( var i = 1; i < 5; i++) {
		this.params['lineItems'][i] = new Array;
		this.params['lineItems'][i]['Quantity'] = $('#txtPiece' + i).val();
		this.params['lineItems'][i]['Weight'] = $('#txtWeight' + i).val();
		this.params['lineItems'][i]['Length'] = $('#txtLength' + i).val();
		this.params['lineItems'][i]['Width'] = $('#txtWidth' + i).val();
		this.params['lineItems'][i]['Height'] = $('#txtHeight' + i).val();
		this.params['lineItems'][i]['ItemUnit'] = $('#ddlUnit' + i).val();
		this.params['lineItems'][i]['FreightClass'] = $('#ddlClass' + i).val();
		this.params['lineItems'][i]['HazMat'] = (($('#chkHazMat' + i)
				.attr('checked')) ? 1 : 0);
		this.params['lineItems'][i]['Tag'] = '1';
	}
};

request._run = function(objId) {
	request.getParams();
	$('#' + request.ID + objId + '-reloadicon').css( {
		display : ''
	});
	var url = HTTP_PATH + 'rater-selections-ajax.php' + request.data;
	$.get(url, {
		fn : request.Fn
	}, function(data) {
		request.responceData = data;
		setData(objId);
	});
};

function getResult(objid, callbak) {
//	console.log(request.story);
	request.getParams();
	var url = HTTP_PATH + 'rater-selections-ajax.php' + request.data;
	if (request.ready) {
		showResult(
				'<div class="loading"><div class="image"><img src="' + HTTP_PATH + 'img/loadercute.gif"></div><div class="info">LOADING...</div>'
				+'</div><div style="clear:both;"></div><div style="text-align:center">Click CALCULATE again if rates do not display within 5 seconds</div>',
				objid);
		$.get(url, {
			fn : request.Fn
		}, function(data) {
			showResult(data, objid);
			if(callbak)
				eval(callbak);
		});
	} else {
		showResult(
				'<div class="error">Sorry, not all required data was inputted.</div>',
				objid);
		if(callbak)
			eval(callbak);
	}
}

function showResult(data, objid) {
	obj = $('#' + objid);
	if (obj.length == 0)
		obj = $("#requestResult");
	obj.fadeOut('slow', function() {
		obj.html(data);
		obj.fadeIn('slow');
	});
}

function resultClear(objid) {
	showResult('', objid);
}