//-----COPYRIGHT NATIONAL FINANCE CHOICE----
//-----AUTHOR: TREVOR BOOTH ----------------
//*************************************************
//Script Client Functions
//  This js file controls all the commonly used
//	code across Eagle Live
//*************************************************
//public variables
var mnuObj
var DefaultAreaCode = "03";
var SSheet = "";
var FileMnuPos = 0;
var WindowMnuPos = 0;
var TxnMnuPos = 0;
var ViewMnuPos = 0;
var EntityReportMnuPos = 0;
var detect = navigator.userAgent.toLowerCase();
var AddressId = 0 //current address id
var StyleDisplay = "Block";
var ContextID
var TxnItemId = 0;
var EntityId = 0;
var vEmail //used for the short cut menu to email
var vWizard //used to detect what type of client has been moused over
var vTxnId = 0;
var EntityChanged = false;
var isOpera = navigator.userAgent.indexOf("Opera") > -1; 
var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera; 
var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera; 
var MListArray = new Array();
var DefaultAreaCode = "03"; //the default area code for phone numbers
document.onclick = processClicks
if(!checkIt('msie')){ //internet explorer
	StyleDisplay = "table-row";
}
function processClicks(e){
	//colapse all div lists in the event that they click on the page away from a list
	if(event.srcElement.tagName != "BUTTON" && event.srcElement.tagName != "INPUT"){
		hideAllDivs('divMListBox');
	}
}
function URLSafeString(str){
	str = str.replace("@","%40");//%40 is URL encoding for the at sign
	str = str.replace("=","%3D");//%3D is URL encoding for equals
	str = str.replace(";","%3B");//%3B is URL encoding for Semi Colon
	str = str.replace(":","%3A");//%3A is URL encoding for colon
	str = str.replace("/","%2F");//%2F is URL encoding for forward slash
	str = str.replace("\\","%5C");//%5C is URL encoding for back slash
	str = str.replace(",","%2C");//%2C is URL encoding for comma
	str = str.replace("+","%2B");//%2B is URL encoding for +
	str = str.replace("$","%24");//%24 is URL encoding for dollar
	str = str.replace("&","%26");//%26 is URL encoding for ampersand
	str = str.replace("%","%25");//%25 is URL encoding for the percentage sign
	return str;
}
function checkIt(string){
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
function SetReadOnly(val){
	for(var i=0; i<document.frmEL.elements.length; i++){
		if(document.frmEL.elements[i].type == "select-one" || document.frmEL.elements[i].type == "radio"){
			document.frmEL.elements[i].disabled = val;
		}else{
			document.frmEL.elements[i].disabled = val;
		}
	}
}
function SentanceCase(me){
	//capitalise the first letter
	if(me.value.length > 0){
		me.value = me.value.substring(0,1).toUpperCase() + me.value.substring(1, me.value.length);
	}
}
function EnableFields(val){
	var arr = new array(val);
	for(var i=0; i<arr.length; i++){
		eval("document.frmEL." + arr(i) + ".disabled = false;");
	}
}
function ViewApplication(TxnId, NoMess, InsuranceCount, IsMarketing){
	if(InsuranceCount >0){
		//insurance only application
		var OpenMe = true;
		if (parent.ApplicationWindow != null){
			if(!parent.ApplicationWindow.closed){
				OpenMe = false;
				if(NoMess){
					sLoading();
					document.frmEL.submit();
				}else{
					alert("You cannot open more than one application or insurance at a time, please close the open application and try again, if this fails please close Horizon and login again");		
				}
			}
		}
		if(OpenMe){parent.ApplicationWindow = window.open('/EagleLive/PublicApplication/framework.aspx?ins=1&Id=' + TxnId,'','height=700,width=840,scrollbars=yes,left=20, top=50'); }			
	}else{
		//finance application
		var OpenMe = true;
		if (parent.ApplicationWindow != null){
			if(!parent.ApplicationWindow.closed){
				OpenMe = false;
				if(NoMess){
					sLoading();
					document.frmEL.submit();
				}else{
					alert("You cannot open more than one application or insurance at a time, please close the open application and try again, if this fails please close Horizon and login again");		
				}
			}
		}
		if(IsMarketing){
			if(OpenMe){parent.ApplicationWindow = window.open('/EagleLive/Txn/Wizard/Main.aspx?isM=1&TxnId=' + TxnId,'','height=' + parseInt(650) + ',width=' + parseInt(930) + 'px,resizable=1,scrollbars=no,left=20, top=20');}			
		}else{
			if(OpenMe){parent.ApplicationWindow = window.open('/EagleLive/Txn/Wizard/Main.aspx?TxnId=' + TxnId,'','height=' + parseInt(650) + ',width=' + parseInt(930) + 'px,resizable=1,scrollbars=no,left=20, top=20');}			
		}
	}
}	
function ValidateABN(me){
	var SVal =0;
	var Digit = 0
	if(me.value.length == 11){
		if(IsNumeric(me.value)){
			for(var i=0; i<11;i++){
				Digit = parseInt(me.value.substring(i, i+1))
				if(i==0){
					SVal = (Digit -1)*10
				}else{
					SVal += (Digit * (((i+1) * 2)-3))
				}
			}
			if(SVal%89 != 0){ //if the remainder is not equal to zero error.
				Invalid = true;
			}
		}else{
			Invalid = true;		
		}
	}else{
		Invalid = true;
	}
	if(Invalid){
		alert("Invalid ABN");
		window.event.returnValue = false;
		me.focus();
	}
}
function ShowMenus(){
	var menuPos
	//file menu
	if(null != document.getElementById("OptionsMenu")){
		menuPos = parseInt(document.getElementById("OptionsMenu").clientHeight);
		menuPos -= parseInt(document.getElementById("OptionMenuHeader").clientHeight);
		document.getElementById("OptionsMenu").style.top = 0 - menuPos;
		FileMnuPos = 0-menuPos;
		document.getElementById("OptionsMenu").style.visibility = "inherit";
	}
	//window menu
	if(null != document.getElementById("WindowMenu")){
		menuPos = parseInt(document.getElementById("WindowMenu").clientHeight);
		menuPos -= parseInt(document.getElementById("WindowMenuHeader").clientHeight);
		document.getElementById("WindowMenu").style.top = 0 - menuPos;
		WindowMnuPos = 0-menuPos;
		document.getElementById("WindowMenu").style.visibility = "inherit";
	}	
	//Transaction menu
	if(null != document.getElementById("TxnMenu")){
		menuPos = parseInt(document.getElementById("TxnMenu").clientHeight);
		menuPos -= parseInt(document.getElementById("TxnMenuHeader").clientHeight);
		document.getElementById("TxnMenu").style.top = 0 - menuPos;
		TxnMnuPos = 0-menuPos;
		document.getElementById("TxnMenu").style.visibility = "inherit";
	}	
	//View menu
	if(null != document.getElementById("ViewMenu")){
		menuPos = parseInt(document.getElementById("ViewMenu").clientHeight);
		menuPos -= parseInt(document.getElementById("ViewMenuHeader").clientHeight);
		document.getElementById("ViewMenu").style.top = 0 - menuPos;
		ViewMnuPos = 0-menuPos;
		document.getElementById("ViewMenu").style.visibility = "inherit";
	}	
	//Entity Report menu
	if(null != document.getElementById("EntityReportMenu")){
		menuPos = parseInt(document.getElementById("EntityReportMenu").clientHeight);
		menuPos -= parseInt(document.getElementById("EntityReportMenuHeader").clientHeight);
		document.getElementById("EntityReportMenu").style.top = 0 - menuPos;
		EntityReportMnuPos = 0-menuPos;
		document.getElementById("EntityReportMenu").style.visibility = "inherit";
	}		
	
}
function SendMail(){
	window.location.href='mailTo:' + vEmail;
}
function GetMyDate(PaymentDate){
	//takes a string and converts it into a date object
	var theDate = new Date();
	var dList = PaymentDate.split("/");
	if(dList[0].substring(0,1) == "0"){dList[0] = dList[0].substring(1,dList[0].length)}
	if(dList[1].substring(0,1) == "0"){dList[1] = dList[1].substring(1,dList[1].length)}
	theDate.setDate(dList[0]);
	theDate.setMonth(parseInt(dList[1])-1);
	theDate.setFullYear(parseInt(dList[2]));
	return theDate
}
function DisableMList(Nme, status){
	eval("frmEL." + Nme + "Display.disabled = status");
	eval("frmEL.img" + Nme + ".disabled = status");	
}
function FlipBW(FlipBack){
	HideWindowMenu();
	if (document.styleSheets(0).href != null){
		if(!FlipBack){
			SSheet = document.getElementById("StyleSheet").href;
			document.getElementById("StyleSheet").href = "/eaglelive/preferences/styles/bw.css";
		}else{
			document.getElementById("StyleSheet").href = SSheet;
		}
	}
		
}
function Highlight(ShowHide, tbox, dd){
	if(ShowHide){//hightlight the box
		tbox.className = "HighlightedTBox";
		if(dd){
			dd.className = "combo-buttonOver";
		}
	}else{//unhighlight the box
		tbox.className = "Box";
		if(dd){
			dd.className = "combo-button";
		}	
	}
}
function HidePopUp(lyr){
	event.cancelBubble = true;
	if(event.toElement != null){
		if("undefined" != typeof event.toElement.id){ //make sure the object exists
			if(event.toElement.id.length > 5){ //if the id is long enough 
				if(event.toElement.id.substring(0,9) != 'mnuOption' && 
				event.toElement.id.substring(0,9) != 'WindowMenu' && 
				event.toElement.id.substring(0,9) != 'TxnOption' &&
				event.toElement.id.substring(0,5) != 'PopUp' &&
				event.toElement.id.substring(0,13) != 'DocOptionMenu'){
					HideWindowMenu(); //if the object is not one allow to mouse over hide the menu
				}
			}else{
				if(event.toElement.tagName != "IMG"){ //if they haven't highlighted one of the menu icons
					HideWindowMenu();
				}
			}
		}else{
			HideWindowMenu();
		}
	}else{
		HideWindowMenu();
	}
}
function HideWindowMenu(){
	hideAllDivs("PopUp"); //close the second layer
	hideAllDivs("TxnOptionMenu");
	hideAllDivs("DocOptionMenu");
	if(document.getElementById('TableOptionsMenu') != null){document.getElementById('TableOptionsMenu').style.border = '1 solid white'; }
	if(document.getElementById('OptionMenuHeader') != null){document.getElementById('OptionMenuHeader').className = 'MenuSideBarHeader'; }
	if(document.getElementById('OptionsMenu') != null){document.getElementById('OptionsMenu').style.top = FileMnuPos;}
	if(document.getElementById('TableTxnMenu') != null){document.getElementById('TableTxnMenu').style.border = '1 solid white'; }
	if(document.getElementById('TxnMenuHeader') != null){document.getElementById('TxnMenuHeader').className = 'MenuSideBarHeader'; }
	if(document.getElementById('TxnMenu') != null){document.getElementById('TxnMenu').style.top = TxnMnuPos;}	
	if(document.getElementById('TableWindowMenu') != null){document.getElementById("TableWindowMenu").style.border = '1 solid white';}
	if(document.getElementById('WindowMenuHeader') != null){document.getElementById("WindowMenuHeader").className = 'MenuSideBarHeader'; }
	if(document.getElementById('WindowMenu') != null){document.getElementById("WindowMenu").style.top = WindowMnuPos}
}
function ShowPopUp(Id){
	var lft = event.srcElement.offsetWidth -5;
	var tp = event.offsetY;
	if(tp > event.clientY){ //adjusting for stupid ie errors
		tp = 14;
	}
	if((event.clientY - tp) < 0){ //make sure we stay in the screen
		document.getElementById(Id).style.top = 0;
	}else{
		document.getElementById(Id).style.top = event.clientY - tp;
	}
	document.getElementById(Id).style.left = event.clientX + (lft - event.offsetX);
	document.getElementById(Id).style.zIndex = "100000";
	document.getElementById(Id).style.visibility = "inherit";
	document.getElementById(Id).style.display = "block";
}
function formatCurrency(strValue){
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);
	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}
function MyParseFloat(strValue){
	if("undefined" != typeof strValue){ //if our value is not defined
		var tmp = strValue.toString().replace(/\$|\,/g,'')
		tmp = parseFloat(tmp);
		if (isNaN(tmp)){tmp =0;} //if not numeric default it to zero
		return parseFloat(tmp);
	}else{
		return 0;
	}
}
function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   if(isNaN(sText)){
		IsNumber = false;
   }
   for (i = 0; i < sText.length && IsNumber == true; i++){ 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1){
         IsNumber = false;
      }
   }
   if(sText == ""){IsNumber=false}
   return IsNumber;
}
function ForceNumeric(sObj){
	if(sObj.value != ""){
		if(!IsNumeric(sObj.value)){
			alert("Please Enter A Number, do not use dollar signs or commas");
			window.event.returnValue = false;
			sObj.focus();
		}
	}
}
function hideAllDivs(prefix){
	var l = prefix.length; //search for all divs starting with this prefix
	for (var i=0; i<document.getElementsByTagName("DIV").length; i++){
		obj = document.getElementsByTagName("DIV")[i];
		if (!obj || !obj.offsetParent) continue;
			if (obj.id.substring(0,l) == prefix){
				obj.style.display = "none"; //visibility
				obj.style.zIndex = "-100000";
			}
	}
}

function sLoading(){
	if ("undefined" != typeof IsLoading){ //if were the home page
		HideWindowMenu();
		document.getElementById("ELBody").style.cursor = 'wait';	
		document.getElementById("DivLoadingAlert").style.zIndex = 100;
		document.getElementById("DivLoadingAlert").style.visibility = 'inherit';
	}
}

function hLoading(){
	if ("undefined" != typeof IsLoading){ //if were the home page
		document.getElementById("ELBody").style.cursor = '';	
		document.getElementById("DivLoadingAlert").style.zIndex = -1000;
		document.getElementById("DivLoadingAlert").style.visibility = 'hidden';
	}
}

function CheckChanges(){
	var tmp = false;
	if(EntityChanged){
		var agree=confirm("Warning The Changes You Made Will Be Lost, Click OK To Continue Without Saving Your Change or Cancel Then Save To Save Your Changes");
		if(agree){
			tmp = true;
		}else{
			tmp = false;
		}		
	}else{ //if no changes have been made proceed
		tmp = true;
	}
	return tmp;
}
function MenuFX(){
	mnuObj.style.pixelTop = 0;
}
function showHover(myCell){
	eval("document.getElementById('mnuOption" + myCell + "cell1').className = 'MenuSideBarOver'")
	eval("document.getElementById('mnuOption" + myCell + "cell2').className = 'MenuSideBarOver'")
}
function hideHover(myCell){
	eval("document.getElementById('mnuOption" + myCell + "cell1').className = 'MenuSideBar'")
	eval("document.getElementById('mnuOption" + myCell + "cell2').className = 'MenuSideBarContents'")
}
function MultiSelectList(MListName, RadioOption, Editable, SelectedValue, 
						 ItemDisplay, ItemValue, width, MenuBorderColor, MenuBackColor, 
						 MenuTextColor, MenuHoverColor, MenuTextHoverColor, BeforeChange, OnChange,
						 DefaultValue){
	this.MListName = MListName; //The name of the drop down list
	this.RadioOption = RadioOption; //is there a default of this list
	this.Editable = Editable; //can we change the items on this list
	this.SelectedValue = SelectedValue; //which item has been selected
	this.ItemDisplay = ItemDisplay; //Values to show in the list
	this.ItemValue = ItemValue; //Values to store in the database
	this.width = width;
	this.MenuBorderColor = MenuBorderColor;
	this.MenuBackColor = MenuBackColor;
	this.MenuTextColor = MenuTextColor;
	this.MenuHoverColor = MenuHoverColor; //the color each option is when highlighted
	this.MenuTextHoverColor = MenuTextHoverColor; //as above but for the text
	this.BeforeChange = BeforeChange; //event to fire before the list changes
	this.OnChange = OnChange;
	this.DefaultValue = DefaultValue;
}
function validatePage(NoMList){
	//run any form validation here and output the custom controls
	var pass = true;
	var tmp = ""; var myStyle
	for(var i=0; i<document.forms[0].length; i++){ //loop through form elements
		if(document.forms[0].elements[i].style.required  && document.forms[0].elements[i].value == ""){
			document.forms[0].elements[i].style.border = "1px Solid Red";//display error border on text box	
			pass = false;
		}else{
			if(document.forms[0].elements[i].style.required){ document.forms[0].elements[i].style.border = "1px Solid white";}	
		}
	}
	if(pass){
		if ("undefined" == typeof NoMList){ //process this unless caller has explicity asked us not to
			outputMList(); //output drop down lists
		}
		if ("undefined" != typeof AddressList){
			outputAddress(); //output address's
		}
		return true //ok to do what ever the caller requires
	}else{
		window.event.returnValue = false;
		alert("Please Fill In All Required Fields");
		return false //return an error to the caller
	}
}
function saveMList(t) {
	var str = "";
	var IVal = "";
	for(var k=0; k<t.ItemDisplay.length; k++){
		if(t.ItemValue[k] != ""){ //make sure that it is not a blank row
			str += "<Item id=\"" + t.ItemValue[k] + "\""; //set the id
			if(t.SelectedValue == GetMListValue(t.MListName, t.ItemDisplay[k]).replace("&amp;","&")) {
				str += " selected=\"true\"";
			}else{
				str += " selected=\"false\"";
			}
			if(t.DefaultValue == GetMListValue(t.MListName, t.ItemDisplay[k])) {
				str += " default=\"true\"";
			}else{
				str += " default=\"false\"";
			}
			IVal = t.ItemDisplay[k];
			IVal = IVal.replace("&","&amp;") //remove any ampersands
			IVal = IVal.replace("&amp;amp;","&amp;")
			str += ">" + IVal + "</Item>";
		}
	}
	eval("frmEL." + t.MListName + "Display.style.color = 'white'");
	eval("frmEL." + t.MListName + "Display.value = str")
}
function outputMList(){
	for(var i=0; i< MListArray.length; i++){
		saveMList(MListArray[i]);
	}
}
function UpdateMListSelected(MName, MItem){
	for(var i=0; i< MListArray.length; i++){
		if(MListArray[i].MListName == MName){
			for(var k=0; k< MListArray[i].ItemDisplay.length; k++){		
				if(MListArray[i].ItemDisplay[k] == MItem || MListArray[i].ItemValue[k] == MItem){
					if(MListArray[i].SelectedValue != MListArray[i].ItemValue[k]){
						if(MListArray[i].OnChange != ""){
							eval(MListArray[i].OnChange);
						}
					}
					MListArray[i].SelectedValue = MListArray[i].ItemValue[k]
				}
			}
		}
	}
}
function GetMListValue(MName, MItem){
	for(var i=0; i< MListArray.length; i++){
		if(MListArray[i].MListName == MName){
			for(var k=0; k< MListArray[i].ItemDisplay.length; k++){		
				if(MListArray[i].ItemDisplay[k] == MItem || MListArray[i].ItemValue[k] == MItem){
					return MListArray[i].ItemValue[k]
				}
				if(MListArray[i].ItemDisplay[k].replace("'","") == MItem || MListArray[i].ItemValue[k].replace("'","") == MItem){
					return MListArray[i].ItemValue[k]				
				}
			}
		}
	}
}
function GetMListDisplay(MName, MItem){
	for(var i=0; i< MListArray.length; i++){
		if(MListArray[i].MListName == MName){
			for(var k=0; k< MListArray[i].ItemValue.length; k++){		
				if(MListArray[i].ItemValue[k] == MItem){
					return MListArray[i].ItemDisplay[k]
				}
			}
		}
	}
}
function UpdateMListItem(MListName, NewValue){
	for(var i=0; i< MListArray.length; i++){
		if(MListArray[i].MListName == MListName){
			//is it the list were dealing with
			if(MListArray[i].SelectedValue == "New"){
				//are we dealing with a new list item
				if(NewValue != ''){
					//add our new item
					MListArray[i].SelectedValue = "0-" + NewValue; //update the selected value
					MListArray[i].ItemValue[MListArray[i].ItemValue.length] = "0-" + NewValue;
					MListArray[i].ItemDisplay[MListArray[i].ItemDisplay.length] = NewValue;
				}
			}else{
				for(var k=0; k< MListArray[i].ItemDisplay.length; k++){
					if(MListArray[i].ItemValue[k] == MListArray[i].SelectedValue){
						//update our selected item
						if(MListArray[i].Editable){
							MListArray[i].ItemDisplay[k] = NewValue;	
						}else{
							MListArray[i].ItemValue[k] = NewValue;
							MListArray[i].ItemDisplay[k] = NewValue;
						}
					}
				}
			}
		}
	}
}
function WriteMList(MListName, evnt){
	var OS = "";
	var checked = "";
	var tmp = "";
	var LString = 0
	document.getElementById("ELBody").style.cursor = 'wait';
	for(var i=0; i< MListArray.length; i++){
		//update the div
		if(MListArray[i].MListName == MListName){
			if(MListArray[i].Editable || document.getElementById("divMListBox" + MListArray[i].MListName).innerHTML.length <= 1){
				OS = "<table width=\"100%\" style=\"color: " + MListArray[i].MenuTextColor + ";\" bgColor=\"" + MListArray[i].MenuBackColor + "\">";
				for(var k=0; k< MListArray[i].ItemValue.length; k++){
					//loop though list items
					if(MListArray[i].ItemValue[k] != '' && MListArray[i].ItemDisplay[k] !=''){
						//finding the biggest string
						if(MListArray[i].ItemDisplay[k].length > LString){LString = MListArray[i].ItemDisplay[k].length}
						OS += "<tr>";
						OS += "<td nowrap width=\"90%\" style=\"height: 10px; cursor: pointer\"";
						OS += " onclick=\"" + MListArray[i].MListName + "Display.value = '" + MListArray[i].ItemDisplay[k].replace("'","\\") + "';" + MListArray[i].MListName + "Display.focus() ; document.getElementById('divMListBox" + MListArray[i].MListName + "').style.display = 'none'; ";
						OS += " MListArray[" + i + "].SelectedValue = '" + MListArray[i].ItemValue[k] + "'; " + MListArray[i].OnChange + "\"";
													
						if(MListArray[i].ItemDisplay[k].length > 50){
							OS += " onmouseover=\"this.className = 'DDMenuOver';\" onmouseout=\"this.className = '';\" >" + MListArray[i].ItemDisplay[k].substring(0,50);
						}else{
							OS += " onmouseover=\"this.className = 'DDMenuOver';\" onmouseout=\"this.className = '';\" >" + MListArray[i].ItemDisplay[k];
						}
						if(MListArray[i].RadioOption){//show the radio button
							//if our value is the default of this list highlight the radio option
							if(MListArray[i].DefaultValue == MListArray[i].ItemValue[k]){
								tmp = "checked";
							}else{
								tmp = ""
							}
							OS += "</td><td width=\"10%\" style=\"height: 10px;\" align=\"right\"><input " + tmp + " onclick=\"MListArray[" + i + "].DefaultValue = MListArray[" + i + "].ItemValue[" + k + "]\" type=\"radio\" style=\"height: 10px;\" value=\"" + MListArray[i].MListName + "\" name=\"" + MListArray[i].MListName + "SItem\" " + checked + ">";
    					}
						OS += "</td></tr>";
					}
				}
				if(MListArray[i].Editable){
					//Add a new item option
					OS += "<tr>";
					OS += "<td width=\"95%\" style=\"height: 10px; cursor: pointer\"";
					OS += " onclick=\"" + MListArray[i].MListName + "Display.value = ''; " + MListArray[i].MListName + "Display.focus(); MListArray[" + i + "].SelectedValue = 'New'\"";
					OS += " onmouseover=\"this.className = 'DDMenuOver';\" onmouseout=\"this.className = '';\"  > - New Item -";
					OS += "</td></tr>";
				}
				//update the div html
				eval("divMListBox" + MListArray[i].MListName + ".innerHTML = OS");
			}
			alignDiv(i,false, evnt);
		}
	}
	document.getElementById("ELBody").style.cursor = '';
}
function alignDiv(i, SecondPass, evnt){
	var tmp = 0; 
	if(MListArray[i].Editable){
		var hTmp = parseInt(MListArray[i].ItemValue.length * 20) //find how big our box should be
		hTmp += 20; //add space for the new item option
	}else{
		var hTmp = parseInt(MListArray[i].ItemValue.length * 20) //find how big our box should be
	}
	hTmp += 2 // add buffer at the bottom
	if(hTmp > 150){hTmp = 150} //make sure we go no bigger than 150
	if (typeof evnt.offsetY != 'undefined') {  //IE
		tmp = parseInt(evnt.y+document.body.scrollTop); //if we've scrolled include how far down
		tmp += evnt.srcElement.clientHeight; // add the height of the element
		tmp -= parseInt(evnt.offsetY); //remove how far down the element they clicked 
	}
	if (typeof evnt.layerY != 'undefined') { //mozilla
		tmp += evnt.srcElement.clientHeight; // add the height of the element
	}
	//get the height of the drop down list
	eval("var tmp2 = document.getElementById(\"divMListBox" + MListArray[i].MListName + "\").clientHeight");
	//attempt at stopping the drop down list from going off the bottom of the screen
	//dont include the scroll height when checking if were of the bottom of the screen
	if(parseInt(tmp-document.body.scrollTop) + tmp2 > document.body.clientHeight){ //if were over
		eval("var bH = document.getElementById('" + MListArray[i].MListName + "Display').clientHeight");
		tmp -= (tmp2 + bH); //add in the size of the text box
	}
	eval("document.getElementById(\"divMListBox" + MListArray[i].MListName + "\").style.top = " + tmp);
	eval("document.getElementById(\"divMListBox" + MListArray[i].MListName + "\").style.height = " + hTmp);
	eval("document.getElementById(\"divMListBox" + MListArray[i].MListName + "\").style.width = document.getElementById(\"" + MListArray[i].MListName + "Display\").clientWidth");
	if(typeof evnt.layerY != 'undefined'){ //mozilla adjustment
		eval("document.getElementById(\"divMListBox" + MListArray[i].MListName + "\").style.width = document.getElementById(\"" + MListArray[i].MListName + "Display\").clientWidth + 20");
	}
	//check the screen to see if our list goes over the right of the page	
	var wSize, wLeft
	eval("wLeft = event.x - event.offsetX - document.getElementById(\"" + MListArray[i].MListName + "Display\").offsetWidth");
	eval("wSize = document.getElementById(\"divMListBox" + MListArray[i].MListName + "\").offsetWidth");
	var tBoxSize = 0;
	if (typeof evnt.layerY != 'undefined') { //mozilla adjustments
		eval("wLeft = document.getElementById(\"" + MListArray[i].MListName + "Display\").offsetWidth");
		wLeft = evnt.pageX - wLeft - 6;//wLeft - parseInt(wSize)
		eval("document.getElementById(\"divMListBox" + MListArray[i].MListName + "\").style.left = findPosX()");
	}
	if(parseInt(wLeft+wSize)>document.body.clientWidth){
		wLeft += 14; //dont ask me why, something to do with the button
		eval("tBoxSize = document.getElementById(\"" + MListArray[i].MListName + "Display\").offsetWidth");
		eval("document.getElementById(\"divMListBox" + MListArray[i].MListName + "\").style.left = wLeft - parseInt(wSize-tBoxSize)");
	}
	//temporary fix, when the div is above the box it appears away from the box, second click seems to fix this
	if(!SecondPass){alignDiv(i,true, evnt)}
}
function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

//auto complete code
function textboxSelect (oTextbox, iStart, iEnd) { 
   switch(arguments.length) { 
       case 1: 
           oTextbox.select(); 
           break; 
       case 2: 
           iEnd = oTextbox.value.length; 
           /* falls through */ 
       case 3:          
           if (isIE) { 
               var oRange = oTextbox.createTextRange(); 
               oRange.moveStart("character", iStart); 
               oRange.moveEnd("character", -oTextbox.value.length + iEnd);      
               oRange.select();                                              
           } else if (isMoz){ 
               oTextbox.setSelectionRange(iStart, iEnd); 
           }                     
   } 
   oTextbox.focus(); 
} 
function textboxReplaceSelect (oTextbox, sText) { 
   if (isIE) { 
       var oRange = document.selection.createRange(); 
       oRange.text = sText; 
       oRange.collapse(true); 
       oRange.select();                                 
   } else if (isMoz) { 
       var iStart = oTextbox.selectionStart; 
       oTextbox.value = oTextbox.value.substring(0, iStart) + sText + oTextbox.value.substring(oTextbox.selectionEnd, oTextbox.value.length); 
       oTextbox.setSelectionRange(iStart + sText.length, iStart + sText.length); 
   } 
   oTextbox.focus(); 
} 
function autocompleteMatch (sText, arrValues) { 
   for (var i=0; i < arrValues.length; i++) { 
       if (arrValues[i].toUpperCase().indexOf(sText.toUpperCase()) == 0) { 
           return arrValues[i]; 
       } 
   } 
   return null; 
} 
function autocomplete(oTextbox, oEvent, arrValues) { 
   switch (oEvent.keyCode) { 
       case 38: //up arrow  
       case 40: //down arrow 
       case 37: //left arrow 
       case 39: //right arrow 
       case 33: //page up  
       case 34: //page down  
       case 36: //home  
       case 35: //end                  
       case 13: //enter  
       case 9: //tab  
       case 27: //esc  
       case 16: //shift  
       case 17: //ctrl  
       case 18: //alt  
       case 20: //caps lock 
       case 8: //backspace  
       case 46: //delete 
           return true; 
           break; 

       default: 
	       var oRange = document.selection.createRange(); 
	       var sNew = oTextbox.value.substring(0, oTextbox.value.length - oRange.text.length) + String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode);
           var sMatch = autocompleteMatch(sNew, arrValues); 
           if (sMatch != null) { 
	           textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode)); 
		       var iLen = oTextbox.value.length; 
               oTextbox.value = sMatch; 
               textboxSelect(oTextbox, iLen, oTextbox.value.length); 
           }
           return false; 
   } 
} 
function GetMListArray(MListName){
	for(var i=0; i< MListArray.length; i++){
		if(MListArray[i].MListName == MListName){
			return MListArray[i].ItemDisplay;	
		}
	}
}
function DateDiff(date1, date2){
	//returns difference in seconds only.
	var objDate1=new Date(date1);
	var objDate2=new Date(date2);
	return (objDate1.getTime()-objDate2.getTime())/1000;
}

//******************* Auto Formatting *******************
function ValidateDay(dy, mth){
	if(mth == 1 || mth == 3 || mth == 5 || mth == 7 || mth == 8 || mth == 10 || mth == 12){
		if(parseInt(dy) > 31){
			return false;
		}
	}else if(mth ==2){
		if(parseInt(dy) > 29){
			return false;
		}
	}else{
		if(dy > 30){
			return false;
		}
	}
	return true;
}
function UpdateDate(frmObj){
	var tmp = frmObj.value;
	var str = ""
	if(tmp == "t"){ //get today's date
		frmObj.value = "+0d";
		UpdateDate(frmObj);
	}
	if(tmp.substring(0,1) == "+" || tmp.substring(0,1) == "-" ){
		var d = tmp.substring(1, parseInt(tmp.length-1))
		if(tmp.length == "1"){
			var today = (new Date()).getTime();
			eval("var fd = new Date(today " + tmp.substring(0,1) + "DAY)");
		}
		else{
			switch(tmp.substring(parseInt(tmp.length-1), tmp.length)){
				case "d": //were working with days
					var fd = DateAdd(new Date(),d,0,0);break;
				case "w": //were working with days
					var fd = DateAdd(new Date(),(d*7),0,0);	break;			
				case "m": //were working with months
					var fd = DateAdd(new Date(),0,d,0);break;
				case "y": //were working with years
					var fd = DateAdd(new Date(),0,0,d);break;
			}
		}
		var vMth = "0" + (fd.getMonth()+1); vDay = "0" + fd.getDate()
		vMth = vMth.substring((vMth.length-2), vMth.length);
		vDay = vDay.substring((vDay.length-2), vDay.length);
		str = vDay + "/" + vMth + "/" + fd.getYear();
	}
	if(str != ""){frmObj.value = str;}
	var s, y,t
	s = frmObj.value;
	for(i=0; i < s.length; i++){ //looping through each character
		s = s.replace("\\","."); s = s.replace("/","."); s = s.replace(" ",""); s = s.replace("-",".");
	}
	var myArray = s.split(".");
	if(myArray.length == 3){
		var tst
		if(myArray[1].substring(0,1) == "0"){
			myArray[1] = myArray[1].substring(1,2);
		}
		if(myArray[0].substring(0,1) == "0"){
			myArray[0] = myArray[0].substring(1,2);
		}		
		// validate the day 
		if(!ValidateDay(parseInt(myArray[0]), parseInt(myArray[1])) || !IsNumeric(myArray[0])){
			alert("Invalid day of month");
			myArray[0] = 1;
		}
		// validate the month
		if(parseInt(myArray[1]) > 12 || !IsNumeric(myArray[1])){
			alert("Invalid month of the year");
			myArray[1] = 1;
		}
		tst = "0" + myArray[0]; //get the day
		t = tst.substring(tst.length-2,tst.length); //make sure it 2 digits
		tst = "0" + myArray[1]; //get the month
		t += "/" + tst.substring(tst.length-2,tst.length); //make sure it's two digits
		t += "/" 
		if(myArray[2].length == 2){
			if(myArray[2].substring(0,1) == "0"){
				myArray[2] = myArray[2].substring(1,2);
			}
		}
		y = parseInt(myArray[2]); //get the year
	}else{
		if(s.length!=0){
			if(myArray.length == 2){ //if they have entered the mth and year
				var tst
				// validate the month
				if(parseInt(myArray[0]) > 12 || !IsNumeric(myArray[0])){
					alert("Invalid month of the year");
					myArray[1] = 1;
				}
				tst = "01" //set the day
				t = tst.substring(tst.length-2,tst.length); //make sure it 2 digits
				tst = "0" + myArray[0]; //get the month
				t += "/" + tst.substring(tst.length-2,tst.length); //make sure it's two digits
				t += "/" 
				y = parseInt(myArray[1]); //get the year
			}else{
				if(s.length==4){ //if we have the month and the year
					t = "01/" + s.slice(0,2) + "/"
					y = s.slice(2, s.length); //getting the year
					y = parseFloat(y);
				}else{
					var m = s.slice(2,4);
					if(m.substring(0,1) == "0"){
						m = m.substring(1,2);
					}	
					y = s.slice(4, s.length); //getting the year
					t = s.slice(0,2); //getting the day
					// validate the day 
					if(!ValidateDay(parseInt(s.slice(0,2)), parseInt(m)) || !IsNumeric(s.slice(0,2))){
						alert("Invalid day of month");
						t = "01";
					}
					t+= "/";
					if(parseInt(s.slice(2,4)) > 12 || !IsNumeric(s.slice(2,4))){
						alert("Invalid month of the year");
						t += "01";
					}else{
						t+= s.slice(2,4); //getting the month
					}
					t+= "/";
					y = parseFloat(y);
				}
			}
		}
	}
	if(s.length!=0){
		if(y>50 && y<99){ //if its a 2 digit year and the second half on the century
			y = 1900 + y;
		}else if(y<50){ //if its a 2 digit year and the first half of the century
			y = 2000 + y;
		}
		frmObj.value = t + y;
		EntityChanged=true;
	}
}
function DateAdd(startDate, numDays, numMonths, numYears){
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = parseInt(numYears);
	var month = returnDate.getMonth()	+ parseInt(numMonths);
	if (month > 11){
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	returnDate.setTime(returnDate.getTime()+60000*60*24*parseInt(numDays));
	return returnDate;
}
function SetAreaCode(){
	var StateVal = "VIC";
	if("undefined" != typeof document.frmEL.AddressStateDisplay){
		StateVal = document.frmEL.AddressStateDisplay.value;
	}
	if(StateVal == "VIC" || StateVal == "TAS"){DefaultAreaCode = "03";}
	if(StateVal == "NSW" || StateVal == "ACT"){DefaultAreaCode = "02";}
	if(StateVal == "QLD"){DefaultAreaCode = "07";}
	if(StateVal == "WA" || StateVal == "NT" || StateVal == "SA"){DefaultAreaCode = "08";}
}

function UpdateTeleAddress(frmObj, NoAreaCode){
	var prefix, mobilePrefix, n, s, t;
	s = frmObj.value;
	if(s.length == 0){return;}
	for (i = 0; i < s.length; i++){ //looping through each character
		t = s.slice(i, i+1);
		if(isNaN(t)){
			s = s.replace(t, ""); //removing any characters that are not numbers
			i-=1
		}
		s = s.replace(" ","");
	}
	prefix = s.slice(0,2);
	
	switch(prefix){
		case "04": //if were using a mobile number
			if(s.length != 10){
				alert("Mobile numbers must have 10 numbers");
				frmObj.focus();
				return false;
			}
			n = s.slice(0,4) + " " + s.slice(4, 7) + " " + s.slice(7, s.length);
			break;
		case "13": //format for 13 numbers
			if(s.slice(0,4) == "1300"){ //if it's a 1300 number
				if(s.length != 10){
					alert("1800 numbers must have 10 numbers");
					frmObj.focus();
					return false;
				}
				n = s.slice(0,4) + " " + s.slice(4, 7) + " " + s.slice(7, s.length);
				break;	
			}else{ //if it's a six digit 13 number
				if(s.length != 6){
					alert("13 numbers must have 6 numbers");
					frmObj.focus();
					return false;
				}
				n = s.slice(0,3) + " " + s.slice(3, 6);
			}
			break;
		case "18": //format for 1800 numbers
			if(s.length != 10){
				alert("1800 numbers must have 10 numbers");
				frmObj.focus();
				return false;
			}
			n = s.slice(0,4) + " " + s.slice(4, 7) + " " + s.slice(7, s.length);
			break;			
		default: //format all other numbers
			if(s.length < 8){ //if it's not long enough and isn't a 13 number
				alert("Incorrect phone number, must be at least 8 numbers")
				frmObj.focus();
				return false;
			}
			if(prefix > "10"){ //if we don't have an area code use the default area code
				if(!NoAreaCode){ //dont process if they dont want the area code returned
					SetAreaCode();
					s = DefaultAreaCode + s
				}
			}
			if(!NoAreaCode){ //dont process if they dont want the area code returned
				n = "(" + s.slice(0,2) + ") " + s.slice(2, 6) + " " + s.slice(6, s.length);
			}else{
				n = s.slice(0, 4) + " " + s.slice(4, s.length);
			}
			break;
	}
	if (s == ""){
		frmObj.value = ""; //if we have an error return blank
	}
	else{
		frmObj.value = n; //return the formatted number
	}
	EntityChanged=true;
}
function UpdateABN(frmOBJ){
	var tmp, s, i
	s = frmOBJ.value;
	for (i = 0; i < s.length; i++){s = s.replace(" " , "");}
	frmOBJ.value = s.slice(0,2) + " " + s.slice(2, 5) + " " + s.slice(5,8) + " " + s.slice(8, s.length);
}
function UpdateACN(frmOBJ){
	var tmp, s, i
	s = frmOBJ.value;
	for (i = 0; i < s.length; i++){
		s = s.replace(" " , "");
	}
	tmp = s.slice(0,3) + " " + s.slice(3, 6) + " " + s.slice(6,9);
	frmOBJ.value = tmp;
}
function ValidateAreaCode(me){
	if(me.value != ""){
		if(me.value != "1300" && me.value != "02" && me.value != "03" && me.value != "07" && me.value != "08" && me.value != "04"){
			for(var i=0; i<10; i++){
				if(me.value == "04" + i){
					return
				}
			}
			alert("Area Code must be 02, 03, 04, 07, 08 or 1300");
			window.event.returnValue = false;
			me.focus();
		}
	}
}
function ValidatePhone(me, AreaCode){
	if(me.value != ""){ //have we got a phone number first
		if(AreaCode.value == ""){
			alert("You must enter an area code first");
			window.event.returnValue = false;
			AreaCode.focus();
		}else{
			if(IsNumeric(me.value)){
				var PHLength = 8;
				if(AreaCode.value.substring(0,2) == "04"){ //if it's a mobile number then they can have 7 digits
					PHLength = 8;
				}
				if(me.value.length != PHLength){
					alert("The number must be eight characters");
					window.event.returnValue = false;
					me.focus();
				}
			}else{
				alert("Phone number must be numeric");
				window.event.returnValue = false;
				me.focus();
			}
		}
	}
}
function ValidateMobile(me){
	if(IsNumeric(me.value)){
		if(me.value.length != 10){
			alert("The number must be ten characters");
			window.event.returnValue = false;
			me.focus();
		}
	}else{
		alert("Mobile number must be numeric");
		window.event.returnValue = false;
		me.focus();
	}
}
function ValidateYear(frmOBJ){
	var d = new Date();
	if(frmOBJ.value != ""){ //do we have a value
		if(frmOBJ.value.length == 2){ //have they put in a 2 digit year
			if(parseInt(frmOBJ.value) > 50){
				frmOBJ.value = "19" + frmOBJ.value
			}else{
				frmOBJ.value = "20" + frmOBJ.value
			}
		} //is it a 4 digit year
		if(frmOBJ.value.length == 4){
			if(parseInt(frmOBJ.value) > d.getFullYear()){
				alert("Year must be in the past");
			}
		}else{ //if not dont let past
			alert("Please Enter A Valid Year EG. 2004");
			frmOBJ.focus();
		}
	}
}
function SelectPostCode(Suburb, State, PostCode, Prefix){
	//suburb
	document.frmEL(Prefix + "SuburbTown").value = Suburb;
	//post code
	document.frmEL(Prefix + "PostCode").length = 0;
	var oOpt = new Option(PostCode,PostCode);
	document.getElementById(Prefix + "PostCode").options.add(oOpt,0); 	
	//state
	document.frmEL(Prefix + "State").length = 0;
	var oOpt = new Option(State,State);
	document.getElementById(Prefix + "State").options.add(oOpt,0); 	
}
function ShowPostCodeSearch(Suburb, Prefix){
	window.open('/EagleLive/Txn/Wizard/PostCodeSearch.aspx?Prefix=' + Prefix + '&Suburb=' + Suburb,'','height=480,width=600,scrollbars=yes,left=50, top=10')
}
