﻿function GetRadWindow() {
    var oWindow = null;
    if (window.radWindow)
        oWindow = window.radWindow;
    else if (window.frameElement.radWindow)
        oWindow = window.frameElement.radWindow;
    return oWindow;
}

/********************************************************************************/
//rounded corners
/********************************************************************************/

$(document).ready(function () {
    $(".roundedImg").load(function () {
        /*$(this).wrap(function () {
            return '<div class="' + $(this).attr('class') + '" style="background-image:url(' + $(this).attr('src') + ');background-repeat:no-repeat; width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px;" />';
        });
        $(this).css("opacity", "0");*/
        $(this).wrap(function () {
        return '<div class="' + $(this).attr('class') + '" style="width: ' + $(this).width() + 'px; height: ' + $(this).height() + 'px;" />';
        });
    });

});


/********************************************************************************/
//admin
/********************************************************************************/


/* default content */
function ManageSiteDefaultContent(siteId, sectionAreaId) {
    var oManager = GetRadWindowManager();
    var url = "/Admin/ManageSiteDefaultContent.aspx?siteId=" + siteId + "&sectionAreaId=" + sectionAreaId;
    var oWnd = oManager.open(url);
    oWnd.add_close(SectionEditorClose);
    oWnd.maximize();
}
/********************************************************************************/
//end admin 
/********************************************************************************/


/*section editor */
function EditSection(sectionId, pageId, sectionArea, sortId) {

    var oManager = GetRadWindowManager();
    var url = "/Admin/ManageSiteSection.aspx?sectionId=" + sectionId + "&sectionArea=" + sectionArea + "&sortId=" + sortId;
    var oWnd = oManager.open(url);
    oWnd.add_close(SectionEditorClose);
    oWnd.maximize();
}
function SectionEditorClose(sender, args) {
    window.location = window.location.href;
}
function AddSection(sectionId, pageId, sectionArea, sortId) {
    var oManager = GetRadWindowManager();
    var url = "/Admin/ManageSiteSection.aspx?sectionId=0&pageId=" + pageId + "&sectionArea=" + sectionArea + "&sortId=" + sortId;
    url = url.replace("Pages/", "Pages\\/");
    var oWnd = oManager.open(url);
    oWnd.add_close(SectionEditorClose);
    oWnd.maximize();
    return false;
}
function DeleteSection(sectionId, pageId) {

    VoilaTech.Web.SiteWebService.SiteSectionDelete(sectionId, DeleteSuccess, DeleteFail);
}
function DeleteSuccess(responseJSON) {

    window.location = window.location;
}
function DeleteFail(arg) {
    alert("DeleteFail");
}


function RedirectToUrl(url) {
    window.location = url;
}
function CloseRadWin(sender, args) {
    GetRadWindow().Close();
}
//printing support
function makepage(src) {
    // We break the closing script tag in half to prevent
    // the HTML parser from seeing it as a part of
    // the *main* page.
    return "<html>\n" +
                    "<head>\n" +
                    "<tit" + "le>Temporary Printing Window</tit" + "le>\n" +
                    "<script>\n" +
                    "function step1() {\n" +
                    "  setTimeout('step2()', 10);\n" +
                    "}\n" +
                    "function step2() {\n" +
                    "  window.print();\n" +
                    "  window.close();\n" +
                    "}\n" +
                    "</scr" + "ipt>\n" +
                    "</head>\n" +
                    "<body onLoad='step1()'>\n" +
                    "<img src='" + src + "'/>\n" +
                    "</body>\n" +
                    "</html>\n";
}

function printImage(evt) {
    src = evt;
    link = location.protocol + '//' + location.host + '/blank.htm';

    var pw = window.open(link, "_new");
    pw.document.open();
    pw.document.write(makepage(src));
    pw.document.close();
}


function ValidateEmailForm(btn) {
    $("#aspnetForm").validate();
    var valid = $("#aspnetForm").valid();
    if (valid == true) {
        var frm = btn.form;
        var inputValues = '';
        var mailTo = "";
        var myInputArray = new Array();
        var myInputArray2 = new Array();
        var hidInputArray = new Array();
        var formName = new Array();
        myInputArray = frm.getElementsByTagName("INPUT");
        myInputArray2 = frm.getElementsByTagName("TEXTAREA");

        var idStart = "em_";
        var elems = new Array();

        for (var i = 0; i < myInputArray.length; i++) {

            var id = myInputArray[i].name;

            if ((id.length >= idStart.length) && (id.substring(0, idStart.length) == idStart)) {
                var strs = id.split('_');
                if (myInputArray[i].type == 'checkbox') {
                    elems[strs[1]] = strs[2] + "|" + myInputArray[i].checked;
                }
                else if (myInputArray[i].type == 'radio') {
                    elems[strs[1]] = strs[2] + "|" + myInputArray[i].checked;
                }
                else if (myInputArray[i].type == 'text') {
                    elems[strs[1]] = strs[2] + "|" + myInputArray[i].value;
                }
                else if (myInputArray[i].type == 'hidden') {
                    elems[strs[1]] = strs[2] + "|" + myInputArray[i].value;
                    if (strs[2] == "MailTo") {
                        mailTo = myInputArray[i].value;
                    }

                    else if (strs[2] == "Success") {
                        formName[1] = myInputArray[i].value; ;
                    }
                    else if (strs[2] == "FormName") {
                        formName[0] = myInputArray[i].value; ;
                    }

                }


                // we have a winner!
                // do something neato with myInputArray[i]
            }

        }
        for (var j = 0; j < myInputArray2.length; j++) {

            var id = myInputArray2[j].name;

            if ((id.length >= idStart.length) && (id.substring(0, idStart.length) == idStart)) {
                var strs = id.split('_');
                elems[strs[1]] = strs[2] + "|" + myInputArray2[j].value;
                // we have a winner!
                // do something neato with myInputArray[i]
            }

        }


        var debugStr = '';
        for (var x = 0; x < elems.length; x++) {
            debugStr += elems[x] + "\r\n"
        }


        VoilaTech.Web.SiteWebService.FormSendEmail(elems, mailTo, FormSendSuccess, FormSendFail, formName);
        return false;
    }

}
function FormSendSuccess(result, userContext, methodName) {

    var elem = $get(userContext[0]);

    elem.innerHTML = "<strong>" + userContext[1] + "</strong><br /><br /><br /><br /><br /><br /><br />";




}
function FormSendFail(arg) {


}
