﻿/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}
function SetParentCook(parentID) {
    $.cookies.set('parentMenu', parentID);
}
function Enter(value) {
        if (this.value == "") {return;}
        document.onkeydown = function(e) {
            e = (window.event) ? event : e;
            var k = e.keyCode;
            if (k == 13) {
                SubmitCustomSearch();
                return false;
            }
        }
    }

function SubmitCustomSearch() {
    var searchText = document.getElementById("searchText");
    if (searchText != null) {
        if (searchText.value != null && searchText.value!="") {
            document.location = "/ct/vbpq/Lists/LawType/TimKiemVBPL.aspx?q=" + searchText.value;
        } else { alert("Nhập nội dung tìm kiếm"); }
    }
}
//dự báo thời tiết
//function changeLocaltion(id) {
  //  if (document.getElementById) {
    //    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    //}
    //if (x) {
      //  x.onreadystatechange = function() {
        //    el = document.getElementById('noidung');
          //  el.innerHTML = 'Loading...';
            //if (x.status == 200) {
   //             el.innerHTML = x.responseText;
            //}
      //  }
       // x.open("GET", '/UserControls/ucGalleryThaiBinh/DubaoThoiTiet/weather.aspx?id=' + id, true);
        //x.send(null);
    //}
///}
function createMessage(title, message) {
    $("#dialog-message").attr("title", title);
    $("#ui-dialog-title-dialog-message").attr("title", title);
    $("#ui-dialog-title-dialog-message").text(title);    
    $("#dialog-message").html("<p>" + message + "</p>");
    $("#dialog-message").dialog({
        resizable: false,
        height: 160,
        modal: true,
        buttons: {
            "Đóng lại": function () {
                $(this).dialog("close");
            }
        }
    });
}
function throwMessageOnErrorDialog(message) {
    $("#dialog-message").attr("title", "Có lỗi xảy ra");
    $("#ui-dialog-title-dialog-message").attr("title", "Có lỗi xảy ra");
    $("#ui-dialog-title-dialog-message").text("Có lỗi xảy ra");
    $("#dialog-message").html("<p>" + message + "</p>");
    $("#dialog-message").dialog({
        resizable: false,
        height: 160,
        modal: true,
        buttons: {
            "Đóng lại": function () {
                $(this).dialog("close");
            }
        }
    });
}
function createUploader(title) {
    var uploader = new qq.FileUploader({
    	title: title,
        element: document.getElementById('btnChosse'),
        action: '/UserControls/UploadFile.aspx',
        debug: true,
        onSubmit: function (id, fileName) {
            // check trùng file
            var exits = false;
            //check trong trường hợp mới upload
            $("#listFileAttach li").each(function (index, item) {
                if (fileName == $(this).children("span").attr("title"))
                    exits = true;
            });
            //check trên file đã upload
            $("#listFileAttachRemove li").each(function (index, item) {
                if (fileName == $(this).children("span").attr("title"))
                    exits = true;
            });

            if (exits) {
                createMessage("Thông báo", fileName + " Đã tồn tại.");
                return false;
            }
        },
        onComplete: function (id, fileName, responseJSON) {
            if (responseJSON.upload) {
                $("#listFileAttach").append(getHTMLDeleteLink(responseJSON));
                $("#listValueFileAttach").val(changeHiddenInput())
            } else {
                createMessage("Thông báo", responseJSON.message);
            }
        }
    });
}
function createCustomUploader(title, btnupload, ullistFileAttach, hdlistValueFileAttach) {
    var uploader = new qq.FileUploader({
    	title: title,
        element: document.getElementById(btnupload),		
        action: '/UserControls/UploadFile.aspx',
        debug: true,
        onSubmit: function (id, fileName) {
            // check trùng file
            var exits = false;
            //check trong trường hợp mới upload
            $("#" + ullistFileAttach + " li").each(function (index, item) {
                if (fileName == $(this).children("span").attr("title"))
                    exits = true;
            });

            if (exits) {
                createMessage("Thông báo", fileName + " đã tồn tại.");
                return false;
            }
        },
        onComplete: function (id, fileName, responseJSON) {
            if (responseJSON.upload) {
                $("#" + ullistFileAttach + "").append(getHTMLDeleteLinkUnique(responseJSON, btnupload, ullistFileAttach, hdlistValueFileAttach));
                $("#" + hdlistValueFileAttach + "").val(changeHiddenInputUnique(ullistFileAttach))

            } else {
                createMessage("Thông báo", responseJSON.message);
            }
        }
    });
}
function createUploaderUnique(btnupload, ullistFileAttach, ullistFileAttachRemove, hdlistValueFileAttach) {
    var uploader = new qq.FileUploader({
        element: document.getElementById(btnupload),
        action: '/UserControls/UploadFile.aspx',
        debug: true,
        onSubmit: function (id, fileName) {
            // check trùng file
            var exits = false;
            //check trong trường hợp mới upload
            $("#" + ullistFileAttach + " li").each(function (index, item) {
                if (fileName == $(this).children("span").attr("title"))
                    exits = true;
            });
            //check trên file đã upload
            $("#" + ullistFileAttachRemove + " li").each(function (index, item) {
                if (fileName == $(this).children("span").attr("title"))
                    exits = true;
            });

            if (exits) {
                createMessage("Thông báo", fileName + " Đã tồn tại.");
                return false;
            }
        },
        onComplete: function (id, fileName, responseJSON) {
            if (responseJSON.upload) {
                $("#" + ullistFileAttach + "").append(getHTMLDeleteLinkUnique(responseJSON, btnupload, ullistFileAttach, hdlistValueFileAttach));
                $("#" + hdlistValueFileAttach + "").val(changeHiddenInputUnique(ullistFileAttach))

            } else {
                createMessage("Thông báo", responseJSON.message);
            }
        }
    });
}
function DeleteFileUpdateUnique(file, btnUpload, ullistFileAttachRemove) {
    var linkDelete = $("#listValueFileAttachRemove").val();
    $("#listValueFileAttachRemove").val(linkDelete + file + "#");
    $("#" + ullistFileAttachRemove + " span[id=" + file + "]").parent().remove();
    $("#" + btnUpload).css("display", "");
}
//lấy dữ liệu từ list Unique
function changeHiddenInputUnique(ollistFileAttach) {
    var valueFile = '[';
    var total = $("#" + ollistFileAttach + " li").length;
    $("#" + ollistFileAttach + " li").each(function (i) {
        valueFile += '{"FileServer": "' + $(this).children("span").attr("id") + '"\,';
        valueFile += '"FileName": "' + $(this).children("span").attr("title") + '"\}';
        if (i + 1 < total)
            valueFile += ',';
    });
    valueFile += "]";
    return valueFile;
}
//Lấy về html file của upload unique
function getHTMLDeleteLinkUnique(data, btnupdate, ollistFileAttach, hdlistValueFileAttach) {
    $("#" + btnupdate).css("display", "none");
    return "<li><span id=\"" + data.fileserver + "\" title=\"" + data.filename + "\">" + data.filename
    + "</span><a href=\"javascript:DeleteFileUnique('" + data.fileserver + "', '"
    + btnupdate + "', '" + ollistFileAttach + "','" + hdlistValueFileAttach + "' );\"><img src=\"/UserControls/Images/new_icon/act_filedelete.png\" title=\"Xóa file đính kèm\" border=\"0\"></a></li>";
}
//xóa file Unique
function DeleteFileUnique(file, btnupdate, ollistFileAttach, hdlistValueFileAttach) {
    $.post('/UserControls/DeleteFile.aspx', { del: file });
    $("#" + ollistFileAttach + " span[id=" + file + "]").parent().remove();
    $("#" + hdlistValueFileAttach).val(changeHiddenInputUnique(ollistFileAttach));
    $("#" + btnupdate).css("display", "");
}
/*file đính kèm*/
//Lấy về html file
function getHTMLDeleteLink(data) {
    return "<li><span id=\"" + data.fileserver + "\" title=\"" + data.filename + "\">" + data.filename + "</span><a href=\"javascript:DeleteFile('" + data.fileserver + "');\"><img src=\"/UserControls/Images/new_icon/act_filedelete.png\" title=\"Xóa file đính kèm\" border=\"0\"></a></li>";
}

//xóa file
function DeleteFile(file) {
    $.post('/UserControls/DeleteFile.aspx', { del: file });
    $("#listFileAttach span[id=" + file + "]").parent().remove();
    $("#listValueFileAttach").val(changeHiddenInput());
}

function DeleteFileUpdate(file) {
    var linkDelete = $("#listValueFileAttachRemove").val();
    $("#listValueFileAttachRemove").val(linkDelete + file + "#");
    $("#listFileAttachRemove span[id=" + file + "]").parent().remove();
}

//lấy dữ liệu từ list 
function changeHiddenInput() {
    var valueFile = '[';
    var total = $("#listFileAttach li").length;
    $("#listFileAttach li").each(function (i) {
        valueFile += '{"FileServer": "' + $(this).children("span").attr("id") + '"\,';
        valueFile += '"FileName": "' + $(this).children("span").attr("title") + '"\}';
        if (i + 1 < total)
            valueFile += ',';
    });
    valueFile += "]";
    return valueFile;
}
