﻿/* AJAX CLASS */
var jm_HttpRequest = null;
var jm_oPnr = null;
var jm_oBynavn = null;
    
function jm_ajax()
{
    this.jm_sPath = "";
    this.oDoc = null;
    
    /*funktioner*/
    this.executeRequest = jm_executeRequest;
    this.asyncResult = jm_asyncResult;
    this.setPath = jm_setPath;
    this.setPathObj = jm_setPathObj;
    this.init = jm_initHTTP;
    
}

function jm_initHTTP()
{
    if (window.ActiveXObject) 
    {
        try {
            jm_HttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
            try 
            {
                jm_HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
    }
    else if (typeof(XMLHttpRequest)!= 'undefined')
    {
        jm_HttpRequest = new XMLHttpRequest();
        if (jm_HttpRequest.overrideMimeType)
        {
            jm_HttpRequest.overrideMimeType('text/xml');
        }
    }
    if (!jm_HttpRequest)
    {
        return false;
    }
}

function jm_setPathObj(s, oPnrObj, oBynavnObj)
{
    this.jm_sPath = 'http://' + s + '/gaarde_2007_getCityName.asp?postnr=';    
    this.jm_sPath = '/gaarde_2007_getCityName.asp?postnr='; 
    
    jm_oPnr = oPnrObj;
    jm_oPnr.setAttribute("a_path", this.jm_sPath);
    jm_oBynavn = oBynavnObj;
    
    jm_bindings(jm_oPnr);
}

function jm_setPath(s, sPnr, sBynavn)
{
    this.jm_sPath = 'http://' + s + '/gaarde_2007_getCityName.asp?postnr=';   
    this.jm_sPath = '/gaarde_2007_getCityName.asp?postnr=';  
    
    jm_oPnr = document.getElementById(sPnr);
    jm_oPnr.setAttribute("a_path", this.jm_sPath);
    jm_oBynavn = document.getElementById(sBynavn);
    
    jm_bindings(jm_oPnr);
}

function jm_bindings(jm_oPnr)
{
    jm_oPnr.onchange = jm_postnr_change;
    jm_oPnr.onblur = jm_postnr_change;
    jm_oPnr.onclick = jm_postnr_change;
    jm_oPnr.onkeyup = jm_postnr_change;
}

function jm_postnr_change()
{
    jm_initHTTP();
    var s = jm_oPnr.value.replace(' ',''); 
    if( s.length == 4 )
    {
        if( "Post nr.:" != s )
            jm_oBynavn.value = "Ukendt";
        jm_executeRequest(s, jm_oPnr.getAttribute("id"));
    }
    else
    {
        if( "Post nr.:" != s )
            jm_oBynavn.value = "Ukendt";
    }
}

function jm_callBack(oDoc)
{
    if( oDoc != null )
    {
        if( oDoc.documentElement.tagName == "cityname" )
            jm_oBynavn.value = oDoc.documentElement.text;
        else
            if( "Post nr.:" != s )
                jm_oBynavn.value = "Ukendt";
    }
}

function jm_asyncResult()
{
    try
    {            
        if(jm_HttpRequest.readyState == 4)
        {
            if( jm_HttpRequest.status == 200 )
            {           
                if (window.ActiveXObject)
                {
                    oDoc = new ActiveXObject("Microsoft.XMLDOM");
                    oDoc.async = false;
                    oDoc.loadXML(jm_HttpRequest.responseXML.xml);
                    jm_callBack(oDoc);
                }
                else if (document.implementation && document.implementation.createDocument)
                {
                    oDoc = document.implementation.createDocument("","",null);
                    oDoc.onload = jm_HttpRequest.responseXML.xml;
                    jm_callBack(oDoc);
                }
                else
                {
                    alert("This webpage uses scripts that are not supported by your browser.\nWe recommend updating your browser.");
                }   
            }
            else
            {
                alert( 'Fejl returneret:\n' + jm_HttpRequest.responseText );
            }
        }
    }
    catch (e)
    {
        alert( "jm_asyncResult:" + e.description );
        alert( jm_HttpRequest.responseText );
    }
}

function jm_executeRequest(sPostNr, sID)
{
    if(jm_HttpRequest.readyState == 4 || jm_HttpRequest.readyState == 0)
    {
        var thisSPath = jm_oPnr.getAttribute("a_path");
        if(thisSPath)
        {
            try
            {
                jm_HttpRequest.onreadystatechange = jm_asyncResult; 
                jm_HttpRequest.open('GET', thisSPath + sPostNr, true);
                jm_HttpRequest.send(null);
            }
            catch (e)
            {
                alert("jm_executeRequest:\n" + e.description);
                alert( jm_HttpRequest.responseText );
            }
        }
        else
        {
            alert("Request is missing a path");
        }
    }
}
