//=============================================================================
// Set Tab. 
//=============================================================================
var InsertOrder_OrderDetails_Tab = 0;
var InsertOrder_Property_Tab = 1;
var InsertOrder_Buyers_Sellers_Tab = 2;
var InsertOrder_Loans_Mortgage_Tab = 3;
var InsertOrder_RealEstateAgents_Tab = 4;
var InsertOrder_AdditionalServices_Tab = 5;

var EditOrder_OrderDetail_Tab = 0;
var EidtOrder_Property_Tab = 1;
var EditOrder_Buyers_BuyerAttorney_Tab = 2;
var EditOrder_Sellers_Tab = 3;
var EditOrder_Loans_Mortgage_Tab = 4;
var EditOrder_RealEstateAgents_Tab = 5;

//=============================================================================
// Set variable to see if using Netscape browser.
//=============================================================================
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

//=============================================================================
// OPERATION:  autoTab
//
// Handles tabbing to next field automatically when last character in textbox 
// is entered.
// 
// Web Sites:
//		http://members.xoom.com/cyanide_7
//		http://javascript.internet.com
//=============================================================================
function autoTabField(input, e)
{
	var len = input.maxLength;
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode))
	{
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}

	function containsElement(arr, ele) 
	{
		var found = false, index = 0;
		while(!found && index < arr.length)
		if(arr[index] == ele)
		found = true;
		else
		index++;
		return found;
	}
	
	function getIndex(input)
	{
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
		return index;
	}

	return true;
}


function mOvr(src,clrOver,clrFont,msg)
{ 
		window.status = msg
		src.style.cursor = 'hand'; 
		src.style.backgroundColor = clrOver; 
		src.style.color = clrFont;
}

function mOut(src,clrIn,clrFont)
{ 
		window.status = ''
		src.style.cursor = 'default'; 
		src.style.backgroundColor = clrIn;
		src.style.color = clrFont;
}

//=============================================================================
// OPERATION:  checkAll
//
// PARMS:
// checkBoxAll		- instance of checkbox in the header.
// checkBoxItemId	- id of the checkbox in the ItemTemplate
//
// Useful in datagrids to allow user to click checkbox in header
// and it will automatically check/uncheck all the other checkboxes in the column
//
// Note: Make sure checkBoxAllId and checkBoxItemId are not too similar otherwise
//       indexOf function won't work properly.
//=============================================================================
function checkAll( checkBoxAll, checkBoxItemId )
{
	var frm = checkBoxAll.form;
	var checkBoxAllState = checkBoxAll.checked;
	for(i=0; i< frm.length;i++)
	{
		var e = frm.elements[i];
        if(e.type=='checkbox' && e.name.indexOf(checkBoxItemId) != -1)
		{
            e.checked = checkBoxAllState;
		}
	}
}

//=============================================================================
// OPERATION:  checkChanged
//
// PARMS:
// frm				- form containing datagrid
// checkBoxAllId	- id of the checkbox in the HeaderTemplate.
// checkBoxItemId	- id of the checkbox in the ItemTemplate
//
// Useful in datagrids to allow user to click checkbox in column
// and it will automatically check/uncheck checkbox in the header.
//
// Note: Make sure checkBoxAllId and checkBoxItemId are not too similar otherwise
//       indexOf function won't work properly.
//=============================================================================
function checkChanged(frm, checkBoxAllId, checkBoxItemId)
{
	var boolAllChecked = true;
	for(i=0; i< frm.length; i++)
	{
		var e = frm.elements[i];
        if (e.type == 'checkbox' && e.name.indexOf(checkBoxItemId) != -1)
        {
			if (e.checked == false)
			{
				boolAllChecked = false;
				break;
			}
		}
	}

	for(i=0; i< frm.length; i++)
	{
		var e = frm.elements[i];
        if (e.type == 'checkbox' && e.name.indexOf(checkBoxAllId) != -1)
        {
			if (boolAllChecked == false)
			{
				e.checked = false;
			}
			else
			{
				e.checked = true;
			}
			break;
		}
	}
 }

//=============================================================================
// OPERATION:  setFocusByIdWithTimeout
//
// PARMS:
// controlId		- id for the control to set focus to.
// timeout          - number of milliseconds to wait before the command is executed.
// 
// NOTES:
// This function is usefule when tabbing because 
//=============================================================================
function setFocusByIdWithTimeout(controlId, timeout)
{
	setTimeout("setFocusById('" + controlId +"')", timeout);
}

//=============================================================================
// OPERATION:  setFocusById
//
// PARMS:
// controlId		- id for the control to set focus to.
//=============================================================================
function setFocusById(controlId)
{ 
	var control = document.getElementById(controlId)

	if ( control != null )
	{
		// The reason for this is when you tab, the focus shifts to the browser's url bar.
		// So in order to shift focus to a conrol you must give the control's window focus first.
		self.focus();
		
		control.focus();
	}
}

//=============================================================================
// OPERATION:  formatCurrency
//
// PARMS:
// num		- A string containing unformatted numbers.
//=============================================================================
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);
}

//=============================================================================
// OPERATION:  isDate
//
// Checks if a given date string is in one of the valid formats:
//
// a) M/D/YYYY format
// b) M-D-YYYY format
// c) M.D.YYYY format
// d) M_D_YYYY format   
//
// PARMS:
// str		- A string containing date. 
//
// NOTES: 
//=============================================================================
function isDate(s)
{
    // make sure it is in the expected format
    if (s.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{4}/g) != 0)
    {
        return false;
    }
    
    // remove other separators that are not valid with the Date class
    s = s.replace(/[\-|\.|_]/g, "/");
    
    // convert it into a date instance
    var dt = new Date(Date.parse(s));
    
    // check the components of the date
    // since Date instance automatically rolls over each component
    var arrDateParts = s.split("/");
    return (
        dt.getMonth() == arrDateParts[0]-1 && 
        dt.getDate() == arrDateParts[1] && 
        dt.getFullYear() == arrDateParts[2]
    ); 
}

//=============================================================================
// OPERATION:  openWindow
//
// PARMS:
// url		        - location to open window to
// windowName       - name of window
// windowAttributes - attributes of new window.
//                  - Ex: width=400,height=300,screenX=50,left=50,screenY=50,top=50,status=yes,menubar=yes
// 
// NOTES:
// This function is used to open a window.
// Here is help: http://www.yourhtmlsource.com/javascript/popupwindows.html
//=============================================================================
function openWindow(url, windowName, windowAttributes)
{
    var newwindow = window.open(url ,windowName, windowAttributes);
    
    // Make sure the focus method is supported by the browser.
    // If so, set focus to the newwindow. The browser only does
    // this for you the first time. If the window already exists,
    // it won't do it.
    if (window.focus)
    {
        newwindow.focus();
    }
}




