﻿// JScript File

function kd() {
	if (event.keyCode==13) {
        //shoot();
        return false;
	}
}


function ReceiveServerData(arg, context)
{
    document.getElementById(_controlID + 'litLookupStatus').style.visibility='hidden';

    var a = arg.split("|");

    if (a[0] == 'Found')
    {
        _frm.elements[_controlName+_questionControlPrefix+'Address'].value = a[1];
    }
    else if (a[0] == 'NotFound')
    {
        alert(a[1]);
    }
    else if (a[0] == 'MultipleFound')
    {
        for (var jj=document.getElementById(_controlID+'selectPostcode').childNodes.length-1;jj>=0;jj--)
        {
            document.getElementById(_controlID+'selectPostcode').removeChild(document.getElementById(_controlID+'selectPostcode').childNodes[jj]);
        }

        var li = document.createElement("li");
        li.innerHTML = '<b>Several matches.</b>'; 
        li.style.listStyle = "none";
        li.style.textAlign = "left";
        var li2 = document.createElement("li");
        li2.innerHTML = '<b>Please select one</b>'; 
        li2.style.listStyle = "none";
        li2.style.textAlign = "left";
        

        document.getElementById(_controlID+'selectPostcode').appendChild(li);
        document.getElementById(_controlID+'selectPostcode').appendChild(li2);

        for (var i=1;i<a.length-1;i++)
        {
            var li = document.createElement("li");
            li.style.textAlign = "left";
            li.innerHTML = a[i]; 
            li.style.listStyle = "none";
            li.style.cursor = "pointer";
            //li.setAttribute("onclick","LookupWithKey('"+a[i]+"')"); // only works in FF
            eval( "li.onclick = function() {LookupWithKey('" + a[i] + "');}" ); // hack to make it work in IE
            document.getElementById(_controlID+'selectPostcode').appendChild(li);
        }
    }
    else
    {
        alert(arg);
    }
}


function ProcessCallBackError(arg, context)
{
    document.getElementById(_controlID + 'litLookupStatus').style.visibility='hidden';
    alert("error:" +arg);
}
var _controlName;// ex  'Wizard1$ctl12$'
var _controlID;// ex  'Wizard1_ctl12_'
var _frm;
var _questionControlPrefix
function Lookup(btn, questionControlPrefix)
{
    //QuestionControlPrefix typically contains answers*12* where 12 is the question number

    _frm = btn.form; // store ref to current form (since there might be more than one form with a lookup function)
    _controlName = btn.name.substring(0,btn.name.lastIndexOf('$')+1 ); // store the containing control name (as there might be more than one lookup control)
    _controlID = _controlName.replace(/\$/g,"_"); // store the containing control id
    _questionControlPrefix = questionControlPrefix;

    _frm.elements[_controlName+_questionControlPrefix+'Address'].value = ''; // clear prev results
    var postCode = _frm.elements[_controlName+_questionControlPrefix+'PostCode'].value;
    var premises = _frm.elements[_controlName+_questionControlPrefix+'Premises'].value;

//    var postCode = document.getElementById(_controlName+'PostCode').nodeValue;
//    var premises = document.getElementById(_controlName+'Premises').nodeValue;
//    document.getElementById(_controlName+'Address').nodeValue = '';


    document.getElementById(_controlID + 'litLookupStatus').style.visibility='';// show status "working"

    CallTheServer(postCode+'|'+premises)
}
function LookupWithKey(strKey)
{
    _frm.elements[_controlName+_questionControlPrefix+'Address'].value = ''; // clear prev results

    document.getElementById(_controlID + 'litLookupStatus').style.visibility='';// show status "working"

    CallTheServer(strKey)
}
