

/*---------------------------------------------------------------------*/
passwd = "loginData.dat";           //Login information;
content = "infoData.dat";           //Actual Data To Be Stored;
/*-------------------------------------------------------------------- */





document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() 
{
        console.log(navigator.notification);
}

function onLoad()
{
        document.addEventListener("deviceready", onDeviceReady, true);
}

// ...additional event handlers here...
document.addEventListener("deviceready", init, false);
function init() 
{

    //This alias is a read-only pointer to the app itself
    window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + "www/index.html", gotFile, fail);

}

function fail(e) 
{
    console.log("FileSystem Error");
    console.dir(e);
}

function login()
{
    var password = document.getElementById("txt_pass").value
    if(password == "getrekt")
    {
      activate_subpage("#main")
      readFile();
      navigator.notification.alert("Welcome",alertDismissed,'D3fkon','Done')
    }else{

    alert("Wrong Password");
  }


}






function readFile(fileName,id)
{
    window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dir)
    {
            dir.getFile(fileName, {create: false}, function(fileEntry)
            {
                fileEntry.file(function(file)
                {
                    var reader = new FileReader();
                    reader.onloadend = function (evt) 
                    {
                        //alert(evt.target.result);
                        document.getElementById(id).value = evt.target.result;
                    };
                    reader.readAsText(file);
                }, function(error){
                    alert("Error: " + error.code);
                });
            }, function(error){
                alert("Error: " + error.code);
            });
    }, function(){
        alert("Error");
    });
}






function writeFile(fileName,id)
{
    window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dir)
    {
            dir.getFile(fileName, {create: true}, function(fileEntry)
            {
                fileEntry.createWriter(function(fileWriter)
                {
                    //fileWriter.seek(fileWriter.length);
                    fileWriter.write( document.getElementById(id).value + "\n");
                    navigator.notification.alert("Saved",alertDissmised,"D3fkon","Done");
                }, function(error)
                {
                    alert("Error: " + error.code);
                });
            }, function(error){
                alert("Error: " + error.code);
            });
    }, function(){
        alert("Error");
    });
}






function exitApp()
{
    navigator.app.exitApp();
}





function accountVerify()
{
    //activate_subpage('#page_16_58');
    activate_subpage("#page_SignUp");
    if(checkForFile(passwd))
    {
        activate_subpage("#page_72_39");
        //resetPage
    }
    else
        activate_subpage("#page_10_37");
}







function checkForFile(fileName)
{
    var checkFile 
    //activate_subpage("#page_10_37");
    window.requestFileSystem(LocalFileSystem.PERSISTENT,0,function(fileSystem)
    {
        fileSystem.root.getFile(fileName,{create:false},success,failure);
    },error);
    function success()
    {
        checkFile = 1;
    }
    function failure()
    {
        checkFile = 0;
    }
    function error(evt)
    {
        alert(evt.target.err.code);
    }
    if(checkFile)
    {
        return 1;
    }
    else
    {
        return 0;
    }
    
}

function signUp()
{

    var pass1 = document.getElementById("pass1").value;
    var pass2 = document.getElementById("pass2").value;
    //alert("wut" + pass1 + " " + pass2);
    if(pass1.length > 5)
    {
        if(pass1 == pass2)
        {
            navigator.notification.alert("Saved",function(){},"Password Manager");
            navigator.notification.beep(1);
            navigator.notification.vibrate(250);
            writeFile(loginData,"pass1");   //writeFile() accepts the "fileName" and "elementId" as arguments;
            activate_subpage("#page_16_58");
        }
        else
            alert("Passwords don't match!");
    }
    else
        alert("Minimum password length = 5");
}




