(function($) {

// RAM / CPU Linkage
var ramCPU = {
  "0.5" : 1,
  "1.0" : 1,
  "1.5" : 2,
  "2.0" : 2,
  "4.0" : 3,
  "8.0" : 4      
};

// baseline Fees (assuming this is based on RAM)

var baseMonthlyFee = 0;
var baseSetupFee = 0;


// delta values for monthly fee

var monthlyFeeDelta = {
  "ram" : {
    "0.5" : +20,
    "1.0" : +35,
    "1.5" : +53,
    "2.0" : +70,
    "4.0" : +200,
    "8.0" : +400       
  },
  
  "storage" : {  
    "30"  : +45,
    "50"  : +62,
    "80"  : +100,
    "100" : +125
  },
  
  "traffic" : {
    "25"  : +38,
    "50"  : +100,
    "75"  : +150,
    "100" : +175,
    "250" : +425        
  },
  
  "os" : {
    "centos_linux"       : 0,
    "windows_web_2003"   : +30,
    "windows_web_2008"   : +35,
    "windows_std_2003"   : +50,
    "windows_std_2008"   : +310
  },

  "db" : {
    "MySQL"       		 : 0,
    "windows_sql_2003_workgroup"   : +115,
    "windows_sql_2003_std"   : +395,
    "windows_sql_2008_web"   : +35,
    "windows_sql_2008_std"   : +399
  },
  
  "state" : {
    "nsw" : 0,
    "qld" : 0
  },
  
  "ip-alloc" : {
    "1"   : 0,
    "6"   : +5,
    "13"  : +10,
    "29"  : +20
  },
  
  "server-management" : {
    "n"   : 0,
    "y"   : +150
  }
  
};

// Fee delta values for setup cost
var setupFeeDelta = {
  "contract-term" : {
    "12" : 0,
    "6"  : +250
  }
};

var buildFields;

function updateFees() {
  
  var monthlyFee = baseMonthlyFee;
  var setupFee = baseSetupFee;
  
  // run through each of the fields, and grab Fee deltas
  buildFields.each( function() {
    monthlyFee += getDelta(monthlyFeeDelta, this);
    setupFee += getDelta(setupFeeDelta, this);
  });
  
  $('#monthly-fee').val("$" + monthlyFee);
  $('#setup-fee').val("$" + setupFee);
}

function updateCPU() {
  $('#cpu').val(ramCPU[$('#ram').val()]);
}

function getDelta(obj, field) {
  if (obj[field.id]) {
    var delta = obj[field.id][$(field).val()];
    
    if (delta) {
      return delta;
    }
  }
    
  return 0;
}

var submissionFields;

function configureServerOnSubmit(e) {
	
	if (!$('#server-build-configure').is(':hidden')) {
		$('#server-build-configure').hide();
		$('#server-contact-us').show();
		
		$('#server-build-new').validate();
		
	} else {
		//submit
			
	
		if (!$('#server-build-new').valid()) {
			
			e.preventDefault();
			return false;
		} 
		
		
		$('#server-contact-us').hide();
		$('#server-loading').show();
		var data = "monthly-fee="+$('#monthly-fee').val()+"&setupFee="+$('#setup-fee').val();
		submissionFields = $('#storage, #traffic, #os, #db, #state, #ip-alloc, #server-management, #contract-term, #first-name, #last-name, #company, #email, #repeat-email, #phone, #mobile, #company-add, #company-add2, #company-suburb, #company-state, #company-postcode, #company-website, #company-industry, #promotional-code, #optin-yes');
		submissionFields.each( function() {
			if (this.id!="optin-yes") data += "&"+this.id+"="+$(this).val();
			else data += "&"+this.id+"="+$(this).attr("checked");
		});
		
		
		$.ajax({
	    	url: "/submit_virt_order.php",    
		    data: data,
		    type: "POST",
		    dataType: "html",
		    cache: false,
		    success: quoteOnSuccess,
		    error: quoteOnError
	  	});
	}

	e.preventDefault();
}

function quoteOnError(XMLHttpRequest, textStatus, errorThrown) {
  alert("Sorry, there was an error submitting your order. Please try again later"); 
  $('#server-build-configure').hide();
  $('#server-loading').hide();
  $('#server-contact-us').show();
  $('#config').focus().select();
}

function quoteOnSuccess(obj) {
 $('#server-loading').hide();
 $('#server-thankyou').show();
}

function serverBuildRetrieveOnSubmit(e) {
  
  $('#bt-retrieve').hide();
  $('#retrieve-loading').show();
  
  $.ajax({
    url: "/retrieve_" + $('#config').val() + '.js', // dummy only, see below    
    type: "GET",
    dataType: "json",
    cache: false,
    success: retrieveOnSuccess,
    error: retrieveOnError
  });
  
  e.preventDefault();
  
  /* 
  
  TODO: The above is a dummy request for testing the client-side. Real request might be more like
        the following, with a server-side script and querystring variables:
  
  $.ajax({
    url: "/retrieve_config.php",    
    data: "config=" + $('#config').val(),
    type: "GET",
    dataType: "json",
    cache: false,
    success: retrieveOnSuccess,
    error: retrieveOnError
  });
  
  */
}

function retrieveOnError(XMLHttpRequest, textStatus, errorThrown) {
  alert("Sorry, the saved configuration could not be retrieved. Check that you entered the configuration name correctly, and try again"); 
  $('#retrieve-loading').hide();
  $('#bt-retrieve').show();
  $('#config').focus().select();
}

function retrieveOnSuccess(obj) {
  buildFields.each( function() {
    if (obj[this.id])
        $(this).val(obj[this.id]);
  });
  
  
  $('#retrieve-loading').hide();
  $('#bt-retrieve').show();

  $('#server-build-retrieve').hide();
  $('#server-build-new').show();


  updateCPU();
  updateFees();
}


$(document).ready( function() {
        
    // setup the new / retrieve radio buttons
    
/*    $("#type-n").click( function(e) {
        $('#server-build-retrieve').hide();
		$('#server-contact-us').hide();
        $('#server-build-new').show();
      }).click();

    $("#type-r").click( function(e) {
        $('#server-build-new').hide();
		$('#server-contact-us').hide();
        $('#server-build-retrieve').show();
        $('#config').focus();
      });*/
  

    $('#server-build-retrieve').hide();
    $('#server-loading').hide();
	$('#server-contact-us').hide();
	$('#server-thankyou').hide();
    $('#server-build-new').show();

    buildFields = $('#ram, #storage, #traffic, #os, #db, #state, #ip-alloc, #server-management, #contract-term');

    // setup price update events
    buildFields.not('#ram').change(updateFees);
    
    $('#ram').change( function() {
        updateCPU();
        updateFees();
    }).change();

//    $('#server-build-retrieve').submit(serverBuildRetrieveOnSubmit);
    $('#server-build-new').submit(configureServerOnSubmit);
			
			
  });


})(jQuery);

