﻿//For browser detection
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

// JScript File
var DisplaySwap = "no";
var DisplayOptions = "no";
var DisplayEffects = "no";
var objElement;
window.onload = function()
{
    if(document.getElementById('KeyCode') != null)
    {
        if(document.getElementById('KeyCode').value != "")
        {
                document.getElementById('divList').style.display = "block";
                document.getElementById('divList').style.position = "absolute";
                //To set the top and left positions of drop down
                document.getElementById('divList').style.top = getTop(document.getElementById('txbMedication')) + document.getElementById('txbMedication').offsetHeight + "px";
                document.getElementById('divList').style.left = getLeft(document.getElementById('txbMedication')) + "px" ;
                document.getElementById('KeyCode').value = "";
        }
    }
    if(document.getElementById('EffectKeyCode') != null)
    {
        if(document.getElementById('EffectKeyCode').value != "")
        {
            document.getElementById('divEffects').style.display = "block";
            document.getElementById('divEffects').style.position = "absolute";
            //        document.getElementById('divList').style.top = document.getElementById('ContentPage_txbMedication').offsetTop + 205;
            document.getElementById('divEffects').style.top = getTop(document.getElementById('txbSearch')) + 25 + "px";
            //        document.getElementById('divList').style.left = document.getElementById('ContentPage_txbMedication').offsetLeft + 122;
            document.getElementById('divEffects').style.left = getLeft(document.getElementById('txbSearch')) + "px";
            document.getElementById('EffectKeyCode').value = "";

        }
    }
}
//To select the text in auto complete text box, on clicking the text box.

function SelectEffect()
{
   document.getElementById('txbSearch').select();
}
function GetEffects(evt)
{
    var key = (window.Event) ? evt.which : evt.keyCode;
    if(document.getElementById('txbSearch').value != "")
    {        
        if(key == "40" && document.getElementById('lstEffects') != null)
        {
        document.getElementById('lstEffects').selectedIndex = document.getElementById('lstEffects').selectedIndex +1;
        }
        else if(key == "38" && document.getElementById('lstEffects') != null)
        {
        document.getElementById('lstEffects').selectedIndex = document.getElementById('lstEffects').selectedIndex -1;    
        }
        else if(key == "13")
        {
//        document.getElementById('txbSearch').value = document.getElementById('lstEffects').value;
            SetEffect()
        }
        else
        {
        document.getElementById('EffectKeyCode').value = key;
        document.getElementById('divEffects').style.display = "block";
        document.getElementById('divEffects').style.position = "absolute";
        //        document.getElementById('divList').style.top = document.getElementById('ContentPage_txbMedication').offsetTop + 205;
        document.getElementById('divEffects').style.top = getTop(document.getElementById('txbSearch')) + 20 + "px";
        //        document.getElementById('divList').style.left = document.getElementById('ContentPage_txbMedication').offsetLeft + 122;
        document.getElementById('divEffects').style.left = getLeft(document.getElementById('txbSearch'))  + "px";
        document.getElementById('btnGetEffects').click();
        } 
    }
    else
    {
        document.getElementById('divEffects').style.display = "none";
    } 
}
function SetEffect()
{
    if(document.getElementById('lstEffects') != null)
    {
    document.getElementById('txbSearch').value = document.getElementById('lstEffects').value;
    document.getElementById('divEffects').style.display = "none";
    document.getElementById('btnAddEffect').focus();
    document.getElementById('btnAddEffect').click();
    }
}  
var changeClass = false;
function Hover(oElement)
{
    if(oElement.className == "normal_tab")
    {
       
        oElement.className = "active_tab";
        changeClass = true;
    }
    else
    {
        changeClass = false; 
    }
}

function MouseOut(oElement)
{
    if(changeClass)
    {
    oElement.className = "normal_tab";
    } 
}

//To select the text in auto complete text box, on clicking the text box.
function SelectValue()
{
    if(document.getElementById('txbMedication')!= null)
    {
        document.getElementById('txbMedication').select();
    }
    else
    {
         document.getElementById('ctl00_ContentPage_txbMedication').select();
    }
}
//Calling server side function to dropdown the list of
//medications while user types in medication name.
//This is fired on key up event of the auto complete text box.
function GetList(evt)
{
    var key = (window.Event) ? evt.which : evt.keyCode;
    if(document.getElementById('txbMedication')!= null)
    {
        if(document.getElementById('txbMedication').value != "")
        {
        
            //to navigate to next record while hitting down arrow key
            if(key == "40" && document.getElementById('lstMedications') != null)
            {
                document.getElementById('lstMedications').selectedIndex = document.getElementById('lstMedications').selectedIndex +1;
                document.getElementById('txbMedication').value = document.getElementById('lstMedications').value;

            }
            //to navigate to previous record while hitting up arrow key
            else if(key == "38" && document.getElementById('lstMedications') != null)
            {
                document.getElementById('lstMedications').selectedIndex = document.getElementById('lstMedications').selectedIndex -1;    
                document.getElementById('txbMedication').value = document.getElementById('lstMedications').value;

            }
            //To show medication list when user types in medication to the autocomplete textbox.
            else
            {
            //setting the position and visibility of the dropdown list.
            document.getElementById('KeyCode').value = key;
            document.getElementById('divList').style.display = "block";
            document.getElementById('divList').style.position = "absolute";
            //To set the top and left positions of drop down
            document.getElementById('divList').style.top = getTop(document.getElementById('txbMedication')) + document.getElementById('txbMedication').offsetHeight + "px";
            document.getElementById('divList').style.left = getLeft(document.getElementById('txbMedication')) + "px" ;
            document.getElementById('btnGetList').click();
            } 
        } 
        //hiding drodown list when text box is cleared.
        else
        {
            document.getElementById('divList').style.display = "none";
        }
    }
    else
    {
        if(document.getElementById('ctl00_ContentPage_txbMedication').value != "")
        {
            //to navigate to next record while hitting down arrow key
            if(key == "40" && document.getElementById('ctl00_ContentPage_lstMedications') != null)
            {
                document.getElementById('ctl00_ContentPage_lstMedications').selectedIndex = document.getElementById('ctl00_ContentPage_lstMedications').selectedIndex +1;
                document.getElementById('ctl00_ContentPage_txbMedication').value = document.getElementById('ctl00_ContentPage_lstMedications').value;
            }
            //to navigate to previous record while hitting up arrow key
            else if(key == "38" && document.getElementById('ctl00_ContentPage_lstMedications') != null)
            {
                document.getElementById('ctl00_ContentPage_lstMedications').selectedIndex = document.getElementById('ctl00_ContentPage_lstMedications').selectedIndex -1;    
                document.getElementById('txbMedication').value = document.getElementById('lstMedications').value;
            }
            //To show medication list when user types in medication to the autocomplete textbox.
            else
            {
            //setting the position and visibility of the dropdown list.
            document.getElementById('divList').style.display = "block";
            document.getElementById('divList').style.position = "absolute";
            //To set the top and left positions of drop down
            document.getElementById('divList').style.top = getTop(document.getElementById('ctl00_ContentPage_txbMedication')) + document.getElementById('ctl00_ContentPage_txbMedication').offsetHeight + "px";
            document.getElementById('divList').style.left = getLeft(document.getElementById('ctl00_ContentPage_txbMedication')) + "px" ;
            document.getElementById('ctl00_ContentPage_btnGetList').click();
            } 
        } 
        //hiding drodown list when text box is cleared.
        else
        {
            document.getElementById('divList').style.display = "none";
        }
    }
}
function SetOnEnter(evt)
{
    var key = (window.Event) ? evt.which : evt.keyCode;
    if(document.getElementById('txbMedication')!= null)
    {
                //Add medication while hitting enter key after selecting a medication
            if(key == "13")
            {
                evt.returnValue=false;
                evt.cancel = true;
                if(document.getElementById('lstMedications') != null)
                {
                    document.getElementById('txbMedication').value = document.getElementById('lstMedications').value;
                    document.getElementById('divList').style.display = "none";
                    document.getElementById('btnAdd').focus();
                    //when enter key pressed firefox will not work if click() is fired. 
                    // focus() is enough for updating graph and med..
                    if(BrowserDetect.browser  != "Firefox")
                    {
                    document.getElementById('btnAdd').click(); 
                    }
                } 
            } 
    }
    else
    {
        if(document.getElementById('ctl00_ContentPage_txbMedication') != null)
        {
            //Add medication while hitting enter key after selecting a medication
            if(key == "13")
            {
                SetValue();
            } 
        }
    }
}
function SetEffectOnEnter(evt)
{    var key = (window.Event) ? evt.which : evt.keyCode;
    if(document.getElementById('txbSearch').value != "")
    {  
        if(key == "13")
        {
            SetEffect();
        }
    }
}
//Set selected medication and calls the server side method that adds the 
//medication to medlist.
function SetValue()
{ 
    if(document.getElementById('lstMedications') != null)
    {
    document.getElementById('txbMedication').value = document.getElementById('lstMedications').value;
    document.getElementById('divList').style.display = "none";
    document.getElementById('btnAdd').focus();
    document.getElementById('btnAdd').click(); 
    } 
    else  
    {
        if(document.getElementById('ctl00_ContentPage_lstMedications') != null)
        {      
            document.getElementById('ctl00_ContentPage_txbMedication').value = document.getElementById('ctl00_ContentPage_lstMedications').value;
            document.getElementById('divList').style.display = "none";
            document.getElementById('ctl00_ContentPage_btnSearch').focus();
            document.getElementById('ctl00_ContentPage_btnSearch').click();
        }
    }
}   
// functions to get the left and top positions
// of the dropdown list.
function getLeft(oTextBox) 
{
    var oNode = oTextBox;
    var iLeft = 0;
    while(oNode.tagName != "BODY" && oNode.tagName != "HTML") {
      iLeft += oNode.offsetLeft;
      oNode = oNode.offsetParent;
      if(oNode.offsetParent == null)
      {
        break;
      }
    }
    return iLeft; 
};
function getTop(oTextBox) 
{
    var oNode = oTextBox;
    var iTop = 0;
    while(oNode.tagName != "BODY" && oNode.tagName != "HTML") {
        iTop += oNode.offsetTop;
        oNode = oNode.offsetParent;   
        if(oNode.offsetParent == null)
        {
        break;
        }
    }
    return iTop;
};
function ShowOptions(oElement)
{
    DisplayOptions = "yes";
    // The options div should be shown only for the last medication 
    // that has been pointed by mouse. So the last element pointed 
    //is stored as objElement.
    objElement = oElement;
    var objElement_Id = oElement.id;
    var timeout = window.setTimeout("ShowOptions1('" +  objElement_Id + "')", 200);
}
function ShowOptions1(Element_Id)
{
    // Checking whether display is set to 'yes' and the current element id mactches 
    // the id of the element last pointed by mouse.
    if(DisplayOptions == "yes" && Element_Id == objElement.id)
    {
        var oElement = document.getElementById(Element_Id); 
        if(oElement != null)
        {
            document.getElementById('divOptions').style.display = "block";
            document.getElementById('divOptions').style.position = "absolute";
            //To set the top and left positions of drop down
            var top = getTop(oElement);
            document.getElementById('divOptions').style.top = top -10 + "px";
            document.getElementById('divOptions').style.left = getLeft(oElement) + oElement.offsetWidth + 5 + "px" ;
            var selectedMed = oElement.getAttribute("medication");
            document.getElementById('MedSelected').value = selectedMed;
            return false;
        }
    }
}
function HideOptions(oElement)
{
    DisplayOptions = "no";
    var timeout = window.setTimeout("HideOptions1()", 2500);
} 
function HideOptions1()
{
    if(DisplayOptions == "no")
    {
        document.getElementById('divOptions').style.display = "none";
        document.getElementById('divSwapList').style.display = "none";
    }
}
function ShowEffectOptions(oElement)
{    
    DisplayEffects = "yes";
    // The options div should be shown only for the last medication 
    // that has been pointed by mouse. So the last element pointed 
    //is stored as objElement.
    objElement = oElement;
    var objElement_Id = oElement.id;
    var timeout = window.setTimeout("ShowEffectOptions1('" +  objElement_Id + "')", 200);
}
function ShowEffectOptions1(Element_Id)
{
    // Checking whether display is set to 'yes' and the current element id mactches 
    // the id of the element last pointed by mouse.
    if(DisplayEffects == "yes" && Element_Id == objElement.id)
    {
        var oElement = document.getElementById(Element_Id); 
        if(oElement != null)
        {
            document.getElementById('divEffectOptions').style.display = "block";
            document.getElementById('divEffectOptions').style.position = "absolute";
            //To set the top and left positions of drop down
            document.getElementById('divEffectOptions').style.top = getTop(oElement) + 1 + "px";
            document.getElementById('divEffectOptions').style.left = getLeft(oElement)+  oElement.offsetWidth + 2 + "px";
            var selectedMed = oElement.getAttribute("effect");
            document.getElementById('EffectSelected').value = selectedMed;
            return false;
        }
    }
}
function HideEffectOptions(oElement)
{
    DisplayEffects = "no";
    var timeout = window.setTimeout("HideEffectOptions1()", 2500);
} 
function HideEffectOptions1()
{
    if(DisplayEffects == "no")
    {
        document.getElementById('divEffectOptions').style.display = "none";
    }
}
function ShowMe(oElement)
{
    DisplayOptions = "yes";
   oElement.style.display = "block";
} 
function HideMe(oElement)
{
    DisplayOptions = "no";
    var timeout = window.setTimeout("HideMe1()", 2000);
} 
function HideMe1(oElement)
{
    if(DisplayOptions == "no")
    {
        document.getElementById('divOptions').style.display = "none";
        document.getElementById('divSwapList').style.display = "none";
    }
}
function ShowSwapList()
{
DisplaySwap = "yes";
document.getElementById('divSwap').style.display = "block";
}
function ShowCustomSearch()
{
document.getElementById('pnlCustomSearch').style.display = "block";
}
function HideCustomSearch()
{
document.getElementById('pnlCustomSearch').style.display = "none";
}
function ShowSwapList(oElement)
{
    DisplaySwap = "yes";
    var imgElement = document.getElementById('divOptions');
    document.getElementById('divSwapList').style.display = "block";
    document.getElementById('divSwapList').style.position = "absolute";
    document.getElementById('divOptions').style.display = "block";
    //To set the top and left positions of drop down
    document.getElementById('divSwapList').style.top = getTop(imgElement) + 1 + "px";
    document.getElementById('divSwapList').style.left = getLeft(imgElement)+ 107 + "px";
    //document.getElementById('btnSwap').click();
    return false;
}
function HideSwapList()
{
    DisplaySwap = "no";
    var timeout = window.setTimeout("HideSwapList1()", 2000);
}
function HideSwapList1()
{
    if(DisplaySwap == "no")
    {
    document.getElementById('divSwapList').style.display = "none";
    }
}
function ShowSwap(oElement)
{
    DisplaySwap = "yes";
    DisplayOptions = "yes";
    document.getElementById('divSwapList').style.display = "block";
    document.getElementById('divOptions').style.display = "block";
}
function HideSwap(oElement)
{
DisplaySwap = "no";
DisplayOptions = "no";
var timeout = window.setTimeout("HideSwap1()", 2000);
}
function HideSwap1(oElement)
{
    if(DisplaySwap == "no" && DisplayOptions == "no")
    {
        document.getElementById('divSwapList').style.display = "none";
        document.getElementById('divOptions').style.display = "none";
    }
}

//function DeactivateSwapBtn()
//{
//   document.getElementById('btnSwap').style.color = "black";
//}

function HoverSub(oElement)
{
    if(oElement.className == "normal_navlist")
    {    
        oElement.className = "active_navlist";
        changeClass = true;
    }
    else
    {
        changeClass = false; 
    }
}
function MouseOutSub(oElement)
{
    if(changeClass)
    {
        oElement.className = "normal_navlist";
    } 
}
function Activate(oElement)
{
oElement.style.color = "white";
}
function Deactivate(oElement)
{
oElement.style.color = "black";
}
//function ActivateMed(oElement)
//{
//    if(oElement.className == "ShowOnGraph")
//    {    
//    oElement.children[0].style.color = "white";
//    oElement.style.textDecoration='none';
////    oElement.className = "ActiveMed";
//    }
//} 
//function DeactivateMed(oElement)
//{
//    if(oElement.className == "ShowOnGraph")
//    {
////        oElement.className = "ShownOnGraph";
//    var children = GetChildren(oElement);
//    oElement.children[0].style.color = "blue";
//    oElement.style.textDecoration='underline';
//    }
//}
function ActivateMed(oElement)
{
    if(oElement.className == "ShowOnGraph")
    {    
    var children = oElement.childNodes;
    children[0].style.color = "#15188B";
    oElement.style.textDecoration='none';
    oElement.style.fontWeight='bold';
    }
} 
function DeactivateMed(oElement)
{
    if(oElement.className == "ShowOnGraph")
    {
    var children = oElement.childNodes;
    children[0].style.color = "#15188B";
    oElement.style.textDecoration='underline';
    oElement.style.fontWeight='normal';
    }
}
function ContinueExploring(Path)
{
    window.location.href = Path;
}
function Swap(oElement)
{
///For getting the brandname from the key 'brandname'onto the hidden field (SwappedValue)
document.getElementById('SwappedValue').value = oElement.getAttribute("brandname");
///Event which needs to be fired. The btnSwapWith_Click event is fired 
///automatically when this method Swap() is executed.
document.getElementById('btnSwapWith').click();
}
function AddListEffect(oElement)
{
///For getting the effect from the key 'effect'onto the hidden field (SwappedValue)
///document.getElementById('SwappedValue').value = oElement.getAttribute("effect");
///Event which needs to be fired. The btnSwapWith_Click event is fired 
///automatically when this method Swap() is executed.
document.getElementById('txbSearch').value = oElement.getAttribute("effect");
document.getElementById('btnAddEffect').focus();
document.getElementById('btnAddEffect').click();
}