(function($) {
	$.fn.outerHTML = function(s) {
		return (s) 
			? this.before(s).remove() 
			: $('<p>').append(this.eq(0).clone()).html();
	}
})(jQuery);

function LH(OG)
{
    if($get("Title"+OG.wid) != null && $get("Title"+OG.wid).getAttribute("title") != null && $get("Title"+OG.wid).getAttribute("title") != "")
    {
        $get("Title"+OG.wid).innerHTML = $get("Title"+OG.wid).getAttribute("title");
    }
}
function LS(OG)
{
    if($get("Title"+OG.wid) != null)
    {
        $get("Title"+OG.wid).setAttribute("title",  $get("Title"+OG.wid).innerHTML); 
        $get("Title"+OG.wid).innerHTML = OG.MSG;
    }
}
function createPaging(OG)
{
    paging = document.createElement("div");
    paging.setAttribute("style", "clear:both;direction:"+OG.dir+";");
    for(t=0;t<paging.childNodes.length;++i)
    {
        paging.removeChild(paging.childNodes[t]);
    }
    temp = Math.ceil(OG.rowcount/OG.pagesize);
    first = 0;
    
    if(temp >OG.pagercount)
    {
        first = OG.curpage;
        if(OG.curpage >= 2 ) first = OG.curpage - 2;
    }
    end = OG.pagercount+first;
    if(end >  temp) end = temp;

    a = document.createElement("a");
    if(0 != OG.curpage)
    {
        a.setAttribute("href", "javascript:void(0);");
        a.setAttribute("onclick", "getData"+OG.id+"(0);");
    }
    else
    {
        a.setAttribute("style", 'color:red;');
    }
    a.innerHTML = '<img src="/image/Portal/Common/arrow_left.gif">';
    paging.appendChild(a);

    for(i = first; i<end; ++i)
    {
        a = document.createElement("a");
        if(i != OG.curpage)
        {
            a.setAttribute("href", "javascript:void(0);");
            a.setAttribute("onclick", "getData"+OG.id+"("+i+");");
        }
        else
        {
            a.setAttribute("style", 'color:red;');
        }
        a.innerHTML = i+1;
        paging.appendChild(a);
    }
    
    a = document.createElement("a");
    if(temp-1 != OG.curpage)
    {
        a.setAttribute("href", "javascript:void(0);");
        a.setAttribute("onclick", "getData"+OG.id+"("+(temp-1)+");");
    }
    else
    {
        a.setAttribute("style", 'color:red;');
    }
    a.innerHTML = '<img src="/image/Portal/Common/arrow_right.gif">';
    paging.appendChild(a);
    return paging.innerHTML;
}
function SortGrid(field, OG)
{
    OG.curpage = 0;
    if(OG.SortField == field+"_"+"DESC") OG.Order = "ASC";
    else OG.Order = "DESC";
    OG.SortField = field+"_"+OG.Order;
    OG.filter.PageIndex = OG.curpage;
    OG.filter.OrderBy = field;
    OG.filter.Order = OG.Order;
    return OG.filter;
}
function getData(index, OG)
{
    LS(OG);
    if(OG.SortField != "")
    {
        OG.filter.OrderBy = OG.OrderBy = OG.SortField.split("_")[0];
        OG.filter.Order = OG.SortField.split("_")[1];
    }
    if(index.toString() == "+1") OG.curpage++;
    else if(index == "-1") OG.curpage--;
    else OG.curpage = index;
    OG.filter.PageIndex = OG.curpage;
    return OG.filter;
}
function OnSucess(result, OG) 
{  
    LH(OG);
    node = $get("Grid"+OG.id);
    if(node.firstChild != null)
    node.removeChild(node.firstChild);
    OG.rowcount = result[1];
    drawGrid2(result[0], OG);
}  
function OnFauilre(result, OG) 
{  
    if($get("Title"+OG.wi) != null && $get("Title"+OG.wi).getAttribute("title") != null)
    {
        $get("Title"+OG.wi).innerHTML=$get("Title"+OG.wi).getAttribute("title");
    }
}  
function ImportAttr(o1, o2)
{
    for(i_=0;i_<o2.attributes.length;++i_)
    {
        if(o2.attributes.item(i_).nodeName.toLowerCase() != "title" && o2.attributes.item(i_).nodeName.toLowerCase() != "id" && o2.attributes.item(i_).nodeName.toLowerCase() != "class")
        {
            o1.setAttribute(o2.attributes.item(i_).nodeName, o2.attributes.item(i_).nodeValue);
        }
        if(o2.attributes.item(i_).nodeName.toLowerCase() == "class")
        {
            o1.className = o2.attributes.item(i_).nodeValue;
        }
    }
    return o1;    
}
function ShowSelectHead(OG)
{
    if($get("Selected"+OG.id) != null)
    {
        temp = $get("SelectHead").innerHTML;
        r = new RegExp("{ClientID}", 'gi');
        temp = temp.replace(r, OG.id);
        $get("Selected"+OG.id).innerHTML = temp;
    }
}
function drawGrid2(obj, OG)
{
ShowSelectHead(OG);
var oTbl = $get(OG.tid);
oTbl.style.display = "none";
if($get("VisualGrid"+OG.id) != null) 
{
    if($get("GridView"+OG.id) == null)
        $get("VisualGrid"+OG.id).parentNode.removeChild($get("VisualGrid"+OG.id));    
    else $get("GridView"+OG.id).removeChild($get("VisualGrid"+OG.id));    
}
var oTbl_new = document.createElement(oTbl.getAttribute("title"));
oTbl_new.setAttribute("id", "VisualGrid"+OG.id);
oTbl_new = ImportAttr(oTbl_new, oTbl);

if(oTbl_new.tagName == "table" || oTbl_new.tagName == "TABLE") oTbl_new.style.display = 'block';    
else oTbl_new.style.display = 'inline';    

/*if($get("GridView"+OG.id) == null)
{
    oTbl.parentNode.appendChild(oTbl_new);
}
else $get("GridView"+OG.id).appendChild(oTbl_new);*/

trs = oTbl.getElementsByTagName("tr");
if(trs[3] != null)
{
    
}
if(trs.length != 3) alert(OG.tid);
oRow1 = document.createElement(trs[0].getAttribute("title"));
oTbl_new.appendChild(oRow1);
oRow1 = ImportAttr(oRow1, trs[0]);
tds = trs[0].getElementsByTagName("td");
for(t=0;t<tds.length;t++)
{
    if(tds[t].getAttribute("sort") != null)
    {
        oCell = document.createElement(tds[t].getAttribute("title"));
        oCell = ImportAttr(oCell, tds[t]);
        oRow1.appendChild(oCell);
        oCell.innerHTML = "<a href='javascript:void(0);' onclick='SortGrid"+OG.id+"(\""+tds[t].getAttribute("sort")+"\")'>"+tds[t].innerHTML+"</a>";
    }
    else
    {
        oCell = document.createElement(tds[t].getAttribute("title"));
        oCell = ImportAttr(oCell, tds[t]);
        oRow1.appendChild(oCell);
        oCell.innerHTML = tds[t].innerHTML;
    }
}
for(i=0;i<obj.length;i++)
{
    oRow = document.createElement(trs[1].getAttribute("title"));
    oTbl_new.appendChild(oRow);
    oRow = ImportAttr(oRow, trs[1]);
    tds = trs[1].getElementsByTagName("td");
    for(t=0;t<tds.length;t++)
    {
        oCell = document.createElement(tds[t].getAttribute("title"));
        oCell = ImportAttr(oCell, tds[t]);
        oRow.appendChild(oCell);
        temp = tds[t].innerHTML;
        if(typeof(obj[i]) != 'undefined')
        for(field in obj[i])
        {
            if(obj[i][field] == null) obj[i][field] = "";
            if(typeof(obj[i][field]) != 'object') 
            {
                if(typeof(obj[i][field]) != 'string') obj[i][field] = (String)(obj[i][field]);
                str = obj[i][field];
                r = new RegExp("'", 'gi');
                try{str = str.replace(r, "’");}
                catch(Exception){alert(typeof(obj[i][field]));}
                r = new RegExp('"', 'gi');
                str = str.replace(r, '&gt;');
                r = new RegExp("[\(]{1}", 'gi');
                str = str.replace(r, "\(");
                r = new RegExp("[\)]{1}", 'gi');
                str = str.replace(r, "\)");
                r = new RegExp("[\r\n\t]{1,}", 'gi');
                str = str.replace(r, "<br/>");
                r = new RegExp("%7b"+field+"%7d", 'gi');
                temp = temp.replace(r, str);
                r = new RegExp("{"+field+"}", 'gi');
                temp = temp.replace(r, str);
                r = new RegExp("{ClientID}", 'gi');
                temp = temp.replace(r, OG.id);
            }
        }
        oCell.innerHTML = temp;
    }
}
oRow2 = document.createElement(trs[2].getAttribute("title"));
tds = trs[2].getElementsByTagName("td");
var idpaging = oRow2;
for(t=0;t<tds.length;t++)
{
    if( tds[t].getAttribute("title") != "" && tds[t].getAttribute("title") != null)
    {
        oCell = document.createElement(tds[t].getAttribute("title"));
        oCell = ImportAttr(oCell, tds[t]);
        if(tds[t].getAttribute("paging") == "true"){idpaging = oCell;}
        oCell.innerHTML = tds[t].innerHTML;
        oRow2.appendChild(oCell);
    }
}
oTbl_new.appendChild(oRow2);
oRow2 = ImportAttr(oRow2, trs[2]);
if(OG.pagesize < OG.rowcount)
{
    idpaging.style.display = "inline";
    idpaging.innerHTML = createPaging(OG);
}
else idpaging.style.display = "none";

div1 = document.createElement("table1");
div = document.createElement("table");
div = div.appendChild(oTbl_new)
div1.appendChild(div)
if($get("GridView"+OG.id) == null)
    $get("Visual"+OG.id).innerHTML = div1.innerHTML;
 else 
    $get("GridView"+OG.id).appendChild(oTbl_new);

}
function shownews(id,image,subject,comment,url,header)
{
    $get('Image'+id).innerHTML = "<img width=100 height=100 src='"+ image +"'>";
    $get('Subject'+id).innerHTML = "<a href='"+ url +"'>"+ subject +"</a><br><span class=\"Comment\">" + comment+ "</span>";
    $get('HeadLine'+id).innerHTML = header;
}
/*
function shownews(id,image,subject,comment,url,header)
{
    $get('Image'+id).style.display = "block";
    c = $get('Image'+id).childNodes.length;
    ok = 0;
    for(i=0;i<c;++i)
    {
        $get('Image'+id).childNodes.item(i).style.display = "none";
        if($get('Image'+id).childNodes.item(i).id == image.replace(/\//g, "") )
        {
            $get('Image'+id).childNodes.item(i).style.display = "block";
            ok = 1;
        }
    }
    img = document.createElement("image");
    img.setAttribute("id", image.replace(/\//g, "") );
    img.setAttribute("width", "100");
    img.setAttribute("hright", "100");
    img.setAttribute("src",image);
    if(ok == 0) $get('Image'+id).appendChild(img);
    //$get('Image'+id).innerHTML = "<img width=100 height=100 src='"+ image +"'>";
    $get('Subject'+id).innerHTML = "<a href='"+ url +"'>"+ subject +"</a><br><span class=\"Comment\">" + comment+ "</span>";
    $get('HeadLine'+id).innerHTML = header;
}
*/
