//Function below is used to open new window passing both the target URL and failure authentication URL
function cacLogin(targetFailureURL,targetURL) {
	//target after authenticated.
	var newTarget = new String(targetURL);
	newTarget = newTarget.replace(/:81/g,"");
	newTarget = newTarget.replace(/=/g,"%3D");
	newTarget = newTarget.replace(/http/g,"http");
	//target if authentication fails.
	var failedTarget = new String(targetFailureURL);
	failedTarget = failedTarget.replace(/:81/g,"");
	failedTarget = failedTarget.replace(/=/g,"%3D");
	
	//failedTarget = failedTarget.replace(/http/g,"https");"https://apps.usma.edu/(website)PKI/portal/forms_client.cfm For dev use https://usmapki/forms_client.cfm Only used if the site you are going to is https width=350,height=150 https://apps.usma.edu/(website)PKI/portal/forms_client.cfm
	var cacWin = window.open("https://apps.usma.edu/(website)PKI/portal/forms_client.cfm?client_fail=" + failedTarget + "&client_name=LIB&client_URL=" + newTarget, "cacLogin", "width=350,height=150,scrollbars=yes,resizeable=yes");
	cacWin.focus();
}

//This is used if to check if the user has already attempted to authenticate against AD. 		             
//Closing the browser is essential to ensure the certificate is available during an authentication check.
function checkIfAuthenticated(){
	var args = parseQueryString(location.search);
	var login = args["login"];
	if(login=='Failed'){		  
		if(confirm('Authenitcation failed\n\nYou must close the browser to continue.')==true){		
			self.close();
			return true;
		}
		else{	
			self.close()
			return true;	
		}
	}
	return true;
}
//Used to parse the query string and replaces http with https to ensure SSL when authenticating the user.
function parseQueryString (str) {
  str = str ? str : location.search;
  var query = str.charAt(0) == '?' ? str.substring(1) : str;
  var args = new Object();
  if (query) {
	var fields = query.split('&');
	for (var f = 0; f < fields.length; f++) {
	  var field = fields[f].split('=');
	  args[unescape(field[0].replace(/\+/g, ' '))] =
	  unescape(field[1].replace(/\+/g, ' '));
	}
  }
  return args;
}