// Handles rollover images for NN3+ and IE4+
var loaded = new Array();
var F_menuIsEntered = false;
var F_menuIsCreated = false;
var F_menuAgt = navigator.userAgent.toLowerCase();
var F_menuIsGecko = (F_menuAgt.indexOf('gecko') != -1);
var F_menuIsOpera = (F_menuAgt.indexOf('opera') != -1);
var F_menuSubmenu;
var F_menuTable = new Array();

function F_loadRollover(image,imageName,menu) {
	if (image && image.src &&
		(null == image.out || typeof(image.out) == typeof(void(0)))) {
		s = image.src;
		image.out = new Image();
		image.out.src = s;
		image.over = new Image();
		if (imageName.lastIndexOf('/') >= 0 || imageName.lastIndexOf('\\') >= 0) {
			s = imageName;
		} else {
			i = s.lastIndexOf('/');
			if (i<0) i = s.lastIndexOf('\\');
			if (i<0) { s = imageName; }
			else	 { s = s.substring(0,i+1) + imageName; }
		}
		image.over.src = s;
		image.menu = menu;
		loaded[image.name] = image;
	}
}
function F_roll(imageName,over) {
	if (document.images) {
	if (over) { imageObject = "over"; }
	else	  { imageObject = "out"; }
	image = loaded[imageName];
	if (image) {
		ref = eval("image."+imageObject);
		if (ref) image.src = eval("image."+imageObject+".src");
		else image.src = '';

		ref = eval("image.menu");
		if (ref)
		{
			if (over)
				F_menuOpen(image, image.menu);
			else
				F_menuClose();
		}
	}
	if (window.event)
		window.event.cancelBubble = true;
	}
}
 
function F_menufindLeft(b)
{
  v = b.offsetLeft;
  if (b.offsetParent)
     v += F_menufindLeft(b.offsetParent);
  return v;
}

function F_menufindTop(b)
{
  v = b.offsetTop;
  if (b.offsetParent)
     v += F_menufindTop(b.offsetParent);
  return v;
}

function F_menuOpen(o, a)
{
	var menu;

	if (F_menuIsGecko)
		menu = document.getElementById(a[1] + 'LYR');
	else if (document.layers)
		menu = document.layers[a[1] + 'LYR'];
	else
		menu = eval(a[1] + 'LYR');
	
	if (F_menuSubmenu)
	{
		if (F_menuSubmenu != menu)
		{
			if (document.layers)
				F_menuSubmenu.visibility = 'hidden';
			else
				F_menuSubmenu.style.visibility = 'hidden';
		}
		else
		{
			F_menuIsCreated = true;
			return;
		}
	}

	F_menuSubmenu = menu;

	var horizontal = a[2];

	if (document.layers)
	{
		F_menuSubmenu.document.open();
		F_menuSubmenu.document.write(F_menuTable[a[1]]);
		F_menuSubmenu.document.close();
		F_menuSubmenu.top = document.layers[o.name].pageY + (horizontal ? a[0] : 0);
		F_menuSubmenu.left = document.layers[o.name].pageX + (horizontal ? 0 : a[0]);
		F_menuSubmenu.visibility = 'visible';
	}
	else
	{
		var menutop = F_menufindTop(o);
		var menuleft = F_menufindLeft(o);

		if (horizontal)
			menutop += a[0];
		else
			menuleft += a[0];

		F_menuSubmenu.style.top=menutop;
		F_menuSubmenu.style.left=menuleft;
		F_menuSubmenu.style.visibility = 'visible';
	}

	F_menuIsCreated = true;
}

function F_menuClose()
{
	F_menuIsCreated = false;
	setTimeout('F_checkMenuClose()', 100);
}

function F_checkMenuClose()
{
	if (!F_menuIsEntered && !F_menuIsCreated && F_menuSubmenu)
	{
		if (document.layers)
			F_menuSubmenu.visibility = 'hidden';
		else
			F_menuSubmenu.style.visibility = 'hidden';
		F_menuSubmenu = 0;
	}
}

function F_menu(a)
{
	var navname = a[0];
	var s;

	s = '<table onmouseover=F_menumouseover(this) onmouseout=F_menumouseout() border="0" cellspacing="0" cellpadding="0">';
	var i = 1;
	while (i < a.length)
	{
		var link = a[i];
		var target = '';
		var p = link.indexOf(' target=');
		if (p != -1)
		{
			target = link.substr(p + 8);
			link = link.substr(0, p);
		}
		s += '<tr><td width="100%">';
		s += '<a href="' + link + '"';
		if (target.length)
			s += ' target="' + target + '"';
		if (a[i + 2].length)
			s += ' onMouseOver="F_rolls(\'' + navname + 's' + i + '\',1)" onMouseOut="F_rolls(\'' + navname + 's' + i + '\',0)"';
		s += '>';
		s += '<img border="0" src="' + a[i + 1] + '"';
		if (a[i + 2].length)
			s += ' name=\'' + navname + 's' + i + '\' onLoad="F_loadRollover(this,\'' + a[i + 2] + '\',0)"';
		s += '>';
		s += '</a></td></tr>';
		i += 3;
	}
	s += '</table>';

	if (F_menuIsGecko)
	{
		document.writeln("</center><DIV ID='" + navname + "LYR' STYLE='Z-Index:5'>");
		document.writeln(s);
		document.writeln("</DIV>");
	}
	else if (F_menuIsOpera)
	{
		document.writeln("</center><DIV ID='" + navname + "LYR'>");
		document.writeln(s);
		document.writeln("</DIV>");
	}
	else if (document.all)
	{
		document.writeln("<DIV ID='" + navname + "LYR' STYLE='Z-Index:5'>");
		document.writeln(s);
		document.writeln("</DIV>");
	}
	else if (document.layers)
	{
		document.writeln('<LAYER id="' + navname + 'LYR" visibility=hidden onmouseover="F_menumouseover()" onmouseout="F_menumouseout()"></LAYER>');
		F_menuTable[navname] = s;
	}
}

function F_menumouseover()
{
	F_menuIsEntered = true;
	F_menuIsCreated = false;
}

function F_menumouseout()
{
	F_menuIsEntered = false;
	setTimeout('F_checkMenuClose()', 100);
}

function F_rolls(imageName,over)
{
	if (over)
		F_menumouseover();
	else
		F_menumouseout();

	F_roll(imageName,over);
}

function ValidateForm1(form)
{

   if(IsEmpty(form.search)) 
   { 
      alert('Please enter valid Mode Electronics part number or name of the part you are looking for.'); 
      form.search.focus(); 
      return false; 
   }
	
   return true;
 
}

function ValidateForm2(form)
{
   if(IsEmpty(form.stock_number)) 
   { 
      alert('Please enter valid Mode Electronics part number.'); 
      form.stock_number.focus(); 
      return false; 
   }
	
   return true; 
}

function ValidateForm3(el)
{
   if(IsEmpty(el[0].value)) 
   { 
      alert('Please enter your first name.'); 
      return false; 
   }
   if(IsEmpty(el[1].value)) 
   { 
      alert('Please enter your last name.'); 
      return false; 
   }
   if(IsEmpty(el[2].value)) 
   { 
      alert('Please enter the name of your company.'); 
      return false; 
   }
   if(IsEmpty(el[3].value)) 
   { 
      alert('Please enter your email address.'); 
      return false; 
   }
   if(invalidEmail(el[3].value)) 
   { 
      alert('You have entered an invalid email address. Please try again.'); 
      return false; 
   }
   if(IsEmpty(el[4].value)) 
   { 
      alert('Please re-enter enter your email address.'); 
      return false; 
   }
   if(el[3].value != el[4].value) 
   { 
      alert('Entered email addresses do not match');
      return false; 
   }
	
   return true; 
}

function ValidateForm4(form)
{
   if(IsEmpty(form.mail)) 
   { 
      alert('Email address not entered!'); 
      form.mail.focus(); 
      return false; 
   }
   if(IsEmpty(form.stripper)) 
   { 
      alert('Password not entered!'); 
      form.stripper.focus(); 
      return false; 
   }
	
   return true; 
}

function ValidateForm5(form)
{
   if(IsEmpty(form.email)) 
   { 
      alert('Please enter your email address.'); 
      form.email.focus(); 
      return false; 
   }
   if(IsEmpty(form.fname)) 
   { 
      alert('Please enter your first name.'); 
      form.fname.focus(); 
      return false; 
   }
   if(IsEmpty(form.lname)) 
   { 
      alert('Please enter your last name.'); 
      form.lname.focus(); 
      return false; 
   }

   return true; 
}

function IsEmpty(text) {
   if ((text.length==0) ||
   (text==null)) {
      return true;
   }
   else { return false; }
}

function invalidEmail(checkEmail) {
	if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.')))
	{
		return true;
	}
	else {
		return false;
	}
}
function invalidEmail2(checkEmail) {
	if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.')))
	{
		return true;
	}
	else {
		return false;
	}
}

function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
	return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
	return "";
	}
	else{
	return TRIM_VALUE;
	}
}

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function


var pressedItem0 = "-1";
function xppr(im){var i=new Image();i.src='index.html.images/bt'+im;return i;} function xpe0(id){ x=id.substring(0,id.length-1);xe=id.substring(id.length-1,id.length);if (pressedItem0==x&&(xe=='n'||xe=='o')) return;if (xe=='c'){if (pressedItem0!='-1'){document['xpwb'+pressedItem0].src=eval('xpwb'+pressedItem0+'n'+'.src');if(pressedItem0.indexOf('e')!=-1) document['xpwb'+pressedItem0+'e'].src=eval('xpwb'+pressedItem0+'n'+'e.src');}pressedItem0=x;} document['xpwb'+x].src=eval('xpwb'+id+'.src');if(id.indexOf('e')!=-1)document['xpwb'+x+'e'].src=eval('xpwb'+id+'e.src');}
/*xpwb0n=xppr('0_0.gif');xpwb0o=xppr('0_1.gif');xpwb0c=xppr('0_2.gif');xpwb1n=xppr('1_0.gif');xpwb1o=xppr('1_1.gif');xpwb1c=xppr('1_2.gif');xpwb2n=xppr('2_0.gif');xpwb2o=xppr('2_1.gif');xpwb2c=xppr('2_2.gif');xpwb3n=xppr('3_0.gif');xpwb3o=xppr('3_1.gif');xpwb3c=xppr('3_2.gif');xpwb4n=xppr('4_0.gif');xpwb4o=xppr('4_1.gif');xpwb4c=xppr('4_2.gif');xpwb5n=xppr('5_0.gif');xpwb5o=xppr('5_1.gif');xpwb5c=xppr('5_2.gif');
if(pressedItem0!='-1')xpe0(pressedItem0+'c');*/

function Calculate(){

	var j=0;
	var p=0;
	var v=0;
	var i=0;
	var r=0;
	var anum=/(^\d+$)|(^\d+\.\d+$)|(^\.\d+$)/
	
	p = document.CalcForm.power.value;
	if ( p != 0) {
		j = j + 1;
		if ( !anum.test(p)){
			alert("Power is not a valid number.")
			return; }
		}

	v = document.CalcForm.voltage.value;
	if ( v != 0) {
		j = j + 1;
		if ( !anum.test(v)){
			alert("Voltage is not a valid number.")
			return; }
		}
	

	i = document.CalcForm.current.value;
	if ( i != 0) {
		j = j + 1;
		if ( !anum.test(i)){
			alert("Current is not a valid number.")
			return; }
		}

	r = document.CalcForm.resistance.value;
	if ( r != 0) {
		j = j + 1;
		if ( !anum.test(r)){
			alert("Resistance is not a valid number.")
			return; }
		}

	if(j<2){
		alert("Must Enter Two Variables");
		return;
	}
	if(j>2){
		alert("Too Many Variables, Only Enter Two.");
		return;
	}

	if(p == 0){
		if( v == 0 ){
			p = i * i * r; }
		else if ( i == 0 ) {
			p = v * v / r; }
		else {
			p = v * i; }
	}
	if(i == 0){
		if( v == 0 ){
			i = Math.sqrt( p / r); }
		else if ( r == 0 ) {
			i = p / v; }
		else {
			i = v / r; }
	}
	if(v == 0){
		v = p / i; }

	if(r == 0){
		r = p / ( i * i ); }

	document.CalcForm.power.value = ( Math.round( p * 100000 )/100000);
	document.CalcForm.current.value = ( Math.round( i * 100000 )/100000);
	document.CalcForm.voltage.value = ( Math.round( v * 100000 )/100000);
	document.CalcForm.resistance.value = ( Math.round( r * 100000 )/100000);

} 
  
function ClearNumbers(CalcForm){
	document.CalcForm.power.value="";
	document.CalcForm.voltage.value="";
	document.CalcForm.current.value="";
	document.CalcForm.resistance.value=""; 
}

function ValidateLogin(email_id, pass_id)
{
   if(IsEmpty(email_id)) 
   { 
      alert('Email address not entered!'); 
      email_id.focus(); 
      return false; 
   }
   if(IsEmpty(pass_id)) 
   { 
      alert('Password not entered!'); 
      pass_id.focus(); 
      return false; 
   }
	
   return true; 
}



function findResistance()

{
var Vs = Number(document.led.Vs.value);
var Vf = Number(document.led.Vf.value);
var Id = Number(document.led.Id.value);

if (Vs < 2) {
  alert ("Most LEDs require at least 2 volts to operate.");
  return;
}
if (Vf > Vs-0.1) {
  alert ("The source voltage must be higher than the forward voltage.");
  return;
}
if (Vf > Vs) {
  alert ("You entered an LED forward voltage that is greater than your supply voltage.");
  return;
}
if (Vf < 1.6) {
  alert ("Warning: Your LED forward voltage may be to low.");
  return;
}
//
st = new Array(0.1,1.0,1.2,1.5,1.8,2.2,2.7,3.3,3.9,4.7,5.6,6.8,8.2,10,12,15,18,22,27,33,39,47,56,68,75,82,91,100,120,150,180,220,240,270,300,330,360,390,430,470,510,560,620,680,750,820,910,1000,1200,1500,1800,2000,2200,2700,3000,3300,3900,4300,4700,5100,5600,6200,6800,8200,10000,11000);
//
color = new Array('Not Valid','Brown/Black/Gold','Brown/Red/Gold','Brown/Green/Gold','Brown/Grey/Gold','Red/Red/Gold','Red/Violet/Gold','Orange/Orange/Gold','Orange/White/Gold','Yellow/Violet/Gold','Green/Blue/Gold','Blue/Grey/Gold','Grey/Red/Gold','Brown/Black/Black','Brown/Red/Black','Brown/Green/Black','Brown/Grey/Black','Red/Red/Black','Red/Violet/Black','Orange/Orange/Black','Orange/White/Black','Yellow/Violet/Black','Green/Blue/Black','Blue/Grey/Black','Violet/Green/Black','Grey/Red/Black','White/Brown/Black','Brown/Black/Brown','Brown/Red/Brown','Brown/Green/Brown','Brown/Grey/Brown','Red/Red/Brown','Red/Yellow/Brown','Red/Violet/Brown','Orange/Brown/Brown','Orange/Orange/Brown','Orange/Blue/Brown','Orange/White/Brown','Yellow/Orange/Brown','Yellow/Violet/Brown','Green/Brown/Brown','Green/Blue/Brown','Blue/Brown/Brown','Blue/Grey/Brown','Violet/Green/Brown','Grey/Red/Brown','White/Brown/Brown','Brown/Black/Red','Brown/Red/Red','Brown/Green/Red','Brown/Grey/Red','Red/Brown/Red','Red/Red/Red','Red/Violet/Red','Orange/Brown/Red','Orange/Orange/Red','Orange/White/Red','Yellow/Orange/Red','Yellow/Violet/Red','Green/Brown/Red','Green/Blue/Red','Blue/Red/Red','Blue/Grey/Red','Grey/Red/Red','Brown/Black/Orange','OUT-OF-RANGE');
//
wattrat = new Array('1/8','1/4','1/2','1','2','3','4','5','6','7','8','9','10','11','12');
wattsz = new Array('125','250','500','1000','2000','3000','4000','5000','6000','7000','8000','9000','10000','11000','12000');
//
pn = new Array('0','1.0','1.2','1.5','1.8','2.2','2.7','3.3','3.9','4.7','5.6','6.8','8.2','10','12','15','18','22','27','33','39','47','56','68','75','82','91','100','120','150','180','220','240','270','300','330','360','390','430','470','510','560','620','680','750','820','910','1K','1.2K','1.50K','1.8K','2K','2.2K','2.70K','3K','3.3K','3.90K','4.3K','4.7K','5.1K','5.6K','6.2K','6.8K','8.2K','10K','11K');
//
actualR = (1000*(Vs-Vf)/(Id));
for (i=0;i<63;i++){
  if (Math.max(st[i],actualR) == st[i]) {
    closestR = st[i];
    colorR = color[i];
    pnS = pn[i];
    break;
    }
  }
//
actualW = (1000*(Vs-Vf)*(Vs-Vf)/closestR);
for (i=0;i<11;i++){
  if (Math.max(wattsz[i],actualW) == wattsz[i]) {
    closestW = wattrat[i];
    break;
    }
  }
//
actualI = (Vs-Vf)/(closestR)
document.led.R1.value = (Math.round(10*actualR)/10);
document.led.R2.value = closestR;
//document.led.color.value = colorR;
//document.led.I1.value = Math.round(1000*((Vs-Vf)/closestR));
//document.led.P1.value = Math.round(1000 * actualI * Vf);
//document.led.P2.value = Math.round(1000*((Vs-Vf)/closestR)*(Vs-Vf));
document.led.W1.value = closestW;
closestR = 0;
closestW = 0;

if (Vs > 24) {
  alert ("Warning: Your sorce voltage is grater than 24V. Results may not be accurate.");
  return;
}

if (Vs < 3.6) {
  alert ("Warning: Your source voltage is to low.");
  return;
}
}

// Fade Section ///////////////////////////////////////////////////////
function init1() {
	// parse the form, get the select menu options
	// (start and finish colours + loop method)
	start = "666666";
	finish= "d3d3d3";
	loop  = "1"; loopID= 0;

	// parse the text boxes and get the rates of change of
	// each of the different colour channels
	tmp="1";
	if (tmp>=0 && tmp<=255) d_r= parseInt(tmp);
	tmp="1";
	if (tmp>=0 && tmp<=255) d_g= parseInt(tmp);
	tmp="1";
	if (tmp>=0 && tmp<=255) d_b= parseInt(tmp);

	// generate the starting RGB values
	s_r= split(start,0);  s_g= split(start,1);  s_b= split(start,2);

	// and do the same for the final RGB values
	f_r= split(finish,0); f_g= split(finish,1); f_b= split(finish,2);

	// work out the difference between the start and finish
	// colours of each channel
	l_r= s_r- f_r;        l_g= s_g- f_g;        l_b= s_b- f_b;

	// and set the current colour to the starting colour
	red= s_r;             green= s_g;           blue= s_b;
}

// The main fade routines ////////////////////////////////////////////////

function startFade(ELEMENT) {
	// in case of multiple clicks on the start button, clear any
	// waiting loops so that this fade isn't affected
	clearTimeout(loopID);

	// create a fading effect by reducing the difference between the
	// current and final RGB channels (according to the rates of change)
	if (l_r< 0-d_r) l_r+=d_r; else { if (l_r> 0+d_r) l_r-=d_r; else l_r=0; }
	if (l_g< 0-d_g) l_g+=d_g; else { if (l_g> 0+d_g) l_g-=d_g; else l_g=0; }
	if (l_b< 0-d_b) l_b+=d_b; else { if (l_b> 0+d_b) l_b-=d_b; else l_b=0; }

	// have we closed the difference to zero?
	if (l_r== 0 && l_g== 0 && l_b== 0) {

		// what type of loop do we want to do?
		if (loop== 0) {
			// then stop fading about
			stopFade();

		} else {
			// we're looping again, so reset the amount of each colour left
			l_r= f_r- s_r; l_g= f_g- s_g; l_b= f_b- s_b;

			if (loop== 1) {
				// two-way fade (swap start and finish colours)
				tmp= s_r; s_r= f_r; f_r= tmp; // swap red channel
				tmp= s_g; s_g= f_g; f_g= tmp; // swap green channel
				tmp= s_b; s_b= f_b; f_b= tmp; // swap blue channel
			} else {
				// one-way fade (do nothing, act naturally)
			}

			// and prepare to do it all again
			loopID= setTimeout("startFade('" + ELEMENT + "')",20);
		}

	} else {
		// business as usual, update the current colour
		red= l_r+ f_r; green= l_g+ f_g; blue= l_b+ f_b;

		// set the background to this colour
		var el = document.getElementById(ELEMENT);
		el.style.color = getHex(red,green,blue);

		// and prepare to do it all again
		loopID= setTimeout("startFade('" + ELEMENT + "')",20);
	}
}

// Utility functions used in the fade ////////////////////////////////////

function split(colour, which_pair) {
	// take a hex string and a string position and return the decimal
	// value of that pair (0xFF= 255 etc..)
	return HexToDec(colour.substring(0+ (which_pair* 2), 2+ (which_pair* 2)));
}

function getHex(r,g,b) {
	// convert 3 dec integers to hex strings and use as a colour value
	return '#'+DecToHex(r)+DecToHex(g)+DecToHex(b);
}

var hexbase="0123456789ABCDEF";
function DecToHex(number) {
	// take a decimal integer, return 2-digit hex string
	return hexbase.charAt((number>> 4)& 0xf)+ hexbase.charAt(number& 0xf);
}

function HexToDec(number) {
	// take a hex string, return decimal integer
	return parseInt(number.toUpperCase(), 16);
}

// flashing link
var col = new String();
var xx=1;

function blink()
{
 if(xx%2) 
 {
  col = "white";
 }else{
  col = "1c2d85";
 }

 flink.style.color=col;xx++;if(xx>2){xx=1};setTimeout("blink()",800);
}