/**********************************************************************
/* File Name	: JavaScriptFunctions.js
/* Copyright	: Copyright (c)  1996 - 2002
/*				: NorthAmerican  Advanced Technology - NAT,Inc.
/*				: This program is protected by copyright laws.
/*				: Unauthorized reproduction or distribution of this 
/*				: program or any portion of it is Prohibited.
/* Author		: Renaldas Budrys
/* Date	Created	: 6/10/2002
/* Description	: Common JavaScript functions
/* Change History:
/*		Date		By					Description
/*		6/1/2003	Renaldas Budrys		added function for address lookup by zip code
/**********************************************************************/

/***********************************************************************
* COMMON FUNCTIONS
***********************************************************************/

// ******* Functionality to pop up lose of data message. **********//
/*
1.	Call close it function from onbeforeunload event of body.
2.	You need to set blnWarningOff flag to false when user makes any data entry. 
	It can be done by setting onkeypress='blnWarningOff=false;' of body.
3.	You need to take care of drop downs to set blnWarningOff to false for changes.
	It can be done by setting it false on onchange event.
*/
var blnWarningOff = true;
function closeIt() {
	if (!blnWarningOff) {
		event.returnValue = "You will lose your work on the current page.";
	}		
}
// ** END *****/

function SetRowsClass(oTable)
{
	SetRowsClassWithRowId(oTable,"row");
}

function SetRowsClassWithRowId(oTable,strRowId)
{
	SetRowsClassWithRowIdColNum(oTable,strRowId,0);
}

String.format = function( text )
{
    if ( arguments.length <= 1 )
    {
        return text;
    }

    //decrement to move to the second argument in the array

    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ),arguments[ token + 1 ] );
    }
    return text;

};



function SetRowsClassWithRowIdColNum(oTable,strRowId,iColNum)
{
	if (oTable==null) return;
	var objRows = oTable.all(strRowId);
	if (objRows==null) return;
	
	if ( objRows.length == undefined ) {
		if ( (objRows.cells(iColNum).children(0).innerHTML!="") ||
			(objRows.cells(iColNum).children(0).dataFld=="") )
		{
			// single valid row
			objRows.className = "rowDetailAlt";		
			oTable.tBodies(0).style.display = "inline";	
			return;
		}
		else
		{
			// no valid row			
			oTable.tBodies(0).style.display = "none";
			return;
		}
	}
	
	var i;
	var j = 0;
	for (i=0; i<objRows.length; i++)
	{
		//check for hidden rows
		if (objRows.item(i).style.display != 'none')
		{
			if (j % 2 == 1)			
				objRows.item(i).className = "rowDetail";			
			else
				objRows.item(i).className = "rowDetailAlt";
			
			j++;
			try {
				oTable.tBodies(i).style.display = "inline";			
			}
			catch (er) {}
		}
	}
}

//========================================================================
// Name			: trimJS
// Description	: remove leading and trailing white spaces
// Parameters	: 
// Input		: String
// Output		: String without leading and trailing white spaces
// Use			: 
// Change History:
//		Date		By				Description
//	06/10/2002	Renaldas Budrys	- Create
//========================================================================
function trimJS(strIn) { 
	//remove leading spaces
	while (strIn.indexOf(" ") == 0) {				
		strIn = strIn.substring(1,strIn.length);
	}
	// remove embedded & trailing white spaces
	while ((strIn.lastIndexOf(" ") == (strIn.length -1)) && (strIn.length > 0)) {		
		strIn = strIn.substring(0,strIn.length-1);
	}
	return strIn
}

//========================================================================
// Name			: trimAll
// Description	: remove leading white spaces and carriage returns
//				  remove trailing white spaces and carriage returns
// Parameters	: 
// Input		: String
// Output		: String without leading and trailing white spaces, carriage returns
// Use			: 
// Change History:
//	Date			By					Description
//	09/17/2004		Shruti Gandhi		Create
//========================================================================
function trimAll(strIn) { 

 // Remove leading spaces and carriage returns  
  while ((strIn.substring(0,1) == ' ') || (strIn.substring(0,1) == '\n') || (strIn.substring(0,1) == '\r'))
  {
    strIn = strIn.substring(1,strIn.length);
  }

  // Remove trailing spaces and carriage returns
  while ((strIn.substring(strIn.length-1,strIn.length) == ' ') || (strIn.substring(strIn.length-1,strIn.length) == '\n') || (strIn.substring(strIn.length-1,strIn.length) == '\r'))
  {
    strIn = strIn.substring(0,strIn.length-1);
  }
  return strIn;
}

//========================================================================
// Name			: GetNodeTypedValue
// Description	: Get a node value (return empty string when error occured)
// Parameters	: 
// Input		: 
// Output		: String
// Use			: 
// Change History:
//		Date		By				Description
//	07/01/2002	Yuttana Buasen	- Create
//========================================================================
function GetNodeTypedValue(objDom, strQuery)
{
	var strValue;
	try
	{
		strValue = objDom.selectSingleNode(strQuery).nodeTypedValue;
	}
	catch (error)
	{
		strValue = "";
	}
	return strValue;
}

//========================================================================
// Name			: SetNodeTypedValue
// Description	: Set a node value (return true if succeeded)
// Parameters	: 
// Input		: 
// Output		: String
// Use			: 
// Change History:
//		Date		By				Description
//	07/10/2002	Yuttana Buasen	- Create
//========================================================================
function SetNodeTypedValue(objDom, strQuery, strValue)
{
	var objNode;
	try
	{
		objNode  = objDom.selectSingleNode(strQuery);
		objNode.text = strValue;
	}
	catch (error)
	{
		return false;
	}
	return true;
}

//========================================================================
// Name			: GetDateTimeString
// Description	: Format date time
// Parameters	: 
// Input		: 
// Output		: String
// Use			: 
// Change History:
//		Date		By				Description
//	07/10/2002	Yuttana Buasen	- Create
//========================================================================
function GetDateTimeString(dt)
{
	var dateStr;
	dateStr = "";
	
	// month
	if ((dt.getMonth()+1) < 10 )
		dateStr += "0";
	dateStr += (dt.getMonth() + 1) + "/";
	
	// day
	if (dt.getDate() < 10 )
		dateStr += "0";
	dateStr += dt.getDate() + "/";
	
	// 4-digit year
	dateStr += dt.getFullYear() + " ";
	
	// hour
	var hr;
	hr = dt.getHours() % 12;
	if (hr==0) hr = 12;
	if ( hr < 10 )
		dateStr += "0";
	dateStr += hr + ":";
	
	// minute
	if (dt.getMinutes() < 10 )
		dateStr += "0";
	dateStr += dt.getMinutes();
	
	// AM-PM
	if (dt.getHours() >= 12)
		dateStr += "PM";		
	else
		dateStr += "AM";
	
	return dateStr;
}

//========================================================================
// Name			: CreateNewNote
// Description	: Create new note
// Parameters	: 
// Input		: 
// Output		: XML node
// Use			: 
// Change History:
//		Date		By				Description
//	07/10/2002	Yuttana Buasen	- Create
//========================================================================
function CreateNewNote(idXML, strNote, intType, strUser)
{
	var objNewNote	= new ActiveXObject("Msxml2.DOMDocument");
	var objChild	= new ActiveXObject("Msxml2.DOMDocument");
		
	objNewNote	= idXML.createNode(1,"note","");
	objChild	= idXML.createNode(1,"inumber","");
	objChild.text = 0;
	objNewNote.appendChild(objChild);
	objChild  = idXML.createNode(1,"ssrv_time","");
	var strTime	  =	GetDateTimeString( GetDatabaseServerTime() );
	objChild.text = strTime;	
	objNewNote.appendChild(objChild);
	objChild  = idXML.createNode(1,"dtsrv_note","");
	objChild.text = strTime;
	objNewNote.appendChild(objChild);
	objChild  = idXML.createNode(1,"ssrv_note_text","");
	objChild.text = strNote;
	objNewNote.appendChild(objChild);
	objChild  = idXML.createNode(1,"inote_type_id","");
	objChild.text = intType;
	objNewNote.appendChild(objChild);
	objChild  = idXML.createNode(1,"suser_id","");
	objChild.text = strUser;
	objNewNote.appendChild(objChild);
	
	return objNewNote;
}

function CreateNewTPANote(idXML, strNote, intType, strUser)
{
	var objNewNote	= new ActiveXObject("Msxml2.DOMDocument");
	var objChild	= new ActiveXObject("Msxml2.DOMDocument");
	var strTime	  =	GetDateTimeString( GetDatabaseServerTime() );
	
	objNewNote	= idXML.createNode(1,"note","");
	objChild	= idXML.createNode(1,"inumber","");
	objChild.text = 0;
	objNewNote.appendChild(objChild);
	objChild  = idXML.createNode(1,"dtnote_date","");
	objChild.text = strTime;
	objNewNote.appendChild(objChild);
	objChild  = idXML.createNode(1,"snote_text","");
	objChild.text = strNote;
	objNewNote.appendChild(objChild);
	objChild  = idXML.createNode(1,"suser_id","");
	objChild.text = strUser;
	objNewNote.appendChild(objChild);
	
	return objNewNote;
}

function GetDatabaseServerTime()
{
	var strXML;
	var objDom;
	var strDt;
	var dt;
	
	try
	{
		strXML = GetSQLXML("get_database_server_time.xml",false,null);
		objDom = new ActiveXObject("Msxml2.DOMDocument");	
		if (true==objDom.loadXML(strXML))	
		{
			strDt  = objDom.documentElement.selectSingleNode("*/sdatetime").text;
			dt = new Date(strDt);
			return dt;
		}
	}
	catch (error)
	{		
	}
	
	dt = new Date();
	return dt;
}


//========================================================================
// Name			: CreateNewNote
// Description	: Create new note
// Parameters	: 
// Input		: oDrp - dropdown object
//				: oTxt - textbox object
//				: bDisable - if TRUE then text box will be visible,else dropdown
// Output		: none
// Use			: 
// Change History:
//		Date		By				Description
//	07/10/2002	Renaldas Budrys	- Created
//========================================================================
function disableDropdown(oDrp, oTxt, bDisable)
{
	if (bDisable){
		if (oDrp.selectedIndex != -1)
			oTxt.value = oDrp.options(oDrp.selectedIndex).innerHTML;
		else
			oTxt.value = '';
			
		oDrp.style.display = 'none';
		oTxt.style.display = 'inline';
		oTxt.className = 'textboxRO'
		oTxt.readOnly = true;
	}
	else{
		//oDrp.value = oTxt.value;
		oTxt.style.display = 'none';
		oDrp.style.display = 'inline';
	}
}

//========================================================================
// Name			: Content_OnActivate
// Description	: Used to open the popup script window in the parent hidden frame
// Parameters	: 
// Input		: none
// Output		: none
// Use			: 
// Change History:
//		Date		By				Description
//	12/10/2002		Chris Page		created
//========================================================================
function Content_OnActivate(sColumnName, sTPACode) {
	try 
	{
		window.parent.ifrHidden.DisplayPopup(sColumnName, sTPACode);
	}
	catch(exc) {
	
	}
}


//========================================================================
// Name			: disableFields
// Description	: used to disable the entry fields on the form if needed
// Parameters	: 
// Input		: none
// Output		: none
// Use			: 
// Change History:
//		Date		By				Description
//	12/10/2002		Renaldas		created
//========================================================================
function disableFields(oAllControls, sTpaCode)
{
	try 
	{
		window.parent.ifrHidden.disableFields(oAllControls, sTpaCode);
	}
	catch(exc) {
	
	}
}


//========================================================================
// Name			: setFocus()
// Description	: used to set the focus to the first available control on the form
// Parameters	: 
// Input		: none
// Output		: none
// Use			: 
// Change History:
//		Date		By				Description
//	12/10/2002		Renaldas		created
//========================================================================
function setFocus()
{
	try 
	{
		var oItem = null;
		var oControls = document.frm.all;
		
		for(var i=1;i<oControls.length;i++)
		{
			oItem = oControls.item(i);
			if ((oItem.tagName == 'SELECT') || (oItem.tagName == 'INPUT'))
				if (oItem.className == 'textbox')
				{
					oItem.focus();
					break;
				}
		}
	}
	catch(exc) {
	}
}


//========================================================================
// Name			: modalWindowIsOpen()
// Description	: used to set the flag that shows if the modal window is open
// Parameters	: 
// Input		: none
// Output		: none
// Use			: 
// Change History:
//		Date		By				Description
//	12/10/2002		Renaldas		created
//========================================================================
function modalWindowIsOpen(blnModalOpen)
{
	if ('eSCS' == window.parent.name)
	{
		var oHiddenFrm = window.parent.ifrHidden;
		
		if (true == blnModalOpen)
		{
			oHiddenFrm.blnModalWindowIsOpen = true;
			document.cookie = 'strModalWindowIsOpen=true';
			
			//change user status to busy
			changeUserStatus(0);			
		}
		else
		{
			oHiddenFrm.blnModalWindowIsOpen = false;
			document.cookie = 'strModalWindowIsOpen=false';
			if ('true' == GetUserData('session', 'strSessionExpired'))
			{
				window.parent.frames(1).location.href = 'login.asp?er=2';
			}
		}
	}
	else
	{
		if (!blnModalOpen)
		{
			if ('true' == GetCookieJS('strModalWindowIsOpen'))
			{
				if ('true' == GetUserData('session', 'strSessionExpired'))
				{
					try
					{
						blnWarningOff = true;
					}
					catch(err)
					{
					}
					window.close();
				}
			}
		}
	}
}


function GetCookieJS(name) {
	var start = document.cookie.indexOf(name+"=");
	
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return "";
	if (start == -1) return "";
	var end = document.cookie.indexOf(";",len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}

function SetCookieJS(name,value,expires,path,domain,secure) {
 document.cookie = name + "=" +escape(value) +
     ( (expires) ? ";expires=" + expires.toGMTString() : "") +
     ( (path) ? ";path=" + path : "") + 
     ( (domain) ? ";domain=" + domain : "") +
     ( (secure) ? ";secure" : "");
}

//========================================================================
// Name			: setPointer()
// Description	: changes mouse pointer to hourglass
// Parameters	: 
// Input		: obj [optional] if it is not passed then document object will be used
// Output		: none
// Use			: 
// Change History:
//		Date		By				Description
//	1/10/2002		Renaldas		created
//========================================================================
function setPointer(obj) {
	if (null == obj)
	{
		obj = document;
	}
	
    if (obj.all)
        for (var i=0;i < obj.all.length; i++)
             obj.all(i).style.cursor = 'wait';
}

//========================================================================
// Name			: resetPointer()
// Description	: changes mouse pointer to default
// Parameters	: 
// Input		: obj [optional] if it is not passed then document object will be used
// Output		: none
// Use			: 
// Change History:
//		Date		By				Description
//	1/10/2002		Renaldas		created
//========================================================================
function resetPointer(obj) {
	if (null == obj)
	{
		obj = document;
	}
		
    if (obj.all)
    {
        for (var i=0;i < obj.all.length; i++)
		{
             obj.all(i).style.cursor = 'default';
        }
    }
}

//========================================================================
// Name			: GetUserDataObject()
// Description	: 
// Parameters	: 
// Input		: 
// Output		: 
// Use			: 
// Change History:
//		Date		By				Description
//	01/01/2002		Yuttana Buasen	created
//========================================================================
function GetUserDataObject()
{
	if (null==document.all["hdnUserData"])
	{
		var objUserData = document.createElement("<SPAN>");
		objUserData.id	= "hdnUserData";
		objUserData.name= "hdnUserData";
		objUserData.style.behavior = "url(#default#userData)";
		objUserData.style.display = "none";
		document.body.appendChild(objUserData);
	}
	return document.body.all["hdnUserData"];
}   

//========================================================================
// Name			: GetUserData()
// Description	: 
// Parameters	: 
// Input		: 
// Output		: 
// Use			: 
// Change History:
//		Date		By				Description
//	01/01/2002		Yuttana Buasen	created
//========================================================================
function GetUserData(sStoreName, sAttributeName)
{
	try
	{
		var objUserData = GetUserDataObject();
		objUserData.load(sStoreName);
		return objUserData.getAttribute(sAttributeName);
	}
	catch (err)
	{
		return null;
	}
}

//========================================================================
// Name			: SetUserData()
// Description	: 
// Parameters	: 
// Input		: 
// Output		: 
// Use			: 
// Change History:
//		Date		By				Description
//	01/01/2002		Yuttana Buasen	created
//========================================================================
function SetUserData(sStoreName, sAttributeName, sAttributeValue)
{
	try
	{
		// set the expiration date to one year after 
		// the information is persisted.
		var oTimeNow = new Date();
		oTimeNow.setFullYear(oTimeNow.getFullYear() + 1);		
		var sExpirationDate = oTimeNow.toUTCString();
		
		var objUserData = GetUserDataObject();		
		objUserData.expires	= sExpirationDate;
		objUserData.setAttribute(sAttributeName, sAttributeValue);		
		objUserData.save(sStoreName);	
		return true;
	}
	catch (err)
	{
		alert('set err: ' + err.description);
		return false;
	}
}   

//========================================================================
// Name			: LoadDialogSize()
// Description	: 
// Parameters	: 
// Input		: 
// Output		: 
// Use			: 
// Change History:
//		Date		By				Description
//	01/01/2002		Yuttana Buasen	created
//========================================================================

// default margin between ScreenLeft and DialogLeft
var intLeftMargin = 4;
// default margin between ScreenTop and DialogTop
var intTopMargin = 23;
function LoadDialogSize(strWindowName)
{	
	strWindowName = GetCookieJS("LoginUserID") + "_" + strWindowName;
	
	var objUserData = GetUserDataObject();
	objUserData.load(strWindowName);
	if (null==objUserData.getAttribute("dialogLeft"))
	{
		return;
	}		
	window.dialogLeft	= objUserData.getAttribute("dialogLeft"); 
	window.dialogTop	= objUserData.getAttribute("dialogTop"); 
	window.dialogWidth	= objUserData.getAttribute("dialogWidth"); 
	window.dialogHeight	= objUserData.getAttribute("dialogHeight"); 
	intLeftMargin = parseInt(window.screenLeft,10) - parseInt(window.dialogLeft.replace("px",""),10);
	intTopMargin  = parseInt(window.screenTop,10) - parseInt(window.dialogTop.replace("px",""),10);
}

//========================================================================
// Name			: SaveDialogSize()
// Description	: 
// Parameters	: 
// Input		: 
// Output		: 
// Use			: 
// Change History:
//		Date		By				Description
//	01/01/2002		Yuttana Buasen	created
//========================================================================
function SaveDialogSize(strWindowName)
{   	
	//###########################################################//
	// The dialogLeft and dialogTop must be explicitly updated 
	// because they are not updated if the dialog has been moved
	//###########################################################//
	
	// Need these two line because screenLeft and screenTop will
	// be modified if either dialogLeft or dialogTop is modified
	var intScreenLeft = parseInt(window.screenLeft,10);
	var intScreenTop = parseInt(window.screenTop,10);		
	window.dialogLeft = intScreenLeft-intLeftMargin;
	window.dialogTop  = intScreenTop-intTopMargin;
	
	strWindowName = GetCookieJS("LoginUserID") + "_" + strWindowName;
	var objUserData = GetUserDataObject();
	objUserData.setAttribute("dialogLeft",window.dialogLeft);
	objUserData.setAttribute("dialogTop",window.dialogTop);
	objUserData.setAttribute("dialogWidth",window.dialogWidth);
	objUserData.setAttribute("dialogHeight",window.dialogHeight);
	objUserData.save(strWindowName);	
	
}

//========================================================================
// Name			: GetDialogFeatureString()
// Description	: 
// Parameters	: 
// Input		: 
// Output		: 
// Use			: 
// Change History:
//		Date		By				Description
//	01/01/2002		Yuttana Buasen	created
//========================================================================
function GetDialogFeatureString
(
	strWindowName, 	
	defaultDialogLeft,
	defaultDialogTop,
	defaultDialogWidth,
	defaultDialogHeight,
	btCenter,
	btResizable,
	btShowStatus,
	btShowHelp,
	btLoadLocation,
	btLoadSize
)
{
	strWindowName = GetCookieJS("LoginUserID") + "_" + strWindowName;
	if (btCenter==null) btCenter=true;
	if (btResizable==null) btResizable=true;
	if (btShowStatus==null) btShowStatus=false;
	if (btShowHelp==null) btShowHelp=false;
	if (btLoadLocation==null) btLoadLocation=true;
	if (btLoadSize==null) btLoadSize=true;
	
	var objUserData = GetUserDataObject();
	try 
	{
		objUserData.load(strWindowName);
	}
	catch (er) {}
	
	var strFeatures = new String();
	strFeatures	= "status:no; ";
	// LEFT
	if ( (null==objUserData.getAttribute("dialogLeft")) || (!btLoadLocation) ) 
	{
		if (!btCenter) 
		{
			strFeatures += ".dialogLeft:" + defaultDialogLeft + "px; ";
		}
	}
	else
	{	
		strFeatures += ".dialogLeft:" + objUserData.getAttribute("dialogLeft") + "px; ";
	}
	
	// TOP
	if ( (objUserData.getAttribute("dialogTop")==null) || (!btLoadLocation) )  
	{
		if (!btCenter) {
			strFeatures += ".dialogTop:" + defaultDialogTop + "px; ";
		}
	}
	else
	{
		strFeatures += ".dialogTop:" + objUserData.getAttribute("dialogTop") + "px; ";
	}
	
	//	Width
	if ( (objUserData.getAttribute("dialogWidth") == null) || (!btLoadSize) )
	{
		strFeatures += ".dialogWidth:" + defaultDialogWidth + "px; ";
	}
	else
	{
		strFeatures += ".dialogWidth:" + objUserData.getAttribute("dialogWidth") + "px; ";
	}
		
	// Height
	if ( (null==objUserData.getAttribute("dialogHeight")) || (!btLoadSize) )
	{
		strFeatures += ".dialogHeight:" + defaultDialogHeight + "px; ";
	}
	else
	{
		strFeatures += ".dialogHeight:" + objUserData.getAttribute("dialogHeight") + "px; ";
	}
		
	strFeatures += (btCenter)?"center:yes; ":"center:no; ";

	strFeatures += (btResizable)?"scroll:yes; resizable:yes; ":"scroll:no; resizable:no; ";
	
	//strFeatures += (btShowStatus)?"status:yes; ":"status:no; ";
	
	strFeatures += (btShowHelp)?"help:yes; ":"help:no; ";
	
	return strFeatures;
}


//========================================================================
// Name			: popupCalendarModal(fld)
// Description	: changes mouse pointer to default
// Parameters	: 
// Input		: fld - html control
// Output		: none
// Use			: 
// Change History:
//		Date		By				Description
//	2/17/2003		Renaldas		created
//========================================================================
function popupCalendarModal(fld) 
{
	//load calendar control
	var url = 'util/calendar.aspx?';
	
		
	modalWindowIsOpen(true);
	var strResult = window.showModalDialog('util/calendar.aspx?date=' + escape(fld.value), '','dialogHeight:171px;dialogWidth:187px;dialogLeft:' + event.screenX +';dialogTop:' + event.screenY + ';resizeble:no;scroll:no;status:no;help:no;');
	modalWindowIsOpen(false);
	
	if (null != strResult)
	{
		fld.value = strResult;
		
		try		
		{
			if (fld.datafld!='') 
			{
				document.onafterupdate();
			}
			fld.onchage();			
		}
		catch (err)
		{
		}
	}
	
	
	//set focus in try catch block, just in case the field is readOnly
	try
	{   		
		fld.focus();	
	}
	catch(err)
	{
	}
	
	
	return false;
}

function OpenServiceCenter(intServiceId)
{	
	var strURL = "SCInformation.asp?";
	strURL += "iservice_id=" + escape(intServiceId) + "&";
	var objArgument = new Object();
	objArgument.WindowName = "SCProfile";
	var strFeatures = GetDialogFeatureString(objArgument.WindowName,0,0,812,500,true); 
	modalWindowIsOpen(true);
	var oResult = window.showModalDialog(strURL,objArgument,strFeatures);	
	modalWindowIsOpen(false);
	return oResult;
}

function CheckSODuplicate() {
	var objDuplicateNode = null;
	try {
		objDuplicateNode = idSOXML.selectSingleNode("*/@iDuplicate");
	}
	catch (er) {}
	if (objDuplicateNode != null) {
		if (objDuplicateNode.text == "1") {
			alert("The Service Order you just saved is a duplicate.  Please refer to the Comments.");
			objDuplicateNode.text = "0";
		}
	}
}

/********************************************************
 undressPhoneNumber -formats the phone number for SQL
********************************************************/
function undressPhoneNumber(s) {
	var sPhone = new String();
	sPhone = s;
	
	// clean up the phone number
	if (sPhone.length > 0) {
		sPhone = sPhone.replace(" ", "");
		sPhone = sPhone.replace("(", "");
		sPhone = sPhone.replace(")", "");
		sPhone = sPhone.replace("-", "");
		sPhone = sPhone.replace(".", "");
	}	
	
	if (sPhone.length > 10) {
		sPhone = "";
	}
	
	return sPhone;
}



//========================================================================
// Name			: getAddressInformationByZip(strZipCode, objCity, objCounty, objState, objMSACode)
// Description	: gets the address information for the zipcode and fills the city, county, state and msa fields
// Parameters	: 
// Input		:	strZipCode	- Zip Code [string]
//					objCity		- html control
//					objCounty	- html control
//					objState	- html control
//					objMSACode	- html control
// Output		: none
// Use			: 
// Change History:
//		Date		By				Description
//	6/1/2003		Renaldas		created
//========================================================================
function getAddressInformationByZip (
										strZipCode, 
										objCity, 
										objCounty, 
										objState, 
										objMSACode
									)
{
	//make sure that the zip code is 5 chars long
	if ((strZipCode.length == 10)||(strZipCode.length == 9))
		strZipCode = strZipCode.substring(0, 5);
		
	//check for valid zip code
	if (strZipCode.length != 5)
		return;

	//get address information
	var arrPara = new Array();			
	arrPara[0] = new para("szip_code", strZipCode);
	//test();
	var strResults = GetSQLXML('get_address_by_zip.xml',false,arrPara);	

	//validate results
	var objDom = new ActiveXObject("Msxml2.DOMDocument");
	objDom.async = false;
	if (objDom.loadXML(strResults))
	{
		if ((objDom.selectSingleNode("*/*/sstate_code") != null)&&(objState != null))
		{
			objState.value = objDom.selectSingleNode("*/*/sstate_code").text;
		}

		if ((objDom.selectSingleNode("*/*/scounty_name") != null)&&(objCounty != null))
		{
			objCounty.value = objDom.selectSingleNode("*/*/scounty_name").text;
		}
		
		if ((objDom.selectSingleNode("*/*/smsa_code") != null)&&(objMSACode != null))
		{
			//*** When you need the value of a xml node INCLUDING space,
			//***  use "firstChild.nodeValue" property instead of "text" property
		
			if (objMSACode.value == '')
			{
				objMSACode.value = objDom.selectSingleNode("*/*/smsa_code").firstChild.nodeValue;
			}
			else
			{
				if (objMSACode.value != objDom.selectSingleNode("*/*/smsa_code").firstChild.nodeValue)
				{
					alert('MSA Code will be changed from "' + trimJS(objMSACode.value) + '" to "' + trimJS(objDom.selectSingleNode("*/*/smsa_code").firstChild.nodeValue) + '".');
					objMSACode.value = objDom.selectSingleNode("*/*/smsa_code").firstChild.nodeValue;
				}
			}
		}

		var objCities = objDom.getElementsByTagName('*/*/cities/scity');
		if ((objCities.length > 0)&&(objCity != null))
		{
			if (objCities.length == 1)
			{
				objCity.value = objCities.item(0).text;
				return;
			}
			
			//open multiple city screen
			var intListSize = (objCities.length > 5)?5:objCities.length;
			modalWindowIsOpen(true);
			objCity.value = window.showModalDialog('CitySelect.asp?size=' + intListSize, objDom.getElementsByTagName('*/*/cities/scity'),"dialogHeight:190px; dialogWidth:350px; scroll:no; center:yes; status:no; resizable:no");
			modalWindowIsOpen(false);
		}
	}
}

//========================================================================
// Name			: GetHtmlObject(strObjName)
// Description	: finds and returns an html object from document
// Parameters	: 
// Input		:	srObjName - Object Name or Id
//
// Output		: obj - Html object
//
// Change History:
//		Date		By				Description
//	12/09/2003		M Kochanowski	created
//========================================================================	
function GetHtmlObject(strObjName)
{
	var obj;
	for(i=0;i<document.all.length;i++)
	{
		if(document.all(i).id==strObjName | document.all(i).name==strObjName)
		{
			obj = document.all(i);
		}
	}
	return obj;
}

//========================================================================
// Name			: PopupCalendar
// Description	: Show calendar and return selected date
// Parameters	: 
// Input		: strDate - initial selected date
//				  strLocation - relative location of ModalWindowHost.aspx
//						and Calendar.aspx
//
// Output		: selected date, or false if cancel
//
// Change History:
//		Date		By				Description
//	03/12/2004		Yuttana Buasen	created
//	05/02/2006		Nilay Patel		Added objArgument.Opener = self which will
//									help determine parent window.
//========================================================================	
function PopupCalendar(strDate, strLocation)
{
	if (strLocation==undefined)
	{
		strLocation = "";
	}	
		
	var strURL = "Calendar.aspx?"; 
	strURL += "date=" + strDate + "&"; 	
	strURL  = strLocation + "ModalWindowHost.aspx?URL=" + escape(strURL);

	var objArgument = new Object();
	objArgument.Opener = self; 
	objArgument.WindowName = "PopupCalendar"; 	
	// fixed location and size; location is based on mouse's position
	var strFeatures = GetDialogFeatureString(objArgument.WindowName,event.screenX,event.screenY,213,218,false,false,false,false,false,false);
	var strReturnValue = window.showModalDialog(strURL,objArgument,strFeatures);	
	return strReturnValue;
}

//========================================================================
// Name			: PopupCalendar
// Description	: Show calendar and return selected date
// Parameters	: 
// Input		: obj - object to be set date
//				  strLocation - relative location of ModalWindowHost.aspx
//						and Calendar.aspx
//
// Output		: 
//
// Change History:
//		Date		By				Description
//	03/12/2004		Yuttana Buasen	created
//========================================================================	
function PopupCalendarUpdateObject(obj, strLocation)
{
	var strDate = obj.value;	
	var strReturnValue = PopupCalendar(strDate, strLocation);
	if(strReturnValue != false)
	{
		obj.value = strReturnValue;		
	}	
}

/*	Created By		:Praveen A 
	Date			: 
	Description		: Returns InnerText of a Label by passing LabelId as Parameter
	Change History	:
	
*/
function GetText(strLabelCode)
{
    var objLabel=document.getElementById(strLabelCode);
    
    if(objLabel != null)
    {
        return objLabel.innerText;
    }
    return 'Error';    
}    

/*	Created By		:Praveen A 
	Date			: 09/18/2007
	Description		: Used to SetFocus to first available control(which is editable) on a aspx page.
	Change History	:	
*/
function SetFocusToFirstControl()
{
  var bFound = false;  
  //for each form
  for (f=0; f < document.forms.length; f++) 
  { 
    //for each element in each form
    for(i=0; i < document.forms[f].length; i++) 
    { 
      //if it's not a hidden element
      if (document.forms[f][i].type != "hidden") 
      { 
        //and it's not disabled
        if (document.forms[f][i].disabled != true) 
        {
          try {
             //set the focus to it
             document.forms[f][i].focus();
             var bFound = true;
          }
          catch(er) {
          }
        }
      }
      //if found in this element, stop looking
      if (bFound == true)
        break;
    }
    //if found in this form, stop looking
    if (bFound == true)
      break;
  }
}

/*	Created By		: Anand Shah
	Date			: 06/18/2008
	Description		: Used to disable control for which ClientID is passed.
	Change History	:	
*/
function DisableControl(ControlID)
{
    var control = document.getElementById(ControlID);
    if (control)
    {
        control.disabled = true;
    }
}

/*	Created By		: Anand Shah
	Date			: 06/18/2008
	Description		: Used to enable control for which ClientID is passed.
	Change History	:	
*/
function EnableControl(ControlID)
{
    var control = document.getElementById(ControlID);
    if (control)
    {
        control.disabled = false;
    }
}

/*
    Created By : Aanal Vekaria
    Create Date : 06/24/2008
    Summary: set focus to control when Tab key is pressed.
*/
function SetFocusOnTab(strId)
{
    //Tab key keycode   
    var TAB_KEY = 9;
    if(event.keyCode == TAB_KEY)
    {
        document.all[strId].focus();
        //Cancel all other events so that focus will not go to address bar.
        event.keyCode = 0;
        event.cancelBubble = true;
        event.returnValue = false;              
    }
}

/*
    Created By  : Praveen A
    Create Date : 07/08/2008
    Summary     : Set the modified flag 'On' to show warning pop-up
*/
function SetModifiedStatusOn()
{    
    blnWarningOff = false;    
}

/*
    Created By  : Praveen A
    Create Date : 07/08/2008
    Summary     : Set the modified flag 'Off' to show warning pop-up
*/
function SetModifiedStatusOff()
{    
    blnWarningOff = true;    
}

