var json_common_version = "1.0.0";
/*
************************************************
 * json_common_version.js
 * For json common utility functions
 * Note: Do not put any comments as the starting line
************************************************
*/

// Check is the value is a URI
function getValueOfPropertyFromSimpleJsonObject(jsonObj, property)
{
    var myJsonObj = jsonObj;
    var x;
    var result = '';
    if ( jsonObj != undefined && property != undefined ){
    	for (x in myJsonObj) {
        	if (x == property) {
        		result = jsonObj[x];
        		break;
        	}    	
        }
    }  
   return result;
}

function getValueOfPropertyFromArrayOfSimpleJsonObject(arraySimpleJsonObj, property) {
	var myArrayJsonObj = arraySimpleJsonObj;
	var myArrayLength = myArrayJsonObj.length;
	var result = null;
	for (i in arraySimpleJsonObj) {
		result = getValueOfPropertyFromSimpleJsonObject(arraySimpleJsonObj[i], property);
		if ( result != null) {
			break;		
		}
	}
	return result;	 
}


function getLast4Digits(str) {
	var res = "";
	if (str != null || str.length >= 4) {
		res = str.slice(str.length-4, str.length);
	}
	return res;
}

function determineNdmLibraryName(inputMethod, languageSelected, moduleName) {
	var retCode = "";

	if (inputMethod != null || languageSelected != null) { 
		if (inputMethod=="dtmf" && languageSelected.toLowerCase()=="en-us") {
			retCode = moduleName + "_dtmf";
		} else if (inputMethod=="dtmf" && languageSelected.toLowerCase()=="es-us") {
			retCode = moduleName + "_dtmf";
		} else {
        	if (inputMethod=="speech" && languageSelected.toLowerCase()=="en-us") {
				retCode = moduleName;
            } else if (inputMethod=="speech" && languageSelected.toLowerCase()=="es-us"){
            	retCode = moduleName;
            }
		}
	}	
	return retCode;
}

function getCardType(ServiceableDebitCardAccountTypesJsinArrayObjects, cardType) {
	var myArrayJsonObj = ServiceableDebitCardAccountTypesJsinArrayObjects;
	/*
	 * Will need to use this variable if the in operand is doesn't work.  var myArrayLength = myArrayJsonObj.length;
	 */
	var result = "credit_card";
	for (i in ServiceableDebitCardAccountTypesJsinArrayObjects) {
		if ( ServiceableDebitCardAccountTypesJsinArrayObjects[i] == cardType) {
			result = "debit_card";
			break;	
		}
	}
	return result;
}

function determineDynGrammarFile(inputMethod, languageSelected, moduleName) {
	var retCode = "";
    var dir = "/CollectionIVR/data/grammars/"+languageSelected+"/";
	if (inputMethod != null || languageSelected != null) { 
		if (inputMethod=="dtmf" && languageSelected.toLowerCase()=="en-us") {
			retCode = dir + moduleName + "_dtmf.grxml";
		} else if (inputMethod=="dtmf" && languageSelected.toLowerCase()=="es-us") {
			retCode = dir + moduleName + "_dtmf.grxml";
		} else {
        	if (inputMethod=="speech" && languageSelected.toLowerCase()=="en-us") {
				retCode = dir + moduleName + ".grxml";
            } else if (inputMethod=="speech" && languageSelected.toLowerCase()=="es-us"){
            	retCode = dir + moduleName + ".grxml";
            }
		}
	}	
	return retCode;
}

function determineFirstAddress(firstAddress) {
	var length = firstAddress.length;
	 var i;
	 var newString = '';
	 var stop = false;
	 
	 if (firstAddress == null || firstAddress == '') {
         return firstAddress;
     }
	 else{	
		 for (i=0; i< length && stop == false; i++)
		 {		
		var curChar = firstAddress.substring(i, i+1);
        if (isNaN(curChar)) {
            if (curChar == '-') {
                newString += curChar + ' ';
            } else {
                stop = true;               
               newString += firstAddress.substring(i) ;
            }
        }
        else {
            newString += curChar + ' ';
            if (firstAddress.substring(i + 1, 1) == ' ') {
                newString += firstAddress.substring(i + 1);
                stop = true;
            }
        }
	}
	 return newString;
	 }
}
















