isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
var ie5 = (document.getElementById && document.all); 
var ns6 = (document.getElementById && !document.all); 
var ua = navigator.userAgent.toLowerCase();
var isapple = (ua.indexOf('applewebkit') != -1 ? 1 : 0);
var local={
	'year':'Year',
	'make':'Make',
	'model':'Model',
	'tire':'Tire Brand',
	'name': 'First Name',
	'lname': 'Last Name',
	'email': 'E-Mail',
	'address': 'Address',
	'zip': 'Zip/Postal Code',
	'city': 'City'
};

function rnd100(v) {
    t=""+Math.round(v*100)/100;
    l=t.indexOf(".");
    if (l<0) {
        t += ".00";
    }
    else if((t.length-l-1)==1) {
        t+="0";
    }
    return t;
}

function updscrollers() {
  for(i=0;i<=maxscrollds;i++) {
    t=getelementbyid('scrolldiscuss'+i+'t');
    //alert(t.offsetHeight)
    if(t.offsetHeight < scrollws[i] ) {
      tup=getelementbyid('scrolldiscuss'+i+'up');
      tdn=getelementbyid('scrolldiscuss'+i+'dn');
      tup.src='stuff/images/bars/scrollbarupb.gif';
      tdn.src='stuff/images/bars/scrollbardnb.gif';
      scrollws[i]=t.offsetHeight;
      sd=getelementbyid('scrolldiscuss'+i);
      sd.style.height=t.offsetHeight+'px';
      //alert(sd.style.height)
    }
  }
}


function getelementbyid(myid) {
   if (isNS4){
        objElement = document.layers[myid];
     }else if (isIE4) {
        objElement = document.all[myid];
     }else if (isIE5 || isNS6) {
             objElement = document.getElementById(myid);
     }
return(objElement);
}

function getparentelementbyid(myid) {
   if (isNS4){
        objElement = parent.layers[myid];
     }else if (isIE4) {
        objElement = parent.all[myid];
     }else if (isIE5 || isNS6) {
             objElement = parent.document.getElementById(myid);
     }
return(objElement);
}

 function findPos(obj) {
         var curleft = curtop = 0;
         if (obj && obj.offsetParent) {
                 curleft = obj.offsetLeft
                 curtop = obj.offsetTop
                 while (obj = obj.offsetParent) {
                         curleft += obj.offsetLeft
                         curtop += obj.offsetTop
                 }
         }
         return [curleft,curtop];
 }
function show(id)
 {
  el=getelementbyid(id);
  if(el) {
    el.style.display='block';
  }
 }
function xlshow(id)
 {
  el=getelementbyid(id);
  if(el) {
    el.style.display='block';
  }
 }
function xlshowhide(id)
 {
  el=getelementbyid(id);
  if(typeof el == undefined) { return ;}
  if((el.style.display=='block')||(el.style.display=='')) {
    xlhide(id);
  }
  else {
    xlshow(id);
  }
 }
function confdel(url,msg)
 {
  if (typeof(msg)=='undefined') 
   { 
    msg="Are you sure you want to do this?\nThis action could not be undone!";
   }
  if (confirm(msg))
   {
    location.href=url;
   }
 }

function newreq() {
        var xmlhttp=false;
        /*@cc_on @*/
        /*@if (@_jscript_version >= 5)
        // JScript gives us Conditional compilation, we can cope with old IE versions.
        // and security blocked creation of the objects.
        try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                try {
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                        xmlhttp = false;
                }
        }
        @end @*/
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                try {
                        xmlhttp = new XMLHttpRequest();
                } catch (e) {
                        xmlhttp=false;
                }
        }
        if (!xmlhttp && window.createRequest) {
                try {
                        xmlhttp = window.createRequest();
                } catch (e) {
                        xmlhttp=false;
                }
        }
        return xmlhttp;
}
xmlhttp=newreq();

function admin_tyres_load() {
  fwidth=getelementbyid("twidth").value;
  fheight=getelementbyid("theight").value;
  fratio=getelementbyid("tratio").value;
  xmlhttp.open('GET', 'ajax/admin_tyres.php?act=list&w='+fwidth+'&h='+fheight+'&r='+fratio,true);
  xmlhttp.onreadystatechange=function() {
    if(xmlhttp.readyState==4) {
      getelementbyid('admin_tyres_list').innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}
function user_tyres_load() {
  var xmlhttp=newreq();
  if(getelementbyid("twidth")) {
    fwidth=getelementbyid("twidth").value;
    fheight=getelementbyid("theight").value;
    fratio=getelementbyid("tratio").value;
  }
  else {
  	fwidth=0;fheight=0;fratio=0;
  }
  xmlhttp.open('GET', 'ajax/admin_tyres.php?act=ulist&w='+fwidth+'&h='+fheight+'&r='+fratio,true);
  xmlhttp.onreadystatechange=function() {
    if(xmlhttp.readyState==4) {
      getelementbyid('user_tyres_list').innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}

function shipping_estimate() {
	//zip=document.buy.zip.value;
	weight=document.buy.weight.value;
	method=document.buy.shipmethod.value;  
  country=document.buy.country.value;
	prc=document.buy.price.value;
	itot=document.buy.itotal;

	tshp=getelementbyid('tshipping');
  clist=getelementbyid('countrylist');
	ttot=getelementbyid('ttotal');
  
  xmlhttp_m=newreq();
  xmlhttp_m.open('GET', 'ajax/methods.php?weight='+weight+'&country='+country+'&selected='+method,false);
  xmlhttp_m.send(null);

  clist.innerHTML=xmlhttp_m.responseText;
	if(country=='') {
		tshp.innerHTML='Enter country Code to estimate';
	}
	else {
		tshp.innerHTML='Estimating...';
    xmlhttp_c=newreq();
    xmlhttp_c.open('GET', 'ajax/usps.php?method='+method+'&weight='+weight+'&country='+country,false);
    xmlhttp_c.send(null);
    tshp.innerHTML=xmlhttp_c.responseText;
    if(IsNumeric(tshp.innerHTML)) {
      itot.value=rnd100(parseFloat(tshp.innerHTML)+parseFloat(prc));
      ttot.innerHTML='$'+itot.value;
      tshp.innerHTML='$'+tshp.innerHTML;
    }
	}
  
  /*if(country=='USA'){
    clist.innerHTML="<select name='shipmethod' ` class='i100' onchange='shipping_estimate()'>"+
    "<option value='FCM'>First Class Mail</option><option value='PM'>Priority Mail</option><option value='EM'>Express Mail</option>"+
    "</select>";
  }
  else{
    clist.innerHTML="<select name='shipmethod' ` class='i100' onchange='shipping_estimate()'>"+
    "<option value='FCM'>First Class Mail</option><option value='PM'>Priority Mail</option>"+
    "</select>";
  } */

}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function validateform(form,x,errdiv) {
  var gut=true;
  errdiv.innerHTML="";
  for (var i = 0;i<form.elements.length;i++) {
    y=form.elements[i];
    if((in_array(y.name,x))&&(y.value.length==0)) {
      gut=false;
      if(typeof(local[y.name])==undefined) {
        local[y.name]=y.name;
      }
      errdiv.innerHTML+='<img src="images/errdiv.gif" alt="Error"> Please fill '+local[y.name]+' field<br>';
      errdiv.style.padding='5px';
      errdiv.style.border='1px #d8d885 solid';
    }
  }
  return gut;
}

function in_array(the_needle, the_haystack) {
  var the_hay = the_haystack.toString();
  if(the_hay == '') {
      return false;
  }
  var the_pattern = new RegExp(the_needle, 'g');
  var matched = the_pattern.test(the_haystack);
  return matched;
}


