/*
<!-- -->
<!-- The copyright in the work that is PublicAccess is the exclusive -->
<!-- property of CAPS Solutions Ltd, and its respective copyright owners, and is -->
<!-- protected under United Kingdom copyright law and other international copyright -->
<!-- treaties and conventions. 
<!-- © 2004. CAPS Solutions Ltd and its licensor(s). All rights reserved. -->
*/

// dhtml.js
// common dhtml scripts for visibility and positioning

// field types
var szcFIELDTYPE_DATE = 'date';
var szcFIELDTYPE_TIME = 'time';
var szcFIELDTYPE_SHORTTIME = 'shorttime';
var szcFIELDTYPE_HOURSMINUTES = 'hoursminutes';
var szcFIELDTYPE_NUMBER = 'number';
var szcFIELDTYPE_YESNO = 'yesno'; // Y OR N
var szcFIELDTYPE_STRING = 'string';
var szcFIELDTYPE_CHAR = 'char';
var szcFIELDTYPE_UFOFFICER = 'officer';
var szcFIELDTYPE_EMAIL = 'email';
var szcFIELDTYPE_PHONENUMBER = 'phonenumber';



//initialisation


//***********************************************************************************/
// toggletab function - shows and hides tabs in detailview screens
/***********************************************************************************/
function toggleTab_Click(iTabId){
	
	// CSS classNames & DOM id values
	var szcDOMID_TABCONTENT_PREFIX='TC';
	var szcDOMID_TAB_PREFIX='T';
	var szcSTYLECLASS_TAB='cTab';
	var szcSTYLECLASS_TAB_SELECTED='cTabSelected';
	var szcSTYLECLASS_TABCONTENT_VISIBLE='cTabContentVis';
	var szcSTYLECLASS_TABCONTENT_HIDDEN='cTabContentHidden';
	var szcSTYLECLASS_TAB_DISABLED='cTabDisabled';
	var szcFORMELEMENT_TABLASTSELECTED='TabLastSelected';

	
	var iTabCount = document.getElementById('TabCount').value;

	var elCurrTab;
	var elCurrTabContent;
	var elTabLastSelected;

	elTabLastSelected = document.getElementById('szcFORMELEMENT_TABLASTSELECTED');
	
	if (document.getElementById(szcDOMID_TAB_PREFIX + iTabId).className==szcSTYLECLASS_TAB_DISABLED) {
		return;
	}
	
	
	for (var i=1;i <= iTabCount;i++){
		
		elCurrTab = document.getElementById(szcDOMID_TAB_PREFIX + i);
		elCurrTabContent = document.getElementById(szcDOMID_TABCONTENT_PREFIX + i);
				
		if (""+i == iTabId){

			if (elCurrTab.className != szcSTYLECLASS_TAB_SELECTED){
				elCurrTab.className = szcSTYLECLASS_TAB_SELECTED;
				elCurrTabContent.className = szcSTYLECLASS_TABCONTENT_VISIBLE;
				//toggleTabWindowedObjectsVisibility(elCurrTabContent, true);
				
				if (elCurrTab.outerHTML.indexOf("maptab") > 0)
				{
					if (bMapLoaded==false)
					{
						refreshMap();
					}
				}
			
			}
			
			
			//set the id of currently selected tab in XSL
			if(elTabLastSelected)
				elTabLastSelected.value = iTabId;
			}
			
		else{
			if (elCurrTab.className==szcSTYLECLASS_TAB_DISABLED) {
				return;
				}
			
			if (elCurrTab.className != szcSTYLECLASS_TAB){
				elCurrTab.className = szcSTYLECLASS_TAB;
				elCurrTabContent.className = szcSTYLECLASS_TABCONTENT_HIDDEN;
				//toggleTabWindowedObjectsVisibility(elCurrTabContent, false);
				
			}
			
		}	
	}
	
	return;
	
	
}




//****************************************************************************************  
// returns to last selected tab after a save - use when refreshing history etc. (thahn)
//****************************************************************************************  
function returnToLastSelectedTab(){
	if (document.getElementById('TabLastSelected')){

		var iTabLastSelected = document.getElementById('TabLastSelected').value;

		if (iTabLastSelected > 0)
			toggleTab_Click(iTabLastSelected);
	}
}


//******************************************************************************
//  used for hiding windowed controls because they interfere with the tabs on WInCE
// IE 4 on WinCE does not follow the MSDN doc 
// - you cannot control visibility, display or height using dynamic CSS style.
// Had to wrap it in a div (with visibility attribute set),
// then toggle visibility AND display of the div (all this for IE4 CE only)
//******************************************************************************
function toggleTabWindowedObjectsVisibility(elCurrTabContent, bVisible) {
	
	var tmpSelectTags = document.all.tags("select");	
	
	if (tmpSelectTags.length <= 0){
		return false;
	}
	
	var tmpDivTags = document.body.all.tags("div");

	var elDiv;
	
	for (var i=0; i < tmpDivTags.length; i++) {
		
		elDiv = tmpDivTags[i];

		if (elDiv.windowedobjwrapper=='true' && elCurrTabContent.contains(elDiv)){
				
			if (bVisible){
				elDiv.style.visibility = "visible";
				elDiv.style.display = "block";
			}else{
				elDiv.style.visibility = "hidden";
				elDiv.style.display = "none";
			}
		}
	}

}


//***********************************************************************************/
// highlights textbox or textarea with focus
/***********************************************************************************/
function getFocus(el){
	
	if (el.className != 'cInputGreyed'){
		if (el.className=="cRequired") { 
			el.style.backgroundColor = "#FFC5C5";
		}									
		else	{el.className = 'cGetFocus';
		}
	}
}
//***********************************************************************************/
// returns highlight to normal on blur and clears any hidden buddy variables on delete
/***********************************************************************************/
function lostFocus(el){

	// change the color
	
	if (el.className != 'cInputGreyed'){
		if (el.className=="cRequired") {
			el.style.backgroundColor = "#FFE3E3";
		}
			else {el.className = 'select';
		}
	}
	
	// if date or time check that valid input received
	var msg='';
	
	var fieldtype;
	var bFoundFieldTypeAttribute=false;
	var bFoundDescAttribute=false;
	var bFoundMaxlengthAttribute=false;
	var bFoundTypeAttribute=false;
	var elementAttribute;
	var iMaxLen;
	var desc, type;
	var i=0;
	
	if (navigator.appName=="Microsoft Internet Explorer" && navigator.userAgent.indexOf("Opera")==-1)
	{
		fieldtype = el.fieldtype;
		iMaxLen = el.maximumlength;
		desc = el.desc;
	}
	else
	{
		//pguz, 25/02/2004, for cross-browser compatibility.  Avoids use of IE's element.attribute syntax
		while ((!bFoundFieldTypeAttribute || !bFoundDescAttribute || !bFoundMaxlengthAttribute || !bFoundTypeAttribute) && i<el.attributes.length)
		{
			elementAttribute = el.attributes[i++];
			switch (elementAttribute.name.toLowerCase())
			{
				case "fieldtype":
					fieldtype = elementAttribute.value;
					bFoundFieldTypeAttribute=true;
					break;
					
				case "desc":
					desc = elementAttribute.value;
					bFoundDescAttribute=true;
					break;
				
				case "maximumlength":
					iMaxLen = elementAttribute.value;
					bFoundMaxlengthAttribute=true;
					break;
				
				case "type":
					type = elementAttribute.value;
					bFoundTypeAttribute=true;
					break;
			}
		}
	}

	
	if (fieldtype)
	{
		switch (fieldtype){

			case szcFIELDTYPE_DATE:
				if (!isFieldValidDate(el)){
					msg = 'Invalid date value  (' + getFormatText(szcFIELDTYPE_DATE) + ')  entered into field: ' + desc;
				}
				break;
			
			case szcFIELDTYPE_NUMBER:
				if (!isFieldValidNumber(el)){
					msg = 'Invalid numeric value entered into field: ' + el.desc;
					
					if (!isNaN(el.decplaces) && el.decplaces > 0)
						msg = msg + ". \nEnter up to " + el.decplaces + " decimal places.";

				}
				break;
				
			case szcFIELDTYPE_TIME:
				if (!isFieldValidTime	(el)){
					msg = 'Invalid time value (' + getFormatText(szcFIELDTYPE_TIME) + ') entered into field: ' + desc;
				}
				break;
			
			case szcFIELDTYPE_SHORTTIME:
				if (!isFieldValidShortTime	(el)){
					msg = 'Invalid time value entered (' + getFormatText(szcFIELDTYPE_SHORTTIME) + ') into field: ' + desc;
				}
				break;
			
			case szcFIELDTYPE_HOURSMINUTES:
				if (!isFieldValidHoursMinutes(el)){
					msg = 'Invalid time value entered (' + getFormatText(szcFIELDTYPE_HOURSMINUTES) + ') into field: ' + desc;
				}
				break;
				
				
			case szcFIELDTYPE_PHONENUMBER:
				if (!isFieldValidTelephoneNumber(el)){
					msg = 'Invalid telephone number entered into field: ' + desc;
				}
				break;
				
			case szcFIELDTYPE_EMAIL:
				if (!isFieldValidEmailAddress(el)){
					msg = 'Invalid email address entered into field: ' + desc;
				}
				break;
			
			default:
				msg='';
				break;

				
		}
	}
	if (msg != ''){
		alert(msg);
	}
	
}


//******************************************************************
// checks for double clicks on text fields and inserts default values 
//******************************************************************

if (navigator.appName=="Microsoft Internet Explorer")
{
	document.ondblclick=ondblclick;
}

function ondblclick()
{
	window.event.cancelBubble=true;	
	var eSrc = window.event.srcElement;
	
	if (eSrc.type == "text" && eSrc.readOnly==false){
		
		switch (eSrc.fieldtype){
			
			case 'undefined':
				break;
			
			case szcFIELDTYPE_TIME:
				writeCurrentTimeToField(eSrc);
				break;
			
			case szcFIELDTYPE_SHORTTIME:
				writeCurrentShortTimeToField(eSrc);
				break;
				
			case szcFIELDTYPE_DATE:
				writeTodaysDateToField(eSrc);
				break;
			
			case szcFIELDTYPE_UFOFFICER:
				writeLogonOfficerDetailsToField(eSrc);
				break;
			
			default:
				break;
		
		}
	}
}



//*************************************************************************
//	function to resize a Modal Dialog to accomodate for the 
// full document size
//*************************************************************************
function windowResize(){

	var icMinWidth = 250;
	var icMinHeight = 250;
	
	var docBody = window.document.body;
	var iWinHeight = docBody.scrollHeight + 20 + parseInt(docBody.bottomMargin) + parseInt(docBody.topMargin) ; 
	var iWinWidth = docBody.scrollWidth + 20 + parseInt(docBody.leftMargin) + parseInt(docBody.rightMargin) ; 
	
	if (iWinHeight > screen.availHeight)
		iWinHeight = screen.availHeight;
		
	if (iWinWidth > screen.availWidth)
		iWinWidth = screen.availWidth;
	

	if (iWinWidth < icMinWidth)
		iWinWidth = icMinWidth;
	
	if (iWinHeight < icMinHeight)
		iWinHeight = icMinHeight;
	
	//window.dialogHeight = iWinHeight; 
	//window.dialogWidth = iWinWidth;
	window.resizeTo(docBody.scrollWidth+20,docBody.scrollHeight+60);
	
	window.scrollTo(0,0);

}

/*******************************************************************
show the status bar - create if doesn't exist
*********************************************************************/
function showPleaseWaitMessage(){
	
	setCursor('wait');
	document.body.disabled=true;
	
	if (!document.getElementById('divStatusBar')){
		writePleaseWaitMessage();
	}
	
	document.getElementById('divStatusBar').style.display='block';
	document.getElementById('divStatusBar').style.left=(document.body.offsetWidth/2) - (document.getElementById('divStatusBar').offsetWidth/2) + (document.body.scrollLeft);
	document.getElementById('divStatusBar').style.top = (document.body.offsetHeight/2) -(document.getElementById('divStatusBar').offsetHeight/2)+ (document.body.scrollTop);

}	
/*******************************************************************
hide the status bar 
*********************************************************************/
function hidePleaseWaitMessage(){

	setCursor('default');
	document.body.disabled=false;
	if (document.getElementById('divStatusBar')){
		document.getElementById('divStatusBar').style.display='none';
	}
}	

/*******************************************************************
update the status bar 
*********************************************************************/
function updatePleaseWaitMessage(){
	
	var elStat = document.getElementById('theStatusBar');

	if (parseInt(elStat.style.width) >= 200){
		elStat.style.width = 0;
	}else{
		var szWidth = parseInt(elStat.style.width) + 20;
		elStat.style.width = szWidth;
	}
}

/*******************************************************************
write the status bar 
*********************************************************************/
function writePleaseWaitMessage()
{
	var szStatusHTML = '<div id="divStatusBar" style="border:4 solid red; position:absolute; left:;top:;width: 220px; height: 80px; background: white; text-align: center; z-index:10"><h5 id="statusText">Please wait, working...</h5><!--<hr id="theStatusBar" style="height: 10px; color: yellow; width:0; margin-left:10px; border:2 solid black; text-align: left;" />--></div>'
	
	var b = document.body;
	b.insertAdjacentHTML('beforeEnd', szStatusHTML);
}


function setCursor(szCursorStyle)
{
	document.body.style.cursor=szCursorStyle;
}


/*********************
show the Help window
**********************/
function showHelp(url)
{
	var screenWidth = screen.width;
	var x=screenWidth-350;
	var w = window.open(url,'help',"menubar='no',statusbar='no',scrollbars,screenX="+x+",screenY=50,top=50,left="+x+",width=300,height=500");
	w.focus();
}


/*********************
 disable the search button
**********************/
function disableSearchButton(f)
{
	var btnSubmit = f.elements['btnSubmitSearchForm'];
	var K_btnSubmit = f.elements['KHTML_btnSubmitSearchForm'];
	
	if (btnSubmit) btnSubmit.disabled=true;
	if (K_btnSubmit) K_btnSubmit.disabled=true;
}
