window.onload = start; 
 
function start()
{
    init_page();
    adjustSidebar();
};
 


var loaderHtml = '<div class="loader"><h3>Please Wait</h3></div>';





function ajaxPostJ(object,url,params,oncomplete) 
{

  $.ajax({ type: "POST", data: params,url: url, context: object, success: function(data, textStatus, XMLHttpRequest){
     		if ( object != null ) 
        {    
          $("#"+object).html(data);
          if(typeof initBindings == 'function') { 
            initBindings();
          }
        }
        if ( oncomplete != null )
    		{
          oncomplete();
        }
      }});

}


function ajaxPost(object,url,params,oncomplete) 
{
  var paramlength = 0;
  if (params != null)
  {
    paramlength = params.length;
  }
  
  var http = null;
  if (window.XMLHttpRequest) {
    // If IE7, Mozilla, Safari, and so on: Use native object.
    http = new XMLHttpRequest();
  }
  else
  {
    if (window.ActiveXObject) {
       // ...otherwise, use the ActiveX control for IE5.x and IE6.
       http = new ActiveXObject('MSXML2.XMLHTTP.3.0');
    }
  }

  http.open("POST", url, true);
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", paramlength);
  http.setRequestHeader("Connection", "close");
  http.send(params);
  

  http.onreadystatechange = function() 
  {
    if(http.readyState == 3) 
    {
      //document.body.style.cursor = "wait";
      //if(loaderHtml) { document.getElementById(object).innerHTML=loaderHtml; }

    }
  	if(http.readyState == 4 ) 
    {
      if ( http.status == 200 )
      {
    		if ( object != null ) 
        { 
          document.getElementById(object).innerHTML=http.responseText; 
          if(typeof initBindings == 'function') { 
            initBindings();
          }
        }
    		//document.body.style.cursor = "default";
    		if ( oncomplete != null )
    		{
          oncomplete();
        }
      } else {
        alert('Ajax error');
      }
	  }
  }
}

function checkout()
{
  ajaxPost('checkout_codes','/cart/ajax/checkout_codes/',null,jump_to_portal);
}

function jump_to_portal()
{
  var params = getParams('checkout_codes');
  //alert('Do not press the \'Back\' button on your browser past this point');
  parent.location='https://portal.fastserv.com/?'+params;
}

function update_quantities()
{
  var params = getParams('cart_wrapper');
  ajaxPost(null,'/cart/ajax/update_quantities/',params,refresh_cart);
}

function cart_add(id)
{
  var params = getParams('order_form');
  ajaxPost(null,'/cart/ajax/cart_add/'+id+'/',params,jump_to_cart);
  //ajaxPost(null,'/cart/ajax/cart_add/'+id+'/',params);
  //alert(params);
  //ajaxPost('debug','/cart/ajax/cart_add/'+id+'/',params,null);
}

function edit_item(id,type)
{
  if ( type == 'server' )
  {
    parent.location='/cart/edit-item/'+id+'/';
  } else {
    alert("Cannot edit this item");
  }
}

function remove_item(id)
{
  refresh_cart();
  ajaxPost(null,'/cart/ajax/remove_item/'+id+'/','',refresh_cart);
}

function jump_to_cart()
{
  parent.location='/cart/';
}

function refresh_cart()
{
  ajaxPost('cart','/cart/ajax/cart_view/','');
  ajaxPost('order_summary','/cart/ajax/cart_summary/','');
}

function init_page()
{
  reload_summary();
  
}

function package_changed(server)
{
  reload_options(server);
}

function option_changed(option)
{
  reload_summary();
}

function reload_options(server)
{
  var params = getParams('order_form');
  ajaxPost('options','/managed-hosting/ajax/options_all/',params,reload_summary);
}

function os_changed(os)
{
  reload_cp(os);
}

function cp_changed(cp)
{
  reload_summary();
  reload_cp(cp);
}

function reload_summary()
{
  var params;
  if ( document.getElementById('order_summary') != null && document.getElementById('order_form') != null )
  {
    if ( params = getParams('order_form') )
    {
      ajaxPost('order_summary','/managed-hosting/ajax/order_summary/',params);
    }
  }
}

function reload_os(server)
{
  var params = getParams('order_form');
  ajaxPost('operating_systems','/managed-hosting/ajax/os/',params,reload_summary);
}

function reload_cp(os)
{
  var params = getParams('order_form');
  ajaxPost('control_panels','/managed-hosting/ajax/cp/',params,reload_summary);
}

function getRadioValue(radioGroupName) 
{
  radios = document.getElementsByName(radioGroupName);
  for (i = 0; i < radios.length; i++) 
  {
    if (radios[i].checked) return radios[i].value;
  }
}

function getParams(div_id) 
{
  if ( div_id != '' )
  {
    var element;
    if ( element = document.getElementById(div_id))
    {
      var elements = element.getElementsByTagName('*');
      var params = '';
      for (i = 0; i < elements.length; i++) 
      {
        
        if ( elements[i].checked || elements[i].type == 'select-one' || elements[i].type == 'text' || elements[i].type == 'textarea' || elements[i].type == 'hidden' )
        {
          var name =  elements[i].name;
          var value = '';
          
          if ( elements[i].type == 'select-one' )
          {
            var j = elements[i].selectedIndex;
            value = elements[i].options[j].value;
          } else {
            value = elements[i].value;
          }
        
          if (params != '' )
          { 
            params = params+'&'+name+'='+encodeURI(value); 
          } else {
            params = name+'='+encodeURI(value); 
          }
        }
      }
      return params;
    }
  }
}

function adjustSidebar()
{
  if ( document.getElementById('order_summary') != null )
  {
  	var sidebar_height = parseInt( $('#order_summary').height() );
  	var top_offset = 15;
  	var bottom_offset = 0;
  	var margin_top = 260;
  // 	if ($('#billboard').length > 0) {
  // 		margin_top = $('#billboard').outerHeight() + $('#billboard').offset().top - top_offset + 7;
  // 	}
  // 	else {
  // 		// Pages without the billboard visible
  // 		margin_top = $('#navTopPanel').outerHeight() + $('#navTopPanel').offset().top - top_offset + 7;
  // 	}
  
  	// Get the current left offset for the sidebar - we will maintain this.
  	var sidebar_left = $('#order_summary').offset().left - 0;
  	var msie6 = $.browser.msie && $.browser.version.charAt( 0 ) < 7;
  
  	// Adjust whenever a scroll event fires.
  	$(window).scroll(function(){
  		var margin_bottom = parseInt($('#footer_wrapper').offset().top) - sidebar_height - bottom_offset;
  		var top = $(window).scrollTop();
  
  		//Shopping cart at the top
  		if ( top <= margin_top  )
  		{
  			$("#order_summary").css({
  				top: '',
  				left: '',
  			        position: 'absolute'
  			});
  		}
  		else if ( top < margin_bottom )
  		{
  			//Shopping cart in the middle
  			if( msie6 )
  			{
  				// Internet explorer 6 and lower does not have fixed position
  				$("#order_summary").css("position","absolute");
  				var tot = top+top_offset;
  				$("#order_summary").css("top",top+"px");
  			}
  			else
  			{
  				// All other browsers
  				$("#order_summary").css({
  					position: 'fixed',
  					left: sidebar_left+'px',
  					top: top_offset+'px'
  				});
  			}
  		}
  		else
  		{
  			//Shopping cart at the bottom
  			$("#order_summary").css({
  				top: margin_bottom + 'px',
  				position: 'absolute'
  			});
  		}
  	});
  }
}

    

