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);
		//alert("1: "+res);
		if(res == -1){
			// attempt to set the value of the existing item
			res = TelML.PermanentApplicationData.Item(itemName).SetValue(itemValue);
			//alert(TelML.PermanentApplicationData.Item(itemName) + "\n2: "+res);
		}
	}else{
		if(document.getElementById(itemName))
			document.getElementById(itemName).innerHTML = itemValue;
		if(parent.document.getElementById(itemName))
			parent.document.getElementById(itemName).innerHTML = 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; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return strReturn;
}

//=== Application URL and datastore parameters ===================================================
function getPhonebookURLParamString(){
    // This function returns the string of the format:
    // "Extension=1907&ExchangePassword=5550IPc&Exchange=1"
    // This string can then be appended to the URL redirect address as a set of URL parameters
    var exch = getURLParam("Exchange");
	var ext = ParseExtension();
	var pwd = GetDatastoreItem("TradersPwdFor"+ext,"");
	var url_params = "Extension=" + ext + "&Password=" + pwd;
	url_params += (exch == null || exch == "") ? "" : "&Exchange=" + exch;
	return url_params;
}

function getPhonebookURLParamStringInvertExchange(){
    // This function returns the string of the format:
    // "Extension=1907&ExchangePassword=5550IPc&Exchange=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("Exchange");
	var ext = ParseExtension();
	var pwd = GetDatastoreItem("TradersPwdFor"+ext,"");
	var url_params = "Extension=" + ext + "&Password=" + pwd;
	url_params += (exch == null || exch == "") ? "&Exchange=1" : "";
	return url_params;
}

function GetPasswordFromDatastore(){
	var pwd;
	if(parent.TelML){
		var ext = ParseExtension();
		pwd = GetDatastoreItem("TradersPwdFor"+ext,"");
	}else{
		// this will work on PC and on Demo page only.
		pwd = parent.document.getElementById('PasswordHidden').innerHTML;
	}
	return pwd;
}

function SavePasswordToDatastore(new_pwd){
    if(parent.TelML){
		var ext = ParseExtension();
		SaveDatastoreItem("TradersPwdFor"+ext, new_pwd);
	}else{
		parent.document.getElementById('Password').value = new_pwd;
		parent.document.getElementById('PasswordHidden').innerHTML = new_pwd;
	}
}