var xmlHttp;

function ajaxzipInit() {
	xmlHttp = getXmlHttpObject();
}

function handleHttpResponse() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        var xmlDoc = xmlHttp.responseXML;
		var ctvl;
		var opt, inpAdr1, inpAdr2;

        if (xmlDoc.documentElement) {
            var pref    = xmlDoc.getElementsByTagName('pref').item(0).firstChild;
            var city    = xmlDoc.getElementsByTagName('city').item(0).firstChild;
            var ville   = xmlDoc.getElementsByTagName('ville').item(0).firstChild;
            var param   = xmlDoc.getElementsByTagName('param').item(0).firstChild;
			opt = ((param != null) ? param.data : "") + "adr";
			inpAdr1 = opt + "1";
			inpAdr2 = opt + "2";
            ctvl = ( (city != null) ? city.data : '' ) + ( (ville != null) ? ville.data : '' );

            document.getElementById(inpAdr1).value = (pref != null) ? pref.data : '';
            document.getElementById(inpAdr2).value = ctvl;
        } else {
        }
    }
}

function getAddrss(opt) {
    var zip = document.getElementById(opt + 'zip').value;

	zip = zip.replace('-','');
    xmlHttp.open('GET', './zipcode/?zip=' + escape(zip) + '&prm=' + opt, true);
    xmlHttp.onreadystatechange = handleHttpResponse;
    xmlHttp.send(null);
}

function getXmlHttpObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
    @else
        xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
            xmlhttp.overrideMimeType("text/xml"); 
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
