// <copyright file="GeneralUtilities.js" company="BioCentury Publications Inc."> 
// All contents Copyright © 2005-2009 BioCentury Publications, Inc. ALL RIGHTS RESERVED
// 
// http://www.biocentury.com
// </copyright>

// Commented by Marius Paltanea - Never used - TO-DO


//-------------------------------------------------------------------------------
// ADD - Bela Gaspar - 06/09/2006
// Descr: Validation for a positive floating point number, up to 2 decimal points
//-------------------------------------------------------------------------------
//function isValidPositiveFloatingNumber(val)
//{
//    if (val==null) {return false;}
//    if (val.length==0) {return false;}
//    
//    var DecimalFound = false
//    for (var i = 0; i < val.length; i++)
//    {
//        var ch = val.charAt(i)
//        if (i == 0 && ch == "-") 
//        {
//			return false
//        }
//        if (ch == "." && !DecimalFound) 
//        {
//			DecimalFound = true
//			if (val.substring(i+1, val.length).length > 2)	        
//                return false
//        }
//        if ((ch < "0" || ch > "9") && ch != ".") 
//        {
//                return false
//        }
//    }
//    return true
//}

//function ShowHide(SubModuleID,visibilityState) {
//	var theModule = document.getElementById(SubModuleID);
//	if (theModule) {
//		theModule.className = (visibilityState) ? "DivVisible" : "DivHidden"; 
//	}
//} 



//function DoShow(theControl) {
//	var oControl = document.getElementById(theControl+"Values");
//	if (oControl) {
//		for (var i=0;i<oControl.length;i++) {		// Hide all input ranges associated with children Elements of this Control
//			ShowHide(oControl[i].id+"Div", false);
//		}
//		ShowHide(oControl[oControl.selectedIndex].id+"Div", true);	// Display the input ranges associated with the selected child Element of this Control
//	}
//} 




//function showHideDiv(divID,state)
//{
//	if (state)
//		document.getElementById(divID).className = 'DivVisible';
//	else
//		document.getElementById(divID).className = 'DivHidden';
//}

function loadPrintPreview(theID, appBasePath, mode, isPublic) {
    switch (mode) {
        case "comp":
            {
                var link = appBasePath + "Common/PopUpPage.aspx?ControlType=CompanyProfile&CompanyID=" + theID + "&mode=printPreview&isPublic=" + isPublic;
                printWindow = window.open(link, 'Print', 'status=yes,width=820px,resizable=yes,toolbar=yes,menubar=yes,scrollbars=yes');
                break;
            }
        case "mt":
            {
                var link = appBasePath + "Common/PopUpPage.aspx?ControlType=MolecularTargetProfile&TargetID=" + theID + "&mode=printPreview&isPublic=" + isPublic;
                printWindow = window.open(link, 'Print', 'status=yes,width=820px,resizable=yes,toolbar=yes,menubar=yes,scrollbars=yes');
                break;
            }
        case "prod":
            {
                var link = appBasePath + "Common/PopUpPage.aspx?ControlType=ProductProfile&ProductID=" + theID + "&mode=printPreview&isPublic=" + isPublic;
                printWindow = window.open(link, 'Print', 'status=yes,width=820px,resizable=yes,toolbar=yes,menubar=yes,scrollbars=yes');
                break;
            }
    }
    printWindow.focus();
}

function clickButton(buttonID) {
    if (document.getElementById(buttonID).dispatchEvent) {
        var e = document.createEvent("MouseEvents");
        e.initEvent("click", true, true);
        document.getElementById(buttonID).dispatchEvent(e);
    }
    else {
        document.getElementById(buttonID).click();
    }
}
var tipsWin = null;
// added by Marius
// used in email alert tips
// opens the popup
function openAlertTips(module) 
{
    if (tipsWin != null)
        tipsWin.close();

    tipsWin = window.open('../Common/Help/'+module+'.htm',
            'tipsPopUp', 'status=yes,resizable=yes,width=600,height=600,toolbar=no,menubar=no,scrollbars=yes');
    tipsWin.focus();
    return false;
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    var cookie = c_name + "=" + escape(value)
    if (expiredays != null)
        cookie += ";expires=" + exdate.toGMTString();
    document.cookie = cookie + ";path=/";
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return null;
}

function showHideCheck() {
    if (document.getElementById('chkNeverShow')) {
        if (getCookie('tipsCookie') == 'false') {
            document.getElementById('chkNeverShow').checked = false;
        }
        else {
            if (getCookie('tipsCookie') == null)
                setCookie('tipsCookie', 'true', 365);
            document.getElementById('chkNeverShow').checked = true;
        }
    }
}

//Get Event in all Browsers
//This function returns the event object.
//If the event type was a key press,up or down it adds 2 properties:
//evnt.key (charCode)
//and evnt.letter (literal letter).

//It works in IE, Firefox,Seamonkey and Opera 8
function GetEvent(e) {
    var ev = (window.event) ? window.event : e;
    if (!ev || !ev.type) return false;
    var ME = ev;

    if (ME.type.indexOf('key') != -1) {
        if (document.all || ME.type.indexOf('keypress') != -1) {
            ME.key = (ev.keyCode) ? ev.keyCode : ((ev.charCode) ? ev.charCode : ev.which);
        }
        else ME.key = ev.charCode;
        if (ME.key) ME.letter = String.fromCharCode(ME.key);
    }
    return ME;
}

function removeCriteria(criteriaListID, index) {
    document.getElementById(criteriaListID).remove(index);
}

function removeDiv(fromOpener) {
    try {
        if (fromOpener) {
            var openerWindow;
            if (window.dialogArguments)
                openerWindow = window.dialogArguments;
            else
                openerWindow = window.opener;

            if (openerWindow.divGridviewID) {
                div = openerWindow.window.document.getElementById(openerWindow.divGridviewID);
                divGeneralInfo = openerWindow.window.document.getElementById("DivElement");
                if (div != null) {
                    //            var loadDiv = openerWindow.window.document.createElement("div");
                    //            loadDiv.setAttribute("style", "background-color: #ffffff; display:none; width:200px; border:1px solid #dfdfdf");
                    //            var image = loadDiv.appendChild(openerWindow.window.document.createElement("img"));
                    //            image.setAttribute("src", "../Images/loading.gif");
                    //parentOfDiv = div.parentNode;

                    div.parentNode.removeChild(div);

                    if (divGeneralInfo != null)
                        divGeneralInfo.parentNode.removeChild(divGeneralInfo);

                } //if div!=null
            }

        }
        else {
            div = document.getElementById(divGridviewID);
            divGeneralInfo = document.getElementById("DivElement");
            if (div != null) {
                div.parentNode.removeChild(div);
                if (divGeneralInfo != null)
                    divGeneralInfo.parentNode.removeChild(divGeneralInfo);
            }
        }
    }
    catch (e)
    { }
}


//--------------------------------------------------------------------------
//used only in CompanyAnalyst for basic and custom view in order to remove the gridview div
//--------------------------------------------------------------------------
function removeGridViewDiv() {
    if (window.divGridviewID != "undefined" && window.divGridviewID != null) {
        div = document.getElementById(divGridviewID);
        if (div != null) {
            div.parentNode.removeChild(div);
        }
    }
}


// title - modal popup title
// text - text to ve shown
function ShowGenericModalPopup(title, text, modalPopup) {
    document.getElementById('pGenericModalPopupTitle').innerText = title;
    document.getElementById('divGenericModalPopup').innerText = text;
    $find(modalPopup).show();
}

// loads the companies select settings
function loadSelectSettings() {
    var strPreControlName = 'ctl00_cphMain_UserListsCriteriaControl_';

    var search = document.getElementById(strPreControlName + 'txtCompanyName');
    ddl = document.getElementById(companyListID);
    div = document.getElementById('divCompaniesDropdown');

    if (search.value != 'Company Name') {
        search.className = "enabled";

        div.style["width"] = "45px";

        if (document.all) {// IE
            ddl.setAttribute('disabled', 'true');

            search.style.width = "126px";
        }
        else {
            ddl.className = "disabledCompanies";
            ddl.setAttribute('onfocus', 'this.blur();');
            search.style.width = "126px";
        }
        ddl.style["width"] = "45px";
    }
    else {
        if (document.all) {// IE
            //ddl.setAttribute('disabled', 'true');
            ddl.style["width"] = "94px";
            div.style["width"] = "94px";
        }
        else {
            ddl.style["width"] = "94px";
            //ddl.className = "disabledCompanies";
            //ddl.setAttribute('onfocus', 'this.blur();');
        }
    }
}

//function restore() {
//    document.getElementById('div1').style["width"] = "150px";
//    ddl = document.getElementById('select1');
//    ddl.style["width"] = "150px";
//    if (document.all) {// IE
//        ddl.setAttribute('disabled', 'true');
//    }
//    else {
//        ddl.className = "test";
//        ddl.setAttribute('onfocus', 'this.blur();');
//    }
//    return false;
//}


// Added by Marius Paltanea 7/31/2009
// Opens the popup page where a guest or a user
// cand share a link with a colleague
var shareWithColleagueWin = null;
function openShareWithColleague(ID, type, appBasePath, notUser, redirectUrl, strSearchBy) {
    //    if (notUser) {
    //        document.location.href = redirectUrl;
    //        //alert(IsPublicCompanyProfile);
    //        return false;
    //    }
    if (type != 'prod') {
        shareWithColleagueWin = window.open(appBasePath + "Common/ShareWithColleague.aspx?ID=" + ID + "&type=" + type + "&strSearchBy=" + strSearchBy, "_blank", 'width=550,height=630,toolbar=0,resizable=0, scrollbar=1,menubar=0,location=0');
    }
    else {
        shareWithColleagueWin = window.open(appBasePath + "Common/ShareWithColleague.aspx?ID=" + ID + "&type=" + type + "&strSearchBy=" + strSearchBy + "&publicURL=" + document.location.href, "_blank", 'width=550,height=630,toolbar=0,resizable=0, scrollbar=1,menubar=0,location=0');
    }
    return false;
}

// Deletes all filter sessions for 
// a product
function DeleteFilterSessions(productID) {
    try {
        var httpRequest = GetXmlHttpObject();


        var xmlSOAP = '<?xml version="1.0" encoding="utf-8"?>' +
         '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
         'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> ' +
          '<soap:Body>' +
           '<ClearFilterSessions xmlns="http://www.biocentury.com/webservices/">' +
            '<productID>' + productID + '</productID>' +
           '</ClearFilterSessions>' +
          '</soap:Body>' +
         '</soap:Envelope>';



        // send XML document to Web server
        //httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

        httpRequest.onreadystatechange = new function() { };
        //alert(UtilitiesWSUrl);
        httpRequest.open("POST", UtilitiesWSUrl, false);

        httpRequest.setRequestHeader("SOAPAction", "http://www.biocentury.com/webservices/ClearFilterSessions");
        httpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        httpRequest.send(xmlSOAP);
    }
    catch (err) { }
}