function getSubscriptions(username, password, subscriptionBoxId){
	getSubscriptionsRequest(subscriptionBoxId, username, password);
}

function onLogin(subscriptionBoxId){
	var errorPane = document.getElementById('loginError');
	if(errorPane){
		errorPane.style.height = '1px';
		errorPane.style.padding = '0px';
		errorPane.innerHTML = '';
	}

	var subscriptionid = document.getElementById(subscriptionBoxId).value;
	if(subscriptionid){
		var usernameInput = document.getElementById("username");
		var usernameValue = usernameInput.value;
		usernameInput.value = usernameValue.toLowerCase();
		return true;
	}
	else{
		var username = encodeURIComponent(document.getElementById("username").value.toLowerCase());
		var password = encodeURIComponent(document.getElementById("password").value);
		getSubscriptions(username, password, subscriptionBoxId);
		return false;
	}
}

function addOptions(selectBoxId, ajaxResponse){
	var subscriptionDiv = document.getElementById("subscriptions");
	var subscriptionBox = document.getElementById("subscription");
	var outsideTag = 'subscriptions';
	var insideTag = 'subscription';
	var startIndex = ajaxResponse.indexOf('<' + outsideTag + '>');
	var endIndex = ajaxResponse.indexOf('</' + outsideTag + '>') + outsideTag.length + 3;
	var feedbackContent = '<?xml version="1.0"?>' +	ajaxResponse.substring(startIndex, endIndex);
	var ajaxResponse = Try.these(
		function() { return new DOMParser().parseFromString(feedbackContent, 'text/xml'); },
		function() { var xmldom = new ActiveXObject('Microsoft.XMLDOM'); xmldom.loadXML(feedbackContent); return xmldom; }
	);
	var subscriptionsArray = ajaxResponse.getElementsByTagName(insideTag);
	var currentcounttag = 'currentcount';
	var currentnametag  = 'currentname';
	var currentidtag = 'currentid';
	var remainingspotstag = 'remainingspots';
	var licenseidtag = 'licenseid';
	if(subscriptionsArray.length > 1)
	{
		for(var index = 0; index < subscriptionsArray.length; index++){
			var currentcount = subscriptionsArray[index].getElementsByTagName(currentcounttag)[0].childNodes[0].nodeValue;
			var currentname = subscriptionsArray[index].getElementsByTagName(currentnametag)[0].childNodes[0].nodeValue;
			var currentid = subscriptionsArray[index].getElementsByTagName(currentidtag)[0].childNodes[0].nodeValue;
			var remainingSpotsArray = subscriptionsArray[index].getElementsByTagName(remainingspotstag)[0].childNodes;
			var licenseid = subscriptionsArray[index].getElementsByTagName(licenseidtag)[0].childNodes[0].nodeValue;
			var newOption = document.createElement('option');
			newOption.setAttribute('value', currentid + '_' + licenseid);
			
			if(remainingSpotsArray.length > 0){
				newOption.innerHTML = currentname + ' - (' + remainingSpotsArray[0].nodeValue + ') available ';
			}
			else{
				newOption.innerHTML = currentname;
			}
			
			subscriptionBox.appendChild(newOption);
		}
		subscriptionDiv.style.position = "relative";
		subscriptionDiv.style.visibility = "visible";
	}
	else if (subscriptionsArray.length == 1){
		var currentid = subscriptionsArray[0].getElementsByTagName(currentidtag)[0].childNodes[0].nodeValue;
		var licenseid = subscriptionsArray[0].getElementsByTagName(licenseidtag)[0].childNodes[0].nodeValue;
		var newOption = document.createElement('option');
		newOption.setAttribute('value', currentid + '_' + licenseid);
		newOption.setAttribute('selected', 'selected');
		subscriptionBox.appendChild(newOption);
		document.getElementById('login_form').submit();
	}
	else{
		document.getElementById('login_form').submit();
	}
}

function getSubscriptionsRequest(selectBoxId, username, password){
	var url = 'http://' + window.location.hostname + '/getSubscriptions';

	var getSubscriptionsReq = newHttpRequest();

	var handlerFunction = getReadyStateHandler(getSubscriptionsReq , selectBoxId, addOptions);
	getSubscriptionsReq.open("POST", url, true);
	getSubscriptionsReq.onreadystatechange = handlerFunction;
	getSubscriptionsReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
//	alert(url+'?'+'username=' + username + '&password=' + password);
	getSubscriptionsReq.send('username=' + username + '&password=' + password);
}

function newHttpRequest() {

  var xmlreq = false;

  if (window.XMLHttpRequest) {

    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer

      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e1) {

      // Failed to create required ActiveXObject

      try {
	// Try version supported by older versions
	// of Internet Explorer

	xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (e2) {

	// Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }

  return xmlreq;
}

function getReadyStateHandler(req, divID, responseHandler) {

  // Return an anonymous function that listens to the
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {

      // Check that a successful server response was received
      if (req.status == 200) {

	responseHandler(divID, req.responseText);

      } else {

	// An HTTP problem has occurred

      }
    }
  }
}