﻿
// ใช้สำหรับเปิด Page ที่สร้างไว้ในรูปแบบของ Modal Dialog
// Example Code
// <input id="AddFileButton" type="button" value="Add File" onclick="OpenDialog('FileUpload.aspx', 'popup', 500, 300); return false;"/>
function OpenDialog(url, name, w, h) {
    // Fudge factors for window decoration space.
    // In my tests these work well on all platforms & browsers.
    w += 32;
    h += 96;
    wleft = (screen.width - w) / 2;
    wtop = (screen.height - h) / 2;
    // IE5 and other old browsers might allow a window that is
    // partially offscreen or wider than the screen. Fix that.
    // (Newer browsers fix this for us, but let's be thorough.)
    if (wleft < 0) {
        w = screen.width;
        wleft = 0;
    }
    if (wtop < 0) {
        h = screen.height;
        wtop = 0;
    }
    var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, modal=yes' +
    'status=no, toolbar=no, scrollbars=no, resizable=no', 'tite=no', 'resizable=no', 'directories=no', 'status=no');
    // Just in case width and height are ignored
    win.resizeTo(w, h);
    // Just in case left and top are ignored
    win.moveTo(wleft, wtop);
    win.focus();
}

// ใช้สำหรับปิด Page ปัจจุบัน
function CloseDialog() {
    windows.close();
}

// Get QueryString
function GetQueryString(queryStringName) {
    strUrl = window.location.search.substring(1);
    arrayQueryString = strUrl.split("&");
    for (i = 0; i < arrayQueryString.length; i++) {
        valueQueryString = arrayQueryString[i].split("=");
        if (valueQueryString[0] == queryStringName) {
            return valueQueryString[1];
        }
    }
}

// function = ใช้หา Control ในหน้าเดียวกัน 
// ทำการส่ง ServerID เข้ามาจะทำการหาว่าเป็น ID อะไร จาก ClientID
// มีประโยชน์ในกรณีที่มีการ link ไว้หลาย Event โดยไม่รู้ว่า เป็น Control ตัวไหน
function GetControlID(controlID) {
    //ทำการ Split ClientID ด้วย _ 
    var mySplitResult = controlID.split("_");
    // หา Index สุดท้ายที่ทำการ Split ไว้
    var lastIndex = mySplitResult.length - 1;
    // จะได้ Server ID ของ TextBox ตัวที่ส่งเข้ามา
    return mySplitResult[lastIndex];
}

//ใช้หาค่าของ DropDownList ที่ทำการ Select
function GetValueOfDropDownListSelected(dropdownID) {
    var dropdownSelected = document.getElementById(dropdownID); // select element
    if (dropdownSelected)
        return dropdownSelected.options[dropdownSelected.selectedIndex].value;
}

//ยืนยัน
function confirmationItem(ButtomID, strMessage) {
    var answer = confirm(strMessage)
    if (answer) {
        // ถ้ายืนยัน จะทำการเรียก Event Button Click ของ ปุ่ม Delete ที่เป็น Server Side
        __doPostBack(ButtomID, '');
    }
}

//ทำการ แสดง Dialog ในการ Confirm จาก User ว่าต้องการให้ PostBack Event 
//ของ Control หรือไม่ ( สำหรับ GridView มีการตรวจสอบ ว่าต้องถูก Select อย่างน้อย 1 ด้วย )
function confirmationItemGridView(GridViewClientName, ButtomID, strMessage) {
    var count = GridViewClientName.GetSelectedRowCount() > 0;
    if (!count) {
        alert("กรุณาเลือกรายการที่ต้องการ");
        return;
    }

    var answer = confirm(strMessage)
    if (answer) {
        // ถ้ายืนยัน จะทำการเรียก Event Button Click ของ ปุ่ม Delete ที่เป็น Server Side
        __doPostBack(ButtomID, '');
    }
}

//Calendar
function getDay(ddlDay, ddlMonth, ddlYear) {
    var ddlMonth = document.getElementById(ddlMonth);
    var ddlYear = document.getElementById(ddlYear);
    var MonthValue = ddlMonth.options[ddlMonth.selectedIndex].value;
    var YearValue = ddlYear.options[ddlYear.selectedIndex].value;
    YearValue = YearValue - 543;
    if (((YearValue % 4) == 0) && ((YearValue % 100) != 0) || ((YearValue % 400) == 0)) {
        switch (MonthValue) {
            case "1":
            case "3":
            case "5":
            case "7":
            case "8":
            case "10":
            case "12": CreateDay(ddlDay, 31); break;
            case "4":
            case "6":
            case "9":
            case "11": CreateDay(ddlDay, 30); break;
            case "2": CreateDay(ddlDay, 29); break;
        }
    }
    else {
        switch (MonthValue) {
            case "1":
            case "3":
            case "5":
            case "7":
            case "8":
            case "10":
            case "12": CreateDay(ddlDay, 31); break;
            case "4":
            case "6":
            case "9":
            case "11": CreateDay(ddlDay, 30); break;
            case "2": CreateDay(ddlDay, 28); break;
        }
    }
}
function CreateDay(ddlDay, DayOfMonth) {
    var ddlDay = document.getElementById(ddlDay);
    for (var i = ddlDay.options.length - 1; i >= 0; i--) {
        ddlDay.remove(i);
    }
    for (var i = 1; i <= DayOfMonth; i++) {
        var option = document.createElement('option');
        option.text = i.toString();
        option.value = i.toString();
        try {
            ddlDay.add(option, null); // standards compliant
        }
        catch (ex) {
            ddlDay.add(option); // IE only
        }
    }
}
//End Calendar


function OpenBannerTracking(BannerId) {
    window.open('../BannerTracking.aspx?bid=' + BannerId);
}

function FindMember(labelMailTo, HiddenFieldMemberID, HiddenFieldEmail) {
    window.open('../Dialog/FindMember.aspx?labelMailTo=' + labelMailTo + '&HiddenFieldMemberID=' + HiddenFieldMemberID + '&HiddenFieldEmail=' + HiddenFieldEmail, 'FindMember', 'width=880,height=500,scrollbars=yes');
}
function FindMemberCompany(labelMailTo, HiddenFieldEmail, uid) {
    window.open('../Dialog/FindMemberCompany.aspx?labelMailTo=' + labelMailTo + '&HiddenFieldEmail=' + HiddenFieldEmail + '&uid=' + uid, 'FindMemberCompany', 'width=880,height=500,scrollbars=yes');
}