﻿//Global XMLHTTP Request object
var XmlHttp;
var bBrowser;
var lobjList;
var lobjListVal;
var lobjListTxt;
var iValid;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	bBrowser = false;
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
	    bBrowser = true;
		XmlHttp = new XMLHttpRequest();
	}
}

function requestL() 
{
    var objL = document.getElementById('txtLOGIN');
    var objP = document.getElementById('txtPWD');

    validateInt(objL);
    if (iValid == 0) {
            alert("Login ID tidak sah");
            document.getElementById('txtLOGIN').value = "";
            document.getElementById('txtPWD').value = "";
            document.getElementById('txtLOGIN').focus(); 
    } else {
	    //Getting the value.
	    var inValL = objL.value;
        var inValP = objP.value;
        var randomnumber=Math.floor(Math.random()*9999);

	    var requestUrl = 'scripts/AjaxProcessL.aspx?valL=' + encodeURIComponent(inValL) + '&valP=' + encodeURIComponent(inValP) + '&valT=' +randomnumber;
	    CreateXmlHttp();
	    // If browser supports XMLHTTPRequest object
	    if(XmlHttp)
	    {
		    //Setting the event pointerler for the response
		    XmlHttp.onreadystatechange = pointerleL;		
		    //Initializes the request object with GET (METHOD of posting), 
		    //Request URL and sets the request as asynchronous.
		    XmlHttp.open("GET", requestUrl,  true);
    		
		    //Sends the request to server
		    XmlHttp.send(null);		
	    }
	}
}

function pointerleL()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{					    
		    //alert(XmlHttp.responseText);
			chkL(XmlHttp.responseXML.documentElement);				        	        			
		}
		else
		{
			//alert("There was a problem retrieving data from the server." );
			alert("Data Tidak Boleh Dicapai. Sila Cuba Lagi." );
		}
	}
}

function chkL(vNode)
{
        var valValue;
        var msgValue; 
        var valNodes = vNode.getElementsByTagName('DATA');
        for (var count = 0; count < valNodes.length; count++)
        {
            if (bBrowser)
            {
                valValue = valNodes[count].childNodes[0].textContent;
                msgValue = valNodes[count].childNodes[1].textContent;
            }   
            else 
            { 
                valValue = valNodes[count].childNodes[0].text;
                msgValue = valNodes[count].childNodes[1].text; 
            }
        } 
        if (valValue != '1')
        { 
            //alert('Nooo Cannot Apply!!!!' + ' ' + valValue + ' ' + msgValue)
            alert(msgValue);
            document.getElementById('txtLOGIN').value = "";
            document.getElementById('txtPWD').value = "";
            document.getElementById('txtLOGIN').focus(); 
        } 
        else
        { 
            window.location.href = "Main.aspx";
        } 
}

function validateInt(o)
   {
      switch (isInteger(o.value))
      {
         case true:
            iValid= 1;
            break;
         case false:
            iValid = 0;
      }
   }
  
  function isInteger (s)
   {
      var i;
      
      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
   }

   function isEmpty(s)
   {
      return ((s == null) || (s.length == 0))
   }

   function isDigit (c)
   {
      return ((c >= "0") && (c <= "9"))
   } 

