/*
dialog for displaying a form for displaying compliance info

requires the following in the client page:ItemDetail.jsp
 <script src="/js/scriptaculous/lib/prototype.js" type="text/javascript"></script>         
*/

var DisplayComplianceDialog = {
     compElement: null,
     compitemCode: null,
    
    
    /**
    shows the compliance dialog and is called by client
    */
    show: function(element, item)
        {
        this.compitemCode = item;
        this.compElement = $(element);
        $('ShowComplianceInfo').setStyle ( {display:'none'} );
        var myAjax = new Ajax.Request("/ajax?",
            {method: 'get', 
            parameters : { fctype:'adc.falcon.FormControllers.ProductDisplayFormCtrl', 
                          cmd:'getProductCompliance', 
                          itemCode:item },
            onComplete:  this.showDialog,
            onFailure: this.showFailure,
            onException: this.showException});    
        },
    /*
    position the dialog and make it visible.
    */            
    position: function ()    
        {
        //console.log ( 'positioning...' );
        var gap = 25;
        var cOffset = Element.cumulativeOffset(this.compElement);
        var leftPxPos = cOffset.left - 350 - gap;
        var topPxPos = cOffset.top - 125 ;
        $('ShowComplianceInfo').setStyle ( { left:leftPxPos+'px', top:topPxPos+'px' } );
       //console.log ( 'done positioning...' );
        },
         
    /*
    process response from item data request.
    */
    showDialog: function ( data )
        {
        var item = data.responseText.evalJSON();
        //item =  DisplayComplianceDialog.trim(item);
        DisplayComplianceDialog.position();
        DisplayComplianceDialog.load ( item );
       
        $('ShowComplianceInfo').setStyle ( { display:'block' } );
        }, 
   
   /**
   load the compliance data
   */     
   load : function(item)
        {
        $('comp_item_cd').update (  this.compitemCode );
        $('compliance_comments').update ( "" );
        if(item.hasCompInfo == "true")
            {
	        DisplayComplianceDialog.loadULCSA( "ul", "/images/icons/ul.gif", "UL Listed File Number:  ", item.ul_listed );
	        if(item.hasULDocLink == "true" && item.ul_note != null && item.ul_note != "")
	            {
	        	 var ulSplitNote = item.ul_note.split(",");
	        	 var ulSplitLink = item.ul_document_link.split(",");
	        	 	
	        	 var htmlstr = "";
		         for(i=0; i<ulSplitNote.length; i++)
		             {
		        	 	var ulurl = ulSplitLink[i];
		        	 	if(ulurl == null)
			        	 	{
		        	 		ulurl = ulSplitLink[0];	
			        	 	}
		        	 	htmlstr = htmlstr + '<table width="98%" border="0" cellspacing="0" cellpadding="0" valign="top" >';
			            htmlstr = htmlstr + '<tr>' +
			                    '<td width = "15%">&nbsp;</td>' +
			                    '<td width = "85%" > <A  style="color:blue;font-size:8pt;text-decoration:underline" ' + 
			                            'target="legacywin" href="' + ulurl +'">' +  ulSplitNote[i] +'</A></td>  ' +
			                    '<td width = "5%">&nbsp;</td>' +
			                '</tr> </table>';
		             }
	            $('ulnote').innerHTML = htmlstr;       
	            $('ulnote').setStyle ( { display:'block' } );
	            }
	        else if(item.ul_note != null && item.ul_note != "")
		        {
		         var htmlstr = '<table width="98%" border="0" cellspacing="0" cellpadding="0" valign="top" >' +
		         '<tr> <td width = "15%">&nbsp;</td>' +
		         	'<td width = "80%" style="font-size:8pt;padding:2px;">' + item.ul_note + '</td></tr></table>';
		       	$('ulnote').innerHTML = htmlstr;       
	            $('ulnote').setStyle ( { display:'block' } );
		        }
	        DisplayComplianceDialog.loadULCSA( "recognizedUL", "/images/icons/ur.jpg", "UL Recognized File Number:  ", item.ul_recognized );
	        DisplayComplianceDialog.loadULCSA( "hazardousUL", null, "UL Hazardous File Number:  ", item.ul_hazardous_locs);
	        if(item.hasUL == "true")
	            {
	            var htmlstr = '<table width="98%" border="0" cellspacing="0" cellpadding="0" valign="top" >';
	            htmlstr = htmlstr + '<tr>' +
	                    '<td width = "15%">&nbsp;</td>' +
	                    '<td nowrap width = "80%" style="font-size:9pt;"> <A  style="color:blue;font-size:9pt;text-decoration:underline" ' + 
	                            'target="legacywin" href="http://database.ul.com/cgi-bin/XYV/template/LISEXT/1FRAME/gfilenbr.html"> Go here to search UL file numbers </A></td>  ' +
	                    '<td width = "5%">&nbsp;</td>' +
	                '</tr> </table>';
	            $('ULLink').innerHTML = htmlstr;       
	            $('ULLink').setStyle ( { display:'block' } );
	            }
	        var ceLink = false;
	        if(item.hasCELink == "true") 
		        {
		        ceLink = true;
		        }
	        DisplayComplianceDialog.loadCE( "ce", "/images/icons/CE_logo.gif", "CE:  ", item.ce_link, ceLink );
            DisplayComplianceDialog.loadULCSA( "csa", "/images/icons/csa_ca.gif", "CSA File Number:  ", item.csa);
            if(item.csa != null && item.csa != "")
	            {
	            var htmlstr = '<table width="98%" border="0" cellspacing="0" cellpadding="0" valign="top" >';
	                htmlstr = htmlstr + '<tr>' +
	                        '<td width = "15%">&nbsp;</td>' +
	                        '<td nowrap width = "80%" style="font-size:9pt;"> <A  style="color:blue;font-size:9pt;text-decoration:underline" ' + 
	                                'target="legacywin" href="http://directories.csa-international.org/"> Go here to search CSA file numbers </A></td>  ' +
	                        '<td width = "5%">&nbsp;</td>' +
	                    '</tr> </table>';
	             $('csaLink').innerHTML = htmlstr;       
	             $('csaLink').setStyle ( { display:'block' } );
	            
	            }
	        DisplayComplianceDialog.loadRohs ( "/images/icons/rohs.gif", item.rohs, item.rohs_date, item.rohs_comments );    
            $('euDiv').setStyle ( { display:'block' } );     
            }
        else
            {  
            $('compliance_comments').update ( "No Data Available" );     
            $('compcomments').setStyle ( { display:'block' } );
            }
        },
     
    /**
    Load UL and CSA data   
    */   
    loadULCSA  : function(element, image, title, field)
        {
        if(field == null || field == "")
            {
            field = "None";
            }
         var ulSplitResult = field.split(",");
         
            var htmlstr =  '<table width="98%" border="0" cellspacing="0" cellpadding="0" valign="top" >' +
                '<tr> <td width = "15%">';
            if (image != null && image != "")
                {
                htmlstr = htmlstr + '<IMG SRC=' + image + ' align="left" VSPACE="0" HSPACE="0" BORDER="0" />';
                }
            else 
	            {
	            htmlstr = htmlstr + '&nbsp;';
	            }
            htmlstr = htmlstr + '</td>' +
             '<td nowrap width = "85%" style="color:#000000;font-size:9pt;font-weight:bold;" align="left">'+
            	'<div>' + title + '&nbsp;&nbsp;' +  ulSplitResult[0] + '</div>';
 
             htmlstr = htmlstr + '</td>' + 

             '</tr></table> ';
      
             for(i=1; i<ulSplitResult.length; i++)
             	{
            	 htmlstr =  htmlstr + '<table width="98%" border="0" cellspacing="0" cellpadding="0" valign="top" >';
            	 htmlstr = htmlstr + '<tr><td nowrap width = "56%">&nbsp;</td>' + 
            	 '<td><font  style="color:#000000;font-size:9pt;font-weight:bold;;padding:2px;border:1px; ">' + '&nbsp;&nbsp;' +ulSplitResult[i] + '</font></td></tr></table>';	 
             	}
            $(element).innerHTML = htmlstr;       
            $(element).setStyle ( { display:'block' } );
        },
        
     /**
     Load CE data
     */
     loadCE  : function(element, image, title, field, islink)
            {
            //debugger;
            var htmlstr =  '<table width="98%" border="0" cellspacing="0" cellpadding="0" valign="top" >' +
                '<tr> <td width = "15%">';
            if (image != null && image != "")
                {
                htmlstr = htmlstr + '<IMG SRC=' + image + ' align="left" VSPACE="0" HSPACE="0" BORDER="0" />';
                }
            else 
                {
                htmlstr = htmlstr + '&nbsp;';
                }
            htmlstr = htmlstr + '</td>' ;
            
           
            if(!islink)
                {
                if(field == null || field == "" )
	                {
	                field = "None";
                    htmlstr = htmlstr + 
                        '<td width = "80%" style="font-size:10pt;padding:2px;font-weight:bold;"> ' + field + '</td>  ' +
                        '<td width = "5%">&nbsp;</td>' +
                    '</tr>'; 
	                }
                else if(field.match(/\s*CE compliant\.*/g))
                     {
                      htmlstr = htmlstr + 
                        '<td width = "80%"  nowrap style="font-size:10pt;padding:2px;font-weight:bold;"> CE Compliant</td>  ' +
                        '<td width = "5%">&nbsp;</td>' +
                    '</tr>' +
   
                        '<tr> ' +
                            '<td width = "15%">&nbsp;</td>' +
                            '<td width = "80%"  nowrap style="font-size:10pt;padding:2px;font-weight:bold;"> <A " style="color:blue;font-size:9pt;text-decoration:underline" ' + 
                                'target="legacywin" href="/adc/Form/User/TechInquiry"> Contact us for more information</A> </td>  ' +
                            '<td width = "5%">&nbsp;</td>' +
                        '</tr>'; 

                     }
                else
	                {
	                htmlstr = htmlstr + 
	                    '<td width = "80%" style="font-size:10pt;padding:2px;font-weight:bold;"> ' + field + '</td>  ' +
	                    '<td width = "5%">&nbsp;</td>' +
	                '</tr>';  
	                }
                
                }
             else
                {
                 htmlstr = htmlstr + 
                    '<td width = "80%"  nowrap style="font-size:9pt;padding:2px;"> <A " style="color:blue;font-size:9pt;text-decoration:underline" ' + 
                            'target="legacywin" href="'+ field + '"> Go here to view CE declarations</A> </td>  ' +
                    '<td width = "5%">&nbsp;</td>' +
                '</tr>';  

                }
             htmlstr = htmlstr + '</table><BR>';
            $(element).innerHTML = htmlstr;
            $(element).setStyle ( { display:'block' } );

            },
        
      /**
      Load RoHS data     
      */
      loadRohs  : function(image,rohs,date,comment)
            {
            //debugger;
            var rohsStatus = "None";
            if(rohs == "2") 
	            {
	            rohsStatus = "EXEMPT";
	            }
            else if(rohs == "1") 
	            {
	            rohsStatus = "YES";
	            }
            var htmlstr =  '<table width="98%" border="0" cellspacing="0" cellpadding="0" valign="top" >' +
                '<tr> <td width = "15%">';
            if (image != null && image != "")
                {
                htmlstr = htmlstr + '<IMG SRC=' + image + ' align="left" VSPACE="0" HSPACE="0" BORDER="0" /></td>';
                }
            else 
                {
                htmlstr = htmlstr + '&nbsp;</td>';
                }
            
            if(rohs == "2") 
                {
                 htmlstr = htmlstr + 
                    '<td width = "80%"  nowrap style="font-size:9pt;padding:2px;font-weight:bold;"> RoHS Status:&nbsp;&nbsp; <A " style="color:blue;font-size:9pt;text-decoration:underline" ' + 
                            'target="legacywin" href="http://support.automationdirect.com/docs/rohs_weee.pdf"> ' +
                            'EXEMPT</A> </td>  ' +
                    '<td width = "5%">&nbsp;</td>' +
                '</tr>' ;
                }        
            else 
                {             
                htmlstr = htmlstr + 
                    '<td width = "80%" style="font-size:9pt;padding:2px;font-weight:bold;"> RoHS Status:&nbsp;&nbsp;'  +rohsStatus + '</td>' +
                    '<td width = "5%">&nbsp;</td>' +
                '</tr>' ;
                }
           
          
             if(date != null && date != "")
                 {
                 htmlstr = htmlstr + '<tr>' +
                    '<td width = "15%">&nbsp;</td>' +
                    '<td width = "80%" style="font-size:9pt;padding:2px;font-weight:bold;"> RoHS Date:&nbsp;&nbsp;  ' + date + '</td>' +
                    '<td width = "5%">&nbsp;</td>' +
                    '</tr>';
                  }
          
            htmlstr = htmlstr + '</table><BR>';

            $('rohsDiv').innerHTML = htmlstr;       
            $('rohsDiv').setStyle ( { display:'block' } );
            },
    
    /*
    Hide the dialog.
    */   
    hideDialog : function()
        {
        if ( $('ShowComplianceInfo') != null )
            {
            $('ShowComplianceInfo').setStyle ( { display:'none' } );
            }
        },
        
     /*
     Show an exception
     */   
     showException : function(resultObj)
        {
        Element.update ( $('ShowComplianceInfo'), 'Error loading product compliance info...' );
        } 

    };
