var BROWSER='';

//IE5.5+
if(navigator.appVersion.indexOf("MSIE")!=-1)
  {BROWSER='IE';}
//Firefox
else if(navigator.userAgent.indexOf("Firefox")!=-1)
  {BROWSER='Firefox';}
//Opera
else if(navigator.userAgent.indexOf("Opera")!=-1)
  {BROWSER='Opera';}
//Netscape
else if(navigator.userAgent.indexOf("Netscape")!=-1)
  {BROWSER='Netscape';}

var d=document;

var filter_email=new RegExp("^([a-z0-9_.-])+@(([a-z0-9_-])+.)+([a-z0-9]{2,4})$","gi");
var filter_email_replace=new RegExp("([a-z0-9_.-])+@(([a-z0-9_-])+.)+([a-z0-9]{2,4})","gi");
var filter_url=new RegExp("^(http://|https://){1}([a-z0-9_.-])+.{1}([a-z0-9]{2,4})(/(.)*)?$","gi");
var filter_url_replace=new RegExp("(http://|https://){1}([a-z0-9_.-])+.{1}([a-z0-9]{2,4})(/(.)*)?","gi");
var filter_newline=new RegExp("(\n)+|(\r\n)+","g");

function test_email(email)
{
  var filter_email=new RegExp("^([a-z0-9_.-])+\@(([a-z0-9_-])+\.)+([a-z0-9]{2,4})$","gi");

  return filter_email.test(email);
}

function test_url(url)
{
  var filter_url=new RegExp("^(http://|https://){1}([a-z0-9_.-])+\.+([a-z0-9]{2,4})(/(.)*)?$","gi");

  return filter_url.test(url);
}

function test_ip(ip)
{
  var filter_ip=new RegExp("^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$","g");

  return filter_ip.test(ip);
}

function test_date(date)
{
  filter_date=new RegExp("^([0-9]{2,2})/([0-9]{2,2})/([0-9]{4,4})$","g");

  return filter_date.test(date);
}

function findPosX(obj)
{
  var curleft = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curleft += obj.offsetLeft
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
    curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}

function screenCenterX()
{
  return getInnerWindowWidth()/2;
}

function screenCenterY()
{
  if(BROWSER=='IE')
    {return getInnerWindowHeight()/2+d.documentElement.scrollTop;}
  else
    {return getInnerWindowHeight()/2+window.pageYOffset;}
}

function loadArray(s)
{
  return s.split(";");
}

function getArrayKey(a,v)
{
  var j;
  for(j=0;j<a.length;j++)
  {
    if(a[j]==v)
      {return j;}
  }

  return -1;
}

function getArrayKeyByObjId(a,v)
{
  var j;
  for(j=0;j<a.length;j++)
  {
    if(a[j].id==v)
      {return j;}
  }

  return -1;
}

function trimString(s)
{
  while (s.substring(0,1)==" " || s.substring(0,1)=="\r" || s.substring(0,1)=="\n" || s.substring(0,1)=="\t")
    {s=s.substring(1, s.length);}
  while (s.substring(s.length-1, s.length)==" " || s.substring(s.length-1, s.length)=="\r" || s.substring(s.length-1, s.length)=="\n" || s.substring(s.length-1, s.length)=="\t")
    {s=s.substring(0,s.length-1);}
  return s;
}

function removeAllChild(obj)
{
  while(obj.firstChild)
    {obj.removeChild(obj.firstChild);}
}

function createXMLHttpRequest()
{
  if (window.ActiveXObject)
    {return new ActiveXObject("Microsoft.XMLHTTP");}
  else if (window.XMLHttpRequest)
    {return new XMLHttpRequest();}
}

function DBStr(id,str)
{
  this.id=id;
  this.str=str;
}

function getInnerWindowWidth()
{
  if (!window.innerWidth)
    {return d.documentElement.clientWidth;}
  else
    {return window.innerWidth;}
}

function getInnerWindowHeight()
{
  if (!window.innerHeight)
    {return d.documentElement.clientHeight;}
  else
    {return window.innerHeight;}
}

function getDocumentHeight()
{
  return d.body.clientHeight;
}

function setOpacity(obj,opacity)
{
  if(BROWSER=='IE')
    {obj.style.filter='alpha(opacity='+opacity+')';}
  else
    {obj.style.opacity=(opacity/100);}
}

function showLayer(obj,left,top,right,bottom,width,height,border)
{
  if(obj)
  {
    if(!border>0)
      {border=0;}

    var obj_iframe=d.getElementById(obj.id+'Iframe');
    if(BROWSER=='IE' && obj_iframe)
    {
      if(width>0)
        {obj_iframe.style.width=(width+border*2)+'px';}
      if(height>0)
        {obj_iframe.style.height=(height+border*2)+'px';}

      if(left>0)
        {obj_iframe.style.left=Math.round(left)+'px';}
      if(top>0)
        {obj_iframe.style.top=Math.round(top)+'px';}
      if(right>0)
        {obj_iframe.style.right=Math.round(right)+'px';}
      if(bottom>0)
        {obj_iframe.style.top=Math.round(bottom)+'px';}

      obj_iframe.style.visibility='visible';
    }

    obj.style.borderWidth=border+'px';

    if(width>0)
      {obj.style.width=(width)+'px';}
    if(height>0)
      {obj.style.height=(height)+'px';}

    if(left>0)
      {obj.style.left=Math.round(left)+'px';}
    if(top>0)
      {obj.style.top=Math.round(top)+'px';}
    if(right>0)
      {obj.style.right=Math.round(right)+'px';}
    if(bottom>0)
      {obj.style.top=Math.round(bottom)+'px';}

    obj.style.visibility='visible';
  }
}

function hideLayer(obj)
{
  if(obj)
  {
    var obj_iframe;
    if(BROWSER=='IE' && (obj_iframe=d.getElementById(obj.id+'Iframe')))
    {
      obj_iframe.style.visibility='hidden';
    }

    obj.style.visibility='hidden';
  }
}

function checkInteger(str)
{
  return str.replace(/([^0-9])+/g,'');
}

function getPasswordStrength(pw)
{
  //length of the password
  var pwlength=(pw.length);
  if(pwlength>8)
    {pwlength=8;}

  //use of numbers in the password
  var numnumeric = pw.replace (/[0-9]/g, "");
  var numeric=(pw.length - numnumeric.length);
  if(numeric>2)
    {numeric=2;}

  //use of symbols in the password
  var symbols = pw.replace (/\W/g, "");
  var numsymbols=(pw.length-symbols.length);
  if(numsymbols>2)
    {numsymbols=2;}

  //use of lowercase in the password
  var numlower = pw.replace (/[a-z]/g, "");
  var lower=(pw.length-numlower.length);
  if(lower>2)
    {lower=2;}

  //use of uppercase in the password
  var numupper = pw.replace (/[A-Z]/g, "");
  var upper=(pw.length-numupper.length);
  if(upper>2)
    {upper=2;}

  var variety=-3;
  if(numeric>0)
    {variety++;}
  if(numsymbols>0)
    {variety++;}
  if(lower>0)
    {variety++;}
  if(upper>0)
    {variety++;}

  var pwstrength=((pwlength*12)-10)+(numeric*8)+(numsymbols*10)+(upper*7)+(variety*10);

  // make sure we're give a value between 0 and 100
  if(pwstrength<0)
    {pwstrength=0;}

  if(pwstrength>100)
    {pwstrength=100;}

  return pwstrength;
}

function getTextLength(txt,chr_tot,counter)
{
  var cnt;
  if(!chr_tot>0)
    {chr_tot=600;}

  if(txt.value.length>chr_tot)
    {txt.value=txt.value.substr(0,chr_tot);}

  if(cnt=d.getElementById(counter))
  {
    if((chr_tot-txt.value.length)>=0)
      {cnt.innerHTML=(chr_tot-txt.value.length);}
    else
      {cnt.innerHTML=0;}
  }
}

function multiIdString(frm)
{
  var id_string=new String();
  var elems=frm.elements;

  var i;
  for(i=0;i<elems.length;i++)
  {
    if(elems[i].name.substr(0,4)=='sel_')
    {
      if(elems[i].checked)
        {id_string=id_string+elems[i].name.substr(4)+',';}
    }
  }

  return id_string.substr(0,id_string.length-1);
}

function showMap(id,lat,lng)
{
  window.open('index.php?action=popup&function=view_map&id='+id+'&lat='+lat+'&lng='+lng,'','location=no,menubar=no,toolbar=no,status=no,scrollbars=no,resizable=yes,width=750,height=550');
}

function showTechSpec(id)
{
  window.open('index.php?action=popup&function=view_techspec&id='+id,'','location=no,menubar=no,toolbar=no,status=no,scrollbars=auto,resizable=yes,width=500,height=550');
}

function showPolicy()
{
  window.open('index.php?action=popup&function=view_policy','','location=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width=560,height=500');
}

function newModelRequest()
{
  window.open('index.php?action=popup&function=new_model_request','','location=no,menubar=no,toolbar=no,status=no,scrollbars=no,resizable=no,width=400,height=430');
}

function navbarClick(tab)
{
  tab_modern_cars=d.getElementById('modern_cars');
  tab_new_cars=d.getElementById('new_cars');
  tab_used_cars=d.getElementById('used_cars');
  tab_classic_cars=d.getElementById('classic_cars');
  tab_vintage_cars=d.getElementById('vintage_cars');

  if(tab=='modern_cars')
  {
    if(area=='modern_cars')
    {
      tab_used_cars.style.display='block';
      tab_new_cars.style.display='block';
    }
    else
    {
      area='modern_cars';
      tab_used_cars.className='nsel';
      tab_new_cars.className='nsel';
      tab_modern_cars.className='sel';
    }

    return false;
  }
  else if(tab=='used_cars')
  {
    tab_used_cars.className='sel';
    tab_new_cars.className='nsel';
    tab_modern_cars.className='nsel';
    area='used_cars';
    return false;
  }
  else if(tab=='new_cars')
  {
    tab_used_cars.className='nsel';
    tab_new_cars.className='sel';
    tab_modern_cars.className='nsel';
    area='new_cars';
    return false;
  }
  else if(tab=='classic_cars')
  {
    if(area=='classic_cars')
      {tab_vintage_cars.style.display='block';}
    else
    {
      area='classic_cars';
      tab_vintage_cars.className='nsel';
      tab_classic_cars.className='sel';
      if(d.form_quick_search.anno_a)
        {selectOption(d.form_quick_search.anno_a,'');}
    }

    return false;
  }
  else if(tab=='vintage_cars')
  {
    tab_vintage_cars.className='sel';
    tab_classic_cars.className='nsel';
    area='vintage_cars';
    if(d.form_quick_search.anno_a)
      {selectOption(d.form_quick_search.anno_a,'1970');}
    return false;
  }

  return true;
}

function showHomeIntro()
{
  d.getElementById('intro_home_container').appendChild(d.getElementById('intro_home'));
  d.getElementById('intro_home_container').appendChild(d.getElementById('intro_home_links'));
  d.getElementById('intro_home').style.display='block';
  d.getElementById('intro_home_links').style.display='block';
}

function showDetailModelText()
{
  d.getElementById('detail_model_text_container').appendChild(d.getElementById('detail_model_text'));
  d.getElementById('detail_model_text').style.display='block';
}

function showDetailModelTestDrive()
{
  d.getElementById('detail_model_test_drive').style.display='block';
}


var tooltip_id='';

function showTooltip(msg,msg_id,w,h,from,side,x,y)
{
  tt=d.getElementById('tooltipContainer');
  if(tt)
  {
    tt_text=d.getElementById('tooltip_text');
    tt_v=d.getElementById('tooltip_v');

    if(tt.style.visibility=='visible' && msg_id==tooltip_id)
      {hideLayer(tt);}
    else
    {
      tooltip_id=msg_id

      if(side!='l' && side!='r')
        {side='r';}

      if(side=='r')
      {
        tt_v.src='http://image.i0100.com/tooltip_vr.gif';
        tt_v.style.left='6px';
      }
      else
      {
        tt_v.src='http://image.i0100.com/tooltip_vl.gif';
        tt_v.style.left=(w-29-6)+'px';
      }

      if(from)
      {
        if(side=='r')
        {
          x=findPosX(from)+from.clientWidth-24;
          y=findPosY(from)-h;
        }
        else
        {
          x=findPosX(from)-w+24;
          y=findPosY(from)-h;
        }
      }

      var tt_iframe=d.getElementById(tt.id+'Iframe');
      if(BROWSER=='IE' && tt_iframe)
      {
        tt_iframe.style.width=w+'px';
        tt_iframe.style.height=h+'px';
        tt_iframe.style.left=x+'px';
        tt_iframe.style.top=y+'px';

        tt_iframe.style.visibility='visible';
      }

      tt.style.width=w+'px';
      tt.style.height=h+'px';
      tt.style.left=x+'px';
      tt.style.top=y+'px';
      d.getElementById('tooltip_t').style.width=(w-12)+'px';
      d.getElementById('tooltip_b').style.width=(w-12)+'px';
      d.getElementById('tooltip_l').style.height=(h-32)+'px';
      d.getElementById('tooltip_r').style.height=(h-32)+'px';
      d.getElementById('tooltip_title').style.width=(w-12)+'px';
      tt_text.style.width=(w-12)+'px';
      tt_text.style.height=(h-48)+'px';
      tt_text.innerHTML=msg;

      tt.style.visibility='visible';
    }
  }
}
