/**
 * Name: ajax.js
 * Date Created: 28 Jun' 2011
 * Date Modified: 29 Jun' 2011
 * Description: This file contain the basic function for the implementation of ajax in pages.
 */

/**
 * http is XML object for AJAX.
 */
var http = getXMLHTTPObject(); // We create the HTTP Object
var ajaxfor;  //This check that ajax call is from insert/ edit page

/**
 * getXMLHTTPObject creates the browser specific XML object
 */
function getXMLHTTPObject()
{    
    try 
    {
        req = new XMLHttpRequest();
    } catch (err1)
    {
        try
        {
            req = new ActiveXObject("Msxml12.XMLHTTP");
        } catch (err2)
        { 
            try
            { 
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (err3)
            { 
                req = false;
            }
        }
    }
    return req;
} //getXMLHTTPObject()

/**
 * verifyMembership is method which is called for AJAX implementation
 */
function verifyMember(member)
{
    ajaxfor = 1; //AJAX call from login.php onclick send button
    
    myRand = parseInt(Math.random()*999999);
    var url = 'includes/ajax_handler.php';
    var postStr = "rand=" + encodeURIComponent(myRand) + "&member=" + encodeURIComponent(member);
    
    //alert(postStr);
    http.open("POST", url, true);
    http.onreadystatechange = handleHttpResponse;
    
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", postStr.length);
    http.setRequestHeader("Connection", "close");
    
    http.send(postStr);
} //function verifyMembership(membership)

/**
 * checkUser is method which is called for AJAX implementation,
 * checking the existence of user from database
 */
function checkUser(user)
{
    ajaxfor = 2; //AJAX call from registration.php onsubmit event
    
    myRand = parseInt(Math.random()*999999);
    var url = 'includes/ajax_user_handler.php';
    var postStr = "rand=" + myRand + "&name=" + user.name.value + "&email=" + user.email.value + "&password=" + user.password.value + "&pincode=" + user.pincode.value + "&city=" + user.city.value + "&phone=" + user.phone.value + "&user_exp=" + user.user_exp.value + "&affliation=" + user.affliation.value + "&usertitle=" + user.usertitle.value + "&mobile=" + user.mobile.value + "&fax=" + user.fax.value + "&membership=" + user.membership.value + "&designation=" + user.designation.value + "&currentaddress=" + user.currentaddress.value + "&corporateaddress=" + user.corporateaddress.value + "&organisation=" + user.organisation.value;
    
    //alert(postStr);
    http.open("POST", url, true);
    http.onreadystatechange = handleHttpResponse;
    
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", postStr.length);
    http.setRequestHeader("Connection", "close");
    
    http.send(postStr);
} //function verifyMembership(membership)

/**
 * verifylogin is method which is called for AJAX implementation
 */
function verifylogin(login)
{
    ajaxfor = 3; //AJAX call from login.php onclick send button
    
    myRand = parseInt(Math.random()*999999);
    var url = 'includes/ajax_login_handler.php';
    var postStr = "rand=" + myRand + "&username=" + login.name.value + "&password="+login.password.value;
    
    //alert(postStr);
    http.open("POST", url, true);
    http.onreadystatechange = handleHttpResponse;
    
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", postStr.length);
    http.setRequestHeader("Connection", "close");
    
    http.send(postStr);
} //function verifylogin(membership)

/**
 * handleHttpResponse() is used for handling the response.
 */
function handleHttpResponse()
{
    if (http.readyState == 4) 
    {
        if (ajaxfor == 1)
        {
            //document.getElementById("result").innerHTML = http.responseText;
            if (http.responseText == 0) 
            {
                document.getElementById("result").innerHTML = "Your E-mail I.D does not match with our database. Please check and try again.";
            }
            else if (http.responseText == 1)
            {
                document.getElementById("result").innerHTML = "";
                document.forms[1].submit();
            }
        } //if (ajaxfor == 1)
        if (ajaxfor == 2)
        {
            //alert(http.responseText);
            if (http.responseText == 1)
            {
                document.getElementById("result").innerHTML = "E-mail ID already exits in our database. Please check and try again.";
            }
            else if (http.responseText == 0)
            {
                location.href = 'success_register.php';
            }
        } //if (ajaxfor == 2)
        if (ajaxfor == 3)
        {
            //alert(http.responseText);
            if (http.responseText == 0)
            {
                document.getElementById("result").innerHTML = "E-mail ID & Password does not match.<br>Please check and try again.";
            }
            else if (http.responseText == 1)
            {
                document.getElementById("result").innerHTML = "";
                document.forms[0].submit();
            }
        } //if (ajaxfor == 3)
    } //if (http.readyState == 4)
} //handleHttpResponse()

