﻿

// Make each row highlighted on mouseover
function highlightGridview( gridviewId ) {
    $(document).ready(function(){
        $('#' + gridviewId +' tr').mouseover(function() { $(this).addClass("hover"); }).mouseout(function() { $(this).removeClass("hover"); });
    });   
}

// Show a session message
function showSessionMessage(message) {
//    alert(message);
    if(message != null && message != '') { 
        var lines = getOccurencesInString(message, '<br />');
        var height = 125 + (lines > 2 ? (lines - 2) * 20 : 0);
       
        $("body").append("<div id='session-message-placeholder' class='hidden'><div id='session-message'><p>" + message + "</p></div></div>");
        tb_show("", "#TB_inline?height=" + height + "&width=300&inlineId=session-message-placeholder");
    }
}

// Show an alert
function showAlertMessage(message) {
//    alert(message);
    if(message != null && message != '') {
        var lines = getOccurencesInString(message, '<br />');
        var height = 125 + (lines > 2 ? (lines - 2) * 20 : 0);
       
        $("body").append("<div id='alert-message-placeholder' class='hidden'><div id='session-message'><p>" + message + "</p></div></div>"); 
        tb_show("", "#TB_inline?height=" + height + "&width=300&inlineId=alert-message-placeholder");
    }
}

// Try to close this thickbox window
function closeThickbox() {
    try {
        self.parent.tb_remove();
    } catch(err) {
    }
}

// Open a thickbox
function openThickbox(TargetUrl, DesiredWidth, DesiredHeight, Caption, OpenModal) {
    // Default values
    if(Caption == null) { Caption = ''; }
    if(OpenModal == null || OpenModal == false) { OpenModal = 'false'; }
    if(OpenModal == true) { OpenModal = 'true'; }
    if(TargetUrl.indexOf('?') == -1 ) { TargetUrl = TargetUrl + '?' }
    
    tb_show(Caption, 
        TargetUrl + "&" + 
        "TB_iframe=true&" + 
        "KeepThis=true&" +
        "modal=" + OpenModal + "&" +
        "width=" + DesiredWidth + "&" + 
        "height=" + DesiredHeight
        );
}


// Open popup in new window
function openPopup(TargetUrl, DesiredWidth, DesiredHeight, HideMenu) {
    // Default values
    if(HideMenu == null) { HideMenu = 0; }
    
    // Center screen
    var top = (screen.height - DesiredHeight) / 2;
	var left = (screen.width - DesiredWidth) / 2;
	var menuValue = (HideMenu == 0 ? 'yes' : 'no');
	
	// Show popup
	var params = "top=" + top + ", " +
	    "left=" + left + ", " + 
	    "width=" + DesiredWidth + ", " +
	    "height=" + DesiredHeight + ", " + 
	    "menubar=no" + ", " +
	    "scrollbars=" + menuValue + ", " +
	    "statusbar=" + menuValue;
	var myPopup = window.open(TargetUrl, null, params);
	myPopup.focus();
}

// Close popup
function closePopup() { 
    if( self.opener == null) {
        closeThickbox()
    } else {
        try { 
            window.close(); 
        } catch(err) { 
        }
    }
}

// Change opener location
function changeOpenerLocation(TargetUrl, ConfirmWithUser, ConfirmationMessage)
{
    // Default values
    if(ConfirmationMessage == null || ConfirmationMessage == '') {
        ConfirmationMessage = 'En cliquant \'Oui\', cette fenêtre se fermera et affichera les informations dans le site;\r\nÊtes-vous sur de vouloir procéder?';
    }
    if(ConfirmWithUser == null) { ConfirmWithUser = true; }
    
    // Confirm if user want to change opener location
    var doRefresh = true;
	var myOpener = window.opener;
	if(myOpener != undefined){
	    if( DoConfirm ) { doRefresh = confirm(ConfirmationMessage) };
		if( doRefresh ) {
		    myOpener.location = TargetUrl;
		    myOpener.focus();
		}
	}
}

// Allows to call a page method in Javascript
function callPageMethod(methodName, onSuccess, onFail) {
    var args = '';
    var l = arguments.length;
    if (l > 3) {
        for (var i = 3; i < l - 1; i += 2) {
            if (args.length != 0) args += ',';
            args += '"' + arguments[i] + '":"' + arguments[i + 1] + '"';
        }
    }
    var loc = window.location.href;
    loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "default.aspx" : loc;
    $.ajax({
        type: "POST",
        url: loc + "/" + methodName,
        data: "{" + args + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onSuccess,
        fail: onFail
    });
}

function getOccurencesInString(Text, StringToSearch) {
    return Text.split(StringToSearch).length;
}

// fix Thickbox on AJAX requests
function FixThickbox() {
    $(document).ready(function() {
        try {
            var isAsyncPostback = Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();
            if (isAsyncPostback) { tb_init("a.thickbox, area.thickbox, input.thickbox"); }
        } catch (err) { }
    });
}
