/**
		 * Copyright (c) 2005 New Wireless Services,Inc.
		 *
		 * All rights reserved.These computer programs,listings, and 
		 * specifications are the property of New Wireless Services,
		 * Inc.and may not be copied ,stored,used or transimitted,in
		 * whole or in part,in any for or by any means,without the
		 * prior written permission of New Wireless Services,Inc.
		 
		*/
		
        /**
    Constructor for the AjaxRequest class. 

    <br><br>
    Example:

    <br><br>
    var ajaxRequest = new AjaxRequest("YOUR_URL");

    @class The AjaxRequest object wraps an instance of XMLHttpRequest and provides 
    facilities for setting functions that are called before a request is made
    and after a request returns. By default, AjaxRequest handles the server
    response by simply calling eval(), passing to it the responseText from 
    the XMLHttpRequestObject, of course assuming that the response was 
    generated by Taconite on the server side and that running eval() will 
    update the web page.<br><br>Example Usage:<br><br>var ajaxRequest = new AjaxRequest("YOUR_URL");
    <br>ajaxRequest.addFormElements("form_element_id_attribute_value");
    <br>ajaxRequest.sendRequest();

    @constructor
    @param {String} a String repesenting the URL to which the Ajax request
    will be sent.
*/
function AjaxRequest(url) {
    /** @private */
    var self = this;

    /** @private */
    var xmlHttp = createXMLHttpRequest();
    
    /** @private */
    var queryString = "";

    /** @private */
    var requestURL = url;

    /** @private */
    var method = "POST";

    /** @private */
    var preRequest = null;

    /** @private */
    var postRequest = null;

    /** @private */
    var debugResponse = false;
	
    /** @private */
    var async = true;

    /** @private errorHandler*/ 
    var errorHandler = null;


    /**
        Return the instance of the XMLHttpRequest object wrapped by this object.
        @return XMLHttpRequest
    */
    this.getXMLHttpRequestObject = function() {
        return xmlHttp;
    }

    /**
        Set the pre-request function. This function will be called prior to 
        sending the Ajax request. The pre-request function is passed a reference
        to this object.
        @param {Function} The function to be called prior to sending the Ajax
        request. The function is passed a refernce of this object.
    */
    this.setPreRequest = function(func) {
        preRequest = func;
    }

    /**
        Set the post-request function. This function will be called after the
        response has been received and after eval() has been called using the 
        XMLHttpRequest object's responseText. The post-request function is passed 
        a reference to this object.
        @param {Function} The function to be called after receiving the Ajax
        response. The function is passed a refernce of this object.
    */
    this.setPostRequest = function(func) {
        postRequest = func;
    }

    /**
        Send the Ajax request using the POST method. Use with caution -- some
        browsers do not support the POST method with the XMLHttpRequest object.
    */
    this.setUsePOST = function() {
        method = "POST";
    }

    /**
        Send the Ajax request using the GET method, where parameters are sent
        as a query string appended to the URL. This is the default behavior.
    */
    this.setUseGET = function() {
        method = "GET";
    }

    /**
        Enable client-side debugging.  The server's response will be written
        to a text area appended to the bottom of the page.  If parsing is
        performed on the client side, then the results of the parsing operations
        are shown in their own text areas.
    */
    this.setEchoDebugInfo = function() {
        debugResponse = true;
    }

    /**
        Indicate if debugging is enabled.
        @return boolean
    */
    this.isEchoDebugInfo = function() {
        return debugResponse;
    }

    /**
        Set the query string that will be sent to the server. For GET
        requests, the query string is appended to the URL. For POST
        requests, the query string is sent in the request body. This 
        method is useful, for example, if you want to send an XML string
        or JSON string to the server.
        @param {String} qa, the new query string value.
    */
    this.setQueryString = function(qs) {
        queryString = qs;
    }

    /**
        Return the query string.
        @return The query string.
    */
    this.getQueryString = function() {
        return queryString;
    }

    /** 
        @param {Boolean} asyncBoolean, set to true if asynchronous request, false synchronous request. 
    */
    this.setAsync = function(asyncBoolean){
            async = asyncBoolean;
    }

    /** 
        @param {Function} Set the error handler function that is called if the 
        server's HTTP response code is something other than 200.
    */	
    this.setErrorHandler = function(func){
        errorHandler = func;
    }

    /**
        Send the Ajax request.
    */
    this.sendRequest = function(contextID) {
        if(preRequest) {
            preRequest(self);
        }

        var obj = this;

        xmlHttp.onreadystatechange = function () { processStateChange(self,contextID) };

        if(requestURL.indexOf("?") > 0) {
            requestURL = requestURL + "&ts=" + new Date().getTime();
        }
        else {
            requestURL = requestURL + "?ts=" + new Date().getTime();
        }


        if(method == "GET") {
            if(queryString.length > 0) {
                requestURL = requestURL + "&" + queryString;
            }
            xmlHttp.open(method, requestURL, true);
            xmlHttp.send(null);
        }
        else {
            //Fix a bug in Firefox when posting  //removed by ken Firefox has fixed this issue
            //if (xmlHttp.overrideMimeType) {
            //   xmlHttp.setRequestHeader("Connection", "close");
            //}
            xmlHttp.open(method, requestURL, true);
            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
            xmlHttp.send(queryString);
        }

        if(!async) {  //synchronous request, handle the state change
            processStateChange(self,contextID);
        }

        if(self.isEchoDebugInfo()) {
            echoRequestParams();
        }
    }
	
	// THIS IS THE CALLBACK FUNCTION
 processStateChange =function(ajaxRequest,contextID) {
 
     if (ajaxRequest.getXMLHttpRequestObject().readyState == 4) { // Complete
      if (ajaxRequest.getXMLHttpRequestObject().status == 200) { // OK response
     // setValues(ajaxRequest.getXMLHttpRequestObject().responseText)
    	//alert(ajaxRequest.getXMLHttpRequestObject().responseText);
        document.getElementById(contextID).innerHTML = ajaxRequest.getXMLHttpRequestObject().responseText;
       
       //  document.getElementById("org").style.visibility="hidden"
      } else {
     	//document.getElementById(processID).style.visibility="hidden"
       // alert("Problem: " + ajaxRequest.getXMLHttpRequestObject().statusText);
      }
    }
 }
    /** @private */
    function echoRequestParams() {
        var qsTextBox = document.getElementById("qsTextBox");
        if(qsTextBox == null) {
            qsTextBox = createDebugTextBox("Query String:", "qsTextBox");
        }
        qsTextBox.value = queryString;

        var urlTextBox = document.getElementById("urlTextBox");
        if(urlTextBox == null) {
            urlTextBox = createDebugTextBox("URL (Includes query string if GET request):", "urlTextBox");
        }
        urlTextBox.value = requestURL;
    }

    /** @private */
    function createDebugTextBox(label, id) {
        textBox = document.createElement("input");
        textBox.setAttribute("type", "text");
        textBox.setAttribute("id", id);
        textBox.setAttribute("style", "width:100%");
        textBox.style.cssText = "width:100%";

        document.getElementsByTagName("body")[0].appendChild(document.createTextNode(label));
        document.getElementsByTagName("body")[0].appendChild(textBox);
        return textBox;
    }


}


/**
    Create an instance of the XMLHttpRequest object, using the appropriate
    method for the type of browser in which this script is running. For Internet
    Explorer, it's an ActiveX object, for all others it's a native JavaScript
    object.
    @return an instance of the XMLHttpRequest object.
*/
function createXMLHttpRequest() {
    var req = false;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
       	try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
      	}
        catch(e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {
                req = false;
            }
        }
    }
    return req;
}
	
function getAvailableNumbers(url){

	var ajaxRequest = new AjaxRequest(url); //Create AjaxRequest object
    ajaxRequest.sendRequest("numberSelection","getMobileProcess"); //Send the request
}
 
function selectedNbrChangeMDN(topNbr,iValue)	{


	var recValue = topNbr+"";
	var selectedValue = recValue.substring(0,iValue);
	javascript:getAvailableNumbers('changeMSISDN.do?dispmethd=call&nbr='+selectedValue);
}

function selectedNbrRegister(topNbr,iValue)	{
	var recValue = topNbr+"";
	var selectedValue = recValue.substring(0,iValue);
	javascript:getAvailableNumbers('selfregister.do?dispmethd=save&getmsisdn=true&numselection=enable&nbr='+selectedValue);
}
function selectedNbrNewMDN(topNbr,iValue)	{
	var recValue = topNbr+"";
	var selectedValue = recValue.substring(0,iValue);
	javascript:getAvailableNumbers('newmsisdn.do?dispmethd=call&nbr='+selectedValue);
}
