﻿var xhr = getXmlHttpRequestObject();
   
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
	    return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
	    return new ActiveXObject("Microsoft.XMLHTTP");
	    }
     else {	
        alert("Ajax Search is not supported on this browser");
        }
}

function MainFunction() {
        var divLoading = document.getElementById('divMsg');
        document.getElementById('MailingList1_txtJoin').style.display = 'none';
        document.getElementById('MailingList1_btnJoin').style.display = 'none';
        divLoading.style.display='block';
        document.getElementById('imgLoading').style.display='inline';
        var msg = document.getElementById('msgLoading');
        msg.style.display='inline';
        msg.innerHTML = 'Joining, Please wait...'
	    if (xhr.readyState == 4 || xhr.readyState == 0) {
	        var str = escape(document.getElementById('MailingList1_txtJoin').value);
                 xhr.open("GET", '../TLQatarEN/AddMemberAjax.aspx?Email=' + str, true);
                
//		        xhr.open("GET", 'AddMemberAjax.aspx?Email=' + str, true);
	        xhr.onreadystatechange = handleAjaxRequest;
            xhr.send(null);
	        }
}

function handleAjaxRequest() {
    if (xhr.readyState == 4) {
        //document.getElementById('MailingList1_txtJoin').style.display = 'inline';
        document.getElementById('MailingList1_btnJoin').style.display = 'inline';
        var str = xhr.responseText;
        if (str.length > 0)
        {
            document.getElementById('imgLoading').style.display='none';
            var msg = document.getElementById('msgLoading');
            //msg.innerHTML = str;
            if ( str == '1')//if we have any error or reason to stop the ajax threading
              {  var email = document.getElementById('MailingList1_txtJoin').value;
//                if (window.XMLHttpRequest)
//                {
//                window.navigate("NewsRegistration.aspx?pageid=334&Email=" + email); //for IE
//                }
//                else
//                {
//                 window.location.href="NewsRegistration.aspx?pageid=334&Email=" + email //for Both IE and Fire fox
//                }
                //window.navigate("NewsRegistration.aspx?pageid=334&Email=" + email); for IE
               
               window.location.href="NewsRegistration.aspx?pageid=334&Email=" + email //for Both IE and Fire fox
                }else{
                msg.innerHTML = str;
                }
            document.getElementById('btnReset').style.display='inline';
            document.getElementById('MailingList1_btnJoin').style.display='none';
        }
    }
}

function MainFunction2() {
        var divLoading = document.getElementById('divMsg');
        document.getElementById('MailingListSub1_txtJoin').style.display = 'none';
        document.getElementById('MailingListSub1_btnJoin').style.display = 'none';
        divLoading.style.display='block';
        document.getElementById('imgLoading').style.display='inline';
        var msg = document.getElementById('msgLoading');
        msg.style.display='inline';
        msg.innerHTML = 'Joining, Please wait...'
	    if (xhr.readyState == 4 || xhr.readyState == 0) {
	        var str = escape(document.getElementById('MailingListSub1_txtJoin').value);
	        xhr.open("GET", 'AddMemberAjax.aspx?Email=' + str, true);	
	        xhr.onreadystatechange = handleAjaxRequest2;
	        xhr.send(null);
	        }
}

function handleAjaxRequest2() {
    if (xhr.readyState == 4) {
        //document.getElementById('MailingListSub1_txtJoin').style.display = 'inline';
        document.getElementById('MailingListSub1_btnJoin').style.display = 'inline';
        var str = xhr.responseText;
        if (str.length > 1)
        {
            document.getElementById('imgLoading').style.display='none';
            var msg = document.getElementById('msgLoading');
            msg.innerHTML = str;
            document.getElementById('btnReset').style.display='inline';
            document.getElementById('MailingListSub1_btnJoin').style.display='none';
        }
    }
}
