var useragent = window.navigator.userAgent;

// Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)
// Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.38 Safari/532.21.10
// Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; FDM; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)
var firefox_reg = /(Firefox[\/](\d*[\.]\d*)*)/;
var isFireFox = firefox_reg.test(useragent);
var gecko_reg = /(Gecko[\/]\d*)/;
var isGecko = gecko_reg.test(useragent);
var mozilla_reg = /(Mozilla[\/]\d*\.\d)/;
var isMozilla = mozilla_reg.test(useragent);
var chrome_reg = /(Chrome[\/]\d*(\.\d*)*)/
var isChrome = chrome_reg.test(useragent);
var ie_reg =/(MSIE\s\d*\.\d*)/;
var isIE = ie_reg.test(useragent);
var safari_reg = /(Safari[\/]\d*(\.\d*)*)/;
var isSafari = safari_reg.test(useragent);


    var queryParams;
    function parseQueryString()
    {   
        var s=window.location.search;        
        if (s!='')
        {
            s=s.substring( 1 );
            var p=s.split('&');
            for (var i=0;i<p.length;i++)
            {
                var q=p[i].split('=');
                queryParams[q[0]]=q[1];
            }
        }
    }

      
    function getQueryParam(p)
    {
        if (!queryParams)
        {
            queryParams = [];
            parseQueryString();
        }
        if (queryParams[p])
            return queryParams[p];
        else 
            return '';
    }
    
    function getTimeStamp()
    {
         var d = new Date();
         var date = d.getDate()+','+d.getDay()+','+d.getFullYear()+','+d.getMonth()+','+d.getTime()+','+d.getMilliseconds();
         return date;  
    }
    function PostSearchURL(searchctrl)
    {
        //[438243-87-9]
        var searchctrlobj = document.getElementById(searchctrl);
        var criteria = ''
        if (/^select$/i.test(searchctrlobj.tagName)) {
            criteria = searchctrlobj.options[searchctrlobj.selectedIndex].value;
            //alert(criteria);
        }
        var searchfor = document.getElementById('searchfor'); 
        if(searchfor && searchfor.tagName.toLowerCase() == 'input')
        {
            var value = searchfor.value;
            if (!value) value = '';
            value = trim(value);
            var reg = /^\[\d*[-]*\]$/
            
            value = value.replace(/[\[]/, "");
            value = value.replace(/[\]]/, "");
            //var ops = [{ id: 'all', name: 'All' }, { id: 'jrhid', name: 'Search by Product ID' }, { id: 'name', name: 'Search by Name' }, { id: 'casno', name: 'Search by CAS No' }, { id: 'formula', name: 'Search by Molecular Formula'}];
    
            var params = 'complexString=' + escape(value);
            if(criteria != 'jrhid' && criteria != 'casno'  && criteria != 'formula' )
                document.location.href = 'ProductCatelogue.aspx?' + params + '&t=' + getTimeStamp();
            else
                document.location.href = 'Results.aspx?' + params + '&t=' + getTimeStamp();
        }
    }

    function trim(stringToTrim) {
        return stringToTrim.replace(/^\s+|\s+$/g, "");
    }
    function ltrim(stringToTrim) {
        return stringToTrim.replace(/^\s+/, "");
    }
    function rtrim(stringToTrim) {
        return stringToTrim.replace(/\s+$/, "");
    }

    function complexpostURL()
    {
        
     var value = document.getElementById('complex').value;
     var params = 'complexString='+value;
     document.location.href = 'ProductCatelogue.aspx?' + params + '&t=' + getTimeStamp();
      
    }
       
    
    function SetSearchFrameURL()
    {
        var name = getQueryParam('name');
        var smile = getQueryParam('smile');
        var jrhid = getQueryParam('jrhid');
        var casno = getQueryParam('casno');
        var formula = getQueryParam('formula');
        var complexString = getQueryParam('complexString');
        var showsubstructures = getQueryParam('showsubstructures');
        var searchStructuresView = document.getElementById('searchStructuresView');
        var searchfor = document.getElementById('searchfor');
        var search = searchfor.Value;
        searchStructuresView.style.display='none';
        var title = document.getElementById('searchTitle').innerHTML;
        var param = '';

        var isSmile = false;
        if(name != '')
        {
            title = ' Results of your search of ' +name;
            param = 'name='+name;
        }
        else if(jrhid != '') 
        { 
            title = ' Results of your search of ' +jrhid;
            param = 'jrhid='+jrhid;
        }
        else if(casno != '') 
        {
            title =' Results of Your Search of ' +casno;
            param = 'casno='+casno;
        }
        else if(formula != '')
        { 
            title =  ' Results of Your Search of ' +formula;
            param = 'formula='+formula;
        }
        else if(complexString != '') {
            title = ' Results of Your Search of ' +complexString;
            param = 'complexString='+complexString;
        }

        else if(smile != '') 
        {
            if(showsubstructures == '1')
                title = title + ' Results of your search for an exact structure of ';
            else
                title = title + ' of this structures'; 
            param = 'smile='+smile+'&showsubstructures='+showsubstructures;          
            searchStructuresView.style.display='block';
            isSmile = true;
            
            var jmestring = getQueryParam('jmestring'); 
            if(jmestring == ''){
                     
                var handle = new sack(); 
                handle.viewControl = title;
                handle.method ="GET";
                handle.onCompletion = SmileResults;
                var url = "DBServicesHandler.ashx?action=getCategory&smile="+smile;
                handle.runAJAX(url);
            }
            else{
                document.getElementById('searchTitle').innerHTML = 'Search results for an exact structure of <br/><b>'+unescape(smile)+'</b>';
                document.JME.readMolecule(jmestring);               
            }
        }
        else
        { 
            title = title + 'Products List';
        } 
        
        var url = 'Default.aspx?'+param;
        var handle = new sack();
        handle.method ="GET";
        handle.onCompletion = UpdateSearchResults;
        handle.runAJAX(url);
        //alert(url);
        if(isSmile == false)
            document.getElementById('searchTitle').innerHTML = title;
        document.getElementById('frmSearchResults').src = url;
        
    }
    
    function UpdateSearchResults(response){
       document.getElementById('divsearchResults').innerHTML= response;
    }
    
    function SmileResults(response,title)
    {
        var obj = eval('['+response+']');
        obj = obj[0]
        if(obj.name){
            document.getElementById('searchTitle').innerHTML = 'Search results for an exact structure of <br/><b>'+unescape(obj.name)+'</b>';
            document.JME.readMolecule(obj.jmestring);
        }
    }
    

    
    
     function ReadMoleFiles(){
        
        var handle = new sack();            
        handle.method ="GET";
        handle.onCompletion = completion;
        var url = "DBServicesHandler.ashx?action=getCategories";
        handle.runAJAX(url);
        
    }
    function ShowClassifiedProducts(e){
        var eVal = !isIE ? e.target : event.srcElement;
        var value = eVal.getAttribute('jmestring');
        var categoryname = eVal.getAttribute('categoryname');
        var d = new Date();
        var date = d.getDate()+','+d.getDay()+','+d.getFullYear()+','+d.getMonth()+','+d.getTime()+','+d.getMilliseconds();
        document.location.href = 'ProductCatelogue.aspx?jmestring=' + escape(value) + '&categoryname=' + categoryname + '&showsubstructures=1&t=' + date;
    }
    
   
    function completion(response){
        //alert(response);
        var obj = response;           
        var classifications = document.getElementById('tblclassifications');
        
        var row;
        for(var x in obj){
            
            var rowitem = parseInt(x)+1;
            if(rowitem == 1){
                row = document.createElement('tr');
                classifications.appendChild(row);
            }
            var cell = document.createElement('td');
            row.appendChild(cell);
            cell.width = '25%';
            cell.setAttribute('nowrap','nowrap');
            
            if(cell.addEventListener){
                cell.addEventListener('click',ShowClassifiedProducts,false);
            }
            else if(cell.attachEvent){ 
                cell.attachEvent("onclick", ShowClassifiedProducts);
            }
            else
                cell.onclick = ShowClassifiedProducts; 
            
            cell.style.cursor = 'pointer'; 
            var srcImage = null;
            
            
            if(isFireFox){
                /*srcImage = new Image();
                srcImage.src = "imgs/arrow.gif";
                srcImage.width = '11px';
                srcImage.height = '11px';
                srcImage.src = "imgs/arrow.gif";*/
				srcImage = document.createElement('IMG');
                srcImage.setAttribute('SRC', 'imgs/arrow.gif');
                srcImage.setAttribute('width', '11');
                srcImage.setAttribute('height', '11');
            }
            
            if(isIE){
                //srcImage = document.createElement('<IMG SRC="imgs/arrow.gif" width="11" height="11" />');
				srcImage = document.createElement('IMG');
				srcImage.setAttribute('SRC','imgs/arrow.gif');
				srcImage.setAttribute('width','11');
				srcImage.setAttribute('height','11');
			}
           
            if(srcImage != null) 
                cell.appendChild(srcImage);
            
            var text = document.createElement('div');    
            text.style.display ='inline';         
            text.style.marginLeft ='3px';        
            text.style.paddingLeft ='3px';     
            text.innerHTML = obj[x].name;
            text.setAttribute('jmestring',obj[x].jmestring);
            text.setAttribute('categoryname',obj[x].name);
            cell.appendChild(text);
            
            if((Math.floor(rowitem/4)*4 - rowitem == 0)){
                row = document.createElement('tr');
                classifications.appendChild(row);
            }
            
        }
        //alert(classifications.innerHTML);
    }
        
        
    
function doRedirect()
{
    window.location.href = 'OnlineChemicals.html';
}


function LoadSearchForm(tblId)
{
    //alert(tblId);
	//<table id='tblSearchForm' width="100%" border="0" align="center" cellpadding="0" cellspacing="5"></table>
    var searchForm = document.createElement('table');
	searchForm.id = tblId;
	searchForm.cellpadding = '5';
	searchForm.cellspacing = '5';
	searchForm.style.marginLeft = "20px";
	searchForm.style.paddingTop = '10px';
	//searchForm.style.border = "solid 2px red";
	
	//searchFrom.setAttribute("style","");
	searchForm.width = '276px';	
	searchForm.border = 1;
	
    
    var row = document.createElement('tr');
    searchForm.appendChild(row);
    var cell = document.createElement('td');
    cell.colSpan = 2;
    cell.width = '100%';
    
    row.appendChild(cell);
    var select = document.createElement('select');
    select.setAttribute('name', 'ddlSearch');
    select.setAttribute('id', 'ddlSearch');
    select.setAttribute('class', 'style2');
    
    var ops = [{ id: 'all', name: 'All' }, { id: 'jrhid', name: 'Search by Product ID' }, { id: 'name', name: 'Search by Name' }, { id: 'casno', name: 'Search by CAS No' }, { id: 'formula', name: 'Search by Molecular Formula'}];
    for (var x in ops) {
        var option = document.createElement('option');
        option.value = ops[x].id;
        option.text = ops[x].name;
        if (isIE)
            select.add(option);
        else
            select.appendChild(option);
    }
    cell.appendChild(select);
    
    row = document.createElement('tr');
    searchForm.appendChild(row);

    cell = document.createElement('td');
    cell.colSpan = 2;
    row.appendChild(cell);

    var searchButton = document.createElement('table');
    searchButton.cellpadding = '0';
    searchButton.cellspacing = '0';
    searchButton.border = '0';
    searchButton.width = '85%';
    searchButton.style.paddingTop = "5px";
    var row1 = document.createElement('tr');
    searchButton.appendChild(row1);
    cell1 = document.createElement('td');
    cell1.className = 'style2';
    row1.appendChild(cell1);
    
    var text = document.createElement('input');
    text.type = 'text';
    text.id = 'searchfor';
    text.setAttribute('name','searchfor');
    text.setAttribute('size','20');
    cell1.appendChild(text);
    
    cell1 = document.createElement('td');
    cell1.width = '20%';
    cell1.className = 'style2';
    
    cell1.align = 'left';
    row1.appendChild(cell1);

    
    
    text = document.createElement('input');
    text.type = 'button';
    text.id = 'Submit';
    text.value = 'Go';
    text.setAttribute('name','Submit');
    text.setAttribute('size','15');
    text.style.cursor = 'pointer';
    if(text.addEventListener){        
        text.addEventListener('click',PostSearchURL,false);
        text.setAttribute('onclick', 'PostSearchURL("ddlSearch");');
    }
    else {
        text.onclick = 'if(typeof(PostSearchURL) == "function") PostSearchURL("ddlSearch");';
    }
    cell1.appendChild(text);
    cell.appendChild(searchButton);
    
    
    row = document.createElement('tr');
    searchForm.appendChild(row);
    cell = document.createElement('td');
    cell.colSpan = 2;
    cell.vAlign = 'bottom';
    cell.className = 'style2';
    row.appendChild(cell);
    
    var searchBystructure = document.createElement('table');
    searchBystructure.width = '100%';
    searchBystructure.border = 0;
    searchBystructure.style.paddingTop = "10px";
    searchBystructure.cellPadding = 0;
    searchBystructure.cellSpacing = 3;
    searchBystructure.align = 'center';
    cell.appendChild(searchBystructure);
    
    var row2 = document.createElement('tr');
    searchBystructure.appendChild(row2);
    var cell2 = document.createElement('td');
    var caption = document.createElement('b');
    caption.innerHTML = 'Search by structure : '; 
    cell2.appendChild(caption);
    row2.appendChild(cell2);
    
     row2 = document.createElement('tr');
    searchBystructure.appendChild(row2);
    cell2 = document.createElement('td');
    row2.appendChild(cell2);
    var text2 = document.createElement('img');
    text2.src = 'search.gif';
    text2.id = 'img';
    text2.setAttribute('name','Submit2');
    text2.setAttribute('size','20');
    if(cell2.addEventListener){
        cell2.addEventListener('click',doRedirect,false);
        cell2.setAttribute('onclick','doRedirect();');
    }else  
    cell2.onclick = 'if(typeof(doRedirect) == "function") {doRedirect();}'; 
    cell2.style.cursor = 'pointer';
    
    cell2.appendChild(text2);


    if (document.getElementById('divSearchForm')) {
        document.getElementById('divSearchForm').innerHTML = searchForm.innerHTML;
        document.getElementById('divSearchForm').style.paddingLeft = "20px";
    }

    
}
            

function LoadEventsAndShows(tblId)
{
    //alert(tblId);
	//<table id='tblSearchForm' width="100%" border="0" align="center" cellpadding="0" cellspacing="5"></table>
    var EventsAndShows = document.createElement('table');
	EventsAndShows.id = tblId+'1';
	EventsAndShows.cellpadding = '1px';
	EventsAndShows.cellspacing = '1px';
	EventsAndShows.cellspacing = '1px';
	
	//searchFrom.setAttribute("style","");
	EventsAndShows.width ='100%';
	EventsAndShows.border = 0;
	
    var row = document.createElement('tr');
    EventsAndShows.appendChild(row);
    var cell = document.createElement('td');    
    row.appendChild(cell);
    cell.className = 'style16';
    cell.style.textAlign = 'center';
    cell.innerHTML = 'Upcoming Events and Shows ';
    
    
    row = document.createElement('tr');
    EventsAndShows.appendChild(row);
    cell = document.createElement('td');
    cell.width = '100%';
    cell.className = 'style2';
    cell.style.textAlign = 'center';
    row.appendChild(cell);
    cell.innerHTML = 'Visit Us at Booth  #1133';
   
    
    row = document.createElement('tr');
    EventsAndShows.appendChild(row);
    cell = document.createElement('td');
    cell.width = '100%';
    cell.className = 'style2';
    cell.style.textAlign = 'center';
    row.appendChild(cell);
    cell.innerHTML = '240th ACS National Exposition';
    
    
    row = document.createElement('tr');
    EventsAndShows.appendChild(row);
    cell = document.createElement('td');   
    cell.className = 'style2';
    cell.style.textAlign = 'center';
    row.appendChild(cell);
    cell.innerHTML = 'Boston, MA';
    
    row = document.createElement('tr');
    EventsAndShows.appendChild(row);
    cell = document.createElement('td');   
    cell.className = 'style2';
    cell.style.textAlign = 'center';
    row.appendChild(cell);
    cell.innerHTML = 'August 22nd-26th, 2010';
    
    
//    alert(EventsAndShows.outerHTML);
	if( document.getElementById(tblId))
		document.getElementById(tblId).innerHTML = EventsAndShows.outerHTML;
}
            

