﻿
	// This script is used to pull a form into an existing web page
	// via the custom Ajax Proxy
	
	var objWebForm;
	var objHtmlForm;
	var objXmlHttp = false;
	var IsPostedBack = false;
	var strProxyUrl = '';
	var strFormGuid = '';
	var strSessionId = '';
	var strServerException = '';
	var strHostUrl = '';
	var strLoginGuid = '';
		
	function LoadForm(){
		var objWebForms = document.getElementsByTagName('WebForm');
		if(objWebForms.length == 1){
			objWebForm = objWebForms[0];
			
			// Help
			var blnHelp = objWebForm.getAttribute('Help');
			if (ObjectValid(blnHelp) == true){
				DisplayHelp();
				return;
			}
			
			// Proxy Url
			strProxyUrl = objWebForm.getAttribute('ProxyUrl');
			if (ObjectValid(strProxyUrl) == false){
				alert('The <WebForm> tag is missing a required attribute called ProxyUrl');
				return;
			}
			
			// FormGuid
			strFormGuid = objWebForm.getAttribute('FormGuid');
			if (ObjectValid(strFormGuid) == false){
				alert('The <WebForm> tag is missing a required attribute called FormGuid');
				return;
			}
			
			// The Url of the webpage that is hosting the form
			// This is required in the event the form has a security credential applied
			strHostUrl = objWebForm.getAttribute('HostUrl');
			if (ObjectValid(strHostUrl) == false){
				strHostUrl = '';
			}
			
			// SessionId - This won't be available on the first page load
			strSessionId = RequestQueryString("SessionId");
			
			// LoginGuid - This will be returned if our form requires authentication
			// and the user was successfully authenticated
			strLoginGuid = RequestQueryString("LoginGuid");
			if (strLoginGuid.length > 0){			
				// If we have a SessionId then we have already authenticated
				if (strSessionId.length == 0){
					ProcessFinalAuthentication(strLoginGuid);
					return;
				}
			}
			
			
			// See if we were returned any exceptions from the server
			strServerException = RequestQueryString("Exception");
			if (strServerException.length > 0){
				alert(unescape(strServerException));
			}
	
			PrepareProxyUrl();
			PrepareHtmlForm();
			CallLocalProxy();			
		}
	}
	
	function ProcessFinalAuthentication(LoginGuid){
		// I think I will have perform a POST to the Proxy here.
		var objHtmlForm = document.getElementById("DynamicForm");
		strProxyUrl = strProxyUrl + "?FormGuid=" + strFormGuid + '&LoginGuid=' + LoginGuid;
		if (ObjectValid(objHtmlForm)){
			objHtmlForm.action = strProxyUrl;			
			objHtmlForm.submit();
		}
	}
	
	function PrepareProxyUrl(){		
		if (strSessionId == ''){
			strProxyUrl = strProxyUrl + "?FormGuid=" + strFormGuid;
		} else {
			strProxyUrl = strProxyUrl + "?FormGuid=" + strFormGuid + "&SessionId=" + strSessionId;
		}
		
		if (strHostUrl != ''){
			strProxyUrl = strProxyUrl + "&HostUrl=" + strHostUrl;
		}
	}
	
	function PrepareHtmlForm(){
		var objHtmlForm = document.getElementById("DynamicForm");
		if (ObjectValid(objHtmlForm)){
			//Setting Form Properties other than action does not seem to work			
			objHtmlForm.action = strProxyUrl;
		}
	}
	
	
	// Processes all responses receieved from the DynamicForm Proxy
	function ProcessXmlHttpResults(Html){
		
		var objDiv = document.getElementById('DynamicFormDiv');
	
		// The first thing we need to check is whether or not we have
		// received AuthenticationRequired notification
		if(InStr(Html, '<AuthenticationRequired="True"') != -1){
		
			// Ok, this form requires Authentication
			// Make sure we have a HostUrl or else we cannot tell the Authentication
			// system where to redirect us back to (UrlReferrer cannot be captured from Ajax)
			if (strHostUrl.length == 0){
				alert('The <WebForm> tag is missing a required attribute called HostUrl');
				return;
			}
		
			var GuidStartingIndex = InStr(Html, '<AuthenticationRequired="True"');
			var strCredentialGuid = Html.replace('<AuthenticationRequired="True" Guid="', '');
			strCredentialGuid = strCredentialGuid.replace('" />', '');
			if (strCredentialGuid.length > 0){
				var strAuthenticationUrl = 'https://www-s.admin.uiuc.edu/SSL/OVCR/Authentication/Authenticate.aspx?CredentialGuid=' + strCredentialGuid + '&ReturnUrl=' + encodeURIComponent(strHostUrl);
				//alert(strAuthenticationUrl);
				document.location.href = strAuthenticationUrl;
				//document.location.href = 'http://localhost/DynamicFormManager/Testing/ReferrerTest.aspx?CredentialGuid=' + strCredentialGuid;
			} else {
				objDiv.innerHTML = 'Error: This form requires authentication but the credential required was not returned';
			}
		} else {
			objDiv.innerHTML = Html;
		}
	}
	
	function CallLocalProxy(){
		
		// Mozilla/Safari
		if (window.XMLHttpRequest){
			objXmlHttp = new XMLHttpRequest();
		}
		
		// IE
		else if (window.ActiveXObject){
			objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		objXmlHttp.onreadystatechange = XmlHttpStateChange;		
		objXmlHttp.open('get', strProxyUrl, true);
		//alert(strProxyUrl);
		objXmlHttp.send("");
	
	}
	
	
	function XmlHttpStateChange(){		
		if (objXmlHttp.readyState == 4){
			//alert(objXmlHttp.statusText);
			if (objXmlHttp.status == 200){				
				ProcessXmlHttpResults(objXmlHttp.responseText);
			} else {
				var objDiv = document.getElementById('DynamicFormDiv');
				objDiv.innerHTML = "Problem retrieving Dynamic Form: " + objXmlHttp.status;				
			}
		}
	}
	
	function RequestQueryString(Key){
		var objQuerystring = window.location.search.substring(1);
		var objNameValuePairs = objQuerystring.split("&");
		for (var i = 0; i < objNameValuePairs.length; i++){
			var objNameValuePair = objNameValuePairs[i].split("=");
			if (objNameValuePair[0] == Key){
				return objNameValuePair[1];
			}
		}
		return "";
	}
	
	function ObjectValid(Object){
		if (Object == null){return false};
		if (Object == undefined || Object == 'undefined'){return false};
		return true;
	}
	
	function Trim(Value) {
		while (Value.substring(0, 1) == ' '){
			Value = Value.substring(1, Value.length);
		}
		while (Value.substring(Value.length-1, Value.length) == ' '){
			Value = Value.substring(0, Value.length-1);
		}
		return Value;
	}
	
	function InStr(SearchString, Value){
		return SearchString.indexOf(Value);
	}
	
	function DisplayHelp(){
		alert('Help is coming soon');
	}
	
	