var xmlHttp                                 = GetXmlHttpObject();
function loadPages()
{
    alert('here');
}
function GetXmlHttpObject()
{
    var xmlHttp;

    xmlHttp                                 = null;
    try
    {
        xmlHttp                             = window.XMLHttpRequest? new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)
    {
        alert("error");
    }
    return xmlHttp;
}
function saveEntry()
{
    if (xmlHttp == null)
    {
        alert ("Browser does not support HTTP Request")
        return;
    }

    var name                                = document.getElementById("name").value;
    var email                               = document.getElementById("email").value;
    var comments                            = document.getElementById("comments").value;
    var siteid                              = document.getElementById("siteid").value;
    var website                             = document.getElementById("website").value;
    var secc                                = document.getElementById("secc").value;

    var postStrng                             = "name=" + name + "&email=" + email+ "&comments=" + comments + "&siteid=" + siteid +"&secc="+secc+ "&website=" + website ;
    
    var baseUrl = 'insertEntry.php';
    xmlHttp.open('POST',baseUrl,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange =rtnSaveEntry;
    xmlHttp.send(postStrng);

}

function rtnSaveEntry()
{
    if(xmlHttp.readyState == 4)
    {
        if(xmlHttp.status == 200)
        {
            var msgstring                     = xmlHttp.responseText;
            var msg_array                   = msgstring.split("*;*");

            if(msg_array[0] == 1000)
            {

                document.getElementById("msgDiv").innerHTML=msg_array[1];
                document.getElementById("errDiv").innerHTML="";
                //loadSubmitForm();
		loadList();
            }
            else
            {
                document.getElementById("msgDiv").innerHTML= "";
                document.getElementById("errDiv").innerHTML=msg_array[1];

            }

        }
        else
        {
            alert('There was a problem with the request.');
        }
    }
}

//This function used for loading the guest book list in base page 
function loadList()
{
    if (xmlHttp == null)
    {
        alert ("Browser does not support HTTP Request")
        return;
    }
    document.getElementById("errDiv").innerHTML="";
    var baseUrl = 'listEntries.php';
    xmlHttp.open('POST',baseUrl,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange =rtnLoadList;
    xmlHttp.send(null);
}
function rtnLoadList()
{
    if(xmlHttp.readyState == 4)
    {
        if(xmlHttp.status == 200)
        {
            var msgstring                     = xmlHttp.responseText;
            document.getElementById("ajaxDiv").innerHTML=msgstring;
        }
        else
        {
            alert('There was a problem with the request.');
        }
    }
}

function viewDelete(id)
{
    if (xmlHttp == null)
    {
        alert ("Browser does not support HTTP Request")
        return;
    }
    
    document.getElementById("msgDiv").innerHTML= "";
    document.getElementById("errDiv").innerHTML= "";
    
    var postStrng                             = "id=" + id; 
    var baseUrl = 'deleteEntry.php';
    xmlHttp.open('POST',baseUrl,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange =rtnLoadList;
    xmlHttp.send(postStrng);
}
function deleteEntry()
{
    if (xmlHttp == null)
    {
        alert ("Browser does not support HTTP Request")
        return;
    }
    
    var userId                                = document.getElementById("userId").value;
    var pwd                                   = document.getElementById("pwd").value;
    var id                                    = document.getElementById("id").value;
    var postStrng                             = "userId=" + userId + "&pwd=" + pwd+ "&id=" + id;
    if(document.getElementById('chk').checked)
    {
        postStrng                               += "&ipChk="+document.getElementById("chk").value;
    }    
    var baseUrl = 'deleteEntryCode.php';
    xmlHttp.open('POST',baseUrl,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange =rtnDeleteEntry;
    xmlHttp.send(postStrng);
}
function rtnDeleteEntry()
{
    if(xmlHttp.readyState == 4)
    {
        if(xmlHttp.status == 200)
        {
            var msgstring                     = xmlHttp.responseText;
            var msg_array                   = msgstring.split("*;*");
            if(msg_array[0] == 1000)
            {

                document.getElementById("msgDiv").innerHTML=msg_array[1];
                document.getElementById("errDiv").innerHTML="";
                loadList();
            }
            else
            {
                document.getElementById("msgDiv").innerHTML= "";
                document.getElementById("errDiv").innerHTML=msg_array[1];
            
            }
        }
        else
        {
            alert('There was a problem with the request.');
        }
    }
}
function loadSubmitForm()
{
    if (xmlHttp == null)
    {
        alert ("Browser does not support HTTP Request")
        return;
    }
    //document.getElementById("errDiv").innerHTML="";
    //document.getElementById("msgDiv").innerHTML="";
    var baseUrl = 'submitForm.php';
    xmlHttp.open('POST',baseUrl,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange =rtnLoadList;
    xmlHttp.send(null);
}

