// DEFINITION HERE
// Define All the Battery specs here

var vlow = new Array(); var vhigh = new Array();
var ledList = new Array();
var ledIsat = new Array(); var ledVf = new Array(); 
var ledColor = new Array(); var ledFamily = new Array();

vlow['12V'] = 9.0; vhigh['12V'] = 14.6;
vlow['42V'] = 36; vhigh['42V'] = 48;
vlow['Lithium'] = 3.2; vhigh['Lithium'] = 5.6;	
vlow['NiCd/NiMH']= 1.2; vhigh['NiCd/NiMH'] = 1.6;
vlow['Alkaline1.5V'] = 1.3; vhigh['Alkaline1.5V']= 1.55;
vlow['Alkaline9V'] = 7.2; vhigh['Alkaline9V'] = 9.6;	
vlow['Custom'] = 6; vhigh['Custom'] = 8;

// Dynamic generation


function isNum(InString_1)
{
        if(InString_1.length==0) return false;
        var RefString="+1234567890-.";
        for(Count=0;Count<InString_1.length;Count++)
        {
                 var TempChar=InString_1.substring(Count,Count+1);
                if(RefString.indexOf(TempChar,0)==-1)
                        {
                                return (false);
                        }
        }
        return(true);
}

function updateLEDImages(col, row) {
        var rown = row;
	if (rown > 6)
	    rown = 'n';
	  
	var imagePath  = "/appinfo/webench/led/images/";
	var imagesName = imagePath + "led" + col + "x" + rown+ ".png";
	//var imagesName = imagePath + "led" + col + "x" + row + ".gif";
	document.images['ledimages'].src = imagesName; 
}

function calculateVf_Rd() {

	if (document.solutionSelector.ledpartnumber.value != "custom")
	{
		if (document.solutionSelector.io.value == document.solutionSelector.defaultIf.value)
		{
			document.solutionSelector.vf.value = document.solutionSelector.defaultVf.value;	
			document.solutionSelector.ledrd.value = document.solutionSelector.defaultRd.value;	
		} else {
			if (document.solutionSelector.io.value < document.solutionSelector.IfMin.value ||
                            document.solutionSelector.io.value > document.solutionSelector.IfMax.value)
                	{
                        	var alertStr = "Operating current for this LED ";
	                        alertStr +=  document.solutionSelector.ledpartnumber.value;
       	                 	alertStr += " should be in limit of : ";
       	                 	alertStr += document.solutionSelector.IfMin.value;
                        	alertStr += " < If < ";
                        	alertStr += document.solutionSelector.IfMax.value;
                        	alertStr += " Amps.";
                        	alert(alertStr);

				document.solutionSelector.vf.value = document.solutionSelector.defaultVf.value;	
				document.solutionSelector.ledrd.value = document.solutionSelector.defaultRd.value;	
				document.solutionSelector.io.value = document.solutionSelector.defaultIf.value;	
                	} else {
			
				var nvt = document.solutionSelector.nVt.value;
				var io  = document.solutionSelector.io.value;
				var isat= document.solutionSelector.isat.value;
				var vf  = nvt * Math.log(io/isat);
				document.solutionSelector.vf.value = vf;
				document.solutionSelector.ledrd.value = nvt/io;
			}

		}
	}

	var numpara = document.solutionSelector.ledparallel.value;
	var numseri = document.solutionSelector.ledseries.value;
	var vf      = document.solutionSelector.vf.value;
	var is	    = document.solutionSelector.io.value;
	
	var vout1   = (Math.round(100 * numseri * vf))/100;
	var iout1   = (Math.round(100 * numpara * is))/100;	
	document.solutionSelector.O1V.value = vout1;
	document.solutionSelector.O1I.value = iout1;
	
}

function calculateReq() {

	var numpara = document.solutionSelector.ledparallel.value;
	var numseri = document.solutionSelector.ledseries.value;
	var vf      = document.solutionSelector.vf.value;
	var is	    = document.solutionSelector.io.value;
	
	var vout1   = (Math.round(100 * numseri * vf))/100;
	var iout1   = (Math.round(100 * numpara * is))/100;	
	document.solutionSelector.O1V.value = vout1;
	document.solutionSelector.O1I.value = iout1;
	updateLEDImages(numpara, numseri);
}

function wb5_redirect () {
    var strHref = window.location.href; 
    var strQueryString = "";
    if ( strHref.indexOf("?") > -1 ){ 
        var strQueryString = strHref.substr(strHref.indexOf("?")); 
    }
    window.location="http://webench.national.com/webench5/power/webench5.cgi" + strQueryString;
}

function wb4_redirect () {
    var strHref = window.location.href; 
    var strQueryString = "";
    if ( strHref.indexOf("?") > -1 ){ 
        var strQueryString = strHref.substr(strHref.indexOf("?")); 
    }
    window.location="http://webench.national.com/ss1/wb4" + strQueryString;
}

function redirectSW() {

	if (getParam("redirect") != "")
	{
		wb5_redirect();
	}

	if (getParam("sw") == "WB5")
	{
		wb5_redirect();
	}

//	if (getParam("source") == "AC")
//	{
//		wb5_redirect();
//	}

	if (getParam("sw") == "WB4")
	{
		wb4_redirect();
	}

}

function resetVin () {

	if (getParam("VinMin") != "")
		document.solutionSelector.VinMin.value = getParam("VinMin");
		
	else
		document.solutionSelector.VinMin.value = 15;
	
	if (getParam("VinMax") != "")
		document.solutionSelector.VinMax.value = getParam("VinMax");
	else
		document.solutionSelector.VinMax.value = 30;

	if (getParam("ledparallel") != "")
	{
		var p_led = getParam("ledparallel");
		p_led = p_led - 1;
		document.solutionSelector.ledparallel.options[p_led].selected = true;
	}

	if (getParam("ledseries") != "")
	{
		var s_led = getParam("ledseries");
		s_led = s_led - 1;
		document.solutionSelector.ledseries.options[s_led].selected = true;
	}

	if (getParam("source") != "")
	{
		var isource = getParam("source");
		if (isource == "AC")
		{
			document.solutionSelector.source[0].checked = false;
			document.solutionSelector.source[1].checked = true;
			document.solutionSelector.line_fsw[0].disabled = false;
			document.solutionSelector.line_fsw[1].disabled = false;
		        setInnerText('vmin_text', 'Vin RMS Min');
        		setInnerText('vmax_text', 'Vin RMS Max');
			document.solutionSelector.sw.value = "WB4";

		} 
		else 
		{
			document.solutionSelector.source[0].checked = true;
			document.solutionSelector.source[1].checked = false;
			document.solutionSelector.line_fsw[0].disabled = true;
			document.solutionSelector.line_fsw[1].disabled = true;

		}
	}

	if (getParam("line_fsw") != "")
	{
		var isource = getParam("line_fsw");
		if (isource == "60")
		{
			document.solutionSelector.line_fsw[0].checked = false;
			document.solutionSelector.line_fsw[1].checked = true;
		} 
		else 
		{
			document.solutionSelector.line_fsw[0].checked = true;
			document.solutionSelector.line_fsw[1].checked = false;

		}
	}

	if (getParam("op_TA") != "")
		document.solutionSelector.op_TA.value = getParam("op_TA");
	else
		document.solutionSelector.op_TA.value = 30;

	document.solutionSelector.application.value = "LED_DRIVER";
	document.solutionSelector.fromdisty.value = getParam("fromdisty");

	calculateReq();
}

function getParam( name )
{  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null )    
		return "";  
	else    
		return results[1];
}

function updateBattery() {
	var bType = document.solutionSelector.batteryType.value;
	if (bType == "Custom" || bType == "12V" || bType == "42V") {
		document.solutionSelector.numCell.options[0].selected = true;
		document.solutionSelector.numCell.disabled = true;
		
	} else {
		document.solutionSelector.numCell.disabled = false;
	}
	
	updateBatteryVin();
}

function updateFields () {
	updateBattery();
	findPart ();
	updateLED ();

}


function updateBatteryVin() {


	var bType = document.solutionSelector.batteryType.value;
	var numCell = document.solutionSelector.numCell.value;
	
	
	document.solutionSelector.VinMin.value = vlow[bType] * numCell;
	document.solutionSelector.VinMax.value = vhigh[bType] * numCell;
	
}

function updateLED() {

	if (document.solutionSelector.ledpartnumber.options.length != 0) {
		var partNum = document.solutionSelector.ledpartnumber.value;
		document.solutionSelector.vf.value = ledVf[partNum];
		document.solutionSelector.io.value = ledIsat[partNum];
	}
	
	calculateReq();
		
}


function updateLEDPart(partNum) {

	if (partNum != "" && ledVf[partNum].length != 0) {
		document.solutionSelector.vf.value = ledVf[partNum];
		document.solutionSelector.io.value = ledIsat[partNum];
	}
	
	calculateReq();
		
}

function updateLEDPart2(partNum, lVf, lIo, ifmin, ifmax, rd, nvt, isat) {

	document.solutionSelector.ledpartnumber.value = partNum;
	document.solutionSelector.vf.value = lVf;
	document.solutionSelector.io.value = lIo;
	document.solutionSelector.IfMin.value = ifmin;
	document.solutionSelector.IfMax.value = ifmax;
	document.solutionSelector.ledrd.value = rd;
	document.solutionSelector.nVt.value = nvt;
	document.solutionSelector.defaultIf.value = lIo;
	document.solutionSelector.defaultVf.value = lVf;
	document.solutionSelector.defaultRd.value = rd;
	document.solutionSelector.isat.value = isat;
	
	
	calculateReq();
		
}

function updateLEDCustomStatus() {
	if (document.solutionSelector.ledcustom.checked == true) {
		document.solutionSelector.ledpartnumber.value='custom';	
		document.solutionSelector.vf.readOnly=false;	
		document.solutionSelector.ledrd.readOnly=false;
	} else {
		document.solutionSelector.vf.readOnly=true;	
		document.solutionSelector.ledrd.readOnly=true;
	}
}

function createPartNumber(partnumStr) {
	solutionSelector.ledpartnumber.options.length = 0;
    var comma = ",";
    var trim  = " ";
    var currentColor = solutionSelector.currentColor.value;
    solutionSelector.ledpartnumber.options.length= 256;
    var j = 0;
    
    var partString = partnumStr;
    
    if (partnumStr == "all") {
    	partString = "";
    	for (var vendor in ledList) {
    		
    		partString = ledList[vendor];
    		    
		    var tokens = partString.split(comma);
		    var result = false;
		    
		
		    // Loop through the comma seperator list if any
		    
		    for(var i=0; i< tokens.length; i++)
		    {
		            var str= tokens[i];
		
		            // trim whitespace
		            while(str.slice(0, trim.length) == trim)
		                    str = str.slice(trim.length);
		            while(str.slice(str.length-trim.length) == trim)
		                    str = str.slice(0, str.length-trim.length);
		            
		            var strName = vendor + ': ' + str + ' [';
		            
		            if (currentColor == "all") {        
			            strName = strName + ledColor[str] + ', '     
			        }
			        
			        strName = strName +  ledVf[str] + 'V, '+ ledIsat[str] + 'A]';
			        
		           	if (currentColor == ledColor[str] || currentColor == "all") {
						solutionSelector.ledpartnumber.options[j]=new Option(strName, str, false, false);
						j++;
					}
		    }
		    solutionSelector.ledpartnumber.options[0].selected = true;
    	}
    	
    } else {
    	partString = partnumStr; 
    	var tokens = partString.split(comma);
	    var result = false;
	    
	
	    // Loop through the comma seperator list if any
	    
	    for(var i=0; i< tokens.length; i++)
	    {
	            var str= tokens[i];
	
	            // trim whitespace
	            while(str.slice(0, trim.length) == trim)
	                    str = str.slice(trim.length);
	            while(str.slice(str.length-trim.length) == trim)
	                    str = str.slice(0, str.length-trim.length);
	            
	            var strName = str + ' [';
	            
	            if (currentColor == "all") {        
		            strName = strName + ledColor[str] + ', '     
		        }
		        
		        strName = strName +  ledVf[str] + 'V, '+ ledIsat[str] + 'A]';
		        
	           	if (currentColor == ledColor[str] || currentColor == "all") {
					solutionSelector.ledpartnumber.options[j]=new Option(strName, str, false, false);
					j++;
				}
				solutionSelector.ledpartnumber.options[0].selected = true;
	    }
    }
    

    solutionSelector.ledpartnumber.options.length= j;


}

//
// Will use JSP bean to fill-in dynamic data later
function findPart () {
	var manufacture = solutionSelector.manufacturers.value;
	
	
	if (manufacture == "Custom") {
		solutionSelector.ledpartnumber.options.length=0;
		solutionSelector.vf.disabled = false;
		solutionSelector.io.disabled = false;
		solutionSelector.ledpartnumber.disabled = true;
		document.solutionSelector.O1V.value = "3.0";
		document.solutionSelector.O1I.value = "0.35";		
		return;
	} else {
		solutionSelector.vf.disabled = true;
		solutionSelector.io.disabled = true;
		solutionSelector.ledpartnumber.disabled = false;			
		if (manufacture == "all")
			createPartNumber("all");		
		else
			createPartNumber(ledList[manufacture]);
	}
	
	
	
	updateLED();
}

function check(form)
{       
        if (getParam("base_pn") != "")
        {
                form.base_pn.value = getParam("base_pn");
        }

        if (getParam("AppType") != "")
        {
                form.AppType.value = getParam("AppType");
        }

	var vinMin = form.VinMin.value;
	var vinMax = form.VinMax.value;
        if (form.VinMin.value == "")
     		{
		alert ("Please enter 'VinMin'.");
                form.VinMin.value=14.0;
                return false;
		}
        if (form.VinMax.value == "")
                {
                alert("Please enter 'VinMax'.");
                form.VinMax.value=22.0;
                return false;
                }

        if ((form.O1I.value == "") || (form.O1I.value <= 0))
        	{
                alert("Please enter 'Output 1 I out'. It must be greater than zero.");
                return false;
        	}

        if (parseFloat(vinMax) < parseFloat(vinMin))
        {
                alert(" 'Vin Max' must be greater than or equal to 'Vin Min'. ");
                return false;
        }       
        if (form.disty.value != "")
	{
        	for(Count=0;Count<form.disty.value.length;Count++)
        	{
               		var TempChar=form.disty.value.substring(Count,Count+1);
               		if(TempChar == " ")
                 	{
				alert("Please enter Coupon Id without spaces.");
                                return (false);
                        }
        	}

	}
	if (form.ledpartnumber.value != "custom") 
	{
        	if (form.io.value < form.IfMin.value || 
			form.io.value > form.IfMax.value)
		{
			var alertStr = "Operating current for this LED ";
			alertStr +=  form.ledpartnumber.value;
			alertStr += " should be in limit of : ";
			alertStr += form.IfMin.value;
			alertStr += " < If (" + form.io.value + ") < ";
			alertStr += form.IfMax.value;
			alertStr += " Amps.";
			alert(alertStr);
			return (false);
		}
	}
}  

function updateBatterySelection(value) {
	if (value == "battery") {
		document.solutionSelector.batteryType.disabled = false;
		document.solutionSelector.numCell.disabled = false;
		updateBattery();
	} else {
		document.solutionSelector.batteryType.disabled = true;
		document.solutionSelector.numCell.disabled = true;	
	} 
} 

function updateVin() {
	var numCell = document.solutionSelector.numCell.value;
	var battType = document.solutionSelector.batteryType.value;
	
}

/*
 * Displays checkboxes to filter out Account.
 */
function Callback_Vendor(arrUniqueColumnValues) {
  var strHtml = '';
  strHtml += '<TABLE>';
  for (var iVal = 0; iVal < arrUniqueColumnValues.length; iVal++) {
    if (arrUniqueColumnValues[iVal].value == 'Cree') 
    {
	    strHtml +=
	     '<tr><td>' +
	     '<div><input type="checkbox" checked onclick="' +
	     arrUniqueColumnValues[iVal].onclick + '"/> ' +
	     arrUniqueColumnValues[iVal].value + 
	     '</div></tr></td>';
     } 
     else 
     {
	    strHtml +=
	     '<tr><td>' +
	     '<div><input type="checkbox" onclick="' +
	     arrUniqueColumnValues[iVal].onclick + '"/> ' +
	     arrUniqueColumnValues[iVal].value + 
	     '</div></tr></td>';
     
     }
  }
  strHtml += '</TABLE>';
  document.getElementById('myFilterOut').innerHTML = strHtml;

}

function filterCree () {
	Zapatec.Grid.checkboxOnClick('0',0,'Avago',this.checked);
	//Zapatec.Grid.checkboxOnClick('0',0,'Cree',this.checked);
	Zapatec.Grid.checkboxOnClick('0',0,'OSRAM',this.checked);
	Zapatec.Grid.checkboxOnClick('0',0,'Philips Lumileds',this.checked);
	
}

function filter(value) {
  if (myTable) {
    myTable.setFilter({
      text: value
    });
  }
  return false;
}

function onRowClick(objGrid, objRow) {
  var value = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 2));
  var vf    = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 4));
  var io    = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 5));
  var ifmin = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 9));
  var ifmax = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 10));
  var rd    = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 11));
  var nvt    = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 12));
  var isat    = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 13));
  updateLEDPart2(value, vf, io, ifmin, ifmax, rd, nvt, isat);
};


function updateRowOnClick(objGrid, objRow) {
  var value = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 2));
  var vf    = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 4));
  var io    = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 5));
  var ifmin = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 9));
  var ifmax = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 10));
  var rd    = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 11));
  var nvt    = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 12));
  var isat    = objGrid.getCellValueString(objGrid.getCellByRow(objRow, 13));
  updateLEDPart2(value, vf, io, ifmin, ifmax, rd, nvt, isat);
};

function myStyle(objGrid, objCell) {
	if (objGrid.getCellId(objCell) != 3) {
		return  '';
	}
	
	var cellcolor = objGrid.getCellValue(objCell);
	
	if (cellcolor == 'white' || cellcolor == 'White')
		return 'text-align: center;';
	
	if (cellcolor == 'red-orange') 
		cellcolor = 'orangered';
		
	if (cellcolor == 'amber')
		cellcolor = 'orange';
		
	if (cellcolor == 'cyan')
		cellcolor = 'darkcyan';	
			
	if (cellcolor == 'yellow')
		cellcolor = 'goldenrod';			
		
	if (cellcolor == 'royal blue')
		cellcolor = 'navy';
		
	//return 'background: ' + cellcolor + '; text-align: center;'	
	return 'color: ' + cellcolor + '; text-align: center;'
}
 
function popIt(url, name) 
{
        window.open(url,name,"toolbar=no,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=400,height=400,screenX=50,screenY=50,left=50,top=50");
}

function setInnerText (elementId, text) 
{
	var element;
	if (document.getElementById) {
		element = document.getElementById(elementId);
	}
	else if (document.all) {
		element = document.all[elementId];
	}
	if (element) {
		if (typeof element.textContent != 'undefined') {
			element.textContent = text;
		}
		else if (typeof element.innerText != 'undefined') {
			element.innerText = text;
		}
		else if (typeof element.removeChild != 'undefined') {
			while (element.hasChildNodes()) {
				element.removeChild(element.lastChild);
			}
			element.appendChild(document.createTextNode(text)) ;
		}
	}
}


