//moz and ie 
//global variables
  var ie = /MSIE/.test(navigator.userAgent);
  var moz = !ie //'note: if you are not ie we assume Gecko compatible      && navigator.product == "Gecko";

          // ieVer not in use: var ieVer =0;
  if (moz) {
	emulateAttachEvent();
  }
          // ieVer not in use: else {
          // ieVer not in use: getIEver()
          // ieVer not in use: }
// end moz and ie


var keepSub = 0;
var keepFather = "";

// the following array holds the total width from the left of the father buttons
fathersLoc = [0,98,223,345,467,617,779];
// the following array holds the total width from the right of the father buttons
fathersLocRight = [779,681,556,434,312,162,0];

allSubs = ['1','2','3','4','5','6','4x1','4x2','5x1','5x2','5x4','6x1','6x2','6x4','6x5','6x6','6x7','6x8'];
allFathers = ["tb_whereto","tb_guide","tb_forms","tb_moetzet","tb_infoMinhal","tb_info_mekar"];
 

//the following array holds the pages that contains combobox on the top of the page
ProbIdSelectPage = ["/IdkunKtovotMachoz/FindMachozForYishuv.aspx",
					"/TozaotMichrazim/search.aspx",
					"/TozaotMichrazim/search.aspx",
					"/TozaotMichrazim/search.aspx",
					"/TozaotMichrazim/DefineReport.aspx",
					"/TozaotMichrazim/DefineReport.aspx",
					"/TozaotMichrazim/DefineReport.aspx",
					"/TozaotMichrazim/DefineReport.aspx",
					"/IturTabot/taba1.asp",
					"/IturTabot/taba1.asp",
					"/IturTabot/taba1.asp",
					"/IturTabot/taba1.asp",
					"/IturTabot/taba1.asp",
					"/TozaotIturTabot/DefineReport.aspx",
					"/TozaotIturTabot/DefineReport.aspx",
					"/TozaotIturTabot/DefineReport.aspx",
					"/TozaotIturTabot/DefineReport.aspx",
					"/TozaotIturTabot/DefineReport.aspx",
					"/TozaotIturTabot/DefineReport.aspx",
					"/TozaotIturTabot/DefineReport.aspx",
					"/TozaotIturTabot/DefineReport.aspx",
					"/TashlumChovrot/BerurTashlum.aspx"]
//the following array holds the name of each combo					
allProbIdSelect = ["GnrYishuvim",
				   "drpYeshuv",
				   "drpMerchav",
				   "drpYeud",
				   "drpMerchav",
				   "drpMachoz",
				   "drpYeshuv",
				   "drpYeud",
   				   "selMonzipali",
				   "selMachoz",
				   "selSugVaada",
				   "selYeudim",
				   "ishuv",
				   "drpMachoz",
				   "drpYeshuv",
				   "drpMerchav",
				   "drpMoaaza",
				   "drpYeud",
				   "drpVaada",
				   "drpAchrai",
				   "drpMetachnen",
				   "ddlMisMichraz"]


MM_preloadImages('/images/tb_whereto_on.gif','/images/tb_guide_on.gif','/images/tb_forms_on.gif','/images/tb_moetzet_on.gif','/images/tb_infominhal_on.gif','/images/tb_info_mekar_on.gif');


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_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_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.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];}
}


function openSub(num,menuLine){
   var delimiter;
   var fatherWidth;
   var innerTableWidth;
   var myY;
   var i;
	if (document.getElementById("sub" + num)){
	// the "x" is a delimiter in the divs names - for a submenu name	
	delimiter = num.indexOf("x");
    i = num.charAt(0); //subYxZ
    
	if (delimiter >-1 ){	 //has "x"	
		// opening a grand-son so we dont need to close everything, except for the last opened child of the same parent
		// currently keepSub must hold the parent, or else we cant see the son we just want to open
        
		closeAllChildren(num);
        fatherWidth = fathersLocRight[i-2] - fathersLocRight[i-1];
		
		if (menuLine > 0) {
			// open the grand-son at the top of the line of the parent		
			myY = (document.getElementById("sub" + keepSub).clientHeight) / ((document.getElementById("subTable" + keepSub).rows.length + 1) /2); // 'שכפול עבור דיב
			//alert("a: " + document.getElementById("sub" + keepSub).clientHeight);
			//alert ("myY: " + myY);

			myY =   myY * menuLine;
			myY = myY + document.getElementById("blackBorderCenter").offsetTop;
			//alert ("b: " + document.getElementById("blackBorderCenter").offsetTop);  
			document.getElementById("sub" + num).style.top = document.getElementById("sub" + keepSub).style.top+myY; 
		}
	}else{  //without "x"
        fatherWidth = fathersLocRight[i-1] - fathersLocRight[i];

		if (keepSub>0){
			document.images["father" + keepSub].src = "/images/" + allFathers[keepSub-1] + "_off.gif";
		}
		keepSub = num ;
		// closes all menus and children, except for keepSub
		swapFatherImg
		closeAllSub()
		myY = 0;
	}
	
	innerTableWidth =  document.getElementById("subTable"+num).offsetWidth;

 //width (set acording to the bigger width between the two option  )
  if (fatherWidth > innerTableWidth) {
        document.getElementById("sub"+num).width = fatherWidth;  
		document.getElementById("subTable"+num).width = fatherWidth;
		innerTableWidth = fatherWidth;
  }else{
        document.getElementById("sub"+num).width = innerTableWidth;  
  }


		if (delimiter==-1) {
			// father is the images that trigger the subMenu
			resFix = (document.body.clientWidth - 779) /2 ;
			if (resFix<0) resFix=0;
			
			fatherLeft = fathersLoc[num-1];
			fatherRight = fathersLocRight[num];
			
			if (ie) {
				if (num == 1) {
					// when text size is large we need to move the right side acording to the width of the table inside
					document.getElementById("sub" + num).style.right = resFix + (779-document.getElementById("sub" + num).width) + "px";  
				}else{
					document.getElementById("sub" + num).style.right = resFix + fatherRight + "px";	
				}
			}else{  //moz 
				var x;
				x = resFix + fatherLeft - (innerTableWidth-fatherWidth);
				if (x < 0) {
					document.getElementById("sub" + num).style.left = resFix + 0;  ////תיקון אחרון
				}else{
					document.getElementById("sub" + num).style.left = resFix + fatherLeft - (innerTableWidth-fatherWidth);
				}
			  
			  
		    }
			
		}else{  
			
			fatherNum = parseInt(num.substring(0,num.indexOf("x")))-1;

			fatherLeft = fathersLoc[fatherNum-1];
			fatherRight = fathersLocRight[fatherNum];
			
			resFix = (document.body.clientWidth - 779) /2 ;
			if (resFix<0) resFix=0;
 
 			if (ie) {
				 document.getElementById("sub" + num).style.right = resFix + parseInt(fathersLocRight[fatherNum+1]) + parseInt(document.getElementById("sub" + (fatherNum+1)).width); 
			}else{
				
				document.getElementById("sub" + num).style.left = resFix + fatherLeft - ((document.getElementById("sub" + i).width - document.getElementById("father" + i).width) + (document.getElementById("sub" + num).width - document.getElementById("father" + (i-1)).width));  
			}		
	}	
	// displays the correct div
	document.getElementById("sub" + num).style.visibility = "visible";

	//  בעיה Explorer פחות מ-7: תג select תמיד עליון
	if (ie) 
	{
	for (i=0; i<ProbIdSelectPage.length; i++)
	{
	   if (ProbIdSelectPage[i].toLowerCase()==document.location.pathname.toLowerCase()) 
	   {
		if (document.getElementById(allProbIdSelect[i]))     // רק אם קיים בעקבות שינויים בין האתר הפנימי והחיצוני במחולל דוחות תבע של מרים מרדכי
		{
			document.getElementById(allProbIdSelect[i]).style.visibility = "hidden";   
		}
	   }
	}  // for
	}  // ie
	
	}	
}
// ******************************************************************************************
function DoMyLink(obj) {
	window.parent.location.href = obj.getAttribute("strLink");
}

// ******************************************************************************************
function subOver(obj) {
	
	if (ie) 
	{
		obj.setAttribute('className','subOver');
	}else{
		obj.setAttribute('class','subOver');
	}		
    
    if (obj.getAttribute("strLink")==null) 
    {
		window.status='Done';
	}else{
		window.status='http://' + document.location.hostname + obj.getAttribute("strLink");
	}
}
// ******************************************************************************************
function subOut(obj) {
	if (ie) 
	{
		obj.setAttribute('className','');
	}else{
		obj.setAttribute('class','');
	}		

	window.status="";
}
// ******************************************************************************************
function childOver(obj) {

	if (ie) 
	{
		obj.setAttribute('className','childOver');
	}else{
		obj.setAttribute('class','childOver');
	}		

	window.status='http://' + document.location.hostname + obj.getAttribute("strLink");
}

// ******************************************************************************************
function closeAllChildren(exception){
	// "exception" get the currently requested "num" from the "openSub" function 

	for (i=0; i<allSubs.length; i++){
		delimiter = allSubs[i].indexOf("x");
		father = allSubs[i].substring(0,delimiter);

		if (delimiter>-1 && father==keepSub && allSubs[i]!=exception){
			
			document.getElementById("sub" + allSubs[i]).style.visibility = "hidden";
			
		}
	}
}

// ******************************************************************************************


function closeAllGrandchildren(){
	// close All the Grandchildren
	for (i=0; i<allSubs.length; i++){
		delimiter = allSubs[i].indexOf("x");
		father = allSubs[i].substring(0,delimiter);

		if (delimiter > -1){
			if (true){
				document.getElementById("sub" + allSubs[i]).style.visibility = "hidden";
			}
		}
	}
}

// ******************************************************************************************

function swapFatherImg(){
	
	if (keepSub.length > 2){
		keepFather = keepSub.substring(0,1);				
		document.images["father" + keepFather].src = "/images/" + allFathers[keepFather-1] + "_off.gif";
		
	}else{		
		if (document.images["father" + keepSub]){
			document.images["father" + keepSub].src = "/images/" + allFathers[keepSub-1] + "_off.gif";
		}			
		keepFather = "";
	}

}
// ******************************************************************************************

function callcloseAllSub() {
    iTimerID = window.setTimeout("closeAllSub();",100,"JavaScript");
	//  בעיה Explorer פחות מ-7: תג select תמיד עליון
	if (ie) 
	{
	for (i=0; i<ProbIdSelectPage.length; i++)
	{
	   if (ProbIdSelectPage[i].toLowerCase()==document.location.pathname.toLowerCase()) 
	   {
		if (document.getElementById(allProbIdSelect[i]))  
		{
			document.getElementById(allProbIdSelect[i]).style.visibility = "visible";   
		}
	   }
	}  // for
	}  // ie
}
// ******************************************************************************************

function closeAllSub(){
    
	for (i=0; i<allSubs.length; i++){
	    
		if (true){		
		    if (allSubs[i]!=keepSub && allSubs[i]!=keepFather) {
			    if (document.getElementById("sub" + allSubs[i])){
				    document.getElementById("sub" + allSubs[i]).style.visibility = "hidden";
			    }
			    // else {alert("not document.all " + i);}
		    }
		    // else {alert("= keepSub  " + i);}		
		}
		// else {alert("not document.all[sub" + i);}
	}
	
	
}


// ******************************************************************************************

function isMenu(toClose){
	// received "true" from the main TD - so reseting the keepSub and closing all subs	
    //alert('isMenu');
	if (toClose){
		swapFatherImg();
		keepSub="";
		callcloseAllSub();
	}
	
}
// ******************************************************************************************


// ******************************************************************************************

function getIEver(){
      var appVer = navigator.appVersion.toLowerCase();
      var iePos = appVer.indexOf('msie');
      if (iePos != -1) { 
         ieVer = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));
      }
}

function  showDate(){
// if have span element with id dateOfPage, show date in yellow footer bar
// else do not show date and erase innerHtml in span with id updateText
             
if (document.getElementById('dateOfPage')){
	   if (document.getElementById('dateOfPage').innerHTML){
		    document.getElementById('updateDate').innerHTML = document.getElementById('dateOfPage').innerHTML ;
	   }
	   else {
	    document.getElementById('updateDate').innerHTML = "1.1.2003";
	   }
}
else {
     document.getElementById('updateText').innerHTML = "";
}
}

function goUp()  {
	this.top.scrollTo(0,0);
	this.scrollTo(0,0);
	return true;
}	
//IE Emulation:
/*
 * Extends the event object with srcElement, cancelBubble, returnValue,
 * fromElement and toElement
 */

function extendEventObject()
 {
	Event.prototype.__defineSetter__("returnValue", function (b) {

		if (!b) this.preventDefault();

		return b;
	});


	Event.prototype.__defineSetter__("cancelBubble", function (b) {

		if (b) this.stopPropagation();

		return b;
	});


	Event.prototype.__defineGetter__("srcElement", function () {
		var node = this.target;
		while (node.nodeType != 1) node = node.parentNode;
		return node;
	});

	Event.prototype.__defineGetter__("fromElement", function () {
		var node;
		if (this.type == "mouseover")
			node = this.relatedTarget;
		else if (this.type == "mouseout")
			node = this.target;
		if (!node) return;
		while (node.nodeType != 1) node = node.parentNode;
		return node;
	});

	Event.prototype.__defineGetter__("toElement", function () {
		var node;
		if (this.type == "mouseout")
			node = this.relatedTarget;
		else if (this.type == "mouseover")
			node = this.target;
		if (!node) return;
		while (node.nodeType != 1) node = node.parentNode;
		return node;
	});

	Event.prototype.__defineGetter__("offsetX", function () {
		return this.layerX;
	});
	Event.prototype.__defineGetter__("offsetY", function () {
		return this.layerY;
	});
}

/*
 * Emulates element.attachEvent as well as detachEvent
 */
function emulateAttachEvent() {
	HTMLDocument.prototype.attachEvent =
	HTMLElement.prototype.attachEvent = function (sType, fHandler) {
		var shortTypeName = sType.replace(/on/, "");
		fHandler._ieEmuEventHandler = function (e) {
			window.event = e;
			return fHandler();
		};
		this.addEventListener(shortTypeName, fHandler._ieEmuEventHandler, false);
	};

	HTMLDocument.prototype.detachEvent =
	HTMLElement.prototype.detachEvent = function (sType, fHandler) {
		var shortTypeName = sType.replace(/on/, "");
		if (typeof fHandler._ieEmuEventHandler == "function")
			this.removeEventListener(shortTypeName, fHandler._ieEmuEventHandler, false);
		else
			this.removeEventListener(shortTypeName, fHandler, true);
	};
}

/*
 * This function binds the event object passed along in an
 * event to window.event
 */
function emulateEventHandlers(eventNames) {
	for (var i = 0; i < eventNames.length; i++) {
		document.addEventListener(eventNames[i], function (e) {
			window.event = e;
		}, true);	// using capture
	}
}

/*
 * Simple emulation of document.all
 * this one is far from complete. Be cautious
 */

function emulateAllModel() {
	var allGetter = function () {
		var a = this.getElementsByTagName("*");
		var node = this;
		a.tags = function (sTagName) {
			return node.getElementsByTagName(sTagName);
		};
		return a;
	};
	HTMLDocument.prototype.__defineGetter__("all", allGetter);
	HTMLElement.prototype.__defineGetter__("all", allGetter);
}

function extendElementModel() {
	HTMLElement.prototype.__defineGetter__("parentElement", function () {
		if (this.parentNode == this.ownerDocument) return null;
		return this.parentNode;
	});

	HTMLElement.prototype.__defineGetter__("children", function () {
		var tmp = [];
		var j = 0;
		var n;
		for (var i = 0; i < this.childNodes.length; i++) {
			n = this.childNodes[i];
			if (n.nodeType == 1) {
				tmp[j++] = n;
				if (n.name) {	// named children
					if (!tmp[n.name])
						tmp[n.name] = [];
					tmp[n.name][tmp[n.name].length] = n;
				}
				if (n.id)		// child with id
					tmp[n.id] = n
			}
		}
		return tmp;
	});

	HTMLElement.prototype.contains = function (oEl) {
		if (oEl == this) return true;
		if (oEl == null) return false;
		return this.contains(oEl.parentNode);
	};
}

function emulateCurrentStyle() {
	HTMLElement.prototype.__defineGetter__("currentStyle", function () {
		return this.ownerDocument.defaultView.getComputedStyle(this, null);
		/*
		var cs = {};
		var el = this;
		for (var i = 0; i < properties.length; i++) {
			cs.__defineGetter__(properties[i], encapsulateObjects(el, properties[i]));
		}
		return cs;
		*/
	});
}

function emulateHTMLModel() {

	// This function is used to generate a html string for the text properties/methods
	// It replaces '\n' with "<BR"> as well as fixes consecutive white spaces
	// It also repalaces some special characters
	function convertTextToHTML(s) {
		s = s.replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<BR>");
		while (/\s\s/.test(s))
			s = s.replace(/\s\s/, "&nbsp; ");
		return s.replace(/\s/g, " ");
	}

	HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
		var df;	// : DocumentFragment
		var r = this.ownerDocument.createRange();

		switch (String(sWhere).toLowerCase()) {
			case "beforebegin":
				r.setStartBefore(this);
				df = r.createContextualFragment(sHTML);
				this.parentNode.insertBefore(df, this);
				break;

			case "afterbegin":
				r.selectNodeContents(this);
				r.collapse(true);
				df = r.createContextualFragment(sHTML);
				this.insertBefore(df, this.firstChild);
				break;

			case "beforeend":
				r.selectNodeContents(this);
				r.collapse(false);
				df = r.createContextualFragment(sHTML);
				this.appendChild(df);
				break;

			case "afterend":
				r.setStartAfter(this);
				df = r.createContextualFragment(sHTML);
				this.parentNode.insertBefore(df, this.nextSibling);
				break;
		}
	};

	HTMLElement.prototype.__defineSetter__("outerHTML", function (sHTML) {
	   var r = this.ownerDocument.createRange();
	   r.setStartBefore(this);
	   var df = r.createContextualFragment(sHTML);
	   this.parentNode.replaceChild(df, this);

	   return sHTML;
	});

	HTMLElement.prototype.__defineGetter__("canHaveChildren", function () {
		switch (this.tagName) {
			case "AREA":
			case "BASE":
			case "BASEFONT":
			case "COL":
			case "FRAME":
			case "HR":
			case "IMG":
			case "BR":
			case "INPUT":
			case "ISINDEX":
			case "LINK":
			case "META":
			case "PARAM":
				return false;
		}
		return true;
	});

	HTMLElement.prototype.__defineGetter__("outerHTML", function () {
		var attr, attrs = this.attributes;
		var str = "<" + this.tagName;
		for (var i = 0; i < attrs.length; i++) {
			attr = attrs[i];
			if (attr.specified)
				str += " " + attr.name + '="' + attr.value + '"';
		}
		if (!this.canHaveChildren)
			return str + ">";

		return str + ">" + this.innerHTML + "</" + this.tagName + ">";
	});


	HTMLElement.prototype.__defineSetter__("innerText", function (sText) {
		this.innerHTML = convertTextToHTML(sText);
		return sText;
	});

	var tmpGet;
	HTMLElement.prototype.__defineGetter__("innerText", tmpGet = function () {
		var r = this.ownerDocument.createRange();
		r.selectNodeContents(this);
		return r.toString();
	});

	HTMLElement.prototype.__defineSetter__("outerText", function (sText) {
		this.outerHTML = convertTextToHTML(sText);
		return sText;
	});
	HTMLElement.prototype.__defineGetter__("outerText", tmpGet);

	HTMLElement.prototype.insertAdjacentText = function (sWhere, sText) {
		this.insertAdjacentHTML(sWhere, convertTextToHTML(sText));
	};
}



//End IE Emulation.


