_date_months= ['ינואר','פברואר','מרץ','אפריל','מאי','יוני','יולי','אוגוסט','ספטמבר','אוקטובר','נובמבר','דצמבר'];

function validate_field(name)
{
  if (el=document.getElementById(name))
  {
    if (el.value!="")
    {
      el.style.backgroundColor="#ffffff";
      return true;
    }
    else
    {
      el.style.backgroundColor="#ffdddd";
      return false
    }
  }
  else
  {
    return false;
  }
}

function verify_idnum(idnum)
{
    checksum=0
    duble=false;
    for(var i=idnum.length-1;i>=0;--i)
    {
        digit=idnum.charAt(i);
        if(duble)
        {
            digit*=2;
            duble=false;
        }
        else
        {
            duble=true;
        }
        ndigit=0;
        digit=digit.toString();
        for (l=0;l<=digit.length-1;++l) ndigit+=parseInt(digit.charAt(l));
        checksum+=ndigit;
    }
    return (checksum %10)==0 && idnum!='0';
}

function verify_email(emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
}

function verify_ccnum(type, ccnum) {
   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MC") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Disc") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AmEx") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   } else if (type == "Isracard") {   
    checksum=0
    c=1;
    for(i=ccnum.length-1;i>=0;--i)
    {
	digit=ccnum.charAt(i);
	digit*=c;
	++c; 
	checksum+=digit;
    }
    return (checksum %11)==0;
   
   
   }
   else
   {
     return false
   }
   if (!re.test(ccnum)) return false;
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}


function addEvent(objObject, strEventName, fnHandler) 
{
  // DOM-compliant way to add an event listener
  if (objObject.addEventListener) 
   objObject.addEventListener(strEventName, fnHandler, false);
   //IE/windows way to add an event listener
  else if (objObject.attachEvent)
    objObject.attachEvent("on" + strEventName, fnHandler);
}
		
function getEventSrc(e) 
{
  // get a reference to the IE/windows event object
  if (!e) e = window.event;  
  // DOM-compliant name of event source property
  if (e.target)
    return e. target;
  // IE/windows name of event source property
  else if (e.srcElement)
    return e.srcElement;
}

function stopEvent(e)
{
      if(e.preventDefault) e.preventDefault();
      if(e.preventDefault) e.stopPropagation();
      e.returnValue=false
      e.cancelBubble=true;
}
		    
function update_date(e)
{
  e=getEventSrc(e);
  e=e.parentNode;
  if (e._fy.value!='' && e._fm.value!='' && e._fd.value!='')
    e._target.value=e._fy.value+"-"+e._fm.value+"-"+e._fd.value;
}

function build_date_widget(target,start_year,end_year)
{
 // Build start/end dates
 t=new Date();
 cur_year=t.getFullYear();
 cur_month=t.getMonth();
 cur_day=t.getDate();
 
 widget=document.createElement('span');
 widget.className="dateWidget";
 
 widget._target=target
 
 re=/(\d{4})-(\d{1,2})-(\d{1,2})/;
 m=re.exec(target.value);
 if(m)
 {
  vy=m[1];
  vm=m[2];
  vd=m[3];
 }
 else
 {
   vy=0;
   vm=0;
   vd=0;
 }
 fy=document.createElement('select');
 widget.appendChild(fy)
 
if (target.value=="")
{
  var opt=document.createElement('option');
  opt.appendChild(document.createTextNode('בחר'));
  opt.value=""
  fy.appendChild(opt);
}
if (end_year>start_year)
 { 
   for(y=start_year;y<end_year;++y)
   {
     opt=document.createElement('option');
     opt.value=y
     if (parseInt(y,10)==parseInt(vy,10)) opt.selected=true;
     opt.appendChild(document.createTextNode(y));
     fy.appendChild(opt)
   }
 }
 else
 {
   for(y=start_year-1;y>=end_year;--y)
   {
     opt=document.createElement('option');
     opt.value=y
     if (parseInt(y,10)==parseInt(vy,10)) opt.selected=true;
     opt.appendChild(document.createTextNode(y));
     fy.appendChild(opt)
   }
 }

 fm=document.createElement('select');
 widget.appendChild(fm)

if (target.value=="")
{
  var opt=document.createElement('option');
  opt.appendChild(document.createTextNode('בחר'));
  opt.value=""
  fm.appendChild(opt);
}
 for(y=1;y<=12;++y)
 {
    opt=document.createElement('option');
    opt.value=y;
     if (parseInt(y,10)==parseInt(vm,10)) opt.selected=true;
    opt.appendChild(document.createTextNode(_date_months[y-1]));
    fm.appendChild(opt)
 }

 fd=document.createElement('select');
 widget.appendChild(fd)
 
if (target.value=="")
{
  var opt=document.createElement('option');
  opt.appendChild(document.createTextNode('בחר'));
  opt.value=""
  fd.appendChild(opt);
}
 for(y=1;y<=31;++y)
 {
    opt=document.createElement('option');
    opt.value=y
    opt.appendChild(document.createTextNode(y));
    if (parseInt(y,10)==parseInt(vd,10)) opt.selected=true;
    fd.appendChild(opt)
 }
 
 addEvent(fy,"change",update_date);
 addEvent(fm,"change",update_date);
 addEvent(fd,"change",update_date);
 
 widget._fy=fy
 widget._fm=fm
 widget._fd=fd
 
 target.style.display='none'
 
 return widget;
}

function update_yearmonth(e)
{
  e=getEventSrc(e);
  e=e.parentNode;
  e._target.value=e._fm.value+"/"+e._fy.value
}


function build_yearmonth_widget(target,start_year)
{
 // Build start/end dates
 t=new Date();
 cur_year=t.getFullYear();
 cur_month=t.getMonth();
 
 widget=document.createElement('span');
 widget.className="dateWidget";
 
 widget._target=target
 
 re=/(\d{1,2})\/(\d{4})/;
 m=re.exec(target.value);
 if(m)
 {
  vm=m[1];
  vy=m[2];
 }
 else
 {
   vy=0;
   vm=0;
 }
 fy=document.createElement('select');
 widget.appendChild(fy)
if (target.value=="")
{
  var opt=document.createElement('option');
  opt.appendChild(document.createTextNode('בחר'));
  opt.value=""
  fy.appendChild(opt);
}
 if (!start_year)
   start_year=cur_year
 for(y=start_year;y<cur_year+7;++y)
 {
     opt=document.createElement('option');
     opt.value=y
     if (parseInt(y,10)==parseInt(vy,10)) opt.selected=true;
     opt.appendChild(document.createTextNode(y));
     fy.appendChild(opt)
 }

 fm=document.createElement('select');
 widget.appendChild(fm)
if (target.value=="")
{
  var opt=document.createElement('option');
  opt.appendChild(document.createTextNode('בחר'));
  opt.value=""
  fm.appendChild(opt);
}

 for(y=1;y<=12;++y)
 {
    opt=document.createElement('option');
    opt.value=y
     if (parseInt(y,10)==parseInt(vm,10)) opt.selected=true;
    opt.appendChild(document.createTextNode(y));
    fm.appendChild(opt)
 }
 
 addEvent(fy,"change",update_yearmonth);
 addEvent(fm,"change",update_yearmonth);
 
 widget._fy=fy
 widget._fm=fm
 
 target.style.display='none'
 
 
 return widget;
}

function update_time(e)
{
  e=getEventSrc(e);
  e=e.parentNode;
  e._target.value=e._fh.value+":"+e._fm.value
}

function build_time_widget(target)
{
 // Build start/end dates
 t=new Date();
 cur_hour=t.getHours();
 cur_minute=t.getMinutes();
 
 widget=document.createElement('span');
 widget.className="timeWidget";
 
 widget._target=target
 
 re=/(\d{1,2}):(\d{1,2})/;
 m=re.exec(target.value);
 if(m)
 {
  vh=m[1];
  vm=m[2];
 }
 else
 {
   vh=0;
   vm=0;
 }
 fh=document.createElement('select');
 widget.appendChild(fh)
if (target.value=="")
{
  var opt=document.createElement('option');
  opt.appendChild(document.createTextNode('בחר'));
  opt.value=""
  fh.appendChild(opt);
}
 
 for(y=0;y<24;++y)
 {
     opt=document.createElement('option');
     opt.value=y
     if (parseInt(y,10)==parseInt(vh,10)) opt.selected=true;
     opt.appendChild(document.createTextNode(y));
     fh.appendChild(opt)
 }

 fm=document.createElement('select');
 widget.appendChild(fm)
if (target.value=="")
{
  var opt=document.createElement('option');
  opt.appendChild(document.createTextNode('בחר'));
  opt.value=""
  fm.appendChild(opt);
}

 for(y=0;y<60;++y)
 {
    opt=document.createElement('option');
    opt.value=y
     if (parseInt(y,10)==parseInt(vm,10)) opt.selected=true;
    opt.appendChild(document.createTextNode(y));
    fm.appendChild(opt)
 }

 addEvent(fh,"change",update_time);
 addEvent(fm,"change",update_time);
 
 widget._fh=fh
 widget._fm=fm
 
 target.style.display='none'
 
 return widget;
}

function force_intval(e)
{
  val=e.value;
  loop_depressor=500 // Prevent endless loops
  while( 
      (
        (!val.match(/^([\d.\-]+)$/) && e.value!="") 
        ||
        (e.value!=val.toString())
      )
      &&
      ( --loop_depressor>0 )
    )
  {
    e.value=e.value.substr(0,e.value.length-1)
    val=e.value;
  }
}

function force_ccnum(e)
{
  val=e.value;
  loop_depressor=500 // Prevent endless loops
  while( 
      (
        (!val.match(/^([\d]+)$/) && e.value!="") 
        ||
        (e.value!=val.toString())
      )
      &&
      ( --loop_depressor>0 )
    )
  {
    e.value=e.value.substr(0,e.value.length-1)
    val=e.value;
  }
}

function check_phone(e)
{
  val=e.value;
  loop_depressor=500 // Prevent endless loops
  while( 
      (
        (!val.match(/^([0-9\-]+)$/) && e.value!="") 
        ||
        (e.value!=val.toString())
      )
      &&
      ( --loop_depressor>0 )
    )
  {
    e.value=e.value.substr(0,e.value.length-1)
    val=e.value;
  }
}

function check_hebrew(e)
{
  val=e.value;
  loop_depressor=500 // Prevent endless loops
  while( 
      (
        (!val.match(/^([\u0590-\u05FF ]+)$/) && e.value!="") 
        ||
        (e.value!=val.toString())
      )
      &&
      ( --loop_depressor>0 )
    )
  {
    e.value=e.value.substr(0,e.value.length-1)
    val=e.value;
  }
}

function check_code(e)
{
  val=e.value;
  loop_depressor=500 // Prevent endless loops
  while( 
      (
        (!val.match(/^([a-zA-Z0-9]+)$/) && e.value!="") 
        ||
        (e.value!=val.toString())
      )
      &&
      ( --loop_depressor>0 )
    )
  {
    e.value=e.value.substr(0,e.value.length-1)
    val=e.value;
  }
}
