﻿function index(u){var L=document.getElementById("Select1").options.length;for(var i=0;i<L;i++){if(document.getElementById("Select1").options[i].text==u){return i;}}}

var start=true;
var urls;
function myCal() {
var nn,fv,tv, ft,tt,sft,stt;
var result,result1;
var output,output1;
var nDicimal,nScientific;
var validated;
var ifzero = "";
var indx1,indx2,ur,up; 
urls = (window.location) ? window.location.protocol + "//" + window.location.host + window.location.pathname : urls;
   
if (start==true) {
var pgurl = location.href; 
var pgurl = pgurl.replace(/%20/g,' ')
var cs = pgurl.match(/v=(.+)\&uf=(.+)\&ut=(.+)\&ur=(\d)/);
if (cs!=null) {
nn = (RegExp.$1) ? RegExp.$1 : 1;
ft = (RegExp.$2) ? RegExp.$2 : document.getElementById("Select1").options[document.getElementById("Select1").selectedIndex].text;
tt = (RegExp.$3) ? RegExp.$3 : document.getElementById("Select2").options[document.getElementById("Select2").selectedIndex].text;
if (RegExp.$4) {
ur = RegExp.$4;
if (ur==1) {
document.getElementById("Radio1").checked=true;
}else 
{
document.getElementById("Radio2").checked=true;
}
}
cs =pgurl.match(/up=(.+)/);
if ((RegExp.$1) && cs!=null) {
document.getElementById("SelectP").selectedIndex = RegExp.$1;
}
cs= pgurl.match(/(.+)\?/);
document.getElementById("Text1").value = nn;
indx1 = index(ft);
indx2 = index(tt);
document.getElementById("Select1").selectedIndex =indx1;
document.getElementById("Select2").selectedIndex =indx2;
}
start =false;
}
validated=numberValidation();
if (validated == true) {
        nn = document.getElementById("Text1").value;
        fv = document.getElementById("Select1").value;
        tv = document.getElementById("Select2").value;
        
       
       indx1 = document.getElementById("Select1").selectedIndex;
        indx2 = document.getElementById("Select2").selectedIndex;
if ((display==1)) {
ft = document.getElementById("Select1").options[indx1].text;
tt = document.getElementById("Select2").options[indx2].text;
}
else
{
ft = du[indx1];
tt = du[indx2];
sft = syb[indx1];
stt = syb[indx2];
}
        
        if (fv == 1) {
            if (tv == 1) {
                result = nn;
            } else if (tv == 2) {
                result = (9 / 5 * parseFloat(nn)) + 32;
            } else if (tv == 3) {
                result = parseFloat(nn) + 273.15;
            } else if (tv == 4) {
                result = (9 / 5) * parseFloat(nn) + 491.67;
            }
        } else if (fv == 2) {
            if (tv == 1) {
                result = 5 / 9 * (parseFloat(nn) - 32);
            } else if (tv == 2) {
                result = parseFloat(nn);
            } else if (tv == 3) {
                result = 5 / 9 * (parseFloat(nn) - 32) + 273.15;
            } else if (tv == 4) {
                result = parseFloat(nn) + 459.67;
            }
        } else if (fv == 3) {
            if (tv == 1) {
                result = parseFloat(nn) - 273.15;
            } else if (tv == 2) {
                result = (9 / 5 * (parseFloat(nn) - 273.15)) + 32;
            } else if (tv == 3) {
                result = parseFloat(nn);
            } else if (tv == 4) {
                result = 9 / 5 * parseFloat(nn);
            }
        } else if (fv == 4) {
            if (tv == 1) {
                result = (parseFloat(nn) - 491.67) * 5 / 9;
            } else if (tv == 2) {
                result = parseFloat(nn) - 459.67;
            } else if (tv == 3) {
                result = 5 / 9 * parseFloat(nn);
            } else if (tv == 4) {
                result = parseFloat(nn);
            }
        }
        up = document.getElementById("SelectP").options[document.getElementById("SelectP").selectedIndex].text;
        if (document.getElementById("Radio1").checked) {
            ur = 1;
            nDicimal = parseInt(up);
            result1 = formatDecimals(result, nDicimal);
        } else {
            ur = 2;
            nScientific = parseInt(up);
            result1 = toScientific(result, nScientific + 1);
        }
        if ((display==1)) {
        output1 = nn + " " + ft + " = " + result1 + " " + tt;}
        else
        {
        output1 = nn + " " + ft + " = " + result1 + " " + tt+ "<br>"
               + nn + " " + sft + " = " + result1 + " " + stt;
}
       
        document.getElementById("rel").innerHTML = output1;
        longurl = urls + "?" + "v=" + nn + "&uf=" + ft + "&ut=" + tt + "&ur=" + ur + "&up=" + up;
        longurl = longurl.replace(/ /g,'%20');
        document.getElementById("txtlink").value = longurl;
    }
}

function linkUnitDetails(){var ft,tt,urlf,urlt;if((display==1)){ft=document.getElementById("Select1").options[document.getElementById("Select1").selectedIndex].text;tt=document.getElementById("Select2").options[document.getElementById("Select2").selectedIndex].text;}else{ft=du[document.getElementById("Select1").selectedIndex];tt=du[document.getElementById("Select2").selectedIndex];}urlf="../unitsDict/Temperatures.htm#"+ft;urlt="../unitsDict/Temperatures.htm#"+tt;if(ft==tt){document.getElementById("linkfrom").innerHTML="more about "+ft.link(urlf);document.getElementById("linkto").innerHTML="";}else{document.getElementById("linkfrom").innerHTML="more about "+ft.link(urlf)+" and ";document.getElementById("linkto").innerHTML=tt.link(urlt);}}
String.prototype.expandExponential = function() {return this.replace(/^([+-])?(\d+).?(\d*)[eE]([-+]?\d+)$/,function(x, s, n, f, c){var l = +c < 0, i = n.length + +c, x = (l ? n : f).length,c = ((c = Math.abs(c)) >= x ? c - x + l : 0),z = (new Array(c + 1)).join("0"), r = n + f;return  (l ? r = z + r : r += z).substr(0, i += l ? z.length : 0) + (i < r.length ? "." + r.substr(i) : "");});};
function formatDecimals(num,digits){if(digits<=0){return Math.round(num);}var tenToPower=Math.pow(10,digits);var cropped=String(Math.round(num*tenToPower)/tenToPower);cropped=String(cropped).expandExponential();if(cropped.indexOf(".")==-1){cropped+=".0";}var halves=cropped.split(".");var zerosNeeded=digits-halves[1].length;for(var i=1;i<=zerosNeeded;i++){cropped+="0";}return(cropped);}
function toScientific(num,sigDigs){num=Number(num);if(isNaN(num))return num;var exponent=Math.floor(Math.log(Math.abs(num))/Math.LN10);if(num==0)exponent=0;var tenToPower=Math.pow(10,exponent);var mantissa=num/tenToPower;mantissa=formatDecimals(mantissa,sigDigs-1);var output=mantissa;if(exponent!=0){output+="e"+exponent;}return(output);}
function writeJS(){var str='';str+='<ul >';str+='<li><a href="Angle.htm">Angle<\/a><\/li>';str+='<li><a href="Area.htm">Area<\/a><\/li>';str+='<li><a href="Density.htm">Density<\/a><\/li>';str+='<li><a href="..\/unitguide\/Electricity.htm">Electricity<\/a><\/li>';str+='<li><a href="Energy.htm">Energy<\/a><\/li>';str+='<li><a href="FlowRate.htm">Flow Rate<\/a><\/li>';str+='<li><a href="Force.htm">Force<\/a><\/li>';str+='<li><a href="FuelEconomy.htm">Fuel Economy<\/a><\/li>';str+='<li><a href="..\/unitguide\/Heat.htm">Heat<\/a><\/li>';str+='<li><a href="Length.htm">Length<\/a><\/li>';str+='<li><a href="..\/unitguide\/Light.htm">Light<\/a><\/li>';str+='<li><a href="..\/unitguide\/Magnetism.htm">Magnetism<\/a><\/li>';str+='<li><a href="Mass.htm">Mass<\/a><\/li>';str+='<li><a href="Pressure.htm">Pressure<\/a><\/li>';str+='<li><a href="Power.htm">Power<\/a><\/li>';str+='<li><a href="..\/unitguide\/Radiation.htm">Radiation<\/a><\/li>';str+='<li><a href="Temperature.htm">Temerature<\/a><\/li>';str+='<li><a href="Time.htm">Time<\/a><\/li>';str+='<li><a href="Torque.htm">Torque<\/a><\/li>';str+='<li><a href="Velocity.htm">Velocity<\/a> <\/li> ';str+='<li><a href="Volume.htm">Volume<\/a><\/li>';str+='<li><a href="..\/unitguide\/Viscosity.htm">Viscosity<\/a><\/li>';str+='<\/ul>';document.write(str);}
function NiftyCheck(){if(!document.getElementById||!document.createElement)return(false);var b=navigator.userAgent.toLowerCase();if(b.indexOf("msie 5")>0&&b.indexOf("opera")==-1)return(false);return(true);}function Rounded(selector,bk,color,size){var i;var v=getElementsBySelector(selector);var l=v.length;for(i=0;i<l;i++){AddTop(v[i],bk,color,size);AddBottom(v[i],bk,color,size);}}function RoundedTop(selector,bk,color,size){var i;var v=getElementsBySelector(selector);for(i=0;i<v.length;i++)AddTop(v[i],bk,color,size);}function RoundedBottom(selector,bk,color,size){var i;var v=getElementsBySelector(selector);for(i=0;i<v.length;i++)AddBottom(v[i],bk,color,size);}function AddTop(el,bk,color,size){var i;var d=document.createElement("b");var cn="r";var lim=4;if(size&&size=="small"){cn="rs";lim=2}d.className="rtop";d.style.backgroundColor=bk;for(i=1;i<=lim;i++){var x=document.createElement("b");x.className=cn+i;x.style.backgroundColor=color;d.appendChild(x);}el.insertBefore(d,el.firstChild);}function AddBottom(el,bk,color,size){var i;var d=document.createElement("b");var cn="r";var lim=4;if(size&&size=="small"){cn="rs";lim=2}d.className="rbottom";d.style.backgroundColor=bk;for(i=lim;i>0;i--){var x=document.createElement("b");x.className=cn+i;x.style.backgroundColor=color;d.appendChild(x);}el.appendChild(d,el.firstChild);}function getElementsBySelector(selector){var i;var s=[];var selid="";var selclass="";var tag=selector;var objlist=[];if(selector.indexOf(" ")>0){s=selector.split(" ");var fs=s[0].split("#");if(fs.length==1)return(objlist);return(document.getElementById(fs[1]).getElementsByTagName(s[1]));}if(selector.indexOf("#")>0){s=selector.split("#");tag=s[0];selid=s[1];}if(selid!=""){objlist.push(document.getElementById(selid));return(objlist);}if(selector.indexOf(".")>0){s=selector.split(".");tag=s[0];selclass=s[1];}var v=document.getElementsByTagName(tag);if(selclass=="")return(v);for(i=0;i<v.length;i++){if(v[i].className==selclass){objlist.push(v[i]);}}return(objlist);}
function numberValidation(){if(isNaN(document.getElementById("Text1").value)){document.getElementById("txtAlart").innerHTML="* \n\n number! number!";document.getElementById("Text1").focus();document.getElementById("Text1").select();linkUnitDetails();return(false);}else{var cc,nn,fv,tv;nn=document.getElementById("Text1").value;fv=document.getElementById("Select1").value;tv=document.getElementById("Select2").value;ft=document.getElementById("Select1").options[document.getElementById("Select1").selectedIndex].text;tt=document.getElementById("Select2").options[document.getElementById("Select2").selectedIndex].text;if(fv==1){cc=parseFloat(nn);}else if(fv==2){cc=5/9*(parseFloat(nn)-32);}else if(fv==3){cc=parseFloat(nn)-273.15;}else if(fv==4){cc=(parseFloat(nn)-491.67)*5/9;}}if(cc<-273.15){if(fv==1){document.getElementById("rel").innerHTML="* input temperatue must be > = -273.15°C";document.getElementById("rel").style.color="red";}else{document.getElementById("rel").innerHTML="* input temperature "+nn+" "+ft+", < -273.15 Celsius. Can this happen on earth?";document.getElementById("rel").style.color="red";}document.getElementById("Text1").focus();document.getElementById("Text1").select();linkUnitDetails();return(false);}else{document.getElementById("txtAlart").innerHTML="";linkUnitDetails();return(true);}}



var display=1;
var isload=false;

function loadjs(){if(isload==false){var fl="../../scripts/Temp0.js";var rf=document.createElement('script');rf.setAttribute("language","javascript");rf.setAttribute("type","text/javascript");rf.setAttribute("src",fl);if(typeof rf!="undefined"){document.getElementsByTagName("head")[0].appendChild(rf);}isload=true;}else if((display==1)&&(isload=true)){unit();}else if((display==2)&&(isload=true)){symbol();}else if((display==3)&&(isload=true)){unitsymbol();}}
function init(){var ele=document.getElementsByTagName("span");var i=0;for(var i=0;i<ele.length;i++){if(ele[i].id.indexOf("dis")!=-1){ele[i].onclick=setDisplay;}}}

 function getTargetElement(evt) {
	 var elem = (evt.target) ? ((evt.target.nodeType == 3) ? evt.target.parentNode : evt.target) : evt.srcElement;
	    return elem;
    }
    
    
 function setDisplay(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "")
	 
		if(evt){var elem=getTargetElement(evt);if(elem.id=="disunit"){display=1;loadjs();}else if(elem.id=="dissym"){display=2;loadjs();}else if(elem.id=="disus"){display=3;loadjs();}}
    }
    
window.onload=function(){if(!NiftyCheck())return;Rounded("div.vnav","#FFF","#E4E7F2");Rounded("div.bkmk","#FFF","#E4E7F2");Rounded("div.calDiv","#FFF","#E4E7F2");init();myCal();}
