﻿function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); }

function guidforFlash() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4()).toUpperCase()

}

function AjaxFailed(result) {
    alert(result.status + ' ' + result.statusText);
}


function checkFileExtension(elem, validExt, CVFileUploadHdn, strAlert)
{
    var filePath = elem.value;

    if (filePath.indexOf('.') == -1) {
        document.getElementById(CVFileUploadHdn).value = "a";
        alert(strAlert);
        return false;
    }
    var validExtensions = validExt.split(",");
    var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();

    for (var i = 0; i < validExtensions.length; i++)
    {
        if (ext == validExtensions[i]) {
            document.getElementById(CVFileUploadHdn).value = "1";
            return true;
        }
    }
    document.getElementById(CVFileUploadHdn).value = "a";
    alert(strAlert);
    return false;
}

function InsertImageFromFileManager(url, EditorClientId, text) {
    //alert();
    
    //alert(text);
    var el = document.getElementById(EditorClientId);
    //alert(el.parentElement.id)
    var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
    if (ie7) {
        var editor = $find(el.parentElement.id);
    }
    else {
        var editor = $find(el.parentNode.id);
    }
    try {
        // For 'Undo'
        editor._editPanel._modePanels[0]._saveContent();
        //value="avi;bmp;doc;docx;flv;gif;jpg;mp3;pdf;png;ppt;pptx;rar;swf;txt;xls;xlsx;zip" />
        // What to do - insert image at current selection if it is an image file if not insert href
        //---------------------------------------------------
        //alert(url.lastIndexOf(".jpg"));
        if (url.lastIndexOf(".swf") > -1) {
            var unicString = guidforFlash();
            var filename = url.substring(url.lastIndexOf('/') + 1);
            filename = filename.replace(".swf", "");
            var flashstring = "[Flash here]<!--[FlashStart]<div id=\"" + unicString + "\">Flash here</div><script src=\"/scripts/swfobject.js\" type=\"text/javascript\"></script><script type=\"text/javascript\"> var fo = new SWFObject('" + url + "', '" + filename + "', '720', '405', '9.0.28', '#000000'); fo.addParam('scale', 'noscale'); fo.addParam('wmode', 'transparent'); fo.addParam('quality', 'best'); fo.write('" + unicString + "'); </script>[FlashEnd]-->"
            editor._editPanel._modePanels[0].insertHTML(flashstring);
        }
        else 
        {
            if (url.lastIndexOf(".jpg") > -1 || url.lastIndexOf(".bmp") > -1 || url.lastIndexOf(".gif") > -1 || url.lastIndexOf(".png") > -1) {
                editor._editPanel._modePanels[0].insertHTML("<img src=\"" + url + "\" />");
            }
            else {
                var htmlToEditor = "&nbsp;&nbsp;<a href=\"" + url + "\">" + text + "</a>";
                editor._editPanel._modePanels[0].insertHTML(htmlToEditor);


            }
        }
        //---------------------------------------------------

        // Notify Editor about content changed and update toolbars linked to the edit panel
        //setTimeout(function() { editor._editPanel._modePanels[0].onContentChanged(); editor._editPanel._modePanels[0].updateToolbar(); }, 0);
        // Ensure focus in design panel
        editor._editPanel._modePanels[0].focusEditor();
        $find('ModalFileMnager_ModalPopupExtender').hide();
        return true;
    } catch (e) { alert(e.message) }

}

function InsertLinkFromCmsPageHierarchy(url, EditorClientId, text) {
    //alert(EditorClientId);
    var el = document.getElementById(EditorClientId);
    //alert(el.parentElement.id)
    //alert(EditorClientId);
    var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
    if (ie7) {
        var editor = $find(el.parentElement.id);
    }
    else {
        var editor = $find(el.parentNode.id);
    }
    
    try {
        // For 'Undo'
        editor._editPanel._modePanels[0]._saveContent();
        //alert(url);
        //alert(text);
        var htmlToEditor = "&nbsp;&nbsp;<a href=\"" + url + "\">" + text + "</a>";
        var htmlToEditorTemp = "<p>url=" + url + "text" + text + "</p>";
        //alert(htmlToEditorTemp);
        // What to do - insert image at current selection
        //---------------------------------------------------

        //editor._editPanel._modePanels[0].insertHTML(htmlToEditorTemp);

        editor._editPanel._modePanels[0]._setToEnd();
        editor._editPanel._modePanels[0].focusEditor();
        //editor._editPanel._modePanels[0]._saveContent();
        editor._editPanel._modePanels[0].insertHTML(htmlToEditor);
        //editor._execCommand("createLink", false, this._emptySrc);
        //---------------------------------------------------

        // Notify Editor about content changed and update toolbars linked to the edit panel
        //setTimeout(function() { editor._editPanel._modePanels[0].onContentChanged(); editor._editPanel._modePanels[0].updateToolbar(); }, 0);
        // Ensure focus in design panel
        editor._editPanel._modePanels[0].focusEditor();
        $find('ModalPagePicker_ModalPopupExtender').hide();
        return true;
    } catch (e) { alert(e.message) }

}

//Online Help
function OpenOnlineHelpWindow(url, role, languageId, windowTitle) {
    var onlineHelpUrl = url + "?role=" + role + "&languageId=" + languageId;
    var title = "";
    var features = "width=1000,height=680,resizable=no,scrollbars=yes";
    window.open(onlineHelpUrl,title,features);
}

//DefaultButton
function WebFormFireDefaultButton(event, target)
{
    var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
    if (ie7)
    {
        document.getElementById(target).focus();
        //alert(document.getElementById(target));
        document.all[target].click();
        event.returnValue=false;
        return;
    }
    else
    {
        //alert("alo");
        var pos=document.getElementById(target).href.indexOf("javascript:");
        var evt = document.createEvent('MouseEvents');
        evt.initMouseEvent('click', true, true, window, 0, 0, 
                               0, 0, 0, false, false, false, false, 0, null);
                                if (pos>=0)
                                {
                                    eval(unescape(document.getElementById(target).href));
                                }
                               
       document.getElementById(target).dispatchEvent(evt);
    } 

}
//Tooltip
function showToolTip(DivID)
{
    document.getElementById(DivID).style.display = "block";
}

function hideToolTip(DivID)
{
    document.getElementById(DivID).style.display = "none";
}

function changeDisplay(DivID)
{
    if (document.getElementById(DivID).style.display == "none")
    {
        document.getElementById(DivID).style.display = "block";
    }
    else
    {
        document.getElementById(DivID).style.display = "none";
    }
}
//Form
function FormFieldClear(txtName) {
    document.getElementById(txtName).value = "";
}

//Translation
function TranslationCopy(destination, sourceText)
{
    //document.forms[0].elements[destination].value = sourceText;
    document.getElementById(destination).value = sourceText;
}
function TranslationClear(txtName)
{
    //document.forms[0].elements[txtName].value = "";
    document.getElementById(txtName).value = "";	
}

function TranslationCopyHtmlEditor(destination, sourceText)
{
    var editor = $find(destination).get_contentArea();
    //editor._editPanel._modePanels[0]._copyCut('x', true);
    editor.innerHTML = sourceText;
    //editor._editPanel._modePanels[0].insertHTML(sourceText);
    //$find(destination).SetText(sourceText);
}
function TranslationClearHtmlEditor(txtName) {

    var editor = $find(txtName).get_contentArea();
    //var editor = $find(txtName);
    //editor._editPanel._modePanels[0]._copyCut('x', true);
    //editor._editPanel._modePanels[0]._editPanel.set_content("");
    editor.innerHTML = "";
}
function convertNumberCulture(number)
{
    var convertedNumber = number.toString().replace(",", ".");
    return convertedNumber;
}
//force UpOrderItemsCosts to update
    function LoadUpdatePanel(PanelId)
    {
        __doPostBack(PanelId, '');
    }

function SetCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue)
                 + ";expires=" + expire.toGMTString();
}

function SetFiltersOnLoad(moduleClass) {
    $(function() {
        $(moduleClass + " .FilterOptions").css("display", "block");
    });
}

function SetFilters(moduleClass) {
    // execute your scripts when the DOM is ready. this is a good habit
    $(function() {
        $(moduleClass + " .FiltersShowHide").click(function(event) {
            event.preventDefault();
            if ($(moduleClass + " .FilterOptions").css("display") == "none") {
                $(moduleClass + " .FilterOptions").show("slow");
                $(moduleClass + " .FilterOptions input:first").focus();
            }
            else
            { $(moduleClass + " .FilterOptions").hide("slow"); }
        });
    });
}

/* Admin */
function checkAdminSorting(turnOnText, turnOffText) {
    $(".adminCommandsDiv a.SortingLink").click(function() {
        if ($(".adminCommandsDiv .SortingLink .Sorting").length > 0) {
            $(".adminCommandsDiv .SortingLink").attr("class", "SortingLink");
            $(".adminCommandsDiv .SortingLink span").replaceWith(turnOnText);
            $(".DragMeColumn").hide("fast");
            $(".ReorderList").removeClass("SortingEnabled");
        }
        else {
            $(".adminCommandsDiv .SortingLink").attr("class", "SortingLink On");
            $(".adminCommandsDiv .SortingLink span").replaceWith(turnOffText);
            $(".DragMeColumn").show("fast");
            $(".ReorderList").addClass("SortingEnabled");
        }
    });
}

function CloneButtons(parentDivClass, divToCloneClass) {
//    $().ready(function() {
//        var countTr = $('.GvAdminListRow').length;
//        if (countTr > 8) {
//            $(parentDivClass).before($(divToCloneClass).clone());
//        }
    //    });

}
function CloneListButtons() {
    $().ready(function() {
        var module = $(".PnlContent .CMS .List");
        if (module != null) {
            $(".PnlContent .CMS .List").each(function() {
                var countTr = $(this).find("table .GvAdminListRow").length;
                if (countTr > 0) {
                    var clone = $(this).find("div.adminCommandsDiv").clone().attr('class', 'adminCommandsDiv cloned');
                    $(this).before(clone);

                    $(this).parent().find(".cloned").find("a").each(function() {
                        if ($(this).attr("id") != null) {
                            var aid = $(this).attr("id");
                            var ahref = $(this).attr("href");
                            $(this).attr("id", "");
                            if (ahref != null && ahref.toLowerCase().indexOf("javascript:__doPostBack") >= 0) {
                                $(this).attr("href", "#");
                                $(this).attr("onClick", ahref);
                            }
                        };
                    });
                }
                else {
                    countTr = 0;
                }
            });
        }
    });
}
