
	function processCaret(e)
	{
		var event = (window.event) ? window.event : e;
		var keycode =(event.keyCode) ? event.keyCode : e.which;	
		if (keycode == 37 || keycode == 39 ) //arrow left / right
		{
			var t = event.srcElement ? event.srcElement : event.currentTarget;
			var len = t.value.length;
			var p = getCaretPosition(t);
			if ( keycode == 37 )
			{
				if (p > 0 ) setCaretPosition(t, p-1);
			}
			else {
				if (p < len) setCaretPosition(t, p+1);
			}
		}
		
		
	}
	function getCaretPosition (ctrl) {
	
		var CaretPos = 0;
		// IE Support
		if (document.selection) {
			ctrl.focus ();
			var Sel = document.selection.createRange ();
			Sel.moveStart ('character', -ctrl.value.length);
			CaretPos = Sel.text.length;
		}
		// Firefox support
		else if (ctrl.selectionStart || ctrl.selectionStart == '0')
			CaretPos = ctrl.selectionStart;
	
		return (CaretPos);
	}
	
	
	function setCaretPosition(ctrl, pos)
	{
	
		if(ctrl.setSelectionRange)
		{
			ctrl.focus();
			ctrl.setSelectionRange(pos,pos);
		}
		else if (ctrl.createTextRange) {
			var range = ctrl.createTextRange();
			range.collapse(true);
			range.moveEnd('character', pos);
			range.moveStart('character', pos);
			range.select();
		}
	}
	
	
	function getcartamount()
	{
		$.ajax({
		  url: "xls.shop.php",
		  cache: false,
		  dataType: 'html',
		  data: {REQ: 'GET_CARTFLOAT'},
		  success: function(_html){
			$("#cartfloater").html(_html);
		  }
		});
	}
	
	function cartfloater( action , args)
	{
		if (typeof(action) == 'undefined') action = 'total';
		if (typeof(args) == 'undefined') args = '';
		
		$.ajax({
		  url: "xls.shop.php",
		  cache: false,
		  async : true,
		  dataType: 'html',
		  data: {REQ: 'SHOWCART',ACTION: action, ARGS: args },
		  success: function(_html){
				$("#formconf_body").html(_html);
		  }
		});
	}
	
	function cartview( action , args)
	{
		if (typeof(action) == 'undefined') action = 'view';
		if (typeof(args) == 'undefined') {
			args = '';
			$(":input").each(function(i){		
				if(this.name == "") return;
				if (this.type =='radio' && this.checked == false) this.value=0;
				if (this.type =='checkbox' && this.checked == false) {
					this.value = 0;
				}

				args = args + '&' + this.name + '=' + this.value;
			});
		}
		
		$.ajax({
		  url: "xls.shop.php",
		  cache: false,
		  async : true,
		  dataType: 'html',
		  data: {REQ: 'SHOWCART',ACTION: action, ARGS: args },
		  success: function(html){
			$("#cartview").html(html).fadeIn(200);
			cartfloater();
			window.getcartamount();
		  }
		});
		
	}
		
	
	jQuery(document).ready(function() 
	{
		getcartamount();
		
		// ------------------------------------------------
		// homepage blocks
		// ------------------------------------------------
		$('.blockliter').bind('mouseenter', function(e) {	
			$('#blockheader', $(this)).css({
				'background-position': '0px -27px'
				,'text-decoration' : 'none'
			});
			$('p,i,span', $(this)).each(function(i){
				$(this).addClass('orange');
			});
		});

		$('.blockliter').bind('mouseleave', function(e) {	
			$('#blockheader', $(this)).css({
				'background-position': '0px 0px'
			});
			$('p,i,span', $(this)).each(function(i){
				$(this).removeClass('orange');
			});
		});

		$('.blockliter').bind('click', function(e) {	
			$('#blockheader', $(this)).css({
				'background-position': '0px -54px'
			});
		});

	});
	

	// =============================================================	
	// functions for the form
	// =============================================================
	
	function load_formcontent(_id, _step)
	{
		$.ajax({
		  url: "xls.shop.php",
		  cache: false,
		  dataType: 'html',
		  data: {ID: _id, REQ: 'BASE'},
		  beforeSend: function(){
			$("#formcontent").html('<img src="/images/interface/throbber.gif">').fadeIn(1000);
		  },
		  success: function(html){
			$("#formcontent").html(html);
			$("#formcontent").show();
		  }
		});
	}
	
	function check_mainbuts( _id )
	{
		$('#mainbutholder').children().each(function(i) {
			if ($(this).hasClass(_id)) {
				$(this).removeClass('mainbutup');
				$(this).addClass('nohover');
				$.hasopa = true;
			}
			else {
				$(this).addClass('mainbutup');
				$(this).removeClass('nohover');
			}
		});
	}
	function next_step(_step)
	{
	
		var ff = ''; 
		$(":input").each(function(i){
			if (this.type =='radio' && this.checked == false) return;
			if (this.type =='checkbox' && this.checked == false) {
				return;
			}
			ff = ff + '&' + this.name + '=' + this.value;
		});
	
		$.ajax({
		  url: "xls.shop.php",
		  cache: false,
		  dataType: 'html',
		  data: {ACTION: 'NEXT_STEP', step: _step,FORMVALS: ff},
		  success: function(html){
			$("#formcontent").html(html);
		  }
		});
	
	}
	
	function send_command( cat , command, _async )
	{
		if(typeof(_async) == 'undefined') _async = true;
		$.ajax({
		  url: "xls.shop.php",
		  cache: false,
		  async: _async,
		  dataType: 'html',
		  data: {ID: cat , REQ: 'COMMAND', ACTION: command },
		  success: function(html){
			$("#executioner").html(html);
		  }
		});
	}
	function cart_senddata( req , _data, _async )
	{
		if(typeof(_async) == 'undefined') _async = true;
		$.ajax({
		  url: "xls.shop.php",
		  cache: false,
		  async: _async,
		  dataType: 'html',
		  data: {REQ: req, DATA: _data },
		  success: function(html){
		  	var t = new Date();
		  	t = t.getTime();
			$("body").append("<div id='"+t+"'>"+html+"</div>");
		  	//$("#executioner").html();
			//$("#executioner").html(html);
			$("#"+t).remove();
		  }
		});
	}	

	function cart_sendredirect( req , _data, _async )
	{
		if(typeof(_async) == 'undefined') _async = true;
		$.ajax({
		  url: "xls.shop.php",
		  cache: false,
		  async: _async,
		  dataType: 'html',
		  data: {REQ: req, DATA: _data },
		  success: function(html){
			$("#executioner").html(html);
			document.location.href="/xls-vps-bestellen.html";
		  }
		});
	}	
	
	function add_cart( _id , async)
	{
		if(typeof(async) == 'undefined') async = true;
		do_cart( _id , 'UPDATE_CART', 'ADD', async);
	}
	function toggle_cart( _id , async)
	{
		if(typeof(async) == 'undefined') async = true;
		do_cart( _id , 'UPDATE_CART', 'ADD', async, true);
	}

	function do_cart( _id, _req, _act, _async, _toggle )
	{
		if(typeof(_async) == 'undefined') _async = true;
		$.ajax({
		  url: "xls.shop.php",
		  cache: false,
		  async: _async,
		  dataType: 'html',
		  data: {ID: _id, REQ: _req ,ACTION: _act, TOGGLE: _toggle},
		  success: function( _html){
			$("#formconf_body").html(_html);
		  }
		});
	}


	// -----------------------------------------------------
	// widget helpers
	// -----------------------------------------------------
	
	function setslider( _selector, _val, _name  )
	{
		var t = $(_selector).xlslider('setslider' , _val, true);
	}
	function setrecalibrated( _selector, _val, _vps  )
	{
		$(_selector).xlslider('setrecalibrated' , _val, true, _vps);
	}
	

	var slideritems = {
		'RAM':'.slider_ram' 
		,'HDD':'.slider_hdd' 
		,'TRAFFIC':'.slider_traffic'
	};



	function setsliders( o, updatecores )
	{	
		add_cart('VPS:'+o.VPS, false);
		
		for( var i in slideritems )
		{
			if ( $(slideritems[i]).xlslider('recalibrate' ,o[i] , true ) )
			{
				setslider(slideritems[i] ,o[i] , i);
			}
		}
		
		if(typeof(o.updatecores) != 'undefined') 
		$('.slider_cpucores').xlslider('checkparent', true); // force pointer
	}


	function setradio( args )
	{
		
		if (typeof(args[2]) == 'undefined') return;
		if (typeof(args[0][1]) == 'undefined') return;

		var updateself = true;

		if (typeof(args[3]) != undefined 
			&& parseInt(args[3]) <= parseInt(args[1]))
		{
			updateself = false;
		}
		
		var RADIO = $('#vpsradiogroup');
		var caller = args[2];
		var vpsnr = args[0][1];
		var vpsconfig = RADIO.xlradio('get_data', vpsnr);
		var current_value = null;
		for( var i in slideritems )
		{
			if ( i == args[2] && updateself == false) continue;
			if ( i != args[2] ) 
			{
				current_value = $(slideritems[i]).xlslider('get_current');
			}
			
			$(slideritems[i]).xlslider(
				'recalibrate' , vpsconfig[i], null, current_value
			);
		}
		
		RADIO.xlradio('set_state', vpsnr);
		//add_cart('VPS:'+vpsnr, true);
		if(args[2] == 'RAM') {
			$('.slider_ram').xlslider('checkchildren');
		}		

		// make shure sticky memeber traffic moves with state		
		if(args[2] != 'TRAFFIC') {
			$(slideritems['TRAFFIC']).xlslider(
				'recalibrate' , vpsconfig.TRAFFIC, true
			);
			$('.slider_traffic').xlslider('setslider',vpsconfig.TRAFFIC, true);

		}
	}

	function check_neighbours( obj )
	{
		var RADIO = $('#vpsradiogroup');
		var VPS = RADIO.xlradio('get_index', obj.self, obj.value);
		var current = RADIO.xlradio('get_selected');
		
		var c = 0;
		var t = 6;
		var vpo = {};
		
		for (var i in slideritems)
		{
			vpo[i] = $(slideritems[i]).xlslider('get_current');
			if (i == obj.self) continue; 	
			v = RADIO.xlradio('get_index' ,i ,vpo[i]);
			if (v < t) t = v;
		}

		var low = (t != 0 && t < VPS ? t : VPS);
		var slidercount = RADIO.xlradio('returnlen',vpo);
		
		if (current != false && low > current &&  slidercount == 3) 
		{
			var vpsconfig = RADIO.xlradio('get_data', low);
			for ( var i in slideritems )
			{
				$(slideritems[i]).xlslider(
					'recalibrate' , vpsconfig[i], null, vpo[i]
				);
			}
			RADIO.xlradio('set_state', low);
			//add_cart('VPS:'+low, true);
		}
		
		if(obj.self == 'RAM') {
			checkCores(vpo['RAM']);
			//$('.slider_cpucores').xlslider('checkparent', true);
			$('.slider_ram').xlslider('checkchildren');
		}
	}
	function submitxlradio()
	{
		var c = $('#vpsradiogroup').xlradio("get_selected");
		var d = $('#formcomponents [data-member^="VPS"]').attr('data-member');
		if(parseInt(d.replace("VPS:","")) == c) return;
		add_cart('VPS:'+c, true);
	}
	function checkCores( ram )
	{
		if (typeof(ram) == undefined) return;
		for (var i = 1; i < 6; i++) {
			$('#cores'+i).css({'background-color':'#d2d4d9'});
		}
		if (ram < 1024 ) fillcores(1);
		if (ram >= 1024 && ram < 4096 ) fillcores(2);
		if (ram >= 4096 && ram < 8192) fillcores(3);
		if (ram >= 8192 ) fillcores(4);

	}
	function fillcores( n )
	{		
		for (var i = 1; i < 6; i++) {
			if (i <= n ) $('#cores'+i).css({'background-color':'#fc9d44'});
			else $('#cores'+i).css({'background-color':'#d2d4d9'});
		}	
		$('#coreghz').html((Math.round(n * 220)/100)+' GHz');	
	}

