/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


var context = {  
  'paths': { 'prompt'   : '',
             'cityPrompt'   : '',
             'datePrompt'   : '',
             'timePrompt'   : '',
             'numberPrompt'   : '',
             'letterPrompt'   : '',
             'namePrompt' : '',
             'filmPrompt' : '',
             'cinemaPrompt' : '',
             'categoryPrompt' : '',
             'grammar'  : '',
             'data'     : ''
            },
  'vars': {'tries' : 0}
};

var max_errors = 3;
var confirmConfidence =  0.8;
var maxTry = 3;


//http://node1.nirvanix.com/GGMNrHoH~2MU7Ktn5f7~-p8CRQwD~coIjUvSqs0~Nvepvwk/FreeDrive/fd_80116/Prompts/is_that_right.wav?disposition=attachment

//##############################################################################

function promptPath(prompt) {
    if(PATHS[prompt])
        return PATHS[prompt];
    else
        return context.paths.prompt + prompt + '.wav';
}

function cityPromptPath(p){
    var prompt = p.toLowerCase();
    if(PATHS[prompt])
        return PATHS[prompt];
    else
        return context.paths.cityPrompt + prompt + '.wav';
}

function datePromptPath(prompt) {
    if(PATHS[prompt])
        return PATHS[prompt];
    else
        return context.paths.datePrompt + prompt + '.wav';
}

function namePromptPath(p) {
    var prompt = p.toLowerCase();
    if(PATHS[prompt])
        return PATHS[prompt];
    else
        return context.paths.namePrompt + prompt + '.wav';
}

function timePromptPath(prompt) {
    if(PATHS[prompt])
        return PATHS[prompt];
    else
        return context.paths.timePrompt + prompt + '.wav';
}

function numberPromptPath(prompt) {
    if(PATHS[prompt])
        return PATHS[prompt];
    else
        return context.paths.numberPrompt + prompt + '.wav';
}

function letterPromptPath(p) {
    var prompt = p.toLowerCase();
    if(PATHS[prompt])
        return PATHS[prompt];
    else
        return context.paths.letterPrompt + prompt + '.wav';
}

function filmPromptPath(p) {
    var prompt = p.toLowerCase();
    if(PATHS[prompt])
        return PATHS[prompt];
    else
        return context.paths.filmPrompt + prompt + '.wav';
}

function categoryPromptPath(p) {
    var prompt = p.toLowerCase();
    if(PATHS[prompt])
        return PATHS[prompt];
    else
        return context.paths.categoryPrompt + prompt + '.wav';
}

function cinemaPromptPath(p) {
    var prompt = p.toLowerCase();
    if(PATHS[prompt])
        return PATHS[prompt];
    else
        return context.paths.cinemaPrompt + prompt + '.wav';
}

function promptValue(prompt) {
    if(PROMPTS[prompt])
        return PROMPTS[prompt];
    else
        return prompt;
}

//##############################################################################

function GetData(d) {
    try {
        var result = d.documentElement.getElementsByTagName('result').item(0).firstChild.data;
        result = result.trim(); //la funcion trim para quitar los espacios
    }
    catch(e) {        
        result = "{}";    
    }
    bevocal.log('data = '+ result); //funcion de la Nuance
    return  eval("(" + result  + ")"); //convierte una cadena a un objeto
}

//##############################################################################

function replaceQuestionMark(jsonString) {
      return jsonString.replace(/\?/g,'@');
}

//##############################################################################

function isValidCategory(category,json){
    for(var i=0;i<json.length;i++)
        if (json[i].name == category ) 
            return true;
        
    return false;
}

//##############################################################################

/*function isValidDate(date){

    var myDate = new String(date);
    var currentDate = new Date();
    var year = new String(myDate.substring(0,(myDate.length)-5));
    var month = new String(myDate.substring((myDate.length)-4,(myDate.length)-3));
    var day = new String(myDate.substring((myDate.length)-2,(myDate.length)-1));

    if (isNaN(year) || year.length<4 || parseFloat(year)<1900){
        return false;
    }
    if (isNaN(month) || parseFloat(month)<1 || parseFloat(month)>12){
        return false;
    }
    if (isNaN(day) || parseInt(day, 10)<1 || parseInt(day, 10)>31){
        return false;
    }
    if (month==4 || month==6 || month==9 || month==11 || month==2) {
        if ((month==2 && day > 28) || day>30) {
            return false;
        }
    }
    if (myDate < currentDate){
        return false;
    }

 return true;
}*/

//##############################################################################

function GetFilmForSelectedCategory(category,json){
   var result = [];
   var k = 0;
     for(var i=0;i<json.length;i++){
         if (json[i].category == category){
             result[k] = json[i];
             k++;
          }
     }
    return result;
}

//##############################################################################

String.prototype.trim = function() {
                          return this.replace(/^\s+|\s+$/g, '');
                          
}

//##############################################################################

function isValidDate(date){
    
   var currentDate = new Date();
   var year = currentDate.getFullYear();
   var month = (currentDate.getMonth())+1;
   var day = currentDate.getDate();
   if ((month) < 10) month = '0'+ month;
   if ((day) < 10) day = '0'+ day;
   var currentDateStr = '' + year + month + day;
   return (date>currentDateStr);
}

//##############################################################################

function verifyLength(number){
    return(length(number));
}

//##############################################################################
/* 1200a => 0000
 * 1159a => 1159
 * 1200p/? => 1200
 * 0130p/? => 1330
 * 1159p/? => 2359
 **/

function timeHHMMX_to_HHMM(time){
     var hours = time.substring(0,2);
     var h = parseInt(hours,10);
     var minutes = time.substring(2,4);
     var last = time.substring(4);
     if(last=='a' && h==12) return('00' + minutes);
     else if((last=='p' || last=='?') && h!=12) return((h+12) + minutes);
     else return(hours + minutes);
}


/* 0000 => 12:00am
 * 0130 => 01:30am
 * 1200 => 12:00pm
 * 2359 => 11:59pm
 **/

function timeHHMM_to_HHMMxm(time){
     var hours = time.substring(0,2);
     var h = parseInt(hours);
     var minutes = time.substring(2,4);
     var last = (h<12)?'am':'pm';
     var HH = '';

     if(h>12)
     	HH = ((h - 12) <10)? ('0'+(h - 12)) : (''+(h - 12));
     else if(h == 0)
     	HH = '12';
     else
     	HH = hours;

     return (HH + ':' + minutes + last);
}


//##############################################################################

function isValidTime(json,time){
    for(var i=0;i<json.length;i++){
      if (json[i] == time)
           return true;
    }
    return false;
}

//##############################################################################

function resetVariables(){
    context.vars.film = undefined;
    context.vars.films = undefined;
    context.vars.criteria = undefined;
    context.vars.categories = undefined;
    context.vars.category = undefined;
    context.vars.cinemas = undefined;
    context.vars.cinema = undefined;
    context.vars.date = undefined;
    context.vars.cinemaResult = undefined;
    context.vars.cinemasResults = undefined;
    context.vars.filmResult = undefined;
    context.vars.filmsResults = undefined;
    context.vars.timeResult = undefined;
    context.vars.timesResult = undefined;
    context.vars.time = undefined;
    context.vars.numTickets = undefined;
    context.vars.availableTickets = undefined;
    context.vars.Show_id = undefined;
    context.vars.Show_price = undefined;
    context.vars.Capacity = undefined;
    context.vars.result = undefined;
    context.vars.totalAmount = undefined;
    context.vars.CCowner = undefined;
    context.vars.digitalSignature = undefined;
    context.vars.creditcardType = undefined;
    context.vars.creditcardNumber = undefined;
    context.vars.expirationDate = undefined;
    context.vars.last3Digits = undefined;
    context.vars.moduleResult = undefined;

}
//##############################################################################

/*function isValidLength(number,leng){
    if(number.length==leng)
        return true;
    return false;
}*/

//##############################################################################

/*function isValidExpDate(date){
     var year = date.substring(0,4);
     var month = date.substring(4,6);
     if(!isNaN(year) && !isNaN(month)) return(year + month);
     else if(isNaN(month)) return undefined;
     else return undefined;
}*/

//##############################################################################

function isValidExpDate(date){
    var currentDate = new Date();
    var year = currentDate.getFullYear();
    var month = (currentDate.getMonth())+1;
    if ((month) < 10) month = '0'+ month;
    var yyyymmCD = '' + year + month;
    var yyyymm = date.substring(0,6);
    if(!isNaN(yyyymm) && yyyymm >= yyyymmCD) return yyyymm;
    else return undefined;
}

//##############################################################################

function replaceAllSpaces(data){
return data.replace(/ /g,'');
}

//##############################################################################

function randomize(num) {
  return Math.floor(Math.random() * num);
}
//##############################################################################

function nDaysFromNow(n)
{
		var ONE_DAY = 1000 * 60 * 60 * 24;
		var today = new Date();
		var d = Date.parse(today); // convert today to milliseconds

		today.setTime(d+ (n * ONE_DAY)); // add n days to today

		var mm = ((today.getMonth()+1)<10)? '0'+(today.getMonth()+1): ''+(today.getMonth()+1);
		var dd = (today.getDate()<10)? '0'+today.getDate(): ''+today.getDate();
		var yyyy = ''+today.getFullYear();

		return yyyy+mm+dd;
}

function isTomorrow(date){
  return(nDaysFromNow(1)==date);
}

//##############################################################################

function stringToArray(data){
	var digits = new Array();
    for (var i = 0; i < data.length; i++) {
    	digits[i] = data.charAt(i);
    }
    return digits;
}

//##############################################################################

function separateNames(strData){
    return strData.split(" ");
}

//##############################################################################