function GetDatastoreItem(itemName, defaultValue){
	// This function returns the value of the item (with key itemName)
	// stored in the TelML persistent datastore
	// If such an item doesn't exist it returns defaultValue.
	var res;
	if(parent.TelML){
		if (TelML.PermanentApplicationData.QueryKey(itemName)){
			res = TelML.PermanentApplicationData.Item(itemName).GetValue();
		}else{
			res = defaultValue;
		}
	}else
		res = defaultValue;
	return res;
}

function SaveDatastoreItem(itemName, itemValue){
	// This function saves the itemValue to the 
	// permanent application datastore under the name itemName
	if(parent.TelML){
		// attempt to add new item into built-in TelML datastore 
		var res = TelML.PermanentApplicationData.AddItem(itemName, itemValue);
		if(res == -1){
			// attempt to set the value of the existing item
			res = TelML.PermanentApplicationData.Item(itemName).SetValue(itemValue);
		}
	}
}

//=== GENERAL ==================================================================
function getURLParam(strParamName){
	var strReturn = "";
	var strHref = window.location.href;
	if (strHref.indexOf("?") > -1){
		var strQueryString = strHref.substr(strHref.indexOf("?")+1);
		var aQueryString = strQueryString.split("&");
		for (var iParam=0, len=aQueryString.length; iParam < len; iParam++){
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return decodeURIComponent(strReturn);
}
function RedirectViaLoadingDiv(url, msg){
	var loading_div = document.getElementById("LoadingDiv");
	if(loading_div){
		document.getElementById("LoadingDiv").innerHTML = msg;
		document.getElementById("LoadingDiv").style.visibility = "visible";
	}
	if(url != null && url != "")
		document.location.href = url;
}
function encodeHtml(str) {
	str = str.replace(/ /g,"&nbsp;");
	str = str.replace(/</g,"&lt;");
	return str;
} 
//=== Application URL and datastore parameters ===================================================
function getIntDirectoryURLParamString(){
    // This function returns the string of the format:
    // "M=1907&P=5550IPc&E=1"
    // This string can then be appended to the URL redirect address as a set of URL parameters
    var exch = getURLParam("E");
    var url_params = "";
	if(parent.TelML){
		var ext = ParseExtension();
		var pwd = GetDatastoreItem("IntDirectoryPwdFor"+ext,"");
		url_params = "M=" + ext + "&P=" + pwd;
		url_params += (exch == null || exch == "") ? "" : "&E=" + exch;
	}else{
		// this will work on PC and on Demo page only.
		if(parent.document.getElementById('PasswordHidden')){
			url_params = "M=" + parent.document.getElementById("Extension").value + 
					"&P=" + parent.document.getElementById("PasswordHidden").innerHTML;
			url_params += (exch == null || exch == "") ? "" : "&E=" + exch;
		}
	}
	return url_params;
}

function getIntDirectoryURLParamStringInvertExchange(){
    // This function returns the string of the format:
    // "M=1907&P=5550IPc&E=1".
    // It inverts the value of the Exchnage parameter (makes it empty if it wasn't and visa versa).
    // This string can then be appended to the URL redirect address as a set of URL parameters
    var exch = getURLParam("E");
	var ext = ParseExtension();
	var pwd = GetDatastoreItem("IntDirectoryPwdFor"+ext,"");
	var url_params = "M=" + ext + "&P=" + pwd;
	url_params += (exch == null || exch == "") ? "&E=1" : "";
	return url_params;
}

function GetPasswordFromDatastore(){
	var pwd;
	if(parent.TelML){
		var ext = ParseExtension();
		pwd = GetDatastoreItem("IntDirectoryPwdFor"+ext,"");
	}else{
		// this will work on PC and on Demo page only.
		if(parent.document.getElementById('PasswordHidden')){
			pwd = parent.document.getElementById('PasswordHidden').innerHTML;
		}
	}
	return pwd;
}

function SavePasswordToDatastore(new_pwd){
    if(parent.TelML){
		var ext = ParseExtension();
		SaveDatastoreItem("IntDirectoryPwdFor"+ext, new_pwd);
	}else{
		if(parent.document.getElementById('PasswordHidden')){
			parent.document.getElementById('Password').value = new_pwd;
			parent.document.getElementById('PasswordHidden').innerHTML = new_pwd;
		}
	}
}