﻿function eID(el)
{
    return(document.getElementById(el));
}

function randomNumber(num)
{
    return(Math.floor(Math.random()*num));
}

function roundDown(val)
{
    val = parseFloat(val);
    var iVal = parseInt(val);
    if((val-iVal)>0.98)
        return(Math.round(val))
    else
        return(iVal);
}

function isDefined(el)
{
    return (!(!el));
}

function addLoadEvent(func)
{ 
  var oldonload = window.onload; 
  if (typeof window.onload != 'function') { 
    window.onload = func; 
  } else { 
    window.onload = function() { 
      if (oldonload) { 
        oldonload(); 
      } 
      func(); 
    } 
  } 
} 

function SetRed(ID)
{
	eID("Submit"+ID).style.color="red";
}

function writeDiv(style)
{
	document.write("<div style=\""+style+"\"></div>");
}function getFlash(file,width,height,flashvars)
{
    var params ="<param name=\"movie\" value=\""+file+"\" />" +
                "<param name=\"quality\" value=\"high\" />" +
                "<param name=\"wmode\" value=\"transparent\" />" +
                "<param name=\"flashvars\" value=\""+flashvars+"\" />"
/*
    var str="<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\""+width+"\" height=\""+height+"\">" +
            params +
            "</object>" +
            "<!--[if !IE]>-->" +
            "<object type=\"application/x-shockwave-flash\" data=\""+file+"\" width=\""+width+"\" height=\""+height+"\">" +
            params +
            "</object>" +
            "<!--<![endif]-->"
*/
    var str="<object type=\"application/x-shockwave-flash\" data=\""+file+"\" width=\""+width+"\" height=\""+height+"\">" +
            params +
            "</object>"
            
    document.write(str);
}function getYoutubeEmbed(url,width,height){    return("<object width=\""+width+"\" height=\""+height+"\"><param name=\"movie\" value=\""+url+"\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\""+url+"\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" wmode=\"transparent\" width=\""+width+"\" height=\""+height+"\"></embed></object>");}function pageScroll(TargetPosition,EachJump) 
{
    var CurrentPosition = f_scrollTop();
    if(CurrentPosition>TargetPosition)
        EachJump = 0-EachJump;
    //alert(CurrentPosition);
    while(CurrentPosition!=TargetPosition)
    {
        // בודק האם מה שנשאר קטן מקפיצה אחת
        if(Math.abs(TargetPosition-CurrentPosition) < Math.abs(EachJump))
            EachJump = TargetPosition - CurrentPosition;
	    
	    //setTimeout("window.scrollBy(0,"+EachJump+");",MillisBetweenJumps);
	    window.scrollBy(0,EachJump);
	    CurrentPosition+=EachJump;
	}
}

function PauseScript(millisecondes) 
{
    var date = new Date();
    var curDate = null;

    do 
        curDate = new Date();
    while(curDate-date < millisecondes);
} 

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}function sortNumber(a,b)
{
    return a - b;
}

function OpenWindow(FormName,WindowName)
{
	var w=window.open('',WindowName,'toolbars=no,addressbar=no,scrollbars=1,width=410,height=300,left=200,top=200');
	if(w)
	{
		document.forms[FormName].target=WindowName;
		document.forms[FormName].submit();
		w.focus();
	}
	else
	{
		alert("כנראה שמופעל אצלך חוסם pop-up...");
	}
}

function SetElementsNameDisplay(ElementsName,Display)
{
	var v = document.getElementsByName(ElementsName);
	for(i=0;i<v.length;i++)
		v[i].style.display = Display;
}

function SetElemntInputsAndSelectsDisabled(Element,Disabled)
{
	if((Element.nodeName=="INPUT")||(Element.nodeName=="SELECT"))
		Element.disabled = Disabled;

	var x = Element.childNodes;
	
	for (var i=0;i<x.length;i++)
		SetElemntInputsAndSelectsDisabled(x[i],Disabled);
}
function PrintChilds(Element)
{
	var x = Element.childNodes;
	for (var i=0;i<x.length;i++)
		alert(x[i]);
}

function RemoveChilds(Element)
{
	var x = Element.childNodes;
	for (var i=0;i<x.length;i++)
	{
	    RemoveChilds(x[i]);
   		Element.removeChild(x[i]);
	}
}

function SwapElementsById(Element1ID,Element2ID)
{
    var Element1 = document.getElementById(Element1ID);
    var Element2 = document.getElementById(Element2ID);
	
	var x = Element1;
	
	Element2 = Element1;
	Element1 = x;
}

function SwapElementsInnerHTML(Element1ID,Element2ID)
{
    var Element1 = document.getElementById(Element1ID);
    var Element2 = document.getElementById(Element2ID);
	
	var tmp = Element1.innerHTML;
	//alert(tmp);
	
	//RemoveChilds(Element1);
	
	//alert(Element1.innerHTML);
	Element1.innerHTML = Element2.innerHTML;
	Element2.innerHTML = tmp;
}

function ShowOrHideElement(ElementID)
{
	if(document.getElementById(ElementID).style.display == "none")
		document.getElementById(ElementID).style.display = "";
	else
		document.getElementById(ElementID).style.display = "none";
}

function HideShowEl(HideID,ShowID)
{
    document.getElementById(HideID).style.display='none';
    document.getElementById(ShowID).style.display='';
}

function ShowAndHideElements(Element1ID,Element2ID)
{
	if(document.getElementById(Element1ID).style.display == "none")
	{
		document.getElementById(Element1ID).style.display = "";
		document.getElementById(Element2ID).style.display = "none";
    }
	else
	{
		document.getElementById(Element1ID).style.display = "none";
		document.getElementById(Element2ID).style.display = "";
    }
}

function ShowElement(ElementID)
{
    if(typeof document.getElementById(ElementID) != "undefined")
	    document.getElementById(ElementID).style.display='';
}

function HideElement(ElementID)
{
    if(typeof document.getElementById(ElementID) != "undefined")
	    document.getElementById(ElementID).style.display='none';
}

function OpenPopUp(url,wName,width,height)
{
	var w=window.open(url,wName,"toolbars=no,addressbar=no,scrollbars=0,width="+width+",height="+height+",left=200,top=200");
	if(w)
	{
		w.focus();
		return true;
	}
	else
	{
		alert("כנראה שמופעל אצלך חוסם pop-up...");
		return false;
	}
}

function PopUp(url,wName,width,height)
{
	var w=window.open(url,wName,"toolbars=no,addressbar=no,scrollbars=1,width="+width+",height="+height+",left=200,top=200");
	if(w)
	{
		w.focus();
		return w;
	}
	else
	{
		alert("כנראה שמופעל אצלך חוסם pop-up...");
		return false;
	}
}

function editWindow(url)
{
    PopUp(url,"editWindow","600","350");
}

function updatePage(url)
{
    GetDataWithAjax('',url,'window.location.reload();');
}

function GetSideBanner()
{
	windowWidth = document.body.clientWidth;
	var DivWidth = (windowWidth - 780) / 2;
	if  ( DivWidth < 110 ) 
	{
		document.getElementById('LeftBanner').style.display = 'none';
		document.getElementById('RightBanner').style.display = 'none';
	}
	else
	{
		document.getElementById('LeftBanner').style.display = '';
		document.getElementById('RightBanner').style.display = '';
		document.getElementById('LeftBanner').style.left = (DivWidth - 110) / 2;
		document.getElementById('RightBanner').style.right = (DivWidth - 110) / 2; //windowWidth - DivWidth + (DivWidth / 2)
	}

	document.getElementById('LeftBanner').style.top = document.body.scrollTop;
	document.getElementById('RightBanner').style.top = document.body.scrollTop;
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.left = function(num)
{
	return this.substring(0,num);
}

String.prototype.right = function(num)
{
	return this.substring(this.length-num,this.length);
}

function cleanHTMLTags(txt)
{
    /////<([A-Z][A-Z0-9]*)\b[^>]*>.*?</\1>
    txt = cleanSpans(txt);
    return txt.replace(/(<[^>]*>)|(&.+;)/g," ");
}

function cleanSpans(txt)
{
    /////////////////////<([A-Z][A-Z0-9]*)\b[^>]*>.*?</\1>
    txt = txt.replace(/&nbsp;/g," ");
    return txt.replace(/(<span\b[^>]*>)|(<\/span>)/g,"");
}

function wordsCount(txt)
{
    txt = cleanHTMLTags(txt);
    //alert(txt);
    var arr = txt.match(/[א-ת\w]([^א-ת\w]|$)/gm);
    
    return ((!arr)?0:arr.length);
}

function replaceAll(oldStr,findStr,repStr) 
{
  var srchNdx = 0;  // srchNdx will keep track of where in the whole line
                    // of oldStr are we searching.
  var newStr = "";  // newStr will hold the altered version of oldStr.
  while (oldStr.indexOf(findStr,srchNdx) != -1)  
                    // As long as there are strings to replace, this loop
                    // will run. 
  {
    newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
                    // Put it all the unaltered text from one findStr to
                    // the next findStr into newStr.
    newStr += repStr;
                    // Instead of putting the old string, put in the
                    // new string instead. 
    srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
                    // Now jump to the next chunk of text till the next findStr.           
  }
  newStr += oldStr.substring(srchNdx,oldStr.length);
                    // Put whatever's left into newStr.             
  return newStr;
}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) 
    {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}

function isNumeric(sText)
{
	return isInteger(sText) || isDouble(sText);
}

function isDouble(sText)
{
	var NumRX = /^[+-]?[0-9]+\.[0-9]+$/;
	return NumRX.test(sText);
}

function isInteger(sText)
{
	var NumRX = /^[+-]?[0-9]+$/;
	return NumRX.test(sText);
}
function CheckZehut(m_id_num) //m_id_num must contain digits only
{
	var m_id_tmp;
	var a1, a2, a3, a4, a5, a6, a7, a8;
	var b2, b4, b6, b8;
	var m_sfx_num;
	var strMsg;
	var Response;
	    
	m_id_tmp = Left(Right(Zeros(9) + m_id_num, 9), 8)
	a1 = parseInt(Left(m_id_tmp, 1))
	a2 = parseInt(Left(Right(m_id_tmp, 7), 1)) * 2
	a3 = parseInt(Left(Right(m_id_tmp, 6), 1))
	a4 = parseInt(Left(Right(m_id_tmp, 5), 1)) * 2
	a5 = parseInt(Left(Right(m_id_tmp, 4), 1))
	a6 = parseInt(Left(Right(m_id_tmp, 3), 1)) * 2
	a7 = parseInt(Left(Right(m_id_tmp, 2), 1))
	a8 = parseInt(Right(m_id_tmp, 1)) * 2
	    
	b2 = (a2 % 10) + parseInt(a2 / 10)
	b4 = (a4 % 10) + parseInt(a4 / 10)
	b6 = (a6 % 10) + parseInt(a6 / 10)
	b8 = (a8 % 10) + parseInt(a8 / 10)
	    
	if (((a1 + b2 + a3 + b4 + a5 + b6 + a7 + b8) % 10) >= 1 && ((a1 + b2 + a3 + b4 + a5 + b6 + a7 + b8) % 10) <= 9)
		m_sfx_num = parseInt(10 - ((a1 + b2 + a3 + b4 + a5 + b6 + a7 + b8) % 10));
	else
		m_sfx_num = 0;
	    
	return (parseInt(Right((m_id_num), 1)) == m_sfx_num)
}
function CheckMail(mail)
{
	var RegExp = /^[A-Za-z\_\-0-9]+(\.[A-Za-z\_\-0-9]+)*@[A-Za-z\_\-0-9]+(\.[A-Za-z\_\-0-9]+)?(\.[A-Za-z]{2,3}){1,2}$/;
	return RegExp.test(mail);
}
function isPhone(sText)
{
	var RX = /^(0[2|3|4|8|9]|077)(\-)?[0-9]{7}$/;
	return (RX.test(sText));
}
function isCellular(sText)
{
	var RX = /^05[0|2|4|7](\-)?[0-9]{7}$/;
	return RX.test(sText);
}
function Left(str,num)
{
	return str.substring(0,num);
}
function Right(str,num){
	return str.substring(str.length-num,str.length);
}
function Zeros(num)
{
	var str="";
	var i;
	for (i=1; i<=num; i++)
		str+="0";
	return str;
}

function PutSelectValue(selectObj,value)
{
    var i;
	for(i=0;i<selectObj.length;i++)
		if(selectObj[i].innerHTML == value)
			selectObj[i].selected = true;
}

function SelectValue(obj)
{
	return obj[obj.selectedIndex].value;
}

function isDate(day,month,year)
{	
	if (!isNumeric(day) || !isNumeric(month) || !isNumeric(year)) return false;
	if (day.length !=2 || month.length!=2 || year.length!=4) return false;
	var Meuberet = ((year % 4)==0 && (year % 100)!=0) || ((year % 400)==0);
	if (month == 2 && day >28 && !Meuberet){
		return false;
	}
	if (month<1 || month>12)
		return false;
	if (month == 1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
	{
		if (day<1 || day>31)
		{
			return false;
		}
	}else{
		if (day<1 || day>30)
		{
			return false;
		}
	}
	return true;
}

function DigitsOnly(d)
{
    return((d>=48&&d<=57)||(d>=96&&d<=105)||d==8||d==9||d==16||d==46||(d>=35&&d<=39)||d==13);
}

function phoneCheck(val)
{
    var letter = val.substring(0,1);
    return(letter!='0'&&letter!='1');
}

function Get2DigitsNumber(number)
{
	return(Math.round(number*100)/100);
}

function GetInteger(number,Digits)
{
    var str="";
    for(var i=0;i<(Digits-(number.toString().length));i++)
       str+="0";
	str+=number;
	return(str);
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function ReplaceHttpsLinks()
{
	var links;
	links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++)
		links[i].href=links[i].href.replace("https://","http://");
	links = document.getElementsByTagName("area");
	for (var i=0; i<links.length; i++)
		links[i].href=links[i].href.replace("https://","http://");
}

function GetDataWithAjax(Element,url,evalString)
{
    var responseTxt;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("הדפדפן שבו אתה משתמש ישן מדי. אנא התקן גרסה חדשה יותר...");
	  return;
	} 
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange=function()
	{ 
		if (xmlHttp.readyState==4)
		{
		    if(xmlHttp.status==200)
		    {
			    responseTxt = xmlHttp.responseText;
		        if(responseTxt.substr(0,12)=="<!--login-->")
		        {
		            document.location.reload();
		            return(false);
		        }
			    if(Element.length>0) eID(Element).innerHTML = responseTxt;
			    if(evalString.length>0) eval(evalString);
			}
			else if (typeof evalStr_onServerError!="undefined")
                eval(evalStr_onServerError);
		}
	}		
	xmlHttp.send(null);
} 

function GetXMLWithAjax(url,evalString)
{
    var responseTxt;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("הדפדפן שבו אתה משתמש ישן מדי. אנא התקן גרסה חדשה יותר...");
	  return;
	} 
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange=function()
	{ 
		if (xmlHttp.readyState==4)
		{
		    if(xmlHttp.status==200)
		    {
			    var xml = xmlHttp.responseXML;
			    if(evalString.length>0) eval(evalString);
			}
			else if (typeof evalStr_onServerError!="undefined")
                eval(evalStr_onServerError);
		}
	}		
	xmlHttp.send(null);
} 

function GetXML(url)
{
    var responseTxt;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("הדפדפן שבו אתה משתמש ישן מדי. אנא התקן גרסה חדשה יותר...");
	  return;
	} 
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	return(xmlHttp.responseXML);
} 

function findPosition(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return {x:curleft,y:curtop};
}

function getElementsByClass(tag, name) {
     
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("class");
          if((!(!att))&&att.indexOf(name)>=0) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function getCheckboxValuesByName(name,checked) {
     
     var elem = document.getElementsByTagName("INPUT");
     var str = "";
     for(i = 0; i < elem.length; i++) 
     {
          att = elem[i].getAttribute("name");
          if((!(!att))&&att==name)
          {
            if(checked==null)
             str += (","+elem[i].value);
            else if(elem[i].checked==checked)
                str += (","+elem[i].value);
          }
     }
     if(str!="")
        str = str.substring(1);
     return str;
}

function setCheckboxesCheckedByName(name,checked) 
{
     var elem = document.getElementsByTagName("INPUT");
     for(i = 0; i < elem.length; i++) 
     {
          att = elem[i].getAttribute("name");
          if((!(!att))&&att==name)
             elem[i].checked = checked;
     }
}

function AppendClassToClasses(tag, ToClassName, className) {
     var att;
     var elem = document.getElementsByTagName(tag); 
     
     for(i = 0; i < elem.length; i++) 
     {
          att = elem[i].getAttribute("class");
          if((!(!att))&&att.indexOf(ToClassName)>=0&&att.indexOf(className)<0)
            elem[i].setAttribute("class",att + " " + className);
     }
}

function RemoveClassFromClasses(tag, FromClassName, className) 
{
     var att;
     var elem = document.getElementsByTagName(tag); 
     
     for(i = 0; i < elem.length; i++) 
     {
          att = elem[i].getAttribute("class");
          if((!(!att))&&att.indexOf(FromClassName)>=0)
            elem[i].setAttribute("class",att.replace(" " + className,""));
     }
}

function RemoveClassFromElementById(id, className) 
{
    att = eID(id).getAttribute("class");
    if(!(!att))
        eID(id).setAttribute("class",att.replace(" " + className,""));
}

function changeClassInnerHTML(doc, tag, className,val) {
     
     var elem = doc.document.getElementsByTagName(tag);
     for(i = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("class");
          if((!(!att))&&att.indexOf(className)>=0)
               elem[i].innerHTML = val;
     }
}

function getClassAttributeList(doc, tag, className,attName)
{
    var str="",elem = doc.document.getElementsByTagName(tag);
    for(i = 0; i < elem.length; i++) 
    {
      att = elem[i].getAttribute("class");
      if((!(!att))&&att.indexOf(className)>=0)
          str += (","+elem[i].getAttribute(attName));
    }
    if(str!="")
        str = str.substring(1);
    return(str);
}

function selectOption(optionEl,value)
{
    var optionsEl = optionEl.parentNode;
    var name = optionsEl.id;
    
    eID(name+"_value").value=value;
    
    eval(name+"_selectedEl.className='';");
    eval(name+"_selectedEl=optionEl;");
    
    eID(name+"_txt").value=optionEl.innerHTML;
    optionEl.className="selected";
    
    optionsEl.style.display="none";
}

function getFlashVersion()
{
  // ie
  try {
    try {
      // avoid fp6 minor version lookup issues
      // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
      var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
      try { axo.AllowScriptAccess = 'always'; }
      catch(e) { return '6,0,0'; }
    } catch(e) {}
    return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
  // other browsers
  } catch(e) {
    try {
      if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
        return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
      }
    } catch(e) {}
  }
  return '0,0,0';
}

function CreateBookmarkLink() {

 title = "Webpage Title"; 
  
 url = "Webpage URL";
  
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
 }
 
 function GetStageSize() 
 {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    var o = {};
    o.myWidth = myWidth;
    o.myHeight = myHeight;
    return o;
}

