/* Copyright 2009 Palm, Inc.  All rights reserved. */

var PlatformType = {

    getPhoneType: function(external){
    
        if (Mojo.Controller.getAppController().assistant.gsm != undefined) 
            return;
        
        if (!external && this.timerHandle) 
            this.timerHandle = null;
        
        QDLogger.log( "PlatformType::getPhoneType", "Calling Phone Query " , this.numberOfRetries);
        // FIXME
        TelephonyCommands.getPlatformType(this.platformTypeOnSuccess.bind(this), this.platformTypeOnFailure.bind(this));
        
    },
    
    platformTypeOnSuccess: function(payload){
		QDLogger.log( "PlatformType::platformTypeOnSuccess");
   
        Mojo.Controller.getAppController().assistant.gsm = (payload.extended.platformType == 'gsm');
        QDLogger.log( "PlatformType::platformTypeOnSuccess", "gsm: " , Mojo.Controller.getAppController().assistant.gsm);
   
    },
    
    platformTypeOnFailure: function(payload){
        QDLogger.log( "PlatformType::platformTypeOnFailure");
   
		if (this.numberOfRetries <= 100) {
        
            if (!this.timerHandle) 
                this.timerHandle = setTimeout(this.getPhoneType.bind(this), 5000);
            this.numberOfRetries++;
        }
    },
    
    isGSM: function(){
        return Mojo.Controller.getAppController().assistant.gsm;
    },
    
	isGSMNorthAmerican: function() {
		return this.isGSM() && Mojo.Controller.getAppController().assistant.phoneGSMNorthAmericanSettings;
	},

    getListofEmergencyNumbers: function(){
        return Mojo.Controller.getAppController().assistant.emergencyNumbers;
    },
	
	getDefaultEmergencyNumber: function() {
		var number = "";
		var numberList = this.getListofEmergencyNumbers();
		
		if (numberList && numberList.length > 0 && numberList[0].length > 0) {
			number = numberList[0];
			QDLogger.log("getDefaultEmergencyNumber: using", number );
		} else {
			QDLogger.log("getDefaultEmergencyNumber: defaulting to TEAM AMERICA (911)");
			number = "911";
		}
		
		return number;
	},
	
	// check if number is on list of emergency numbers
	isEmergencyNumber: function(number) {
		if (!number) {
			return false;	
		}
		var list = this.getListofEmergencyNumbers();
        return (list && list.indexOf(number) >= 0)
	},
	
	// if number starts with otasp
	isOtaspNumber: function(number) {
		var otasp = Mojo.Controller.getAppController().assistant.otaspNumber;
		return otasp && number && number.startsWith(otasp);
	},
	
    numberOfRetries: 0,
    timerHandle: null,


};
