

// This initStuff function is called from a scriptmanager.RegisterStartupScript in the codebehind page.
function initStuff() 
{
//debugger;
    // Get all the Elements with the Class Name "UserInputs"
    var items = document.getElementsByClassName('UserInputs');
	
	// iterate over each element with the classname of UserInputs.  This classname has been placed on 
	// each of the <asp:TextBox> objects.
    items.each(function(item)
    {
        // Bind the observe keypress event to each item
        Event.observe(item,'keypress',function()
        {   // If the event is thrown and the divInputsChanged div is hidden then show it.
	        if ($('divInputsChanged').style.visibility == 'hidden')
		        showdiv('divInputsChanged');
        }.bind(item));
    });
    
    setupZoom();
    
    ShowHideChannelButtons('remove');
}

           


function showdiv(id) 
{
    var elem = document.getElementById(id);
    
    if (elem != null)
    {
        elem.style.visibility  = "visible";
	    elem.style.display = "";
	}
}
     
function SelectRadioButton(rbtn, gridView)
{
    hidediv('pnlPopIC');
    document.getElementById('Button1').click();
    inputComponents = document.getElementsByTagName("input");
    for (i = 0; i < inputComponents.length; i++)
    {
        if (inputComponents[i].type == "radio")
        {
            if (inputComponents[i].id.indexOf(gridView + "_ctl") != -1)
            {
                inputComponents[i].checked = false;
            }
        }
    }
    var hdnIC = document.getElementById('hdnSelectedIC');
    hdnIC.value = rbtn.value;
    rbtn.checked = true;

 }
function Underline(comp)
{
    document.getElementById(comp).style.textDecoration = "underline";
}

function NoUnderline(comp)
{
    document.getElementById(comp).style.textDecoration = "none";
}

function ShowUpdatingBOM()
{
    var bomUpdateDIV = document.getElementById('uprogressBOM');
    bomUpdateDIV.style.visibility = "visible";
    bomUpdateDIV.style.display = "inline";    
    var bomGV = document.getElementById('gvBOMnews');
    bomGV.style.visibility = "hidden";
    bomGV.style.display = "none";
}

function ShowUpdatingChart()
{
    var chartUpdateDIV = document.getElementById('uprogressChart');
    chartUpdateDIV.style.visibility = "visible";
    chartUpdateDIV.style.display = "inline";
    var chartIMGChart = document.getElementById('ImgEfficencyChart');
    chartIMGChart.style.visibility = "hidden";
    chartIMGChart.style.display = "none";
}



// BELOW


// Implements the ScrollTo object must have an id attribute set
// Also, this function relies on scriptaculous being loaded previously
function ScrollToObject(obj)
{
    new Effect.ScrollTo(obj, {duration: 1.0});
    return false;
}

// merge these two functions, add param
function JumpToObject(obj)
{
    new Effect.ScrollTo(obj, {duration: 0.0});
    return false;
}

function SetActiveTab(tabControl, tabNumber)
{
    var ctrl = $find(tabControl);
    ctrl.set_activeTab(ctrl.get_tabs()[tabNumber]);                  
}


function ToggleInputChangeDetectedMessage(sToggle)
{
    if (sToggle == "Display")
    {
        div = document.getElementById("divOptValuesChangeMsg");
        div.style.visibility = "visible";
		div.style.display = "inline";
	}
	else
	    // hidden
	{
	    div = document.getElementById("divOptValuesChangeMsg");
        div.style.visibility = "hidden";
        div.style.display = "none";    
	}
}

function dispVal(p_id)  {  alert ("Control VALUE for " + p_id + "= " + document.getElementById(p_id).value); }

function RangeChecks()
{
//   	if (document.getElementById("TextBoxVout").value != "") {

//	if (document.getElementById("TextBoxVinMin").value > document.getElementById("TextBoxVinMax").value) 
//	{
//        //
//    }
}

function CheckRequiredFields()
{

    sDisp = "show";
	// Only Enable the buttons if we have a dropdown.
	if (document.getElementById("TextBoxVout").value != "") {
	            
	    if (document.getElementById("TextBoxIout").value != "") {	     
		    if (document.getElementById("TextBoxVinMin").value != "") {
 		        if (document.getElementById("TextBoxVinMax").value != "") {
    		        if (document.getElementById("TextBoxTMaxAmb").value != "") {    		                
    		                sDisp="hide";    		                
                    }
                }
            }
        }
    }
    
    if (sDisp == "show") 
        showdiv('spanViewSolutionMsg');     
    else // "hide
        hidediv('spanViewSolutionMsg'); 
}

function ClearRequiredFields()
{
    ClearField("tbxVinmin1");
    ClearField("tbxVinmax1");
    ClearField("tbxVout1");
    ClearField("tbxIout1");
    ClearField("tbxTmax1");
    ClearField("tbxVinmin2");
    ClearField("tbxVinmax2");
    ClearField("tbxVout2");
    ClearField("tbxIout2");
    ClearField("tbxTmax2");
}

function ClearField(id)
{   
    var element = $(id);
    if( element != null){
        element.value = "";
    }
}

function ShowHideSelectICDiv(action)
{

    if (action == "show")
    {
     
      // Show the "Select an IC" div
      showdiv("divSelectIC");
      
      // Hide the "User Inputs" div
      hidediv("divUserInputs");
      
      $("spanSelectICLabel").style["visibility"] = "hidden";
     
    }
    else
    {
      // Show the "User Inputs" div
      showdiv("divUserInputs");
      
      // Hide the "Select an IC" div
      hidediv("divSelectIC");
      
      // Hide the label.
       $("spanSelectICLabel").style["visibility"] = "visible";  
     
    }
}

function disableChannel2AdvancedInputs(flag)
{
    var inputs = document.getElementsByTagName("select"); 
   // var inputArray = new Array();  

    for(var i=0; i<inputs.length; i++){ 
	    if(inputs[i].id.indexOf("_ch2")>0)
	    {
	        if($(inputs[i].id) != "null")
	        {
	            //another if loop to check if it was already disabled
	           $(inputs[i].id).disabled =  flag; 
	        }
		    //inputArray.push(inputs[i].id);		
	    }
	 }
}
function ShowHideChannelButtons(action)
{
    var ctrl = null;
   
    if (action == "add")
    {
        // Update the Tmax for channel 2 from channel 1
        document.getElementById("tbxTmax2").value = document.getElementById("tbxTmax1").value;
        
        //update for VinMax
        document.getElementById("tbxVinmin2").value = document.getElementById("tbxVinmin1").value;
        
        //update for VinMin
        document.getElementById("tbxVinmax2").value = document.getElementById("tbxVinmax1").value;
        
        //setting the dual channel flag to true
        document.getElementById("hdnDualChannel").value = "true";
        
        // Show the Table Row of Inputs
        showdiv("extraChannelRow");
        
        // Hide the Add Channel Button
        hidediv("btnAddChannel");
        
        // Show the Remove Channel Button
        showdiv("btnRemoveChannel");
        
        if($("hdnIC").value == "ADP1823" || $("hdnIC").value == "ADP1829"){
            disableChannel2AdvancedInputs(false);
        }
        
        setContraints();
    }
    else
    {
        ClearField("tbxVinmin2");
        ClearField("tbxVinmax2");
        ClearField("tbxVout2");
        ClearField("tbxIout2");
        ClearField("tbxTmax2");
        
        // Hide Extra Channel Row
        hidediv("extraChannelRow");
        
        // Show the Add Channel Button
        showdiv("btnAddChannel");
        
        // Hide the Remove Button
        hidediv("btnRemoveChannel");
        
        //setting the dual channel flag to false
        document.getElementById("hdnDualChannel").value = "false";
        
        if($("hdnIC").value == "ADP1823" || $("hdnIC").value == "ADP1829"){
            disableChannel2AdvancedInputs(true);
        }
        
        setContraints();
    }
}

function ShowEmail()
{
    // Enable the Send button
    document.getElementById('btnEmail').disabled = false;
    
    // Hide the successful message
    hidediv('lblEmailMessage');
    
    // Show the Email div window
    showdiv('EmailPanel');
}

/*
navigate handler added to history for using the history control on the client side
function histNav(sender, eventArgs) {
         
         if(Sys.Application.get_history()._historyPointIsNew){
            
            if (eventArgs == null) {
                eventArgs = new Sys.HistoryEventArgs();
                throw Error.invalidOperation('Expected HistoryEventArgs');
            }
            var state = eventArgs.get_state();
            
            if(modalFlag){
                destroyIframe();
            }else{
            //    __doPostBack('Sys.Application.get_history()._clientId','');
            }
         
         }

}*/

function openModalPopUp(hover){

    var id = hover.id;
    var hdnBomCoponent;
    var hdnBomNumber;
    
    if(id.indexOf("lbnForEditHover") >=0)
    {
        hdnBomCoponent = $(id.replace("lbnForEditHover","hdnBOMComponent")).value;
        hdnBomNumber = $(id.replace("lbnForEditHover","hdnBOMNumber")).value;
    }else{
        hdnBomCoponent = $(id.replace("lblBOMItemNumber","hdnBOMComponent")).value;
        hdnBomNumber = $(id.replace("lblBOMItemNumber","hdnBOMNumber")).value;
    }
    
    var loc = document.location+ "";
    var loc = loc.substring(0, loc.indexOf("dtPowerMain"));
    var src = "UpdateComponentChild.aspx?compNm=" + hdnBomCoponent + "&compNumb=" + hdnBomNumber;
     
    
    var ifrm = document.createElement("iframe");
    ifrm.id = "ifrm";
    ifrm.setAttribute("src", loc+src);
   /* ifrm.style.width = 100 +"%";
    ifrm.style.height = 600+"px";
    */
    ifrm.className = "modalIframe"

    $("modalWindow").appendChild(ifrm);


    
    
    setTimeout('$find(modalPopUp).show()',3000);
   // $("modalWindow").style.display = "";
    
    Sys.Application.get_history().add_navigate(destroyIframe);
}

function destroyIframe(){
    var ifrm = $("ifrm");
    
    if(ifrm){
        try{
            $("modalWindow").removeChild(ifrm);
        }catch(e){
            ifrm.parentNode.removeChild(ifrm);
        }
    }
    $find(modalPopUp).hide();
     Sys.Application.get_history().remove_navigate(destroyIframe);
}

function disableDropdown(driverDropdown, slaveDropdown){
    
    if($(driverDropdown).selectedIndex > 0){
        $(slaveDropdown).disabled = false;
        //$(slaveDropdown).enable();
    }else{
        $(slaveDropdown).selectedIndex = 0;
         $(slaveDropdown).disabled = true;
    }
}

function updateChanneltwo(channel1Id){
    var channel2Id = channel1Id.replace("1", "2");
    
    if(($(channel2Id) != null) && ($(channel1Id).value != "")){
        $(channel2Id).value = $(channel1Id).value   
    }
}

function setTrackVin_ch1(trackingId, trackVinid){
   
   var trackVinArray;
    if($(trackingId).selectedIndex == 0){
        $(trackVinid).disabled = true;
    }else if($(trackingId).selectedIndex == 2){
        $(trackVinid).disabled = false;
       //remove existing options
       $(trackVinid).options.length = 0;
       
       //split the string into an array
       trackVinArray = trackVinRatio_ch1.split(",");
       
       for(var i=0; i < trackVinArray.length; i++)
       {
            var optn = document.createElement("OPTION");
            optn.text = trackVinArray[i];
            optn.value = (i+1);
            $(trackVinid).options.add(optn);
       }
      // $(trackVinid).innerHTML = trackVinRatio_ch1;
    }else if($(trackingId).selectedIndex == 1){
        $(trackVinid).disabled = false;
        //$(trackVinid).innerHTML = trackVinCoin_ch1;
        $(trackVinid).options.length = 0;
       
       //split the string into an array
       trackVinArray = trackVinCoin_ch1.split(",");
       
       for(var i=0; i < trackVinArray.length; i++)
       { 
            var optn = document.createElement("OPTION");
            optn.text = trackVinArray[i];
            optn.value = (i+1);
            $(trackVinid).options.add(optn);
       }
        
    }
    
}

function setTrackVin_ch2(trackingId, trackVinid){
    
    var trackVinArray;
    
    if($(trackingId).selectedIndex == 0){
        $(trackVinid).disabled = true;
    }else if($(trackingId).selectedIndex == 2){
       $(trackVinid).disabled = false;
      // $(trackVinid).innerHTML = trackVinRatio_ch2;
        //remove existing options
       $(trackVinid).options.length = 0;
       
       //split the string into an array
       trackVinArray = trackVinRatio_ch2.split(",");
       
       for(var i=0; i < trackVinArray.length; i++)
       {
            var optn = document.createElement("OPTION");
            optn.text = trackVinArray[i];
            optn.value = (i+1);
            $(trackVinid).options.add(optn);
       }
    
    }else if($(trackingId).selectedIndex == 1){
       // $(trackVinid).innerHTML = trackVinCoin_ch2;
        $(trackVinid).disabled = false;
        
         $(trackVinid).options.length = 0;
       
       //split the string into an array
       trackVinArray = trackVinCoin_ch2.split(",");
       
       for(var i=0; i < trackVinArray.length; i++)
       { 
            var optn = document.createElement("OPTION");
            optn.text = trackVinArray[i];
            optn.value = (i+1);
            $(trackVinid).options.add(optn);
       }
    }
    
    
}


function addBackground(){
if($("hdnIC").value == "ADP1821" || $("hdnIC").value == "ADP1822" || $("hdnIC").value == "ADP1823" || $("hdnIC").value == "ADP1828" || $("hdnIC").value == "ADP1829")
    $("divUpdateProgress4").style.display="block";
    $("DivUProg").style.display = "none";
}

function removeBackground(){
     $("DivUProg").style.display = "";
    $("divUpdateProgress4").style.display="none";
}

 function hidediv(id){
    var elem = document.getElementById(id);
    
    if (elem != null)
    {
        elem.style.visibility = "hidden";
        elem.style.display = "none"; 
    }
}

function handlePkgTopChanges(pkgTopCh1Id, pkgTopCh2Id, pkgBotCh1Id, pkgBotCh2Id){
    //check if dual channel mode is on, the current IC is 1823 or 1829
    if(($("hdnIC").value == "ADP1823") || ($("hdnIC").value == "ADP1829"))
    {
        var noAutoFlag = true;
        var pkgTopch2Array;
        var pkgBotCh1Array;
        var pkgBotCh2Array;
        
         //erase all the existing options
        $(pkgTopCh2Id).options.length = 0;
        $(pkgBotCh1Id).options.length = 0;
        $(pkgBotCh2Id).options.length = 0;
        
        if($(pkgTopCh1Id).selectedIndex == 0){
            
            var optn = document.createElement("OPTION");
            optn.text = "AutoQLa";
            optn.value = 1;
            $(pkgTopCh2Id).options.add(optn);
            
            var optn = document.createElement("OPTION");
            optn.text = "AutoQHb";
            optn.value = 1;
            $(pkgBotCh1Id).options.add(optn);
            
            var optn = document.createElement("OPTION");
            optn.text = "AutoQLb";
            optn.value = 1;
            $(pkgBotCh2Id).options.add(optn);
            
            noAutoFlag = false;
        
        }else if(($(pkgTopCh1Id).selectedIndex <= 3) && !(($(pkgTopCh1Id).selectedIndex == 0))){
            pkgTopCh2Array = pkgTopCh2Default.split(",");
            pkgBotCh1Array = pkgBotCh1Default.split(",");
            pkgBotCh2Array = pkgBotCh2Default.split(",");
        }else{
            pkgTopCh2Array = pkgTopCh2High.split(",");
            pkgBotCh1Array = pkgBotCh1High.split(",");
            pkgBotCh2Array = pkgBotCh2High.split(",");
        }
       
       
       if(noAutoFlag){
            //now change the dropdown values depending on the selection
             //handle pkgTopCh2
             for(var i=0; i<pkgTopCh2Array.length; i++){
                var optn = document.createElement("OPTION");
                optn.text = pkgTopCh2Array[i];
                optn.value = i + 1;
                $(pkgTopCh2Id).options.add(optn);
            }
            
            //handle pkgBotCh1
             for(var i=0; i<pkgBotCh1Array.length; i++){
                var optn = document.createElement("OPTION");
                optn.text = pkgBotCh1Array[i];
                optn.value = i + 1;
                $(pkgBotCh1Id).options.add(optn);
            }
            
            //handle pkgBotCh2
             for(var i=0; i<pkgBotCh2Array.length; i++){
                var optn = document.createElement("OPTION");
                optn.text = pkgBotCh2Array[i];
                optn.value = i + 1;
                $(pkgBotCh2Id).options.add(optn);
            }
        }
    }

}

function handleFreqSwitchingChanges(fswId, fSyncId, fadjId, clkOutId){
    if($("hdnIC").value == "ADP1828"){
         $(clkOutId).options.length = 0;
         var clkoutArray;
         
        if($(fswId).selectedIndex == 3){
            //if fysn is selected enable fsync, disable everything else
            $(fSyncId).disabled = false;
            $(fadjId).disabled = true; 
            
            //change the value of clock out to match the average of fsync
             clkoutArray = clkoutFsync.split(",");
             var optn = document.createElement("OPTION");
             optn.text = clkoutArray[$(fSyncId).selectedIndex];
             optn.value = 1;
             $(clkOutId).options.add(optn);
                        
        }else if($(fswId).selectedIndex == 4){
            //if adjust is selected enable adjust
             $(fadjId).disabled = false;
             $(fSyncId).disabled = true;
             
             //change the value of clock out to match adjust
             clkoutArray = clkoutFadj.split(",");
             var optn = document.createElement("OPTION");
             optn.text = clkoutArray[$(fadjId).selectedIndex];
             optn.value = 1;
             $(clkOutId).options.add(optn);
             
             var optn = document.createElement("OPTION");
             optn.text = (clkoutArray[$(fadjId).selectedIndex] * 2);
             optn.value = 2;
             $(clkOutId).options.add(optn);
            
        }else if($(fswId).selectedIndex == 0){
        
             $(fadjId).disabled = true;
             $(fSyncId).disabled = true;
            //set clkout to default values
            clkoutArray = clkoutDefault.split(",");
            
             for(var i=0; i<clkoutArray.length; i++){
                var optn = document.createElement("OPTION");
                optn.text = clkoutArray[i];
                optn.value = i;
                $(clkOutId).options.add(optn);
            }
        }else if($(fswId).selectedIndex == 1){
                $(fadjId).disabled = true;
                $(fSyncId).disabled = true;
                //set clkout to default values
                clkoutArray = clkoutFromLow.split(",");
                
                 for(var i=0; i<clkoutArray.length; i++){
                    var optn = document.createElement("OPTION");
                    optn.text = clkoutArray[i];
                    optn.value = i + 1;
                    $(clkOutId).options.add(optn);
                }  
        }else if($(fswId).selectedIndex == 2){
                    $(fadjId).disabled = true;
                    $(fSyncId).disabled = true;
                    //set clkout to default values
                    clkoutArray = clkoutFromHigh.split(",");
                    
                     for(var i=0; i<clkoutArray.length; i++){
                        var optn = document.createElement("OPTION");
                        optn.text = clkoutArray[i];
                        optn.value = i + 1;
                        $(clkOutId).options.add(optn);
                    }
        }
        
    }else{
        if($(fswId).selectedIndex == 3){
            $(fSyncId).disabled = false;
        }else{
             $(fSyncId).disabled = true;
        }
    }

}

function handleFsyncUpdate(fsyncId, clkoutId){
    
    if($("hdnIC").value == "ADP1828"){
        $(clkoutId).options.length = 0;
        var clkoutArray = clkoutFsync.split(",");
        
        var optn = document.createElement("OPTION");
        optn.text = clkoutArray[$(fsyncId).selectedIndex];
        optn.value = 1;
        $(clkoutId).options.add(optn);
    }
}


function handleFadjUpdate(fadjId, clkoutId){
     if($("hdnIC").value == "ADP1828"){
         $(clkoutId).options.length = 0;
        var clkoutArray = clkoutFadj.split(",");  
        
        var optn = document.createElement("OPTION");
        optn.text = clkoutArray[$(fadjId).selectedIndex];
        optn.value = 1;
        $(clkoutId).options.add(optn);
        
          
        var optn = document.createElement("OPTION");
        optn.text = (clkoutArray[$(fadjId).selectedIndex] * 2);
        optn.value = 2;
        $(clkoutId).options.add(optn);
     }
}


//constrain object 
function constraint(name, min, max){
    this.name = name;
    this.min = min;
    this.max = max;
    
    return true;
}

//placeholder for adding the contraints on client side
function setContraints(){
//logic: check if single channel
//true: set constraint from single channel array
//false: set constraint from dual channel array
    if("false" == $("hdnDualChannel").value){
        //load single channel ranges
        for(var i=0; i<singleChannelConstraints.length; i++){
            var html = singleChannelConstraints[i].min + " <u><</u> x <u><</u> " + singleChannelConstraints[i].max;
            $(singleChannelConstraints[i].name).innerHTML = html;
        }
    }else{
        for(var i=0; i<dualChannelConstraints.length; i++){
            var html = dualChannelConstraints[i].min + " <u><</u> x <u><</u> " + dualChannelConstraints[i].max;
            $(dualChannelConstraints[i].name).innerHTML = html;
        }
    }

}


function showBothChannelTotal(){
    //hide the total row for first channel
    $("gvBOMnews").rows[$("gvBOMnews").rows.length -1].style.display = "none";
    
    //replace the total for second channel with both channel totals
    var totalArray = bothChannelTotal.split(",");
    var bomRow = $("gvBOMnews_ch2").rows[$("gvBOMnews_ch2").rows.length -1];
    
    //last value is the v4show, so stop 2 short of length
    for(var i=0; i<totalArray.length -1; i++){
        //if totalArray has a vlue replace the row value with the new one
        if(totalArray[i] != ""){
            bomRow.cells[i].text = totalArray[i]; 
        }
    }
}
//CD: 9.4.1 change - icData is the js objetc to hold the download link and solution exisits flag
function icData(downloadUrl, solutionExists){
    this.downloadUrl = downloadUrl;
    this.solutionExists = solutionExists;
}
//CD: 9.4.1 change - this will determine whether to do a postback or show the div
function downloadTool(){
   var selectedIndex = $("ddlIC").selectedIndex;
   if(selectedIndex > 0){
    var selectedValue = $("ddlIC").options[selectedIndex].value.replace("*", "");
    if(icArray[selectedValue].solutionExists){
         //$("downloadToolDiv").style.display = "none";
        setTimeout('__doPostBack(\'ddlIC\',\'\')', 0);
    }else{
    	//9.4.1 change
        //$("downloadToolDiv").style.display = "";
        openDownloadLink();
    }
   }
}
//CD: 9.4.1 change - opens a new window with the provided link
function openDownloadLink(){
     var selectedIndex = $("ddlIC").selectedIndex;
     var selectedValue = $("ddlIC").options[selectedIndex].value.replace("*", "");
     var link = icArray[selectedValue].downloadUrl;
     window.open(link);
}
//CD: 9.4.1 change
function selectorGuideDownloadLink(ic){
     var link = icArray[ic].downloadUrl;
      window.open(link);
}
