var pagedirty=false;
var arrMonths=new Array("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var today=new Date();
var todayFormatted=today.getDate()+arrMonths[today.getMonth()]+today.getFullYear();
var firstmenu=false;
var tabGroupDivs=new Array();

function showPopUp(divID){
	var divPopup=document.getElementById(divID);
	divPopup.style.display="inline";
	divPopup.style.visibility="visible";
	//divPopup.style.top=(document.body.clientHeight-divEdit.clientHeight);
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
	var dsocleft=document.all? iebody.scrollLeft : pageXOffset
	var dsoctop=document.all? iebody.scrollTop : pageYOffset
//crossobj.style.left=parseInt(dsocleft)+5+"px"
//crossobj.style.top=dsoctop+5+"px"

//	divPopup.style.top=(dsoctop-divPopup.clientHeight)/2;
//	divPopup.style.left=(parseInt(dsocleft)-divPopup.clientWidth)/2;
	var screenpos=Number(((iebody.clientHeight-divPopup.clientHeight)/2)*1);
	var offset=Number(dsoctop);
	divPopup.style.top=(screenpos+offset)+"px";

	var screenpos=Number(((iebody.clientWidth-divPopup.clientWidth)/2)*1);
	var offset=Number(dsocleft);
	divPopup.style.left=(screenpos+offset)+"px";

	divPopup.style.zIndex=99;
	try{
		eval("onDisplay_"+divID+"();");
	}catch(e){
	}
//	divPopup.style.top=(document.body.clientHeight-divPopup.clientHeight)/2;
//	divPopup.style.left=(document.body.clientWidth-divPopup.clientWidth)/2;
}
function selectOptionByAttribute(selectObject, attributeName, value){
	for(var i=0;i<selectObject.options.length;i++){
		try{
				
			if (selectObject.options[i].attributes[attributeName].value==value){
				selectObject.selectedIndex=i;
			}
		}catch(e){
		}
	}
}
function disableOptionsByAttribute(selectObject, attributeName, value){
	for(var i=0;i<selectObject.options.length;i++){
		try{
				
			if (selectObject.options[i].attributes[attributeName].value==value){
				selectObject.selectedIndex=i;
			}
		}catch(e){
		}
	}
}
function hidePopUp(divID){
	document.getElementById(divID).style.visibility="hidden";
	document.getElementById(divID).style.zIndex=1;

}
function validateKeyStrokes(objEvent){
//
// To use to validate keystrokes against datatypes
//
// Set datatype property of input box to one of:
//		integer
//		float
//		currency
//		text
//
//	Set onkeydown = "validateKeyStrokes(event);"
//

	var objElement;
	var keyCode;
	var datatype;
	var shiftDown;
	objElement=getEventSource(objEvent);
	keyCode=getEventKeyCode(objEvent);
	lastKeyCode=keyCode;
/*	if (browser=="IE") {
	    objElement=objEvent.srcElement;
	    keyCode=objEvent.keyCode;
	    
	}else{
	    objElement=objEvent.target;
	    keyCode=objEvent.which;
	    
	}
*/
	if (keyCode==16){return}
	shiftDown=(objEvent.shiftKey!=false);
	datatype=objElement.attributes["datatype"].value;
//alert(datatype+" "+keyCode+" "+shiftDown+" "+objElement.readOnly);
    var ok=false;
    if (objElement.readOnly==false
    ) {
	    if (keyCode==9||keyCode==0){ //Tab (0 tab in firefox!)
	        //nextElement(objEvent);
	        return;
	    }
        // Check for Valid Control characters
		if(datatype!="readonly"){
			if(
			    (keyCode>=37 && keyCode<=40 ) || //Backspace and delete
			    keyCode==8 || keyCode==46 ||//Arrow keys
			    keyCode==35 || keyCode==36 //End or Home
			    ) {
			    return;
			}
		}
			
//alert(objElement.datatype.value);
//var x;
//for (var idx=0; idx<objElement.attributes.length; idx++){
//	x+=objElement.attributes[idx].name+"="+objElement.attributes[idx].value+"; ";
//}
//alert(x);
		
	    if (
		    	datatype=="integer" && 
	    		!shiftDown && 
			objElement.value.length<objElement.attributes["size"].value &&
			(
			    (keyCode>=48 && keyCode<=57 ) ||//Normal Keyboard numbers
			    (keyCode>=96 && keyCode<=105 ) //numeric keypad numbers
			)
            ) {
            //alert("good");
            return;
        }else{
            //alert("bad");
        }
	    if (datatype=="float" && (
    		    (keyCode>=48 && keyCode<=57 ) || //Normal Keyboard numbers
		        (keyCode>=96 && keyCode<=105 ) || //numeric keypad numbers
                (objElement.value.indexOf(".")==-1 && (keyCode==190||keyCode==110))  //. or KeyPad .
                )
            ) {
            return;
        }
	    if (datatype=="currency" && (
    		    (keyCode>=48 && keyCode<=57 ) || //Normal Keyboard numbers
		        (keyCode>=96 && keyCode<=105 ) || //numeric keypad numbers
                (objEvent.srcElement.value.indexOf(".")==-1 && (keyCode==190||keyCode==110)) //. or KeyPad .
		   ) && (
                (objElement.value.indexOf(".")==-1 || objElement.value.length-objElement.value.indexOf(".")<=2)
           )     
           ) {
            return;
        }
	    if (datatype=="text" && (
	            (keyCode!=192 || objEvent.shiftKey==true) &&
	    	    (keyCode!=39)
	            )
	        ) {
	        return;
	    }
	    if (objElement.tagName.toUpperCase()=="BUTTON" || objElement.type.toUpperCase()=="BUTTON"){
	        return;
	    }
	    
    }
//document.getElementById("txtMessage").value=keyCode;
	CancelBubble(objEvent);
	objEvent.returnValue = false;
	if (browserIsIE()){ 
		//alert(objEvent.cancelable);
		if (objEvent.cancelable){	
		//alert("canceling");
			try{
			objEvent.stopPropagation();
			objEvent.preventDefault();
			return false;
			}catch(e){
				alert(e);
			}
		}
	}
}
function CancelBubble(e){
	if (browserIsIE()){ 
		e.cancelBubble=true;
	}else{
		try {
			e.stopPropagation();
			e.preventDefault();
		} catch(ev){
		}
	}
}
function getEventSource(e){
	if (browserIsIE()){ 
	    return e.srcElement;
	    
	}else{
	    return e.target;
	}
}
function getEventKeyCode(e){
	if (browserIsIE()){ 
	    return e.keyCode;
	    
	}else{
	    return e.which;
	}
}
function browserIsIE(){
	return (navigator.appName == "Microsoft Internet Explorer")
}
function confirmDelete(id, name){
	document.getElementById("divConfirmDeleteName").innerHTML=name;
	document.getElementById("hidConfirmDeleteID").value=id;
	showPopUp("divConfirmDelete"); 
}
function deleteConfirmed(){
	document.getElementById("hidDeleteID").value=document.getElementById("hidConfirmDeleteID").value;
//alert(document.getElementById("hidDeleteID").value);
	submitForm();
}
function deleteCancelled(){
	hidePopUp("divConfirmDelete"); 	
}

function confirmCancel(id, name){
	if (pagedirty==true){
		showPopUp("divConfirmCancel");
	}else{
		cancelConfirmed();
	}
}
function cancelConfirmed(){
	document.getElementById("hidCancelled").value="yes";
//alert(document.getElementById("hidDeleteID").value);
}
function cancelCancelled(){
	document.getElementById("hidCancelled").value="no";
	hidePopUp("divConfirmCancel"); 	
}
function doCancel(){
	showWait("doCancel2()");
}
function doCancel2(){
	try {
		cancel() // call cancel function if it exists
	}catch(e){
		document.location.reload(reloadurl);
	}
}
function doSave(){
	try{
		save();
	}catch(e){
		submitForm();
	}
}
function showWait(nextFunction){
	try{
		document.getElementById('MainOuter').style.visibility='hidden';
		showPopUp('divWait');
		document.getElementById('divWait').style.zIndex=100; //force over other popups 
	} catch(e){
	}
	setTimeout(nextFunction,1);
}
function submitForm(){
//alert("submitForm");
	showWait("submitForm2()");
}
function submitForm2(){
	try {
		if(validateForm()){
//alert("Validated OK");
			document.forms[0].submit();
		}else{
//alert("Validated Bad");
			cancelCancelled();
			return;
		}
	} catch(e){
//alert("Validation error");
//return;
		document.forms[0].submit();
	}
}
function showCalendar(InputField, Button, ShowDate, ShowTime){
alert("showCalendar 0");
var format="%d-%b-%Y";
//alert("showCalendar 1");
if (ShowDate) format="%d-%b-%Y";
///alert("showCalendar 2");
if (ShowTime) format="%k:%M";
//alert("showCalendar 3");
if (ShowDate&&ShowTime) format="%d-%b-%Y %k:%M";
//alert(format);
alert(
	Calendar.setup(
		{
		      inputField  : InputField,         // ID of the input field
		      ifFormat    : format,    // the date format
		      daFormat    : format,    // the date format
		      button      : Button,       // ID of the button
		      showsTime   : ShowTime,
		      flat	  : false,
		      electric    : false
		}
	)
	);
	
}
function tooltip(e,msg,on) { 
//alert("tooltip 2");
var tip;
try{
	tip=document.getElementById('divtooltip');
}catch(er){
}
if (!tip){
	tip=document.createElement("div");
	tip.id="divtooltip";
	tip.style.visibility="hidden";
//alert("new tip created "+tip);
	document.body.add(tip);
	tip=document.getElementById('divtooltip');
	
}
//alert(tip);
      if (on) {
	  tip.innerHTML=msg;
	  tip.top=e.pageY+15;
	  tip.left=e.pageX;
	  tip.style.visibilty="visible";
      }
      else {tip.style.visibility="hidden"; 
   }
//alert("ok");
   return true;
}


