var winHV;

function checkBrowser()
{
	//Check OS
	if (navigator.userAgent.indexOf('Windows') == -1)
	{
		alert("ISO HomeValue requires a Microsoft Windows operating system.");		
		return false;
	}

	//Check browser
	var intIndex = navigator.appVersion.indexOf('MSIE');
	var dblVersion = 0;

	if (intIndex != -1)
	{
		dblVersion = navigator.appVersion.substring(intIndex + 5, intIndex + 8);
	}

	if (dblVersion < 5.5)
	{
		alert("ISO HomeValue requires Microsoft Internet Explorer 5.5 or later. Please obtain the latest version of Internet Explorer from your administrator and try again.");
		return false;
	}

	//Check cookies are enabled
	var blnCookiesOK = true;

	if (navigator.cookieEnabled == false)
	{
		blnCookiesOK = false
	}
	else
	{
		document.cookie = 'HVCookieTest'
		if (document.cookie == "") blnCookiesOK = false;
	}

	if (blnCookiesOK == false)
	{
		alert("ISO HomeValue requires the use of cookies. Please enable cookies in Internet Explorer by clicking on Tools, Internet Options. Then, refresh this page and try again.");
		return false;
	}
}

function getBrowserInfo()
{
	var strComponentVersion;

	if (typeof(objClientCaps) == 'undefined')
	{
		if (document.appendChild)
		{
			document.appendChild(document.createElement('<IE:clientCaps id="objClientCaps" style="behavior:url(#default#clientcaps)"/>'));
			strComponentVersion = objClientCaps.getComponentVersion("{89820200-ECBD-11CF-8B85-00AA005B4383}", "ComponentID");
		} else
		{
			strComponentVersion = '';
		}
	}
	
	return navigator.userAgent + ' Build ' +  strComponentVersion + ' Res.: ' + screen.width + 'x' + screen.height; }

function launchHomeValue(strUserName, strPassword, strAgencyName, strCompanyKey, strURL, blnUnderwriter, strGrouping, strXML, strCompanyToken, strFieldToUpdate, blnSameWindow,blnInsured,strUserXML)
{
	if (strUserName == '') return;
	if (checkBrowser() == false) return;	

	var strBrowserInfo = getBrowserInfo();

	if (strPassword == null)
	{
		strPassword = '';
	}

	if (strAgencyName == null)
	{
		strAgencyName = '';
	}

	if (strUserXML == null)
	{
		strUserXML = '';
	}

	if ((!blnUnderwriter) || (blnUnderwriter == null))
	{
		strUnderwriter = "false";
	}
	else
	{
		strUnderwriter = "true";
	}

	if ((!blnInsured) || (blnInsured == null))
	{
		strInsured = "false";
	}
	else
	{
		strInsured = "true";
	}

	if ((typeof(strGrouping) == 'undefined') || (strGrouping == null))
	{
		strGrouping = '';
	}

	if ((typeof(strXML) == 'undefined') || (strXML == null))
	{
		strXML = '';
	}

	if ((typeof(strCompanyToken) == 'undefined') || (strCompanyToken == null))
	{
		strCompanyToken = '';
	}

	if ((typeof(strFieldToUpdate) == 'undefined') || (strFieldToUpdate == null))
	{
		strFieldToUpdate = '';
	}

	if (!blnSameWindow)
	{
		if(typeof(winHV) == 'undefined')
		{	
			winHV = window.open();
		}
		else if(winHV.closed == true)
		{
			winHV = window.open();
		}
		else
		{
			winHV.focus();
			return;
		}
	}
	else
	{
		winHV = window;
	}
	
	with(winHV.document)
	{
		write('<html><body><form name="frmAuth" method="POST" action="https://' + strURL + '/homevaluenet/_login.aspx">');
		write('<input type="hidden" name="strUserName" value="' + strUserName + '"><input type="hidden" name="strPassword" value="' + strPassword + '">');
		write('<input type="hidden" name="strAgencyName" value="' + strAgencyName + '">');
		write('<input type="hidden" name="strUnderwriter" value="' + strUnderwriter + '">');
		write('<input type="hidden" name="strInsured" value="' + strInsured + '">');
		write('<input type="hidden" name="strGrouping" value="' + strGrouping + '">');
		write('<input type="hidden" name="hv" value="hv"><input type="hidden" name="ck" value="' + strCompanyKey + '">');
		write('<input type="hidden" name="XML" value="' + strXML + '">');
		write('<input type="hidden" name="BrowserInfo" value="' + strBrowserInfo + '">');
		write('<input type="hidden" name="CompanyToken" value="' + strCompanyToken + '">');
		write('<input type="hidden" name="FieldToUpdate" value="' + strFieldToUpdate + '">');
		write('<input type="hidden" name="strUserXML" value="' + strUserXML + '">');
		write('<input type="hidden" name="Version" value="3.0">');
		write('</form></body><script type="text/javascript" src="https://' + strURL + '/homevaluenet/_autologin.aspx?hv=homevalue"></script></html>');
	}
}