var OpenSubX = (screen.width/2)-5;
var OpenSubY = (screen.height/2)-100;
var pos = "left="+OpenSubX+",top="+OpenSubY;

var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0;
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5") != -1) ? 1 : 0;
var is_opera = ((navigator.userAgent.indexOf("Opera6") != -1) || (navigator.userAgent.indexOf("Opera/6") != -1)) ? 1 : 0; 
var bName = navigator.appName;
var isNav = (bName == "Netscape");
var ff = false;
if (isNav && navigator.userAgent.indexOf('Firefox') > 0)
{
	ff = true;
}

function getdown()
{
	return false;
}

function OpenCalander(x)
{
		//OpenSub1Window = window.open("calendar.php?id="+x ,"not","width=190,height=165,"+pos+",status=no");
	OpenSub1Window = window.open("calendar.php?id="+x,"not","width=190,height=170,status=no,toolbar=no," + pos  +",directories=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no");
}

function checkFieldVal(fld, txt)
{
	if (fld.value == "")
	{
		if (txt != "")
			alert(txt);
		fld.focus();
		return false;
	}
	return true;
}

function checkFieldRBs(ra, txt)
{

	onechecked = false;
	for (x = 0; x < ra.length; x++)
	{
		if(document.getElementById(ra[x]) == null)
			alert(ra[x] + ' does not exist');
		else
			if (document.getElementById(ra[x]).checked)
				onechecked = true;
	}
	
	if (!onechecked)
	{
		if (txt != "")
			alert(txt)
		document.getElementById(ra[0]).focus();
		return false;
	}
	
	return true;
}

function reset()
{
	var fm = document.frmx;
	fm.submit();
}

function checkSelect(fld, val, efldn)
{
	if (fld == null)
		return;

	if (fld.options[fld.selectedIndex].value == val)
		enableFld(efldn, false);
	else
		enableFld(efldn, true);
}

var g_divdep = new Array();

function addDivDep(fldn, val, showfld)
{
	var fld = getFieldById(fldn);
	var x;
	
	if (fld == null)
		return;
	// if a radio button, add functions to the onclick.
	if (fld.type =='radio')
	{
		for (x=1; x < 15; x++)
		{
			fld = document.getElementById(fldn + x);
			
			if (fld != null)
			{
				str = fld.cclick;
				
				if (str == undefined || str == null)
					str = '';

				if (fld.value == val)
					str += "ShowDiv('" + showfld + "', true);";
				else
					str += "ShowDiv('" + showfld + "', false);";

				if (fld.checked)
					eval(str);

				fld.cclick = str;				
	
				setClick(fld, str);
			}
		}
	}
}

function addSum(fldn, optflds)
{
	var ra = optflds.split(',');
	var x= 0;
	var fld;
	for(x=0; x< ra.length; x++)
	{
		fld = getFieldById(ra[x]);
		if (fld != null)
		{
			txt = fld.getAttribute('onblur');
			if (txt == null)
				txt = '';
			txt += "SumVals('" + fldn + "','" + optflds + "');";
			fld.setAttribute('onblur', txt);
		}
	}
}

function addDep(fldn, val, showfld)
{
	fld = getFieldById(fldn);
	var x;
	if (fld == null)
		return;
	// if a radio button, add functions to the onclick.
	if (fld.type =='radio')
	{
		for (x=1; x < 15; x++)
		{
			fld = document.getElementById(fldn + x);
			
			if (fld != null)
			{
				str = fld.cclick;
				
				if (str == undefined || str == null)
					str = '';

				if (fld.value == val)
					str += "enableFld('" + showfld + "', false);";
				else
					str += "enableFld('" + showfld + "', true);";

				// need to go ahead and initialize the dependant field. 
				if (fld.checked)
					eval(str);
				fld.cclick = str;	
				setClick(fld, str);
			}
		}
	}
	else if (fld.type == 'checkbox')
	{
				str = fld.cclick;
				
				if (str == undefined || str == null)
					str = '';

				str += "ischecked('" + fldn + "','" + showfld + "');"; //('" + showfld + "', false);";

				// need to go ahead and initialize the dependant field. 
				if (fld.checked)
					eval(str);
				fld.cclick = str;	
				setClick(fld, str);		
	}
	else if (fld.type == 'select-one')
	{
		sval = fld.options[fld.selectedIndex].value;
		str = getChange(fld);
		if (str == undefined || str == null)
			str = '';
		
		str += "checkSelect(this,'" + val + "','" + showfld + "');";

		setChange(fld, str);
		checkSelect(fld, val, showfld);
	}
}

function CheckField(fldn, txt, parentdiv)
{
	fld = getFieldById(fldn);
	
	if (fld == null)
		return;
	
	if (parentdiv != '')
	{
		div = getFieldById(parentdiv);
		
		if (div != null)
		{
			if (div.style.display == 'none')
				return true;
		}
	}
	hasval = false;
	// if a radio button, add functions to the onclick.
	if (fld.type == 'radio')
	{
		for (x=1; x < 15; x++)
		{
			fld2 = document.getElementById(fldn + x);
			
			if (fld2 != null)
			{
				if (fld2.disabled == true)
				{
					hasval = true;
					x = 15;
				}
					
				if (fld2.checked)
					hasval = true;
			}
		}
	}
	else if (fld.type == 'text' || fld.type=='textarea')
	{
		if (fld.value != '' || fld.disabled == true || fld.visible == false)
			hasval = true;
		
	}
	else if (fld.type == 'select-one')
	{
		if (fld.disabled == true || fld.selectedIndex > 0)
			hasval = true;
	}
	
	if (!hasval)
	{
		alert('"' +txt + '" is a required field.');
		fld.focus();
	}
	
	return hasval;
}

function getFieldById(fldn)
{
	var tfld = document.getElementById(fldn);
	
	if (tfld == null)
		tfld = document.getElementById(fldn+'1');
	
	return tfld;
}

function setClick(fld, str)
{
	if (window.attachEvent) // is IE browser
		eval ("fld.onclick = function(){"+ str + "};");
	else
		fld.setAttribute('onclick', str);
}

function setChange(fld, str)
{
	if (window.attachEvent) // is IE browser
		eval ("fld.onchange = function(){"+ str + "};");
	else
		fld.setAttribute('onchange', str);
	
	fld.cchange = str;
}

function getChange(fld)
{
	var str;
	if (window.attachEvent) // is IE browser
		str = fld.cchange;
	else
		str = fld.getAttribute('onchange');
	
	if (str == undefined)
		str = fld.getAttribute('onchange');
	return str;
}

function enableFld(fldn, isV)
{
	var fld = document.getElementById(fldn);
	var x;
	if (fld == null)
		fld = document.getElementById(fldn+'1');

	if (fld.type == undefined)
	{
		if (isV == false)
			fld.style.visibility="visible";
		else
			fld.style.visibility="hidden";
	}
	else if (fld.type == 'text' || fld.type == 'select-one' || fld.type=='textarea')		
		fld.disabled = isV;
	else if (fld.type =='radio')
	{
		for (x=1; x < 15; x++)
		{
			fld = document.getElementById(fldn + x);
			
			if (fld != null)
			{
				fld.disabled = isV;				
			}
		}
	}
}

function ischecked(fldn, showfld)
{
	var fld = document.getElementById(fldn);

	if (fld == null)
		return;

	if (fld.checked)
		enableFld(showfld, true);
	else
		enableFld(showfld, false);
}


window.onerror = null;

//BLINKING
function ShowDiv(divname, bShow)
{
	if (bShow)
		document.getElementById(divname).style.display="inline";
	else
		document.getElementById(divname).style.display="none";
		
	return;
}

function setparent(txt)
{
	var layer1 = "parentName";

	if (NS4 || IE4) 
	{
			eval(layerRef+'["'+layer1+'"].innerHTML="\" + txt + \""');
	}
}

function lossCheck(fld)
{
	document.frmx.LossNum.disabled = fld.checked;
//	document.frmx.Lossexperience.disabled = fld.checked;
}
var keyCode = '';
var desiredFmt = '';

function keyPress(e)
{
	keyCode = e == null ? window.event.keyCode : e.charCode;
	if (e != null && e.charCode == null)
		keyCode = e.keyCode;
}

function keyPressPhone(fld)
{
	fmtPhone(fld); 

	if (keyCode == 0)
		return true;
	return false;
}

function keyPressZip(fld)
{
	fmtZip(fld); 

	if (keyCode == 0)
		return true;
	return false;
}
function keyPressDate(fld)
{
	fmtDate(fld); 

	if (keyCode == 0)
		return true;
	return false;
}

function keyPressNumeric(fld)
{
	fmtNumber(fld); 

	if (keyCode == 0)
		return true;
	return false;
}

function keyPressState(fld)
{
	fmtState(fld);
	return true;
}

function fmtPhone(fld)
{
	return formatNumValue(fld, '(999)999-9999[x9999]');
}
function fmtZip(fld)
{
	return formatNumValue(fld, '99999[-9999]');
}
function fmtDate(fld)
{
	return formatNumValue(fld, '99/99/9999');
}
function fmtNumber(fld)
{
	return formatNumValue(fld, '9[9999999]');
}
function fmtState(fld)
{
	if (window.event != null)
		event.returnValue = false;

	var fval = fld.value;

	if (fval.length >= 2)
		return true;

	if (keyCode != 0 && keyCode != null)
	{
		fval += String.fromCharCode(keyCode);
		keyCode = null;
	}
	var tval = "";
	var x;
	var f;
	var isvalid = false;
	var inoptional = false;
	var reqcount = 0;
	if (fval == "")
		return true;

	fval = alphaOnly(fval);

	fval = fval.toUpperCase();
	fld.value = fval;

	return true;
}

function keepKey(fld)
{
	if (!ff)
		return true;
	if (window.event != null)
		event.returnValue = false;

	var character = '';
	if (keyCode != 0 && keyCode != null)
	{
		character = String.fromCharCode(keyCode);
		keyCode = null;
	}
	insertAtCursor(fld, character);
	return true;
}

function insertAtCursor(myField, myValue) 
{
  //IE support
  if (document.selection) 
  {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') 
  {
	var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;

    myField.value = myField.value.substring(0, startPos) + myValue
             + myField.value.substring(endPos, myField.value.length);
  } 
  else 
  {
  	myField.value += myValue;
  }

}
function Validate(fld, isvalid)
{
	if (isvalid)
		return true;

	alert('Field format incorrect. Format should be:' + desiredFmt);
	fld.focus();
	return false;
}
// format a numeric value field. 
// 9 stands for a numeric only value. required unless in []
// [] indicates optional values (not required) - must come at end of string.
// eg phone # with extentsion might be '(999)999-9999[x9999]
function formatNumValue(fld, fmt)
{
//	if (window.event != null)
//		event.returnValue = false;

	//var fval = fld.value;  
	var character = '';	
	if (keyCode != 0 && keyCode != null)
	{
		character = String.fromCharCode(keyCode);
	//	fval += String.fromCharCode(keyCode);
		keyCode = null;
	}
	if (character != '')
		insertAtCursor(fld, character);
	
	fval = fld.value;
	var tval = "";
	var x;
	var f;
	var isvalid = false;
	var inoptional = false;
	var reqcount = 0;
	if (fval == "")
		return true;

	fval = stripNaN(fval);

	x = 0;
	for(f=0; f < fmt.length; f++)
	{
		if (fmt.charAt(f) != '9')
		{
			if (fmt.charAt(f) != '[' && fmt.charAt(f) != ']')
			{
				if (x < fval.length)
					tval += fmt.charAt(f);
			}
			else
				inoptional = !inoptional;
		}
		else
		{
			if (!inoptional)
				reqcount++;

			if (x < fval.length)
			{
				tval += fval.charAt(x++);
			}
		}
	}

	if (reqcount <= fval.length || tval == '')
		isvalid = true;
	else
		desiredFmt = fmt;
	fld.value = tval;

	return isvalid;
}

function stripNaN(fldVal)
{
   var x;
   var strTmp;
   
   strTmp = ''
	for (x = 0; x < fldVal.length; x++)
	{
		if ((isNaN(fldVal.charAt(x)) == false && fldVal.charAt(x) != ' '))
			strTmp = strTmp + '' + fldVal.charAt(x)
	}
	return strTmp;
}
function alphaOnly(strval)
{
	return strval.replace(/[^A-Za-z ]+/g, '');
}

var ajaxCall = null;
var recount = 0;
function AJAXCall(url, returnfx)
{
	if (ajaxCall != null) // if we are currently calling the javascript
	{
		if (recount < 5)
		{ 
			recount++;
			setTimeout(function(){AJAXCall(url, returnfx)}, 1000);//, fullurl, returnfx);
			return;
		}
	}
	recount = 0;
	ajaxCall = returnfx;
	
	xmlHttp = GetXmlHttpObject(AJAXRet);
	url += '&' + Date();
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}

function AJAXRet()
{
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') 
    {
		if (ajaxCall != null)
		{
			ajaxCall(xmlHttp.responseText); // nothing should get returned.
			ajaxCall = null;
		}
	}
}
function GetXmlHttpObject(handler) {
    var objXmlHttp = null;
    if (is_ie) {
        var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';
        try {
            objXmlHttp = new ActiveXObject(strObjName);
            objXmlHttp.onreadystatechange = handler;
        }
        catch (e) {
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled');
            return;
        }
    }
    else if (is_opera) {
        alert('Opera detected. The page may not behave as expected.');
        return;
    }
    else {
        objXmlHttp = new XMLHttpRequest();
        objXmlHttp.onload = handler;
        objXmlHttp.onerror = handler;
    }
    return objXmlHttp;
} 

function toggleDIV(divname, setto)
{	//warning: do not use debug_ within this function
	var fld = document.getElementById(divname);

	if (fld == null)
	return;

	if (isNav) 
	{
        scrl = window.pageYOffset;
        if (scrl > 0)
			fld.style.top += scrl + 'px';
    }
    else 
    {
        var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
        scrl = iebody.scrollTop;
        if (scrl > 0)
			fld.style.pixelTop += scrl;
    }

    if (setto == null)
    {
		if (fld.style.display != 'block')
			setto = 'block';
		else
			setto = 'none';
	}

	fld.style.display = setto;
	return;
}

function showText(textval)
{
	var div = document.getElementById('popin');
	
	if (div.style.display == 'block')
		return; // it's already visible. don't move it around.
	
	div.innerHTML = textval;
	tempY += 16;
    if (isNav) 
    {
        div.style.left = tempX + 'px';
        div.style.top = tempY + 'px';
    }
    else 
    {
        div.style.pixelLeft = tempX;
        div.style.pixelTop = tempY;  
    }
	
	toggleDIV('popin', 'block');
}

function hideText()
{
	toggleDIV('popin', 'none');
}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  return true
}

function DeducCalc(fldname)
{
	var calc = fieldval(fldname, 0);
	
	if (calc == 0)
		calc = fieldval(fldname+'00', 0);

	document.getElementById(fldname+'01').innerHTML = '$' + FmtNum(calc * 0.025);
}

function SoftCalc()
{
	var calc = parseInt(0.0);
	
	calc += fieldval('xtra_loanint', 0);
	calc += fieldval('xtra_taxes', 0);
	calc += fieldval('xtra_consult', 0);
	calc += fieldval('xtra_legal', 0);
	calc += fieldval('xtra_premium', 0);
	calc += fieldval('xtra_advert', 0);
	calc += fieldval('xtra_rentinc', 0);
	
	document.getElementById('ttl_soft_costs').innerHTML = '$' + FmtNum(calc);
}

function SumVals(fldn, opts)
{
	var calc = parseInt(0.0);
	var x;
	ra = opts.split(',');
	for (x = 0; x < ra.length; x++)
		calc += fieldval(ra[x], 0);
	
	document.getElementById(fldn).innerHTML = '$' + FmtNum(calc);
}

function fieldval(fldn, def)
{
	fld = document.getElementById(fldn);

	if (fld == null)
		return parseInt(def);
	
	if (fld.type == 'select-one')
		fval = fld.options[fld.selectedIndex].value;
	else
		fval = fld.value;
	fval = stripNaN(fval);
	
	if (fval == '')
		return parseInt(def);
	
	return parseInt(fval);
}

function FmtNum(val)
{
   var m = 0;
   var y,x;
   var tmpstr = "";
	val = '' + val;

	x = val.indexOf('.');
	
	if (x>=0)
	{
		val = val.substring(0,x);
	}
   //--------------------------------------------------------------------------
   // 1. Remove all leading zeros by finding last leading zero then reassign 
   //    variable to everything right of that zero.
   // 2. work the string backwards from the right, inserting commas at every 
   //    third number. 
   //--------------------------------------------------------------------------
   for (x=0; val.charAt(x) == '0';x++)
   ;
   val = val.substr(x)
   
   x = val.length
   for(y=x; y>=1;y--)
   {
      m +=1
      tmpstr = val.charAt(y-1) + tmpstr
      if (m==3)
      {
         if (y > 1)
            tmpstr = "," + tmpstr
         m = 0
      }
   }
   
   return tmpstr
}

var rspan = '';
function xmlGetZipcode(fldn)
{
	fld = document.getElementById(fldn);
	
	if (fld == null)
		return;
	
	rspan = fldn + '_span'; // the span should be named similar to the field. 
	
	val = fld.value;
	
	if (val == '')
		return;

	page = 'XmlHttp/xmlZipSpan.php?zipcode=' + val
	
	AJAXCall(page, xmlsetspan);
}

function xmlsetspan(stream)
{
//	SetText('Debug', stream);
//	alert(stream);

	eval(stream)
}
// ----------------------------------------------------------------------------
// SetText - populates text in a division.
// ----------------------------------------------------------------------------
function SetText(block, txt) {
    if (txt.indexOf("<!DOCTYPE html ") > -1)
        return;
    
    document.getElementById(block).innerHTML = txt;
}
