﻿function eID(el)
{
    return(document.getElementById(el));
}

function isDefined(el)
{
    return (!(!el));
}

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 DigitsOnly()
{
    var d = event.keyCode;
    //alert(d);
    if((d>=48&&d<=57)||d==8||d==9||d==16||d==46||(d>=35&&d<=39)||d==13)
        return(true);
    else
        return(false);
}

function GetSideBanner()
{
	windowWidth = document.body.clientWidth;
	var DivWidth = (windowWidth - 780) / 2;
	if  ( DivWidth < 110 ) 
	{
		eID('LeftBanner').style.display = 'none';
		eID('RightBanner').style.display = 'none';
	}
	else
	{
		eID('LeftBanner').style.display = '';
		eID('RightBanner').style.display = '';
		eID('LeftBanner').style.left = (DivWidth - 110) / 2;
		eID('RightBanner').style.right = (DivWidth - 110) / 2; //windowWidth - DivWidth + (DivWidth / 2)
	}

	eID('LeftBanner').style.top = document.body.scrollTop;
	eID('RightBanner').style.top = document.body.scrollTop;
}

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 = eID(Element1ID);
    var Element2 = eID(Element2ID);
	
	var x = Element1;
	
	Element2 = Element1;
	Element1 = x;
}

function SwapElementsInnerHTML(Element1ID,Element2ID)
{
    var Element1 = eID(Element1ID);
    var Element2 = eID(Element2ID);
	
	var tmp = Element1.innerHTML;
	//alert(tmp);
	
	//RemoveChilds(Element1);
	
	//alert(Element1.innerHTML);
	Element1.innerHTML = Element2.innerHTML;
	Element2.innerHTML = tmp;
}

function ShowOrHideElement(ElementID)
{
    if(!eID(ElementID))
        return(false);
	if(eID(ElementID).style.display == "none")
		eID(ElementID).style.display = "";
	else
		eID(ElementID).style.display = "none";
}

function ShowElement(ElementID)
{
    if(!eID(ElementID))
        return(false);
	eID(ElementID).style.display='';
}

function HideElement(ElementID)
{
    if(!eID(ElementID))
        return(false);
	eID(ElementID).style.display='none';
}

function OpenWindow(FormName)
{
	var w=window.open('','CWindow','toolbars=no,addressbar=no,scrollbars=0,width=410,height=300,left=200,top=200');
	if(w)
	{
		document.forms[FormName].target="CWindow";
		document.forms[FormName].submit();
		w.focus();
		return true;
	}
	else
	{
		alert("כנראה שמופעל אצלך חוסם pop-up...");
		return false;
	}
}

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 true;
	}
	else
	{
		alert("כנראה שמופעל אצלך חוסם pop-up...");
		return false;
	}
}

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 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 SelectValue(obj)
{
	return obj[obj.selectedIndex].value;
}

function PutSelectValue(selectObj,value)
{
    var i;
	for(i=0;i<selectObj.length;i++)
		if(selectObj[i].innerHTML == value)
			selectObj[i].selected = true;
}

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 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 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);
			}
		}
	}		
	xmlHttp.send(null);
} 

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 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};
}