// Script for the auto Populate from web service
// Function for adding suggestion functionality
// for text input fields in Microsoft CRM
// textfield: document.getElementById('companyname')
// method: a function, that returns an array of strings
// preload: true or false
function SuggestionTextBox(textfield, method, preload)
{ // max items in the suggestion box
var maxItems = 6;
this.suggestionList = new Array();
this.suggestionListDisplayed = new Array();
var actual_textfield = textfield;
var actual_value = '';
var selectedNumber = 0;
var countMatches = 0;
if (preload)
{
// load the data via external method
this.suggestionList = method();
}
// attach this function to the textfield
textfield.attachEvent("onfocus", initTextfield);
// init textfield and attach necessary events
function initTextfield()
{
// when leaving the field we have to clear our site
textfield.attachEvent("onblur", resetTextfield);
document.attachEvent("onkeydown", keyDown);
}
function resetTextfield(e)
{
//when leaving the field, we have to remove all attached events
document.detachEvent("onkeydown", keyDown);
textfield.detachEvent("onblur",resetTextfield);
}
function keyDown(e)
{
keyCode = e.keyCode;
switch (keyCode)
{
case 9: case 13:
// enter & tab key
if (countMatches > 0)
{
actual_textfield.value = suggestionListDisplayed[selectedNumber];
if (document.getElementById('suggestion_table') != null)
{
document.body.removeChild(document.getElementById('suggestion_table'));
}
}
break;
case 38:
//pressing up key
if(selectedNumber > 0 && countMatches > 0)
{
selectedNumber--;
createSuggestionTable();
}
return false;
break;
case 40:
// pressing down key
if(selectedNumber < countMatches-1 && countMatches > 0 && selectedNumber < maxItems)
{
selectedNumber++;
createSuggestionTable();
}
return false;
break;
default:
// do not call the function to often
setTimeout(
function()
{
executeSuggestion(keyCode)
}, 200 /* in ms */
);
break;
}
}
function executeSuggestion(keyCode)
{
selectedNumber = 0;
countMatches = 0;
actual_value = textfield.value;
//todo add keyCode
// get all possible values from the suggestionList
if (!preload)
{
// load the data via external method
// todo add some caching function
this.suggestionList = method();
}
// using regular expressions to match it against the suggestion list
var re = new RegExp(actual_value, "i");
//if you want to search only from the beginning
//var re = new RegExp("^" + actual_value, "i");
countMatches = 0;
this.suggestionListDisplayed = new Array();
// test each item against the RE pattern
for (i = 0; i < this.suggestionList.length; i++)
{
// if it matche add it to suggestionListDisplayed array
if (re.test(this.suggestionList[i]) && actual_value != '')
{
this.suggestionListDisplayed[countMatches] = this.suggestionList[i];
countMatches++;
// if there are more values than in maxItems, just break
if (maxItems == countMatches)
break;
}
}
if (countMatches > 0)
{
createSuggestionTable();
}
else
{
if (document.getElementById('suggestion_table'))
{
document.body.removeChild(document.getElementById('suggestion_table'));
}
}
}
function createSuggestionTable()
{
if (document.getElementById('suggestion_table'))
{
document.body.removeChild(document.getElementById('suggestion_table'));
}
// creating a table object which holds the suggesions
table = document.createElement('table');
table.id = 'suggestion_table';
table.width = actual_textfield.style.width;
table.style.position= 'absolute';
table.style.zIndex = '100000';
table.cellSpacing = '1px';
table.cellPadding = '2px';
topValue = 0;
objTop = actual_textfield;
while(objTop)
{
topValue += objTop.offsetTop;
objTop = objTop.offsetParent;
}
table.style.top = eval(topValue + actual_textfield.offsetHeight) + "px";
leftValue = 0;
objLeft = actual_textfield
while(objLeft)
{
leftValue += objLeft.offsetLeft;
objLeft = objLeft.offsetParent;
}
table.style.left = leftValue + "px";
table.style.backgroundColor = '#FFFFFF';
table.style.border = "solid 1px #7F9DB9";
table.style.borderTop = "none";
document.body.appendChild(table);
// iterate list to create the table rows
for ( i = 0; i < this.suggestionListDisplayed.length; i++)
{
row = table.insertRow(-1);
row.id = 'suggestion_row' + (i);
column = row.insertCell(-1);
column.id = 'suggestion_column' + (i);
if (selectedNumber == i)
{
column.style.color = '#ffffff';
column.style.backgroundColor = '#316AC5';
}
else
{
column.style.color = '#000000';
column.style.backgroundColor = '#ffffff';
}
column.style.fontFamily = 'Tahoma';
column.style.fontSize = '11px';
column.innerHTML = this.suggestionListDisplayed[i];
}
}
// return object
return this;
}
var countries = new Array();
var obj;
var f = function GetListOfCountries()
{
return countries;
}
function CallWebService()
{
var objHttp;
// create an XmlHttp instance
objHttp = new ActiveXObject("Microsoft.XMLHTTP");
// Create the SOAP Envelope
strEnvelope = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance/"" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema/"" +
" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope//">" +
" <soap:Body>" +
" <" + "GetListOfCountries" + " xmlns=\"http://webservices//">" +
" </" + "GetListOfCountries" + ">" +
" </soap:Body>" +
"</soap:Envelope>";
// Set up the post
objHttp.onreadystatechange = function()
{
// a readyState of 4 means we're ready to use the data returned by XMLHTTP
if (objHttp.readyState == 4)
{
// get the return envelope
var szResponse = objHttp.responseText;
var startTag = "<string xmlns=\"http://webservices//">";
var endTag = "</string>";
var cities;
var valueStart = 0;
var valueEnd = 0;
//Parsing the returned XML
valueStart = objHttp.responseXML.xml.indexOf(startTag, valueEnd) + startTag.length;
valueEnd = objHttp.responseXml.xml.indexOf(endTag, valueEnd+1);
var tmpCountries = objHttp.responseXML.xml.substring(valueStart, valueEnd);
countries = tmpCountries.split(',,');
obj = SuggestionTextBox(document.getElementById('companyname'), f, true);
}
}
var szUrl;
szUrl = "http://localhost/webservice2/service1.asmx/GetListOfCountries";
// send the POST to the Web service
objHttp.open("POST", szUrl, true);
objHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
objHttp.send(strEnvelope);
}
CallWebService();
// Function for adding suggestion functionality
// for text input fields in Microsoft CRM
// textfield: document.getElementById('companyname')
// method: a function, that returns an array of strings
// preload: true or false
function SuggestionTextBox(textfield, method, preload)
{ // max items in the suggestion box
var maxItems = 6;
this.suggestionList = new Array();
this.suggestionListDisplayed = new Array();
var actual_textfield = textfield;
var actual_value = '';
var selectedNumber = 0;
var countMatches = 0;
if (preload)
{
// load the data via external method
this.suggestionList = method();
}
// attach this function to the textfield
textfield.attachEvent("onfocus", initTextfield);
// init textfield and attach necessary events
function initTextfield()
{
// when leaving the field we have to clear our site
textfield.attachEvent("onblur", resetTextfield);
document.attachEvent("onkeydown", keyDown);
}
function resetTextfield(e)
{
//when leaving the field, we have to remove all attached events
document.detachEvent("onkeydown", keyDown);
textfield.detachEvent("onblur",resetTextfield);
}
function keyDown(e)
{
keyCode = e.keyCode;
switch (keyCode)
{
case 9: case 13:
// enter & tab key
if (countMatches > 0)
{
actual_textfield.value = suggestionListDisplayed[selectedNumber];
if (document.getElementById('suggestion_table') != null)
{
document.body.removeChild(document.getElementById('suggestion_table'));
}
}
break;
case 38:
//pressing up key
if(selectedNumber > 0 && countMatches > 0)
{
selectedNumber--;
createSuggestionTable();
}
return false;
break;
case 40:
// pressing down key
if(selectedNumber < countMatches-1 && countMatches > 0 && selectedNumber < maxItems)
{
selectedNumber++;
createSuggestionTable();
}
return false;
break;
default:
// do not call the function to often
setTimeout(
function()
{
executeSuggestion(keyCode)
}, 200 /* in ms */
);
break;
}
}
function executeSuggestion(keyCode)
{
selectedNumber = 0;
countMatches = 0;
actual_value = textfield.value;
//todo add keyCode
// get all possible values from the suggestionList
if (!preload)
{
// load the data via external method
// todo add some caching function
this.suggestionList = method();
}
// using regular expressions to match it against the suggestion list
var re = new RegExp(actual_value, "i");
//if you want to search only from the beginning
//var re = new RegExp("^" + actual_value, "i");
countMatches = 0;
this.suggestionListDisplayed = new Array();
// test each item against the RE pattern
for (i = 0; i < this.suggestionList.length; i++)
{
// if it matche add it to suggestionListDisplayed array
if (re.test(this.suggestionList[i]) && actual_value != '')
{
this.suggestionListDisplayed[countMatches] = this.suggestionList[i];
countMatches++;
// if there are more values than in maxItems, just break
if (maxItems == countMatches)
break;
}
}
if (countMatches > 0)
{
createSuggestionTable();
}
else
{
if (document.getElementById('suggestion_table'))
{
document.body.removeChild(document.getElementById('suggestion_table'));
}
}
}
function createSuggestionTable()
{
if (document.getElementById('suggestion_table'))
{
document.body.removeChild(document.getElementById('suggestion_table'));
}
// creating a table object which holds the suggesions
table = document.createElement('table');
table.id = 'suggestion_table';
table.width = actual_textfield.style.width;
table.style.position= 'absolute';
table.style.zIndex = '100000';
table.cellSpacing = '1px';
table.cellPadding = '2px';
topValue = 0;
objTop = actual_textfield;
while(objTop)
{
topValue += objTop.offsetTop;
objTop = objTop.offsetParent;
}
table.style.top = eval(topValue + actual_textfield.offsetHeight) + "px";
leftValue = 0;
objLeft = actual_textfield
while(objLeft)
{
leftValue += objLeft.offsetLeft;
objLeft = objLeft.offsetParent;
}
table.style.left = leftValue + "px";
table.style.backgroundColor = '#FFFFFF';
table.style.border = "solid 1px #7F9DB9";
table.style.borderTop = "none";
document.body.appendChild(table);
// iterate list to create the table rows
for ( i = 0; i < this.suggestionListDisplayed.length; i++)
{
row = table.insertRow(-1);
row.id = 'suggestion_row' + (i);
column = row.insertCell(-1);
column.id = 'suggestion_column' + (i);
if (selectedNumber == i)
{
column.style.color = '#ffffff';
column.style.backgroundColor = '#316AC5';
}
else
{
column.style.color = '#000000';
column.style.backgroundColor = '#ffffff';
}
column.style.fontFamily = 'Tahoma';
column.style.fontSize = '11px';
column.innerHTML = this.suggestionListDisplayed[i];
}
}
// return object
return this;
}
var countries = new Array();
var obj;
var f = function GetListOfCountries()
{
return countries;
}
function CallWebService()
{
var objHttp;
// create an XmlHttp instance
objHttp = new ActiveXObject("Microsoft.XMLHTTP");
// Create the SOAP Envelope
strEnvelope = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance/"" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema/"" +
" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope//">" +
" <soap:Body>" +
" <" + "GetListOfCountries" + " xmlns=\"http://webservices//">" +
" </" + "GetListOfCountries" + ">" +
" </soap:Body>" +
"</soap:Envelope>";
// Set up the post
objHttp.onreadystatechange = function()
{
// a readyState of 4 means we're ready to use the data returned by XMLHTTP
if (objHttp.readyState == 4)
{
// get the return envelope
var szResponse = objHttp.responseText;
var startTag = "<string xmlns=\"http://webservices//">";
var endTag = "</string>";
var cities;
var valueStart = 0;
var valueEnd = 0;
//Parsing the returned XML
valueStart = objHttp.responseXML.xml.indexOf(startTag, valueEnd) + startTag.length;
valueEnd = objHttp.responseXml.xml.indexOf(endTag, valueEnd+1);
var tmpCountries = objHttp.responseXML.xml.substring(valueStart, valueEnd);
countries = tmpCountries.split(',,');
obj = SuggestionTextBox(document.getElementById('companyname'), f, true);
}
}
var szUrl;
szUrl = "http://localhost/webservice2/service1.asmx/GetListOfCountries";
// send the POST to the Web service
objHttp.open("POST", szUrl, true);
objHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
objHttp.send(strEnvelope);
}
CallWebService();
No comments:
Post a Comment