﻿
// Custom validation function using AJAX to check existing user values
// Uses the httphandler UserValidationHandler.cs
var chrono;
var chrono2;


function closePopup() 
{
    $find('PopupExtend').hide();

}
function showPopup() {
    $find('PopupExtend').show();

}


function DoSearchPostbackOnTimeOut()
{
    clearTimeout(chrono);
   
    chrono = setTimeout('__doPostBack(\'\', \'\')', 800);
 
   
   }
   function SetCursorToTextEnd(textControlID)
   {
       var text = document.getElementById(textControlID);
       if (text != null && text.value.length > 0) 
       {
           if (text.createTextRange)
           {
               var FieldRange = text.createTextRange();
               FieldRange.moveEnd('character', text.value.length);
               FieldRange.collapse();
               FieldRange.select();
           }
       } 
    }

    function reloadimage() {

        setTimeout(document.images['BusyImage'].src = '../Images/illo/loaders/ajax-loader.gif', 10000);
       
    }



function CustomCreateUserValidator(source, args) {
    var SourceId = source.id;
    var ValidationType = "";
    if (SourceId.indexOf("cvUserName") > 0) {
        ValidationType = "valUserName";
    }
    else if (SourceId.indexOf("cvEmail") > 0) {
        ValidationType = "valEmail";
    }
    else if (SourceId.indexOf("cvCAPTCHA") > 0) {
        ValidationType = "valCAPTCHA";
    }
    if (ValidationType != "") {
        var rnd = Math.random() * 1000000;
        if (window.XMLHttpRequest) { // Mozilla, Safari, IE7...
            request = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) { // IE6 and older
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url = "UserValidation.aspx?" + ValidationType + "=" + args.Value + "&R=" + rnd;
        request.open("GET", url, false);
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                if (request.status == 200) {
                    if (request.responseText == "true") {
                        args.IsValid = true;
                    }
                    else {
                        args.IsValid = false;
                    }
                }
            }
        }
        request.send(null);
    }
}
function CustomDataSourceValidator(source, args) {
    var SourceId = source.id;
    var ValidationType = "";
    if (SourceId.indexOf("cvDataSourceName") > 0) {
        ValidationType = "valDataSorceName";
    }
    else if (SourceId.indexOf("cvDSName") > 0) {
        ValidationType = "valDataSorceName";
    }
    if (ValidationType != "") {
        var rnd = Math.random() * 1000000;
        if (window.XMLHttpRequest) { // Mozilla, Safari, IE7...
            request = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) { // IE6 and older
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url = "DataSourceValidator.aspx?" + ValidationType + "=" + args.Value + "&R=" + rnd;
        request.open("GET", url, false);
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                if (request.status == 200) {
                    if (request.responseText == "true") {
                        args.IsValid = true;
                    }
                    else {
                        args.IsValid = false;
                    }
                }
            }
        }
        request.send(null);
    }
}
function CustomShareReceiverValidator(source, args) {
    var SourceId = source.id;
    var ValidationType = "";
    if (SourceId.indexOf("cvValidateReceiver") > 0) {
        ValidationType = "ShareName";
    }
    if (ValidationType != "") {
        var rnd = Math.random() * 1000000;
        if (window.XMLHttpRequest) { // Mozilla, Safari, IE7...
            request = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) { // IE6 and older
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url = "ShareReceiverValidator.aspx?" + ValidationType + "=" + args.Value + "&R=" + rnd;
        request.open("GET", url, false);
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                if (request.status == 200) {
                    if (request.responseText == "true") {
                        args.IsValid = true;
                    }
                    else {
                        args.IsValid = false;
                    }
                }
            }
        }
        request.send(null);
    }
}
// When no analysis is selected the title is "TARGIT NET"
// When an analysis i selected the title contains a '/' seperating cube and filename and "TARGIT NET"
function CustomIsAnalysisSelectedValidator(source, args) {
    var iFrame = document.getElementsByTagName("iframe")[0];
    if (iFrame.contentWindow.document.title.indexOf('/') > 0) {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }
    return args;
}
function CommentHandler2(CommentId, Action) {
    if (Action != "") {
        var rnd = Math.random() * 1000000;
        if (window.XMLHttpRequest) { // Mozilla, Safari, IE7...
            request = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) { // IE6 and older
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url = "CommentHandler.aspx?act=" + Action + "&CommentId=" + CommentId + "&R=" + rnd;
        request.open("GET", url, true);
       
        request.send(null);
    }
}

function CountDown(Seconds) {
    var ActivationLabel = document.getElementById("ActivationCountdown");
    if (Seconds > 0) {
        ActivationLabel.innerText = Seconds + " seconds";
        Seconds = Seconds - 1;
        setTimeout('CountDown(' + Seconds + ')', 1000);
    }
    else {

        var TheForm = document.forms['aspnetForm'];
        //alert(TheForm.value);
        //TheForm.submit();
        window.location = "Search.aspx"

    }
}
// Variables used to specify menu movement, speed and position
//var menuLeft = '50'
//var menuStart = '0';
//var menuMovement = '42';
var menuSpeed = '3';
var menuStep = 15;
var moving = null;
var movement = menuStep;
// The X and Y for the menu item needs to be set before this will work
function ToggleMenu(ControlToMove, ControlToResize, BulletListId, MenuStart) {
    if (ControlToMove != null) {
        if (moving != null) {
            return;
        }
        ControlToResize = document.getElementById(ControlToResize);
        if (parseInt(ControlToMove.style.top.substring(0, (ControlToMove.style.top.length - 2))) == MenuStart) {
            ControlToResize.style.top = ControlToMove.offsetTop;  //ControlToMove.offsetTop;
            ControlToResize.style.left = ControlToMove.offsetLeft; //ControlToMove.offsetLeft;
            ControlToResize.style.height = 0 + 'px';
            var Distance = document.getElementById(BulletListId).offsetHeight + parseInt(10);
            toggleMenuOut(ControlToMove, ControlToResize, Distance, MenuStart, ControlToMove.offsetLeft, ControlToMove.offsetTop);
        }
        else {
            toggleMenuIn(ControlToMove, ControlToResize, MenuStart, ControlToMove.offsetLeft, ControlToMove.offsetTop);
        }
    }
}


// The menu function
function toggleMenuOut(ControlToMove, ControlToResize, Distance, MenuStart, FromPositionX, FromPositionY) {
    var menuBottomMax = parseInt(MenuStart) + parseInt(Distance);
    var menuPos = parseInt(ControlToMove.style.top.substring(0, (ControlToMove.style.top.length - 2)));
    //    alert(menuPos+','+menuLeftMax+','+menuStart+','+ menuMovement);
    if (menuPos < menuBottomMax) {
        if (menuPos + menuStep < menuBottomMax) {
            movement = menuStep;
        } else {
            movement = menuBottomMax - menuPos;
        }
        ControlToMove.style.top = parseInt(ControlToMove.style.top) + movement + 'px';
        ControlToResize.style.height = ControlToMove.style.top;
        clearTimeout(moving);
        moving = setTimeout(function() { toggleMenuOut(ControlToMove, ControlToResize, Distance, MenuStart, FromPositionX, FromPositionY) }, menuSpeed);
    } else {
        clearTimeout(moving);
        moving = null
        ControlToMove.style.backgroundImage = "url('../Images/AnalyzeMenuOpened.png')";
    }
}
function toggleMenuIn(ControlToMove, ControlToResize, MenuStart, FromPositionX, FromPositionY) {
    var menuTopMax = parseInt(MenuStart);
    var menuPos = parseInt(ControlToMove.style.top.substring(0, (ControlToMove.style.top.length - 2)));
    if (menuPos > menuTopMax) {
        if (menuPos - menuStep > menuTopMax) {
            movement = -menuStep;
        } else {
            movement = menuTopMax - menuPos;
        }
        ControlToMove.style.top = parseInt(ControlToMove.style.top) + movement + 'px';
        ControlToResize.style.height = ControlToMove.style.top;
        clearTimeout(moving);
        moving = setTimeout(function() { toggleMenuIn(ControlToMove, ControlToResize, MenuStart, FromPositionX, FromPositionY) }, menuSpeed);
    } else {
        clearTimeout(moving);
        moving = null
        ControlToMove.style.backgroundImage = "url('../Images/AnalyzeMenuClosed.png')";
    }
}
function ResizePDKIframe(DivId) {




}
function getwindowheight() {
    var windowHeight = 0;
    if (typeof (window.innerHeight) == 'number') {
        windowHeight = window.innerHeight;
    } else {
        if (document.documentElement && document.documentElement.clientHeight) {
            windowHeight = document.documentElement.clientHeight;
        }
        else {
            if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}

function getWindowWidth() {
    var windowWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        windowWidth = window.innerWidth;
    }
    else {
        if (document.documentElement && document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        }
        else {
            if (document.body && document.body.clientWidth) {
                windowWidth = document.body.clientWidth;
            }
        }
    }
    return windowWidth;
}

function ShowCreateDataSource(ObjectId) {
    var CreateNewDataSourceTable = document.getElementById(ObjectId);
    if (CreateNewDataSourceTable.style.display == 'none') {
        CreateNewDataSourceTable.style.display = 'block';
        CenterObject(CreateNewDataSourceTable, 140);
    }
    else {
        CreateNewDataSourceTable.style.display = 'none';
    }
}
function CenterObject(Object, TopPos) {
    Object.style.top = TopPos + "px";
    Object.style.left = (document.documentElement.clientWidth / 2) - (Object.offsetWidth / 2);
}
function ToggleElement(ElementId) {
    var Element = document.getElementById(ElementId);
    if (Element.style.display == 'none') {
        Element.style.display = 'block';
    } else {
        Element.style.display = 'none';
    }
}
function ToggleBackgroundColor(RowId) {
    var ElementToColor = document.getElementById(RowId);
    if (ElementToColor.style.backgroundColor.toUpperCase() == "#D5D5D5") {
        ElementToColor.style.backgroundColor = "";
        ElementToColor.style.color = "#000000";
    }
    else {
        ElementToColor.style.backgroundColor = "#D5D5D5";
        ElementToColor.style.color = "#AF0E23";
    }
}
function UpdateAnalysis(LastTitle) {
//    var IFrame = document.getElementsByTagName("iframe")[0];
//    if (LastTitle != IFrame.contentWindow.document.title) {
//        LastTitle = IFrame.contentWindow.document.title;     
//    }
//    else {
//    }
    setTimeout('UpdateAnalysis(\'' + LastTitle + '\')', 2000);
}

function CommentFocus(element) {
    if (element.value == element.defaultValue) {
        element.value = '';

        document.getElementById('AddCommentBtn').style.visibility = 'visible';
    }
}
function CommentBlur(element) {
    if (element.value == '') {
        element.value = element.defaultValue;

        document.getElementById('AddCommentBtn').style.visibility = 'hidden';
    }
}

function PulseToggleComment(textElement, buttonElement, showHide) {
    if (showHide) {
        textElement.value = '';
        document.getElementById(buttonElement).style.display = 'inline';
    }
    else {
        setTimeout(
            function() {
                if (textElement != null) {
                    textElement.value = textElement.defaultValue;
                }
                var btnElement = document.getElementById(buttonElement);
                if (btnElement != null) {
                    btnElement.style.display = 'none';
                }
            }
                , 100);
    }
}

//function testFunc(elementName) {
//    var theElement = document.getElementById(elementName);
//    theElement.style.display = 'none';
//}


// Script added serverside to avoid failure if clientid changes
//function UpdateCommentArea() {
//    __doPostBack("ctl00$cpAnalyze$Button2", "");
//}
function ShowEditControls(DivId) {
    var element = document.getElementById(DivId);
    if (element.style.display == 'none') {
        element.style.display = 'block';
    }
}
   
      //each code can achieve the hidden function:    
          //style.visibility = "hidden"       
           //style.display = "none"        
           function textClick() {            
           //$find("hmeSupplyDiv2")._hoverBehavior._hoverElement.style.visibility = "hidden";            
           $find("hmeSupplyDiv2")._hoverBehavior._hoverElement.style.display = "none";        }        
           function textMouseOver() {           
            //$find("hmeSupplyDiv2")._hoverBehavior._hoverElement.style.visibility = "visible";            
            $find("hmeSupplyDiv2")._hoverBehavior._hoverElement.style.display = "";        }
            function textMouseOut()
            {            
            //$find("hmeSupplyDiv2")._hoverBehavior._hoverElement.style.visibility = "hidden";
                $find("SearchItemHolder")._hoverBehavior._hoverElement.style.display = "none";
            }

            function classchange(newclassname, IDitemtochange) 
            {
                document.getElementById(IDitemtochange).className = newclassname;
            }
            
            
            function UpdateImg(ctrl, imgsrc) {
                var img = document.getElementById(ctrl);
                img.src = imgsrc;
            }

