var xmlHttp;

function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
    return xmlHttp;
}

/**
Tahani Paging
**/

function Paging1(page){
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }
    var url="Library/mabruk_paging.asp";
    var params = "page1="+page;
    params += "&table=1";
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = readyState1;
    xmlHttp.send(params);
}

function readyState1(){
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        document.getElementById("tahaniCards").innerHTML = xmlHttp.responseText;
    } else if (xmlHttp.readyState == 4 && xmlHttp.status != 200){
        alert(xmlHttp.responseText);
    }
}

/**
Mabruk Cards Paging
**/

function Paging2(page){
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }
    var url="Library/mabruk_paging.asp";
    var params = "page2="+page;
    params += "&table=2";
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = readyState2;
    xmlHttp.send(params);
}
function readyState2(){
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        document.getElementById("mabrukCards").innerHTML = xmlHttp.responseText;
    } else if (xmlHttp.readyState == 4 && xmlHttp.status != 200){
        alert(xmlHttp.responseText);
    }
}

function openNewCardWindow(){
    window.open("AddTahaniCard.asp","","width=570,height=570,scrollbars=0,location=0");
}

function showCard(){
    window.open('AddMabrukCard.asp','','location=0,status=0,scrollbars=0,width=545,height=580');
}

function changeColor(tr,col){
    if(col == 1){
        tr.style.backgroundColor = "#EBDEEA";
    } else if(col == 2) {
        tr.style.backgroundColor = "#E5E5E9";
    }
}

function newsRegester()
{
    window.open("","newsRegester","width=625,height=500,toolbar=0");
}

function showCommentWindow(url){
    window.open (url,"","location=0,status=0,scrollbars=1,width=690,height=620" );
}

function trim(str){
    return str.replace(/^\s+|\s+$/g,"");
}

/**
    Statistics
**/
var RedirectionPage = "";
function watchElement(ElementType, ElementID, RPage){
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }
    var url="Library/AfkarElementStatistics.asp";
    var params = "ElementType="+encodeURIComponent(ElementType);
    params += "&ElementID="+ElementID;
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = watchElementStatus;
    xmlHttp.send(params);
    RedirectionPage = RPage;
}

function watchElementStatus(){
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        if(RedirectionPage!=""){
            var tmp = RedirectionPage;
            RedirectionPage = "";
            window.location = tmp;
        }
    } else if (xmlHttp.readyState == 4 && xmlHttp.status != 200){
        if(RedirectionPage!=""){
            var tmp = RedirectionPage;
            RedirectionPage = "";
            window.location = tmp;
        }
    }
}