window.onunload = function(){if(opener==null && self.screenTop>9000)killSession();}

function login() {
    if (validateForm()) {
        document.UserLogin.submit();
    }
}
function passwordRecovery() {
    location.href = "/ext/PasswordRecovery";

}
function collaborationSite() {
    location.href = "/ext/CollabSiteEntry";
}
function limitText(textArea, length) {
    if (textArea.value.length > length) {
        textArea.value = textArea.value.substr(0,length);
    }
}
function pause(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}
function trimString(str) {
    while (str.charAt(0) == ' ')
        str = str.substring(1);
    while (str.charAt(str.length - 1) == ' ')
        str = str.substring(0, str.length - 1);
    return str;
}

function imgSwap(oImg) {
   var strOver  = "_1"
   var strOff = "_0"
   var strImg = oImg.src
   if (strImg.indexOf(strOver) != -1)
      oImg.src = strImg.replace(strOver,strOff)
   else
      oImg.src = strImg.replace(strOff,strOver)
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var isnavold, isnavnew, isie
isnavold=(navigator.appName.indexOf('Netscape')!=-1&&parseInt(navigator.appVersion)<5)
isnavnew=(navigator.appName.indexOf('Netscape')!=-1&&parseInt(navigator.appVersion)>4.9)
isie=(navigator.appName.indexOf('Microsoft')!=-1)


function setVisible(field) {
    if(isie) {
        var wait = eval('document.all.' + field);
        wait.style.visibility="visible";
    } else if(isnavnew) {
        var wait = document.getElementById(field);
        wait.style.visibility="visible";
    }
}

function setInvisible(field) {
    if(isie) {
        var wait = eval('document.all.' + field);
        wait.style.visibility="hidden";
    } else if(isnavnew) {
        var wait = document.getElementById(field);
        wait.style.visibility="hidden";
    }
}



function lyrWrt(Item, text) {
    if(isie) {
        var a = eval('document.all.' + Item);
        a.innerHTML = text;
    } else if(isnavnew) {
        document.getElementById(Item).innerHTML = text
    }
}
function killSession() {
    if (window.XMLHttpRequest){
        reqXML = new XMLHttpRequest();
    } else if(window.ActiveXObject){
        reqXML = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(reqXML){
        reqXML.open("POST", "/core/ext/SessionKill.jsp", true);
        reqXML.setRequestHeader("Content-Type", "text/xml");
        reqXML.send("<?xml version='1.0' encoding='UTF-8'?>");
    }
}


function clearWaitMessage() {
    if (document.getElementById('messageDiv')) {
        document.getElementById('messageDiv').innerHTML='&nbsp;';
    }
}



function popMessage(text) {
    if (document.getElementById('submessageDiv')) {
        if (text.indexOf('Error') != -1 || text.indexOf('Invalid') != -1 || text.indexOf('Fail') != -1) {
            document.getElementById('submessageDiv').className='messageTagError';
        } else {
            document.getElementById('submessageDiv').className='messageTagAlert';
        }
        document.getElementById('submessageDiv').innerHTML=text;
    } else if (document.getElementById('messageDiv')) {
        if (text.indexOf('Error') != -1 || text.indexOf('Invalid') != -1 || text.indexOf('Fail') != -1) {
            document.getElementById('messageDiv').className='messageTagError';
        } else {
            document.getElementById('messageDiv').className='messageTagAlert';
        }
        document.getElementById('messageDiv').innerHTML=text;
    }
}


function appendMessage(text) {
    if (document.getElementById('submessageDiv')) {
        if (text.indexOf('Error') != -1 || text.indexOf('Invalid') != -1 || text.indexOf('Fail') != -1) {
            document.getElementById('submessageDiv').className='messageTagError';
        } else {
            document.getElementById('submessageDiv').className='messageTagAlert';
        }
        document.getElementById('submessageDiv').innerHTML=document.getElementById('submessageDiv').innerHTML+text;
    } else if (document.getElementById('messageDiv')) {
        if (text.indexOf('Error') != -1 || text.indexOf('Invalid') != -1 || text.indexOf('Fail') != -1) {
            document.getElementById('messageDiv').className='messageTagError';
        } else {
            document.getElementById('messageDiv').className='messageTagAlert';
        }
        document.getElementById('messageDiv').innerHTML=document.getElementById('messageDiv').innerHTML+text;
    }
}



var messageTimeout = null;
function parseMessage(xml) {
    if (xml.getElementsByTagName('message')[0].firstChild!=null) {
        if (xml.getElementsByTagName('message')[0].firstChild.nodeValue != '') {
            if (document.getElementById('submessageDiv')) {
                if (xml.getElementsByTagName('message')[0].firstChild.nodeValue.indexOf('Error') != -1) {
					document.getElementById('submessageDiv').className='messageTagError';
                } else {
					document.getElementById('submessageDiv').className='messageTagAlert';
                }
                document.getElementById('submessageDiv').innerHTML=xml.getElementsByTagName('message')[0].firstChild.nodeValue;
            } else if (document.getElementById('messageDiv')) {
                if (xml.getElementsByTagName('message')[0].firstChild.nodeValue.indexOf('Error') != -1) {
					document.getElementById('messageDiv').className='messageTagError';
                } else {
					document.getElementById('messageDiv').className='messageTagAlert';
                }
                document.getElementById('messageDiv').innerHTML=xml.getElementsByTagName('message')[0].firstChild.nodeValue;
            }
        }
    }
}
function alertMessage(xml) {
    if (xml.getElementsByTagName('alert_message')[0].firstChild!=null) {
        if (xml.getElementsByTagName('alert_message')[0].firstChild.nodeValue != '') {
			alert(xml.getElementsByTagName('alert_message')[0].firstChild.nodeValue);
        }
    }
}
function confirmMessage(xml) {
    if (xml.getElementsByTagName('alert_message')[0].firstChild!=null) {
        if (xml.getElementsByTagName('alert_message')[0].firstChild.nodeValue != '') {
			return confirm(xml.getElementsByTagName('alert_message')[0].firstChild.nodeValue);
        }
    }
	return false;
}
function setMessageTimeout(millis) {
    messageTimeout=setTimeout("clearMessage()",millis);
}



function clearMessage() {
    if (document.getElementById('submessageDiv')) {
        document.getElementById('submessageDiv').innerHTML='&nbsp;';
    }
    if (document.getElementById('messageDiv')) {
        document.getElementById('messageDiv').innerHTML='&nbsp;';
    }
    clearTimeout(messageTimeout);
}



function switchMenu(el, pic) {
	if(document.getElementById(el).style.display != "block") {
		expand(el, pic);
	} else {
		collapse(el, pic);
	}
}




	//the calendar will appear on a DIV tag with the following id.
	var calendarDivId="PopCalendarDiv";
	
	//display the pop up calender
	function popCalendar(formElementName, date) {
		var formElement = eval(formElementName);
		
		//get the currently selected date or, if provided a new date.
		var current_date = formElement.value;
		if(date) current_date= date
		
		//get or create the calendar division and append it to the document.
		calendarDiv = document.getElementById(calendarDivId);
		if(!calendarDiv){
			calendarDiv = document.createElement("div");
			calendarDiv.id = calendarDivId
			calendarDiv.style.position = "absolute"
			calendarDiv.style.zIndex = 5000;
			formElement.parentNode.appendChild(calendarDiv)

		}
		
		//perfrom ajax call to get the calendar html.
		var query = 'form_field=' + formElementName + '&current_date=' + current_date;
		ajaxGetWrite('/CalendarView', query, calendarDivId);
		
		//position the calendar to the left of the form element it relates to.
		calendarDiv.style.left = findPosX(formElement)+150;
		calendarDiv.style.top = findPosY(formElement)-120; 
	}
	
	//determine the x coordinate position of an HTML element.
	function findPosX(obj){
	  var curleft = 0;
	  if(obj.offsetParent)
	      while(1) 
	      {
	        curleft += obj.offsetLeft;
	        if(!obj.offsetParent)
	          break;
	        obj = obj.offsetParent;
	      }
	  else if(obj.x)
	      curleft += obj.x;
	  return curleft;
	}
	
	//determine the y coordinate position of an HTML element.
	function findPosY(obj){
	  var curtop = 0;
	  if(obj.offsetParent)
	      while(1)
	      {
	        curtop += obj.offsetTop;
	        if(!obj.offsetParent)
	          break;
	        obj = obj.offsetParent;
	      }
	  else if(obj.y)
	      curtop += obj.y;
	  return curtop;
	}

	//remove the calendar from the document.
	function removeCalendar(){
		calendarDiv = document.getElementById(calendarDivId);
		if(calendarDiv){
			calendarDiv.parentNode.removeChild(calendarDiv)

		}
	}

	//change the background color of a calendar day cell.
	function changeCalendarBGColor(isOn, cell, normal_color, high_color) {
		if (document.getElementById) {
			cell=document.getElementById(cell);
			if (isOn) {
				cell.style.backgroundColor = high_color;
			} else {
				cell.style.backgroundColor = normal_color;
			}
		}
	}
	
	//reload the calendar to the month of the supplied date.
	function gotoMonth(date, formElementName) {
		popCalendar(formElementName, date)
	}
	
	//put the selected date into the form and remove the calendar from the document.
	function populateDate(date, formElementName) {
		var formElement = eval(formElementName);
		formElement.value = date;
		removeCalendar();
	}	
	
function logout() {
    if (confirm('Are you sure you want to Logout?')) {
	    location.href = '/ext/Logout';
    }
}


