﻿var varRaterType;
var varIsUploaded = false;
var quoteReadOnly = false;
var varCurrentTab;

var CONFIRM_YES = 6;
var CONFIRM_NO = 7;
var CONFIRM_CANCEL = 2;

var menu_button_clicked = 0;
var skipFile = false;
var skipPrint = false;
var skipBindMenu = false;
var Handle_County;
var IsRatesCalced = false;
var ForeignSessionSave = null;

function FileClick(num)
{
	if (skipFile)
	{
		skipFile = false;
		return;
	}

	var hc = Handle_County;
	Handle_County = false;
	
	if (document.getElementById('divFile').style.display == 'none')
	{
		document.getElementById('divFile').style.display = '';
		
		if (document.getElementById('divPrint').style.display != 'none');
		{
			document.getElementById('divPrint').style.display = 'none'				
			HideDropdowns(false);
		}

		if (document.getElementById('divBindMenu').style.display != 'none')
		{
			document.getElementById('divBindMenu').style.display = 'none';
		}

		var z = document.getElementById('spanFile');
		document.getElementById('divFile').style.left = z.offsetLeft;
		document.getElementById('divFile').style.top = 73;
		document.getElementById('divFile').focus();
	}
	else 
	{
		document.getElementById('divFile').style.display = 'none';
	}
	Handle_County = hc;
}

function body_click()
{
	if (document.getElementById('divFile') == null || document.getElementById('divPrint') == null)
	{
		return;
	}

	if (document.getElementById('divFile').style.display == '' && menu_button_clicked == 0)
	{
		FileClick();
		if(menu_button_clicked == 1)
		{
			skipFile = true;
			menu_button_clicked == 0;
		}
		document.getElementById('divFile').style.display = 'none';
	}
	
	if (document.getElementById('divPrint').style.display == '' && menu_button_clicked == 0)
	{
		PrintClick();
		if(menu_button_clicked == 2)
		{
			skipPrint = true;
			menu_button_clicked == 0;
		}
		document.getElementById('divPrint').style.display = 'none';
		HideDropdowns(false);
	}

	if (document.getElementById('divBindMenu').style.display == '' && menu_button_clicked == 0)
	{
		if(menu_button_clicked == 3)
		{
			skipBindMenu = true;
			menu_button_clicked == 0;
		}
		document.getElementById('divBindMenu').style.display = 'none';
		HideDropdowns(false);
	}

	HideDropdowns(false);
}

function HideDropdowns()
{
}


function GetDDLSelection(id)
{
	var ddl = document.getElementById(id);
	return ddl.options[ddl.selectedIndex].value;
}

function CutDigits()
{
  if (!event.shiftKey)
  {
		if (event.keyCode >= 48 && event.keyCode <= 57) //0-9
			event.returnValue = true;
		else if (event.keyCode >= 96 && event.keyCode <= 105)//0-9 numpad
			event.returnValue = true;
		else if (event.keyCode == 8) //backspace
			event.returnValue = true;
		else if (event.keyCode == 9) //tab
			event.returnValue = true;
		else if (event.keyCode == 37) //left arrow
			event.returnValue = true;
		else if (event.keyCode == 39) //right arrow
			event.returnValue = true;
		else if (event.keyCode == 46) //Del
			event.returnValue = true;
    else 
			event.returnValue = false;
	}
	else
	{
		if (event.keyCode == 37) //left arrow
			event.returnValue = true;
		else if (event.keyCode == 39) //right arrow
			event.returnValue = true;
		else if (event.keyCode == 46) //Del
			event.returnValue = true;
		else if (event.keyCode == 9) // tab
			event.returnValue = true;
		else 
			event.returnValue = false;
	}
}

var invalid_date = false;
function CheckDate(inp)
{
	invalid_date = false;
	if (!(inp.id == 'txtEffDate' && inp.value == ''))
	{
		inp.value = ChangeByMask(inp);
	}
	if (inp.id == 'txtEffDate' || (inp.value != '' && inp.value != '__/__/____'))
	{
		var d = Date.parse(inp.value);
		if (!d)
		{
			invalid_date = true;
			alert('Invalid date entered!\nDate must be MM/DD/YYYY');
			inp.focus();
		}
	}
}

function ShowBindMenu()
{
	var w = screen.availWidth - 5;
	var h = screen.availHeight - 25;

	if (skipPrint)
	{
		skipPrint = false;
		return;
	}

	if (document.getElementById('divBindMenu') != null)
	{
		if (document.getElementById('divBindMenu').style.display == 'none')
		{
			document.getElementById('divBindMenu').style.display = '';
			HideDropdowns(true);
		}

		if (document.getElementById('divFile').style.display != 'none')
		{
			document.getElementById('divFile').style.display = 'none';
		}

		if (document.getElementById('divPrint').style.display != 'none')
		{
			document.getElementById('divPrint').style.display = 'none';
		}

		var x = document.getElementById('Head_tdBindPolicy');
		document.getElementById('divBindMenu').style.left = x.offsetLeft;
		document.getElementById('divBindMenu').style.top = 73;

		document.getElementById('divBindMenu').focus();			
	}		
}

function PrintClick()
{
	var w = screen.availWidth - 5;
	var h = screen.availHeight - 25;

	if (skipPrint)
	{
		skipPrint = false;
		return;
	}

	if (document.getElementById('divPrint') != null)
	{
		if (document.getElementById('divPrint').style.display == 'none')
		{
			document.getElementById('divPrint').style.display = '';
			HideDropdowns(true);
		}

		if (document.getElementById('divFile').style.display != 'none')
		{
			document.getElementById('divFile').style.display = 'none';
		}
		if (document.getElementById('divBindMenu').style.display != 'none')
		{
			document.getElementById('divBindMenu').style.display = 'none';
		}

		var x = document.getElementById('spanPrint');
		document.getElementById('divPrint').style.left = x.offsetLeft;
		document.getElementById('divPrint').style.top = 73;

		document.getElementById('divPrint').focus();			
	}		
}

function ShowMainMenu()
{
	if (varCurrentTab != 'Start' && !document.getElementById('Head_linkSavequote').disabled)
	{
		var mdl = confirm_ex('Do you wish to save quote')
		if (mdl == CONFIRM_CANCEL)
		{
			return;
		}
		if (mdl == CONFIRM_YES)
		{
			__doPostBack('Head', 'main_menu_save');
		}
		else
		{
			__doPostBack('Head', 'main_menu');
		}
	}
	else
	{
		__doPostBack('Head', 'main_menu');
	}
}

function Header_PrintAppBinder()
{
	document.getElementById('divPrint').style.display = 'none';
	__doPostBack('Head', 'print_hoapp_binder');
}

function Header_PrintHOApp()
{
	document.getElementById('divPrint').style.display = 'none';
	__doPostBack('Head', 'print_hoapp');
}

function Header_PrintQuote()
{
	document.getElementById('divPrint').style.display = 'none';
	__doPostBack('Head', 'print_quote');
}

function DoDisplayPrintQuote()
{
	DisplayDelayedWnd("window.open('PrintQuote.aspx');");
}

function DoShowPrintPreview()
{
	DisplayDelayedWnd("window.open('PrintHOApp.aspx');");
}

function Header_PrintQuote()
{
	document.getElementById('divPrint').style.display = 'none';
	__doPostBack('Head', 'print_quote');
}

function OnBindPolicy()
{
	if (varIsUploaded)
	{
		ShowBindForm();
	}
	else
	{
		document.getElementById('divPrint').style.display = 'none';
		__doPostBack('Head', 'bind_policy');
	}
}

function SaveQuoteAsDef()
{
	document.getElementById('divFile').style.display = 'none';
	__doPostBack('Head', 'save_quote_as_def');
}

function CreateDupQuote()
{
	document.getElementById('divFile').style.display = 'none';
	__doPostBack('Head', 'duplicate_quote');
}

function SaveQuote()
{
	document.getElementById('divFile').style.display = 'none';
	__doPostBack('Head', 'save_quote');
}

function SaveQuoteNew()
{
	document.getElementById('divFile').style.display = 'none';
	__doPostBack('Head', 'save_quote_new');
}

function NewQuote()
{
	document.getElementById('divFile').style.display = 'none';
	if (confirm('This will clear all your entries and reset the rater to your defaults. Are you sure you want to do this?'))
	{
		__doPostBack('Head', 'new_quote');
	}
}

function DoLogout()
{
	if (varCurrentTab != 'Start' && !document.getElementById('Head_linkSavequote').disabled)
	{
		var mdl = confirm_ex('Do you wish to save quote')
		if (mdl == CONFIRM_CANCEL)
		{
			return;
		}
		if (mdl == CONFIRM_YES)
		{
			__doPostBack('Head', 'logout_save');
		}
		else
		{
			__doPostBack('Head', 'logout');
		}
	}
	else
	{
		__doPostBack('Head', 'logout');
	}
}

function Header_SendEmail()
{
	document.getElementById('divFile').style.display = 'none';
	__doPostBack('Head', 'email_quote');
}

function DoEmailQuote()
{
	var z;
	
	var email_url = 'EmailQuote.aspx';
	z = window.showModalDialog(email_url, null, 
		'status=yes;dialogWidth=400px;dialogHeight=322px');
	
	if (z != null)
	{
		alert(z);
	}
}

function DoPrintBinder()
{
	window.open('PrintBinder.aspx?interest=0&conds=');
}

function Header_PrintBinder()
{
	document.getElementById('divFile').style.display = 'none';
	__doPostBack('Head', 'print_binder');
}

function ShowBindForm(tat)
{
	res = window.showModalDialog('BindPolicy.aspx', 
		'', 'dialogWidth:600px; dialogHeight:600px;scroll:no; center:yes; help:no; status:no;');

	if (res != null)
	{
		var ho_app = false;
		var ho_binder = false;
		var j;
		var w = screen.availWidth - 5;
		var h = screen.availHeight - 25;
		var UploadToFinance = false;
		for (j = 1; j < res.length; j++)
		{
			if (res[j] == '$finance$')
			{
				document.getElementById('hdnAction').value = 'bindfinance|';
				UploadToFinance = true;
			}
			else if (res[j] == '$hoapp$')
			{
				ho_app = true;
			}
			else if (res[j] == '$binder$')
			{
				ho_binder = true;
			}
			else
			{
				window.open(res[j], '_blank', 'resizable=1; width=' + w	+ 'px; height=' + h + 'px');
			}
		}
		
		if (ho_app && ho_binder)
		{
			Header_PrintAppBinder();
		}
		else if (ho_app)
		{
			Header_PrintHOApp();
		}
		else if (ho_binder)
		{
			Header_PrintBinder();
		}
		else
		{
			document.location.reload();
		}
	}
	
}

function ShowReqBind()
{
	document.getElementById('divBindMenu').style.display = 'none';
	__doPostBack('Head', 'bind_policy');
}

function DoRequestBind()
{
	var z = window.showModalDialog('MessagePopup.aspx?mode=request_bind', null, 
		'status=yes;dialogWidth=405px;dialogHeight=375px');
	if (z != null)
	{
		document.location.reload();
	}
}

function MakeBind()
{
	document.getElementById('divBindMenu').style.display = 'none';
	var z = window.showModalDialog('MessagePopup.aspx?mode=make_bind', null, 
		'status=yes;dialogWidth=405px;dialogHeight=375px');
	if (z != null)
	{
		document.location.reload();
	}
}

function RefuseBind()
{
	document.getElementById('divBindMenu').style.display = 'none';
	var z = window.showModalDialog('MessagePopup.aspx?mode=refuse_bind', null, 
		'status=yes;dialogWidth=405px;dialogHeight=375px');
	if (z != null)
	{
		document.location.reload();
	}
}

function DoShowAddrPopup()
{
	document.getElementById('divFile').style.display = 'none';
	__doPostBack('Head', 'show_map');
}


function ShowAddrPopup()
{
	var s = document.location.toString();
	if (s.substr(0, 5) == 'https')
	{
		s = s.substr(0, 4) + s.substr(5);
	}	
	var ind = null, i;
	for (i = s.length - 1; i > 0; i--)
	{
		if (s.substr(i, 1) == '/')
		{
			ind = i - 1;
			break;
		}
	}
	if (ind != null)
	{
		var modal = window.showModalDialog(s.substr(0, ind + 2) + 'DistanceMeter.aspx', null, 
			'status=no;dialogWidth=580px;dialogHeight=495px;');
		document.location.reload();
	}
}

function DoRequestException()
{
	document.getElementById('divFile').style.display = 'none';
	__doPostBack('Head', 'request_exception');
}

function DoRefuseException()
{
	document.getElementById('divFile').style.display = 'none';
	__doPostBack('Head', 'refuse_exception');
}

function DoAllowException()
{
	document.getElementById('divFile').style.display = 'none';
	__doPostBack('Head', 'allow_exception');
}


function RequestException(check_premium)
{
	var z = window.showModalDialog('MessagePopup.aspx?mode=request_exception', null, 
		'status=yes;dialogWidth=405px;dialogHeight=355px');
	if (z == '1')
	{
		alert('Your exception request was sent successfully. You will receive an email confirmation shortly.');
		document.location.reload();
	}
}

function AllowException()
{
	var z = window.showModalDialog('MessagePopup.aspx?mode=allow_exception', null, 
		'status=yes;dialogWidth=405px;dialogHeight=355px');
	if (z == '1')
	{
		alert('Your allowing an exception was sent successfully.');
		document.location.reload();
	}
}

function RefuseException()
{
	var z = window.showModalDialog('MessagePopup.aspx?mode=refuse_exception', null, 
		'status=yes;dialogWidth=405px;dialogHeight=355px');
	if (z == '1')
	{
		alert('Your refusing an exception was sent successfully.');
		document.location.reload();
	}
}

function ShowMainHelp()
{
	window.showModalDialog('help.htm', '', 'dialogWidth:650px; dialogHeight:565px;scroll:no; center:yes; help:no; status:yes;');
}

var ForeignSession = '';
var ActionAfterForCalc = '';
function CalcForeignCarrier()
{
	ForeignSession = GetDataFromServer('calc_foreign');
	if (ForeignSession != '')
	{
		ShowClock('Loading...');
	}
}

var run_calc = null;
var fcalc = null;

function CheckForeignCalc()
{
	var s = GetDataFromServer('recheck_foreign&id=' + ForeignSession);
	if (s == '$end$')
	{
		window.clearInterval(run_calc);
		run_calc = null;
		if (fcalc == null && run_calc == null && clock_window != null)
		{
			clock_window.close();
			clock_window = null;
		}
		eval(ActionAfterForCalc);		
	}
	else if (s != '')
	{
		window.clearInterval(run_calc);
		run_calc = null;
		if (fcalc == null && run_calc == null && clock_window != null)
		{
			clock_window.close();
			clock_window = null;
		}

		var arr = s.split('|');
		s = '- ' + arr[0];
		for (var i = 1; i < arr.length; i++)
		{
			s += '\n- ' + arr[i];
		}
		alert('Can not perform this action due following guidelines:\n' + s);
		document.location.reload();
	}
}

function DisplayDelayedWnd(parm)
{
	CalcForeignCarrier();
	if (ForeignSession == '')
	{
		eval(parm);
	}
	else
	{
		ActionAfterForCalc = parm;
		run_calc = window.setInterval('CheckForeignCalc();', 1000);
	}
}


function GetDataFromServer(pars, process_func)
{
	var x = new ActiveXObject("Microsoft.XMLHTTP");
	if (process_func == null)
	{
		x.open('GET', 'DataPage.aspx', false);
	}
	else
	{
		x.open('GET', 'DataPage.aspx', true);
		x.onreadystatechange = process_func;
	}
	
	var rand_val = Math.ceil(Math.random() * 100000);
	x.send('request=' + pars + '&random=' + rand_val);
	
	if (process_func != null) 
	{
		return x;
	}
	
	var ind = x.responseText.indexOf(' ');
	
	if (ind < 0) 
	{
		rand_val = Math.ceil(Math.random() * 900) + 100;
		lastGettingDataTicks = CurrentTicks();
		var old_st = window.status;
		while ((CurrentTicks() - lastGettingDataTicks) < rand_val)
		{
			var z = (CurrentTicks() - lastGettingDataTicks) * 1;
			if (z % 10 == 0) 
			{
				window.status = 'Please wait... ' + Math.ceil(100 * z / rand_val) + '%';
			}
		}
		window.status = old_st;
		lastGettingDataTicks = 0;

		return GetDataFromServer(pars);
	}
	
	if (x.responseText.substr(0, ind) == 'error')
	{
		alert('Internal server error: ' + x.responseText.substr(ind + 1));
		return '';
	}
	
	if (eval(x.responseText.substr(0, ind)) != rand_val) 
	{
		rand_val = Math.ceil(Math.random() * 900) + 100;
		lastGettingDataTicks = CurrentTicks();
		var old_st = window.status;
		while ((CurrentTicks() - lastGettingDataTicks) < rand_val)
		{
			var z = (CurrentTicks() - lastGettingDataTicks) * 1;
			if (z % 10 == 0) window.status = 'Please wait... ' + Math.ceil(100 * z / rand_val) + '%';
		}
		window.status = old_st;
		lastGettingDataTicks = 0;

		return GetDataFromServer(pars);
	}
	
	return x.responseText.substr(ind + 1);
}

var clock_window = null;

function ShowClock(arg)
{
	if (clock_window == null)
	{
		clock_window = window.showModelessDialog('wait.htm',[arg],
			'dialogHeight:120px;dialogWidth:300px;edge:Raised;center:Yes;help:No;resizable:No;status:No');
	}
}

function ShowClock2(arg)
{
	clock_window = window.showModelessDialog('wait.htm',[arg],
		'dialogHeight:120px;dialogWidth:300px;edge:Raised;center:Yes;help:No;resizable:No;status:No');
}

function BeginBlinking()
{
}

var xml_req = null;
function OpenInterboroQuote()
{
	if (xml_req == null)
	{
		ShowClock2("Please wait a moment, your quote info will be transferred and you will be directed to Interboro's direct site.");
		setTimeout('OpenInterboroQuoteHelper()', 1000);
	}
}

function OpenInterboroQuoteHelper()
{
	xml_req = GetDataFromServer('upload_interboro', CheckInterboroUploadRes);
}

function OpenForeignQuote(carr_id, carr_name)
{
	ForeignSessionSave = GetDataFromServer('open_foreign_quote&carrier=' + carr_id);
	if (ForeignSessionSave != '')
	{
		ShowClock("Please wait a moment, your quote info will be transferred and you will be directed to " + carr_name + "'s direct site.");
	}

	save_calc = window.setInterval('CheckForeignSave();', 1000);

	return true;
}

function CheckForeignSave()
{
	var res = GetDataFromServer('check_open_foreign_quote&id=' + ForeignSessionSave);
	if (res == '')
	{
		return;
	}
	clearInterval(save_calc);
	save_calc = null;
	clock_window.close();
	clock_window = null;
	ForeignSessionSave = null;
	if (res.indexOf('fail') == 0)
	{
		alert('Failed to upload:' + res.substr(4));
		if(redirect_to_opened_quote)
		{
			window.close();
		}
	}
	else
	{
		if(redirect_to_opened_quote)
		{
			window.location = res;
		}
		else
		{
			window.open(res);
		}
	}
}

function HideClock()
{
    if (clock_window != null) 
    {
        clock_window.close();
        clock_window = null;
    }
}

function CheckInterboroUploadRes()
{
	var redirect_url="";
	var close_window=false;
	try 
	{ 
    if (xml_req.readyState == 4) 
    {
			if (eval(xml_req.status) == 200) 
			{
				var ind = xml_req.responseText.indexOf(' ');
				var res;
				if (ind	< 0)
				{
					res = xml_req.responseText;
				}
				else
				{
					res = xml_req.responseText.substr(ind + 1);
				}
				
				if (res.substr(0, 4) == 'fail')
				{
					alert('Open quote failed' + res.substr(4));
					if(redirect_to_opened_quote)
					{
						close_window=true;
					}
				}
				else if (res != '')
				{
					if(redirect_to_opened_quote)
					{
						redirect_url = res;
					}
					else
					{
						window.open(res);
					}
				}
			} 
			else 
			{
				alert('Failed to retrieve data: ' + xml_req.statusText);
				if(redirect_to_opened_quote)
				{
					close_window=true;
				}
			}
			xml_req = null;
			if (fcalc == null && run_calc == null && clock_window != null)
			{
				clock_window.close();
				clock_window = null;
			}
			if(redirect_url != "")
			{
				window.location = redirect_url;
			}
			if(close_window)
			{
				window.close();
			}
    }
  }
  catch(e) 
  {
  }
}

function DoShowNotes()
{
	document.getElementById('divFile').style.display = 'none';
	window.showModalDialog('EmailNotes.aspx', null, 
		'status=yes;dialogWidth=600px;dialogHeight=400px');
}

function ShowTechSupport(url, rater)
{
	if (rater != null)
	{
		window.open(url + '?rater=' + rater, '_blank', 'resizable=1; width=580px; height=340px');
	}
	else
	{
		window.open(url, '_blank', 'resizable=1; width=580px; height=340px');
	}
}

function ShowTechSupport2(url, rater, userid, username, email, agency, phone)
{
	window.open(url + '?rater=' + rater + '&username=' + username + '&userid=' + userid + '&email=' + email + '&agency=' + agency + '&phone=' + phone, 
		'_blank', 'resizable=1; width=580px; height=340px');
}


/*@cc_on@*/
/*@if (@_win32 && @_jscript_version>=5)

function confirm_ex(str)
{
    execScript('n = msgbox("' + str + '","4131")', "vbscript");
    return n;
}

function window.confirm(str)
{
    execScript('n = msgbox("' + str + '","4132")', "vbscript");
    return (n == CONFIRM_YES);
}

@end@*/
