﻿var request = null; var req_in_use = false;
function FixupTbl(divTag) {
    $('.newTbl th').addClass('thicktext');
    LoadUserStyle();
    var tblContainer = document.createElement('table');
    $(tblContainer).attr('cellpadding', '0').attr('cellspacing', '0').attr('width', '100%');
    var firstRow = document.createElement('tr');
    var firstRightCell = document.createElement('td');
    $(firstRightCell).css('width', '11px');
    $(firstRightCell).html('<img src="/img/pages_corner_rigt.jpg" />');
    var firstMidCell = document.createElement('td');
    $(firstMidCell).css('border-top', 'solid 1px #cccccc');
    var firstLeftCell = document.createElement('td');
    $(firstLeftCell).css('width', '11px');
    $(firstLeftCell).html('<img src="/img/pages_corner_left.jpg" />');
    firstRow.appendChild(firstRightCell);
    firstRow.appendChild(firstMidCell);
    firstRow.appendChild(firstLeftCell);

    var secondRow = document.createElement('tr');
    var secondRightCell = document.createElement('td');
    $(secondRightCell).css('border-right', 'solid 1px #cccccc');
    var secondMidCell = document.createElement('td');
    if (divTag)
        $(secondMidCell).html(divTag);
    else
        $(secondMidCell).html($('.newTbl'));
    var secondLeftCell = document.createElement('td');
    $(secondLeftCell).css('border-left', 'solid 1px #cccccc');
    secondRow.appendChild(secondRightCell);
    secondRow.appendChild(secondMidCell);
    secondRow.appendChild(secondLeftCell);

    var lastRow = document.createElement('tr');
    var lastRightCell = document.createElement('td');
    $(lastRightCell).html('<img src="/img/pages_corner_rigt_down.jpg" />');
    var lastMidCell = document.createElement('td');
    $(lastMidCell).css('border-bottom', 'solid 1px #cccccc');
    var lastLeftCell = document.createElement('td');
    $(lastLeftCell).html('<img src="/img/pages_corner_left_down.jpg" />');
    lastRow.appendChild(lastRightCell);
    lastRow.appendChild(lastMidCell);
    lastRow.appendChild(lastLeftCell);

    tblContainer.appendChild(firstRow);
    tblContainer.appendChild(secondRow);
    tblContainer.appendChild(lastRow);

    $('.ConvertThisTable').html(tblContainer);

}

//function ShowCounter() {
//    var today = new Date();
//    var d = new Date("May 31, 2011 07:00:00");
//    var cookieVlaue = ReadCookie('ForceAccess');
////    EraseCookie("ForceAccess");
//    if (d > today && cookieVlaue != "1") {
//        $('#countdown_dashboard').countDown({
//            targetDate: {
//                'day': 31,
//                'month': 5,
//                'year': 2011,
//                'hour': 7,
//                'min': 0,
//                'sec': 0
//            },
//            onComplete: function () {
//                $('#counterFrame').hide("explode", 1000);
//                $('#mainContainer').show();
//            }
//        });
//    }
//    else if ((d > today && cookieVlaue == "1") || d < today) {
//        $('#mainContainer').show();
//        $('#counterFrame').hide();
//    }
//}

function LangQuery() {
    var qVlaue = getQuerystring('lang');
    var link = window.location.href.split('?');
    if (qVlaue) {
        if (qVlaue == "ar") {
            var queries = window.location.href.split("?lang=ar");
            window.location = link[0] + "?lang=en" + queries[1];
        }
        else {
            var queries = window.location.href.split("?lang=en");
            window.location = link[0] + "?lang=ar" + queries[1];
        }
    }
    else {
        if (link[1])
            window.location = link + "&lang=en";
        else
            window.location = link[0] + "?lang=en";
    }
}
function InitReq() {
    try {
        request = new XMLHttpRequest();
    }
    catch (d) {
        try {
            request = new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch (x)
        { }
    }
}
function __(e) { return document.getElementById(e); }
function _r(u, c, is_as, si, im_el) {
    if (req_in_use == true) {
        var t = function () { _r(u, c, is_as, si, im_el); };
        //setTimeout("_r('" + u + "'," + c + "," + is_as + "," + si + ")", 200);
        setTimeout(t, 200);
        return;
    }
    if (!request)
        InitReq();
    req_in_use = true;
    request.open("GET", u, is_as);
    if (si == true) {
        var x = __(im_el);
        x.className = x.className.replace("hide", "");
        request.onreadystatechange = function () {
            c();
            if (request.readyState == 4) {
                x.className += " hide"
            }
        };
    }
    else {
        request.onreadystatechange = function () {
            c();
        }
    }
    request.send(null);
}
function _r_p(u, p, c, is_as, si) {
    if (req_in_use == true) {
        return false;
    }
    if (!request)
        InitReq();
    req_in_use = true;
    request.open("POST", u, is_as);

    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", p.length);
    request.setRequestHeader("Connection", "close");
    if (si == true) {
        var x = __('il');
        if (x) {
            x.style.display = '';
            x.style.left = (document.body.offsetWidth / 2 - x.offsetWidth / 2) + 'px';
            x.style.top = (document.body.offsetHeight / 2 - x.offsetHeight / 2) + 'px';
        }
        request.onreadystatechange = function () {
            c();
            HandleLoading();
        };
    }
    else {
        request.onreadystatechange = function () {
            c();
        }
    }
    request.send(p);
}
function HandleLoading() {
    if (request.readyState == 4) {
        var x = __('il')
        if (x)
            x.style.display = 'none';
    }
}
function TellFriendPopup(label, url) {
    var url = "TellFriendPopUp.aspx?l=" + label + "&r=" + url;
    window.open(url, "tell", "menubar=0,resizable=0,width=600,height=250");
}
function CreateCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
function ReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
function IsEnglish() {
    var data = getQuerystring('lang');
    if (data) {
        if (data == 'en') {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

function EraseCookie(name) {
    CreateCookie(name, "", -1);
}
function tabselect(tab, cont_id, class_selected, class_not_selected, tabs_element, cont_elements) {
    var cont = __(cont_id);
    if (cont) {
        var conts = cont_elements ? cont.getElementsByTagName(cont_elements) : cont.childNodes;
        for (var i = 0; i < conts.length; i++) {
            if (!conts[i].className) continue;
            if (conts[i].className.indexOf("show") > -1 || conts[i].className.indexOf("hide") > -1) {
                conts[i].className = conts[i].className.replace("show", "hide");
            }
        }
        var div_id = tab.id.toString().replace("tab-", "");
        var div = __(div_id);
        div.className = div.className.replace("hide", "show");
        var tabs = tabs_element ? tab.parentNode.getElementsByTagName(tabs_element) : tab.parentNode.childNodes;
        for (var j = 0; j < tabs.length; j++) {
            if (!tabs[j].id)
                continue;
            if (tabs[j].id.toString().indexOf("tab-") < 0)
                continue;

            if (tabs[j].className.indexOf(class_selected) > -1) {
                tabs[j].className = tabs[j].className.replace(class_selected, class_not_selected);
            }
            if (tabs[j].className.indexOf(class_not_selected) > -1)
                continue;
            else {
                //if (tabs[j].className.indexOf(class_selected) < 0)
                tabs[j].className += " " + class_not_selected;
            }
        }
        if (tab.className.indexOf(class_not_selected) > -1)
            tab.className = tab.className.replace(class_not_selected, class_selected);
        else
            tab.className = " " + class_selected;
    }
}
var timerUp = null, timerDown = null;
var step = 1;
function scrollTabsUp(id, SValue) {
    debugger;
    document.getElementById(id).scrollTop -= step;
    if (SValue != null) {
        if (eval(SValue) == eval(document.getElementById(id).scrollTop)) {
            clearTimeout(timerUp);
        }
        else {
            timerUp = setTimeout(function () {
                scrollTabsUp(id, document.getElementById(id).scrollTop)
            }, 10);
        }
    }
    else {
        timerUp = setTimeout(function () {
            scrollTabsUp(id, document.getElementById(id).scrollTop)
        }, 10)
    }
}
function scrollTabsDown(id, SValue) {
    document.getElementById(id).scrollTop += step;
    if (SValue != null) {
        if (eval(SValue) == eval(document.getElementById(id).scrollTop)) {
            clearTimeout(timerDown);
        }
        else {
            timerDown = setTimeout(function () {
                scrollTabsDown(id, document.getElementById(id).scrollTop)
            }, 10)
        }
    }
    else {
        timerDown = setTimeout(function () {
            scrollTabsDown(id, document.getElementById(id).scrollTop)
        }, 10)
    }
}
var timerRight = null, timerLeft = null;
function scrollTabsLeft(id, SValue) {
    document.getElementById(id).scrollLeft -= step;
    if (SValue != null) {
        if (eval(SValue) == eval(document.getElementById(id).scrollLeft)) {
            clearTimeout(timerLeft);
        }
        else {
            timerLeft = setTimeout(function () {
                scrollTabsLeft(id, document.getElementById(id).scrollLeft)
            }, 10);
        }
    }
    else {
        timerLeft = setTimeout(function () {
            scrollTabsLeft(id, document.getElementById(id).scrollLeft)
        }, 10)
    }
}
function scrollTabsRight(id, SValue) {
    document.getElementById(id).scrollLeft += step;
    if (SValue != null) {
        if (eval(SValue) == eval(document.getElementById(id).scrollLeft)) {
            clearTimeout(timerRight);
        }
        else {
            timerRight = setTimeout(function () {
                scrollTabsRight(id, document.getElementById(id).scrollLeft)
            }, 10)
        }
    }
    else {
        timerRight = setTimeout(function () {
            scrollTabsRight(id, document.getElementById(id).scrollLeft)
        }, 10)
    }
}




var current_index = 0;
function scrollTabs(id, increment) {
    var right = increment == 1 ? true : false;

    var tabs = __(id).childNodes;
    if (right && current_index == 0)
        return;
    if (!right && current_index >= tabs.length - 3)
        return;
    if (right)
        current_index = current_index + 1;
    else
        current_index = current_index - 1;
    var doing = current_index == 0;
    var count = 0;
    for (var i = 0; i < tabs.length; i++) {
        if (i <= 2)
            tabs[i].firstChild.className = " hide";
    }
}
function FilterUp(ev, id) {
    Filter(ev, __(id).getElementsByTagName("a"));
}
function Filter(ex, all_links) {
    ex = ex || window.event;
    ex = ex.target ? ex.target : ex.srcElement;
    if (!ex)
        return;
    for (var i = 0; i < all_links.length; i++) {
        if (all_links[i].innerHTML.indexOf(ex.value) < 0 && all_links[i].parentNode.parentNode.style.display != 'none')
            all_links[i].parentNode.parentNode.style.display = 'none';
        else if (all_links[i].innerHTML.indexOf(ex.value) >= 0 && all_links[i].parentNode.parentNode.style.display == 'none')
            all_links[i].parentNode.parentNode.style.display = '';
    }
}
function SH(e) {
    if (e) {
        if (e.className.indexOf("hide") > -1) {
            e.className = e.className.replace("hide", "");
        }
        else {
            if (e.className.indexOf("show") > -1) {
                e.className = e.className.replace("show", "hide");
            }
            else {
                e.className = e.className + " hide";
            }
        }

    }
}
function Show(e, a) {
    if (a)
        e = document.getElementById(e);
    if (e) {
        if (e.className.indexOf("hide") > -1) {
            e.className = e.className.replace("hide", "");
        }
    }
}
function Hide(e, a) {
    if (a)
        e = document.getElementById(e);
    if (e) {
        if (e.className.indexOf("show") > -1) {
            e.className = e.className.replace("show", "hide");
        }
        else {
            if (e.className.indexOf("hide") < 0) {
                e.className = e.className + " hide";
            }
        }

    }

}

function SHTag(parent, offset) {
    var ch = parent.childNodes
    //alert(ch.length);
    if (ch) {

        for (var i = offset; i < ch.length; ++i) {
            if (ch[i].className.indexOf("hide") > -1) {
                ch[i].className = ch[i].className.replace("hide", "");
            }
            else {
                if (ch[i].className.indexOf("show") > -1) {
                    ch[i].className = ch[i].className.replace("show", "hide");
                }
                else {
                    ch[i].className = ch[i].className + " hide";
                }
            }
        }
    }
}
function QueryParam() {
    this.Param = "";
    this.Value = "";
}
function GetQueryStringParams() {
    var t = new Array();
    var ps = window.location.href.split('?');
    if (ps && ps.length > 1) {
        if (ps[1].indexOf("#") > -1)
            ps[1] = ps[1].split("#")[0];
        var all_ps = ps[1].split('&');
        for (var i = 0; i < all_ps.length; ++i) {
            var q = all_ps[i].split("=");
            var temp = new QueryParam();
            temp.Param = q[0];
            if (q.length > 1)
                temp.Value = q[1];
            t.push(temp);
        }
        return t;
    }
    return null;
}
function AddVars() {
    var res = 0;
    for (var i = 0; i < arguments.length; ++i) {
        var t = parseInt(arguments[i]);
        res = res + t;
    }
    return res;
}
function SelectWeather(sel) {
    if (sel[sel.selectedIndex].value == "0") {
        var div = document.getElementById("weather_div");
        Hide(div);
        return;
    }
    var u = "/handlers/GetHTMLPage.ashx?url=" + encodeURIComponent("http://weather.yahooapis.com/forecastrss?u=c&w=" + sel[sel.selectedIndex].value);

    //u = "/test-rss.htm"; //commment this line later. 
    _r(u, Selected, true, false);
}
function Selected() {

    if (request.readyState == 4) {

        req_in_use = false;
        var div = document.getElementById("weather_div");
        var img = document.getElementById("weather_icon");
        var lbl = document.getElementById("weather_label");
        var temp = /<yweather:condition.*?temp=\"(\d+)/i;

        lbl.innerHTML = request.responseText.match(temp)[1] + "̊ C ";
        //        var date = /<yweather:condition.*?date=\"(.+)\"/i;
        //        lbl.innerHTML += "<br />" + request.responseText.match(date)[1];
        var icon = /(http:\/\/l.yimg.com\/a\/i\/us\/we.*)\"/;
        img.src = request.responseText.match(icon)[1];
        var w = //
        Show(div);
    }
}
function ShowMsgBox(inside, where) {
    inside.className += " ";
    var exit = document.createElement("img");
    exit.src = "/images/icons_close.png";
    exit.id = "exitImg";    //Hide(inside); Hide(this);
    exit.setAttribute('style', 'left:50%;margin-left:-300px;position:fixed;top:158px;z-index:1004;');
    exit.onclick = function () { $("#all, #popup, #exitImg, #wasika, #bigPopup").hide('slow'); inside.removeChild(inside.childNodes[0]); };
    exit.title = exit.alt = "إغلاق";
    where.parentNode.insertBefore(exit, where);
    //    where.parentNode.insertBefore(inside, where);

}
function RemoveHTML(str) {
    return str.toString().replace(/<(.|\n)+?>/, "")
                         .replace(/<\/.+>/, "");
}
function CallOnEnter(ev, call) {
    if (!ev)
        ev = window.event;
    var text_selector = ev.target ? ev.target : ev.srcElement;
    if (ev.keyCode == 13) {
        call();
        return false;
    }
    return true;
}





















//extensions
//split function


/* Cross-Browser Split 1.0.1
(c) Steven Levithan <stevenlevithan.com>; MIT License
An ECMA-compliant, uniform cross-browser split method */

var cbSplit;

// avoid running twice, which would break `cbSplit._nativeSplit`'s reference to the native `split`
if (!cbSplit) {

    cbSplit = function (str, separator, limit) {
        // if `separator` is not a regex, use the native `split`
        if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
            return cbSplit._nativeSplit.call(str, separator, limit);
        }

        var output = [],
        lastLastIndex = 0,
        flags = (separator.ignoreCase ? "i" : "") +
                (separator.multiline ? "m" : "") +
                (separator.sticky ? "y" : ""),
        separator = RegExp(separator.source, flags + "g"), // make `global` and avoid `lastIndex` issues by working with a copy
        separator2, match, lastIndex, lastLength;

        str = str + ""; // type conversion
        if (!cbSplit._compliantExecNpcg) {
            separator2 = RegExp("^" + separator.source + "$(?!\\s)", flags); // doesn't need /g or /y, but they don't hurt
        }

        /* behavior for `limit`: if it's...
        - `undefined`: no limit.
        - `NaN` or zero: return an empty array.
        - a positive number: use `Math.floor(limit)`.
        - a negative number: no limit.
        - other: type-convert, then use the above rules. */
        if (limit === undefined || +limit < 0) {
            limit = Infinity;
        } else {
            limit = Math.floor(+limit);
            if (!limit) {
                return [];
            }
        }

        while (match = separator.exec(str)) {
            lastIndex = match.index + match[0].length; // `separator.lastIndex` is not reliable cross-browser

            if (lastIndex > lastLastIndex) {
                output.push(str.slice(lastLastIndex, match.index));

                // fix browsers whose `exec` methods don't consistently return `undefined` for nonparticipating capturing groups
                if (!cbSplit._compliantExecNpcg && match.length > 1) {
                    match[0].replace(separator2, function () {
                        for (var i = 1; i < arguments.length - 2; i++) {
                            if (arguments[i] === undefined) {
                                match[i] = undefined;
                            }
                        }
                    });
                }

                if (match.length > 1 && match.index < str.length) {
                    Array.prototype.push.apply(output, match.slice(1));
                }

                lastLength = match[0].length;
                lastLastIndex = lastIndex;

                if (output.length >= limit) {
                    break;
                }
            }

            if (separator.lastIndex === match.index) {
                separator.lastIndex++; // avoid an infinite loop
            }
        }

        if (lastLastIndex === str.length) {
            if (lastLength || !separator.test("")) {
                output.push("");
            }
        } else {
            output.push(str.slice(lastLastIndex));
        }

        return output.length > limit ? output.slice(0, limit) : output;
    };

    cbSplit._compliantExecNpcg = /()??/.exec("")[1] === undefined; // NPCG: nonparticipating capturing group
    cbSplit._nativeSplit = String.prototype.split;

} // end `if (!cbSplit)`

// for convenience...
String.prototype.split = function (separator, limit) {
    return cbSplit(this, separator, limit);
};
String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, ""); };

// get query string
function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}


var up_flag = 0;
function ScrollUp_onClick(divID) {
    if (up_flag == 0) {
        up_flag = 1;
        Down_flag = 0;
        scrollTabsUp(divID, 10);
    } else {
        up_flag = 0;
        clearTimeout(timerUp);
    }
}

var Down_flag = 0;
function ScrollDown_onClick(divID) {
    if (Down_flag == 0) {
        Down_flag = 1;
        up_flag = 0;
        scrollTabsDown(divID, 10);
    } else {
        Down_flag = 0;
        clearTimeout(timerDown);
    }
}
