//identify browser
var isNS4 = (document.layers) ? true : false;
var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;

var secureProtocol = document.location.href.substr(0, document.location.href.indexOf(":"));
var replaceUrl = ""
if (window.IsSecure) {
	//alert(IsSecure + "," + secureProtocol);	
	if (secureProtocol == "http") {
		replaceUrl = "https" + document.location.href.substr(document.location.href.indexOf(":"), document.location.href.length);
		document.location.replace(replaceUrl);
	}
} else {
	if (secureProtocol == "https") {
		replaceUrl = "http" + document.location.href.substr(document.location.href.indexOf(":"), document.location.href.length);
		document.location.replace(replaceUrl);
	}
}
	
	//alert("secureProtocol:" + secureProtocol + ", " + "replaceUrl: " + replaceUrl)

function addPageEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	} else {
		//alert("Handler could not be removed");
	}
} 

//***********************
function divDisplayOn(divID) {

	if (isIE4) {
		elm = document.all[divID];
		elm.style.display = "block";

	} else if (isIE5 || isNS6) {
		elm = document.getElementById(divID);
		elm.style.display = "block";
	}
}


function divDisplayOff(divID) {

	if (isIE4) {
		elm = document.all[divID];
		elm.style.display = "none";

	} else if (isIE5 || isNS6) {
		elm = document.getElementById(divID);
		elm.style.display = "none";
	}
}

//********************************

function VHODoc(VHOfile) {
	//escape squotes, dquotes
	var myRegExp = new RegExp();
	myRegExp = /\'/g;
	
	FileStreamUrl = "https://remoteoffice.sunsetmortgage.com/Sunset/Meta40Portal/Admin/PortalFileDownload.aspx?FileName="

	VHOfile = VHOfile.replace(myRegExp, "\\'");
	//alert(FileStreamUrl + VHOfile)
	//alert('http://themaestro.isolutionsinc.com/Sunset/Meta40Portal/Admin/PortalFileDownload.aspx?FileName=' + VHOfile)
	window.open(FileStreamUrl + VHOfile,'vhowindow','scrollbars=yes,menubar=yes,resizable=yes,status=yes,width=800,height=600,left='+(screen.availWidth/2-400)+',top='+(screen.availHeight/2-300)+'')
	
}

//This generic function will return the value of a QueryString
function getUrlParam(Val) {
	thisURLparamStr = document.location.search;
	//chop "?" off thisURLparamStr
	if (thisURLparamStr.charAt(0) == "?") thisURLparamStr = thisURLparamStr.substring(1, thisURLparamStr.length);
	returnStr = "";
	if (thisURLparamStr != "") {
		//Build array out of thisURLparamStr using "&" as delimiter
		divide1=(thisURLparamStr.split("&"))
		for (i=0; i < divide1.length; i++) {
			divide2 = divide1[i].split("=")
			if (unescape(divide2[0]) == Val) {
				//returnStr = unescape(divide2[1]);
				returnStr = decodeURIComponent(divide2[1]);
				
			}
		}
	}
	return returnStr;
}


function addOptions(fromMenu, toMenu) {

	//fromMenu = document.forms.UserProfile.AvailablePrivs;
	//toMenu = document.forms.UserProfile.FUNCTIONS;
	fromMenuArray = new Array();
	toMenuArray = new Array();
	for (i=0; i<fromMenu.options.length; i++) {
		if (fromMenu.options[i].selected == true) {
			//toMenuArray[toMenuArray.length] = new Array(fromMenu.options[i].value, fromMenu.options[i].text);
			toMenu.options[toMenu.options.length] = new Option(fromMenu.options[i].text, fromMenu.options[i].value, false, false);
		} else {
			fromMenuArray[fromMenuArray.length] = new Array(fromMenu.options[i].value, fromMenu.options[i].text);
		}
	}
	populateSelect(fromMenu, fromMenuArray);

}

function removeOptions(fromMenu, toMenu) {

	//fromMenu = document.forms.UserProfile.FUNCTIONS;
	//toMenu = document.forms.UserProfile.AvailablePrivs;
	fromMenuArray = new Array();
	toMenuArray = new Array();
	for (i=0; i<fromMenu.options.length; i++) {
		if (fromMenu.options[i].selected == true) {
			//toMenuArray[toMenuArray.length] = new Array(fromMenu.options[i].value, fromMenu.options[i].text);
			toMenu.options[toMenu.options.length] = new Option(fromMenu.options[i].text, fromMenu.options[i].value, false, false);
		} else {
			fromMenuArray[fromMenuArray.length] = new Array(fromMenu.options[i].value, fromMenu.options[i].text);
		}
	}
	populateSelect(fromMenu, fromMenuArray);

}

//**** MENU SCRIPTS

document.onmouseover = hideAllMenusTest;
//var myMenuArray = new Array('myMenu1', 'myMenu2', 'myMenu3', 'myMenu5');

function showMenuTest(menuNumber, eventObj, iconID) {
	//alert(iconID)
    hideAllMenusTest(iconID);
    eventObj.cancelBubble = true;
    var menuId = 'myMenu' + menuNumber;
    if(changeObjectVisibility(menuId, 'visible', iconID)) {
	return true;
    } else {
	return false;
    }
}

function hideAllMenusTest(iconID) {
    if (window.myMenuArray) {
        for (i=0; i<myMenuArray.length; i++) {
        	changeObjectVisibility(myMenuArray[i], 'hidden', iconID);
        	
        }
    }
}

function hideMenuTest(eventObj) {
	eventObj.cancelBubble = true;
    if(changeObjectVisibility('myMenu', 'hidden')) {
	return true;
    } else {
	return false;
    }
}


function changeObjectVisibility(objectId, newVisibility, iconID) {
	

    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
    menuTop = 0;
    menuLeft = 0;
    //alert(document.body.clientWidth)
    if (iconID) {
		//alert(iconID)
		var iconObj = document.getElementById(iconID);
		if (iconObj) {
			//alert("2" + iconID)
			menuTop = GetElementTop(iconObj) + iconObj.clientHeight;
			menuLeft = GetElementLeft(iconObj);
		}
		
    }
    	//alert(menuTop + ", " + menuLeft)
	styleObject.top = menuTop;
	styleObject.left = menuLeft;
	styleObject.visibility = newVisibility;
	return true;
    } else {
	//we couldn't find the object, so we can't change its visibility
	return false;
    }
} // changeObjectVisibility


startList = function() {
	if (isIE5) {
		for (h=0; h<mySubMenuArray.length; h++) {
		//navRoot = document.getElementById("nav");
			//alert(mySubMenuArray[h])
			navRoot = document.getElementById(mySubMenuArray[h]);
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
   				}
  			}
  		}
 	}
}
//window.onload=startList;


// ***********************
// BEGIN POSITIONING STUFF
// ***********************

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject


function GetElementLeft(eElement)
{
   if (!eElement && this)                    // if argument is invalid
   {                                         // (not specified, is null or is 0)
      eElement = this;                       // and function is a method
   }                                         // identify the element as the method owner

   var DL_bIE = document.all ? true : false; // initialize var to identify IE

   var nLeftPos = eElement.offsetLeft;       // initialize var to store calculations
   var eParElement = eElement.offsetParent;  // identify first offset parent element

   while (eParElement != null)
   {                                         // move up through element hierarchy

      if(DL_bIE)                             // if browser is IE, then...
      {
         if( (eParElement.tagName != "TABLE") && (eParElement.tagName != "BODY") )
         {                                   // if parent is not a table or the body, then...
            nLeftPos += eParElement.clientLeft; // append cell border width to calcs
         }
      }
      else                                   // if browser is Gecko, then...
      {
         if(eParElement.tagName == "TABLE")  // if parent is a table, then...
         {                                   // get its border as a number
            var nParBorder = parseInt(eParElement.border);
            if(isNaN(nParBorder))            // if no valid border attribute, then...
            {                                // check the table's frame attribute
               var nParFrame = eParElement.getAttribute('frame');
               if(nParFrame != null)         // if frame has ANY value, then...
               {
                  nLeftPos += 1;             // append one pixel to counter
               }
            }
            else if(nParBorder > 0)          // if a border width is specified, then...
            {
               nLeftPos += nParBorder;       // append the border width to counter
            }
         }
      }
      nLeftPos += eParElement.offsetLeft;    // append left offset of parent
      eParElement = eParElement.offsetParent; // and move up the element hierarchy
   }                                         // until no more offset parents exist
   return nLeftPos;                          // return the number calculated
}

function GetElementTop(eElement)
{
   if (!eElement && this)                    // if argument is invalid
   {                                         // (not specified, is null or is 0)
      eElement = this;                       // and function is a method
   }                                         // identify the element as the method owner

   var DL_bIE = document.all ? true : false; // initialize var to identify IE

   var nTopPos = eElement.offsetTop;         // initialize var to store calculations
   var eParElement = eElement.offsetParent;  // identify first offset parent element

   while (eParElement != null)
   {                                         // move up through element hierarchy
      if(DL_bIE)                             // if browser is IE, then...
      {
         if( (eParElement.tagName != "TABLE") && (eParElement.tagName != "BODY") )
         {                                   // if parent a table cell, then...
            nTopPos += eParElement.clientTop; // append cell border width to calcs
         }
      }
      else                                   // if browser is Gecko, then...
      {
         if(eParElement.tagName == "TABLE")  // if parent is a table, then...
         {                                   // get its border as a number
            var nParBorder = parseInt(eParElement.border);
            if(isNaN(nParBorder))            // if no valid border attribute, then...
            {                                // check the table's frame attribute
               var nParFrame = eParElement.getAttribute('frame');
               if(nParFrame != null)         // if frame has ANY value, then...
               {
                  nTopPos += 1;              // append one pixel to counter
               }
            }
            else if(nParBorder > 0)          // if a border width is specified, then...
            {
               nTopPos += nParBorder;        // append the border width to counter
            }
         }
      }

      nTopPos += eParElement.offsetTop;      // append top offset of parent
      eParElement = eParElement.offsetParent; // and move up the element hierarchy
   }                                         // until no more offset parents exist
   return nTopPos;                           // return the number calculated
}


//******* END POSITIONING STUFF