// Define the URL for use in javaascript functions
// Could we replace this with the PHP's URL variable?
var url = "/quote";//"http://www3.temp.belhard.com";

function note_alert()
{
	if (confirm('You have new notes awaiting your review! View them now?'))
	{
		document.location = url + '/new_notes.php';
	}
}

function show_qta_note_box(whatform,whatval)
{
	// This shows the 'questions to agent' note box if said status is selected.
	if (whatval == 'Questions to Agent')
	{
		// Show the note box
		initial_questions.innerHTML = 
		'<font class="smallfont">Questions to ' + whatform.rcptname.value + ':<br>' + 
		'<textarea name="initial_questions" cols="35" rows="5"></textarea>';
	}
	else
	{
		// Hide the note box
		initial_questions.innerHTML = '';
	}
}

function del_included_form(whatval,whatlist)
{
	whatlist.del(whatval);
	refresh_included_form_list(whatlist);
}

function del_included_formid(whatval,whatlist)
{
	whatlist.del(whatval);
}

function refresh_included_form_list(whatlist)
{
	var i;
	var foo = '';
	if (whatlist.length == 0)
	{
		foo = 'None';
	}
	else
	{
		for (i = 0; i < whatlist.length; i++)
		{
			del_link = '<a href="javascript:del_included_formid(\'' + subformids[i] + '\',subformids);del_included_form(\'' + subformlist[i] + '\',subformlist);"><img src="' + url + '/images/redx.gif" border="0"></a> ';
			thisitem = '<input type="hidden" name="includedformids[]" value="' + subformids[i] + '">' + subformlist[i];
			foo = foo + del_link + thisitem + '<br>';
		}
	}
	includedformslist.innerHTML = foo;
}

function goto(url)
{
	document.location = url;
}

function del_included_broker(whatval,whatlist)
{
	whatlist.del(whatval);
	refresh_included_broker_list(whatlist);
}

function del_included_brokerid(whatval,whatlist)
{
	whatlist.del(whatval);
}

function refresh_included_broker_list(whatlist)
{
	var i;
	var foo = '';
	if (whatlist.length == 0)
	{
		foo = 'None';
	}
	else
	{
		for (i = 0; i < whatlist.length; i++)
		{
			del_link = '<a href="javascript:del_included_brokerid(\'' + subformids[i] + '\',subformids);del_included_broker(\'' + subformlist[i] + '\',subformlist);"><img src="' + url + '/images/redx.gif" border="0"></a> ';
			thisitem = '<input type="hidden" name="includedbrokerids[]" value="' + subformids[i] + '">' + subformlist[i];
			foo = foo + del_link + thisitem + '<br>';
		}
	}
	includedbrokerslist.innerHTML = foo;
}

function openwindow(openurl,width,height)
{
	nw = window.open(openurl,"popup","width="+width+",height="+height+",top=20,left=20,scrollbars=1");
}

function opensubformwindow()
{
	nw = window.open('add_sub_forms.php','popup',"width=500,height=90,top=20,left=20,scrollbars=1");
}

function authquote(theform)
{
	if (theform.forms_assigned.value == 0)
	{
		alert('You must assign forms before you can authorize this quote.');
		return 0;
	}
	theform.action = url + "/admin/addquoteauth.php";
	theform.submit();
}

function editquoteauth(theform)
{
	theform.action = url + "/admin/delquoteauth.php";
	theform.submit();
}

function delquoteauth(theform)
{
	theform.action = url + "/admin/delquoteauth.php";
	theform.submit();
}

function unassignforms(theform)
{
	if (theform.requestid.value == "")
	{
		alert('Error');
		return;
	}
	
	theform.action = url + "/admin/unassign_request_forms.php";
	theform.submit();
}

function assignforms(theform)
{
	if (theform.companyid)
		if (theform.companyid.options.selectedIndex == 0)
		{
			alert('You must select a company.');
			return 0;
		}

	if (theform.lobid)
		if (theform.lobid.options.selectedIndex == 0)
		{
			alert('You must select a line of business.');
			return 0;
		}

	if (theform.coverageid)
		if (theform.coverageid.options.selectedIndex == 0)
		{
			alert('You must select a coverage.');
			return 0;
		}

	theform.action = url + "/admin/assign_forms_to_request.php";
	theform.submit();
}

function format_rate_ovr(whatform)
{
	for (var i = 0; i < whatform.length; i++)
	{
	        if (whatform.elements[i].type == "text") // It's a Text box
	        {
	                if (whatform.elements[i].name.indexOf('rate_ovr') == 0)
	                {
	                        whatform.elements[i].value = formatDecimal(whatform.elements[i].value);
	                }
	        }
	}
}

function sf()
{
	document.loginform.password.focus();
}

function navigate_to(url,is_edit)
{
	if (is_edit == 1)
	{
		if (confirm('Exiting the page edit section without doing "Save These Changes" will result in your request version not being saved. Are you sure you wish to do this?') == 0)
		{
			alert('Excellent!');
			return;
		}
	}

	document.location = url;
}

function editrequest()
{
	document.location = url + "/editrequest.php?requestid=new";
}

function use_request_address(whatform, locationcount)
{
	locationcount = parseInt(locationcount, 10);
	if (locationcount > 0)
	{
		alert('You can only use this feature on the first location of your request.');
		whatform.use_address.checked = false;
		return 0;
	}
	if (whatform.use_address.checked)
	{
		whatform.old_address.value = whatform.address.value;
		whatform.old_city.value = whatform.city.value;
		whatform.old_zip.value = whatform.zip.value;
		whatform.old_state.value = whatform.state.value;
		whatform.address.value = whatform.request_address.value;
		whatform.city.value = whatform.request_city.value;
		whatform.zip.value = whatform.request_zip.value;

		for (var i = 0; i < whatform.state.length; i++)
		{
			if (whatform.state[i].value == whatform.request_state.value)
			{
				whatform.state.selectedIndex = i;
			}
		}
	}
	else
	{
		whatform.address.value = whatform.old_address.value;
		whatform.city.value = whatform.old_city.value;
		whatform.zip.value = whatform.old_zip.value;
		for (var i = 0; i < whatform.state.length; i++)
		{
			if (whatform.state[i].value == whatform.old_state.value)
			{
				whatform.state.selectedIndex = i;
			}
		}	
	}
}

function savesession(whatform,whereat)
{
	whatform.action = "/quote/savesession.php";
	if (whereat == "request")
	{
		if (whatform.name.value.length == 0)
		{
			alert('You must fill out at least the name of the insured before continuing');
			return 0;
		}
	}
	whatform.session_where.value = whereat;
	whatform.submit();
}

function formatCurrency(num)
{
        num = num.toString().replace(/\$|\,/g,'');
        
        if(isNaN(num))
                num = "0";
        
        sign = (num == (num = Math.abs(num)));  
        num = Math.floor(num*100+0.50000000001);
        cents = num%100;
        num = Math.floor(num/100).toString();
        
        if(cents<10)
                cents = "0" + cents;
        
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+
        num.substring(num.length-(4*i+3));
        
        return (((sign)?'':'-') + num + '.' + cents);
}

function formatDecimal(num)
{
        num = num.toString().replace(/\$|\,/g,'');
        
        if(isNaN(num))
                num = "0";
        
        sign = (num == (num = Math.abs(num)));  
        num = Math.floor(num*100+0.50000000001);
        cents = num%100;
        num = Math.floor(num/100).toString();
        
        if(cents<10)
                cents = "0" + cents;
        
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3)) + num.substring(num.length-(4*i+3));
        
        return (((sign)?'':'-') + num + '.' + cents);
}