﻿/// <reference name='http://ajax.googleapis.com/ajax/libs/jquery/1.2/jquery.js'/>
function getAllQuotes(obj,c,w,length,width,height,ins,func){
	if(obj==null) obj=$(document);
	
	$.ajaxSetup({
		timeout:10000
	});
	var qo = 0;
	obj.find('.quote-me-table-value').each(function(i,o){
		qo = $(o);
		qo.find('.value,.error,img').remove();
		qo.append(_throbber.clone());
		getQuote(qo,c,w,length,width,height,ins,func,1);
	});
	
	return false;
}

function refreshQuotes(serializedForm,obj,c,w,length,width,height,ins,func) {

	var insuredvalue = getQueryVariable(serializedForm,"insuredvalue");
	var qo = 0;
	obj.find('.quote-me-table-value').each(function(i,o){
		qo = $(o);
		qo.find('.value,.error,img').remove();
		qo.append(_throbber.clone());
		getQuote(qo,c,w,length,width,height,insuredvalue,func,1);
	});
	return false;
}

function getQueryVariable(querystring,variable) {
  var vars = querystring.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
} 

function getQuote(qo,c,w,length,width,height,ins,func,attempt){
	
	var v = qo.find('input[name=serviceId],input[name=courierServiceId]').val();

	if(c==null) c = $('#countryId').val();
	if(w==null) w = $('#weight,#weightkg').val();

	if(length==null) length = $('#lengthcm').val();
	if(length=='') length = 30;
	
	if(width==null) width = $('#widthcm').val();
	if(width=='') width = 30;
	
	if(height==null) height = $('#heightcm').val();
	if(height=='') height = 30;

	if(ins==null) ins = 0;
	qo.find('.select').hide();

	var isInsured = ins > 0;
	var data = {
	    supplierServiceId: v,
	    countryId: c,
	    weight: w, 
	    heightcm: height, 
	    widthcm: width, 
	    lengthcm: length,
	    isInsured: isInsured,
	    insuredValue: ins,
	    returnJson: true
	};
	
	if(fixText($('#postcode').val()) != null){
	    data.attentionName = fixText($('#attentionname').val());
	    data.contactPhone = fixText($('#contactphone').val());
	    data.companyName = fixText($('#companyname').val());
	    data.description = fixText($('#description').val());
	    data.line1 = fixText($('#line1').val());
	    data.line2 = fixText($('#line2').val());
	    data.line3 = fixText($('#line3').val());
	    data.postcode = fixText($('#postcode').val());
	    data.statecode = fixText($('#statecode').val());
	    data.town = fixText($('#town').val());
	}

	$.ajax({
		cache: false,
		type: 'POST',
		url: $('form#quoteForm').attr('action'),
		data: data,
		success: function(data) {
			qo.find('.value,.error,img').remove();
			if (data.Fail) {
				qo.append($('<span class="error"/>').append(
						_alert.clone()
						.attr('alt', data.Message)
						.attr('title', data.Message)));
				if (func != null) {
					func(qo.parent(), data.Message);
				}
			}
			else {
				qo.append($('<span class="value"/>').html('&pound;' + data.Price.toFixed(2) + ' '));
				if (typeof(_startOrderPage) != 'undefined') {
					var selectLink = (_startOrderPage + '?countryId=' + c + '&weight=' + w + '&heightcm=' + height + '&widthcm=' + width + '&lengthcm=' + length);
					qo.find('.select').children().remove();
					qo.find('.select').append($('<a href="' + selectLink + '&serviceId=' + data.ServiceId +
							'" />').text('Select')
						).remove().insertAfter(qo.find('.value')).show();
				}
			}
		},
		dataType: 'json',
		error: function() {
			if (attempt < 3) {
				attempt++;
				getQuote(qo,courierServiceId, c, w, length, width, height, func, attempt);
			}
			else {
			    $('img', qo).remove();
			    qo.append($('<span class="error"/>').append(
				_alert.clone()
				.attr('alt', 'Failed to get quote')
				.attr('title', 'Failed to get quote')));
			}
		}
	});
}

function fixText(txt) {
    if (typeof(txt) == 'undefined')
        return null;
    return txt;
}