///////////////////////////////////////////////////////////////////////
//     25/09/2003                                                    //
//                                                                   //
//     This script was designed by Avi Ben Shoshan			         //
//                                                                   //
//     This Script Icludes General JS Function For The Project	     //
///////////////////////////////////////////////////////////////////////

function OpenPopUp(sPath,sLeft,sTop,sWidth,sHeight)
{
	var winHandle;
	var sHtml;
	winHandle = window.open(sPath,'PopUp','width='+ sWidth +',height='+ sHeight +',top='+ sTop +',left='+ sLeft +',scrollbars=1,status=1');
	winHandle.focus();
}

// ---- Function To Return Selected Posting Or Channel In CMS
// Return Value - The Selected Item
//var strAppPath = Request.ApplicationPath;
//var strExistingTarget ='קיים נתיב';
//var strTitle = 'נושא';
//var strSubTitle = 'תת נושא';
//var iDialogType = סוג תיבת הדו-שיח :
//		0 - Channels Dialog : Default
//		1 - Templates Dialog
//		2 - Reasource Dialog
//		3 - All
function onClickBrowse(strAppPath,strExistingTarget,strTitle,strSubTitle,iDialogType)
{

	// for Internal Links dialog
	var IDS_DEFAULT_OPEN_POSITION = ",dialogTop=50,dialogWidth=300"
	var IDS_WIN_INTLINKS_FEATURES = "width=450,height=500" + IDS_DEFAULT_OPEN_POSITION;
	var IDS_IEWIN_INTLINKS_FEATURES = "dialogWidth:450px;dialogHeight:500px;status:yes" + IDS_IE_DIALOG_DEFAULT_POSITION;
	// IE Modal/Modeless dialog will use this string
	var IDS_IE_DIALOG_DEFAULT_POSITION = ",dialogTop=50,dialogWidth=300"

	//  args = "ExistingTarget;Title;SubTitle"
	var aAttributes = new Array();
	aAttributes[0] = strExistingTarget;
	aAttributes[1] = strTitle;
	aAttributes[2] = strSubTitle;

	try
	{
		if(iDialogType >=0){}
		else
		iDialogType = '0'; // Default Value
	}
	catch(e){iDialogType = '0';} // Default Value
	// Call InternalLinks.aspx with "wbc_caller=IEModal" query string to indicate that
	// we called it with IE's showModalDialog() call instead of JavaScript window.open()
	// Also, need to set NRMODE to Unpublished in order for the tree to display offline objects
	var strDlgRet = window.showModalDialog(strAppPath + "/CmsManagment/InternalLinks/InternalLinks.aspx" + "?NRMODE=Unpublished&wbc_caller=IEModal&DlgMode=" + iDialogType, 
												aAttributes, IDS_IEWIN_INTLINKS_FEATURES);
	if (strDlgRet != "Cancel")
	{
		return strDlgRet;
	}
}

// ---- Function To Return Selected MetaTable From MetaTables Values
// Return Value - The Selected Item
//var strAppPath = Request.ApplicationPath;
//var strExistingTarget ='קיים נתיב';
//var strTitle = 'נושא';
//var strSubTitle = 'תת נושא';
//var iDialogType = סוג תיבת הדו-שיח :
//		0 - Channels Dialog : Default
//		1 - Templates Dialog
//		2 - Reasource Dialog
//		3 - All
function selectMetaTable(strAppPath)
{

	// for Internal Links dialog
	var IDS_DEFAULT_OPEN_POSITION = ",dialogTop=50,dialogWidth=300"
	var IDS_WIN_INTLINKS_FEATURES = "width=550,height=450" + IDS_DEFAULT_OPEN_POSITION;
	var IDS_IEWIN_INTLINKS_FEATURES = "dialogWidth:650px;dialogHeight:450px;status:yes" + IDS_IE_DIALOG_DEFAULT_POSITION;
	// IE Modal/Modeless dialog will use this string
	var IDS_IE_DIALOG_DEFAULT_POSITION = ",dialogTop=50,dialogWidth=300"

	try
	{
		if(iDialogType >=0){}
		else
		iDialogType = '0'; // Default Value
	}
	catch(e){iDialogType = '0';} // Default Value
	// Call InternalLinks.aspx with "wbc_caller=IEModal" query string to indicate that
	// we called it with IE's showModalDialog() call instead of JavaScript window.open()
	// Also, need to set NRMODE to Unpublished in order for the tree to display offline objects
	var strDlgRet = window.showModalDialog(strAppPath + "/CmsManagment/MetaData/GenMetaDataSelectTables.aspx" + "?wbc_caller=IEModal&DlgMode=" + iDialogType, 
												'', IDS_IEWIN_INTLINKS_FEATURES);
	if (strDlgRet != "Cancel")
	{
		return strDlgRet;
	}
}


// ---- Function To OpenPopUp Window inside a dynamic DIV
// x - the left position of the popup including a valid unit for positioning 
// y - the top position of the popup including a valid unit for positioning 
// w - the width of the popup including a valid unit for dimensions 
// h - the height of the popup including a valid unit for dimensions 
// url - The name of the page or page generation script that will populate the popup 
// title - The popup title
// cssClass - css class for decoration - if notitle sent
// innerHtmlCode - innerHTML if no url sent
// USAGE - openPopUpDiv('110px','10px','200px','200px','Warning.html','div1') 
function openPopUpDiv(x,y,w,h,url,title,cssClass,innerHtmlCode)
{
	try
	{
		var objExsists = false;
		var objDiv = document.getElementById('PopUpDIV');  //wrap around div
		if (typeof(objDiv) != "undefined" && objDiv!= null)
			objExsists = true;
		else
			 objDiv = document.createElement('div');  //wrap around div
			 
		objDiv.id="PopUpDIV";
		objDiv.style.left=x;
		objDiv.style.top=y;
		objDiv.style.width=w;
		objDiv.style.height=h;
		objDiv.style.position="absolute";// the div gets styled
   		objDiv.style.visibility="visible";
		objDiv.setAttribute('class',cssClass);
		if (title != "")
		{
			objDiv.style.borderRight="navy 4px solid";
			objDiv.style.borderLeft="navy 4px solid";
			objDiv.style.borderBottom="navy 4px solid";
			objDiv.style.borderTop="navy 4px solid";

   			objDiv.innerHTML = "<table>" +
			"<tr><td width='25' height='25' style='BORDER-RIGHT: 2px solid; BORDER-TOP: 2px solid; BORDER-LEFT: 2px solid; BORDER-BOTTOM: 2px solid; BACKGROUND-COLOR: silver' align='right' handlefor='PopUpDIV' class='handle'><IMG src='../Images/PopupAllow.gif' align='middle' width='27' height='25' alt='' ></td>" +
				"<td style='BORDER-RIGHT: 2px solid; BORDER-TOP: 2px solid; BORDER-LEFT: 2px solid; BORDER-BOTTOM: 2px solid' align='center' handlefor='PopUpDIV' class='handle' width='300'>&nbsp;<b>" + title + "</b></td>" +
				"<td width='25' height='25' style='BORDER-RIGHT: 2px solid; BORDER-TOP: 2px solid; BORDER-LEFT: 2px solid; BORDER-BOTTOM: 2px solid; BACKGROUND-COLOR: silver' align='right' onmousemove='this.style.cursor=\"hand\";' onmouseout='this.style.cursor=\"default\";'><IMG alt='סגור' onclick='closePopUpDiv(this.parentNode);' src='../Images/Close.gif' align='middle' width='25' height='25'></td>" +
			"</tr></table>";
		}
		else
		{

		}

		if(url!=null && url!="")
		{
			popIFrame=document.createElement('iframe');  // iframe is created in the DOM
			popIFrame.setAttribute('height', h);  // simple DOM methods
			popIFrame.setAttribute('width', w);
			popIFrame.setAttribute('src', url);
			popIFrame.setAttribute('frameborder','no');
			if(objExsists==false)
				objDiv.appendChild(popIFrame); // now we add it to the div
		}
		else if(innerHtmlCode!=null && innerHtmlCode!="")
		{
			objDiv.innerHTML = objDiv.innerHTML + innerHtmlCode;
			objDiv.outerHTML = objDiv.outerHTML;
		}
		
		if(objExsists==false)
		{
			try
			{
				var pageForm = document.forms[0];
				if (typeof(pageForm) == "undefined")
					document.body.appendChild(objDiv);
				else
					pageForm.appendChild(objDiv);
			}
			catch(ex)
			{
				document.body.appendChild(objDiv);
			}
		}
		return objDiv;
	}
	catch(e){}
}
function closePopUpDiv(objDiv)
// the close is simple we just clear out the div
{
	try
	{
		//objDiv.innerHTML='';
		objDiv.style.display='none';
		objDiv.style.top = 1;
		objDiv.style.left = 1;
		objDiv.style.width = 1;
		objDiv.style.height = 1;
		//try
		//{
		//	var pageForm = document.forms[0];
		//	if (typeof(pageForm) == "undefined")
		//		document.body.removeChild(objDiv);
		//	else
		//		pageForm.removeChild(objDiv);
		//}
		//catch(ex)
		//{
		//	document.body.removeChild(objDiv);
		//}
		//objDiv = null;
	}
	catch(e){}
}

// ---- Function To print a warning DIV when MSIE-Popup blocker, blocks a JS - window.open() 
function createPopupDiv()
{
	var objDiv = document.createElement('div'); 
	objDiv.id="divExplain";
	objDiv.style.borderRight="navy 4px solid";
	objDiv.style.borderLeft="navy 4px solid";
	objDiv.style.borderBottom="navy 4px solid";
	objDiv.style.borderTop="navy 4px solid";
	objDiv.style.left="250px";
	objDiv.style.top="180px";
	objDiv.style.position="absolute";
	objDiv.style.backgroundColor="white";
	objDiv.style.color="navy";
	
	objDiv.style.width="408";
	objDiv.style.height="180";
   	objDiv.style.visibility="visible";
   	

   	objDiv.innerHTML = "<table>" +
	"<tr><td width='25' height='25' style='BORDER-RIGHT: 2px solid; BORDER-TOP: 2px solid; BORDER-LEFT: 2px solid; BORDER-BOTTOM: 2px solid; BACKGROUND-COLOR: silver' align='right' handlefor='divExplain' class='handle'><IMG src='../Images/PopupAllow.gif' align='middle' width='27' height='25' alt='הקפצת חלונות' ></td>" +
		"<td style='BORDER-RIGHT: 2px solid; BORDER-TOP: 2px solid; BORDER-LEFT: 2px solid; BORDER-BOTTOM: 2px solid' align='center' handlefor='divExplain' class='handle' width='300'>&nbsp;<b>Microsoft - Pop-ups Blocker</b></td>" +
		"<td width='25' height='25' style='BORDER-RIGHT: 2px solid; BORDER-TOP: 2px solid; BORDER-LEFT: 2px solid; BORDER-BOTTOM: 2px solid; BACKGROUND-COLOR: silver' align='right' onmousemove='this.style.cursor=\"hand\";' onmouseout='this.style.cursor=\"default\";'><IMG alt='סגור' onclick='divExplain.style.visibility=\"hidden\";;' src='../Images/Close.gif' align='middle' width='25' height='25'></td>" +
	"</tr>" +
	"<TR><TD style='FONT-SIZE: 5px' colSpan='2'>&nbsp;</TD></TR>" +
	"<TR><TD dir='rtl' colSpan='2'>גרסת הדפדפן שברשותכם מונעת פתיחת</TD></TR>" +
	"<TR><TD dir='rtl' colSpan='2'>&nbsp;&nbsp;&nbsp; 'חלונות קופצים' !</TD></TR>" +
	"<TR><TD style='FONT-SIZE: 8px' colSpan='2'>&nbsp;</TD></TR>" +
	"<TR><TD dir='rtl' colSpan='2'>בכדי לאפשר פעילות זו עליכם :</TD></TR>" +
	"<TR><TD style='FONT-SIZE: 5px' colSpan='2'>&nbsp;</TD></TR>" +
	"<TR><TD dir='rtl' vAlign='middle' colSpan='2'>&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;ללחוץ על הצלמית &nbsp;<IMG style='WIDTH: 30px; HEIGHT: 25px' src='../Images/PopUp.gif' alt=''>&nbsp;בראש הדפדפן .</TD></TR>" +
	"<TR><TD dir='rtl' vAlign='middle' colSpan='2'>&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;לבחור ב : '... Always Allow Pop-ups From This Site ' .</TD></TR>" +
	
	"<TR><TD dir='rtl' vAlign='middle' colSpan='2'>&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;או ב : 'Temporarily Allow Pop-ups' .</TD></TR>" +
	"<TR><TD dir='rtl' vAlign='middle' colSpan='2'>&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;בהצלחה .	</TD></TR>" +
	"<TR><TD style='FONT-SIZE: 10px' colSpan='2'>&nbsp;</TD></TR>" +
	"<TR><TD style='FONT-SIZE: 10px;BORDER-RIGHT: 2px solid; BORDER-TOP: 2px solid; BORDER-LEFT: 2px solid; BORDER-BOTTOM: 2px solid' colSpan='3'>&nbsp;</TD></TR>" +
	"</table>";
	
	try
	{
		var pageForm = document.forms[0];
		if (typeof(pageForm) == "undefined")
			document.body.appendChild(objDiv);
		else
			pageForm.appendChild(objDiv);
	}
	catch(ex)
	{
		document.body.appendChild(objDiv);
	}
}

// Resize Content
function resizeContent()
{
	objTblContent = document.getElementById('TblContent');
	if (typeof(objTblContent) == "undefined" || objTblContent== null)
		return null
	// 65%
	var iTblContentWidth = parseInt(screen.availWidth);
	iTblContentWidth=(isNaN(iTblContentWidth) ? 520 : iTblContentWidth);
	iTblContentWidth = Math.max(520,iTblContentWidth);
	iTblContentWidth = Math.min(parseInt(screen.availWidth),iTblContentWidth);
	iTblContentWidth=(iTblContentWidth*65/100); // 65% width
	objTblContent.style.width = iTblContentWidth;
	
	var cmsMode = requestParam("WBCMODE");
	// Resize the rest only in Presentation OR Dispaly Mode - not edit
	if(cmsMode == "PRESENTATIONUNPUBLISHED" || cmsMode == "DISPLAY")
	for (var i=1;i<objTblContent.all.length;i++)
	{
		if(objTblContent.all(i).innerText.length>0)
		{
			if(objTblContent.all(i).tagName == "TABLE")
			{
				objTblContent.all(i).style.width = iTblContentWidth;
			}
		}
	}	
}

// Get Request from URL
function requestParam(paramName)
{
	var strURL;
	var strParams;
	strURL = document.URLUnencoded.toUpperCase();
	if(strURL.indexOf("?") > -1)
	{
		strParams = strURL.substring(strURL.indexOf("?")+1);
		if(strParams.indexOf(paramName) > -1)
		{
			var sRet = "";
			i=strParams.indexOf("=",strParams.indexOf(paramName)+1);
			j=strParams.indexOf("&",i+1);
			if (j > 0){
				sRet = strParams.substring(i+1,j);}
			else{
				sRet = strParams.substring(i+1);}
				
			return sRet;
		}
	}
	else
		return "DISPLAY";
	
}


///////////////////////////////////////////////////////////////////////
//				Highlight form for Search Results Page				 //
///////////////////////////////////////////////////////////////////////
function highlightWord(node,word,hLiteClass,searchType) {
				if(typeof(node) == "undefined")return;
				word = word.trim();
				if(word.length==0)return;
				// Iterate into this nodes childNodes
				if (node.hasChildNodes) {
					if(node.className!="OverMaxSearch" && node.className!="Title" && node.className!="TdHeaderDesc")
					{
						var hi_cn;
						for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
							highlightWord(node.childNodes[hi_cn],word,hLiteClass,searchType);
						}
					}
				}
			
				// And do this node itself
				if (node.nodeType == 3) { // text node
					if((searchType=="md" && node.parentNode.parentNode.id!="tdSearchTerms")||(searchType=="kw" && node.parentNode.parentNode.id!="tdSearchMetaData"))
					{
						tempNodeVal = (node.nodeValue.toLowerCase());
						tempWordVal = (word.toLowerCase());
						if (tempNodeVal.indexOf(tempWordVal) != -1) {
							pn = node.parentNode;
							if (pn.className != hLiteClass) {
								// word has not already been highlighted!
								nv = node.nodeValue;
								ni = tempNodeVal.indexOf(tempWordVal);
								// Create a load of replacement nodes
								before = document.createTextNode(nv.substr(0,ni));
								docWordVal = nv.substr(ni,word.length);
								after = document.createTextNode(nv.substr(ni+word.length));
								hiwordtext = document.createTextNode(docWordVal);
								hiword = document.createElement("span");
								hiword.className = hLiteClass;
								hiword.appendChild(hiwordtext);
								pn.insertBefore(before,node);
								pn.insertBefore(hiword,node);
								pn.insertBefore(after,node);
								pn.removeChild(node);
							}
						}
					}
				}
			}

			function googleSearchHighlight() {
				var cssHliteClassName = "searchword";
				if (!document.createElement) return;
				//ref = document.referrer;
				ref = document.URLUnencoded;
				if (ref.indexOf('?') == -1) return;
				qs = ref.substr(ref.indexOf('?')+1);
				qsa = qs.split('&');
				for (i=0;i<qsa.length;i++) {
					qsip = qsa[i].split('=');
					if (qsip.length == 1) continue;
					if (qsip[0] == 'kw' || qsip[0] == 'md') { // q= for Google, p= for Yahoo
						//words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);
						words = (qsip[1].replace(/\+/g,' ')).split(/\s+/);
						for (w=0;w<words.length;w++) {
							if(words[w].length>0)
								highlightWord(document.getElementsByTagName("body")[0],words[w],cssHliteClassName+i,qsip[0]);
           				}
					}
				}
			}

			// U0es 2 global JS variables that get their values from the server side page
			// parameters : searchKeyword ,searchMetaData
			function SearchHighlight() 
			{
				var cssIndex=0;
				var cssHliteClassName = 'searchword';
				if (!document.createElement) return;

				if (searchKeyword.length == 0 && searchMetaData.length == 0) return;

				var hLiteArea = document.getElementsByTagName("TblContent")[0];
				if(typeof(hLiteArea) == "undefined")
					hLiteArea = document.getElementById("TblContent");

				// MetaData - OR
				qsa = searchMetaData.split(' AND ');
				for (i=0;i<qsa.length;i++) 
				{
					qsa[i] = qsa[i].split(' OR ');
					if (qsa[i].length > 0) 
					{
						for (j=0;j<qsa[i].length;j++) 
						{
							qsip = qsa[i][j].split(':');
							if (qsip.length == 1) continue;

							words = qsip[1].replace(/\+/g,' ');
							if(words.length>0)
							{
								highlightWord(hLiteArea,words.trim(),cssHliteClassName+cssIndex,"md");
							} 
							cssIndex ++;
						}
					}
					else
					{
						qsip = qsa[i].split(':');
						if (qsip.length == 1) continue;

						words = qsip[1].replace(/\+/g,' ');
						if(words.length>0)
						{
							highlightWord(hLiteArea,words.trim(),cssHliteClassName+cssIndex,"md");
						} 
						cssIndex ++;
					}
				}
				
				
				// Keywords
				qs = searchKeyword.split(',');
				for (i=0;i<qs.length;i++) 
				{
					words = (qs[i].replace(/\+/g,' ')).split(/\s+/);
					var word = "";
					for (w=0;w<words.length;w++) {
						if(words[w].length>0)
						{
							if(words[w].endsWith("\""))
							{
								var hLiteArea = document.getElementsByTagName("TblContent")[0];
								if(typeof(hLiteArea) == "undefined")
									hLiteArea = document.getElementById("TblContent");

								word = word + " " + words[w].substring(0,words[w].length-1);
								highlightWord(hLiteArea,word.trim(),cssHliteClassName+cssIndex,"kw");
								cssIndex++;
								word = "";
							} 
							else if(words[w].startsWith("\"") || word.length>0)
							{
								if(words[w].startsWith("\""))
									word = words[w].substring(1);
								else
									word = word + " " + words[w];
							}
							else
							{
								var hLiteArea = document.getElementsByTagName("TblContent")[0];
								if(typeof(hLiteArea) == "undefined")
									hLiteArea = document.getElementById("TblContent");

								highlightWord(hLiteArea,words[w].trim(),cssHliteClassName+cssIndex,"kw");
								cssIndex++;
							}
						}	
           			}
				}
			}			
///////////////////////////////////////////////////////////////////////
//				END Highlight form for Search Results Page			 //
///////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////
//								Prototypes							 //
///////////////////////////////////////////////////////////////////////
// create the prototype on the String object
String.prototype.trim = function() 
{
 // skip leading and trailing whitespace
 // and return everything in between
  var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}		

String.prototype.startsWith=function(prefix)
{
	return !this.indexOf(prefix);

	//if(this.indexOf(prefix) > -1)
	//	return true
	//else
	//	return false
}

String.prototype.endsWith=function(suffix)
{
	return this.lastIndexOf(suffix) == this.length-suffix.length;
}	

//   String.prototype.trim = function() {
//   return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
//   }	
